Skip to content

Commit

Permalink
Simplify code and add open link dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmiau committed Jan 13, 2024
1 parent c571923 commit 7b493ce
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
const { app, BrowserWindow, Menu, shell } = require('electron');
const { app, BrowserWindow, clipboard, dialog, Menu, shell } = require('electron');
const { autoUpdater } = require('electron-updater');
const windowStateKeeper = require('electron-window-state');
const path = require('path');

// Allow only one instance of the application
const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
app.quit();
} else {
// Create the application menu
const createMenu = () => {
const menu = Menu.getApplicationMenu();
const filteredItems = menu.items.find(item => item.role === 'viewmenu').submenu.items;

Menu.setApplicationMenu(Menu.buildFromTemplate(filteredItems));
};

// Create the main application window
const createWindow = () => {
const mainWindowState = windowStateKeeper({
defaultWidth: 1040,
Expand All @@ -37,29 +33,43 @@ if (!gotTheLock) {
},
});

// Show the window once it's ready to avoid a visual flash
mainWindow.once('ready-to-show', () => {
mainWindow.show();
});

// Open new windows with the default browser
mainWindow.webContents.on('new-window', (event, url) => {
event.preventDefault();
shell.openExternal(url);
openNewWindow(url);
});

// Show the context menu
mainWindow.webContents.on('context-menu', (event, params) => {
Menu.getApplicationMenu().popup(mainWindow, params.x, params.y);
});

// Load the initial URL
mainWindow.loadURL('https://www.panfu.us/play');

// Manage the window's state
mainWindowState.manage(mainWindow);
};

const openNewWindow = (url) => {
const options = {
type: 'question',
buttons: ['Open in Default Browser', 'Copy to Clipboard', 'Cancel'],
defaultId: 0,
title: 'Open Link',
message: 'Choose an action for the link:',
detail: url,
};

dialog.showMessageBox(null, options).then(({ response }) => {
if (response === 0) {
shell.openExternal(url);
} else if (response === 1) {
clipboard.writeText(url);
}
});
};

const initializeFlashPlugin = () => {
const resourcesPath = app.isPackaged ? process.resourcesPath : __dirname;
let pluginName;
Expand All @@ -75,12 +85,10 @@ if (!gotTheLock) {
pluginName = 'libpepflashplayer.so';
}

// Disable Chromium's sandbox on Linux
if (['freebsd', 'linux', 'netbsd', 'openbsd'].includes(process.platform)) {
app.commandLine.appendSwitch('no-sandbox');
}

// Set Flash plugin path and version
app.commandLine.appendSwitch('ppapi-flash-path', path.join(resourcesPath, 'plugins', pluginName));
app.commandLine.appendSwitch('ppapi-flash-version', '32.0.0.465');
};
Expand All @@ -104,7 +112,6 @@ if (!gotTheLock) {
createWindow();
autoUpdater.checkForUpdates();

// On macOS, open a new window if none exist when the app is activated
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
Expand Down

0 comments on commit 7b493ce

Please sign in to comment.