Whalebird-desktop-client-ma.../main/background.ts

82 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2023-11-01 17:20:27 +01:00
import path from 'path'
2024-09-12 16:50:39 +02:00
import { app, ipcMain, shell, IpcMainInvokeEvent, BrowserWindow, Menu } from 'electron'
2023-11-01 17:20:27 +01:00
import serve from 'electron-serve'
import { createWindow } from './helpers'
2024-09-12 16:50:39 +02:00
import { menu } from './menu'
2024-09-23 12:19:16 +02:00
import SystemFonts from 'system-font-families'
2023-11-01 17:20:27 +01:00
const isProd = process.env.NODE_ENV === 'production'
if (isProd) {
serve({ directory: 'app' })
} else {
app.setPath('userData', `${app.getPath('userData')} (development)`)
}
2024-09-09 18:43:49 +02:00
let main: BrowserWindow = null
2023-11-01 17:20:27 +01:00
;(async () => {
await app.whenReady()
2024-09-12 16:50:39 +02:00
Menu.setApplicationMenu(menu)
2023-11-01 17:20:27 +01:00
const mainWindow = createWindow('main', {
width: 1000,
height: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
webSecurity: false,
preload: path.join(__dirname, 'preload.js')
}
})
2024-09-09 18:43:49 +02:00
main = mainWindow
2023-11-01 17:20:27 +01:00
if (isProd) {
await mainWindow.loadURL('app://./')
} else {
const port = process.argv[2]
await mainWindow.loadURL(`http://localhost:${port}/`)
mainWindow.webContents.openDevTools()
}
})()
app.on('window-all-closed', () => {
app.quit()
})
ipcMain.on('message', async (event, arg) => {
event.reply('message', `${arg} World!`)
})
ipcMain.handle('open-browser', (_event: IpcMainInvokeEvent, url: string) => {
shell.openExternal(url)
})
2024-09-09 18:43:49 +02:00
ipcMain.handle('set-proxy', (_event: IpcMainInvokeEvent, data: any) => {
if (main === null) return
const { mode, protocol, host, port } = data
switch (mode) {
case 'os':
console.log('Using system proxy')
main.webContents.session.setProxy({ mode: 'system' })
break
case 'manual':
console.log(`Using proxy: ${protocol}=${host}:${port}`)
main.webContents.session.setProxy({
proxyRules: `${protocol}=${host}:${port}`
})
break
default:
console.log('No proxy configuration')
main.webContents.session.setProxy({ mode: 'direct' })
break
}
})
2024-09-23 12:19:16 +02:00
ipcMain.handle('list-fonts', async (_event: IpcMainInvokeEvent) => {
const systemFonts = new SystemFonts()
const res = await systemFonts.getFonts()
return Array.from(new Set(res)).sort()
})