Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Fix top bar context menu not opening on the drag region https://github.com/siyuan-note/siyuan/issues/19305

Daniel88562693c1f3
diff --git a/app/electron/main.js b/app/electron/main.js
index 39789b2d49..8194a52baf 100644
--- a/app/electron/main.js
+++ b/app/electron/main.js
@@ -655,6 +655,24 @@ const bindSpellcheckContextMenu = (contents) => {
});
};
+// 顶栏空白处是窗口拖拽区域,右键会被系统当作非客户区并弹出系统菜单,这里转交渲染进程显示自定义菜单
+// https://www.electronjs.org/docs/latest/api/base-window#event-system-context-menu-windows-linux
+const bindTopBarContextMenu = (win) => {
+ win.on("system-context-menu", (event, point) => {
+ const bounds = win.getContentBounds();
+ const dipPoint = screen.screenToDipPoint(point);
+ const zoom = win.webContents.getZoomFactor();
+ const x = (dipPoint.x - bounds.x) / zoom;
+ const y = (dipPoint.y - bounds.y) / zoom;
+ // 顶栏高度 32px,融合顶栏 42px,超出范围保留系统菜单
+ if (y < 0 || y > 42) {
+ return;
+ }
+ event.preventDefault();
+ win.webContents.send("siyuan-topbar-context-menu", {x, y});
+ });
+};
+
remote.initialize();
// Electron 相关文件夹名称改为 `SiYuan-Electron` https://github.com/siyuan-note/siyuan/issues/3349
@@ -2051,6 +2069,7 @@ const initMainWindow = (kernel = kernelPort, remoteAuthenticated = true) => {
});
remote.enable(currentWindow.webContents);
bindSpellcheckContextMenu(currentWindow.webContents);
+ bindTopBarContextMenu(currentWindow);
rememberWindowKernelTarget(currentWindow, kernelTarget);
if (resetToCenter) {
diff --git a/app/src/constants.ts b/app/src/constants.ts
index add0a445b0..8d70b05df9 100644
--- a/app/src/constants.ts
+++ b/app/src/constants.ts
@@ -69,6 +69,7 @@ export abstract class Constants {
public static readonly SIYUAN_SAVE_CLOSE: string = "siyuan-save-close";
public static readonly SIYUAN_OPEN_SETTING: string = "siyuan-open-setting";
public static readonly SIYUAN_OPEN_HELP: string = "siyuan-open-help";
+ public static readonly SIYUAN_TOPBAR_CONTEXT_MENU: string = "siyuan-topbar-context-menu"; // 顶栏拖拽区域右键转交渲染进程
// custom
public static readonly CUSTOM_REMINDER_WECHAT: string = "custom-reminder-wechat";
diff --git a/app/src/layout/topBar.ts b/app/src/layout/topBar.ts
index 8b518be8cc..b60fdad076 100644
--- a/app/src/layout/topBar.ts
+++ b/app/src/layout/topBar.ts
@@ -7,7 +7,9 @@ import {exitSiYuan, processSync} from "../dialog/processSystem";
import {goBack, goForward} from "../util/backForward";
import {syncGuide} from "../sync/syncGuide";
import {workspaceMenu} from "../menus/workspace";
+import {initTopBarMenu} from "../menus/topBar";
import {MenuItem} from "../menus/Menu";
+import {hideTooltip} from "../dialog/tooltip";
import {setMode} from "../util/assets";
import {openSetting} from "../config";
import {openSearch} from "../search/spread";
@@ -130,6 +132,12 @@ export const initBar = (app: App) => {
window.addEventListener("siyuan-entry-visibility", updateTopBarLayout);
window.addEventListener("siyuan-topbar-change", updateTopBarLayout);
processSync();
+ /// #if !BROWSER
+ ipcRenderer.on(Constants.SIYUAN_TOPBAR_CONTEXT_MENU, (event, position: IPosition) => {
+ hideTooltip();
+ initTopBarMenu().popup(position);
+ });
+ /// #endif
toolbarElement.addEventListener("click", (event: MouseEvent) => {
let target = event.target as HTMLElement;
if (typeof event.detail === "string") {