Skip to content

Commit

Permalink
Fixes #244 handles error thrown by Electron.NET of getting window sta…
Browse files Browse the repository at this point in the history
…te when window is being closed
  • Loading branch information
tareqimbasher committed Aug 13, 2024
1 parent 1ac1037 commit dc4a212
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -13,7 +13,12 @@ export class ElectronWindowService extends WindowApiClient implements IWindowSer
}

public async getState(): Promise<WindowState> {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -23,7 +23,10 @@ export class WindowControlsManager {
else
viewStatus = WindowViewStatus.UnMaximized;

return new WindowState(viewStatus, window.isAlwaysOnTop());
return <IWindowState>{
viewStatus: viewStatus,
isAlwaysOnTop: window.isAlwaysOnTop()
};
}],
[IpcEventNames.maximize, window => window.isMaximized() ? window.unmaximize() : window.maximize()],
[IpcEventNames.minimize, window => window.isMinimized() ? window.restore() : window.minimize()],
Expand All @@ -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);
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
};
}

Expand Down

0 comments on commit dc4a212

Please sign in to comment.