This repository was archived by the owner on Jan 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
278 lines (245 loc) · 7.42 KB
/
main.js
File metadata and controls
278 lines (245 loc) · 7.42 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
const {
app,
BrowserWindow,
ipcMain,
globalShortcut,
screen,
shell, // shell is already here, which is great
} = require("electron");
const path = require("path");
const fs = require("fs/promises");
const fsSync = require("fs"); // Use sync version for setup
const { exec } = require("child_process");
const os = require("os");
let mainWindow = null;
let settingsWindow = null;
// --- App Setup: Create Thumbnail Cache Directory ---
const cacheDir = path.join(app.getPath("userData"), "thumbnails");
if (!fsSync.existsSync(cacheDir)) {
fsSync.mkdirSync(cacheDir, { recursive: true });
}
// --- Settings File Setup ---
const settingsPath = path.join(app.getPath("userData"), "settings.json");
async function getSettings() {
try {
await fs.access(settingsPath);
const settingsFile = await fs.readFile(settingsPath, "utf-8");
return JSON.parse(settingsFile);
} catch (error) {
// If the file doesn't exist or there's an error, return default settings
const defaultSettings = {
theme: "tokyo-night-blue",
openCommand: "CommandOrControl+Shift+P",
};
await saveSettings(defaultSettings);
return defaultSettings;
}
}
async function saveSettings(settings) {
await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2));
}
const run = (cmd) =>
new Promise((resolve, reject) => {
exec(cmd, { maxBuffer: 1024 * 1024 * 10 }, (error, stdout, stderr) => {
if (error) return reject(error);
if (stderr) return reject(new Error(stderr));
resolve(stdout.trim());
});
});
async function setWallpaper(imagePath) {
const script = `osascript -e 'tell application "System Events" to tell every desktop to set picture to (POSIX file "${imagePath}")'`;
return run(script);
}
function createWindow() {
const currentDisplay = screen.getDisplayNearestPoint(
screen.getCursorScreenPoint(),
);
const { width, height } = currentDisplay.workAreaSize;
const windowWidth = 1920;
const windowHeight = 1080;
mainWindow = new BrowserWindow({
width,
height,
x: Math.round(currentDisplay.bounds.x + (width - windowWidth) / 2),
y: Math.round(currentDisplay.bounds.y + (height - windowHeight) / 2),
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
nodeIntegration: false,
},
frame: false,
transparent: true,
show: false,
vibrancy: "light",
alwaysOnTop: true,
level: "floating",
visibleOnAllWorkspaces: true,
resizable: false,
focusable: true,
});
console.log("Main window created.");
if (process.platform === "darwin") {
app.dock.hide();
}
mainWindow.loadFile(path.join(__dirname, "dist/index.html"));
mainWindow.once("ready-to-show", () => {
mainWindow.show();
});
mainWindow.on("closed", () => {
mainWindow = null;
});
}
ipcMain.on("toggle-settings", () => {
if (mainWindow) {
mainWindow.webContents.send("toggle-settings");
}
});
app.whenReady().then(async () => {
console.log("App is ready.");
const settings = await getSettings();
console.log("Settings loaded:", settings);
globalShortcut.register(settings.openCommand, () => {
console.log("Global shortcut triggered.");
if (mainWindow) {
mainWindow.close();
} else {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("will-quit", () => {
globalShortcut.unregisterAll();
});
// --- IPC Handlers ---
ipcMain.handle("get-settings", async () => {
return await getSettings();
});
ipcMain.handle("save-settings", async (event, settings) => {
await saveSettings(settings);
// You might want to re-register the shortcut if it changed
globalShortcut.unregisterAll();
const newSettings = await getSettings();
globalShortcut.register(newSettings.openCommand, () => {
if (mainWindow) {
mainWindow.close();
} else {
createWindow();
}
});
if (mainWindow) {
mainWindow.webContents.send("theme-updated", newSettings.theme);
}
});
ipcMain.handle("get-themes", async () => {
const themeDir = path.join(__dirname, "dist/themes");
try {
const files = await fs.readdir(themeDir);
const themes = await Promise.all(
files.map(async (file) => {
if (file.endsWith(".json")) {
const filePath = path.join(themeDir, file);
const content = await fs.readFile(filePath, "utf-8");
return JSON.parse(content);
}
return null;
}),
);
return themes.filter((theme) => theme !== null);
} catch (error) {
console.error(`Error reading theme directory: ${themeDir}`, error);
return [];
}
});
// **FIXED**: Handler to open external links securely in the user's default browser.
ipcMain.handle("open-external-link", async (event, url) => {
await shell.openExternal(url);
});
ipcMain.handle("get-wallpapers", async () => {
const wallpaperDir = path.join(os.homedir(), "wallpapers");
try {
if (!fsSync.existsSync(wallpaperDir)) {
fsSync.mkdirSync(wallpaperDir, { recursive: true });
}
const files = await fs.readdir(wallpaperDir);
return files.filter((file) => /\.(jpg|jpeg|png|heic|webp)$/i.test(file));
} catch (error) {
console.error(`Error reading wallpaper directory: ${wallpaperDir}`, error);
throw new Error(`Could not read wallpapers from: ~/wallpapers.`);
}
});
ipcMain.handle("open-wallpapers-folder", async () => {
const wallpaperDir = path.join(os.homedir(), "wallpapers");
try {
if (!fsSync.existsSync(wallpaperDir)) {
fsSync.mkdirSync(wallpaperDir, { recursive: true });
}
await shell.openPath(wallpaperDir);
} catch (error) {
console.error(`Failed to open wallpaper directory: ${wallpaperDir}`, error);
throw error;
}
});
ipcMain.handle("get-app-version", () => {
return app.getVersion();
});
ipcMain.handle("set-wallpaper", async (event, imageName) => {
const imagePath = path.join(os.homedir(), "wallpapers", imageName);
try {
await setWallpaper(imagePath);
return imagePath;
} catch (error) {
console.error(`Failed to set wallpaper: ${imageName}`, error);
throw error;
}
});
ipcMain.handle("get-current-wallpaper", async () => {
try {
return await run(
`osascript -e 'tell application "System Events" to tell every desktop to get picture'`,
);
} catch (error) {
console.error("Failed to get current wallpaper", error);
throw error;
}
});
ipcMain.handle("get-image-as-base64", async (event, fullPath) => {
try {
const data = await fs.readFile(fullPath);
return data.toString("base64");
} catch (error) {
console.error(`Failed to read file: ${fullPath}`, error);
throw error;
}
});
ipcMain.handle("get-thumbnail", async (event, imageName) => {
const sourcePath = path.join(os.homedir(), "wallpapers", imageName);
const thumbPath = path.join(cacheDir, imageName);
try {
await fs.access(thumbPath);
const data = await fs.readFile(thumbPath);
return data.toString("base64");
} catch {
try {
const sipsCommand = `sips -Z 400 "${sourcePath}" --out "${thumbPath}"`;
await run(sipsCommand);
const data = await fs.readFile(thumbPath);
return data.toString("base64");
} catch (generationError) {
console.error(
`Failed to generate thumbnail for ${imageName}:`,
generationError,
);
throw generationError;
}
}
});
ipcMain.on("hide-window", () => {
if (mainWindow) {
mainWindow.close();
}
});