[MM-58780] Don't account for scaleFactor on Windows when resizing if the matching screen is the primary monitor (#3072)

This commit is contained in:
Devin Binnie 2024-06-19 09:27:19 -04:00 committed by GitHub
parent 0d4800fd61
commit d3a76caeef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 7 deletions

View file

@ -39,6 +39,7 @@ jest.mock('electron', () => ({
},
screen: {
getDisplayMatching: jest.fn(),
getPrimaryDisplay: jest.fn(),
},
globalShortcut: {
register: jest.fn(),
@ -104,7 +105,9 @@ describe('main/windows/mainWindow', () => {
BrowserWindow.mockImplementation(() => baseWindow);
fs.readFileSync.mockImplementation(() => '{"x":400,"y":300,"width":1280,"height":700,"maximized":false,"fullscreen":false}');
path.join.mockImplementation(() => 'anyfile.txt');
screen.getDisplayMatching.mockImplementation(() => ({scaleFactor: 1, bounds: {x: 0, y: 0, width: 1920, height: 1080}}));
const primaryDisplay = {id: 1, scaleFactor: 1, bounds: {x: 0, y: 0, width: 1920, height: 1080}};
screen.getDisplayMatching.mockReturnValue(primaryDisplay);
screen.getPrimaryDisplay.mockReturnValue(primaryDisplay);
isInsideRectangle.mockReturnValue(true);
Validator.validateBoundsInfo.mockImplementation((data) => data);
ContextMenu.mockImplementation(() => ({
@ -129,13 +132,13 @@ describe('main/windows/mainWindow', () => {
}));
});
it('should set scaled window size on Windows using bounds read from file', () => {
it('should set scaled window size on Windows using bounds read from file for non-primary monitor', () => {
const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {
value: 'win32',
});
screen.getDisplayMatching.mockImplementation(() => ({scaleFactor: 2, bounds: {x: 0, y: 0, width: 1920, height: 1080}}));
screen.getDisplayMatching.mockImplementation(() => ({id: 2, scaleFactor: 2, bounds: {x: 0, y: 0, width: 1920, height: 1080}}));
const mainWindow = new MainWindow();
mainWindow.init();
expect(BrowserWindow).toHaveBeenCalledWith(expect.objectContaining({
@ -152,13 +155,36 @@ describe('main/windows/mainWindow', () => {
});
});
it('should NOT set scaled window size on Mac using bounds read from file', () => {
it('should NOT set scaled window size on Windows using bounds read from file for primary monitor', () => {
const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {
value: 'win32',
});
screen.getDisplayMatching.mockImplementation(() => ({id: 1, 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 NOT set scaled window size on Mac using bounds read from file for non-primary monitor', () => {
const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {
value: 'darwin',
});
screen.getDisplayMatching.mockImplementation(() => ({scaleFactor: 2, bounds: {x: 0, y: 0, width: 1920, height: 1080}}));
screen.getDisplayMatching.mockImplementation(() => ({id: 2, scaleFactor: 2, bounds: {x: 0, y: 0, width: 1920, height: 1080}}));
const mainWindow = new MainWindow();
mainWindow.init();
expect(BrowserWindow).toHaveBeenCalledWith(expect.objectContaining({
@ -187,7 +213,6 @@ describe('main/windows/mainWindow', () => {
it('should set default window size when bounds are outside the normal screen', () => {
fs.readFileSync.mockImplementation(() => '{"x":-400,"y":-300,"width":1280,"height":700,"maximized":false,"fullscreen":false}');
screen.getDisplayMatching.mockImplementation(() => ({bounds: {x: 0, y: 0, width: 1920, height: 1080}}));
isInsideRectangle.mockReturnValue(false);
const mainWindow = new MainWindow();
mainWindow.init();

View file

@ -260,9 +260,10 @@ export class MainWindow extends EventEmitter {
}
// We check for the monitor's scale factor when we want to set these bounds
// But only if it's not the primary monitor, otherwise it works fine as is
// 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;
const scaleFactor = process.platform === 'win32' && matchingScreen.id !== screen.getPrimaryDisplay().id ? matchingScreen.scaleFactor : 1;
return {
...savedWindowState,
width: Math.floor(savedWindowState.width / scaleFactor),