Update main.js

This commit is contained in:
Anirudh Sevugan 2025-02-21 19:49:45 +05:30 committed by GitHub
parent 0bdc9eb811
commit 01f7f8aca3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,31 +5,57 @@ const os = require('os');
let mainWindow; let mainWindow;
const takeSnapshot = () => { // Handle file opening from Finder or File Explorer
mainWindow.webContents.capturePage().then(image => { app.on('open-file', (event, filePath) => {
event.preventDefault();
openFile(filePath);
});
// Open files using the existing window (or create a new one if needed)
const openFile = (filePath) => {
if (mainWindow) {
mainWindow.webContents.send("play-media", filePath);
} else {
createWindow(() => {
mainWindow.webContents.send("play-media", filePath);
});
}
};
const takeSnapshot = async () => {
if (!mainWindow) return;
try {
const image = await mainWindow.webContents.capturePage();
const png = image.toPNG(); const png = image.toPNG();
const snapshotsDir = path.join(os.homedir(), 'simpliplay-snapshots'); const snapshotsDir = path.join(os.homedir(), 'simpliplay-snapshots');
fs.mkdirSync(snapshotsDir, { recursive: true }); fs.mkdirSync(snapshotsDir, { recursive: true });
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);
// ✅ Show message after saving
async function messageDialog(input) { const { response } = await dialog.showMessageBox({
await dialog.showMessageBox({
type: 'info', type: 'info',
title: 'Snapshot Saved', title: 'Snapshot Saved',
message: `${input}`, message: `Snapshot saved to:\n${filePath}`,
buttons: ['OK'] buttons: ['OK', 'Open File'],
defaultId: 0,
}); });
}
messageDialog(`Snapshot saved to ${filePath}`) if (response === 1) {
}).catch(error => { shell.openPath(filePath);
}
} catch (error) {
console.error("Error capturing snapshot:", 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 = () => { const createWindow = (onReadyCallback) => {
if (mainWindow) {
mainWindow.close();
mainWindow = null;
}
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
width: 1920, width: 1920,
height: 1080, height: 1080,
@ -39,7 +65,15 @@ const createWindow = () => {
}, },
}); });
mainWindow.loadFile('index.html'); mainWindow.loadFile("index.html");
mainWindow.on("closed", () => {
mainWindow = null;
});
mainWindow.webContents.once("did-finish-load", () => {
if (onReadyCallback) onReadyCallback();
});
// Register global shortcuts // Register global shortcuts
globalShortcut.register('CommandOrControl+Shift+S', takeSnapshot); globalShortcut.register('CommandOrControl+Shift+S', takeSnapshot);
@ -50,58 +84,37 @@ const createWindow = () => {
// Modify the default menu // Modify the default menu
const menu = Menu.getApplicationMenu(); const menu = Menu.getApplicationMenu();
if (menu) { if (menu) {
// Add "Take a Snapshot" to File menu
const fileMenu = menu.items.find(item => item.label === 'File'); const fileMenu = menu.items.find(item => item.label === 'File');
if (fileMenu) { if (fileMenu && !fileMenu.submenu.items.some(item => item.label === 'Take a Snapshot')) {
const snapshotItemExists = fileMenu.submenu.items.some(item => item.label === 'Take a Snapshot');
if (!snapshotItemExists) {
fileMenu.submenu.append(new MenuItem({ fileMenu.submenu.append(new MenuItem({
label: 'Take a Snapshot', label: 'Take a Snapshot',
accelerator: 'CommandOrControl+Shift+S', accelerator: 'CommandOrControl+Shift+S',
click: takeSnapshot click: takeSnapshot
})); }));
} }
}
// Add new items to Help menu without removing existing ones
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 existingLabels = helpMenu.submenu.items.map(item => item.label);
if (!existingLabels.includes('Source Code')) { const addMenuItem = (label, url) => {
if (!existingLabels.includes(label)) {
helpMenu.submenu.append(new MenuItem({ helpMenu.submenu.append(new MenuItem({
label: 'Source Code', label,
click: () => { click: () => shell.openExternal(url)
shell.openExternal('https://github.com/A-Star100/simpliplay-desktop');
}
})); }));
} }
};
if (!existingLabels.includes('Website')) { addMenuItem('Source Code', 'https://github.com/A-Star100/simpliplay-desktop');
helpMenu.submenu.append(new MenuItem({ addMenuItem('Website', 'https://simpliplay.netlify.app');
label: 'Website', addMenuItem('Help Center', 'https://simpliplay.netlify.app/help');
click: () => {
shell.openExternal('https://simpliplay.netlify.app');
}
}));
}
if (!existingLabels.includes('Help Center')) {
helpMenu.submenu.append(new MenuItem({
label: 'Help Center',
click: () => {
shell.openExternal('https://simpliplay.netlify.app/help');
}
}));
}
if (!existingLabels.includes('Quit')) { if (!existingLabels.includes('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', label: 'Quit',
click: () => { click: () => app.quit(),
app.quit();
}
})); }));
} }
} }
@ -109,15 +122,18 @@ const createWindow = () => {
// Create context menu // Create context menu
const contextMenu = new 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
}));
}
const inspectItemExists = contextMenu.items.some(item => item.label === 'Inspect'); if (!contextMenu.items.some(item => item.label === 'Inspect')) {
if (!inspectItemExists) {
contextMenu.append(new MenuItem({ type: 'separator' })); contextMenu.append(new MenuItem({ type: 'separator' }));
contextMenu.append(new MenuItem({ contextMenu.append(new MenuItem({
label: 'Inspect', label: 'Inspect',
click: () => { click: () => mainWindow.webContents.openDevTools()
mainWindow.webContents.openDevTools();
}
})); }));
} }
@ -126,18 +142,22 @@ const createWindow = () => {
contextMenu.popup({ window: mainWindow }); contextMenu.popup({ window: mainWindow });
}); });
// Set the updated application menu
Menu.setApplicationMenu(menu); Menu.setApplicationMenu(menu);
}; };
// Handle window events and app lifecycle
app.whenReady().then(() => { app.whenReady().then(() => {
createWindow(); createWindow();
app.on('activate', () => {
app.on("open-file", (event, filePath) => {
event.preventDefault();
openFile(filePath);
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow(); if (BrowserWindow.getAllWindows().length === 0) createWindow();
}); });
}); });
app.on('window-all-closed', () => { app.on("window-all-closed", () => {
if (process.platform !== 'darwin') app.quit(); if (process.platform !== "darwin") app.quit();
}); });