From 1049b0763c0ed475b7c17d49f351c1fbbd622c16 Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Wed, 3 May 2023 08:48:54 -0400 Subject: [PATCH] Fixed a few resizing issues (#2703) * Fix bad handler on resize for Linux * Fix maximize/minimize issue again * Fix macOS rapid resizing issue * Make sure #2638 is still fixed * Still don't need this * Added comment and fixed test --- src/main/preload/desktopAPI.js | 4 --- src/main/windows/mainWindow.test.js | 1 + src/main/windows/mainWindow.ts | 46 ++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/main/preload/desktopAPI.js b/src/main/preload/desktopAPI.js index cf4ba656..4b128fc6 100644 --- a/src/main/preload/desktopAPI.js +++ b/src/main/preload/desktopAPI.js @@ -88,7 +88,6 @@ import { GET_ORDERED_SERVERS, GET_ORDERED_TABS_FOR_SERVER, SERVERS_UPDATE, - VIEW_FINISHED_RESIZING, } from 'common/communication'; console.log('Preload initialized'); @@ -253,6 +252,3 @@ const createKeyDownListener = () => { }; createKeyDownListener(); -window.addEventListener('resize', () => { - ipcRenderer.send(VIEW_FINISHED_RESIZING); -}); diff --git a/src/main/windows/mainWindow.test.js b/src/main/windows/mainWindow.test.js index b46f30d4..e973513c 100644 --- a/src/main/windows/mainWindow.test.js +++ b/src/main/windows/mainWindow.test.js @@ -85,6 +85,7 @@ describe('main/windows/mainWindow', () => { describe('init', () => { const baseWindow = { setMenuBarVisibility: jest.fn(), + setAutoHideMenuBar: jest.fn(), loadURL: jest.fn(), once: jest.fn(), on: jest.fn(), diff --git a/src/main/windows/mainWindow.ts b/src/main/windows/mainWindow.ts index a854ecef..18dd004b 100644 --- a/src/main/windows/mainWindow.ts +++ b/src/main/windows/mainWindow.ts @@ -91,12 +91,13 @@ export class MainWindow extends EventEmitter { } this.win = new BrowserWindow(windowOptions); - this.win.setMenuBarVisibility(false); - if (!this.win) { throw new Error('unable to create main window'); } + this.win.setAutoHideMenuBar(true); + this.win.setMenuBarVisibility(false); + const localURL = getLocalURLString('index.html'); this.win.loadURL(localURL).catch( (reason) => { @@ -129,13 +130,13 @@ export class MainWindow extends EventEmitter { this.win.on('unresponsive', this.onUnresponsive); this.win.on('maximize', this.onMaximize); this.win.on('unmaximize', this.onUnmaximize); - this.win.on('enter-full-screen', () => this.win?.webContents.send('enter-full-screen')); - this.win.on('leave-full-screen', () => this.win?.webContents.send('leave-full-screen')); + this.win.on('enter-full-screen', this.onEnterFullScreen); + this.win.on('leave-full-screen', this.onLeaveFullScreen); this.win.on('will-resize', this.onWillResize); this.win.on('resized', this.onResized); this.win.on('moved', this.onResized); - if (process.platform !== 'darwin') { - mainWindow.on('resize', this.onResize); + if (process.platform === 'linux') { + this.win.on('resize', this.onResize); } this.win.webContents.on('before-input-event', this.onBeforeInputEvent); @@ -406,14 +407,37 @@ export class MainWindow extends EventEmitter { }); } + private emitBoundsForMaximize = () => { + // Workaround for Linux since the window bounds aren't updated immediately when the window is maximized for some reason + if (process.platform !== 'linux') { + return; + } + setTimeout(() => { + this.emit(MAIN_WINDOW_RESIZED, this.getBounds()); + }, 10); + } + private onMaximize = () => { this.win?.webContents.send(MAXIMIZE_CHANGE, true); - this.emit(MAIN_WINDOW_RESIZED, this.getBounds()); + this.emitBoundsForMaximize(); } private onUnmaximize = () => { this.win?.webContents.send(MAXIMIZE_CHANGE, false); - this.emit(MAIN_WINDOW_RESIZED, this.getBounds()); + this.emitBoundsForMaximize(); + } + + private onEnterFullScreen = () => { + this.win?.webContents.send('enter-full-screen'); + this.emitBoundsForMaximize(); + + // For some reason on Linux I've seen the menu bar popup again + this.win?.setMenuBarVisibility(false); + } + + private onLeaveFullScreen = () => { + this.win?.webContents.send('leave-full-screen'); + this.emitBoundsForMaximize(); } /** @@ -434,7 +458,8 @@ export class MainWindow extends EventEmitter { return; } - if (this.isResizing) { + // Workaround for macOS to stop the window from sending too many resize calls to the BrowserViews + if (process.platform === 'darwin' && this.isResizing) { log.debug('prevented resize'); event.preventDefault(); return; @@ -447,7 +472,8 @@ export class MainWindow extends EventEmitter { private onResize = () => { log.silly('onResize'); - if (this.isResizing) { + // Workaround for macOS to stop the window from sending too many resize calls to the BrowserViews + if (process.platform === 'darwin' && this.isResizing) { return; } this.emit(MAIN_WINDOW_RESIZED, this.getBounds());