Files
Electron.NET/ElectronNET.Host/api/tray.ts

150 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-01-04 03:27:12 +01:00
import { Menu, Tray, nativeImage } from 'electron';
let tray: { value: Electron.Tray } = (global['$tray'] = global['tray'] || { value: null });
let electronSocket;
2018-09-26 01:31:53 +02:00
export = (socket: SocketIO.Socket) => {
electronSocket = socket;
2017-10-25 21:28:43 +02:00
socket.on('register-tray-click', (id) => {
if (tray.value) {
tray.value.on('click', (event, bounds) => {
electronSocket.emit('tray-click-event' + id, [(<any>event).__proto__, bounds]);
2017-10-18 05:30:36 +02:00
});
}
});
2017-10-25 21:28:43 +02:00
socket.on('register-tray-right-click', (id) => {
if (tray.value) {
tray.value.on('right-click', (event, bounds) => {
electronSocket.emit('tray-right-click-event' + id, [(<any>event).__proto__, bounds]);
2017-10-18 05:30:36 +02:00
});
}
});
2017-10-25 21:28:43 +02:00
socket.on('register-tray-double-click', (id) => {
if (tray.value) {
tray.value.on('double-click', (event, bounds) => {
electronSocket.emit('tray-double-click-event' + id, [(<any>event).__proto__, bounds]);
2017-10-18 05:30:36 +02:00
});
}
});
2017-10-25 21:28:43 +02:00
socket.on('register-tray-balloon-show', (id) => {
if (tray.value) {
tray.value.on('balloon-show', () => {
electronSocket.emit('tray-balloon-show-event' + id);
2017-10-18 05:30:36 +02:00
});
}
});
2019-01-04 03:27:12 +01:00
2017-10-25 21:28:43 +02:00
socket.on('register-tray-balloon-click', (id) => {
if (tray.value) {
tray.value.on('balloon-click', () => {
electronSocket.emit('tray-balloon-click-event' + id);
2017-10-18 05:30:36 +02:00
});
}
});
2017-10-25 21:28:43 +02:00
socket.on('register-tray-balloon-closed', (id) => {
if (tray.value) {
tray.value.on('balloon-closed', () => {
electronSocket.emit('tray-balloon-closed-event' + id);
2017-10-18 05:30:36 +02:00
});
}
});
socket.on('create-tray', (image, menuItems) => {
const trayIcon = nativeImage.createFromPath(image);
tray.value = new Tray(trayIcon);
if (menuItems) {
const menu = Menu.buildFromTemplate(menuItems);
addMenuItemClickConnector(menu.items, (id) => {
electronSocket.emit('trayMenuItemClicked', id);
});
tray.value.setContextMenu(menu);
}
});
2017-10-15 21:39:52 +02:00
2017-10-18 05:30:36 +02:00
socket.on('tray-destroy', () => {
if (tray.value) {
tray.value.destroy();
2017-10-18 05:30:36 +02:00
}
});
socket.on('tray-setImage', (image) => {
if (tray.value) {
tray.value.setImage(image);
2017-10-18 05:30:36 +02:00
}
});
socket.on('tray-setPressedImage', (image) => {
if (tray.value) {
2019-01-04 03:27:12 +01:00
const img = nativeImage.createFromPath(image);
tray.value.setPressedImage(img);
2017-10-18 05:30:36 +02:00
}
});
socket.on('tray-setToolTip', (toolTip) => {
if (tray.value) {
tray.value.setToolTip(toolTip);
2017-10-18 05:30:36 +02:00
}
});
socket.on('tray-setTitle', (title) => {
if (tray.value) {
tray.value.setTitle(title);
2017-10-18 05:30:36 +02:00
}
});
socket.on('tray-displayBalloon', (options) => {
if (tray.value) {
tray.value.displayBalloon(options);
2017-10-18 05:30:36 +02:00
}
});
socket.on('tray-isDestroyed', () => {
if (tray.value) {
const isDestroyed = tray.value.isDestroyed();
electronSocket.emit('tray-isDestroyedCompleted', isDestroyed);
2017-10-18 05:30:36 +02:00
}
});
socket.on('register-tray-on-event', (eventName, listenerName) => {
if (tray.value){
tray.value.on(eventName, (...args) => {
if (args.length > 1) {
electronSocket.emit(listenerName, args[1]);
} else {
electronSocket.emit(listenerName);
}
});
}
});
socket.on('register-tray-once-event', (eventName, listenerName) => {
if (tray.value){
tray.value.once(eventName, (...args) => {
if (args.length > 1) {
electronSocket.emit(listenerName, args[1]);
} else {
electronSocket.emit(listenerName);
}
});
}
});
2017-10-15 21:39:52 +02:00
function addMenuItemClickConnector(menuItems, callback) {
menuItems.forEach((item) => {
2017-10-18 05:30:36 +02:00
if (item.submenu && item.submenu.items.length > 0) {
2017-10-15 21:39:52 +02:00
addMenuItemClickConnector(item.submenu.items, callback);
}
2017-10-18 05:30:36 +02:00
2019-01-04 03:27:12 +01:00
if ('id' in item && item.id) {
2017-10-15 21:39:52 +02:00
item.click = () => { callback(item.id); };
}
});
}
2019-01-04 03:27:12 +01:00
};