up
This commit is contained in:
+73
-4
@@ -8,6 +8,7 @@ import {
|
||||
screen,
|
||||
session,
|
||||
safeStorage,
|
||||
shell,
|
||||
powerMonitor,
|
||||
powerSaveBlocker,
|
||||
} from 'electron'
|
||||
@@ -43,6 +44,7 @@ let closingWindow: BrowserWindow | null = null
|
||||
let openingOutput = false
|
||||
let outputEpoch = 0
|
||||
let quitting = false
|
||||
let startup: BrowserWindow | null = null
|
||||
let selectedDisplay: number | null = null
|
||||
let online: Online | null = null
|
||||
let settings: WindowSettings
|
||||
@@ -269,13 +271,40 @@ async function show(displayId: number) {
|
||||
// Две копии приложения не должны одновременно менять указатели пакетов.
|
||||
if (!app.requestSingleInstanceLock()) app.exit(0)
|
||||
app.on('second-instance', () => {
|
||||
control?.show()
|
||||
control?.focus()
|
||||
const window = control ?? startup
|
||||
window?.show()
|
||||
window?.focus()
|
||||
})
|
||||
app.on('before-quit', () => {
|
||||
quitting = true
|
||||
})
|
||||
|
||||
app
|
||||
.whenReady()
|
||||
.then(async () => {
|
||||
startup = new BrowserWindow({
|
||||
width: 440,
|
||||
height: 200,
|
||||
resizable: false,
|
||||
title: 'Проверка файлов',
|
||||
webPreferences: { contextIsolation: true, sandbox: true, nodeIntegration: false },
|
||||
})
|
||||
const startupWindow = startup
|
||||
startupWindow.setMenu(null)
|
||||
startupWindow.webContents.setWindowOpenHandler(() => ({ action: 'deny' }))
|
||||
startupWindow.webContents.on('will-navigate', (event) => event.preventDefault())
|
||||
startupWindow.on('closed', () => {
|
||||
if (startup === startupWindow) {
|
||||
startup = null
|
||||
app.quit()
|
||||
}
|
||||
})
|
||||
await startupWindow.loadURL(
|
||||
'data:text/html;charset=utf-8,' +
|
||||
encodeURIComponent(
|
||||
`<!doctype html><html lang="ru"><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'"><title>Проверка файлов</title><style>body{background:#18181c;color:#eee;font:16px system-ui;padding:24px}progress{width:100%;accent-color:#63e2b7}p{margin:12px 0}</style><p id="message" role="status">Проверка загруженных файлов…</p><progress id="progress"></progress><p id="count">Поиск и проверка пакетов…</p></html>`,
|
||||
),
|
||||
)
|
||||
diagnostics = new DiagnosticLog(join(app.getPath('userData'), 'logs'))
|
||||
diagnostics.record('app-start', {
|
||||
version: app.getVersion(),
|
||||
@@ -295,7 +324,29 @@ app
|
||||
const packages = new PackageStore(join(app.getPath('userData'), 'packages'), {
|
||||
fetch: fetchNetwork,
|
||||
})
|
||||
await packages.initialize()
|
||||
let lastProgressUpdate = 0
|
||||
await packages.initialize((progress) => {
|
||||
if (startupWindow.isDestroyed()) return
|
||||
const now = Date.now()
|
||||
if (
|
||||
progress.total !== null &&
|
||||
progress.completed !== progress.total &&
|
||||
now - lastProgressUpdate < 100
|
||||
)
|
||||
return
|
||||
lastProgressUpdate = now
|
||||
const { completed, total } = progress
|
||||
void startupWindow.webContents
|
||||
.executeJavaScript(
|
||||
`(() => {
|
||||
const bar = document.getElementById('progress');
|
||||
const count = document.getElementById('count');
|
||||
${total === null ? "bar.removeAttribute('value');" : `bar.max = ${Math.max(total, 1)}; bar.value = ${completed}; count.textContent = 'Проверено файлов: ${completed} из ${total}';`}
|
||||
})()`,
|
||||
)
|
||||
.catch(() => {})
|
||||
})
|
||||
if (quitting || startupWindow.isDestroyed()) return
|
||||
online = new Online(
|
||||
join(app.getPath('userData'), 'connection'),
|
||||
packages,
|
||||
@@ -324,6 +375,7 @@ app
|
||||
},
|
||||
)
|
||||
await online.initialize()
|
||||
if (quitting || startupWindow.isDestroyed()) return
|
||||
let previousConnection = online.state.connection
|
||||
const logConnection = () => {
|
||||
const status = online!.state.connection
|
||||
@@ -425,6 +477,8 @@ app
|
||||
keepControlOnPrimary()
|
||||
if (settings.value.control?.maximized) control!.maximize()
|
||||
control!.show()
|
||||
startup = null
|
||||
if (!startupWindow.isDestroyed()) startupWindow.destroy()
|
||||
})
|
||||
const scheduleSave = () => {
|
||||
clearTimeout(saveTimer)
|
||||
@@ -503,6 +557,15 @@ app
|
||||
selectingPackage = false
|
||||
}
|
||||
})
|
||||
ipcMain.handle('open-materials-folder', async (event, key: unknown) => {
|
||||
requireControl(event)
|
||||
if (typeof key !== 'string' || key !== controller.state.catalog.key)
|
||||
throw new Error('Выберите текущую игру')
|
||||
for (const path of await packages.folders(key)) {
|
||||
const error = await shell.openPath(path)
|
||||
if (error) throw new Error('Не удалось открыть папку материалов')
|
||||
}
|
||||
})
|
||||
let localSession = ''
|
||||
let localSequence = 0
|
||||
const pendingCommands = new Set<string>()
|
||||
@@ -637,6 +700,12 @@ app
|
||||
.catch(() => {
|
||||
diagnostics?.record('startup-error')
|
||||
console.error('SCREEN_STARTUP_FAILED')
|
||||
app.quit()
|
||||
if (startup && !startup.isDestroyed()) {
|
||||
void startup.webContents
|
||||
.executeJavaScript(
|
||||
`document.getElementById('message').textContent = 'Не удалось проверить файлы. Проверьте доступ к папке данных приложения и перезапустите его.'; document.getElementById('progress').hidden = true; document.getElementById('count').textContent = '';`,
|
||||
)
|
||||
.catch(() => app.quit())
|
||||
} else app.quit()
|
||||
})
|
||||
app.on('window-all-closed', () => app.quit())
|
||||
|
||||
Reference in New Issue
Block a user