Skip to content

Commit

Permalink
feat: open settings page from application menu
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 18, 2024
1 parent d4ea096 commit aa08e26
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
31 changes: 30 additions & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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" },
]),
)
40 changes: 11 additions & 29 deletions src/main/window.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 } : {}),
Expand All @@ -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"))
}
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/pages/settings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function Component() {
return <>Settings</>
}

0 comments on commit aa08e26

Please sign in to comment.