From 504aca747dc09988c9effa13b97e3ceb7fca403b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 11 May 2016 19:00:06 +0200 Subject: [PATCH] main/menu.js: minor refactor Just some code cleanup to make menu.js more internally consistent. - Name the electron.dialog returned value `selectedPaths` which is more accurate. - Move the file menu into the `template` object, like the rest of the menus. Then reach in afterwards for OS-specific tweaks. --- config.js | 3 ++ main/menu.js | 124 ++++++++++++++++++++++++++------------------------- 2 files changed, 66 insertions(+), 61 deletions(-) diff --git a/config.js b/config.js index 55d501a3..ea891229 100644 --- a/config.js +++ b/config.js @@ -28,8 +28,11 @@ module.exports = { CONFIG_TORRENT_PATH: path.join(getConfigPath(), 'Torrents'), GITHUB_URL: 'https://github.com/feross/webtorrent-desktop', + GITHUB_URL_ISSUES: 'https://github.com/feross/webtorrent-desktop/issues', GITHUB_URL_RAW: 'https://raw.githubusercontent.com/feross/webtorrent-desktop/master', + HOME_PAGE_URL: 'https://webtorrent.io', + IS_PORTABLE: isPortable(), IS_PRODUCTION: isProduction(), diff --git a/main/menu.js b/main/menu.js index 644ae4e5..8239de6c 100644 --- a/main/menu.js +++ b/main/menu.js @@ -109,29 +109,29 @@ function getMenuItem (label) { } } -// Prompts the user for a file, then makes a torrent out of the data +// Prompts the user for a file, then creates a torrent. Only allows a single file +// selection. function showOpenSeedFile () { electron.dialog.showOpenDialog({ title: 'Select a file for the torrent file.', properties: [ 'openFile' ] - }, function (filenames) { - if (!Array.isArray(filenames)) return - var file = filenames[0] - windows.main.send('dispatch', 'showCreateTorrent', file) + }, function (selectedPaths) { + if (!Array.isArray(selectedPaths)) return + var selectedPath = selectedPaths[0] + windows.main.send('dispatch', 'showCreateTorrent', selectedPath) }) } -// Prompts the user for a file or folder, then makes a torrent out of the data +// Prompts the user for a file or directory, then creates a torrent. Only allows a single +// selection. To create a multi-file torrent, the user must select a directory. function showOpenSeedFiles () { - // Allow only a single selection - // To create a multi-file torrent, the user must select a folder electron.dialog.showOpenDialog({ title: 'Select a file or folder for the torrent file.', properties: [ 'openFile', 'openDirectory' ] - }, function (filenames) { - if (!Array.isArray(filenames)) return - var fileOrFolder = filenames[0] - windows.main.send('dispatch', 'showCreateTorrent', fileOrFolder) + }, function (selectedPaths) { + if (!Array.isArray(selectedPaths)) return + var selectedPath = selectedPaths[0] + windows.main.send('dispatch', 'showCreateTorrent', selectedPath) }) } @@ -141,10 +141,10 @@ function showOpenTorrentFile () { title: 'Select a .torrent file to open.', filters: [{ name: 'Torrent Files', extensions: ['torrent'] }], properties: [ 'openFile', 'multiSelections' ] - }, function (filenames) { - if (!Array.isArray(filenames)) return - filenames.forEach(function (filename) { - windows.main.send('dispatch', 'addTorrent', filename) + }, function (selectedPaths) { + if (!Array.isArray(selectedPaths)) return + selectedPaths.forEach(function (selectedPath) { + windows.main.send('dispatch', 'addTorrent', selectedPath) }) }) } @@ -155,51 +155,36 @@ function showOpenTorrentAddress () { } function getAppMenuTemplate () { - var fileMenu = [ - { - label: process.platform === 'darwin' ? 'Create New Torrent...' : 'Create New Torrent from Folder...', - accelerator: 'CmdOrCtrl+N', - click: showOpenSeedFiles - }, - { - label: 'Open Torrent File...', - accelerator: 'CmdOrCtrl+O', - click: showOpenTorrentFile - }, - { - label: 'Open Torrent Address...', - accelerator: 'CmdOrCtrl+U', - click: showOpenTorrentAddress - }, - { - type: 'separator' - }, - { - label: process.platform === 'windows' ? 'Close' : 'Close Window', - accelerator: 'CmdOrCtrl+W', - role: 'close' - } - ] - - // In Linux and Windows it is not possible to open both folders and files - if (process.platform !== 'darwin') { - fileMenu.unshift({ - label: 'Create New Torrent from File...', - click: showOpenSeedFile - }) - } - // File > Quit for Linux users with distros where the system tray is broken - if (process.platform === 'linux') { - fileMenu.push({ - label: 'Quit', - click: () => app.quit() - }) - } - var template = [ { label: 'File', - submenu: fileMenu + submenu: [ + { + label: process.platform === 'darwin' + ? 'Create New Torrent...' + : 'Create New Torrent from Folder...', + accelerator: 'CmdOrCtrl+N', + click: showOpenSeedFiles + }, + { + label: 'Open Torrent File...', + accelerator: 'CmdOrCtrl+O', + click: showOpenTorrentFile + }, + { + label: 'Open Torrent Address...', + accelerator: 'CmdOrCtrl+U', + click: showOpenTorrentAddress + }, + { + type: 'separator' + }, + { + label: process.platform === 'windows' ? 'Close' : 'Close Window', + accelerator: 'CmdOrCtrl+W', + role: 'close' + } + ] }, { label: 'Edit', @@ -298,7 +283,7 @@ function getAppMenuTemplate () { submenu: [ { label: 'Learn more about ' + config.APP_NAME, - click: () => electron.shell.openExternal('https://webtorrent.io') + click: () => electron.shell.openExternal(config.HOME_PAGE_URL) }, { label: 'Contribute on GitHub', @@ -309,7 +294,7 @@ function getAppMenuTemplate () { }, { label: 'Report an Issue...', - click: () => electron.shell.openExternal(config.GITHUB_URL + '/issues') + click: () => electron.shell.openExternal(config.GITHUB_URL_ISSUES) } ] } @@ -370,7 +355,16 @@ function getAppMenuTemplate () { role: 'front' } ) - } else { + } + + // In Linux and Windows it is not possible to open both folders and files + if (process.platform === 'linux' || process.platform === 'windows') { + // File menu (Windows, Linux) + template[0].unshift({ + label: 'Create New Torrent from File...', + click: showOpenSeedFile + }) + // Help menu (Windows, Linux) template[4].submenu.push( { @@ -382,6 +376,14 @@ function getAppMenuTemplate () { } ) } + // Add "File > Quit" menu item for Linux distros where the system tray is broken + if (process.platform === 'linux') { + // File menu (Linux) + template[0].push({ + label: 'Quit', + click: () => app.quit() + }) + } return template }