Update main.js

This commit is contained in:
Anirudh Sevugan 2025-02-21 22:22:58 +05:30 committed by GitHub
parent 17b2c792ed
commit 6f959e8d69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,15 +11,22 @@ app.on('open-file', (event, filePath) => {
openFile(filePath); openFile(filePath);
}); });
// Open files using the existing window (or create a new one if needed)
const openFile = (filePath) => { const openFile = (filePath) => {
app.whenReady().then(() => {
if (mainWindow) { if (mainWindow) {
if (mainWindow.webContents.isLoading()) {
mainWindow.webContents.once("did-finish-load", () => {
mainWindow.webContents.send("play-media", filePath); mainWindow.webContents.send("play-media", filePath);
});
} else {
mainWindow.webContents.send("play-media", filePath);
}
} else { } else {
createWindow(() => { createWindow(() => {
mainWindow.webContents.send("play-media", filePath); mainWindow.webContents.send("play-media", filePath);
}); });
} }
});
}; };
const takeSnapshot = async () => { const takeSnapshot = async () => {
@ -33,7 +40,7 @@ const takeSnapshot = async () => {
const filePath = path.join(snapshotsDir, `snapshot-${Date.now()}.png`); const filePath = path.join(snapshotsDir, `snapshot-${Date.now()}.png`);
fs.writeFileSync(filePath, png); fs.writeFileSync(filePath, png);
const { response } = await dialog.showMessageBox({ const { response } = await dialog.showMessageBox(mainWindow, {
type: 'info', type: 'info',
title: 'Snapshot Saved', title: 'Snapshot Saved',
message: `Snapshot saved to:\n${filePath}`, message: `Snapshot saved to:\n${filePath}`,
@ -41,68 +48,68 @@ const takeSnapshot = async () => {
defaultId: 0, defaultId: 0,
}); });
if (response === 1) { if (response === 1) shell.openPath(filePath);
shell.openPath(filePath);
}
} catch (error) { } catch (error) {
console.error("Error capturing snapshot:", error);
dialog.showErrorBox("Snapshot Error", `Failed to capture snapshot: ${error.message}`); dialog.showErrorBox("Snapshot Error", `Failed to capture snapshot: ${error.message}`);
} }
}; };
const createWindow = (onReadyCallback) => { const createWindow = (onReadyCallback) => {
if (mainWindow) { if (!app.isReady()) {
mainWindow.close(); app.whenReady().then(() => createWindow(onReadyCallback));
mainWindow = null; return;
} }
if (mainWindow) mainWindow.close();
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
width: 1920, width: 1920,
height: 1080, height: 1080,
webPreferences: { webPreferences: {
preload: path.join(__dirname, 'preload.js'), preload: path.join(__dirname, "preload.js"),
contextIsolation: true, contextIsolation: true,
}, },
}); });
mainWindow.loadFile("index.html"); mainWindow.loadFile("index.html");
mainWindow.on("closed", () => { mainWindow.once("ready-to-show", () => {
mainWindow = null;
});
mainWindow.webContents.once("did-finish-load", () => {
if (onReadyCallback) onReadyCallback(); if (onReadyCallback) onReadyCallback();
}); });
// Register global shortcuts setupContextMenu();
globalShortcut.register('CommandOrControl+Shift+S', takeSnapshot); };
globalShortcut.register('CommandOrControl+Shift+I', () => {
mainWindow.webContents.openDevTools();
});
// Modify the default menu // Set up context menu (prevents errors if `mainWindow` is undefined)
const setupContextMenu = () => {
if (!mainWindow) return;
const contextMenu = new Menu();
contextMenu.append(new MenuItem({ label: 'Take a Snapshot', click: takeSnapshot }));
contextMenu.append(new MenuItem({ type: 'separator' }));
contextMenu.append(new MenuItem({ label: 'Inspect', click: () => mainWindow.webContents.openDevTools() }));
mainWindow.webContents.on('context-menu', (event) => {
event.preventDefault();
contextMenu.popup({ window: mainWindow });
});
};
// Set up application menu
const setupMenu = () => {
const menu = Menu.getApplicationMenu(); const menu = Menu.getApplicationMenu();
if (menu) { if (!menu) return;
const fileMenu = menu.items.find(item => item.label === 'File'); const fileMenu = menu.items.find(item => item.label === 'File');
if (fileMenu && !fileMenu.submenu.items.some(item => item.label === 'Take a Snapshot')) { if (fileMenu && !fileMenu.submenu.items.some(item => item.label === 'Take a Snapshot')) {
fileMenu.submenu.append(new MenuItem({ fileMenu.submenu.append(new MenuItem({ label: 'Take a Snapshot', accelerator: 'CommandOrControl+Shift+S', click: takeSnapshot }));
label: 'Take a Snapshot',
accelerator: 'CommandOrControl+Shift+S',
click: takeSnapshot
}));
} }
const helpMenu = menu.items.find(item => item.label === 'Help'); const helpMenu = menu.items.find(item => item.label === 'Help');
if (helpMenu) { if (helpMenu) {
const existingLabels = helpMenu.submenu.items.map(item => item.label);
const addMenuItem = (label, url) => { const addMenuItem = (label, url) => {
if (!existingLabels.includes(label)) { if (!helpMenu.submenu.items.some(item => item.label === label)) {
helpMenu.submenu.append(new MenuItem({ helpMenu.submenu.append(new MenuItem({ label, click: () => shell.openExternal(url) }));
label,
click: () => shell.openExternal(url)
}));
} }
}; };
@ -110,43 +117,39 @@ const createWindow = (onReadyCallback) => {
addMenuItem('Website', 'https://simpliplay.netlify.app'); addMenuItem('Website', 'https://simpliplay.netlify.app');
addMenuItem('Help Center', 'https://simpliplay.netlify.app/help'); addMenuItem('Help Center', 'https://simpliplay.netlify.app/help');
if (!existingLabels.includes('Quit')) { if (!helpMenu.submenu.items.some(item => item.label === 'Quit')) {
helpMenu.submenu.append(new MenuItem({ type: 'separator' })); helpMenu.submenu.append(new MenuItem({ type: 'separator' }));
helpMenu.submenu.append(new MenuItem({ helpMenu.submenu.append(new MenuItem({ label: 'Quit', click: () => app.quit() }));
label: 'Quit',
click: () => app.quit(),
}));
} }
} }
}
// Create context menu
const contextMenu = new Menu();
if (!contextMenu.items.some(item => item.label === 'Take a Snapshot')) {
contextMenu.append(new MenuItem({
label: 'Take a Snapshot',
click: takeSnapshot
}));
}
if (!contextMenu.items.some(item => item.label === 'Inspect')) {
contextMenu.append(new MenuItem({ type: 'separator' }));
contextMenu.append(new MenuItem({
label: 'Inspect',
click: () => mainWindow.webContents.openDevTools()
}));
}
mainWindow.webContents.on('context-menu', (event, params) => {
event.preventDefault();
contextMenu.popup({ window: mainWindow });
});
Menu.setApplicationMenu(menu); Menu.setApplicationMenu(menu);
}; };
// Quit Confirmation on CommandOrControl+Q
const setupShortcuts = () => {
globalShortcut.register('CommandOrControl+Q', () => {
if (mainWindow) {
dialog.showMessageBox(mainWindow, {
type: 'question',
buttons: ['Cancel', 'Quit'],
defaultId: 1,
title: 'Quit?',
message: 'Are you sure you want to quit SimpliPlay?',
}).then(({ response }) => {
if (response === 1) app.quit();
});
}
});
globalShortcut.register('CommandOrControl+Shift+S', takeSnapshot);
};
// App lifecycle management
app.whenReady().then(() => { app.whenReady().then(() => {
createWindow(); createWindow();
setupMenu();
setupShortcuts();
app.on("open-file", (event, filePath) => { app.on("open-file", (event, filePath) => {
event.preventDefault(); event.preventDefault();