plex-web-downloader/js/custom-tray-menu.js

113 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2015-10-09 15:12:23 -04:00
"use strict";
2017-07-10 07:51:00 -04:00
var gui = window.require('nw.gui');
2015-10-09 15:12:23 -04:00
var util = require('util');
2017-07-10 07:51:00 -04:00
2015-10-09 15:12:23 -04:00
class CustomTrayMenu {
constructor(windowPath, trayIcon, windowOptions) {
this.shown = false;
this.iconWidth = 12;
//valeur par défaut
this.trayIcon = trayIcon || 'tray2.png';
this.menuWindowPath = windowPath || 'custom-tray-menu.html';
this.menuWndowOptions = windowOptions || {
width: 185,
height: 143
};
this._initTray();
this._initMenuWindow();
2017-07-10 07:51:00 -04:00
2015-10-09 15:12:23 -04:00
this.tray.on('click', this.toggleTrayMenuAt.bind(this));
}
// remove tray, cose custom menu window
remove() {
this.tray.remove();
this.tray = null;
this.trayMenu.close();
}
_initTray() {
this.tray = new gui.Tray({
title: '',
icon: this.trayIcon,
alticon: '',
tooltip: window.document.title,
iconsAreTemplates: false
});
}
_initMenuWindow() {
var windowOptions = util._extend({
width: 200,
height: 100,
frame: false,
transparent: true,
resizable: false,
show: false,
'show_in_taskbar': process.platform == "darwin"
}, this.menuWndowOptions);
this.trayMenu = gui.Window.open(this.menuWindowPath, windowOptions);
// add class to new window's body to apply platform specific css
this.trayMenu.on('document-end', function() {
this.trayMenu.window.document.body.className += ' ' + process.platform;
2015-10-11 16:13:49 -04:00
//console.log("Adding class " + process.platform);
2015-10-09 15:12:23 -04:00
}.bind(this));
this.trayMenu.on('blur', function () {
this.trayMenu.hide();
this.shown = false;
2015-10-11 16:13:49 -04:00
//console.log('Hide custom menu');
2015-10-09 15:12:23 -04:00
}.bind(this));
}
// called when user click on tray icon
toggleTrayMenuAt(position) {
/*if (this.shown) {
this.trayMenu.hide(); // this will trigger listener added above
this.shown = false;
} else {*/
this.translate(position);
this.trayMenu.moveTo(position.x, position.y);
//global.main_win.show();
//global.main_win.focus();
this.trayMenu.show();
//this.trayMenu.focus();
this.shown = true;
2015-10-11 16:13:49 -04:00
//console.log('Show custom menu');
2015-10-09 15:12:23 -04:00
//}
}
// calculdate position for window to appear
translate(pos) {
2015-10-11 16:13:49 -04:00
//console.log("Click position: " + util.inspect(pos));
2015-10-09 15:12:23 -04:00
if (process.platform == 'darwin') {
pos.x -= Math.floor(this.trayMenu.width / 2 - this.iconWidth);
} else {
pos.x -= Math.floor(this.trayMenu.width / 2);
// for windows can not make exac position, because we have position of click. Just move it 5px up
pos.y -= this._trayAreaIsTop(pos) ? 0 : this.trayMenu.height + this.iconWidth / 2 + 5;
}
}
_trayAreaIsTop(pos) {
var screen;
if (gui.Screen.Init) gui.Screen.Init();
function posInBounds(s) {
return pos.y > s.bounds.y && pos.y < (s.bounds.y + s.bounds.height) &&
pos.x > s.bounds.x && pos.x < (s.bounds.x + s.bounds.width);
}
screen = gui.Screen.screens.filter(posInBounds)[0];
return pos.y < (screen.bounds.y + screen.bounds.height) / 2;
}
}
module.exports = CustomTrayMenu;