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