| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- /**
- * @author oldj
- * @blog https://oldj.net
- */
- 'use strict'
- const path = require('path')
- const paths = require('../server/paths')
- const {Menu, shell, dialog} = require('electron')
- const m_lang = require('../server/lang')
- //const getPref = require('../server/actions/getPref')
- const checkUpdate = require('../server/checkUpdate')
- const svr = require('../server/svr')
- //const version = require('../version')
- function doInit (app, lang) {
- let last_path = null
- let download_path = app.getPath('downloads')
- const template = [
- {
- label: lang.file,
- submenu: [
- {
- label: lang.new,
- accelerator: 'CommandOrControl+N',
- click: () => {
- svr.broadcast('add_hosts')
- }
- }, {
- type: 'separator'
- }, {
- label: lang.import,
- accelerator: 'Alt+CommandOrControl+I',
- click: () => {
- dialog.showOpenDialog({
- title: lang.import,
- defaultPath: path.join(last_path || download_path ||
- paths.home_path, 'sh.json'),
- filters: [
- {name: 'JSON', extensions: ['json']},
- {name: 'All Files', extensions: ['*']}
- ]
- }, (fns) => {
- if (fns && fns.length > 0) {
- require('../server/actions/importData')(svr, fns[0])
- last_path = path.dirname(fns[0])
- }
- })
- }
- }, {
- label: lang.export,
- accelerator: 'Alt+CommandOrControl+E',
- click: () => {
- dialog.showSaveDialog({
- title: lang.export,
- defaultPath: path.join(last_path || download_path ||
- paths.home_path, 'sh.json'),
- filters: [
- {name: 'JSON', extensions: ['json']},
- {name: 'All Files', extensions: ['*']}
- ]
- }, (fn) => {
- if (fn) {
- //svr.emit('to_export', fn)
- require('../server/actions/exportData')(svr, fn)
- last_path = path.dirname(fn)
- }
- })
- }
- }, {
- type: 'separator'
- }, {
- label: lang.preferences,
- accelerator: 'CommandOrControl+,',
- click: () => {
- //app.mainWindow.webContents.send('show_preferences')
- svr.broadcast('show_preferences')
- }
- }
- ]
- },
- {
- label: lang.edit,
- submenu: [
- {
- label: lang.undo,
- role: 'undo'
- }, {
- label: lang.redo,
- role: 'redo'
- }, {
- type: 'separator'
- }, {
- label: lang.menu_cut,
- role: 'cut'
- }, {
- label: lang.menu_copy,
- role: 'copy'
- }, {
- label: lang.menu_paste,
- role: 'paste'
- }, {
- label: lang.menu_delete,
- role: 'delete'
- }, {
- label: lang.menu_selectall,
- role: 'selectall'
- }, {
- type: 'separator'
- }, {
- label: lang.search,
- accelerator: 'CommandOrControl+F',
- click () {
- // ipcMain.emit('to_search');
- //app.mainWindow.webContents.send('to_search')
- svr.broadcast('search:start')
- }
- }, {
- label: lang.comment_current_line,
- accelerator: 'CommandOrControl+/',
- click () {
- // ipcMain.emit('to_search');
- //app.mainWindow.webContents.send('to_comment')
- svr.broadcast('to_comment')
- }
- }]
- }, {
- label: lang.view,
- submenu: [
- // {
- // label: 'Reload',
- // accelerator: 'CmdOrCtrl+R',
- // click (item, focusedWindow) {
- // if (focusedWindow) focusedWindow.reload()
- // }
- // },
- // {
- // label: 'Toggle Developer Tools',
- // accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
- // click (item, focusedWindow) {
- // if (focusedWindow) focusedWindow.webContents.toggleDevTools()
- // }
- // },
- // {
- // type: 'separator'
- // },
- {
- label: lang.menu_resetzoom,
- role: 'resetzoom'
- },
- {
- label: lang.menu_zoomin,
- role: 'zoomin'
- },
- {
- label: lang.menu_zoomout,
- role: 'zoomout'
- },
- {
- type: 'separator'
- },
- {
- label: lang.menu_togglefullscreen,
- role: 'togglefullscreen'
- }
- ]
- }, {
- label: lang.window,
- role: 'window',
- submenu: [{
- label: lang.menu_minimize,
- role: 'minimize'
- }, {
- label: lang.menu_close,
- role: 'close'
- }]
- }, {
- label: lang.help,
- role: 'help',
- submenu: [
- {
- label: lang.check_update,
- click () {
- checkUpdate.check()
- }
- }, {
- type: 'separator'
- }, {
- label: lang.feedback,
- click () {
- shell.openExternal('https://github.com/oldj/SwitchHosts/issues')
- .catch(e => console.log(e))
- }
- }, {
- label: lang.homepage,
- click () {
- shell.openExternal('https://oldj.github.io/SwitchHosts/')
- .catch(e => console.log(e))
- }
- }]
- }
- ]
- //const name = require('electron').app.getName()
- const name = 'SwitchHosts!'
- const os = process.platform
- if (os === 'darwin') {
- template.unshift({
- label: name,
- submenu: [
- {
- label: lang.menu_about,
- //role: 'about',
- click: () => {
- svr.broadcast('show-about')
- }
- }, {
- type: 'separator'
- },
- // {
- // role: 'services',
- // submenu: []
- // },
- // {
- // type: 'separator'
- // },
- {
- label: lang.menu_hide,
- role: 'hide'
- }, {
- label: lang.menu_hideothers,
- role: 'hideothers'
- }, {
- label: lang.menu_unhide,
- role: 'unhide'
- }, {
- type: 'separator'
- }, {
- label: lang.menu_quit,
- role: 'quit'
- }]
- })
- // Edit menu.
- /*template[2].submenu.push(
- {
- type: 'separator'
- },
- {
- label: 'Speech',
- submenu: [
- {
- role: 'startspeaking'
- },
- {
- role: 'stopspeaking'
- }
- ]
- }
- );*/
- // Window menu.
- template[4].submenu = [
- {
- label: lang.menu_close,
- accelerator: 'CmdOrCtrl+W',
- role: 'close'
- },
- {
- label: lang.menu_minimize,
- accelerator: 'CmdOrCtrl+M',
- role: 'minimize'
- },
- {
- label: lang.menu_zoom,
- role: 'zoom'
- },
- {
- type: 'separator'
- },
- {
- label: lang.menu_bringalltofront,// 'Bring All to Front',
- role: 'front'
- }
- ]
- } else if (os === 'win32' || os === 'linux') {
- template[0].submenu.unshift({
- type: 'separator'
- })
- template[0].submenu.unshift({
- label: `${lang.menu_about} ${name}`,
- //role: 'about',
- click: () => {
- svr.broadcast('show-about')
- }
- })
- template[0].submenu.push({
- type: 'separator'
- })
- template[0].submenu.push({
- label: lang.menu_quit,
- role: 'quit',
- accelerator: 'CmdOrCtrl+Q'
- })
- // VIEW
- template[2].submenu.splice(0, 4)
- }
- if (process.env.ENV === 'dev') {
- // VIEW
- template[3].submenu = [
- {
- label: lang.menu_reload,
- accelerator: 'CmdOrCtrl+R',
- click (item, focusedWindow) {
- if (focusedWindow) focusedWindow.reload()
- }
- },
- {
- label: lang.menu_toggle_developer_tools,// 'Toggle Developer Tools',
- accelerator: process.platform === 'darwin'
- ? 'Alt+Command+I'
- : 'Ctrl+Shift+I',
- click (item, focusedWindow) {
- if (focusedWindow) focusedWindow.webContents.toggleDevTools()
- }
- },
- {
- type: 'separator'
- }
- ].concat(template[3].submenu)
- }
- const menu = Menu.buildFromTemplate(template)
- Menu.setApplicationMenu(menu)
- }
- exports.init = function (app, language = 'en') {
- let lang = m_lang.getLang(language)
- doInit(app, lang)
- }
|