Browse Source

:art: fix https://github.com/siyuan-note/siyuan/issues/10755

Vanessa 1 year ago
parent
commit
10c181659d
3 changed files with 39 additions and 28 deletions
  1. 20 24
      app/electron/main.js
  2. 18 3
      app/src/config/keymap.ts
  3. 1 1
      app/src/protyle/export/index.ts

+ 20 - 24
app/electron/main.js

@@ -433,7 +433,6 @@ const initKernel = (workspace, port, lang) => {
             backgroundColor: "#1e1e1e",
             icon: path.join(appDir, "stage", "icon-large.png"),
         });
-
         let bootIndex = path.join(appDir, "app", "electron", "boot.html");
         if (isDevEnv) {
             bootIndex = path.join(appDir, "electron", "boot.html");
@@ -682,12 +681,9 @@ app.whenReady().then(() => {
 
         resetTrayMenu(tray, lang, mainWindow);
     };
+
     const getWindowByContentId = (id) => {
-        const wnd = BrowserWindow.getAllWindows().find((win) => win.webContents.id === id);
-        if (!wnd) {
-            return null;
-        }
-        return BrowserWindow.fromId(wnd.id);
+        return BrowserWindow.getAllWindows().find((win) => win.webContents.id === id);
     };
 
     ipcMain.on("siyuan-open-folder", (event, filePath) => {
@@ -700,7 +696,7 @@ app.whenReady().then(() => {
         if (data.cmd === "showOpenDialog") {
             return dialog.showOpenDialog(data);
         }
-        if (data.cmd === "getCurrentWindowId") {
+        if (data.cmd === "getContentsId") {
             return event.sender.id;
         }
         if (data.cmd === "setProxy") {
@@ -796,8 +792,8 @@ app.whenReady().then(() => {
             case "openDevTools":
                 event.sender.openDevTools({mode: "bottom"});
                 break;
-            case "unregisterAll":
-                globalShortcut.unregisterAll();
+            case "unregisterGlobalShortcut":
+                globalShortcut.unregister(hotKey2Electron(data.accelerator));
                 break;
             case "show":
                 if (!currentWindow) {
@@ -1036,7 +1032,6 @@ app.whenReady().then(() => {
         await net.fetch(getServer(data.port) + "/api/system/uiproc?pid=" + process.pid, {method: "POST"});
     });
     ipcMain.on("siyuan-hotkey", (event, data) => {
-        globalShortcut.unregisterAll();
         if (!data.hotkeys || data.hotkeys.length === 0) {
             return;
         }
@@ -1045,31 +1040,32 @@ app.whenReady().then(() => {
             if (!shortcut) {
                 return;
             }
+            if (globalShortcut.isRegistered(shortcut)) {
+                globalShortcut.unregister(shortcut)
+            }
             if (index === 0) {
                 globalShortcut.register(shortcut, () => {
-                    workspaces.forEach(workspaceItem => {
+                    workspaces.find(workspaceItem => {
                         const mainWindow = workspaceItem.browserWindow;
-                        if (mainWindow.isMinimized()) {
-                            mainWindow.restore();
-                            mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456
-                        } else {
-                            if (mainWindow.isVisible()) {
-                                if (1 === workspaces.length) { // 改进 `Alt+M` 激活窗口 https://github.com/siyuan-note/siyuan/issues/7258
+                        if (event.sender.id === mainWindow.webContents.id) {
+                            if (mainWindow.isMinimized()) {
+                                mainWindow.restore();
+                                mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456
+                            } else {
+                                if (mainWindow.isVisible()) {
                                     if (!mainWindow.isFocused()) {
                                         mainWindow.show();
                                     } else {
                                         hideWindow(mainWindow);
                                     }
                                 } else {
-                                    hideWindow(mainWindow);
+                                    mainWindow.show();
                                 }
-                            } else {
-                                mainWindow.show();
                             }
-                        }
-
-                        if ("win32" === process.platform || "linux" === process.platform) {
-                            resetTrayMenu(workspaceItem.tray, data.languages, mainWindow);
+                            if ("win32" === process.platform || "linux" === process.platform) {
+                                resetTrayMenu(workspaceItem.tray, data.languages, mainWindow);
+                            }
+                            return true;
                         }
                     });
                 });

+ 18 - 3
app/src/config/keymap.ts

@@ -55,7 +55,7 @@ export const keymap = {
                 if (!dockConfig.hotkey) {
                     return;
                 }
-                const dockKeymap =  window.siyuan.config.keymap.plugin[item.name][key];
+                const dockKeymap = window.siyuan.config.keymap.plugin[item.name][key];
                 const keyValue = updateHotkeyTip(dockKeymap.custom);
                 commandHTML += `<label class="b3-list-item b3-list-item--narrow b3-list-item--hide-action">
     <span class="b3-list-item__text">${dockConfig.title}</span>
@@ -186,6 +186,7 @@ export const keymap = {
     },
     _setkeymap(app: App) {
         const data: Config.IKeymap = JSON.parse(JSON.stringify(Constants.SIYUAN_KEYMAP));
+        const oldToggleWin = window.siyuan.config.keymap.general.toggleWin.custom
         keymap.element.querySelectorAll("label.b3-list-item input").forEach((item) => {
             const keys = item.getAttribute("data-key").split(Constants.ZWSP);
             const newHotkey = item.getAttribute("data-value");
@@ -211,6 +212,12 @@ export const keymap = {
         fetchPost("/api/setting/setKeymap", {
             data
         }, () => {
+            if (oldToggleWin !== window.siyuan.config.keymap.general.toggleWin.custom) {
+                ipcRenderer.send(Constants.SIYUAN_CMD, {
+                    cmd: "unregisterGlobalShortcut",
+                    accelerator: oldToggleWin
+                });
+            }
             sendGlobalShortcut(app);
         });
     },
@@ -298,7 +305,8 @@ export const keymap = {
         /// #if !BROWSER
         searchKeymapElement.addEventListener("focus", () => {
             ipcRenderer.send(Constants.SIYUAN_CMD, {
-                cmd: "unregisterAll",
+                cmd: "unregisterGlobalShortcut",
+                accelerator: window.siyuan.config.keymap.general.toggleWin.custom
             });
         });
         /// #endif
@@ -325,6 +333,12 @@ export const keymap = {
                 fetchPost("/api/setting/setKeymap", {
                     data: Constants.SIYUAN_KEYMAP,
                 }, () => {
+                    if (window.siyuan.config.keymap.general.toggleWin.default !== window.siyuan.config.keymap.general.toggleWin.custom) {
+                        ipcRenderer.send(Constants.SIYUAN_CMD, {
+                            cmd: "unregisterGlobalShortcut",
+                            accelerator: window.siyuan.config.keymap.general.toggleWin.custom
+                        });
+                    }
                     window.location.reload();
                     sendGlobalShortcut(app);
                 });
@@ -444,7 +458,8 @@ export const keymap = {
             /// #if !BROWSER
             item.addEventListener("focus", () => {
                 ipcRenderer.send(Constants.SIYUAN_CMD, {
-                    cmd: "unregisterAll",
+                    cmd: "unregisterGlobalShortcut",
+                    accelerator: window.siyuan.config.keymap.general.toggleWin.custom
                 });
             });
             /// #endif

+ 1 - 1
app/src/protyle/export/index.ts

@@ -90,7 +90,7 @@ const renderPDF = async (id: string) => {
         themeStyle = `<link rel="stylesheet" type="text/css" id="themeStyle" href="${servePath}/appearance/themes/${window.siyuan.config.appearance.themeLight}/theme.css?${Constants.SIYUAN_VERSION}"/>`;
     }
     const currentWindowId = await ipcRenderer.invoke(Constants.SIYUAN_GET, {
-        cmd: "getCurrentWindowId",
+        cmd: "getContentsId",
     });
     // data-theme-mode="light" https://github.com/siyuan-note/siyuan/issues/7379
     const html = `<!DOCTYPE html>