@felfhenor/electron 0.0.0-stage → 0.2.4

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/window.js ADDED
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.windowRestorableBounds = windowRestorableBounds;
7
+ exports.windowCreate = windowCreate;
8
+ exports.windowBringToFront = windowBringToFront;
9
+ exports.windowRegisterHandlers = windowRegisterHandlers;
10
+ const electron_1 = require("electron");
11
+ const fs_1 = __importDefault(require("fs"));
12
+ const path_1 = __importDefault(require("path"));
13
+ const ipc_1 = require("./ipc");
14
+ const logging_1 = require("./logging");
15
+ const protocol_1 = require("./protocol");
16
+ const stateFile = () => path_1.default.join(electron_1.app.getPath('userData'), 'window-state.json');
17
+ function readWindowState() {
18
+ try {
19
+ return JSON.parse(fs_1.default.readFileSync(stateFile(), 'utf8'));
20
+ }
21
+ catch {
22
+ return {};
23
+ }
24
+ }
25
+ function writeWindowState(state) {
26
+ try {
27
+ fs_1.default.writeFileSync(stateFile(), JSON.stringify({ ...readWindowState(), ...state }));
28
+ }
29
+ catch (e) {
30
+ (0, logging_1.coreLog)().warn('Window', 'could not save window state', e);
31
+ }
32
+ }
33
+ const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
34
+ // Saved bounds can point at an unplugged or since-resized monitor, so they're dropped or pulled back on-screen.
35
+ function windowRestorableBounds(bounds) {
36
+ if (!bounds)
37
+ return undefined;
38
+ const titleBar = { x: bounds.x + bounds.width / 2, y: bounds.y + 8 };
39
+ const display = electron_1.screen
40
+ .getAllDisplays()
41
+ .find(({ workArea: a }) => titleBar.x >= a.x &&
42
+ titleBar.x < a.x + a.width &&
43
+ titleBar.y >= a.y &&
44
+ titleBar.y < a.y + a.height);
45
+ if (!display)
46
+ return undefined;
47
+ const { workArea } = display;
48
+ const width = Math.min(bounds.width, workArea.width);
49
+ const height = Math.min(bounds.height, workArea.height);
50
+ return {
51
+ width,
52
+ height,
53
+ x: clamp(bounds.x, workArea.x, workArea.x + workArea.width - width),
54
+ y: clamp(bounds.y, workArea.y, workArea.y + workArea.height - height),
55
+ };
56
+ }
57
+ function windowOptions({ config, root }, preload) {
58
+ const areas = electron_1.screen.getAllDisplays().map((d) => d.workArea);
59
+ // Windows only: macOS traffic lights and Linux window managers would each need their own layout.
60
+ const overlay = config.titleBar === 'overlay' && process.platform === 'win32';
61
+ return {
62
+ show: false,
63
+ backgroundColor: config.windowBackground,
64
+ icon: path_1.default.join(root, 'icons', 'icon.png'),
65
+ // Capped at the smallest display so a small or heavily scaled laptop screen can still fit the window.
66
+ minWidth: Math.min(config.minWidth, ...areas.map((a) => a.width)),
67
+ minHeight: Math.min(config.minHeight, ...areas.map((a) => a.height)),
68
+ ...electron_1.screen.getPrimaryDisplay().workArea,
69
+ ...windowRestorableBounds(readWindowState().bounds),
70
+ ...(overlay
71
+ ? {
72
+ titleBarStyle: 'hidden',
73
+ titleBarOverlay: {
74
+ color: '#00000000',
75
+ symbolColor: '#e5e7eb',
76
+ height: 32,
77
+ },
78
+ }
79
+ : {}),
80
+ webPreferences: {
81
+ preload,
82
+ contextIsolation: true,
83
+ nodeIntegration: false,
84
+ sandbox: true,
85
+ autoplayPolicy: 'no-user-gesture-required',
86
+ // Otherwise Chromium throttles timers in a minimized window, stalling the loop even with "run in background" on.
87
+ backgroundThrottling: false,
88
+ },
89
+ };
90
+ }
91
+ function windowIsAllowedUrl(url, devOrigin) {
92
+ try {
93
+ const { protocol, host, origin } = new URL(url);
94
+ return ((protocol === `${protocol_1.APP_SCHEME}:` && host === protocol_1.APP_HOST) ||
95
+ origin === devOrigin);
96
+ }
97
+ catch {
98
+ return false;
99
+ }
100
+ }
101
+ // Only http(s) leaves for the browser; the window itself never navigates away from the game.
102
+ function windowGuardNavigation(win, devOrigin) {
103
+ win.webContents.setWindowOpenHandler(({ url }) => {
104
+ if (/^https?:\/\//i.test(url))
105
+ void electron_1.shell.openExternal(url);
106
+ return { action: 'deny' };
107
+ });
108
+ win.webContents.on('will-navigate', (event, url) => {
109
+ if (!windowIsAllowedUrl(url, devOrigin))
110
+ event.preventDefault();
111
+ });
112
+ }
113
+ function windowPersistState(win) {
114
+ win.once('ready-to-show', () => {
115
+ if (readWindowState().maximized && !win.isFullScreen())
116
+ win.maximize();
117
+ win.show();
118
+ });
119
+ win.on('enter-full-screen', () => writeWindowState({ fullscreen: true }));
120
+ win.on('leave-full-screen', () => writeWindowState({ fullscreen: false }));
121
+ win.on('close', () => writeWindowState({
122
+ bounds: win.getNormalBounds(),
123
+ maximized: win.isMaximized(),
124
+ }));
125
+ win.on('focus', () => win.flashFrame(false));
126
+ }
127
+ // The usual desktop-game fullscreen toggles, handled here so they work whatever the page has focused.
128
+ function windowFullscreenHotkeys(win) {
129
+ win.webContents.on('before-input-event', (event, input) => {
130
+ const toggle = input.type === 'keyDown' &&
131
+ !input.isAutoRepeat &&
132
+ (input.key === 'F11' || (input.alt && input.key === 'Enter'));
133
+ if (!toggle)
134
+ return;
135
+ event.preventDefault();
136
+ win.setFullScreen(!win.isFullScreen());
137
+ });
138
+ }
139
+ function windowCreate(options, preload, devOrigin) {
140
+ const win = new electron_1.BrowserWindow(windowOptions(options, preload));
141
+ // Only ever set to true: an explicit false disables the fullscreen button on macOS.
142
+ if (readWindowState().fullscreen)
143
+ win.setFullScreen(true);
144
+ win.setMenu(null);
145
+ windowGuardNavigation(win, devOrigin);
146
+ windowPersistState(win);
147
+ windowFullscreenHotkeys(win);
148
+ return win;
149
+ }
150
+ function windowBringToFront(win) {
151
+ if (win.isMinimized())
152
+ win.restore();
153
+ win.show();
154
+ win.focus();
155
+ }
156
+ // Held so GC keeps the click handler; Windows toasts stay clickable in the Action Center.
157
+ const liveNotifications = [];
158
+ function windowNotify(getWindow, iconDir, title, body) {
159
+ const notification = new electron_1.Notification({
160
+ title,
161
+ body,
162
+ icon: path_1.default.join(iconDir, 'icon.png'),
163
+ });
164
+ notification.on('click', () => {
165
+ const win = getWindow();
166
+ if (win && !win.isDestroyed())
167
+ windowBringToFront(win);
168
+ });
169
+ liveNotifications.push(notification);
170
+ if (liveNotifications.length > 20)
171
+ liveNotifications.shift();
172
+ notification.show();
173
+ }
174
+ // Both only act while the window is unfocused: the player is already looking otherwise.
175
+ function windowRegisterHandlers(getWindow, root) {
176
+ (0, ipc_1.ipcOn)(ipc_1.CHANNELS.flash, () => {
177
+ const win = getWindow();
178
+ if (win && !win.isFocused())
179
+ win.flashFrame(true);
180
+ });
181
+ (0, ipc_1.ipcOn)(ipc_1.CHANNELS.notify, (_event, title, body) => {
182
+ const win = getWindow();
183
+ const isValid = typeof title === 'string' && typeof body === 'string';
184
+ if (!win || win.isFocused() || !isValid || !electron_1.Notification.isSupported())
185
+ return;
186
+ windowNotify(getWindow, path_1.default.join(root, 'icons'), title, body);
187
+ });
188
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.