1- /**
2- * Electron Main Process
3- *
4- * Handles window creation, IPC communication, and app lifecycle.
5- */
6-
71import { app , BrowserWindow , ipcMain } from 'electron' ;
82import path from 'path' ;
93import url from 'url' ;
@@ -13,25 +7,16 @@ import { fileURLToPath } from 'url';
137const __filename = fileURLToPath ( import . meta. url ) ;
148const __dirname = path . dirname ( __filename ) ;
159
16- // Services
1710import ConnectionManager from './services/ConnectionManager.js' ;
1811import VersionChecker from './services/VersionChecker.js' ;
1912import MDNSDiscovery from './services/MDNSDiscovery.js' ;
2013
21- // Determine if running in development mode
2214const isDev = process . argv . includes ( '--dev' ) || process . env . NODE_ENV === 'development' ;
2315
24- // Keep a global reference of the window object
25- let mainWindow ;
26-
27- // Initialize services
28- let connectionManager ;
16+ let connectionManager : ConnectionManager ;
2917
30- /**
31- * Create the main browser window
32- */
33- function createWindow ( ) {
34- mainWindow = new BrowserWindow ( {
18+ function createWindow ( ) : void {
19+ const win = new BrowserWindow ( {
3520 width : 1200 ,
3621 height : 800 ,
3722 minWidth : 800 ,
@@ -42,24 +27,18 @@ function createWindow() {
4227 contextIsolation : true ,
4328 sandbox : true ,
4429 } ,
45- show : false , // Don't show until ready
30+ show : false ,
4631 } ) ;
4732
48- // Load the appropriate URL
4933 if ( isDev ) {
50- // Development: Load from Vite dev server
51- mainWindow . loadURL ( 'http://localhost:5173' ) ;
52-
53- // Open DevTools in development
54- mainWindow . webContents . openDevTools ( ) ;
34+ win . loadURL ( 'http://localhost:5173' ) ;
35+ win . webContents . openDevTools ( ) ;
5536 } else {
56- // Production: Load built files from client/dist-electron (Electron-specific build)
57- // In packaged app, extraResource files are in process.resourcesPath
5837 const staticPath = app . isPackaged
5938 ? path . join ( process . resourcesPath , 'dist-electron' )
6039 : path . join ( __dirname , '../client/dist-electron' ) ;
6140
62- mainWindow . loadURL (
41+ win . loadURL (
6342 url . format ( {
6443 pathname : path . join ( staticPath , 'index.html' ) ,
6544 protocol : 'file:' ,
@@ -68,29 +47,19 @@ function createWindow() {
6847 ) ;
6948 }
7049
71- // Show window when ready to avoid flicker
72- mainWindow . once ( 'ready-to-show' , ( ) => {
73- mainWindow . show ( ) ;
50+ win . once ( 'ready-to-show' , ( ) => {
51+ win . show ( ) ;
7452 } ) ;
7553
7654 // Enable DevTools with keyboard shortcut (Cmd/Ctrl+Shift+I) in production
77- mainWindow . webContents . on ( 'before-input-event' , ( event , input ) => {
55+ win . webContents . on ( 'before-input-event' , ( event , input ) => {
7856 if ( input . key === 'I' && input . shift && ( input . meta || input . control ) ) {
79- mainWindow . webContents . toggleDevTools ( ) ;
57+ win . webContents . toggleDevTools ( ) ;
8058 }
8159 } ) ;
82-
83- // Emitted when the window is closed
84- mainWindow . on ( 'closed' , ( ) => {
85- mainWindow = null ;
86- } ) ;
8760}
8861
89- /**
90- * Register IPC handlers for renderer communication
91- */
92- function registerIPCHandlers ( ) {
93- // Connection management
62+ function registerIPCHandlers ( ) : void {
9463 ipcMain . handle ( 'connections:getAll' , async ( ) => {
9564 return connectionManager . getAllConnections ( ) ;
9665 } ) ;
@@ -119,13 +88,11 @@ function registerIPCHandlers() {
11988 return connectionManager . clearActiveConnection ( ) ;
12089 } ) ;
12190
122- // Server URL synchronous getter (for platform layer)
12391 ipcMain . on ( 'server:getURLSync' , ( event ) => {
12492 const activeConnection = connectionManager . getActiveConnection ( ) ;
12593 event . returnValue = activeConnection ? activeConnection . url : null ;
12694 } ) ;
12795
128- // Storage operations (for platform layer)
12996 ipcMain . handle ( 'storage:get' , async ( event , key ) => {
13097 return connectionManager . getStorageItem ( key ) ;
13198 } ) ;
@@ -142,18 +109,15 @@ function registerIPCHandlers() {
142109 return connectionManager . clearStorage ( ) ;
143110 } ) ;
144111
145- // App information
146112 ipcMain . handle ( 'app:getVersion' , async ( ) => {
147113 return app . getVersion ( ) ;
148114 } ) ;
149115
150- // Version checking
151116 ipcMain . handle ( 'version:check' , async ( event , serverUrl ) => {
152117 const clientVersion = app . getVersion ( ) ;
153118 return VersionChecker . checkVersion ( serverUrl , clientVersion ) ;
154119 } ) ;
155120
156- // mDNS discovery
157121 ipcMain . handle ( 'mdns:discover' , async ( event , timeout = 5000 ) => {
158122 return MDNSDiscovery . discoverServers ( timeout ) ;
159123 } ) ;
@@ -164,33 +128,24 @@ function registerIPCHandlers() {
164128 } ) ;
165129}
166130
167- // App lifecycle events
168131app . whenReady ( ) . then ( ( ) => {
169- // Initialize services
170132 connectionManager = new ConnectionManager ( ) ;
171-
172- // Register IPC handlers
173133 registerIPCHandlers ( ) ;
174-
175- // Create window
176134 createWindow ( ) ;
177135
178136 app . on ( 'activate' , ( ) => {
179- // On macOS, re-create window when dock icon is clicked and no windows open
180137 if ( BrowserWindow . getAllWindows ( ) . length === 0 ) {
181138 createWindow ( ) ;
182139 }
183140 } ) ;
184141} ) ;
185142
186- // Quit when all windows are closed (except on macOS)
187143app . on ( 'window-all-closed' , ( ) => {
188144 if ( process . platform !== 'darwin' ) {
189145 app . quit ( ) ;
190146 }
191147} ) ;
192148
193- // Handle app errors
194149process . on ( 'uncaughtException' , ( error ) => {
195150 console . error ( 'Uncaught exception:' , error ) ;
196151} ) ;
0 commit comments