Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:electron: Convert window-state.js ➡️ window-state.ts #3027

Merged
merged 7 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/desktop-electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Menu,
dialog,
shell,
powerMonitor,
protocol,
utilityProcess,
UtilityProcess,
Expand Down Expand Up @@ -241,7 +242,7 @@ app.on('ready', async () => {

// This is mainly to aid debugging Sentry errors - it will add a
// breadcrumb
require('electron').powerMonitor.on('suspend', () => {
powerMonitor.on('suspend', () => {
console.log('Suspending', new Date());
});

Expand Down
1 change: 1 addition & 0 deletions packages/desktop-electron/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ declare module 'module' {
// bundles not available until we build them
declare module 'loot-core/lib-dist/bundle.desktop.js' {
const initApp: (isDev: boolean) => void;
const lib: { getDataDir: () => string };
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';

const electron = require('electron');
import electron, { BrowserWindow } from 'electron';

// eslint-disable-next-line import/extensions
const backend = require('loot-core/lib-dist/bundle.desktop.js');
const backend = undefined;
const getBackend = async () =>
// eslint-disable-next-line import/extensions
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved
backend || (await import('loot-core/lib-dist/bundle.desktop.js'));

function loadState() {
let state = {};
type WindowState = Electron.Rectangle & {
isMaximized?: boolean;
isFullScreen?: boolean;
displayBounds?: Electron.Rectangle;
};

async function loadState() {
let state: WindowState | undefined = undefined;
const backend = await getBackend();
try {
state = JSON.parse(
fs.readFileSync(
Expand All @@ -18,24 +27,27 @@ function loadState() {
} catch (e) {
console.log('Could not load window state');
}

return validateState(state);
}

function updateState(win, state) {
const screen = electron.screen || electron.remote.screen;
function updateState(win: BrowserWindow, state: WindowState) {
const screen = electron.screen;
const bounds = win.getBounds();
if (!win.isMaximized() && !win.isMinimized() && !win.isFullScreen()) {
state.x = bounds.x;
state.y = bounds.y;
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved
state.width = bounds.width;
state.height = bounds.height;
}

state.x = bounds.x;
state.y = bounds.y;
state.isMaximized = win.isMaximized();
state.isFullScreen = win.isFullScreen();
state.displayBounds = screen.getDisplayMatching(bounds).bounds;
}

function saveState(win, state) {
async function saveState(win: BrowserWindow, state: WindowState) {
const backend = await getBackend();
updateState(win, state);
fs.writeFileSync(
path.join(backend.lib.getDataDir(), 'window.json'),
Expand All @@ -44,7 +56,7 @@ function saveState(win, state) {
);
}

function listen(win, state) {
export function listen(win: BrowserWindow, state: WindowState) {
if (state.isMaximized) {
win.maximize();
}
Expand All @@ -61,7 +73,7 @@ function listen(win, state) {
};
}

function hasBounds(state) {
function hasBounds(state: WindowState) {
return (
Number.isInteger(state.x) &&
Number.isInteger(state.y) &&
Expand All @@ -72,15 +84,18 @@ function hasBounds(state) {
);
}

function validateState(state) {
if (!(hasBounds(state) || state.isMaximized || state.isFullScreen)) {
function validateState(state?: WindowState): Partial<WindowState> {
if (
!state ||
!(hasBounds(state) || state.isMaximized || state.isFullScreen)
) {
return {};
}

const newState = Object.assign({}, state);

if (hasBounds(state) && state.displayBounds) {
const screen = electron.screen || electron.remote.screen;
const screen = electron.screen;
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved

// Check if the display where the window was last open is still available
const displayBounds = screen.getDisplayMatching(state).bounds;
Expand Down Expand Up @@ -116,22 +131,19 @@ function validateState(state) {
return newState;
}

async function get() {
const screen = electron.screen || electron.remote.screen;
export async function get() {
const screen = electron.screen;
const displayBounds = screen.getPrimaryDisplay().bounds;

let state = loadState();
state = Object.assign(
const state: WindowState = Object.assign(
{
x: 100,
y: 50,
width: Math.min(1000, displayBounds.width - 100),
height: Math.min(700, displayBounds.width - 50),
},
state,
await loadState(),
);

return state;
}

module.exports = { get, listen };
6 changes: 6 additions & 0 deletions upcoming-release-notes/3027.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MikesGlitch]
---

Updated Electron window-state file to use typescript
Loading