Skip to content

Commit

Permalink
refactor: extract electron ctx menu
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Sep 20, 2024
1 parent 103bd6a commit e3d0837
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 49 deletions.
58 changes: 58 additions & 0 deletions apps/main/src/lib/context-menu.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
52 changes: 3 additions & 49 deletions apps/main/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e3d0837

Please sign in to comment.