farmcontrol-ui/public/electron.js
2025-08-23 21:09:03 +01:00

153 lines
3.6 KiB
JavaScript

import { app, BrowserWindow, ipcMain, shell } from 'electron'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
let win
function createWindow() {
win = new BrowserWindow({
width: 1200,
height: 800,
frame: false,
titleBarStyle: 'hiddenInset',
trafficLightPosition: { x: 14, y: 12 },
backgroundColor: '#141414',
icon: path.join(__dirname, './logo512.png'),
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
// For development, load from localhost; for production, load the built index.html
if (process.env.ELECTRON_START_URL) {
win.loadURL(process.env.ELECTRON_START_URL)
} else {
win.loadFile(path.join(__dirname, '../build/index.html'))
}
setupWindowEvents()
}
app.whenReady().then(createWindow)
// IPC handler to get OS
ipcMain.handle('os-info', () => {
return {
platform: process.platform
}
})
// IPC handler to get window state
ipcMain.handle('window-state', () => {
return {
isFullScreen: win ? win.isFullScreen() : false,
isMaximized: win ? win.isMaximized() : false
}
})
// Emit events to renderer when window is maximized/unmaximized
function setupWindowEvents() {
if (!win) return
win.on('maximize', () => {
win.webContents.send('window-state', {
isMaximized: true
})
})
win.on('enter-full-screen', () => {
console.log('Entered fullscreen')
win.webContents.send('window-state', {
isFullScreen: true
})
})
win.on('leave-full-screen', () => {
win.webContents.send('window-state', {
isFullScreen: false
})
})
win.on('unmaximize', () => {
win.webContents.send('window-state', {
isMaximized: false
})
})
}
// IPC handlers for window controls
ipcMain.on('window-control', (event, action) => {
if (!win) return
switch (action) {
case 'minimize':
win.minimize()
break
case 'maximize':
if (win.isMaximized()) {
win.unmaximize()
} else {
win.maximize()
}
break
case 'close':
win.close()
break
default:
break
}
})
// Add this after other ipcMain handlers
ipcMain.handle('open-external-url', (event, url) => {
console.log('Opening external url...')
shell.openExternal(url)
})
app.on('open-url', (event, url) => {
event.preventDefault()
console.log('App opened with URL:', url)
if (url.startsWith('farmcontrol://app')) {
// Extract the path/query after 'farmcontrol://app'
const redirectPath = url.replace('farmcontrol://app', '') || '/'
if (win && win.webContents) {
win.webContents.send('navigate', redirectPath)
}
}
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
const env = process.env.NODE_ENV.trim()
console.log(env)
if (env == 'development') {
console.log('Starting development auth web server...')
import('express').then(({ default: express }) => {
const app = express()
const port = 3500
app.use((req, res) => {
const redirectPath = req.originalUrl
res.send(
`Open Farmcontrol to continue... (Redirect path: ${redirectPath})`
)
if (win && win.webContents) {
win.webContents.send('navigate', redirectPath)
win.show()
win.focus()
}
})
app.listen(port, () => {
console.log(`Dev auth server running on http://localhost:${port}`)
})
})
} else {
console.log('Will use url scheme instead of auth server.')
}