-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
114 lines (96 loc) · 4.08 KB
/
main.js
File metadata and controls
114 lines (96 loc) · 4.08 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
const fs = require('fs');
const path = require("path");
const { app, BrowserWindow, globalShortcut, ipcMain } = require("electron");
const defaultShortcut = process.platform === 'darwin' ? 'Cmd+Option+Shift+T' : 'Ctrl+Alt+Shift+T';
const defaultSettings = { width: 500, height: 600, shortcut: defaultShortcut };
const settingsPath = path.join(app.getPath('userData'), 'settings.json');
// You can specify your own shortcut in settings.json by adding a "shortcut" field, e.g.:
// { "width": 500, "height": 600, "shortcut": "Ctrl+Alt+Shift+T" }
function loadSettings() {
try {
const data = fs.readFileSync(settingsPath, 'utf-8');
return JSON.parse(data);
} catch (err) {
return defaultSettings;
}
}
function saveSettings(settings) {
try {
fs.writeFileSync(settingsPath, JSON.stringify(settings));
} catch (err) {
console.error("Ошибка при сохранении настроек:", err);
}
}
const is_mac = process.platform === 'darwin'
if (is_mac) {
app.dock.hide();
}
function createClapWindow() {
const { width, height } = loadSettings();
// Create the browser window.
const mainWindow = new BrowserWindow({
width,
height,
icon: path.join(__dirname, 'assets/icon.icns'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
webviewTag: true // ✅ ОБЯЗАТЕЛЬНО!
}
})
mainWindow.on('resize', () => {
const [width, height] = mainWindow.getSize();
const currentSettings = loadSettings();
saveSettings({ ...currentSettings, width, height });
});
mainWindow.setAlwaysOnTop(true, "screen-saver");
mainWindow.setVisibleOnAllWorkspaces(true);
mainWindow.loadFile('public/reaction.html');
/* Включаем DevTools */
// mainWindow.webContents.openDevTools();
return mainWindow;
}
app.whenReady().then(() => {
const mainWindow = createClapWindow()
const { shortcut } = loadSettings();
globalShortcut.register(shortcut, () => {
if (!mainWindow) return;
if (mainWindow.isVisible()) {
mainWindow.hide();
} else {
/**
* Повторная инициализация настроек.
* На MacOS, по какой-то неведомой причине, спустя некоторое время работы приложения,
* оно перестает открываться поверх других приложений, а появляется и переключает пользователя на основной
* рабочий стол. Если открыть программу на весть экран и затем уменьшить - то все начинает снова работать.
* Ощущение такое, что происходит какая-то гонка за слоями отображения (я тут полный профан) и спустя время
* другие программы забирают право висеть на верхнем слое.
* Не уверен что это поможет, но все же.
*/
// if (is_mac) {
// app.dock.hide();
// }
mainWindow.setAlwaysOnTop(true, "screen-saver");
mainWindow.setVisibleOnAllWorkspaces(true);
mainWindow.show();
mainWindow.focus();
}
});
})
app.on('will-quit', () => {
globalShortcut.unregisterAll();
});
ipcMain.handle('get-user-settings', () => {
return loadSettings();
});
// Добавляем обработчик для обновления горячей клавиши
ipcMain.on('update-shortcut', (event, newShortcut) => {
const currentSettings = loadSettings();
const updatedSettings = { ...currentSettings, shortcut: newShortcut };
saveSettings(updatedSettings);
});
ipcMain.on('restart-app', () => {
app.relaunch();
app.exit();
});