UNPKG

electron-event-flux

Version:

Redux store which synchronizes between instances in multiple process

150 lines (149 loc) 5.81 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var electron = require('electron'); var deepEqual = require('deep-equal'); var isInteger = Number.isInteger; var eventHandlingDelay = 100; function isNumber(num) { return typeof typeof (num) === 'number' && !isNaN(num); } var ElectronWindowState = /** @class */ (function () { function ElectronWindowState(config, state, onSave) { this.state = {}; this.onSave = onSave; this.stateChangeHandler = this.stateChangeHandler.bind(this); this.closeHandler = this.closeHandler.bind(this); this.closedHandler = this.closedHandler.bind(this); this.loadState(state, config || {}); } ElectronWindowState.prototype.loadState = function (state, config) { this.state = this.normState(state); // Check state validity this.validateState(); // Set state fallback values this.state = Object.assign({ width: config.defaultWidth || 800, height: config.defaultHeight || 600, useContentSize: config.useContentSize || false, }, this.state); }; ElectronWindowState.prototype.normState = function (state) { if (isNumber(state.x)) { state.x = Math.floor(state.x); } if (isNumber(state.y)) { state.y = Math.floor(state.y); } if (isNumber(state.width)) { state.width = Math.floor(state.width); } if (isNumber(state.height)) { state.height = Math.floor(state.height); } return state; }; ElectronWindowState.prototype.isNormal = function (win) { return !win.isMaximized() && !win.isMinimized() && !win.isFullScreen(); }; ElectronWindowState.prototype.hasBounds = function () { var state = this.state; return state && isInteger(state.x) && isInteger(state.y) && isInteger(state.width) && state.width > 0 && isInteger(state.height) && state.height > 0; }; ElectronWindowState.prototype.validateState = function () { var state = this.state; if (state && (state.isMaximized || state.isFullScreen)) return; if (this.hasBounds() && state.displayBounds) { // Check if the display where the window was last open is still available var displayBounds = electron.screen.getDisplayMatching(state).bounds; var sameBounds = deepEqual(state.displayBounds, displayBounds, { strict: true }); if (!sameBounds) { if (displayBounds.width < state.displayBounds.width) { if (state.x > displayBounds.width) { state.x = 0; } if (state.width > displayBounds.width) { state.width = displayBounds.width; } } if (displayBounds.height < state.displayBounds.height) { if (state.y > displayBounds.height) { state.y = 0; } if (state.height > displayBounds.height) { state.height = displayBounds.height; } } } } }; ElectronWindowState.prototype.updateState = function () { var state = this.state; var win = this.winRef; if (!win) { return; } // don't throw an error when window was closed try { var winBounds = state.useContentSize ? win.getContentBounds() : win.getBounds(); if (this.isNormal(win)) { state.x = winBounds.x; state.y = winBounds.y; state.width = winBounds.width; state.height = winBounds.height; } state.isMaximized = win.isMaximized(); state.isFullScreen = win.isFullScreen(); state.displayBounds = electron.screen.getDisplayMatching(winBounds).bounds; } catch (err) { console.error(err); } }; ElectronWindowState.prototype.stateChangeHandler = function () { var _this = this; // Handles both 'resize' and 'move' clearTimeout(this.stateChangeTimer); this.stateChangeTimer = setTimeout(function () { return _this.updateState(); }, eventHandlingDelay); }; ElectronWindowState.prototype.closeHandler = function () { this.updateState(); }; ElectronWindowState.prototype.closedHandler = function () { // Unregister listeners and save state this.unmanage(); this.updateState(); this.onSave && this.onSave(this.state); }; ElectronWindowState.prototype.manage = function (win) { var state = this.state; if (state.isMaximized) { win.maximize(); } if (state.isFullScreen) { win.setFullScreen(true); } win.on('resize', this.stateChangeHandler); win.on('move', this.stateChangeHandler); win.on('close', this.closeHandler); win.on('closed', this.closedHandler); this.winRef = win; }; ElectronWindowState.prototype.unmanage = function () { var winRef = this.winRef; if (winRef) { winRef.removeListener('resize', this.stateChangeHandler); winRef.removeListener('move', this.stateChangeHandler); clearTimeout(this.stateChangeTimer); winRef.removeListener('close', this.closeHandler); winRef.removeListener('closed', this.closedHandler); this.winRef = null; } }; return ElectronWindowState; }()); exports.default = ElectronWindowState;