Skip to content

Commit 3666898

Browse files
committed
Share how the website and Actions windows dock on close, with DockOnClose
The fix for the crash on close changed code the two windows had copied (a copy jscpd's baseline listed): DockOnClose now tells a close from the quit and docks once the close event is over, for both. The baseline loses that copy.
1 parent 5564dbe commit 3666898

5 files changed

Lines changed: 43 additions & 44 deletions

File tree

‎.jscpd-baseline.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"version": 1,
33
"fingerprints": {
44
"04bf8b15d3fc15f5": 1,
5-
"17ed5f57aae4aded": 1,
65
"3406d58fbb1964c0": 1,
76
"5527378cebdd03f1": 1,
87
"8a060e80d0b739f6": 1,

‎src/main/ActionsWindow/ActionsWindow.ts‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { app, screen, type BrowserWindow, type WebContents } from 'electron';
1+
import { screen, type BrowserWindow, type WebContents } from 'electron';
22
import { ACTIONS_WINDOW_HASH } from '../../shared/constants';
33
import { IPC_CHANNEL } from '../../shared/ipcChannels';
44
import type { ActionsWindowState, AppEvent } from '../../shared/types';
55
import { loadEditor } from '../launch/loadEditor';
6-
import { createAppWindow, placeWindow, setUpWindow, syncMenuCheck, trackPlacement } from '../windows';
6+
import { createAppWindow, DockOnClose, placeWindow, setUpWindow, syncMenuCheck, trackPlacement } from '../windows';
77
import { ACTIONS_WINDOW_MENU_ID, ACTIONS_WINDOW_SIZE, ACTIONS_WINDOW_TITLE, FORWARDED_EVENTS, MIN_ACTIONS_WINDOW_SIZE } from './constants';
88
import type { ActionsWindowDeps } from './types';
99

@@ -17,12 +17,10 @@ export class ActionsWindow {
1717
private win: BrowserWindow | undefined;
1818
/** Saves the window's bounds at once (they are also saved as it moves). */
1919
private savePlacement: (() => void) | undefined;
20-
/** The app is quitting, and hasn't asked the window to close yet. */
21-
private quitting = false;
20+
/** Closing the window docks the panel. */
21+
private readonly close = new DockOnClose(() => this.detached, () => this.attach());
2222

23-
constructor(private readonly deps: ActionsWindowDeps) {
24-
app.on('before-quit', () => (this.quitting = this.detached));
25-
}
23+
constructor(private readonly deps: ActionsWindowDeps) {}
2624

2725
get detached(): boolean {
2826
return this.win !== undefined;
@@ -47,7 +45,7 @@ export class ActionsWindow {
4745
const win = createAppWindow(bounds, { title: ACTIONS_WINDOW_TITLE, minSize: MIN_ACTIONS_WINDOW_SIZE });
4846
this.win = win;
4947
this.savePlacement = trackPlacement(win, store);
50-
setUpWindow(win, { closing: () => this.closing(), maximized: !!saved.maximized });
48+
setUpWindow(win, { closing: () => this.close.closing(), maximized: !!saved.maximized });
5149
win.setAlwaysOnTop(!!saved.onTop);
5250
void store.update({ detached: true });
5351
this.announce();
@@ -90,18 +88,6 @@ export class ActionsWindow {
9088
if (win && !win.isDestroyed()) win.destroy();
9189
}
9290

93-
/**
94-
* The window is asked to close: it docks the panel instead, once the close event is over (as the website's
95-
* window does: see PageWindow), unless the app quits.
96-
*/
97-
private closing(): void {
98-
if (this.quitting) {
99-
this.quitting = false;
100-
return;
101-
}
102-
setImmediate(() => this.attach());
103-
}
104-
10591
private show(win: BrowserWindow): void {
10692
if (win.isMinimized()) win.restore();
10793
win.focus();

‎src/main/PageWindow/PageWindow.ts‎

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { app, screen, type BrowserWindow, type WebContents } from 'electron';
1+
import { screen, type BrowserWindow, type WebContents } from 'electron';
22
import { PAGE_WINDOW_HASH } from '../../shared/constants';
33
import { IPC_CHANNEL } from '../../shared/ipcChannels';
44
import type { AppEvent, PageState, Rect } from '../../shared/types';
55
import { loadEditor } from '../launch/loadEditor';
66
import { AddressBarFocus } from './AddressBarFocus';
7-
import { createAppWindow, placeWindow, setUpWindow, syncMenuCheck, trackPlacement } from '../windows';
7+
import { createAppWindow, DockOnClose, placeWindow, setUpWindow, syncMenuCheck, trackPlacement } from '../windows';
88
import { DEFAULT_WINDOW_SIZE, MIN_WINDOW_SIZE, PAGE_WINDOW_MENU_ID, UNTITLED } from './constants';
99
import { toViewBounds } from './toViewBounds';
1010
import type { PageWindowDeps } from './types';
@@ -24,12 +24,10 @@ export class PageWindow {
2424
/** Saves the website window's bounds at once (they are also saved as it moves). */
2525
private savePlacement: (() => void) | undefined;
2626
private focus: AddressBarFocus | undefined;
27-
/** The app is quitting, and hasn't asked the website window to close yet. */
28-
private quitting = false;
27+
/** Closing the website window puts the website back. */
28+
private readonly close = new DockOnClose(() => this.detached, () => this.attach());
2929

30-
constructor(private readonly deps: PageWindowDeps) {
31-
app.on('before-quit', () => (this.quitting = this.detached));
32-
}
30+
constructor(private readonly deps: PageWindowDeps) {}
3331

3432
get detached(): boolean {
3533
return this.win !== undefined;
@@ -64,7 +62,7 @@ export class PageWindow {
6462
this.win = win;
6563
this.focus = new AddressBarFocus(win);
6664
this.savePlacement = trackPlacement(win, store);
67-
setUpWindow(win, { closing: () => this.closing(), maximized: !!saved.maximized });
65+
setUpWindow(win, { closing: () => this.close.closing(), maximized: !!saved.maximized });
6866
this.moveView(editor, win);
6967
void store.update({ detached: true });
7068
this.announce();
@@ -108,21 +106,6 @@ export class PageWindow {
108106
if (win && !win.isDestroyed()) win.destroy();
109107
}
110108

111-
/**
112-
* The website window is asked to close: it puts the website back instead, once the close event is over. Not in a
113-
* microtask: that runs before the event returns to Electron, which then goes on with a window destroyed under it
114-
* (the app crashed now and then).
115-
*/
116-
private closing(): void {
117-
// By the quit: it stays as it is, to go with the editor's window (`dispose`) and open again next time. Should
118-
// the editor stay (its drafts couldn't be written), nothing has moved.
119-
if (this.quitting) {
120-
this.quitting = false;
121-
return;
122-
}
123-
setImmediate(() => this.attach());
124-
}
125-
126109
/** Moves the view back into the editor and destroys the website window. Returns whether there was one. */
127110
private putBack(): boolean {
128111
const win = this.win;

‎src/main/windows/DockOnClose.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { app } from 'electron';
2+
3+
/**
4+
* What a window of its own does when asked to close (`setUpWindow`'s `closing`): it docks what it shows back in
5+
* the editor's window, unless the app is quitting. The quit leaves it as it is, to go with the editor's window
6+
* (its owner's `dispose`) and open there again next time; should the editor stay (its drafts couldn't be written),
7+
* nothing has moved.
8+
*/
9+
export class DockOnClose {
10+
/** The app is quitting, and hasn't asked the window to close yet. */
11+
private quitting = false;
12+
13+
constructor(
14+
detached: () => boolean,
15+
private readonly dock: () => void,
16+
) {
17+
app.on('before-quit', () => (this.quitting = detached()));
18+
}
19+
20+
/** The window is asked to close (and stays: `setUpWindow` cancels the close). */
21+
closing(): void {
22+
if (this.quitting) {
23+
this.quitting = false;
24+
return;
25+
}
26+
// Once the close event is over. Not in a microtask: that runs before the event returns to Electron, which then
27+
// goes on with a window `dock` destroyed under it (the app crashed now and then).
28+
setImmediate(this.dock);
29+
}
30+
}

‎src/main/windows/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { createAppWindow } from './createAppWindow';
2+
export { DockOnClose } from './DockOnClose';
23
export { placeWindow } from './placeWindow';
34
export { setUpWindow } from './setUpWindow';
45
export { syncMenuCheck } from './syncMenuCheck';

0 commit comments

Comments
 (0)