From d7ecc0b727e25fbd3733accd3e801f5523012135 Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Wed, 12 Jun 2024 17:22:02 +0300 Subject: [PATCH] [MM-58357] Disable window scaling for OSes that aren't Windows (#3063) (#3064) (cherry picked from commit 3139d485d4a6a6ad594cf9259253501e0b2fd86c) Co-authored-by: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> --- src/main/windows/mainWindow.test.js | 34 ++++++++++++++++++++++++++++- src/main/windows/mainWindow.ts | 7 ++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main/windows/mainWindow.test.js b/src/main/windows/mainWindow.test.js index 3763e60dd1..bb8e5d17d7 100644 --- a/src/main/windows/mainWindow.test.js +++ b/src/main/windows/mainWindow.test.js @@ -129,7 +129,12 @@ describe('main/windows/mainWindow', () => { })); }); - it('should set scaled window size using bounds read from file', () => { + it('should set scaled window size on Windows using bounds read from file', () => { + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'win32', + }); + screen.getDisplayMatching.mockImplementation(() => ({scaleFactor: 2, bounds: {x: 0, y: 0, width: 1920, height: 1080}})); const mainWindow = new MainWindow(); mainWindow.init(); @@ -141,6 +146,33 @@ describe('main/windows/mainWindow', () => { maximized: false, fullscreen: false, })); + + Object.defineProperty(process, 'platform', { + value: originalPlatform, + }); + }); + + it('should NOT set scaled window size on Mac using bounds read from file', () => { + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'darwin', + }); + + screen.getDisplayMatching.mockImplementation(() => ({scaleFactor: 2, bounds: {x: 0, y: 0, width: 1920, height: 1080}})); + const mainWindow = new MainWindow(); + mainWindow.init(); + expect(BrowserWindow).toHaveBeenCalledWith(expect.objectContaining({ + x: 400, + y: 300, + width: 1280, + height: 700, + maximized: false, + fullscreen: false, + })); + + Object.defineProperty(process, 'platform', { + value: originalPlatform, + }); }); it('should set default window size when failing to read bounds from file', () => { diff --git a/src/main/windows/mainWindow.ts b/src/main/windows/mainWindow.ts index 81d3f1e013..1e8c869e1f 100644 --- a/src/main/windows/mainWindow.ts +++ b/src/main/windows/mainWindow.ts @@ -258,12 +258,15 @@ export class MainWindow extends EventEmitter { if (!(matchingScreen && (isInsideRectangle(matchingScreen.bounds, savedWindowState) || savedWindowState.maximized))) { throw new Error('Provided bounds info are outside the bounds of your screen, using defaults instead.'); } + // We check for the monitor's scale factor when we want to set these bounds // This is due to a long running Electron issue: https://github.com/electron/electron/issues/10862 + // This only needs to be done on Windows, it causes strange behaviour on Mac otherwise + const scaleFactor = process.platform === 'win32' ? matchingScreen.scaleFactor : 1; return { ...savedWindowState, - width: Math.floor(savedWindowState.width / matchingScreen.scaleFactor), - height: Math.floor(savedWindowState.height / matchingScreen.scaleFactor), + width: Math.floor(savedWindowState.width / scaleFactor), + height: Math.floor(savedWindowState.height / scaleFactor), }; } catch (e) { log.error(e);