From 2dee9b6bc8f8ebd4684c3c5e76a8c8a00a45d46d Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Tue, 16 Jul 2024 09:35:06 -0400 Subject: [PATCH] Fixed isInsideRectangle to actually work, and removed null check for matchingScreen since it will always return something (#3093) --- src/main/utils.test.js | 15 +++++++++++++++ src/main/utils.ts | 18 +++++++++++++++++- src/main/windows/mainWindow.ts | 4 ++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/utils.test.js b/src/main/utils.test.js index 87575064..57a39c24 100644 --- a/src/main/utils.test.js +++ b/src/main/utils.test.js @@ -162,4 +162,19 @@ describe('main/utils', () => { expect(Utils.shouldIncrementFilename('filename.txt')).toBe('filename (1).txt'); }); }); + + describe('isInsideRectangle', () => { + it.each([ + [{x: 0, y: 0, width: 1920, height: 1080}, {x: 100, y: 100, width: 1280, height: 720}, true], + [{x: 0, y: 0, width: 1920, height: 1080}, {x: -100, y: 100, width: 1280, height: 720}, false], + [{x: 0, y: 0, width: 1920, height: 1080}, {x: 100, y: -100, width: 1280, height: 720}, false], + [{x: 0, y: 0, width: 1920, height: 1080}, {x: 100, y: 100, width: 2560, height: 720}, false], + [{x: 0, y: 0, width: 1920, height: 1080}, {x: 100, y: 100, width: 1280, height: 1440}, false], + [{x: -1920, y: 0, width: 1920, height: 1080}, {x: 100, y: 100, width: 1280, height: 720}, false], + [{x: -1920, y: 0, width: 1920, height: 1080}, {x: -1820, y: 100, width: 1280, height: 720}, true], + [{x: 0, y: -1080, width: 1920, height: 1080}, {x: 100, y: -980, width: 1280, height: 720}, true], + ])('should match case', (a, b, expected) => { + expect(Utils.isInsideRectangle(a, b)).toBe(expected); + }); + }); }); diff --git a/src/main/utils.ts b/src/main/utils.ts index c1af2d25..5fcfa148 100644 --- a/src/main/utils.ts +++ b/src/main/utils.ts @@ -18,7 +18,23 @@ import Utils from 'common/utils/util'; import type {Args} from 'types/args'; export function isInsideRectangle(container: Electron.Rectangle, rect: Electron.Rectangle) { - return container.x <= rect.x && container.y <= rect.y && container.width >= rect.width && container.height >= rect.height; + if (container.x > rect.x) { + return false; + } + + if (container.x + container.width < rect.x + rect.width) { + return false; + } + + if (container.y > rect.y) { + return false; + } + + if (container.y + container.height < rect.y + rect.height) { + return false; + } + + return true; } export function shouldBeHiddenOnStartup(parsedArgv: Args) { diff --git a/src/main/windows/mainWindow.ts b/src/main/windows/mainWindow.ts index ddbbc470..7c59ceef 100644 --- a/src/main/windows/mainWindow.ts +++ b/src/main/windows/mainWindow.ts @@ -254,8 +254,8 @@ export class MainWindow extends EventEmitter { throw new Error('Provided bounds info file does not validate, using defaults instead.'); } const matchingScreen = screen.getDisplayMatching(savedWindowState); - log.debug('matching screen for main window', matchingScreen); - if (!(matchingScreen && (isInsideRectangle(matchingScreen.bounds, savedWindowState) || savedWindowState.maximized))) { + log.debug('closest matching screen for main window', matchingScreen); + if (!(isInsideRectangle(matchingScreen.bounds, savedWindowState) || savedWindowState.maximized)) { throw new Error('Provided bounds info are outside the bounds of your screen, using defaults instead.'); }