Unverified 提交 2def841f authored 作者: Will Chen's avatar Will Chen 提交者: GitHub

Context menu (right-click copy, paste, etc.) (#1492)

上级 4180b5b6
import { app, BrowserWindow, dialog } from "electron";
import { app, BrowserWindow, dialog, Menu } from "electron";
import * as path from "node:path";
import { registerIpcHandlers } from "./ipc/ipc_host";
import dotenv from "dotenv";
......@@ -163,6 +163,47 @@ const createWindow = () => {
// Open the DevTools.
mainWindow.webContents.openDevTools();
}
// Enable native context menu on right-click
mainWindow.webContents.on("context-menu", (event, params) => {
// Prevent any default behavior and show our own menu
event.preventDefault();
const template: Electron.MenuItemConstructorOptions[] = [];
if (params.isEditable) {
template.push(
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "delete" },
{ type: "separator" },
{ role: "selectAll" },
);
} else {
if (params.selectionText && params.selectionText.length > 0) {
template.push({ role: "copy" });
}
template.push({ role: "selectAll" });
}
if (process.env.NODE_ENV === "development") {
template.push(
{ type: "separator" },
{
label: "Inspect Element",
click: () =>
mainWindow?.webContents.inspectElement(params.x, params.y),
},
);
}
const menu = Menu.buildFromTemplate(template);
menu.popup({ window: mainWindow! });
});
};
const gotTheLock = app.requestSingleInstanceLock();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论