diff --git a/src/main/windows/mainWindow.test.js b/src/main/windows/mainWindow.test.js index 58d31107..8dadad47 100644 --- a/src/main/windows/mainWindow.test.js +++ b/src/main/windows/mainWindow.test.js @@ -495,14 +495,35 @@ describe('main/windows/mainWindow', () => { afterEach(() => { mainWindow.ready = false; + mainWindow.win.visible = false; jest.resetAllMocks(); }); - it('should show main window and focus it if it is exists', () => { + it('should show main window if it is exists on macOS/Linux', () => { + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'darwin', + }); mainWindow.ready = true; mainWindow.show(); expect(mainWindow.win.show).toHaveBeenCalled(); + Object.defineProperty(process, 'platform', { + value: originalPlatform, + }); + }); + + it('should focus main window if it exists and is visible on Windows', () => { + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'win32', + }); + mainWindow.ready = true; + mainWindow.win.visible = true; + mainWindow.show(); expect(mainWindow.win.focus).toHaveBeenCalled(); + Object.defineProperty(process, 'platform', { + value: originalPlatform, + }); }); it('should init if the main window does not exist', () => { diff --git a/src/main/windows/mainWindow.ts b/src/main/windows/mainWindow.ts index a0aaf3af..0b66b1d2 100644 --- a/src/main/windows/mainWindow.ts +++ b/src/main/windows/mainWindow.ts @@ -47,7 +47,7 @@ const ALT_MENU_KEYS = ['Alt+F', 'Alt+E', 'Alt+V', 'Alt+H', 'Alt+W', 'Alt+P']; export class MainWindow extends EventEmitter { private win?: BrowserWindow; - private savedWindowState: SavedWindowState; + private savedWindowState?: SavedWindowState; private ready: boolean; private isResizing: boolean; private lastEmittedBounds?: Electron.Rectangle @@ -58,7 +58,6 @@ export class MainWindow extends EventEmitter { // Create the browser window. this.ready = false; this.isResizing = false; - this.savedWindowState = this.getSavedWindowState(); ipcMain.handle(GET_FULL_SCREEN_STATUS, () => this.win?.isFullScreen()); ipcMain.on(VIEW_FINISHED_RESIZING, this.handleViewFinishedResizing); @@ -69,6 +68,9 @@ export class MainWindow extends EventEmitter { } init = () => { + // Can't call this before the app is ready + this.savedWindowState = this.getSavedWindowState(); + const windowOptions: BrowserWindowConstructorOptions = Object.assign({}, this.savedWindowState, { title: app.name, fullscreenable: true, @@ -107,7 +109,7 @@ export class MainWindow extends EventEmitter { if (Config.hideOnStart === false) { this.win.show(); - if (this.savedWindowState.maximized) { + if (this.savedWindowState?.maximized) { this.win.maximize(); } } @@ -167,8 +169,19 @@ export class MainWindow extends EventEmitter { show = () => { if (this.win && this.isReady) { - this.win.show(); - this.win.focus(); + // There's a bug on Windows in Electron where if the window is snapped, it will unsnap when you call show() + // See here: https://github.com/electron/electron/issues/25359 + // So to make sure we always show the window on macOS/Linux (need for workspace switching) + // We make an exception here + if (process.platform === 'win32') { + if (this.win.isVisible()) { + this.win.focus(); + } else { + this.win.show(); + } + } else { + this.win.show(); + } } else { this.init(); } @@ -224,7 +237,7 @@ export class MainWindow extends EventEmitter { if (Config.startInFullscreen) { return Config.startInFullscreen; } - return this.savedWindowState.fullscreen || false; + return this.savedWindowState?.fullscreen || false; } private isFramelessWindow = () => { @@ -244,7 +257,9 @@ export class MainWindow extends EventEmitter { throw new Error('Provided bounds info are outside the bounds of your screen, using defaults instead.'); } } catch (e) { - // Follow Electron's defaults, except for window dimensions which targets 1024x768 screen resolution. + log.error(e); + + // Follow Electron's defaults, except for window dimensions which targets 1024x768 screen resolution. savedWindowState = {width: DEFAULT_WINDOW_WIDTH, height: DEFAULT_WINDOW_HEIGHT}; } return savedWindowState;