@felfhenor/electron 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -86,6 +86,7 @@ async function onReady(options, getWindow, setWindow) {
86
86
  setWindow(win);
87
87
  (0, save_before_close_1.watchSaveBeforeClose)(win);
88
88
  (0, lifecycle_1.watchWindowLifecycle)(win);
89
+ (0, window_1.windowWatchState)(win);
89
90
  loadGame(win, devUrl);
90
91
  }
91
92
  // Squirrel launches the app with install/uninstall flags to manage shortcuts; those runs must quit at once.
package/ipc.d.ts CHANGED
@@ -14,6 +14,12 @@ export declare const CHANNELS: {
14
14
  readonly saveBeforeClose: "fel:window:save-before-close";
15
15
  readonly saveBeforeCloseReady: "fel:window:save-before-close:ready";
16
16
  readonly saveBeforeCloseDone: "fel:window:save-before-close:done";
17
+ readonly windowState: "fel:window:state";
18
+ readonly windowChanged: "fel:window:changed";
19
+ readonly windowSetFullscreen: "fel:window:set-fullscreen";
20
+ readonly windowSetSize: "fel:window:set-size";
21
+ readonly windowDevTools: "fel:window:devtools";
22
+ readonly appQuit: "fel:app:quit";
17
23
  readonly steamAchievements: "fel:steam:achievements";
18
24
  readonly steamAchievementClear: "fel:steam:achievement-clear";
19
25
  readonly steamPresence: "fel:steam:presence";
package/ipc.js CHANGED
@@ -23,6 +23,12 @@ exports.CHANNELS = {
23
23
  saveBeforeClose: 'fel:window:save-before-close',
24
24
  saveBeforeCloseReady: 'fel:window:save-before-close:ready',
25
25
  saveBeforeCloseDone: 'fel:window:save-before-close:done',
26
+ windowState: 'fel:window:state',
27
+ windowChanged: 'fel:window:changed',
28
+ windowSetFullscreen: 'fel:window:set-fullscreen',
29
+ windowSetSize: 'fel:window:set-size',
30
+ windowDevTools: 'fel:window:devtools',
31
+ appQuit: 'fel:app:quit',
26
32
  steamAchievements: 'fel:steam:achievements',
27
33
  steamAchievementClear: 'fel:steam:achievement-clear',
28
34
  steamPresence: 'fel:steam:presence',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@felfhenor/electron",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Desktop shell for felfhenor games: disk saves, backups, logs, window handling, Steam and Discord.",
5
5
  "license": "UNLICENSED",
6
6
  "main": "./index.js",
package/preload.d.ts CHANGED
@@ -2,3 +2,4 @@ declare const contextBridge: Electron.ContextBridge, ipcRenderer: Electron.IpcRe
2
2
  declare const felInvoke: (channel: string, ...args: unknown[]) => Promise<any>;
3
3
  declare const felSend: (channel: string, ...args: unknown[]) => void;
4
4
  declare function felOnSaveBeforeClose(callback: () => Promise<boolean>): () => void;
5
+ declare function felOnWindowState(callback: (state: unknown) => void): () => void;
package/preload.js CHANGED
@@ -14,6 +14,11 @@ function felOnSaveBeforeClose(callback) {
14
14
  felSend('fel:window:save-before-close:ready');
15
15
  return () => ipcRenderer.removeListener('fel:window:save-before-close', listener);
16
16
  }
17
+ function felOnWindowState(callback) {
18
+ const listener = (_event, state) => callback(state);
19
+ ipcRenderer.on('fel:window:changed', listener);
20
+ return () => ipcRenderer.removeListener('fel:window:changed', listener);
21
+ }
17
22
  contextBridge.exposeInMainWorld('felHost', {
18
23
  platform: 'electron',
19
24
  files: {
@@ -30,7 +35,13 @@ contextBridge.exposeInMainWorld('felHost', {
30
35
  window: {
31
36
  flash: () => felSend('fel:window:flash'),
32
37
  onSaveBeforeClose: felOnSaveBeforeClose,
38
+ state: () => felInvoke('fel:window:state'),
39
+ onStateChange: felOnWindowState,
40
+ setFullscreen: (fullscreen) => felInvoke('fel:window:set-fullscreen', fullscreen),
41
+ setSize: (width, height) => felInvoke('fel:window:set-size', width, height),
42
+ toggleDevTools: () => felSend('fel:window:devtools'),
33
43
  },
44
+ quit: () => felSend('fel:app:quit'),
34
45
  notify: (title, body) => felSend('fel:window:notify', title, body),
35
46
  openFolder: (kind) => felInvoke('fel:open-folder', kind),
36
47
  exportWithLogs: (json) => felInvoke('fel:export-with-logs', json),
package/window.d.ts CHANGED
@@ -4,4 +4,15 @@ import type { FelElectronOptions } from './config';
4
4
  export declare function windowRestorableBounds(bounds?: Rectangle): Rectangle | undefined;
5
5
  export declare function windowCreate(options: FelElectronOptions, preload: string, devOrigin?: string): BrowserWindow;
6
6
  export declare function windowBringToFront(win: BrowserWindow): void;
7
+ export declare function windowState(win: BrowserWindow, fullscreen?: boolean): {
8
+ fullscreen: boolean;
9
+ maximized: boolean;
10
+ width: number;
11
+ height: number;
12
+ minWidth: number;
13
+ minHeight: number;
14
+ maxWidth: number;
15
+ maxHeight: number;
16
+ };
17
+ export declare function windowWatchState(win: BrowserWindow): void;
7
18
  export declare function windowRegisterHandlers(getWindow: () => BrowserWindow | undefined, root: string): void;
package/window.js CHANGED
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.windowRestorableBounds = windowRestorableBounds;
7
7
  exports.windowCreate = windowCreate;
8
8
  exports.windowBringToFront = windowBringToFront;
9
+ exports.windowState = windowState;
10
+ exports.windowWatchState = windowWatchState;
9
11
  exports.windowRegisterHandlers = windowRegisterHandlers;
10
12
  const electron_1 = require("electron");
11
13
  const fs_1 = __importDefault(require("fs"));
@@ -171,8 +173,82 @@ function windowNotify(getWindow, iconDir, title, body) {
171
173
  liveNotifications.shift();
172
174
  notification.show();
173
175
  }
176
+ // Sizes cross IPC in physical pixels so presets mean the same thing at any display scaling.
177
+ function windowState(win, fullscreen = win.isFullScreen()) {
178
+ const { workArea, scaleFactor } = electron_1.screen.getDisplayMatching(win.getNormalBounds());
179
+ const [outerWidth, outerHeight] = win.getSize();
180
+ const [contentWidth, contentHeight] = win.getContentSize();
181
+ const [minOuterWidth, minOuterHeight] = win.getMinimumSize();
182
+ const frameWidth = outerWidth - contentWidth;
183
+ const frameHeight = outerHeight - contentHeight;
184
+ const px = (dip) => Math.round(dip * scaleFactor);
185
+ return {
186
+ fullscreen,
187
+ maximized: win.isMaximized(),
188
+ width: px(contentWidth),
189
+ height: px(contentHeight),
190
+ minWidth: px(minOuterWidth - frameWidth),
191
+ minHeight: px(minOuterHeight - frameHeight),
192
+ maxWidth: px(workArea.width - frameWidth),
193
+ maxHeight: px(workArea.height - frameHeight),
194
+ };
195
+ }
196
+ let pushTimer;
197
+ function windowPushState(win) {
198
+ clearTimeout(pushTimer);
199
+ if (!win.isDestroyed())
200
+ win.webContents.send(ipc_1.CHANNELS.windowChanged, windowState(win));
201
+ }
202
+ // Fullscreen and maximize push at once (macOS only flips isFullScreen when these fire); drags settle first.
203
+ function windowWatchState(win) {
204
+ ['enter-full-screen', 'leave-full-screen', 'maximize', 'unmaximize'].forEach((event) => win.on(event, () => windowPushState(win)));
205
+ ['resize', 'move'].forEach((event) => win.on(event, () => {
206
+ clearTimeout(pushTimer);
207
+ pushTimer = setTimeout(() => windowPushState(win), 150);
208
+ }));
209
+ }
210
+ // Sizes come from the renderer, so anything outside what the display and window minimum allow is ignored.
211
+ function windowSetSize(win, width, height) {
212
+ const state = windowState(win);
213
+ const isValid = Number.isInteger(width) &&
214
+ Number.isInteger(height) &&
215
+ width >= state.minWidth &&
216
+ height >= state.minHeight &&
217
+ width <= state.maxWidth &&
218
+ height <= state.maxHeight;
219
+ if (isValid && !win.isFullScreen()) {
220
+ if (win.isMaximized())
221
+ win.unmaximize();
222
+ const { scaleFactor } = electron_1.screen.getDisplayMatching(win.getNormalBounds());
223
+ win.setContentSize(Math.round(width / scaleFactor), Math.round(height / scaleFactor));
224
+ win.center();
225
+ }
226
+ return windowState(win);
227
+ }
228
+ function windowRegisterStateHandlers(getWindow) {
229
+ (0, ipc_1.ipcHandle)(ipc_1.CHANNELS.windowState, () => {
230
+ const win = getWindow();
231
+ return win ? windowState(win) : undefined;
232
+ });
233
+ // Reports the requested mode, since the fullscreen transition can still be animating.
234
+ (0, ipc_1.ipcHandle)(ipc_1.CHANNELS.windowSetFullscreen, (fullscreen) => {
235
+ const win = getWindow();
236
+ if (!win)
237
+ return undefined;
238
+ win.setFullScreen(!!fullscreen);
239
+ return windowState(win, !!fullscreen);
240
+ });
241
+ (0, ipc_1.ipcHandle)(ipc_1.CHANNELS.windowSetSize, (width, height) => {
242
+ const win = getWindow();
243
+ return win ? windowSetSize(win, width, height) : undefined;
244
+ });
245
+ (0, ipc_1.ipcOn)(ipc_1.CHANNELS.windowDevTools, () => getWindow()?.webContents.toggleDevTools());
246
+ // Same path as closing the window, so save-before-close still runs.
247
+ (0, ipc_1.ipcOn)(ipc_1.CHANNELS.appQuit, () => electron_1.app.quit());
248
+ }
174
249
  // Both only act while the window is unfocused: the player is already looking otherwise.
175
250
  function windowRegisterHandlers(getWindow, root) {
251
+ windowRegisterStateHandlers(getWindow);
176
252
  (0, ipc_1.ipcOn)(ipc_1.CHANNELS.flash, () => {
177
253
  const win = getWindow();
178
254
  if (win && !win.isFocused())