Maximize main window if monitor removed/changed resolution (#2486)
* Maximize main window if monitor removed/changed resolution * Fix unit tests * Add better handlers to support active screen changes
This commit is contained in:
@@ -51,6 +51,9 @@ jest.mock('electron', () => ({
|
|||||||
handle: jest.fn(),
|
handle: jest.fn(),
|
||||||
emit: jest.fn(),
|
emit: jest.fn(),
|
||||||
},
|
},
|
||||||
|
screen: {
|
||||||
|
on: jest.fn(),
|
||||||
|
},
|
||||||
session: {
|
session: {
|
||||||
defaultSession: {
|
defaultSession: {
|
||||||
setSpellCheckerDictionaryDownloadURL: jest.fn(),
|
setSpellCheckerDictionaryDownloadURL: jest.fn(),
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
import {app, ipcMain, session} from 'electron';
|
import {app, ipcMain, screen, session} from 'electron';
|
||||||
import installExtension, {REACT_DEVELOPER_TOOLS} from 'electron-devtools-installer';
|
import installExtension, {REACT_DEVELOPER_TOOLS} from 'electron-devtools-installer';
|
||||||
import isDev from 'electron-is-dev';
|
import isDev from 'electron-is-dev';
|
||||||
import log from 'electron-log';
|
import log from 'electron-log';
|
||||||
@@ -119,6 +119,8 @@ export async function initialize() {
|
|||||||
app.whenReady(),
|
app.whenReady(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
initializeScreenEventListeners();
|
||||||
|
|
||||||
// no need to continue initializing if app is quitting
|
// no need to continue initializing if app is quitting
|
||||||
if (global.willAppQuit) {
|
if (global.willAppQuit) {
|
||||||
return;
|
return;
|
||||||
@@ -188,6 +190,11 @@ function initializeAppEventListeners() {
|
|||||||
app.on('will-finish-launching', handleAppWillFinishLaunching);
|
app.on('will-finish-launching', handleAppWillFinishLaunching);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function initializeScreenEventListeners() {
|
||||||
|
screen.on('display-removed', WindowManager.displayRemoved);
|
||||||
|
screen.on('display-metrics-changed', WindowManager.displayMetricsChanged);
|
||||||
|
}
|
||||||
|
|
||||||
function initializeBeforeAppReady() {
|
function initializeBeforeAppReady() {
|
||||||
if (!Config.data) {
|
if (!Config.data) {
|
||||||
log.error('No config loaded');
|
log.error('No config loaded');
|
||||||
|
@@ -4,7 +4,7 @@
|
|||||||
/* eslint-disable max-lines */
|
/* eslint-disable max-lines */
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
import {app, BrowserWindow, nativeImage, systemPreferences, ipcMain, IpcMainEvent, IpcMainInvokeEvent, desktopCapturer} from 'electron';
|
import {app, BrowserWindow, nativeImage, systemPreferences, ipcMain, IpcMainEvent, IpcMainInvokeEvent, desktopCapturer, Display, screen} from 'electron';
|
||||||
import log from 'electron-log';
|
import log from 'electron-log';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -255,6 +255,42 @@ export class WindowManager {
|
|||||||
this.sendToRenderer(MAXIMIZE_CHANGE, false);
|
this.sendToRenderer(MAXIMIZE_CHANGE, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
maximizeMainWindow = () => {
|
||||||
|
if (!(this.viewManager && this.mainWindow)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.mainWindow.maximize?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
displayRemoved = (event: Event, oldDisplay: Display) => {
|
||||||
|
log.debug('WindowManager.displayRemoved', {oldDisplay});
|
||||||
|
|
||||||
|
if (!oldDisplay) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isActiveScreen(oldDisplay.id)) {
|
||||||
|
this.maximizeMainWindow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
displayMetricsChanged = (event: Event, display: Display, changedMetrics: string[]) => {
|
||||||
|
log.debug('WindowManager.displayMetricsChanged', {display, changedMetrics});
|
||||||
|
|
||||||
|
this.maximizeMainWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
isActiveScreen = (id: Display['id']): boolean => {
|
||||||
|
if (!(this.viewManager && this.mainWindow)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mainWindowBounds = this.mainWindow.getBounds();
|
||||||
|
const currentDisplay = screen.getDisplayNearestPoint({x: mainWindowBounds.x, y: mainWindowBounds.y});
|
||||||
|
log.debug('WindowManager.isActiveScreen', {id, currentDisplay});
|
||||||
|
return currentDisplay.id === id;
|
||||||
|
}
|
||||||
|
|
||||||
isResizing = false;
|
isResizing = false;
|
||||||
|
|
||||||
handleWillResizeMainWindow = (event: Event, newBounds: Electron.Rectangle) => {
|
handleWillResizeMainWindow = (event: Event, newBounds: Electron.Rectangle) => {
|
||||||
|
Reference in New Issue
Block a user