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) => {
|
ipcRenderer.on("window-config", (event, config) => {
|
||||||
parentId = event.senderId;
|
parentId = event.senderId;
|
||||||
dialogType = config.type;
|
dialogType = config.type;
|
||||||
|
windowId = config.windowId;
|
||||||
|
|
||||||
// 设置主题
|
// 设置主题
|
||||||
document.documentElement.setAttribute(
|
document.documentElement.setAttribute(
|
||||||
@ -79,7 +80,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
const button = document.createElement("button");
|
const button = document.createElement("button");
|
||||||
button.textContent = btn;
|
button.textContent = btn;
|
||||||
button.onclick = () => {
|
button.onclick = () => {
|
||||||
ipcRenderer.sendTo(parentId, "window-response", {
|
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, {
|
||||||
id: index,
|
id: index,
|
||||||
text: btn,
|
text: btn,
|
||||||
});
|
});
|
||||||
@ -137,7 +138,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
text: item,
|
text: item,
|
||||||
}
|
}
|
||||||
: 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.id = "wait-btn";
|
||||||
waitBtn.textContent = config.text;
|
waitBtn.textContent = config.text;
|
||||||
waitBtn.onclick = () => {
|
waitBtn.onclick = () => {
|
||||||
ipcRenderer.sendTo(parentId, "window-response", true);
|
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
buttonGroup.appendChild(waitBtn);
|
buttonGroup.appendChild(waitBtn);
|
||||||
@ -350,7 +351,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
cancelBtn.id = "wait-cancel-btn";
|
cancelBtn.id = "wait-cancel-btn";
|
||||||
cancelBtn.innerHTML = "✕"; // 使用 × 符号
|
cancelBtn.innerHTML = "✕"; // 使用 × 符号
|
||||||
cancelBtn.onclick = () => {
|
cancelBtn.onclick = () => {
|
||||||
ipcRenderer.sendTo(parentId, "window-response", false);
|
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, false);
|
||||||
};
|
};
|
||||||
buttonGroup.appendChild(cancelBtn);
|
buttonGroup.appendChild(cancelBtn);
|
||||||
}
|
}
|
||||||
@ -384,17 +385,21 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
pauseBtn.onclick = () => {
|
pauseBtn.onclick = () => {
|
||||||
isPaused = !isPaused;
|
isPaused = !isPaused;
|
||||||
pauseBtn.classList.toggle("paused", 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 = () => {
|
document.getElementById("process-close-btn").onclick = () => {
|
||||||
ipcRenderer.sendTo(parentId, "window-response", "close");
|
ipcRenderer.sendTo(parentId, `process-close-${windowId}`);
|
||||||
};
|
};
|
||||||
break;
|
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");
|
const processText = document.getElementById("process-text");
|
||||||
processText.innerHTML = text;
|
processText.innerHTML = text;
|
||||||
processText.scrollTop = processText.scrollHeight;
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ipcRenderer.sendTo(parentId, "window-response", result);
|
ipcRenderer.sendTo(parentId, `window-response-${windowId}`, result);
|
||||||
};
|
};
|
||||||
|
|
||||||
const cancelDialog = () => {
|
const cancelDialog = () => {
|
||||||
@ -489,7 +498,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
default:
|
default:
|
||||||
result = null;
|
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 UBrowser = createBrowserWindow(dialogPath, dialogOptions, () => {
|
||||||
|
const windowId = UBrowser.webContents.id;
|
||||||
|
|
||||||
const windowResponseHandler = (event, result) => {
|
const windowResponseHandler = (event, result) => {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
// 移除监听器
|
|
||||||
UBrowser.destroy();
|
UBrowser.destroy();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -74,16 +75,17 @@ const createDialog = (config, customDialogOptions = {}) => {
|
|||||||
|
|
||||||
// 监听子窗口返回的计算高度, 等待按钮有自己的计算逻辑
|
// 监听子窗口返回的计算高度, 等待按钮有自己的计算逻辑
|
||||||
config.type !== "wait-button" &&
|
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,
|
...config,
|
||||||
isDark: utools.isDarkColors(),
|
isDark: utools.isDarkColors(),
|
||||||
platform,
|
platform,
|
||||||
|
windowId,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -354,7 +356,6 @@ const showProcessBar = async (options = {}) => {
|
|||||||
|
|
||||||
// 创建事件处理器
|
// 创建事件处理器
|
||||||
windowResizeHandler = (event, height) => {
|
windowResizeHandler = (event, height) => {
|
||||||
if (event.senderId !== windowId) return;
|
|
||||||
const bounds = UBrowser.getBounds();
|
const bounds = UBrowser.getBounds();
|
||||||
const y = Math.round(bounds.y - (height - bounds.height));
|
const y = Math.round(bounds.y - (height - bounds.height));
|
||||||
const newBounds = {
|
const newBounds = {
|
||||||
@ -370,32 +371,40 @@ const showProcessBar = async (options = {}) => {
|
|||||||
// 监听暂停/恢复事件
|
// 监听暂停/恢复事件
|
||||||
if (onPause && onResume) {
|
if (onPause && onResume) {
|
||||||
processPauseHandler = (event, isPaused) => {
|
processPauseHandler = (event, isPaused) => {
|
||||||
if (event.senderId !== windowId) return;
|
|
||||||
if (isPaused) {
|
if (isPaused) {
|
||||||
onPause();
|
onPause();
|
||||||
} else {
|
} else {
|
||||||
onResume();
|
onResume();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ipcRenderer.on("process-pause", processPauseHandler);
|
ipcRenderer.on(`process-pause-${windowId}`, processPauseHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 监听子窗口返回的计算高度
|
// 监听子窗口返回的计算高度
|
||||||
ipcRenderer.on("window-resize", windowResizeHandler);
|
ipcRenderer.on(`window-resize-${windowId}`, windowResizeHandler);
|
||||||
|
|
||||||
// 监听对话框结果
|
const closeProcessBar = () => {
|
||||||
ipcRenderer.once("window-response", (event, result) => {
|
if (typeof onClose === "function") {
|
||||||
if (event.senderId !== windowId) return;
|
|
||||||
if (result === "close" && typeof onClose === "function") {
|
|
||||||
onClose();
|
onClose();
|
||||||
}
|
}
|
||||||
// 清理所有事件监听器
|
// 清理所有事件监听器
|
||||||
ipcRenderer.removeListener("window-resize", windowResizeHandler);
|
ipcRenderer.removeListener(
|
||||||
|
`window-resize-${windowId}`,
|
||||||
|
windowResizeHandler
|
||||||
|
);
|
||||||
if (processPauseHandler) {
|
if (processPauseHandler) {
|
||||||
ipcRenderer.removeListener("process-pause", processPauseHandler);
|
ipcRenderer.removeListener(
|
||||||
|
`process-pause-${windowId}`,
|
||||||
|
processPauseHandler
|
||||||
|
);
|
||||||
}
|
}
|
||||||
lastProcessBar = null;
|
lastProcessBar = null;
|
||||||
UBrowser.destroy();
|
UBrowser.destroy();
|
||||||
|
};
|
||||||
|
|
||||||
|
// 监听对话框结果
|
||||||
|
ipcRenderer.once(`process-close-${windowId}`, () => {
|
||||||
|
closeProcessBar();
|
||||||
});
|
});
|
||||||
|
|
||||||
// 发送配置到子窗口
|
// 发送配置到子窗口
|
||||||
@ -407,22 +416,12 @@ const showProcessBar = async (options = {}) => {
|
|||||||
platform: process.platform,
|
platform: process.platform,
|
||||||
showPause: Boolean(onPause && onResume),
|
showPause: Boolean(onPause && onResume),
|
||||||
isLoading: value === undefined,
|
isLoading: value === undefined,
|
||||||
|
windowId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const processBar = {
|
const processBar = {
|
||||||
id: windowId,
|
id: windowId,
|
||||||
close: () => {
|
close: closeProcessBar,
|
||||||
if (typeof onClose === "function") {
|
|
||||||
onClose();
|
|
||||||
}
|
|
||||||
// 清理所有事件监听器
|
|
||||||
ipcRenderer.removeListener("window-resize", windowResizeHandler);
|
|
||||||
if (processPauseHandler) {
|
|
||||||
ipcRenderer.removeListener("process-pause", processPauseHandler);
|
|
||||||
}
|
|
||||||
lastProcessBar = null;
|
|
||||||
UBrowser.destroy();
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
lastProcessBar = processBar;
|
lastProcessBar = processBar;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user