tray.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * tray
  3. * @author oldj
  4. * @blog http://oldj.net
  5. */
  6. 'use strict';
  7. const fs = require('fs');
  8. const path = require('path');
  9. const {Menu, Tray, ipcMain, shell} = require('electron');
  10. const m_lang = require('../lang');
  11. const m_chk_update = require('../../bg/check_for_update');
  12. const pref = require('./../libs/pref');
  13. const os = process.platform;
  14. const util = require('../libs/util');
  15. const current_version = require('../../version').version;
  16. let tray = null;
  17. function makeMenu(app, list, contents, sys_lang) {
  18. let menu = [];
  19. let lang = m_lang.getLang(pref.get('user_language', sys_lang));
  20. menu.push({
  21. label: 'SwitchHosts!',
  22. type: 'normal',
  23. // sublabel: util.formatVersion(current_version), // does not work on Mac
  24. click: () => {
  25. app.emit('show');
  26. }
  27. });
  28. menu.push({label: util.formatVersion(current_version), type: 'normal', enabled: false});
  29. menu.push({label: '-', type: 'separator'});
  30. let ac = '1234567890abcdefghijklmnopqrstuvwxyz'.split('');
  31. list.map((item, idx) => {
  32. menu.push({
  33. label: item.title || 'untitled',
  34. type: 'checkbox',
  35. checked: item.on,
  36. accelerator: ac[idx],
  37. click: () => {
  38. contents.send('tray_toggle_host', idx);
  39. contents.send('get_host_list');
  40. }
  41. });
  42. });
  43. menu.push({type: 'separator'});
  44. menu.push({
  45. label: lang.feedback, type: 'normal', click: () => {
  46. shell.openExternal('https://github.com/oldj/SwitchHosts/issues');
  47. }
  48. });
  49. menu.push({
  50. label: lang.check_update, type: 'normal', click: () => {
  51. m_chk_update.check();
  52. }
  53. });
  54. if (os === 'darwin') {
  55. menu.push({
  56. label: lang.toggle_dock_icon, type: 'normal', click: () => {
  57. let is_dock_visible = app.dock.isVisible();
  58. if (is_dock_visible) {
  59. app.dock.hide();
  60. } else {
  61. app.dock.show();
  62. }
  63. pref.set('is_dock_icon_hidden', is_dock_visible);
  64. }
  65. });
  66. }
  67. menu.push({type: 'separator'});
  68. menu.push({
  69. label: lang.quit, type: 'normal', accelerator: 'CommandOrControl+Q', click: () => {
  70. app.quit();
  71. }
  72. });
  73. return menu;
  74. }
  75. function makeTray(app, contents, sys_lang = 'en') {
  76. let icon = 'logo.png';
  77. if (process.platform === 'darwin') {
  78. icon = 'ilogoTemplate.png';
  79. }
  80. tray = new Tray(path.join(__dirname, '..', 'assets', icon));
  81. tray.setToolTip('SwitchHosts!');
  82. contents.send('get_host_list');
  83. ipcMain.on('send_host_list', (e, d) => {
  84. const contextMenu = Menu.buildFromTemplate(makeMenu(app, d, contents, sys_lang));
  85. tray.setContextMenu(contextMenu);
  86. });
  87. let is_dock_icon_hidden = pref.get('is_dock_icon_hidden', false);
  88. if (is_dock_icon_hidden) {
  89. app.dock.hide();
  90. }
  91. // windows only
  92. if (process.platform === 'win32') {
  93. tray.on('click', () => {
  94. app.emit('show');
  95. });
  96. }
  97. }
  98. exports.makeTray = makeTray;