mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 14:34:13 +08:00
优化窗口事件传递
This commit is contained in:
parent
e02977b732
commit
43a7fa927e
@ -10,6 +10,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
ipcRenderer.on("window-config", (event, config) => {
|
||||
parentId = event.senderId;
|
||||
dialogType = config.type;
|
||||
windowId = config.windowId;
|
||||
|
||||
// 设置主题
|
||||
document.documentElement.setAttribute(
|
||||
@ -79,7 +80,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
const button = document.createElement("button");
|
||||
button.textContent = btn;
|
||||
button.onclick = () => {
|
||||
ipcRenderer.sendTo(parentId, "window-response", {
|
||||
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, {
|
||||
id: index,
|
||||
text: btn,
|
||||
});
|
||||
@ -137,7 +138,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
text: item,
|
||||
}
|
||||
: item;
|
||||
ipcRenderer.sendTo(parentId, "window-response", result);
|
||||
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, result);
|
||||
};
|
||||
|
||||
// 鼠标移入事件
|
||||
@ -339,7 +340,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
waitBtn.id = "wait-btn";
|
||||
waitBtn.textContent = config.text;
|
||||
waitBtn.onclick = () => {
|
||||
ipcRenderer.sendTo(parentId, "window-response", true);
|
||||
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, true);
|
||||
};
|
||||
|
||||
buttonGroup.appendChild(waitBtn);
|
||||
@ -350,7 +351,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
cancelBtn.id = "wait-cancel-btn";
|
||||
cancelBtn.innerHTML = "✕"; // 使用 × 符号
|
||||
cancelBtn.onclick = () => {
|
||||
ipcRenderer.sendTo(parentId, "window-response", false);
|
||||
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, false);
|
||||
};
|
||||
buttonGroup.appendChild(cancelBtn);
|
||||
}
|
||||
@ -384,17 +385,21 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
pauseBtn.onclick = () => {
|
||||
isPaused = !isPaused;
|
||||
pauseBtn.classList.toggle("paused", isPaused);
|
||||
ipcRenderer.sendTo(parentId, "process-pause", isPaused);
|
||||
ipcRenderer.sendTo(parentId, `process-pause-${windowId}`, isPaused);
|
||||
};
|
||||
}
|
||||
|
||||
// 添加关闭按钮点击事件
|
||||
document.getElementById("process-close-btn").onclick = () => {
|
||||
ipcRenderer.sendTo(parentId, "window-response", "close");
|
||||
ipcRenderer.sendTo(parentId, `process-close-${windowId}`);
|
||||
};
|
||||
break;
|
||||
}
|
||||
ipcRenderer.sendTo(parentId, "window-resize", calculateHeight());
|
||||
ipcRenderer.sendTo(
|
||||
parentId,
|
||||
`window-resize-${windowId}`,
|
||||
calculateHeight()
|
||||
);
|
||||
});
|
||||
|
||||
// 监听进度条更新事件
|
||||
@ -412,7 +417,11 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
const processText = document.getElementById("process-text");
|
||||
processText.innerHTML = text;
|
||||
processText.scrollTop = processText.scrollHeight;
|
||||
ipcRenderer.sendTo(parentId, "window-resize", calculateHeight());
|
||||
ipcRenderer.sendTo(
|
||||
parentId,
|
||||
`window-resize-${windowId}`,
|
||||
calculateHeight()
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@ -465,7 +474,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
break;
|
||||
}
|
||||
|
||||
ipcRenderer.sendTo(parentId, "window-response", result);
|
||||
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, result);
|
||||
};
|
||||
|
||||
const cancelDialog = () => {
|
||||
@ -489,7 +498,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
default:
|
||||
result = null;
|
||||
}
|
||||
ipcRenderer.sendTo(parentId, "window-response", result);
|
||||
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, result);
|
||||
};
|
||||
|
||||
// 取消按钮点击事件
|
||||
|
@ -49,9 +49,10 @@ const createDialog = (config, customDialogOptions = {}) => {
|
||||
|
||||
// 创建窗口
|
||||
const UBrowser = createBrowserWindow(dialogPath, dialogOptions, () => {
|
||||
const windowId = UBrowser.webContents.id;
|
||||
|
||||
const windowResponseHandler = (event, result) => {
|
||||
resolve(result);
|
||||
// 移除监听器
|
||||
UBrowser.destroy();
|
||||
};
|
||||
|
||||
@ -74,16 +75,17 @@ const createDialog = (config, customDialogOptions = {}) => {
|
||||
|
||||
// 监听子窗口返回的计算高度, 等待按钮有自己的计算逻辑
|
||||
config.type !== "wait-button" &&
|
||||
ipcRenderer.once("window-resize", windowResizeHandler);
|
||||
ipcRenderer.once(`window-resize-${windowId}`, windowResizeHandler);
|
||||
|
||||
// 监听子窗口返回的返回值
|
||||
ipcRenderer.once("window-response", windowResponseHandler);
|
||||
ipcRenderer.once(`window-response-${windowId}`, windowResponseHandler);
|
||||
|
||||
// 发送配置到子窗口
|
||||
ipcRenderer.sendTo(UBrowser.webContents.id, "window-config", {
|
||||
ipcRenderer.sendTo(windowId, `window-config`, {
|
||||
...config,
|
||||
isDark: utools.isDarkColors(),
|
||||
platform,
|
||||
windowId,
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -354,7 +356,6 @@ const showProcessBar = async (options = {}) => {
|
||||
|
||||
// 创建事件处理器
|
||||
windowResizeHandler = (event, height) => {
|
||||
if (event.senderId !== windowId) return;
|
||||
const bounds = UBrowser.getBounds();
|
||||
const y = Math.round(bounds.y - (height - bounds.height));
|
||||
const newBounds = {
|
||||
@ -370,32 +371,40 @@ const showProcessBar = async (options = {}) => {
|
||||
// 监听暂停/恢复事件
|
||||
if (onPause && onResume) {
|
||||
processPauseHandler = (event, isPaused) => {
|
||||
if (event.senderId !== windowId) return;
|
||||
if (isPaused) {
|
||||
onPause();
|
||||
} else {
|
||||
onResume();
|
||||
}
|
||||
};
|
||||
ipcRenderer.on("process-pause", processPauseHandler);
|
||||
ipcRenderer.on(`process-pause-${windowId}`, processPauseHandler);
|
||||
}
|
||||
|
||||
// 监听子窗口返回的计算高度
|
||||
ipcRenderer.on("window-resize", windowResizeHandler);
|
||||
ipcRenderer.on(`window-resize-${windowId}`, windowResizeHandler);
|
||||
|
||||
// 监听对话框结果
|
||||
ipcRenderer.once("window-response", (event, result) => {
|
||||
if (event.senderId !== windowId) return;
|
||||
if (result === "close" && typeof onClose === "function") {
|
||||
const closeProcessBar = () => {
|
||||
if (typeof onClose === "function") {
|
||||
onClose();
|
||||
}
|
||||
// 清理所有事件监听器
|
||||
ipcRenderer.removeListener("window-resize", windowResizeHandler);
|
||||
ipcRenderer.removeListener(
|
||||
`window-resize-${windowId}`,
|
||||
windowResizeHandler
|
||||
);
|
||||
if (processPauseHandler) {
|
||||
ipcRenderer.removeListener("process-pause", processPauseHandler);
|
||||
ipcRenderer.removeListener(
|
||||
`process-pause-${windowId}`,
|
||||
processPauseHandler
|
||||
);
|
||||
}
|
||||
lastProcessBar = null;
|
||||
UBrowser.destroy();
|
||||
};
|
||||
|
||||
// 监听对话框结果
|
||||
ipcRenderer.once(`process-close-${windowId}`, () => {
|
||||
closeProcessBar();
|
||||
});
|
||||
|
||||
// 发送配置到子窗口
|
||||
@ -407,22 +416,12 @@ const showProcessBar = async (options = {}) => {
|
||||
platform: process.platform,
|
||||
showPause: Boolean(onPause && onResume),
|
||||
isLoading: value === undefined,
|
||||
windowId,
|
||||
});
|
||||
|
||||
const processBar = {
|
||||
id: windowId,
|
||||
close: () => {
|
||||
if (typeof onClose === "function") {
|
||||
onClose();
|
||||
}
|
||||
// 清理所有事件监听器
|
||||
ipcRenderer.removeListener("window-resize", windowResizeHandler);
|
||||
if (processPauseHandler) {
|
||||
ipcRenderer.removeListener("process-pause", processPauseHandler);
|
||||
}
|
||||
lastProcessBar = null;
|
||||
UBrowser.destroy();
|
||||
},
|
||||
close: closeProcessBar,
|
||||
};
|
||||
|
||||
lastProcessBar = processBar;
|
||||
|
Loading…
x
Reference in New Issue
Block a user