Files
AI-Grading-Assistant/electron/main/window.ts
T

93 lines
2.7 KiB
TypeScript

import { BrowserWindow, screen } from 'electron'
import * as path from 'path'
let mainWindow: BrowserWindow | null = null
export let baseAppUrl = ''
export function createMainWindow(): BrowserWindow {
const cursor = screen.getCursorScreenPoint()
const displays = screen.getAllDisplays()
const targetDisplay = displays.find((d: Electron.Display) =>
cursor.x >= d.bounds.x && cursor.x <= d.bounds.x + d.bounds.width &&
cursor.y >= d.bounds.y && cursor.y <= d.bounds.y + d.bounds.height
) || displays[0]
mainWindow = new BrowserWindow({
x: Math.round(targetDisplay.bounds.x + targetDisplay.bounds.width / 2 - 300),
y: targetDisplay.bounds.y + 100,
width: 600,
height: 64,
frame: false,
transparent: true,
alwaysOnTop: true,
skipTaskbar: true,
resizable: false,
webPreferences: {
preload: path.join(__dirname, '../preload/preload.js'),
contextIsolation: true,
nodeIntegration: false
}
})
if (process.env.NODE_ENV === 'development' || process.argv.includes('--dev')) {
baseAppUrl = 'http://localhost:5173'
mainWindow.loadURL(baseAppUrl)
} else {
baseAppUrl = `file://${path.join(__dirname, '../../dist/index.html').replace(/\\/g, '/')}`
mainWindow.loadFile(path.join(__dirname, '../../dist/index.html'))
}
mainWindow.on('closed', () => {
mainWindow = null
})
return mainWindow
}
export function getMainWindow(): BrowserWindow | null {
return mainWindow
}
const dialogWindows: Record<string, BrowserWindow | null> = {}
export function openDialogWindow(type: string): BrowserWindow | null {
const existing = dialogWindows[type]
if (existing && !existing.isDestroyed()) {
existing.focus()
return existing
}
const sizes: Record<string, { width: number; height: number }> = {
settings: { width: 520, height: 560 },
history: { width: 800, height: 600 },
templates: { width: 640, height: 520 },
rubric: { width: 520, height: 480 },
result: { width: 440, height: 560 }
}
const size = sizes[type] || { width: 500, height: 500 }
const child = new BrowserWindow({
...size,
show: false,
frame: false,
transparent: true,
resizable: false,
webPreferences: {
preload: path.join(__dirname, '../preload/preload.js'),
contextIsolation: true,
nodeIntegration: false
}
})
if (process.env.NODE_ENV === 'development' || process.argv.includes('--dev')) {
child.loadURL(`${baseAppUrl}?dialog=${type}`)
} else {
child.loadFile(path.join(__dirname, '../../dist/index.html'), { query: { dialog: type } })
}
child.show()
dialogWindows[type] = child
child.on('closed', () => { dialogWindows[type] = null })
return child
}