-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
141 lines (122 loc) · 3.4 KB
/
main.js
File metadata and controls
141 lines (122 loc) · 3.4 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
const { app, BrowserWindow, Menu, Tray, screen, globalShortcut, autoUpdater } = require('electron');
const path = require('path');
const fs = require('fs');
const AutoLaunch = require('auto-launch');
const CONFIG_FILE = path.join(app.getPath('userData'), 'widget.config.json');
const TOGGLE_SHORTCUT = 'Ctrl+Shift+W'; // Custom shortcut to toggle the widget
let mainWindow;
let tray = null;
let widgetConfig = {
x: null,
y: null,
width: 800,
height: 600
};
const autoLauncher = new AutoLaunch({
name: 'F1 Widget',
path: app.getPath('exe'),
});
autoLauncher.isEnabled().then((isEnabled) => {
if (!isEnabled) autoLauncher.enable();
});
function createWindow() {
const { width: screenWidth, height: screenHeight } = screen.getPrimaryDisplay().workAreaSize;
loadConfig(); // Load the config before creating the window
// If the configuration file is not present or doesn't have x/y, place the window in the top-right corner
if (widgetConfig.x === null || widgetConfig.y === null) {
widgetConfig.x = screenWidth - widgetConfig.width;
widgetConfig.y = 0; // Top-right corner
}
mainWindow = new BrowserWindow({
width: widgetConfig.width,
height: widgetConfig.height,
x: widgetConfig.x,
y: widgetConfig.y,
frame: false,
transparent: true,
resizable: false,
skipTaskbar: true,
alwaysOnTop: false,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js')
}
});
mainWindow.loadFile(path.join(__dirname, 'index.html'));
tray = new Tray(path.join(__dirname, 'icon.png'));
const trayMenu = Menu.buildFromTemplate([
{
label: 'Show',
click() {
mainWindow.show();
}
},
{
label: 'Hide',
click() {
mainWindow.hide();
}
},
{
label: 'Exit',
click() {
app.quit();
}
}
]);
tray.setContextMenu(trayMenu);
tray.setToolTip('Event Card');
tray.on('click', () => {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
});
mainWindow.on('closed', () => {
mainWindow = null;
});
mainWindow.on('close', () => {
const bounds = mainWindow.getBounds();
widgetConfig.x = bounds.x;
widgetConfig.y = bounds.y;
widgetConfig.width = bounds.width;
widgetConfig.height = bounds.height;
saveConfig();
});
// Register a global shortcut for Ctrl+Shift+W
globalShortcut.register(TOGGLE_SHORTCUT, () => {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
});
}
function loadConfig() {
try {
if (fs.existsSync(CONFIG_FILE)) {
const data = fs.readFileSync(CONFIG_FILE, 'utf8');
widgetConfig = JSON.parse(data);
}
} catch (err) {
console.error('Error loading config:', err);
}
}
function saveConfig() {
try {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(widgetConfig));
} catch (err) {
console.error('Error saving config:', err);
}
}
app.on('ready', () => {
createWindow();
// Show the main window initially
mainWindow.show();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on('will-quit', () => {
globalShortcut.unregisterAll();
});