11import { existsSync , mkdirSync , readFileSync , renameSync , writeFileSync } from "node:fs"
22import path from "node:path"
33import type { BrowserWindow , Rectangle } from "electron"
4+ import { Option , Schema } from "effect"
45
56// Remembers a window's bounds and maximized / fullscreen flags across launches in the same JSON
67// file electron-window-state wrote, so existing profiles restore where they were.
78
9+ const Bounds = Schema . Struct ( {
10+ x : Schema . Int ,
11+ y : Schema . Int ,
12+ width : Schema . Int ,
13+ height : Schema . Int ,
14+ } )
15+
16+ const SavedWindowState = Schema . Struct ( {
17+ x : Schema . optional ( Schema . Int ) ,
18+ y : Schema . optional ( Schema . Int ) ,
19+ width : Schema . optional ( Schema . Int . check ( Schema . isGreaterThan ( 0 ) ) ) ,
20+ height : Schema . optional ( Schema . Int . check ( Schema . isGreaterThan ( 0 ) ) ) ,
21+ displayBounds : Schema . optional ( Bounds ) ,
22+ isMaximized : Schema . optional ( Schema . Boolean ) ,
23+ isFullScreen : Schema . optional ( Schema . Boolean ) ,
24+ } )
25+
26+ const SavedWindowFlags = Schema . Struct ( {
27+ isMaximized : Schema . optional ( Schema . Boolean ) ,
28+ isFullScreen : Schema . optional ( Schema . Boolean ) ,
29+ } )
30+
31+ export type SavedWindowState = typeof SavedWindowState . Type
32+
833export type WindowState = {
934 x ?: number
1035 y ?: number
@@ -24,21 +49,19 @@ export type Displays = {
2449// Bounds are only trusted when they still fit on one of the current displays; otherwise the window
2550// falls back to the default size on the primary display. A saved maximized or fullscreen state
2651// without bounds is kept so manage() can restore it.
27- export function resolveWindowState ( saved : unknown , defaults : { width : number ; height : number } , displays : Displays ) {
28- const state = isState ( saved ) ? saved : undefined
29-
30- if ( ! state ) return { width : defaults . width , height : defaults . height } satisfies WindowState
52+ export function resolveWindowState ( saved : SavedWindowState | undefined , defaults : { width : number ; height : number } , displays : Displays ) {
53+ if ( ! saved ) return { width : defaults . width , height : defaults . height } satisfies WindowState
3154
32- if ( ! hasBounds ( state ) ) {
33- return state . isMaximized || state . isFullScreen
34- ? ( { ...state , width : defaults . width , height : defaults . height } satisfies WindowState )
55+ if ( ! hasBounds ( saved ) ) {
56+ return saved . isMaximized || saved . isFullScreen
57+ ? ( { ...saved , width : defaults . width , height : defaults . height } satisfies WindowState )
3558 : ( { width : defaults . width , height : defaults . height } satisfies WindowState )
3659 }
3760
38- if ( ! state . displayBounds ) return state
39- const visible = displays . all ( ) . some ( ( bounds ) => within ( state , bounds ) )
61+ if ( ! saved . displayBounds ) return saved
62+ const visible = displays . all ( ) . some ( ( bounds ) => within ( saved , bounds ) )
4063
41- if ( visible ) return state
64+ if ( visible ) return saved
4265
4366 return {
4467 width : defaults . width ,
@@ -53,11 +76,24 @@ export function windowStateFile(id: string) {
5376 return `window-state-${ id . replace ( / [ ^ a - z A - Z 0 - 9 . _ - ] / g, "-" ) } .json`
5477}
5578
56- export function readWindowState ( file : string ) : unknown {
79+ const decodeWindowState = Schema . decodeUnknownOption ( Schema . fromJsonString ( SavedWindowState ) )
80+
81+ const decodeWindowFlags = Schema . decodeUnknownOption ( Schema . fromJsonString ( SavedWindowFlags ) )
82+
83+ export function readWindowState ( file : string ) : SavedWindowState | undefined {
5784 if ( ! existsSync ( file ) ) return undefined
5885
5986 try {
60- return JSON . parse ( readFileSync ( file , "utf8" ) )
87+ const contents = readFileSync ( file , "utf8" )
88+ const state = Option . getOrUndefined ( decodeWindowState ( contents ) )
89+
90+ if ( state ) return state
91+
92+ // Keep the flags when only the bounds are unusable; resolveWindowState already falls back
93+ // to the default size for those, and the window should still restore maximized/fullscreen.
94+ const flags = Option . getOrUndefined ( decodeWindowFlags ( contents ) )
95+
96+ return flags && ( flags . isMaximized !== undefined || flags . isFullScreen !== undefined ) ? flags : undefined
6197 } catch {
6298 return undefined
6399 }
@@ -111,26 +147,19 @@ export function manageWindowState(win: BrowserWindow, file: string, initial: Win
111147 win . on ( "closed" , closed )
112148}
113149
114- function isState ( value : unknown ) : value is WindowState {
115- return typeof value === "object" && value !== null
150+ function hasBounds ( state : SavedWindowState ) : state is WindowState & { x : number ; y : number } {
151+ return state . x !== undefined && state . y !== undefined && state . width !== undefined && state . height !== undefined
116152}
117153
118- function hasBounds ( state : WindowState ) : state is WindowState & Required < Pick < WindowState , "x" | "y" > > {
119- return (
120- Number . isInteger ( state . x ) &&
121- Number . isInteger ( state . y ) &&
122- Number . isInteger ( state . width ) &&
123- state . width > 0 &&
124- Number . isInteger ( state . height ) &&
125- state . height > 0
126- )
127- }
154+ // Windows reports a window snapped to a screen edge slightly outside the display because of its
155+ // invisible resize borders. Keep those saved bounds instead of falling back to the default window.
156+ const EDGE_TOLERANCE = 16
128157
129158function within ( state : WindowState & { x : number ; y : number } , bounds : Rectangle ) {
130159 return (
131- state . x >= bounds . x &&
132- state . y >= bounds . y &&
133- state . x + state . width <= bounds . x + bounds . width &&
134- state . y + state . height <= bounds . y + bounds . height
160+ state . x >= bounds . x - EDGE_TOLERANCE &&
161+ state . y >= bounds . y - EDGE_TOLERANCE &&
162+ state . x + state . width <= bounds . x + bounds . width + EDGE_TOLERANCE &&
163+ state . y + state . height <= bounds . y + bounds . height + EDGE_TOLERANCE
135164 )
136165}
0 commit comments