From e3d0837cc4f437819e8501aa2c85180bb82ef65f Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 20 Sep 2024 17:28:42 +0800 Subject: [PATCH] refactor: extract electron ctx menu Signed-off-by: Innei --- apps/main/src/lib/context-menu.ts | 58 +++++++++++++++++++++++++++++++ apps/main/src/window.ts | 52 ++------------------------- 2 files changed, 61 insertions(+), 49 deletions(-) create mode 100644 apps/main/src/lib/context-menu.ts diff --git a/apps/main/src/lib/context-menu.ts b/apps/main/src/lib/context-menu.ts new file mode 100644 index 000000000..0331254a7 --- /dev/null +++ b/apps/main/src/lib/context-menu.ts @@ -0,0 +1,58 @@ +import type { BrowserWindow } from "electron" +import { Menu } from "electron" + +import { t } from "./i18n" + +export const registerContextMenu = (window: BrowserWindow) => { + const handler = (_event: Electron.Event, props: Electron.ContextMenuParams) => { + const { selectionText, isEditable } = props + + const selectionMenu = Menu.buildFromTemplate([ + { role: "copy", label: t("menu.copy"), accelerator: "CmdOrCtrl+C" }, + { type: "separator" }, + { role: "selectAll", label: t("menu.selectAll"), accelerator: "CmdOrCtrl+A" }, + ]) + + const inputMenu = Menu.buildFromTemplate([ + { role: "undo", label: t("menu.undo"), accelerator: "CmdOrCtrl+Z" }, + { + role: "redo", + label: t("menu.redo"), + accelerator: "CmdOrCtrl+Shift+Z", + }, + { type: "separator" }, + { + role: "cut", + label: t("menu.cut"), + accelerator: "CmdOrCtrl+X", + }, + { + role: "copy", + label: t("menu.copy"), + accelerator: "CmdOrCtrl+C", + }, + { + role: "paste", + label: t("menu.paste"), + accelerator: "CmdOrCtrl+V", + }, + { + type: "separator", + }, + { role: "selectAll", label: t("menu.selectAll"), accelerator: "CmdOrCtrl+A" }, + ]) + + if (isEditable) { + inputMenu.popup({ + window, + }) + } else if (selectionText && selectionText.trim() !== "") { + selectionMenu.popup({ window }) + } + } + window.webContents.on("context-menu", handler) + + return () => { + window.webContents.removeListener("context-menu", handler) + } +} diff --git a/apps/main/src/window.ts b/apps/main/src/window.ts index 938ccee8d..ce4145c75 100644 --- a/apps/main/src/window.ts +++ b/apps/main/src/window.ts @@ -5,11 +5,11 @@ import { is } from "@electron-toolkit/utils" import { callGlobalContextMethod } from "@follow/shared/bridge" import { imageRefererMatches } from "@follow/shared/image" import type { BrowserWindowConstructorOptions } from "electron" -import { BrowserWindow, Menu, screen,shell } from "electron" +import { BrowserWindow, screen, shell } from "electron" import { isDev, isMacOS, isWindows11 } from "./env" import { getIconPath } from "./helper" -import { t } from "./lib/i18n" +import { registerContextMenu } from "./lib/context-menu" import { store } from "./lib/store" import { logger } from "./logger" import { cancelPollingUpdateUnreadCount, pollingUpdateUnreadCount } from "./tipc/dock" @@ -141,53 +141,7 @@ export function createWindow( }, }) }) - - window.webContents.on("context-menu", (_e, props) => { - const { selectionText, isEditable } = props - - const selectionMenu = Menu.buildFromTemplate([ - { role: "copy", label: t("menu.copy"), accelerator: "CmdOrCtrl+C" }, - { type: "separator" }, - { role: "selectAll", label: t("menu.selectAll"), accelerator: "CmdOrCtrl+A" }, - ]) - - const inputMenu = Menu.buildFromTemplate([ - { role: "undo", label: t("menu.undo"), accelerator: "CmdOrCtrl+Z" }, - { - role: "redo", - label: t("menu.redo"), - accelerator: "CmdOrCtrl+Shift+Z", - }, - { type: "separator" }, - { - role: "cut", - label: t("menu.cut"), - accelerator: "CmdOrCtrl+X", - }, - { - role: "copy", - label: t("menu.copy"), - accelerator: "CmdOrCtrl+C", - }, - { - role: "paste", - label: t("menu.paste"), - accelerator: "CmdOrCtrl+V", - }, - { - type: "separator", - }, - { role: "selectAll", label: t("menu.selectAll"), accelerator: "CmdOrCtrl+A" }, - ]) - - if (isEditable) { - inputMenu.popup({ - window, - }) - } else if (selectionText && selectionText.trim() !== "") { - selectionMenu.popup({ window }) - } - }) + registerContextMenu(window) return window }