From aa08e266ec42200d649bd861ed966e2ffde0ae82 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 18 May 2024 19:20:18 +0800 Subject: [PATCH] feat: open settings page from application menu --- src/main/index.ts | 31 +++++++++++++++++++++- src/main/window.ts | 40 ++++++++--------------------- src/renderer/src/main.tsx | 4 +++ src/renderer/src/pages/settings.tsx | 3 +++ 4 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 src/renderer/src/pages/settings.tsx diff --git a/src/main/index.ts b/src/main/index.ts index 7b01c13a6..9a3397421 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,5 +1,5 @@ import "dotenv/config" -import { app, BrowserWindow, ipcMain } from "electron" +import { app, BrowserWindow, Menu } from "electron" import path from "path" import { electronApp, optimizer } from "@electron-toolkit/utils" import { createWindow } from "./window" @@ -54,3 +54,32 @@ app.on("window-all-closed", () => { // In this file you can include the rest of your app"s specific main process // code. You can also put them in separate files and require them here. + +Menu.setApplicationMenu( + Menu.buildFromTemplate([ + { + role: "appMenu", + submenu: [ + { role: "about" }, + { type: "separator" }, + { + label: "Settings...", + accelerator: "CmdOrCtrl+,", + click: () => { + console.log("Oh, hi there!") + createWindow({ + extraPath: "/settings", + width: 800, + height: 600, + }) + }, + }, + { type: "separator" }, + { role: "hide" }, + { role: "quit" }, + ], + }, + { role: "viewMenu" }, + { role: "windowMenu" }, + ]), +) diff --git a/src/main/window.ts b/src/main/window.ts index 831d061b6..fd860f454 100644 --- a/src/main/window.ts +++ b/src/main/window.ts @@ -1,4 +1,4 @@ -import { app, shell, BrowserWindow, Menu } from "electron" +import { app, shell, BrowserWindow } from "electron" import path from "path" import { is } from "@electron-toolkit/utils" import icon from "../../resources/icon.png?asset" @@ -23,11 +23,15 @@ function handleOpen(url: string) { } } -export function createWindow(): void { +export function createWindow(options?: { + extraPath?: string + width?: number + height?: number +}): void { // Create the browser window. mainWindow = new BrowserWindow({ - width: 1200, - height: 800, + width: options?.width || 1200, + height: options?.height || 900, show: false, autoHideMenuBar: true, ...(process.platform === "linux" ? { icon } : {}), @@ -50,7 +54,9 @@ export function createWindow(): void { // HMR for renderer base on electron-vite cli. // Load the remote URL for development or the local html file for production. if (is.dev && process.env["ELECTRON_RENDERER_URL"]) { - mainWindow.loadURL(process.env["ELECTRON_RENDERER_URL"]) + mainWindow.loadURL( + process.env["ELECTRON_RENDERER_URL"] + (options?.extraPath || ""), + ) } else { mainWindow.loadFile(path.join(__dirname, "../renderer/index.html")) } @@ -93,30 +99,6 @@ export function createWindow(): void { }, ) - Menu.setApplicationMenu( - Menu.buildFromTemplate([ - { - role: "appMenu", - submenu: [ - { role: "about" }, - { type: "separator" }, - { - label: "Settings...", - accelerator: "CmdOrCtrl+,", - click: () => { - console.log("Oh, hi there!") - }, - }, - { type: "separator" }, - { role: "hide" }, - { role: "quit" }, - ], - }, - { role: "viewMenu" }, - { role: "windowMenu" }, - ]), - ) - app.on("second-instance", (_, commandLine) => { if (mainWindow) { if (mainWindow.isMinimized()) mainWindow.restore() diff --git a/src/renderer/src/main.tsx b/src/renderer/src/main.tsx index d23b2e4e0..292b82f61 100644 --- a/src/renderer/src/main.tsx +++ b/src/renderer/src/main.tsx @@ -53,6 +53,10 @@ const router = createBrowserRouter([ path: "redirect", lazy: () => import("./pages/redirect"), }, + { + path: "settings", + lazy: () => import("./pages/settings"), + }, { path: "debug", lazy: () => import("./pages/debug"), diff --git a/src/renderer/src/pages/settings.tsx b/src/renderer/src/pages/settings.tsx new file mode 100644 index 000000000..a102197da --- /dev/null +++ b/src/renderer/src/pages/settings.tsx @@ -0,0 +1,3 @@ +export function Component() { + return <>Settings +}