✨ 增加系统托盘
This commit is contained in:
parent
c9acbfc5f5
commit
b957414e48
@ -1,11 +1,11 @@
|
|||||||
import { app, BrowserWindow, ipcMain, shell } from "electron";
|
import {app, BrowserWindow, ipcMain, Menu, MenuItem, MenuItemConstructorOptions, shell, Tray} from "electron";
|
||||||
import { release } from "node:os";
|
import {release} from "node:os";
|
||||||
import { join } from "node:path";
|
import node_path, {join} from "node:path";
|
||||||
import { initGitHubApi } from "../api/github";
|
import {initGitHubApi} from "../api/github";
|
||||||
import { initConfigApi } from "../api/config";
|
import {initConfigApi} from "../api/config";
|
||||||
import { initProxyApi } from "../api/proxy";
|
import {initProxyApi} from "../api/proxy";
|
||||||
import { initFrpcApi } from "../api/frpc";
|
import {initFrpcApi} from "../api/frpc";
|
||||||
import { initLoggerApi } from "../api/logger";
|
import {initLoggerApi} from "../api/logger";
|
||||||
import {initFileApi} from "../api/file";
|
import {initFileApi} from "../api/file";
|
||||||
// The built directory structure
|
// The built directory structure
|
||||||
//
|
//
|
||||||
@ -40,15 +40,17 @@ if (!app.requestSingleInstanceLock()) {
|
|||||||
// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
|
// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
|
||||||
|
|
||||||
let win: BrowserWindow | null = null;
|
let win: BrowserWindow | null = null;
|
||||||
|
let tray = null;
|
||||||
// Here, you can also use other preload
|
// Here, you can also use other preload
|
||||||
const preload = join(__dirname, "../preload/index.js");
|
const preload = join(__dirname, "../preload/index.js");
|
||||||
const url = process.env.VITE_DEV_SERVER_URL;
|
const url = process.env.VITE_DEV_SERVER_URL;
|
||||||
const indexHtml = join(process.env.DIST, "index.html");
|
const indexHtml = join(process.env.DIST, "index.html");
|
||||||
|
let isQuiting;
|
||||||
|
|
||||||
async function createWindow() {
|
async function createWindow() {
|
||||||
win = new BrowserWindow({
|
win = new BrowserWindow({
|
||||||
title: "Main window",
|
title: "Frpc Desktop",
|
||||||
icon: join(process.env.VITE_PUBLIC, "favicon.ico"),
|
icon: join(process.env.VITE_PUBLIC, "logo/16x16.png"),
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload,
|
preload,
|
||||||
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
|
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
|
||||||
@ -58,7 +60,6 @@ async function createWindow() {
|
|||||||
contextIsolation: false
|
contextIsolation: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (process.env.VITE_DEV_SERVER_URL) {
|
if (process.env.VITE_DEV_SERVER_URL) {
|
||||||
// electron-vite-vue#298
|
// electron-vite-vue#298
|
||||||
win.loadURL(url);
|
win.loadURL(url);
|
||||||
@ -74,22 +75,66 @@ async function createWindow() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Make all links open with the browser, not with the application
|
// Make all links open with the browser, not with the application
|
||||||
win.webContents.setWindowOpenHandler(({ url }) => {
|
win.webContents.setWindowOpenHandler(({url}) => {
|
||||||
if (url.startsWith("https:")) shell.openExternal(url);
|
if (url.startsWith("https:")) shell.openExternal(url);
|
||||||
return { action: "deny" };
|
return {action: "deny"};
|
||||||
});
|
});
|
||||||
|
|
||||||
// 隐藏菜单栏
|
// 隐藏菜单栏
|
||||||
const { Menu } = require("electron");
|
const {Menu} = require("electron");
|
||||||
Menu.setApplicationMenu(null);
|
Menu.setApplicationMenu(null);
|
||||||
// hide menu for Mac
|
// hide menu for Mac
|
||||||
if (process.platform !== "darwin") {
|
if (process.platform !== "darwin") {
|
||||||
app.dock.hide();
|
app.dock.hide();
|
||||||
}
|
}
|
||||||
// win.webContents.on('will-navigate', (event, url) => { }) #344
|
|
||||||
|
win.on('minimize', function (event) {
|
||||||
|
event.preventDefault();
|
||||||
|
win.hide();
|
||||||
|
});
|
||||||
|
|
||||||
|
win.on('close', function (event) {
|
||||||
|
if (!isQuiting) {
|
||||||
|
event.preventDefault();
|
||||||
|
win.hide();
|
||||||
|
if (process.platform === "darwin") {
|
||||||
|
app.dock.hide();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.whenReady().then(createWindow);
|
export const createTray = () => {
|
||||||
|
let menu: Array<(MenuItemConstructorOptions) | (MenuItem)> = [
|
||||||
|
{
|
||||||
|
label: '显示主窗口', click: function () {
|
||||||
|
win.show();
|
||||||
|
if (process.platform === "darwin") {
|
||||||
|
app.dock.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: '退出',
|
||||||
|
click: () => {
|
||||||
|
isQuiting = true;
|
||||||
|
app.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
tray = new Tray(node_path.join(process.env.VITE_PUBLIC, "logo/16x16.png"))
|
||||||
|
tray.setToolTip('Frpc Desktop')
|
||||||
|
const contextMenu = Menu.buildFromTemplate(menu)
|
||||||
|
tray.setContextMenu(contextMenu)
|
||||||
|
}
|
||||||
|
|
||||||
|
app.whenReady().then(() => {
|
||||||
|
createWindow().then(r => {
|
||||||
|
createTray()
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
app.on("window-all-closed", () => {
|
app.on("window-all-closed", () => {
|
||||||
win = null;
|
win = null;
|
||||||
@ -113,6 +158,10 @@ app.on("activate", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.on('before-quit', () => {
|
||||||
|
isQuiting = true;
|
||||||
|
})
|
||||||
|
|
||||||
// New window example arg: new windows url
|
// New window example arg: new windows url
|
||||||
ipcMain.handle("open-win", (_, arg) => {
|
ipcMain.handle("open-win", (_, arg) => {
|
||||||
const childWindow = new BrowserWindow({
|
const childWindow = new BrowserWindow({
|
||||||
@ -126,12 +175,13 @@ ipcMain.handle("open-win", (_, arg) => {
|
|||||||
if (process.env.VITE_DEV_SERVER_URL) {
|
if (process.env.VITE_DEV_SERVER_URL) {
|
||||||
childWindow.loadURL(`${url}#${arg}`);
|
childWindow.loadURL(`${url}#${arg}`);
|
||||||
} else {
|
} else {
|
||||||
childWindow.loadFile(indexHtml, { hash: arg });
|
childWindow.loadFile(indexHtml, {hash: arg});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.on('open-url', (event, url) => {
|
ipcMain.on('open-url', (event, url) => {
|
||||||
shell.openExternal(url).then(r => {});
|
shell.openExternal(url).then(r => {
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
initGitHubApi();
|
initGitHubApi();
|
||||||
|
Loading…
Reference in New Issue
Block a user