diff --git a/src/app/window-actions.ts b/src/app/window-actions.ts index 7e50704c..8229b896 100644 --- a/src/app/window-actions.ts +++ b/src/app/window-actions.ts @@ -26,8 +26,8 @@ const saveWindowSettings = async (): Promise => { const mainWindow = windowHandler.getMainWindow(); if (browserWindow && windowExists(browserWindow)) { - const [ x, y ] = browserWindow.getPosition(); - const [ width, height ] = browserWindow.getSize(); + let [ x, y ] = browserWindow.getPosition(); + let [ width, height ] = browserWindow.getSize(); if (x && y && width && height) { // Only send bound changes over to client for pop-out windows if (browserWindow.winName !== apiName.mainWindowName && mainWindow && windowExists(mainWindow)) { @@ -39,7 +39,18 @@ const saveWindowSettings = async (): Promise => { const isMaximized = browserWindow.isMaximized(); const isFullScreen = browserWindow.isFullScreen(); const { mainWinPos } = config.getUserConfigFields([ 'mainWinPos' ]); - await config.updateUserConfig({ mainWinPos: { ...mainWinPos, ...{ x, y, width, height, isMaximized, isFullScreen } } }); + + if (isMaximized || isFullScreen) { + // Keep the original size and position when window is maximized or full screen + if (mainWinPos !== undefined && mainWinPos.x !== undefined && mainWinPos.y !== undefined && mainWinPos.width !== undefined && mainWinPos.height !== undefined) { + x = mainWinPos.x; + y = mainWinPos.y; + width = mainWinPos.width; + height = mainWinPos.height; + } + } + + await config.updateUserConfig({ mainWinPos: { ...mainWinPos, ...{ height, width, x, y, isMaximized, isFullScreen } } }); } } } diff --git a/src/app/window-handler.ts b/src/app/window-handler.ts index 4cc05142..3d1b665b 100644 --- a/src/app/window-handler.ts +++ b/src/app/window-handler.ts @@ -145,16 +145,20 @@ export class WindowHandler { this.spellchecker = new SpellChecker(); logger.info(`window-handler: initialized spellchecker module with locale ${this.spellchecker.locale}`); + logger.info('window-handler: createApplication mainWinPos: ' + JSON.stringify(this.config.mainWinPos)); + // set window opts with additional config this.mainWindow = new BrowserWindow({ ...this.windowOpts, ...getBounds(this.config.mainWinPos, DEFAULT_WIDTH, DEFAULT_HEIGHT), }) as ICustomBrowserWindow; + this.mainWindow.winName = apiName.mainWindowName; const {isFullScreen, isMaximized} = this.config.mainWinPos ? this.config.mainWinPos : {isFullScreen: false, isMaximized: false}; if (isMaximized) { this.mainWindow.maximize(); logger.info(`window-handler: window is maximized!`); } + if (isFullScreen) { logger.info(`window-handler: window is in full screen!`); this.mainWindow.setFullScreen(true); diff --git a/src/app/window-utils.ts b/src/app/window-utils.ts index 55a66ab2..ead1baf1 100644 --- a/src/app/window-utils.ts +++ b/src/app/window-utils.ts @@ -301,19 +301,45 @@ export const sanitize = async (windowName: string): Promise => { * @return {x?: Number, y?: Number, width: Number, height: Number} */ export const getBounds = (winPos: ICustomRectangle | Electron.Rectangle | undefined, defaultWidth: number, defaultHeight: number): Partial => { + logger.info('window-utils: getBounds, winPos: ' + JSON.stringify(winPos)); + if (!winPos || !winPos.x || !winPos.y || !winPos.width || !winPos.height) { return { width: defaultWidth, height: defaultHeight }; } const displays = electron.screen.getAllDisplays(); for (let i = 0, len = displays.length; i < len; i++) { - const workArea = displays[ i ].workArea; - if (winPos.x >= workArea.x && winPos.y >= workArea.y && - ((winPos.x + winPos.width) <= (workArea.x + workArea.width)) && - ((winPos.y + winPos.height) <= (workArea.y + workArea.height))) { + const bounds = displays[ i ].bounds; + logger.info('window-utils: getBounds, bounds: ' + JSON.stringify(bounds)); + if (winPos.x >= bounds.x && winPos.y >= bounds.y && + ((winPos.x + winPos.width) <= (bounds.x + bounds.width)) && + ((winPos.y + winPos.height) <= (bounds.y + bounds.height))) { return winPos; } } + + // Fit in the middle of immediate display + const display = electron.screen.getDisplayMatching(winPos as electron.Rectangle); + + if (display) { + // Check that defaultWidth fits + let windowWidth = defaultWidth; + if (display.workArea.width < defaultWidth) { + windowWidth = display.workArea.width; + } + + // Check that defaultHeight fits + let windowHeight = defaultHeight; + if (display.workArea.height < defaultHeight) { + windowHeight = display.workArea.height; + } + + const windowX = display.workArea.x + display.workArea.width / 2 - windowWidth / 2; + const windowY = display.workArea.y + display.workArea.height / 2 - windowHeight / 2; + + return { x: windowX, y: windowY, width: windowWidth, height: windowHeight }; + } + return { width: defaultWidth, height: defaultHeight }; };