tray.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, shell} = require('electron')
  10. const m_lang = require('../server/lang')
  11. const checkUpdate = require('../server/checkUpdate')
  12. const pref = require('../server/pref')
  13. const setPref = require('../server/actions/setPref')
  14. const os = process.platform
  15. const current_version = require('../version').version
  16. const svr = require('../server/svr')
  17. const formatVersion = require('../libs/formatVersion')
  18. const getUserHosts = require('../server/actions/getUserHosts')
  19. let tray = null
  20. function formatTitle (title) {
  21. if (!title) {
  22. return 'untitled'
  23. }
  24. let max_len = 30
  25. if (title.length < max_len) {
  26. return title
  27. }
  28. return title.substr(0, max_len) + '..'
  29. }
  30. function makeMenu (app, list, contents, sys_lang) {
  31. let menu = []
  32. let lang = m_lang.getLang(pref.get('user_language', sys_lang))
  33. menu.push({
  34. label: 'SwitchHosts!',
  35. type: 'normal',
  36. // sublabel: util.formatVersion(current_version), // does not work on Mac
  37. click: () => {
  38. app.emit('show')
  39. }
  40. })
  41. menu.push({
  42. label: formatVersion(current_version),
  43. type: 'normal',
  44. enabled: false
  45. })
  46. menu.push({label: '-', type: 'separator'})
  47. let ac = '1234567890abcdefghijklmnopqrstuvwxyz'.split('')
  48. list.map((item, idx) => {
  49. menu.push({
  50. label: formatTitle(item.title),
  51. type: 'checkbox',
  52. checked: item.on,
  53. accelerator: ac[idx],
  54. click: () => {
  55. svr.broadcast('toggle_hosts', Object.assign({}, item))
  56. //svr.emit('update_tray')
  57. }
  58. })
  59. })
  60. menu.push({type: 'separator'})
  61. menu.push({
  62. label: lang.feedback, type: 'normal', click: () => {
  63. shell.openExternal('https://github.com/oldj/SwitchHosts/issues')
  64. }
  65. })
  66. menu.push({
  67. label: lang.check_update, type: 'normal', click: () => {
  68. checkUpdate.check()
  69. }
  70. })
  71. if (os === 'darwin') {
  72. menu.push({
  73. label: lang.toggle_dock_icon, type: 'normal', click: () => {
  74. let is_dock_visible = app.dock.isVisible()
  75. if (is_dock_visible) {
  76. app.dock.hide()
  77. } else {
  78. app.dock.show()
  79. }
  80. setPref(svr, 'is_dock_icon_hidden', is_dock_visible)
  81. }
  82. })
  83. }
  84. menu.push({type: 'separator'})
  85. menu.push({
  86. label: lang.quit,
  87. type: 'normal',
  88. accelerator: 'CommandOrControl+Q',
  89. click: () => {
  90. app.quit()
  91. }
  92. })
  93. return menu
  94. }
  95. function makeTitle(list = []) {
  96. const currItems = (list || []).filter(item => item.on) || []
  97. const appendTitleEvt = function(lis = [], opr = ',') {
  98. let _str = ''
  99. if(lis.length) {
  100. const appendTitle = (prev, curr) => {
  101. return {
  102. title: `${prev.title}${prev.title ? `${opr}` : ''}${curr.title}`
  103. }
  104. }
  105. _str = currItems.reduce(appendTitle, { title: '' }).title || ''
  106. }
  107. return _str;
  108. };
  109. const _ori = appendTitleEvt(list)
  110. return {
  111. ori: _ori,
  112. show: _ori.length > 20 ? `${_ori.substr(0, 20)}...` : _ori,
  113. tips: `${appendTitleEvt(list, '\n')}`
  114. }
  115. }
  116. function makeTray (app, contents, sys_lang = 'en') {
  117. const lang = m_lang.getLang(sys_lang)
  118. let icon = 'logo.png'
  119. if (process.platform === 'darwin') {
  120. icon = 'ilogoTemplate.png'
  121. }
  122. tray = new Tray(path.join(__dirname, '..', 'assets', icon))
  123. svr.on('update_tray', () => {
  124. getUserHosts()
  125. .then(list => {
  126. let contextMenu = Menu.buildFromTemplate(makeMenu(app, list, contents, sys_lang))
  127. tray.setContextMenu(contextMenu)
  128. const { ori = '', show = '', tips = '' } = makeTitle(list)
  129. tray.setTitle(show)
  130. tray.setToolTip(ori ? `\n${lang.current_active_hosts}: \n\n${tips}\n` : 'SwitchHosts!')
  131. })
  132. })
  133. let is_dock_icon_hidden = pref.get('is_dock_icon_hidden', false)
  134. if (is_dock_icon_hidden) {
  135. app.dock.hide()
  136. }
  137. tray.on('click', () => {
  138. if (process.platform === 'win32') {
  139. app.emit('show')
  140. }
  141. })
  142. svr.on('hosts_saved', () => {
  143. svr.emit('update_tray')
  144. })
  145. svr.emit('update_tray')
  146. }
  147. exports.makeTray = makeTray