mirror of
https://github.com/A-Star100/simpliplay-desktop.git
synced 2025-09-18 06:39:44 +00:00
Update main.js
This commit is contained in:
parent
0bdc9eb811
commit
01f7f8aca3
@ -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) {
|
|
||||||
await dialog.showMessageBox({
|
|
||||||
type: 'info',
|
|
||||||
title: 'Snapshot Saved',
|
|
||||||
message: `${input}`,
|
|
||||||
buttons: ['OK']
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
messageDialog(`Snapshot saved to ${filePath}`)
|
const { response } = await dialog.showMessageBox({
|
||||||
}).catch(error => {
|
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);
|
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');
|
fileMenu.submenu.append(new MenuItem({
|
||||||
if (!snapshotItemExists) {
|
label: 'Take a Snapshot',
|
||||||
fileMenu.submenu.append(new MenuItem({
|
accelerator: 'CommandOrControl+Shift+S',
|
||||||
label: 'Take a Snapshot',
|
click: takeSnapshot
|
||||||
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');
|
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')) {
|
|
||||||
helpMenu.submenu.append(new MenuItem({
|
|
||||||
label: 'Source Code',
|
|
||||||
click: () => {
|
|
||||||
shell.openExternal('https://github.com/A-Star100/simpliplay-desktop');
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!existingLabels.includes('Website')) {
|
const addMenuItem = (label, url) => {
|
||||||
helpMenu.submenu.append(new MenuItem({
|
if (!existingLabels.includes(label)) {
|
||||||
label: 'Website',
|
helpMenu.submenu.append(new MenuItem({
|
||||||
click: () => {
|
label,
|
||||||
shell.openExternal('https://simpliplay.netlify.app');
|
click: () => shell.openExternal(url)
|
||||||
}
|
}));
|
||||||
}));
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
if (!existingLabels.includes('Help Center')) {
|
addMenuItem('Source Code', 'https://github.com/A-Star100/simpliplay-desktop');
|
||||||
helpMenu.submenu.append(new MenuItem({
|
addMenuItem('Website', 'https://simpliplay.netlify.app');
|
||||||
label: 'Help Center',
|
addMenuItem('Help Center', 'https://simpliplay.netlify.app/help');
|
||||||
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();
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user