feat: review tray options
This commit is contained in:
1 parent
ecaeb7aa54
commit
1fc3ffa693
4 files changed
+67
-74
No files matched your search
+25
-21
@@ -1,20 +1,15 @@
|
|||||||
import { app, BrowserWindow, RenderProcessGoneDetails, BrowserWindowConstructorOptions } from 'electron'
|
import { app, BrowserWindow, RenderProcessGoneDetails, BrowserWindowConstructorOptions } from 'electron'
|
||||||
import Constants from './utils/Constants'
|
import Constants, { TrayOptions } from './utils/Constants'
|
||||||
import IPCs from './IPCs'
|
import IPCs from './IPCs'
|
||||||
import { createTray, setWindowAutoHide, showWindow } from './tray.ts'
|
import { createTray, hideWindow, showWindow } from './tray.ts'
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
tray: true,
|
|
||||||
trayWindow: false, // true, to use a tray floating window attached to top try icon
|
|
||||||
width: Constants.IS_DEV_ENV ? 1500 : 1200,
|
width: Constants.IS_DEV_ENV ? 1500 : 1200,
|
||||||
height: 650,
|
height: 650,
|
||||||
trayOptions: {
|
tray: {
|
||||||
menu: false,
|
// all optional values from DEFAULT_TRAY_OPTIONS can de defined here
|
||||||
tooltip: 'Vutron App',
|
enabled: true,
|
||||||
margin: {x:0, y:0},
|
trayWindow: false // true, to use a tray floating window attached to top try icon
|
||||||
width: Constants.IS_DEV_ENV ? 800 : 600,
|
|
||||||
height: 650,
|
|
||||||
showAtStartup: false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,13 +31,22 @@ export const createMainWindow = async (): Promise<BrowserWindow> => {
|
|||||||
webPreferences: Constants.DEFAULT_WEB_PREFERENCES,
|
webPreferences: Constants.DEFAULT_WEB_PREFERENCES,
|
||||||
frame: true
|
frame: true
|
||||||
}
|
}
|
||||||
if (options.trayWindow){
|
const trayOptions: TrayOptions = (options.tray?.enabled) ? {
|
||||||
|
...Constants.DEFAULT_TRAY_OPTIONS,
|
||||||
|
...options.tray
|
||||||
|
}: {
|
||||||
|
...Constants.DEFAULT_TRAY_OPTIONS,
|
||||||
|
enabled: false
|
||||||
|
}
|
||||||
|
|
||||||
|
// trayWindow requires tray.enabled=true
|
||||||
|
if (trayOptions.enabled && trayOptions.trayWindow){
|
||||||
opt = {
|
opt = {
|
||||||
...opt,
|
...opt,
|
||||||
width: options.trayOptions.width ,
|
width: options.width ,
|
||||||
height: options.trayOptions.height,
|
height: options.height,
|
||||||
maxWidth: options.trayOptions.width,
|
maxWidth: options.width,
|
||||||
maxHeight: options.trayOptions.height,
|
maxHeight: options.height,
|
||||||
show: false,
|
show: false,
|
||||||
frame: false,
|
frame: false,
|
||||||
fullscreenable: false,
|
fullscreenable: false,
|
||||||
@@ -71,13 +75,13 @@ export const createMainWindow = async (): Promise<BrowserWindow> => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (options.tray) {
|
if (trayOptions.enabled) {
|
||||||
createTray(mainWindow, options.trayWindow, options.trayOptions);
|
createTray(mainWindow, trayOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.trayWindow) {
|
if (trayOptions.enabled && trayOptions.trayWindow) {
|
||||||
setWindowAutoHide(mainWindow);
|
hideWindow(mainWindow);
|
||||||
if (options.trayOptions?.showAtStartup){
|
if (trayOptions.showAtStartup){
|
||||||
showWindow(mainWindow)
|
showWindow(mainWindow)
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
import { screen } from 'electron'
|
|
||||||
|
|
||||||
// To detect proper desktop/workspace, find the display where the mouse cursor is
|
|
||||||
export function getDisplayNearestPoint(){
|
|
||||||
return screen.getDisplayNearestPoint(screen.getCursorScreenPoint());
|
|
||||||
}
|
|
||||||
|
|
||||||
export function moveToCurrentDesktop(mainWindow){
|
|
||||||
const currentDisplay = getDisplayNearestPoint();
|
|
||||||
// Center window relatively to that display
|
|
||||||
const centerX = Math.round((currentDisplay.workArea.width - mainWindow.getBounds().width) / 2);
|
|
||||||
const centerY = Math.round((currentDisplay.workArea.height - mainWindow.getBounds().height) / 2);
|
|
||||||
mainWindow.setPosition(currentDisplay.workArea.x + centerX, currentDisplay.workArea.y + centerY);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
+22
-36
@@ -1,29 +1,23 @@
|
|||||||
import { app, screen, Menu, Tray, BaseWindow, BrowserWindow } from 'electron'
|
import { app, screen, Menu, Tray, BrowserWindow } from 'electron'
|
||||||
import Constants from './utils/Constants.ts'
|
import Constants from './utils/Constants.ts'
|
||||||
import { getDisplayNearestPoint } from './desktop.ts'
|
|
||||||
|
|
||||||
let tray;
|
let tray;
|
||||||
let trayOptions;
|
let trayOptions;
|
||||||
const DEFAULT_OPTIONS = {
|
|
||||||
menu: false,
|
|
||||||
tooltip: 'Vutron App',
|
|
||||||
margin: {x:0, y:0},
|
|
||||||
width: Constants.IS_DEV_ENV ? 1500 : 1200,
|
|
||||||
height: 650,
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createTray(window: BaseWindow, trayWindow: boolean, options) {
|
export function createTray(window: BrowserWindow, options) {
|
||||||
trayOptions = options || DEFAULT_OPTIONS;
|
trayOptions = options || Constants.DEFAULT_TRAY_OPTIONS;
|
||||||
trayOptions.trayWindow=trayWindow;
|
|
||||||
tray = new Tray('buildAssets/icons/icon16.png');
|
tray = new Tray('buildAssets/icons/icon16.png');
|
||||||
|
|
||||||
// tray.on('double-click', function (event) {
|
tray.on('double-click', function (event) {
|
||||||
// toggleWindow(window)
|
console.log('double-click')
|
||||||
// });
|
toggleWindow(window)
|
||||||
|
});
|
||||||
tray.on('right-click', function (event) {
|
tray.on('right-click', function (event) {
|
||||||
|
console.log('right-click')
|
||||||
toggleWindow(window)
|
toggleWindow(window)
|
||||||
});
|
});
|
||||||
tray.on('click', function (event) {
|
tray.on('click', function (event) {
|
||||||
|
console.log('click')
|
||||||
toggleWindow(window)
|
toggleWindow(window)
|
||||||
});
|
});
|
||||||
tray.setToolTip(trayOptions.tooltip);
|
tray.setToolTip(trayOptions.tooltip);
|
||||||
@@ -60,34 +54,33 @@ export function createTray(window: BaseWindow, trayWindow: boolean, options) {
|
|||||||
return tray
|
return tray
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setWindowAutoHide(window: BrowserWindow) {
|
export function hideWindow(window: BrowserWindow) {
|
||||||
|
console.log('hide window');
|
||||||
window.hide();
|
window.hide();
|
||||||
window.on("blur", () => {
|
window.on("blur", () => {
|
||||||
// dont close if devtools
|
// dont close if devtools
|
||||||
if (!window.webContents.isDevToolsOpened()) {
|
if (!window.webContents.isDevToolsOpened()) {
|
||||||
window.hide();
|
window.hide();
|
||||||
// ipcMain.emit("tray-window-hidden", { window: window, tray: tray });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toggleWindow(window: BaseWindow) {
|
export function toggleWindow(window: BrowserWindow) {
|
||||||
if (window.isVisible()) {
|
if (window.isVisible()) {
|
||||||
window.hide();
|
hideWindow(window);
|
||||||
// ipcMain.emit("tray-window-hidden", { window: window, tray: tray });
|
}else{
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
showWindow(window);
|
showWindow(window);
|
||||||
// ipcMain.emit("tray-window-visible", { window: window, tray: tray });
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function showWindow(window: BaseWindow) {
|
export function showWindow(window: BrowserWindow) {
|
||||||
|
console.log('show window');
|
||||||
window.show()
|
window.show()
|
||||||
|
|
||||||
alignWindow(window)
|
alignWindow(window)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function alignWindow(window: BaseWindow) {
|
export function alignWindow(window: BrowserWindow) {
|
||||||
if (!trayOptions.trayWindow) return;
|
if (!trayOptions.trayWindow) return;
|
||||||
|
|
||||||
const position = calculateWindowPosition();
|
const position = calculateWindowPosition();
|
||||||
@@ -101,15 +94,8 @@ export function alignWindow(window: BaseWindow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function calculateWindowPosition() {
|
function calculateWindowPosition() {
|
||||||
// // To detect proper desktop/workspace, find the display where the mouse cursor is
|
const margin = trayOptions.margin;
|
||||||
// const currentDisplay = getDisplayNearestPoint();
|
const b = trayOptions;
|
||||||
// console.log("currentDisplay.workArea=", currentDisplay.workArea);
|
|
||||||
|
|
||||||
const margin = {
|
|
||||||
x: trayOptions.margin.x, // + currentDisplay.workArea.width,
|
|
||||||
y: trayOptions.margin.y // + currentDisplay.workArea.height
|
|
||||||
};
|
|
||||||
const b = trayOptions
|
|
||||||
|
|
||||||
const screenBounds = screen.getPrimaryDisplay().size;
|
const screenBounds = screen.getPrimaryDisplay().size;
|
||||||
const trayBounds = tray.getBounds();
|
const trayBounds = tray.getBounds();
|
||||||
|
|||||||
@@ -4,6 +4,15 @@ import { fileURLToPath } from 'url'
|
|||||||
|
|
||||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||||
|
|
||||||
|
export interface TrayOptions {
|
||||||
|
enabled: boolean,
|
||||||
|
trayWindow: boolean,
|
||||||
|
menu: boolean,
|
||||||
|
tooltip: string,
|
||||||
|
margin: {x: number, y: number},
|
||||||
|
showAtStartup: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export default class Constants {
|
export default class Constants {
|
||||||
// Display app name (uppercase first letter)
|
// Display app name (uppercase first letter)
|
||||||
static APP_NAME = name.charAt(0).toUpperCase() + name.slice(1)
|
static APP_NAME = name.charAt(0).toUpperCase() + name.slice(1)
|
||||||
@@ -25,6 +34,17 @@ export default class Constants {
|
|||||||
preload: join(__dirname, '../preload/index.js')
|
preload: join(__dirname, '../preload/index.js')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static DEFAULT_TRAY_OPTIONS : TrayOptions = {
|
||||||
|
enabled: false,
|
||||||
|
trayWindow: false,
|
||||||
|
menu: false,
|
||||||
|
tooltip: 'Vutron App',
|
||||||
|
margin: {x:0, y:0},
|
||||||
|
showAtStartup: false
|
||||||
|
}
|
||||||
|
|
||||||
static APP_INDEX_URL_DEV = `${debug.env.VITE_DEV_SERVER_URL}/index.html`
|
static APP_INDEX_URL_DEV = `${debug.env.VITE_DEV_SERVER_URL}/index.html`
|
||||||
static APP_INDEX_URL_PROD = join(__dirname, '../index.html')
|
static APP_INDEX_URL_PROD = join(__dirname, '../index.html')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user