style: apply lint and prettier format
This commit is contained in:
1 parent
ceceafae27
commit
13e7117b00
4 files changed
+59
-59
No files matched your search
+16
-9
@@ -1,4 +1,9 @@
|
|||||||
import { app, BrowserWindow, RenderProcessGoneDetails, BrowserWindowConstructorOptions } from 'electron'
|
import {
|
||||||
|
app,
|
||||||
|
BrowserWindow,
|
||||||
|
RenderProcessGoneDetails,
|
||||||
|
BrowserWindowConstructorOptions
|
||||||
|
} from 'electron'
|
||||||
import Constants, { TrayOptions } from './utils/Constants'
|
import Constants, { TrayOptions } from './utils/Constants'
|
||||||
import IPCs from './IPCs'
|
import IPCs from './IPCs'
|
||||||
import { createTray, hideWindow, showWindow } from './tray.ts'
|
import { createTray, hideWindow, showWindow } from './tray.ts'
|
||||||
@@ -32,19 +37,21 @@ export const createMainWindow = async (): Promise<BrowserWindow> => {
|
|||||||
webPreferences: Constants.DEFAULT_WEB_PREFERENCES,
|
webPreferences: Constants.DEFAULT_WEB_PREFERENCES,
|
||||||
frame: true
|
frame: true
|
||||||
}
|
}
|
||||||
const trayOptions: TrayOptions = (options.tray?.enabled) ? {
|
const trayOptions: TrayOptions = options.tray?.enabled
|
||||||
|
? {
|
||||||
...Constants.DEFAULT_TRAY_OPTIONS,
|
...Constants.DEFAULT_TRAY_OPTIONS,
|
||||||
...options.tray
|
...options.tray
|
||||||
}: {
|
}
|
||||||
|
: {
|
||||||
...Constants.DEFAULT_TRAY_OPTIONS,
|
...Constants.DEFAULT_TRAY_OPTIONS,
|
||||||
enabled: false
|
enabled: false
|
||||||
}
|
}
|
||||||
|
|
||||||
// trayWindow requires tray.enabled=true
|
// trayWindow requires tray.enabled=true
|
||||||
if (trayOptions.enabled && trayOptions.trayWindow){
|
if (trayOptions.enabled && trayOptions.trayWindow) {
|
||||||
opt = {
|
opt = {
|
||||||
...opt,
|
...opt,
|
||||||
width: options.width ,
|
width: options.width,
|
||||||
height: options.height,
|
height: options.height,
|
||||||
maxWidth: options.width,
|
maxWidth: options.width,
|
||||||
maxHeight: options.height,
|
maxHeight: options.height,
|
||||||
@@ -77,15 +84,15 @@ export const createMainWindow = async (): Promise<BrowserWindow> => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (trayOptions.enabled) {
|
if (trayOptions.enabled) {
|
||||||
createTray(mainWindow, trayOptions);
|
createTray(mainWindow, trayOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trayOptions.enabled && trayOptions.trayWindow) {
|
if (trayOptions.enabled && trayOptions.trayWindow) {
|
||||||
hideWindow(mainWindow);
|
hideWindow(mainWindow)
|
||||||
if (trayOptions.showAtStartup){
|
if (trayOptions.showAtStartup) {
|
||||||
showWindow(mainWindow)
|
showWindow(mainWindow)
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
mainWindow.once('ready-to-show', (): void => {
|
mainWindow.once('ready-to-show', (): void => {
|
||||||
mainWindow.setAlwaysOnTop(true)
|
mainWindow.setAlwaysOnTop(true)
|
||||||
mainWindow.show()
|
mainWindow.show()
|
||||||
|
|||||||
+28
-31
@@ -2,17 +2,17 @@ import { app, screen, Menu, Tray, BrowserWindow } from 'electron'
|
|||||||
import Constants from './utils/Constants.ts'
|
import Constants from './utils/Constants.ts'
|
||||||
import { debounce } from './utils/Util.ts'
|
import { debounce } from './utils/Util.ts'
|
||||||
let tray
|
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
|
// menu or trayWindow, you need to choose
|
||||||
if (trayOptions.trayWindow){
|
if (trayOptions.trayWindow) {
|
||||||
trayOptions.menu = false
|
trayOptions.menu = false
|
||||||
}
|
}
|
||||||
|
|
||||||
tray = new Tray('buildAssets/icons/icon16.png');
|
tray = new Tray('buildAssets/icons/icon16.png')
|
||||||
tray.setToolTip(trayOptions.tooltip);
|
tray.setToolTip(trayOptions.tooltip)
|
||||||
if (trayOptions.menu) {
|
if (trayOptions.menu) {
|
||||||
const contextMenu = Menu.buildFromTemplate([
|
const contextMenu = Menu.buildFromTemplate([
|
||||||
{
|
{
|
||||||
@@ -39,11 +39,11 @@ export function createTray(window: BrowserWindow, options) {
|
|||||||
} else {
|
} else {
|
||||||
// handle click on tray icon
|
// handle click on tray icon
|
||||||
tray.on('right-click', function (event) {
|
tray.on('right-click', function (event) {
|
||||||
debounce(() => toggleWindow(window));
|
debounce(() => toggleWindow(window))
|
||||||
});
|
})
|
||||||
tray.on('click', function (event) {
|
tray.on('click', function (event) {
|
||||||
debounce(() => toggleWindow(window));
|
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)
|
||||||
@@ -54,22 +54,22 @@ export function createTray(window: BrowserWindow, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function hideWindow(window: BrowserWindow) {
|
export function hideWindow(window: BrowserWindow) {
|
||||||
window.hide();
|
window.hide()
|
||||||
// if (!trayOptions.trayWindow) return;
|
// if (!trayOptions.trayWindow) return;
|
||||||
// hide window when click elsewhere on screen
|
// 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()) {
|
||||||
window.hide();
|
window.hide()
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toggleWindow(window: BrowserWindow) {
|
export function toggleWindow(window: BrowserWindow) {
|
||||||
if (window.isVisible()) {
|
if (window.isVisible()) {
|
||||||
hideWindow(window);
|
hideWindow(window)
|
||||||
}else{
|
} else {
|
||||||
showWindow(window);
|
showWindow(window)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,33 +79,30 @@ export function showWindow(window: BrowserWindow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function alignWindow(window: BrowserWindow) {
|
export function alignWindow(window: BrowserWindow) {
|
||||||
if (!trayOptions.trayWindow) return;
|
if (!trayOptions.trayWindow) return
|
||||||
|
|
||||||
const b = window.getBounds()
|
const b = window.getBounds()
|
||||||
const position = calculateWindowPosition(b);
|
const position = calculateWindowPosition(b)
|
||||||
window.setBounds({
|
window.setBounds({
|
||||||
width: b.width,
|
width: b.width,
|
||||||
height: b.height,
|
height: b.height,
|
||||||
x: position.x,
|
x: position.x,
|
||||||
y: position.y
|
y: position.y
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateWindowPosition(b) {
|
function calculateWindowPosition(b) {
|
||||||
const margin = trayOptions.margin;
|
const margin = trayOptions.margin
|
||||||
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
|
||||||
const x = Math.floor(trayBounds.x - b.width/2 - margin.x + trayBounds.width / 2);
|
const x = Math.floor(trayBounds.x - b.width / 2 - margin.x + trayBounds.width / 2)
|
||||||
const y = bottom ?
|
const y = bottom
|
||||||
Math.floor(trayBounds.y - b.height - margin.y + trayBounds.height / 2)
|
? Math.floor(trayBounds.y - b.height - margin.y + trayBounds.height / 2)
|
||||||
:
|
: Math.floor(trayBounds.y + margin.y + trayBounds.height / 2)
|
||||||
Math.floor(trayBounds.y + margin.y + trayBounds.height / 2)
|
|
||||||
;
|
|
||||||
|
|
||||||
// constraint into screen
|
// constraint into screen
|
||||||
return {
|
return {
|
||||||
x: Math.max(0, Math.min(screenBounds.width - b.width, x)),
|
x: Math.max(0, Math.min(screenBounds.width - b.width, x)),
|
||||||
y: Math.max(0, Math.min(screenBounds.height - b.height, y))
|
y: Math.max(0, Math.min(screenBounds.height - b.height, y))
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
@@ -5,11 +5,11 @@ import { fileURLToPath } from 'url'
|
|||||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||||
|
|
||||||
export interface TrayOptions {
|
export interface TrayOptions {
|
||||||
enabled: boolean,
|
enabled: boolean
|
||||||
trayWindow: boolean,
|
trayWindow: boolean
|
||||||
menu: boolean,
|
menu: boolean
|
||||||
tooltip: string,
|
tooltip: string
|
||||||
margin: {x: number, y: number},
|
margin: { x: number; y: number }
|
||||||
showAtStartup: boolean
|
showAtStartup: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,17 +34,15 @@ export default class Constants {
|
|||||||
preload: join(__dirname, '../preload/index.js')
|
preload: join(__dirname, '../preload/index.js')
|
||||||
}
|
}
|
||||||
|
|
||||||
static DEFAULT_TRAY_OPTIONS : TrayOptions = {
|
static DEFAULT_TRAY_OPTIONS: TrayOptions = {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
trayWindow: false,
|
trayWindow: false,
|
||||||
menu: false,
|
menu: false,
|
||||||
tooltip: 'Vutron App',
|
tooltip: 'Vutron App',
|
||||||
margin: {x:0, y:0},
|
margin: { x: 0, y: 0 },
|
||||||
showAtStartup: false
|
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')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
+8
-10
@@ -1,14 +1,12 @@
|
|||||||
|
export function debounce(func, timeout = 200) {
|
||||||
export function debounce(func, timeout = 200){
|
let timer
|
||||||
let timer;
|
return (function (...args) {
|
||||||
return function(...args) {
|
|
||||||
if (!timer) {
|
if (!timer) {
|
||||||
func.apply(this, args);
|
func.apply(this, args)
|
||||||
}
|
}
|
||||||
clearTimeout(timer);
|
clearTimeout(timer)
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
timer = undefined;
|
timer = undefined
|
||||||
}, timeout);
|
}, timeout)
|
||||||
}();
|
})()
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user