From 08fa75acc11d023a37f72f6f0a86d304ef6fdf18 Mon Sep 17 00:00:00 2001 From: Stefan Date: Wed, 5 Dec 2018 14:33:47 +0100 Subject: Merge 'fix/window-bounds' into 'develop' --- src/assets/images/taskbar/win32/display.ico | Bin 0 -> 101249 bytes src/config.js | 7 +++++ src/electron/windowUtils.js | 11 +++++++ src/index.js | 47 ++++++++++++++++++++++++---- 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 src/assets/images/taskbar/win32/display.ico create mode 100644 src/electron/windowUtils.js (limited to 'src') diff --git a/src/assets/images/taskbar/win32/display.ico b/src/assets/images/taskbar/win32/display.ico new file mode 100644 index 000000000..4f4670a85 Binary files /dev/null and b/src/assets/images/taskbar/win32/display.ico differ diff --git a/src/config.js b/src/config.js index ba3af14b9..d981f9c6a 100644 --- a/src/config.js +++ b/src/config.js @@ -39,6 +39,13 @@ export const DEFAULT_FEATURES_CONFIG = { isServiceProxyPremiumFeature: true, }; +export const DEFAULT_WINDOW_OPTIONS = { + width: 800, + height: 600, + x: 0, + y: 0, +}; + export const FRANZ_SERVICE_REQUEST = 'https://bit.ly/franz-plugin-docs'; export const FRANZ_TRANSLATION = 'https://bit.ly/franz-translate'; diff --git a/src/electron/windowUtils.js b/src/electron/windowUtils.js new file mode 100644 index 000000000..23b946ac4 --- /dev/null +++ b/src/electron/windowUtils.js @@ -0,0 +1,11 @@ +/* eslint import/prefer-default-export: 0 */ + +import { screen } from 'electron'; + +export function isPositionValid(position) { + const displays = screen.getAllDisplays(); + const { x, y } = position; + return displays.some(({ + workArea, + }) => x >= workArea.x && x <= workArea.x + workArea.width && y >= workArea.y && y <= workArea.y + workArea.height); +} diff --git a/src/index.js b/src/index.js index 37c553840..7f3fe5b44 100644 --- a/src/index.js +++ b/src/index.js @@ -15,10 +15,14 @@ import ipcApi from './electron/ipc-api'; import Tray from './lib/Tray'; import Settings from './electron/Settings'; import handleDeepLink from './electron/deepLinking'; +import { isPositionValid } from './electron/windowUtils'; import { appId } from './package.json'; // eslint-disable-line import/no-unresolved import './electron/exception'; -import { DEFAULT_APP_SETTINGS } from './config'; +import { + DEFAULT_APP_SETTINGS, + DEFAULT_WINDOW_OPTIONS, +} from './config'; /* eslint-enable import/first */ const debug = require('debug')('Franz:App'); @@ -52,6 +56,15 @@ const isSecondInstance = app.makeSingleInstance((argv) => { } } } + + if (argv.includes('--reset-window')) { + // Needs to be delayed to not interfere with mainWindow.restore(); + setTimeout(() => { + debug('Resetting windows via Task'); + mainWindow.setPosition(DEFAULT_WINDOW_OPTIONS.x + 100, DEFAULT_WINDOW_OPTIONS.y + 100); + mainWindow.setSize(DEFAULT_WINDOW_OPTIONS.width, DEFAULT_WINDOW_OPTIONS.height); + }, 1); + } }); if (isSecondInstance) { @@ -78,14 +91,23 @@ if (!settings.get('enableGPUAcceleration')) { const createWindow = () => { // Remember window size const mainWindowState = windowStateKeeper({ - defaultWidth: 800, - defaultHeight: 600, + defaultWidth: DEFAULT_WINDOW_OPTIONS.width, + defaultHeight: DEFAULT_WINDOW_OPTIONS.height, }); + let posX = mainWindowState.x || DEFAULT_WINDOW_OPTIONS.x; + let posY = mainWindowState.y || DEFAULT_WINDOW_OPTIONS.y; + + if (!isPositionValid({ x: posX, y: posY })) { + debug('Window is out of screen bounds, resetting window'); + posX = DEFAULT_WINDOW_OPTIONS.x; + posY = DEFAULT_WINDOW_OPTIONS.y; + } + // Create the browser window. mainWindow = new BrowserWindow({ - x: mainWindowState.x, - y: mainWindowState.y, + x: posX, + y: posY, width: mainWindowState.width, height: mainWindowState.height, minWidth: 600, @@ -187,7 +209,20 @@ const createWindow = () => { // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. -app.on('ready', createWindow); +app.on('ready', () => { + if (process.platform === 'win32') { + app.setUserTasks([{ + program: process.execPath, + arguments: `${isDevMode ? `${__dirname} ` : ''}--reset-window`, + iconPath: path.join(`${__dirname}`, '../src/assets/images/taskbar/win32/display.ico'), + iconIndex: 0, + title: 'Move Franz to Current Display', + description: 'Restore the position and size of Franz', + }]); + } + + createWindow(); +}); // This is the worst possible implementation as the webview.webContents based callback doesn't work 🖕 app.on('login', (event, webContents, request, authInfo, callback) => { -- cgit v1.2.3-70-g09d2