-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
127 lines (109 loc) · 2.87 KB
/
main.js
File metadata and controls
127 lines (109 loc) · 2.87 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
import { app, BrowserWindow, globalShortcut, ipcMain, screen } from 'electron';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// eslint-disable-next-line no-undef
const isDev = process.env.NODE_ENV === 'development';
let mainWindow = null;
let isWindowVisible = false;
function createWindow() {
mainWindow = new BrowserWindow({
width: 600,
height: 450,
frame: false,
resizable: false,
alwaysOnTop: true,
skipTaskbar: true,
show: false,
center: true,
transparent: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
webSecurity: true,
preload: path.join(__dirname, 'preload.js')
}
});
// Load the app
if (isDev) {
mainWindow.loadURL('http://localhost:5173');
} else {
mainWindow.loadFile(path.join(__dirname, 'dist/index.html'));
}
// Hide window when it loses focus
mainWindow.on('blur', () => {
if (isWindowVisible) {
hideWindow();
}
});
mainWindow.on('closed', () => {
mainWindow = null;
});
}
function showWindow() {
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
isWindowVisible = true;
}
}
function hideWindow() {
if (mainWindow) {
mainWindow.hide();
isWindowVisible = false;
}
}
function toggleWindow() {
if (isWindowVisible) {
hideWindow();
} else {
showWindow();
}
}
app.whenReady().then(() => {
createWindow();
// IPC handler for hiding window
ipcMain.handle('hide-window', () => {
hideWindow();
});
// IPC handler for resizing window
ipcMain.handle('resize-window', (event, width, height) => {
if (mainWindow) {
mainWindow.setSize(width, height);
mainWindow.center();
const primaryDisplay = screen.getPrimaryDisplay();
const { width: screenWidth, height: screenHeight } = primaryDisplay.workAreaSize;
const x = Math.round((screenWidth - width) / 2);
const y = Math.round((screenHeight - height) / 2);
mainWindow.setBounds({ x, y, width, height });
}
});
// Register global shortcuts
globalShortcut.register('CommandOrControl+Shift+Space', toggleWindow) ||
// Escape key handled by the React app, not globally
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
// eslint-disable-next-line no-undef
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('will-quit', () => {
// Unregister all shortcuts
globalShortcut.unregisterAll();
});
// Prevent the app from quitting when all windows are closed on macOS
app.on('before-quit', (event) => {
// eslint-disable-next-line no-undef
if (process.platform === 'darwin') {
event.preventDefault();
hideWindow();
}
});