main_menu.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * @author oldj
  3. * @blog https://oldj.net
  4. */
  5. 'use strict'
  6. const path = require('path')
  7. const paths = require('../server/paths')
  8. const {Menu, shell, dialog} = require('electron')
  9. const m_lang = require('../server/lang')
  10. const getPref = require('../server/actions/getPref')
  11. const checkUpdate = require('../server/checkUpdate')
  12. const svr = require('../server/svr')
  13. const version = require('../version')
  14. function doInit (app, lang) {
  15. let last_path = null
  16. let download_path = app.getPath('downloads')
  17. const template = [
  18. {
  19. label: lang.file,
  20. submenu: [
  21. {
  22. label: lang.new,
  23. accelerator: 'CommandOrControl+N',
  24. click: () => {
  25. svr.broadcast('add_hosts')
  26. }
  27. }, {
  28. type: 'separator'
  29. }, {
  30. label: lang.import,
  31. accelerator: 'Alt+CommandOrControl+I',
  32. click: () => {
  33. dialog.showOpenDialog({
  34. title: lang.import,
  35. defaultPath: path.join(last_path || download_path ||
  36. paths.home_path, 'sh.json'),
  37. filters: [
  38. {name: 'JSON', extensions: ['json']},
  39. {name: 'All Files', extensions: ['*']}
  40. ]
  41. }, (fns) => {
  42. if (fns && fns.length > 0) {
  43. require('../server/actions/importData')(svr, fns[0])
  44. last_path = path.dirname(fns[0])
  45. }
  46. })
  47. }
  48. }, {
  49. label: lang.export,
  50. accelerator: 'Alt+CommandOrControl+E',
  51. click: () => {
  52. dialog.showSaveDialog({
  53. title: lang.export,
  54. defaultPath: path.join(last_path || download_path ||
  55. paths.home_path, 'sh.json'),
  56. filters: [
  57. {name: 'JSON', extensions: ['json']},
  58. {name: 'All Files', extensions: ['*']}
  59. ]
  60. }, (fn) => {
  61. if (fn) {
  62. //svr.emit('to_export', fn)
  63. require('../server/actions/exportData')(svr, fn)
  64. last_path = path.dirname(fn)
  65. }
  66. })
  67. }
  68. }, {
  69. type: 'separator'
  70. }, {
  71. label: lang.preferences,
  72. accelerator: 'CommandOrControl+,',
  73. click: () => {
  74. //app.mainWindow.webContents.send('show_preferences')
  75. svr.broadcast('show_preferences')
  76. }
  77. }
  78. ]
  79. },
  80. {
  81. label: lang.edit,
  82. submenu: [
  83. {
  84. label: lang.undo,
  85. role: 'undo'
  86. }, {
  87. label: lang.redo,
  88. role: 'redo'
  89. }, {
  90. type: 'separator'
  91. }, {
  92. label: lang.menu_cut,
  93. role: 'cut'
  94. }, {
  95. label: lang.menu_copy,
  96. role: 'copy'
  97. }, {
  98. label: lang.menu_paste,
  99. role: 'paste'
  100. }, {
  101. label: lang.menu_delete,
  102. role: 'delete'
  103. }, {
  104. label: lang.menu_selectall,
  105. role: 'selectall'
  106. }, {
  107. type: 'separator'
  108. }, {
  109. label: lang.search,
  110. accelerator: 'CommandOrControl+F',
  111. click () {
  112. // ipcMain.emit('to_search');
  113. //app.mainWindow.webContents.send('to_search')
  114. svr.broadcast('search:start')
  115. }
  116. }, {
  117. label: lang.comment_current_line,
  118. accelerator: 'CommandOrControl+/',
  119. click () {
  120. // ipcMain.emit('to_search');
  121. //app.mainWindow.webContents.send('to_comment')
  122. svr.broadcast('to_comment')
  123. }
  124. }]
  125. }, {
  126. label: lang.view,
  127. submenu: [
  128. // {
  129. // label: 'Reload',
  130. // accelerator: 'CmdOrCtrl+R',
  131. // click (item, focusedWindow) {
  132. // if (focusedWindow) focusedWindow.reload()
  133. // }
  134. // },
  135. // {
  136. // label: 'Toggle Developer Tools',
  137. // accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
  138. // click (item, focusedWindow) {
  139. // if (focusedWindow) focusedWindow.webContents.toggleDevTools()
  140. // }
  141. // },
  142. // {
  143. // type: 'separator'
  144. // },
  145. {
  146. label: lang.menu_resetzoom,
  147. role: 'resetzoom'
  148. },
  149. {
  150. label: lang.menu_zoomin,
  151. role: 'zoomin'
  152. },
  153. {
  154. label: lang.menu_zoomout,
  155. role: 'zoomout'
  156. },
  157. {
  158. type: 'separator'
  159. },
  160. {
  161. label: lang.menu_togglefullscreen,
  162. role: 'togglefullscreen'
  163. }
  164. ]
  165. }, {
  166. label: lang.window,
  167. role: 'window',
  168. submenu: [{
  169. label: lang.menu_minimize,
  170. role: 'minimize'
  171. }, {
  172. label: lang.menu_close,
  173. role: 'close'
  174. }]
  175. }, {
  176. label: lang.help,
  177. role: 'help',
  178. submenu: [
  179. {
  180. label: lang.check_update,
  181. click () {
  182. checkUpdate.check()
  183. }
  184. }, {
  185. type: 'separator'
  186. }, {
  187. label: lang.feedback,
  188. click () {
  189. shell.openExternal('https://github.com/oldj/SwitchHosts/issues')
  190. }
  191. }, {
  192. label: lang.homepage,
  193. click () {
  194. shell.openExternal('https://oldj.github.io/SwitchHosts/')
  195. }
  196. }]
  197. }
  198. ]
  199. //const name = require('electron').app.getName()
  200. const name = 'SwitchHosts!'
  201. const os = process.platform
  202. if (os === 'darwin') {
  203. template.unshift({
  204. label: name,
  205. submenu: [
  206. {
  207. label: lang.menu_about,
  208. //role: 'about',
  209. click: () => {
  210. svr.broadcast('show-about')
  211. }
  212. }, {
  213. type: 'separator'
  214. },
  215. // {
  216. // role: 'services',
  217. // submenu: []
  218. // },
  219. // {
  220. // type: 'separator'
  221. // },
  222. {
  223. label: lang.menu_hide,
  224. role: 'hide'
  225. }, {
  226. label: lang.menu_hideothers,
  227. role: 'hideothers'
  228. }, {
  229. label: lang.menu_unhide,
  230. role: 'unhide'
  231. }, {
  232. type: 'separator'
  233. }, {
  234. label: lang.menu_quit,
  235. role: 'quit'
  236. }]
  237. })
  238. // Edit menu.
  239. /*template[2].submenu.push(
  240. {
  241. type: 'separator'
  242. },
  243. {
  244. label: 'Speech',
  245. submenu: [
  246. {
  247. role: 'startspeaking'
  248. },
  249. {
  250. role: 'stopspeaking'
  251. }
  252. ]
  253. }
  254. );*/
  255. // Window menu.
  256. template[4].submenu = [
  257. {
  258. label: lang.menu_close,
  259. accelerator: 'CmdOrCtrl+W',
  260. role: 'close'
  261. },
  262. {
  263. label: lang.menu_minimize,
  264. accelerator: 'CmdOrCtrl+M',
  265. role: 'minimize'
  266. },
  267. {
  268. label: lang.menu_zoom,
  269. role: 'zoom'
  270. },
  271. {
  272. type: 'separator'
  273. },
  274. {
  275. label: lang.menu_bringalltofront,// 'Bring All to Front',
  276. role: 'front'
  277. }
  278. ]
  279. } else if (os === 'win32' || os === 'linux') {
  280. template[0].submenu.unshift({
  281. type: 'separator'
  282. })
  283. template[0].submenu.unshift({
  284. label: `${lang.menu_about} ${name}`,
  285. //role: 'about',
  286. click: () => {
  287. svr.broadcast('show-about')
  288. }
  289. })
  290. template[0].submenu.push({
  291. type: 'separator'
  292. })
  293. template[0].submenu.push({
  294. label: lang.menu_quit,
  295. role: 'quit',
  296. accelerator: 'CmdOrCtrl+Q'
  297. })
  298. // VIEW
  299. template[2].submenu.splice(0, 4)
  300. }
  301. if (process.env.ENV === 'dev') {
  302. // VIEW
  303. template[3].submenu = [
  304. {
  305. label: lang.menu_reload,
  306. accelerator: 'CmdOrCtrl+R',
  307. click (item, focusedWindow) {
  308. if (focusedWindow) focusedWindow.reload()
  309. }
  310. },
  311. {
  312. label: lang.menu_toggle_developer_tools,// 'Toggle Developer Tools',
  313. accelerator: process.platform === 'darwin'
  314. ? 'Alt+Command+I'
  315. : 'Ctrl+Shift+I',
  316. click (item, focusedWindow) {
  317. if (focusedWindow) focusedWindow.webContents.toggleDevTools()
  318. }
  319. },
  320. {
  321. type: 'separator'
  322. }
  323. ].concat(template[3].submenu)
  324. }
  325. const menu = Menu.buildFromTemplate(template)
  326. Menu.setApplicationMenu(menu)
  327. }
  328. exports.init = function (app, language = 'en') {
  329. let lang = m_lang.getLang(language)
  330. doInit(app, lang)
  331. }