feat: review tray options
This commit is contained in:
1 parent
1fc3ffa693
commit
22c3baa4a8
3 files changed
+33
-22
No files matched your search
@@ -9,6 +9,7 @@ const options = {
|
|||||||
tray: {
|
tray: {
|
||||||
// all optional values from DEFAULT_TRAY_OPTIONS can de defined here
|
// all optional values from DEFAULT_TRAY_OPTIONS can de defined here
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
menu: false, // true, to use a tray menu ; false to toggle visibility on click on tray icon
|
||||||
trayWindow: false // true, to use a tray floating window attached to top try icon
|
trayWindow: false // true, to use a tray floating window attached to top try icon
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-22
@@ -1,25 +1,17 @@
|
|||||||
import { app, screen, Menu, Tray, BrowserWindow } from 'electron'
|
import { app, screen, Menu, Tray, BrowserWindow } from 'electron'
|
||||||
import Constants from './utils/Constants.ts'
|
import Constants from './utils/Constants.ts'
|
||||||
let tray;
|
import { debounce } from './utils/Util.ts'
|
||||||
|
let tray
|
||||||
let trayOptions;
|
let trayOptions;
|
||||||
|
|
||||||
export function createTray(window: BrowserWindow, options) {
|
export function createTray(window: BrowserWindow, options) {
|
||||||
trayOptions = options || Constants.DEFAULT_TRAY_OPTIONS;
|
trayOptions = options || Constants.DEFAULT_TRAY_OPTIONS;
|
||||||
|
// menu or trayWindow, you need to choose
|
||||||
|
if (trayOptions.trayWindow){
|
||||||
|
trayOptions.menu = false
|
||||||
|
}
|
||||||
|
|
||||||
tray = new Tray('buildAssets/icons/icon16.png');
|
tray = new Tray('buildAssets/icons/icon16.png');
|
||||||
|
|
||||||
tray.on('double-click', function (event) {
|
|
||||||
console.log('double-click')
|
|
||||||
toggleWindow(window)
|
|
||||||
});
|
|
||||||
tray.on('right-click', function (event) {
|
|
||||||
console.log('right-click')
|
|
||||||
toggleWindow(window)
|
|
||||||
});
|
|
||||||
tray.on('click', function (event) {
|
|
||||||
console.log('click')
|
|
||||||
toggleWindow(window)
|
|
||||||
});
|
|
||||||
tray.setToolTip(trayOptions.tooltip);
|
tray.setToolTip(trayOptions.tooltip);
|
||||||
if (trayOptions.menu) {
|
if (trayOptions.menu) {
|
||||||
const contextMenu = Menu.buildFromTemplate([
|
const contextMenu = Menu.buildFromTemplate([
|
||||||
@@ -32,7 +24,7 @@ export function createTray(window: BrowserWindow, options) {
|
|||||||
{
|
{
|
||||||
label: 'Hide App',
|
label: 'Hide App',
|
||||||
click: () => {
|
click: () => {
|
||||||
window.hide()
|
hideWindow(window)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -45,6 +37,13 @@ export function createTray(window: BrowserWindow, options) {
|
|||||||
// tray icon only with classic window
|
// tray icon only with classic window
|
||||||
tray.setContextMenu(contextMenu)
|
tray.setContextMenu(contextMenu)
|
||||||
} else {
|
} else {
|
||||||
|
// handle click on tray icon
|
||||||
|
tray.on('right-click', function (event) {
|
||||||
|
debounce(() => toggleWindow(window));
|
||||||
|
});
|
||||||
|
tray.on('click', function (event) {
|
||||||
|
debounce(() => toggleWindow(window));
|
||||||
|
});
|
||||||
// no menu for tray window
|
// no menu for tray window
|
||||||
window.setMenu(null)
|
window.setMenu(null)
|
||||||
tray.setContextMenu(null)
|
tray.setContextMenu(null)
|
||||||
@@ -55,8 +54,9 @@ export function createTray(window: BrowserWindow, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function hideWindow(window: BrowserWindow) {
|
export function hideWindow(window: BrowserWindow) {
|
||||||
console.log('hide window');
|
|
||||||
window.hide();
|
window.hide();
|
||||||
|
// if (!trayOptions.trayWindow) return;
|
||||||
|
// hide window when click elsewhere on screen
|
||||||
window.on("blur", () => {
|
window.on("blur", () => {
|
||||||
// dont close if devtools
|
// dont close if devtools
|
||||||
if (!window.webContents.isDevToolsOpened()) {
|
if (!window.webContents.isDevToolsOpened()) {
|
||||||
@@ -74,17 +74,15 @@ export function toggleWindow(window: BrowserWindow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function showWindow(window: BrowserWindow) {
|
export function showWindow(window: BrowserWindow) {
|
||||||
console.log('show window');
|
|
||||||
window.show()
|
window.show()
|
||||||
|
|
||||||
alignWindow(window)
|
alignWindow(window)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function alignWindow(window: BrowserWindow) {
|
export function alignWindow(window: BrowserWindow) {
|
||||||
if (!trayOptions.trayWindow) return;
|
if (!trayOptions.trayWindow) return;
|
||||||
|
|
||||||
const position = calculateWindowPosition();
|
|
||||||
const b = window.getBounds()
|
const b = window.getBounds()
|
||||||
|
const position = calculateWindowPosition(b);
|
||||||
window.setBounds({
|
window.setBounds({
|
||||||
width: b.width,
|
width: b.width,
|
||||||
height: b.height,
|
height: b.height,
|
||||||
@@ -93,10 +91,8 @@ export function alignWindow(window: BrowserWindow) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateWindowPosition() {
|
function calculateWindowPosition(b) {
|
||||||
const margin = trayOptions.margin;
|
const margin = trayOptions.margin;
|
||||||
const b = trayOptions;
|
|
||||||
|
|
||||||
const screenBounds = screen.getPrimaryDisplay().size;
|
const screenBounds = screen.getPrimaryDisplay().size;
|
||||||
const trayBounds = tray.getBounds();
|
const trayBounds = tray.getBounds();
|
||||||
const bottom = trayBounds.y > screenBounds.height / 2 ;
|
const bottom = trayBounds.y > screenBounds.height / 2 ;
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
export function debounce(func, timeout = 200){
|
||||||
|
let timer;
|
||||||
|
return function(...args) {
|
||||||
|
if (!timer) {
|
||||||
|
func.apply(this, args);
|
||||||
|
}
|
||||||
|
clearTimeout(timer);
|
||||||
|
timer = setTimeout(() => {
|
||||||
|
timer = undefined;
|
||||||
|
}, timeout);
|
||||||
|
}();
|
||||||
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user