Move service of tray control to separate module

This commit is contained in:
Nick Denry 2019-04-26 22:09:08 +03:00
parent 84f244479a
commit 8135478bb5
3 changed files with 49 additions and 29 deletions

34
js/tray-service.js Normal file
View File

@ -0,0 +1,34 @@
/**
* Module for Tray functions.
*/
const {Tray} = require('electron')
let trayServiceWindow = null
let tray = null
let trayService = {}
trayService.initTray = (window) => {
trayServiceWindow = window
let iconPath = __dirname + '/../images/icon.png'
tray = new Tray(iconPath)
tray.setToolTip('Chimeverse')
tray.on('click', function() {
trayService.hideEnvelope()
trayServiceWindow.show()
})
}
trayService.showEnvelope = () => {
tray.setImage(__dirname + '/../images/envelope.png')
}
trayService.hideEnvelope = () => {
tray.setImage(__dirname + '/../images/icon.png')
}
module.exports = trayService

40
main.js
View File

@ -1,24 +1,15 @@
// Modules to control application life and create native browser window
const {app, BrowserWindow, Tray, shell} = require('electron')
const {app, BrowserWindow, shell} = require('electron')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
let tray = null;
// Require other app modules
const trayService = require(__dirname+'/js/tray-service')
function initApp() {
let iconPath = __dirname + '/images/icon.png'
tray = new Tray(iconPath)
tray.setToolTip('Chimeverse')
createWindow();
tray.on('click', function() {
hideEnvelope();
if (mainWindow === null) {
createWindow();
}
mainWindow.show();
});
createWindow()
}
function createWindow () {
@ -34,6 +25,9 @@ function createWindow () {
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Init tray
trayService.initTray(mainWindow)
// Open the DevTools.
// mainWindow.webContents.openDevTools()
@ -46,10 +40,10 @@ function createWindow () {
})
mainWindow.webContents.on('new-window', function(e, url) {
e.preventDefault();
shell.openExternal(url);
});
//mainWindow.webContents.openDevTools();
e.preventDefault()
shell.openExternal(url)
})
//mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
@ -77,18 +71,10 @@ app.on('activate', function () {
// code. You can also put them in separate files and require them here.
// Allow to play audio automatically
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required')
/**
* Export functions
*/
function showEnvelope() {
tray.setImage(__dirname + '/images/envelope.png')
}
function hideEnvelope() {
tray.setImage(__dirname + '/images/icon.png')
}
exports.showEnvelope = showEnvelope;
exports.hideEnvelope = hideEnvelope;
exports.trayService = trayService

View File

@ -72,11 +72,11 @@ angApp.factory('SystemService', () => {
}
systemService.showEnvelope = () => {
remote.require('./main').showEnvelope()
remote.require('./main').trayService.showEnvelope()
}
systemService.hideEnvelope = () => {
remote.require('./main').hideEnvelope()
remote.require('./main').trayService.hideEnvelope()
}
systemService.reloadWindow = () => {