-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathwindow-manager.ts
More file actions
321 lines (264 loc) · 9.01 KB
/
window-manager.ts
File metadata and controls
321 lines (264 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import {
BrowserWindow, screen, shell, ipcMain,
} from 'electron';
import { join } from 'path';
import { is } from '@electron-toolkit/utils';
const isMac = process.platform === 'darwin';
const isWayland =
process.platform === 'linux' &&
(!!process.env.WAYLAND_DISPLAY || process.env.XDG_SESSION_TYPE === 'wayland');
export class WindowManager {
private window: BrowserWindow | null = null;
private windowedBounds: {
x: number;
y: number;
width: number;
height: number;
} | null = null;
private hoveringComponents: Set<string> = new Set();
private currentMode: 'window' | 'pet' = 'window';
// Track if mouse events are forcibly ignored
private forceIgnoreMouse = false;
constructor() {
ipcMain.on('renderer-ready-for-mode-change', (_event, newMode) => {
if (newMode === 'pet') {
setTimeout(() => {
this.continueSetWindowModePet();
}, 500);
} else {
setTimeout(() => {
this.continueSetWindowModeWindow();
}, 500);
}
});
ipcMain.on('mode-change-rendered', () => {
this.window?.setOpacity(1);
});
ipcMain.on('window-unfullscreen', () => {
const window = this.getWindow();
if (window && window.isFullScreen()) {
window.setFullScreen(false);
}
});
// Handle toggle force ignore mouse events from renderer
ipcMain.on('toggle-force-ignore-mouse', () => {
this.toggleForceIgnoreMouse();
});
}
createWindow(options: Electron.BrowserWindowConstructorOptions): BrowserWindow {
this.window = new BrowserWindow({
width: 900,
height: 670,
show: false,
transparent: true,
backgroundColor: '#ffffff',
autoHideMenuBar: true,
frame: false,
icon: process.platform === 'win32'
? join(__dirname, '../../resources/icon.ico')
: join(__dirname, '../../resources/icon.png'),
...(isMac ? { titleBarStyle: 'hiddenInset' } : {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false,
contextIsolation: true,
nodeIntegration: true,
},
hasShadow: false,
paintWhenInitiallyHidden: true,
...options,
});
this.setupWindowEvents();
this.loadContent();
this.window.on('enter-full-screen', () => {
this.window?.webContents.send('window-fullscreen-change', true);
});
this.window.on('leave-full-screen', () => {
this.window?.webContents.send('window-fullscreen-change', false);
});
return this.window;
}
private setupWindowEvents(): void {
if (!this.window) return;
this.window.on('ready-to-show', () => {
this.window?.show();
this.window?.webContents.send(
'window-maximized-change',
this.window.isMaximized(),
);
});
this.window.on('maximize', () => {
this.window?.webContents.send('window-maximized-change', true);
});
this.window.on('unmaximize', () => {
this.window?.webContents.send('window-maximized-change', false);
});
this.window.on('resize', () => {
const window = this.getWindow();
if (window) {
const bounds = window.getBounds();
const { width, height } = screen.getPrimaryDisplay().workArea;
const isMaximized = bounds.width >= width && bounds.height >= height;
window.webContents.send('window-maximized-change', isMaximized);
}
});
this.window.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url);
return { action: 'deny' };
});
}
private loadContent(): void {
if (!this.window) return;
if (is.dev && process.env.ELECTRON_RENDERER_URL) {
this.window.loadURL(process.env.ELECTRON_RENDERER_URL);
} else {
this.window.loadFile(join(__dirname, '../renderer/index.html'));
}
}
setWindowMode(mode: 'window' | 'pet'): void {
if (!this.window) return;
this.currentMode = mode;
this.window.setOpacity(0);
if (mode === 'window') {
this.setWindowModeWindow();
} else {
this.setWindowModePet();
}
}
private setWindowModeWindow(): void {
if (!this.window) return;
this.window.setAlwaysOnTop(false);
this.window.setIgnoreMouseEvents(false);
this.window.setSkipTaskbar(false);
this.window.setResizable(true);
this.window.setFocusable(true);
this.window.setAlwaysOnTop(false);
this.window.setBackgroundColor('#ffffff');
this.window.webContents.send('pre-mode-changed', 'window');
}
private continueSetWindowModeWindow(): void {
if (!this.window) return;
if (this.windowedBounds) {
this.window.setBounds(this.windowedBounds);
} else {
this.window.setSize(900, 670);
this.window.center();
}
if (isMac) {
this.window.setWindowButtonVisibility(true);
this.window.setVisibleOnAllWorkspaces(false, {
visibleOnFullScreen: false,
});
}
this.window.setIgnoreMouseEvents(false);
this.window.webContents.send('mode-changed', 'window');
}
private setWindowModePet(): void {
if (!this.window) return;
this.windowedBounds = this.window.getBounds();
if (this.window.isFullScreen()) {
this.window.setFullScreen(false);
}
this.window.setBackgroundColor('#00000000');
this.window.setAlwaysOnTop(true, 'screen-saver');
this.window.setPosition(0, 0);
this.window.webContents.send('pre-mode-changed', 'pet');
}
private continueSetWindowModePet(): void {
if (!this.window) return;
// Span all connected displays so the avatar can be dragged freely between monitors.
// On Wayland the overlay works identically — applyIgnoreMouseEvents strips the
// X11-only { forward: true } flag which was the original crash trigger.
const displays = screen.getAllDisplays();
const minX = Math.min(...displays.map((d) => d.bounds.x));
const minY = Math.min(...displays.map((d) => d.bounds.y));
const maxX = Math.max(...displays.map((d) => d.bounds.x + d.bounds.width));
const maxY = Math.max(...displays.map((d) => d.bounds.y + d.bounds.height));
this.window.setBounds({
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY,
});
if (isMac) this.window.setWindowButtonVisibility(false);
this.window.setResizable(false);
this.window.setSkipTaskbar(true);
this.window.setFocusable(false);
if (isMac) {
this.window.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
}
this.applyIgnoreMouseEvents(true);
this.window.webContents.send('mode-changed', 'pet');
}
getWindow(): BrowserWindow | null {
return this.window;
}
// Applies setIgnoreMouseEvents with platform-correct options.
// { forward: true } is an X11 input-shape feature — on Wayland it causes compositor crashes.
private applyIgnoreMouseEvents(ignore: boolean): void {
if (!this.window) return;
if (isMac || isWayland) {
this.window.setIgnoreMouseEvents(ignore);
} else {
this.window.setIgnoreMouseEvents(ignore, { forward: true });
}
}
setIgnoreMouseEvents(ignore: boolean): void {
this.applyIgnoreMouseEvents(ignore);
}
maximizeWindow(): void {
if (!this.window) return;
if (this.isWindowMaximized()) {
if (this.windowedBounds) {
this.window.setBounds(this.windowedBounds);
this.windowedBounds = null;
this.window.webContents.send('window-maximized-change', false);
}
} else {
this.windowedBounds = this.window.getBounds();
const { width, height } = screen.getPrimaryDisplay().workArea;
this.window.setBounds({
x: 0, y: 0, width, height,
});
this.window.webContents.send('window-maximized-change', true);
}
}
isWindowMaximized(): boolean {
if (!this.window) return false;
const bounds = this.window.getBounds();
const { width, height } = screen.getPrimaryDisplay().workArea;
return bounds.width >= width && bounds.height >= height;
}
updateComponentHover(componentId: string, isHovering: boolean): void {
if (this.currentMode === 'window') return;
// If force ignore is enabled, don't change the mouse ignore state
if (this.forceIgnoreMouse) return;
if (isHovering) {
this.hoveringComponents.add(componentId);
} else {
this.hoveringComponents.delete(componentId);
}
if (this.window) {
const shouldIgnore = this.hoveringComponents.size === 0;
this.applyIgnoreMouseEvents(shouldIgnore);
if (!shouldIgnore) {
this.window.setFocusable(true);
}
}
}
// Toggle force ignore mouse events
toggleForceIgnoreMouse(): void {
this.forceIgnoreMouse = !this.forceIgnoreMouse;
const shouldIgnore = this.forceIgnoreMouse || this.hoveringComponents.size === 0;
this.applyIgnoreMouseEvents(shouldIgnore);
this.window?.webContents.send('force-ignore-mouse-changed', this.forceIgnoreMouse);
}
// Get current force ignore state
isForceIgnoreMouse(): boolean {
return this.forceIgnoreMouse;
}
// Get current mode
getCurrentMode(): 'window' | 'pet' {
return this.currentMode;
}
}