Compare commits

...

1 Commits

Author SHA1 Message Date
Brendan Allan
1f95d8a4ca electron: assign ids to windows 2026-04-13 15:53:07 +08:00
4 changed files with 41 additions and 1 deletions

View File

@@ -4,7 +4,7 @@ import type { IpcMainEvent, IpcMainInvokeEvent } from "electron"
import type { InitStep, ServerReadyData, SqliteMigrationProgress, TitlebarTheme, WslConfig } from "../preload/types"
import { getStore } from "./store"
import { setTitlebar } from "./windows"
import { getWindowId, setTitlebar } from "./windows"
const pickerFilters = (ext?: string[]) => {
if (!ext || ext.length === 0) return undefined
@@ -151,6 +151,12 @@ export function registerIpcHandlers(deps: Deps) {
ipcMain.handle("get-window-count", () => BrowserWindow.getAllWindows().length)
ipcMain.handle("get-window-id", (event: IpcMainInvokeEvent) => {
const win = BrowserWindow.fromWebContents(event.sender)
if (!win) return null
return getWindowId(win) ?? null
})
ipcMain.handle("get-window-focused", (event: IpcMainInvokeEvent) => {
const win = BrowserWindow.fromWebContents(event.sender)
return win?.isFocused() ?? false

View File

@@ -1,3 +1,4 @@
import { randomInt } from "node:crypto"
import windowState from "electron-window-state"
import { app, BrowserWindow, nativeImage, nativeTheme } from "electron"
import { dirname, join } from "node:path"
@@ -12,6 +13,9 @@ type Globals = {
const root = dirname(fileURLToPath(import.meta.url))
let backgroundColor: string | undefined
const ids = new WeakMap<BrowserWindow, string>()
const used = new Set<string>()
let seen = false
export function setBackgroundColor(color: string) {
backgroundColor = color
@@ -21,6 +25,10 @@ export function getBackgroundColor(): string | undefined {
return backgroundColor
}
export function getWindowId(win: BrowserWindow) {
return ids.get(win)
}
function iconsDir() {
return app.isPackaged ? join(process.resourcesPath, "icons") : join(root, "../../resources/icons")
}
@@ -88,6 +96,7 @@ export function createMainWindow(globals: Globals) {
sandbox: false,
},
})
track(win)
state.manage(win)
loadWindow(win, "index.html")
@@ -161,3 +170,26 @@ function wireZoom(win: BrowserWindow) {
win.webContents.setZoomFactor(1)
})
}
function track(win: BrowserWindow) {
const id = nextId()
ids.set(win, id)
win.once("closed", () => {
used.delete(id)
})
}
function nextId() {
if (!seen) {
seen = true
used.add("main")
return "main"
}
while (true) {
const id = String(randomInt(100_000, 1_000_000))
if (used.has(id)) continue
used.add(id)
return id
}
}

View File

@@ -29,6 +29,7 @@ const api: ElectronAPI = {
storeLength: (name) => ipcRenderer.invoke("store-length", name),
getWindowCount: () => ipcRenderer.invoke("get-window-count"),
getWindowId: () => ipcRenderer.invoke("get-window-id"),
onSqliteMigrationProgress: (cb) => {
const handler = (_: unknown, progress: SqliteMigrationProgress) => cb(progress)
ipcRenderer.on("sqlite-migration-progress", handler)

View File

@@ -37,6 +37,7 @@ export type ElectronAPI = {
storeLength: (name: string) => Promise<number>
getWindowCount: () => Promise<number>
getWindowId: () => Promise<string | null>
onSqliteMigrationProgress: (cb: (progress: SqliteMigrationProgress) => void) => () => void
onMenuCommand: (cb: (id: string) => void) => () => void
onDeepLink: (cb: (urls: string[]) => void) => () => void