Skip to content

install special new handlers for Cmd+N and Cmd+T when no windows are open #1839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions emain/emain-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0

import { EventEmitter } from "events";

interface GlobalEvents {
"windows-updated": () => void; // emitted whenever a window is opened/closed
}

class GlobalEventEmitter extends EventEmitter {
emit<K extends keyof GlobalEvents>(event: K, ...args: Parameters<GlobalEvents[K]>): boolean {
return super.emit(event, ...args);
}

on<K extends keyof GlobalEvents>(event: K, listener: GlobalEvents[K]): this {
return super.on(event, listener);
}

once<K extends keyof GlobalEvents>(event: K, listener: GlobalEvents[K]): this {
return super.once(event, listener);
}

off<K extends keyof GlobalEvents>(event: K, listener: GlobalEvents[K]): this {
return super.off(event, listener);
}
}

const globalEvents = new GlobalEventEmitter();

export { globalEvents };
3 changes: 3 additions & 0 deletions emain/emain-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClientService, ObjectService, WindowService, WorkspaceService } from "@
import { RpcApi } from "@/app/store/wshclientapi";
import { fireAndForget } from "@/util/util";
import { BaseWindow, BaseWindowConstructorOptions, dialog, globalShortcut, ipcMain, screen } from "electron";
import { globalEvents } from "emain/emain-events";
import path from "path";
import { debounce } from "throttle-debounce";
import {
Expand Down Expand Up @@ -293,6 +294,7 @@ export class WaveBrowserWindow extends BaseWindow {
console.log("win quitting or updating", this.waveWindowId);
return;
}
setTimeout(() => globalEvents.emit("windows-updated"), 50);
waveWindowMap.delete(this.waveWindowId);
if (focusedWaveWindow == this) {
focusedWaveWindow = null;
Expand All @@ -309,6 +311,7 @@ export class WaveBrowserWindow extends BaseWindow {
}
});
waveWindowMap.set(waveWindow.oid, this);
setTimeout(() => globalEvents.emit("windows-updated"), 50);
}

private removeAllChildViews() {
Expand Down
12 changes: 12 additions & 0 deletions emain/emain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { RpcApi } from "@/app/store/wshclientapi";
import * as electron from "electron";
import { globalEvents } from "emain/emain-events";
import { FastAverageColor } from "fast-average-color";
import fs from "fs";
import * as child_process from "node:child_process";
Expand Down Expand Up @@ -572,6 +573,17 @@ process.on("uncaughtException", (error) => {
electronApp.quit();
});

let lastWaveWindowCount = 0;
globalEvents.on("windows-updated", () => {
const wwCount = getAllWaveWindows().length;
if (wwCount == lastWaveWindowCount) {
return;
}
lastWaveWindowCount = wwCount;
console.log("windows-updated", wwCount);
makeAppMenu();
});

async function appMain() {
// Set disableHardwareAcceleration as early as possible, if required.
const launchSettings = getLaunchSettings();
Expand Down
32 changes: 28 additions & 4 deletions emain/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createNewWaveWindow,
createWorkspace,
focusedWaveWindow,
getAllWaveWindows,
getWaveWindowByWorkspaceId,
relaunchBrowserWindows,
WaveBrowserWindow,
Expand Down Expand Up @@ -67,7 +68,11 @@ async function getWorkspaceMenu(ww?: WaveBrowserWindow): Promise<Electron.MenuIt
return workspaceMenu;
}

async function getAppMenu(callbacks: AppMenuCallbacks, workspaceId?: string): Promise<Electron.Menu> {
async function getAppMenu(
numWaveWindows: number,
callbacks: AppMenuCallbacks,
workspaceId?: string
): Promise<Electron.Menu> {
const ww = workspaceId && getWaveWindowByWorkspaceId(workspaceId);
const fileMenu: Electron.MenuItemConstructorOptions[] = [
{
Expand All @@ -83,6 +88,22 @@ async function getAppMenu(callbacks: AppMenuCallbacks, workspaceId?: string): Pr
},
},
];
if (numWaveWindows == 0) {
fileMenu.push({
label: "New Window (hidden-1)",
accelerator: unamePlatform === "darwin" ? "Command+N" : "Alt+N",
acceleratorWorksWhenHidden: true,
visible: false,
click: () => fireAndForget(callbacks.createNewWaveWindow),
});
fileMenu.push({
label: "New Window (hidden-2)",
accelerator: unamePlatform === "darwin" ? "Command+T" : "Alt+T",
acceleratorWorksWhenHidden: true,
visible: false,
click: () => fireAndForget(callbacks.createNewWaveWindow),
});
}
const appMenu: Electron.MenuItemConstructorOptions[] = [
{
label: "About Wave Terminal",
Expand Down Expand Up @@ -299,8 +320,9 @@ async function getAppMenu(callbacks: AppMenuCallbacks, workspaceId?: string): Pr
return electron.Menu.buildFromTemplate(menuTemplate);
}

export function instantiateAppMenu(workspaceId?: string): Promise<electron.Menu> {
export function instantiateAppMenu(numWindows: number, workspaceId?: string): Promise<electron.Menu> {
return getAppMenu(
numWindows,
{
createNewWaveWindow,
relaunchBrowserWindows,
Expand All @@ -311,7 +333,8 @@ export function instantiateAppMenu(workspaceId?: string): Promise<electron.Menu>

export function makeAppMenu() {
fireAndForget(async () => {
const menu = await instantiateAppMenu();
const wwCount = getAllWaveWindows().length;
const menu = await instantiateAppMenu(wwCount);
electron.Menu.setApplicationMenu(menu);
});
}
Expand Down Expand Up @@ -351,10 +374,11 @@ electron.ipcMain.on("contextmenu-show", (event, workspaceId: string, menuDefArr?
if (menuDefArr?.length === 0) {
return;
}
const wwCount = getAllWaveWindows().length;
fireAndForget(async () => {
const menu = menuDefArr
? convertMenuDefArrToMenu(workspaceId, menuDefArr)
: await instantiateAppMenu(workspaceId);
: await instantiateAppMenu(wwCount, workspaceId);
menu.popup();
});
event.returnValue = true;
Expand Down
Loading