2024-08-27 07:03:43 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-12-07 00:33:00 +01:00
|
|
|
import { waveEventSubscribe } from "@/app/store/wps";
|
|
|
|
import { RpcApi } from "@/app/store/wshclientapi";
|
2024-08-27 07:03:43 +02:00
|
|
|
import * as electron from "electron";
|
|
|
|
import { fireAndForget } from "../frontend/util/util";
|
2024-12-02 19:56:56 +01:00
|
|
|
import { clearTabCache } from "./emain-tabview";
|
2024-12-07 00:33:00 +01:00
|
|
|
import {
|
|
|
|
createNewWaveWindow,
|
|
|
|
createWorkspace,
|
|
|
|
focusedWaveWindow,
|
2024-12-07 04:10:34 +01:00
|
|
|
getWaveWindowByWorkspaceId,
|
2024-12-07 00:33:00 +01:00
|
|
|
relaunchBrowserWindows,
|
|
|
|
WaveBrowserWindow,
|
|
|
|
} from "./emain-window";
|
|
|
|
import { ElectronWshClient } from "./emain-wsh";
|
2024-08-27 07:03:43 +02:00
|
|
|
import { unamePlatform } from "./platform";
|
|
|
|
import { updater } from "./updater";
|
|
|
|
|
|
|
|
type AppMenuCallbacks = {
|
|
|
|
createNewWaveWindow: () => Promise<void>;
|
|
|
|
relaunchBrowserWindows: () => Promise<void>;
|
|
|
|
};
|
|
|
|
|
2024-10-05 01:34:05 +02:00
|
|
|
function getWindowWebContents(window: electron.BaseWindow): electron.WebContents {
|
|
|
|
if (window == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-10-17 23:34:02 +02:00
|
|
|
if (window instanceof electron.BaseWindow) {
|
|
|
|
const waveWin = window as WaveBrowserWindow;
|
|
|
|
if (waveWin.activeTabView) {
|
|
|
|
return waveWin.activeTabView.webContents;
|
|
|
|
}
|
|
|
|
return null;
|
2024-10-05 01:34:05 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-12-07 04:10:34 +01:00
|
|
|
async function getWorkspaceMenu(ww?: WaveBrowserWindow): Promise<Electron.MenuItemConstructorOptions[]> {
|
2024-12-07 00:33:00 +01:00
|
|
|
const workspaceList = await RpcApi.WorkspaceListCommand(ElectronWshClient);
|
|
|
|
console.log("workspaceList:", workspaceList);
|
|
|
|
const workspaceMenu: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{
|
2024-12-13 02:21:09 +01:00
|
|
|
label: "Create Workspace",
|
|
|
|
click: (_, window) => fireAndForget(() => createWorkspace((window as WaveBrowserWindow) ?? ww)),
|
2024-12-07 00:33:00 +01:00
|
|
|
},
|
|
|
|
];
|
2024-12-07 01:35:01 +01:00
|
|
|
function getWorkspaceSwitchAccelerator(i: number): string {
|
2024-12-11 21:52:15 +01:00
|
|
|
if (i < 9) {
|
2024-12-13 02:21:09 +01:00
|
|
|
return unamePlatform == "darwin" ? `Command+Control+${i + 1}` : `Alt+Control+${i + 1}`;
|
2024-12-07 01:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
2024-12-07 00:33:00 +01:00
|
|
|
workspaceList?.length &&
|
|
|
|
workspaceMenu.push(
|
|
|
|
{ type: "separator" },
|
2024-12-07 01:35:01 +01:00
|
|
|
...workspaceList.map<Electron.MenuItemConstructorOptions>((workspace, i) => {
|
2024-12-07 00:33:00 +01:00
|
|
|
return {
|
2024-12-13 02:21:09 +01:00
|
|
|
label: `${workspace.workspacedata.name}`,
|
2024-12-07 00:33:00 +01:00
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
((window as WaveBrowserWindow) ?? ww)?.switchWorkspace(workspace.workspacedata.oid);
|
2024-12-07 00:33:00 +01:00
|
|
|
},
|
2024-12-07 01:35:01 +01:00
|
|
|
accelerator: getWorkspaceSwitchAccelerator(i),
|
2024-12-07 00:33:00 +01:00
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
return workspaceMenu;
|
|
|
|
}
|
|
|
|
|
2024-12-07 04:10:34 +01:00
|
|
|
async function getAppMenu(callbacks: AppMenuCallbacks, workspaceId?: string): Promise<Electron.Menu> {
|
|
|
|
const ww = workspaceId && getWaveWindowByWorkspaceId(workspaceId);
|
2024-08-27 07:03:43 +02:00
|
|
|
const fileMenu: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{
|
|
|
|
label: "New Window",
|
|
|
|
accelerator: "CommandOrControl+Shift+N",
|
|
|
|
click: () => fireAndForget(callbacks.createNewWaveWindow),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "close",
|
|
|
|
accelerator: "", // clear the accelerator
|
|
|
|
click: () => {
|
2024-12-02 19:56:56 +01:00
|
|
|
focusedWaveWindow?.close();
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const appMenu: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{
|
|
|
|
label: "About Wave Terminal",
|
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
getWindowWebContents(window ?? ww)?.send("menu-item-about");
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Check for Updates",
|
|
|
|
click: () => {
|
|
|
|
fireAndForget(() => updater?.checkForUpdates(true));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "separator",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
if (unamePlatform === "darwin") {
|
|
|
|
appMenu.push(
|
|
|
|
{
|
|
|
|
role: "services",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "separator",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "hide",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "hideOthers",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "separator",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
appMenu.push({
|
|
|
|
role: "quit",
|
|
|
|
});
|
|
|
|
const editMenu: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{
|
|
|
|
role: "undo",
|
|
|
|
accelerator: unamePlatform === "darwin" ? "Command+Z" : "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "redo",
|
|
|
|
accelerator: unamePlatform === "darwin" ? "Command+Shift+Z" : "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "separator",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "cut",
|
|
|
|
accelerator: unamePlatform === "darwin" ? "Command+X" : "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "copy",
|
|
|
|
accelerator: unamePlatform === "darwin" ? "Command+C" : "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "paste",
|
|
|
|
accelerator: unamePlatform === "darwin" ? "Command+V" : "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "pasteAndMatchStyle",
|
|
|
|
accelerator: unamePlatform === "darwin" ? "Command+Shift+V" : "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "delete",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "selectAll",
|
|
|
|
accelerator: unamePlatform === "darwin" ? "Command+A" : "",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-10-30 21:54:36 +01:00
|
|
|
const devToolsAccel = unamePlatform === "darwin" ? "Option+Command+I" : "Alt+Shift+I";
|
2024-08-27 07:03:43 +02:00
|
|
|
const viewMenu: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{
|
2024-10-17 23:34:02 +02:00
|
|
|
label: "Reload Tab",
|
|
|
|
accelerator: "Shift+CommandOrControl+R",
|
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
getWindowWebContents(window ?? ww)?.reloadIgnoringCache();
|
2024-10-17 23:34:02 +02:00
|
|
|
},
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Relaunch All Windows",
|
|
|
|
click: () => {
|
|
|
|
callbacks.relaunchBrowserWindows();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-17 23:34:02 +02:00
|
|
|
label: "Clear Tab Cache",
|
|
|
|
click: () => {
|
|
|
|
clearTabCache();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Toggle DevTools",
|
|
|
|
accelerator: devToolsAccel,
|
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
let wc = getWindowWebContents(window ?? ww);
|
2024-10-17 23:34:02 +02:00
|
|
|
wc?.toggleDevTools();
|
|
|
|
},
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "separator",
|
|
|
|
},
|
|
|
|
{
|
2024-11-11 22:10:38 +01:00
|
|
|
label: "Reset Zoom",
|
2024-08-27 07:03:43 +02:00
|
|
|
accelerator: "CommandOrControl+0",
|
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
getWindowWebContents(window ?? ww)?.setZoomFactor(1);
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Zoom In",
|
|
|
|
accelerator: "CommandOrControl+=",
|
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
const wc = getWindowWebContents(window ?? ww);
|
2024-10-05 01:34:05 +02:00
|
|
|
if (wc == null) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-17 23:34:02 +02:00
|
|
|
if (wc.getZoomFactor() >= 5) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-05 01:34:05 +02:00
|
|
|
wc.setZoomFactor(wc.getZoomFactor() + 0.2);
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Zoom In (hidden)",
|
|
|
|
accelerator: "CommandOrControl+Shift+=",
|
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
const wc = getWindowWebContents(window ?? ww);
|
2024-10-05 01:34:05 +02:00
|
|
|
if (wc == null) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-17 23:34:02 +02:00
|
|
|
if (wc.getZoomFactor() >= 5) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-05 01:34:05 +02:00
|
|
|
wc.setZoomFactor(wc.getZoomFactor() + 0.2);
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
|
|
|
visible: false,
|
|
|
|
acceleratorWorksWhenHidden: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Zoom Out",
|
|
|
|
accelerator: "CommandOrControl+-",
|
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
const wc = getWindowWebContents(window ?? ww);
|
2024-10-05 01:34:05 +02:00
|
|
|
if (wc == null) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-17 23:34:02 +02:00
|
|
|
if (wc.getZoomFactor() <= 0.2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wc.setZoomFactor(wc.getZoomFactor() - 0.2);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: "Zoom Out (hidden)",
|
|
|
|
accelerator: "CommandOrControl+Shift+-",
|
|
|
|
click: (_, window) => {
|
2024-12-07 04:10:34 +01:00
|
|
|
const wc = getWindowWebContents(window ?? ww);
|
2024-10-17 23:34:02 +02:00
|
|
|
if (wc == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (wc.getZoomFactor() <= 0.2) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-05 01:34:05 +02:00
|
|
|
wc.setZoomFactor(wc.getZoomFactor() - 0.2);
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
2024-10-17 23:34:02 +02:00
|
|
|
visible: false,
|
|
|
|
acceleratorWorksWhenHidden: true,
|
2024-08-27 07:03:43 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "separator",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "togglefullscreen",
|
|
|
|
},
|
|
|
|
];
|
2024-12-11 19:48:50 +01:00
|
|
|
let workspaceMenu: Electron.MenuItemConstructorOptions[] = null;
|
|
|
|
try {
|
|
|
|
workspaceMenu = await getWorkspaceMenu();
|
|
|
|
} catch (e) {
|
|
|
|
console.error("getWorkspaceMenu error:", e);
|
|
|
|
}
|
2024-08-27 07:03:43 +02:00
|
|
|
const windowMenu: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{ role: "minimize", accelerator: "" },
|
|
|
|
{ role: "zoom" },
|
|
|
|
{ type: "separator" },
|
|
|
|
{ role: "front" },
|
|
|
|
{ type: "separator" },
|
|
|
|
{ role: "window" },
|
|
|
|
];
|
|
|
|
const menuTemplate: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
{
|
|
|
|
role: "appMenu",
|
|
|
|
submenu: appMenu,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "fileMenu",
|
|
|
|
submenu: fileMenu,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "editMenu",
|
|
|
|
submenu: editMenu,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "viewMenu",
|
|
|
|
submenu: viewMenu,
|
|
|
|
},
|
2024-12-11 19:48:50 +01:00
|
|
|
];
|
|
|
|
if (workspaceMenu != null) {
|
|
|
|
menuTemplate.push({
|
2024-12-07 00:33:00 +01:00
|
|
|
label: "Workspace",
|
|
|
|
id: "workspace-menu",
|
|
|
|
submenu: workspaceMenu,
|
2024-12-11 19:48:50 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
menuTemplate.push({
|
|
|
|
role: "windowMenu",
|
|
|
|
submenu: windowMenu,
|
|
|
|
});
|
2024-08-27 07:03:43 +02:00
|
|
|
return electron.Menu.buildFromTemplate(menuTemplate);
|
|
|
|
}
|
|
|
|
|
2024-12-07 04:10:34 +01:00
|
|
|
export function instantiateAppMenu(workspaceId?: string): Promise<electron.Menu> {
|
|
|
|
return getAppMenu(
|
|
|
|
{
|
|
|
|
createNewWaveWindow,
|
|
|
|
relaunchBrowserWindows,
|
|
|
|
},
|
|
|
|
workspaceId
|
|
|
|
);
|
2024-12-07 00:33:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function makeAppMenu() {
|
|
|
|
fireAndForget(async () => {
|
|
|
|
const menu = await instantiateAppMenu();
|
|
|
|
electron.Menu.setApplicationMenu(menu);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
waveEventSubscribe({
|
|
|
|
eventType: "workspace:update",
|
|
|
|
handler: makeAppMenu,
|
|
|
|
});
|
|
|
|
|
2024-12-07 04:10:34 +01:00
|
|
|
function convertMenuDefArrToMenu(workspaceId: string, menuDefArr: ElectronContextMenuItem[]): electron.Menu {
|
|
|
|
const menuItems: electron.MenuItem[] = [];
|
|
|
|
for (const menuDef of menuDefArr) {
|
|
|
|
const menuItemTemplate: electron.MenuItemConstructorOptions = {
|
|
|
|
role: menuDef.role as any,
|
|
|
|
label: menuDef.label,
|
|
|
|
type: menuDef.type,
|
|
|
|
click: (_, window) => {
|
|
|
|
const ww = (window as WaveBrowserWindow) ?? getWaveWindowByWorkspaceId(workspaceId);
|
|
|
|
if (!ww) {
|
|
|
|
console.error("invalid window for context menu click handler:", ww, window, workspaceId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ww?.activeTabView?.webContents?.send("contextmenu-click", menuDef.id);
|
|
|
|
},
|
|
|
|
checked: menuDef.checked,
|
|
|
|
};
|
|
|
|
if (menuDef.submenu != null) {
|
|
|
|
menuItemTemplate.submenu = convertMenuDefArrToMenu(workspaceId, menuDef.submenu);
|
|
|
|
}
|
|
|
|
const menuItem = new electron.MenuItem(menuItemTemplate);
|
|
|
|
menuItems.push(menuItem);
|
|
|
|
}
|
|
|
|
return electron.Menu.buildFromTemplate(menuItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
electron.ipcMain.on("contextmenu-show", (event, workspaceId: string, menuDefArr?: ElectronContextMenuItem[]) => {
|
|
|
|
if (menuDefArr?.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fireAndForget(async () => {
|
|
|
|
const menu = menuDefArr
|
|
|
|
? convertMenuDefArrToMenu(workspaceId, menuDefArr)
|
|
|
|
: await instantiateAppMenu(workspaceId);
|
|
|
|
menu.popup();
|
|
|
|
});
|
|
|
|
event.returnValue = true;
|
|
|
|
});
|
|
|
|
|
2024-08-27 07:03:43 +02:00
|
|
|
export { getAppMenu };
|