Skip to content
Snippets Groups Projects
Unverified Commit b85f9102 authored by Michael Clark's avatar Michael Clark Committed by GitHub
Browse files

:electron: Convert window-state.js :arrow_right: window-state.ts (#3027)

parent 942aebed
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
Menu, Menu,
dialog, dialog,
shell, shell,
powerMonitor,
protocol, protocol,
utilityProcess, utilityProcess,
UtilityProcess, UtilityProcess,
...@@ -241,7 +242,7 @@ app.on('ready', async () => { ...@@ -241,7 +242,7 @@ app.on('ready', async () => {
// This is mainly to aid debugging Sentry errors - it will add a // This is mainly to aid debugging Sentry errors - it will add a
// breadcrumb // breadcrumb
require('electron').powerMonitor.on('suspend', () => { powerMonitor.on('suspend', () => {
console.log('Suspending', new Date()); console.log('Suspending', new Date());
}); });
......
...@@ -5,4 +5,5 @@ declare module 'module' { ...@@ -5,4 +5,5 @@ declare module 'module' {
// bundles not available until we build them // bundles not available until we build them
declare module 'loot-core/lib-dist/bundle.desktop.js' { declare module 'loot-core/lib-dist/bundle.desktop.js' {
const initApp: (isDev: boolean) => void; const initApp: (isDev: boolean) => void;
const lib: { getDataDir: () => string };
} }
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const electron = require('electron'); import electron, { BrowserWindow } from 'electron';
// eslint-disable-next-line import/extensions const backend = undefined;
const backend = require('loot-core/lib-dist/bundle.desktop.js'); const getBackend = async () =>
// eslint-disable-next-line import/extensions
backend || (await import('loot-core/lib-dist/bundle.desktop.js'));
function loadState() { type WindowState = Electron.Rectangle & {
let state = {}; isMaximized?: boolean;
isFullScreen?: boolean;
displayBounds?: Electron.Rectangle;
};
async function loadState() {
let state: WindowState | undefined = undefined;
const backend = await getBackend();
try { try {
state = JSON.parse( state = JSON.parse(
fs.readFileSync( fs.readFileSync(
...@@ -18,24 +27,27 @@ function loadState() { ...@@ -18,24 +27,27 @@ function loadState() {
} catch (e) { } catch (e) {
console.log('Could not load window state'); console.log('Could not load window state');
} }
return validateState(state); return validateState(state);
} }
function updateState(win, state) { function updateState(win: BrowserWindow, state: WindowState) {
const screen = electron.screen || electron.remote.screen; const screen = electron.screen;
const bounds = win.getBounds(); const bounds = win.getBounds();
if (!win.isMaximized() && !win.isMinimized() && !win.isFullScreen()) { if (!win.isMaximized() && !win.isMinimized() && !win.isFullScreen()) {
state.x = bounds.x;
state.y = bounds.y;
state.width = bounds.width; state.width = bounds.width;
state.height = bounds.height; state.height = bounds.height;
} }
state.x = bounds.x;
state.y = bounds.y;
state.isMaximized = win.isMaximized(); state.isMaximized = win.isMaximized();
state.isFullScreen = win.isFullScreen(); state.isFullScreen = win.isFullScreen();
state.displayBounds = screen.getDisplayMatching(bounds).bounds; state.displayBounds = screen.getDisplayMatching(bounds).bounds;
} }
function saveState(win, state) { async function saveState(win: BrowserWindow, state: WindowState) {
const backend = await getBackend();
updateState(win, state); updateState(win, state);
fs.writeFileSync( fs.writeFileSync(
path.join(backend.lib.getDataDir(), 'window.json'), path.join(backend.lib.getDataDir(), 'window.json'),
...@@ -44,7 +56,7 @@ function saveState(win, state) { ...@@ -44,7 +56,7 @@ function saveState(win, state) {
); );
} }
function listen(win, state) { export function listen(win: BrowserWindow, state: WindowState) {
if (state.isMaximized) { if (state.isMaximized) {
win.maximize(); win.maximize();
} }
...@@ -61,7 +73,7 @@ function listen(win, state) { ...@@ -61,7 +73,7 @@ function listen(win, state) {
}; };
} }
function hasBounds(state) { function hasBounds(state: WindowState) {
return ( return (
Number.isInteger(state.x) && Number.isInteger(state.x) &&
Number.isInteger(state.y) && Number.isInteger(state.y) &&
...@@ -72,15 +84,18 @@ function hasBounds(state) { ...@@ -72,15 +84,18 @@ function hasBounds(state) {
); );
} }
function validateState(state) { function validateState(state?: WindowState): Partial<WindowState> {
if (!(hasBounds(state) || state.isMaximized || state.isFullScreen)) { if (
!state ||
!(hasBounds(state) || state.isMaximized || state.isFullScreen)
) {
return {}; return {};
} }
const newState = Object.assign({}, state); const newState = Object.assign({}, state);
if (hasBounds(state) && state.displayBounds) { if (hasBounds(state) && state.displayBounds) {
const screen = electron.screen || electron.remote.screen; const screen = electron.screen;
// Check if the display where the window was last open is still available // Check if the display where the window was last open is still available
const displayBounds = screen.getDisplayMatching(state).bounds; const displayBounds = screen.getDisplayMatching(state).bounds;
...@@ -116,22 +131,19 @@ function validateState(state) { ...@@ -116,22 +131,19 @@ function validateState(state) {
return newState; return newState;
} }
async function get() { export async function get() {
const screen = electron.screen || electron.remote.screen; const screen = electron.screen;
const displayBounds = screen.getPrimaryDisplay().bounds; const displayBounds = screen.getPrimaryDisplay().bounds;
let state = loadState(); const state: WindowState = Object.assign(
state = Object.assign(
{ {
x: 100, x: 100,
y: 50, y: 50,
width: Math.min(1000, displayBounds.width - 100), width: Math.min(1000, displayBounds.width - 100),
height: Math.min(700, displayBounds.width - 50), height: Math.min(700, displayBounds.width - 50),
}, },
state, await loadState(),
); );
return state; return state;
} }
module.exports = { get, listen };
---
category: Maintenance
authors: [MikesGlitch]
---
Updated Electron window-state file to use typescript
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment