From b2ee164b8544ab33c7d8889d373dae97cd551307 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Mon, 22 Jul 2024 16:39:45 -0700 Subject: [PATCH] Close a window automatically when the last tab is closed (#131) --- emain/emain.ts | 9 +++++++++ pkg/eventbus/eventbus.go | 1 + pkg/service/windowservice/windowservice.go | 7 +++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/emain/emain.ts b/emain/emain.ts index b6f955f77..3dca4cf7d 100644 --- a/emain/emain.ts +++ b/emain/emain.ts @@ -217,6 +217,15 @@ async function handleWSEvent(evtMsg: WSEventType) { const newWin = createBrowserWindow(clientData.oid, windowData); await newWin.readyPromise; newWin.show(); + } else if (evtMsg.eventtype == "electron:closewindow") { + if (evtMsg.data === undefined) return; + const windows = electron.BrowserWindow.getAllWindows(); + for (const window of windows) { + if ((window as any).waveWindowId === evtMsg.data) { + // Bypass the "Are you sure?" dialog, since this event is called when there's no more tabs for the window. + window.destroy(); + } + } } else { console.log("unhandled electron ws eventtype", evtMsg.eventtype); } diff --git a/pkg/eventbus/eventbus.go b/pkg/eventbus/eventbus.go index 257c7db99..b7b97544c 100644 --- a/pkg/eventbus/eventbus.go +++ b/pkg/eventbus/eventbus.go @@ -22,6 +22,7 @@ const ( WSEvent_BlockControllerStatus = "blockcontroller:status" WSEvent_LayoutAction = "layoutaction" WSEvent_ElectronNewWindow = "electron:newwindow" + WSEvent_ElectronCloseWindow = "electron:closewindow" WSEvent_Rpc = "rpc" ) diff --git a/pkg/service/windowservice/windowservice.go b/pkg/service/windowservice/windowservice.go index d7a45b39c..3140c7e9e 100644 --- a/pkg/service/windowservice/windowservice.go +++ b/pkg/service/windowservice/windowservice.go @@ -67,10 +67,13 @@ func (svc *WindowService) CloseTab(ctx context.Context, uiContext wstore.UIConte var newActiveTabId string if len(ws.TabIds) > 0 { newActiveTabId = ws.TabIds[0] + wstore.SetActiveTab(ctx, uiContext.WindowId, newActiveTabId) } else { - newActiveTabId = "" + eventbus.SendEventToElectron(eventbus.WSEventType{ + EventType: eventbus.WSEvent_ElectronCloseWindow, + Data: uiContext.WindowId, + }) } - wstore.SetActiveTab(ctx, uiContext.WindowId, newActiveTabId) } return wstore.ContextGetUpdatesRtn(ctx), nil }