This commit is contained in:
2026-10-17 18:25:04 +07:00
parent 701bca234e
commit d3683ac6cf
15 changed files with 124 additions and 691 deletions
-51
View File
@@ -1,11 +1,9 @@
import { app, BrowserWindow, ipcMain, protocol, screen, session } from 'electron'
import { createReadStream } from 'node:fs'
import { stat } from 'node:fs/promises'
import { networkInterfaces } from 'node:os'
import { join } from 'node:path'
import { Readable } from 'node:stream'
import { Controller } from './controller.js'
import { LanServer } from './lan.js'
import { parseRange } from './range.js'
import type { HostInfo } from '../src/types/screen.js'
@@ -18,20 +16,12 @@ protocol.registerSchemesAsPrivileged([
])
let control: BrowserWindow | null = null
let output: BrowserWindow | null = null
let lan: LanServer | null = null
let selectedDisplay: number | null = null
const controller = new Controller((state) => {
for (const window of [control, output])
if (window && !window.isDestroyed()) window.webContents.send('state', state)
lan?.broadcast(state)
})
const rendererRoot = () => join(app.getAppPath(), 'dist')
function addresses() {
return Object.values(networkInterfaces())
.flatMap((items) => items ?? [])
.filter((item) => !item.internal && item.family === 'IPv4')
.map((item) => ({ address: item.address, family: item.family }))
}
function host(): HostInfo {
const primary = screen.getPrimaryDisplay().id
return {
@@ -42,12 +32,6 @@ function host(): HostInfo {
id: d.id,
label: `${d.label || 'Дисплей'} — ${d.size.width}×${d.size.height}`,
})),
lan: addresses(),
url: lan?.url ?? '',
code: lan?.code ?? '',
pending: [...(lan?.pending.entries() ?? [])]
.filter(([, p]) => !p.approved && p.expires > Date.now())
.map(([id]) => id),
}
}
function windowOptions() {
@@ -207,38 +191,6 @@ app
console.error('Ошибка отчёта проигрывателя', error)
}
})
ipcMain.handle('lan', async (event, address: unknown) => {
requireControl(event)
if (
address !== null &&
(typeof address !== 'string' || !addresses().some((a) => a.address === address))
)
throw new Error('Некорректный сетевой интерфейс')
await lan?.close()
lan = null
if (address !== null) {
const server = new LanServer(
rendererRoot(),
() => controller.state,
(command) => {
if (!output) throw new Error('Окно показа закрыто')
controller.command(command, 'phone')
},
)
try {
await server.start(address)
lan = server
} catch (error) {
await server.close()
throw error
}
}
})
ipcMain.handle('approve', (event, id: unknown) => {
requireControl(event)
if (typeof id !== 'string' || !lan) throw new Error('Пульт отключён')
lan.approve(id)
})
screen.on('display-removed', (_event, display) => {
if (display.id === selectedDisplay) output?.close()
})
@@ -252,6 +204,3 @@ app
app.quit()
})
app.on('window-all-closed', () => app.quit())
app.on('before-quit', () => {
void lan?.close()
})