mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-21 11:26:39 +00:00
electron: assign ids to windows
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user