Fixed isInsideRectangle to actually work, and removed null check for matchingScreen since it will always return something (#3093)

This commit is contained in:
Devin Binnie 2024-07-16 09:35:06 -04:00 committed by GitHub
parent 35665dee33
commit 2dee9b6bc8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 3 deletions

View file

@ -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);
});
});
});

View file

@ -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) {

View file

@ -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.');
}