diff --git a/src/Apps/NetPad.Apps.App/App/src/core/@application/platforms/electron/services/electron-window-service.ts b/src/Apps/NetPad.Apps.App/App/src/core/@application/platforms/electron/services/electron-window-service.ts index 7e73ed17..ae76b1b1 100644 --- a/src/Apps/NetPad.Apps.App/App/src/core/@application/platforms/electron/services/electron-window-service.ts +++ b/src/Apps/NetPad.Apps.App/App/src/core/@application/platforms/electron/services/electron-window-service.ts @@ -1,6 +1,6 @@ import {ipcRenderer} from "electron"; import {IHttpClient} from "@aurelia/fetch-client"; -import {ChannelInfo, IWindowService, WindowApiClient, WindowState} from "@application"; +import {ChannelInfo, IWindowService, IWindowState, WindowApiClient, WindowState, WindowViewStatus} from "@application"; import {ElectronIpcEventNames} from "../electron-ipc-event-names"; import {ElectronIpcGateway} from "./electron-ipc-gateway"; @@ -13,7 +13,12 @@ export class ElectronWindowService extends WindowApiClient implements IWindowSer } public async getState(): Promise { - const state = await ipcRenderer.invoke(ElectronIpcEventNames.getWindowState) as WindowState; + const state = await ipcRenderer.invoke(ElectronIpcEventNames.getWindowState) as IWindowState; + + if (!state) { + return new WindowState(WindowViewStatus.Unknown, false); + } + return new WindowState(state.viewStatus, state.isAlwaysOnTop); } diff --git a/src/Apps/NetPad.Apps.App/App/src/core/@application/windows/window-state.ts b/src/Apps/NetPad.Apps.App/App/src/core/@application/windows/window-state.ts index 7617b776..3d4199d5 100644 --- a/src/Apps/NetPad.Apps.App/App/src/core/@application/windows/window-state.ts +++ b/src/Apps/NetPad.Apps.App/App/src/core/@application/windows/window-state.ts @@ -5,7 +5,12 @@ export enum WindowViewStatus { "Maximized" = "Maximized", } -export class WindowState { +export interface IWindowState { + viewStatus: WindowViewStatus; + isAlwaysOnTop: boolean; +} + +export class WindowState implements IWindowState { constructor(public viewStatus: WindowViewStatus, public isAlwaysOnTop: boolean) { } diff --git a/src/Apps/NetPad.Apps.App/ElectronHostHook/app/window-controls/models.ts b/src/Apps/NetPad.Apps.App/ElectronHostHook/app/window-controls/models.ts index d6720513..5a520bca 100644 --- a/src/Apps/NetPad.Apps.App/ElectronHostHook/app/window-controls/models.ts +++ b/src/Apps/NetPad.Apps.App/ElectronHostHook/app/window-controls/models.ts @@ -5,7 +5,7 @@ export enum WindowViewStatus { "Maximized" = "Maximized", } -export class WindowState { - constructor(public viewStatus: WindowViewStatus, public isAlwaysOnTop: boolean) { - } +export interface IWindowState { + viewStatus: WindowViewStatus; + isAlwaysOnTop: boolean; } diff --git a/src/Apps/NetPad.Apps.App/ElectronHostHook/app/window-controls/window-controls-manager.ts b/src/Apps/NetPad.Apps.App/ElectronHostHook/app/window-controls/window-controls-manager.ts index 78e0ded5..a276f2c1 100644 --- a/src/Apps/NetPad.Apps.App/ElectronHostHook/app/window-controls/window-controls-manager.ts +++ b/src/Apps/NetPad.Apps.App/ElectronHostHook/app/window-controls/window-controls-manager.ts @@ -1,5 +1,5 @@ import {BrowserWindow, ipcMain} from "electron"; -import {WindowState, WindowViewStatus} from "./models"; +import {IWindowState, WindowViewStatus} from "./models"; export class IpcEventNames { public static readonly getWindowState = "get-window-state"; @@ -23,7 +23,10 @@ export class WindowControlsManager { else viewStatus = WindowViewStatus.UnMaximized; - return new WindowState(viewStatus, window.isAlwaysOnTop()); + return { + viewStatus: viewStatus, + isAlwaysOnTop: window.isAlwaysOnTop() + }; }], [IpcEventNames.maximize, window => window.isMaximized() ? window.unmaximize() : window.maximize()], [IpcEventNames.minimize, window => window.isMinimized() ? window.restore() : window.minimize()], @@ -34,10 +37,16 @@ export class WindowControlsManager { for (const [eventName, handler] of handlers) { ipcMain.handle(eventName, event => { - const window = this.getBrowserWindow(event); - if (!window) return; + try { + const window = this.getBrowserWindow(event); + if (!window) { + return; + } - return handler(window); + return handler(window); + } catch (e) { + console.error(`Error while handling event: '${eventName}'`, e); + } }); } } diff --git a/src/Apps/NetPad.Apps.Shells.Electron/UiInterop/ElectronWindowService.cs b/src/Apps/NetPad.Apps.Shells.Electron/UiInterop/ElectronWindowService.cs index 3aacbccf..87585c4b 100644 --- a/src/Apps/NetPad.Apps.Shells.Electron/UiInterop/ElectronWindowService.cs +++ b/src/Apps/NetPad.Apps.Shells.Electron/UiInterop/ElectronWindowService.cs @@ -66,8 +66,17 @@ private async Task RestoreMainWindowPositionAsync(BrowserWindow window) window.OnClose += async () => { - trivialDataStore.Set("main-window.bounds", - new WindowState(await window.GetBoundsAsync(), await window.IsMaximizedAsync())); + try + { + var bounds = await window.GetBoundsAsync(); + var isMaximized = await window.IsMaximizedAsync(); + + trivialDataStore.Set("main-window.bounds", new WindowState(bounds, isMaximized)); + } + catch (Exception ex) + { + logger.LogError(ex, "Error while saving window state on close."); + } }; }