| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /**
- * tray
- * @author oldj
- * @blog http://oldj.net
- */
- 'use strict';
- const fs = require('fs');
- const path = require('path');
- const {Menu, Tray, ipcMain, shell} = require('electron');
- const m_lang = require('../lang');
- const m_chk_update = require('../../bg/check_for_update');
- const pref = require('./../libs/pref');
- const os = process.platform;
- const util = require('../libs/util');
- const current_version = require('../../version').version;
- let tray = null;
- function makeMenu(app, list, contents, sys_lang) {
- let menu = [];
- let lang = m_lang.getLang(pref.get('user_language', sys_lang));
- menu.push({
- label: 'SwitchHosts!',
- type: 'normal',
- // sublabel: util.formatVersion(current_version), // does not work on Mac
- click: () => {
- app.emit('show');
- }
- });
- menu.push({label: util.formatVersion(current_version), type: 'normal', enabled: false});
- menu.push({label: '-', type: 'separator'});
- let ac = '1234567890abcdefghijklmnopqrstuvwxyz'.split('');
- list.map((item, idx) => {
- menu.push({
- label: item.title || 'untitled',
- type: 'checkbox',
- checked: item.on,
- accelerator: ac[idx],
- click: () => {
- contents.send('tray_toggle_host', idx);
- contents.send('get_host_list');
- }
- });
- });
- menu.push({type: 'separator'});
- menu.push({
- label: lang.feedback, type: 'normal', click: () => {
- shell.openExternal('https://github.com/oldj/SwitchHosts/issues');
- }
- });
- menu.push({
- label: lang.check_update, type: 'normal', click: () => {
- m_chk_update.check();
- }
- });
- if (os === 'darwin') {
- menu.push({
- label: lang.toggle_dock_icon, type: 'normal', click: () => {
- let is_dock_visible = app.dock.isVisible();
- if (is_dock_visible) {
- app.dock.hide();
- } else {
- app.dock.show();
- }
- pref.set('is_dock_icon_hidden', is_dock_visible);
- }
- });
- }
- menu.push({type: 'separator'});
- menu.push({
- label: lang.quit, type: 'normal', accelerator: 'CommandOrControl+Q', click: () => {
- app.quit();
- }
- });
- return menu;
- }
- function makeTray(app, contents, sys_lang = 'en') {
- let icon = 'logo.png';
- if (process.platform === 'darwin') {
- icon = 'ilogoTemplate.png';
- }
- tray = new Tray(path.join(__dirname, '..', 'assets', icon));
- tray.setToolTip('SwitchHosts!');
- contents.send('get_host_list');
- ipcMain.on('send_host_list', (e, d) => {
- const contextMenu = Menu.buildFromTemplate(makeMenu(app, d, contents, sys_lang));
- tray.setContextMenu(contextMenu);
- });
- let is_dock_icon_hidden = pref.get('is_dock_icon_hidden', false);
- if (is_dock_icon_hidden) {
- app.dock.hide();
- }
- // windows only
- if (process.platform === 'win32') {
- tray.on('click', () => {
- app.emit('show');
- });
- }
- }
- exports.makeTray = makeTray;
|