2026-09-22 19:26:35 +00:00
|
|
|
import {copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync} from "node:fs"
|
2026-09-14 17:58:14 +00:00
|
|
|
import {join} from "node:path"
|
2026-09-16 16:59:22 +00:00
|
|
|
import {app, Menu, Tray} from "electron"
|
2026-09-22 19:26:35 +00:00
|
|
|
import {autoUpdater} from "electron-updater"
|
2026-09-07 17:42:52 +00:00
|
|
|
import {createCapacitorElectronApp} from "@capawesome/capacitor-electron"
|
|
|
|
|
|
2026-09-16 16:59:22 +00:00
|
|
|
const {appId, appName} = JSON.parse(
|
|
|
|
|
readFileSync(join(app.getAppPath(), "generated/capacitor.config.json"), "utf8"),
|
|
|
|
|
)
|
2026-09-22 19:26:35 +00:00
|
|
|
const iconPath = join(
|
|
|
|
|
app.getAppPath(),
|
|
|
|
|
app.isPackaged ? "generated/icon.png" : "app/pwa-192x192.png",
|
|
|
|
|
)
|
2026-09-16 16:59:22 +00:00
|
|
|
let tray: Tray | undefined
|
|
|
|
|
let quitting = false
|
|
|
|
|
|
2026-09-22 19:26:35 +00:00
|
|
|
app.whenReady().then(() => {
|
|
|
|
|
if (app.isPackaged) {
|
|
|
|
|
void autoUpdater
|
|
|
|
|
.checkForUpdates()
|
|
|
|
|
.then(result => result?.downloadPromise)
|
|
|
|
|
.catch(error => console.error("Update check failed", error))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-09-16 16:59:22 +00:00
|
|
|
const destroyTray = () => {
|
|
|
|
|
tray?.destroy()
|
|
|
|
|
tray = undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.on("before-quit", () => {
|
|
|
|
|
quitting = true
|
|
|
|
|
destroyTray()
|
|
|
|
|
})
|
|
|
|
|
|
2026-09-07 17:42:52 +00:00
|
|
|
createCapacitorElectronApp({
|
|
|
|
|
window: {width: 1200, height: 800},
|
2026-09-14 17:58:14 +00:00
|
|
|
hooks: {
|
2026-09-16 16:59:22 +00:00
|
|
|
beforeReady: () => {
|
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
|
app.setAppUserModelId(appId)
|
2026-09-22 19:26:35 +00:00
|
|
|
} else if (process.platform === "linux" && app.isPackaged) {
|
|
|
|
|
app.setDesktopName(`${appId}.desktop`)
|
|
|
|
|
try {
|
|
|
|
|
const data = process.env.XDG_DATA_HOME || join(app.getPath("home"), ".local/share")
|
|
|
|
|
const applications = join(data, "applications")
|
|
|
|
|
const entry = join(applications, `${appId}.desktop`)
|
|
|
|
|
const marker = "X-Electron-Generated=true"
|
|
|
|
|
const installed = (process.env.XDG_DATA_DIRS || "/usr/local/share:/usr/share")
|
|
|
|
|
.split(":")
|
|
|
|
|
.some(directory => existsSync(join(directory, "applications", `${appId}.desktop`)))
|
|
|
|
|
if (!installed && (!existsSync(entry) || readFileSync(entry, "utf8").includes(marker))) {
|
|
|
|
|
const icons = join(data, "icons")
|
|
|
|
|
const icon = join(icons, `${appId}.png`)
|
|
|
|
|
mkdirSync(applications, {recursive: true})
|
|
|
|
|
mkdirSync(icons, {recursive: true})
|
|
|
|
|
copyFileSync(iconPath, icon)
|
|
|
|
|
const escape = (value: string) =>
|
|
|
|
|
value.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/\r/g, "\\r")
|
|
|
|
|
const writeEntry = (executable: string) => {
|
|
|
|
|
const command = `"${executable.replace(/[\\"`$]/g, "\\$&").replace(/%/g, "%%")}"`
|
|
|
|
|
// Wayland shells resolve icons through desktop entries, not BrowserWindow.setIcon.
|
|
|
|
|
writeFileSync(
|
|
|
|
|
entry,
|
|
|
|
|
[
|
|
|
|
|
"[Desktop Entry]",
|
|
|
|
|
"Type=Application",
|
|
|
|
|
`Name=${escape(appName)}`,
|
|
|
|
|
`Exec=${escape(command)}`,
|
|
|
|
|
`Icon=${escape(icon)}`,
|
|
|
|
|
`StartupWMClass=${appId}`,
|
|
|
|
|
"NoDisplay=true",
|
|
|
|
|
marker,
|
|
|
|
|
"",
|
|
|
|
|
].join("\n"),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
writeEntry(process.env.APPIMAGE || process.execPath)
|
|
|
|
|
autoUpdater.on("appimage-filename-updated", (destination: string) => {
|
|
|
|
|
try {
|
|
|
|
|
writeEntry(destination)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Could not update the desktop launcher", error)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Could not register the desktop icon", error)
|
|
|
|
|
}
|
2026-09-16 16:59:22 +00:00
|
|
|
}
|
|
|
|
|
},
|
2026-09-14 17:58:14 +00:00
|
|
|
onWindowCreated: window => {
|
2026-09-16 16:59:22 +00:00
|
|
|
const show = () => {
|
|
|
|
|
if (window.isMinimized()) {
|
|
|
|
|
window.restore()
|
|
|
|
|
}
|
|
|
|
|
window.show()
|
|
|
|
|
window.focus()
|
|
|
|
|
}
|
|
|
|
|
if (process.platform === "darwin") {
|
|
|
|
|
window.on("close", event => {
|
|
|
|
|
if (!quitting) {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
window.hide()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
app.on("activate", show)
|
|
|
|
|
window.on("closed", () => app.removeListener("activate", show))
|
|
|
|
|
}
|
|
|
|
|
const icon =
|
|
|
|
|
process.platform === "darwin"
|
|
|
|
|
? "trayTemplate.png"
|
|
|
|
|
: process.platform === "win32"
|
|
|
|
|
? "icon.ico"
|
|
|
|
|
: "icon.png"
|
2026-09-22 19:26:35 +00:00
|
|
|
tray = new Tray(app.isPackaged ? join(app.getAppPath(), "generated", icon) : iconPath)
|
2026-09-16 16:59:22 +00:00
|
|
|
tray.setToolTip(appName)
|
|
|
|
|
tray.setContextMenu(
|
|
|
|
|
Menu.buildFromTemplate([
|
|
|
|
|
{label: "Show", click: show},
|
|
|
|
|
{label: "Quit", click: () => app.quit()},
|
|
|
|
|
]),
|
|
|
|
|
)
|
|
|
|
|
tray.on("click", show)
|
|
|
|
|
window.on("closed", destroyTray)
|
2026-09-22 19:26:35 +00:00
|
|
|
if (process.platform === "darwin") {
|
|
|
|
|
app.dock?.setIcon(iconPath)
|
|
|
|
|
} else if (process.platform === "linux") {
|
|
|
|
|
window.setIcon(iconPath)
|
2026-09-14 17:58:14 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-09-11 11:53:30 +00:00
|
|
|
csp: {
|
|
|
|
|
// SvelteKit's meta CSP owns scripts; the platform default would block its hashed bootstrap.
|
|
|
|
|
policy: "base-uri 'self'; object-src 'none'",
|
2026-09-24 19:58:16 +00:00
|
|
|
// Capawesome 0.1.1's dev policy, plus Flotilla's analytics origin, blob workers and map tiles.
|
2026-09-11 11:53:30 +00:00
|
|
|
devPolicy: [
|
|
|
|
|
"default-src 'self'",
|
|
|
|
|
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://plausible.coracle.social",
|
|
|
|
|
"style-src 'self' 'unsafe-inline'",
|
2026-09-28 22:27:49 +00:00
|
|
|
"img-src 'self' data: blob: https://basemaps.cartocdn.com",
|
2026-09-11 11:53:30 +00:00
|
|
|
"font-src 'self' data:",
|
|
|
|
|
"media-src 'self' blob:",
|
|
|
|
|
"connect-src 'self' https: wss: ws: http:",
|
|
|
|
|
"worker-src 'self' blob:",
|
|
|
|
|
"object-src 'none'",
|
|
|
|
|
"base-uri 'self'",
|
|
|
|
|
].join("; "),
|
|
|
|
|
},
|
2026-09-07 17:42:52 +00:00
|
|
|
})
|