@@ -83,6 +83,7 @@ const AUTO_UPDATE_STARTUP_DELAY_MS = 15_000;
8383const AUTO_UPDATE_POLL_INTERVAL_MS = 4 * 60 * 60 * 1000 ;
8484const DESKTOP_UPDATE_CHANNEL = "latest" ;
8585const DESKTOP_UPDATE_ALLOW_PRERELEASE = false ;
86+ const DESKTOP_LOOPBACK_HOST = "127.0.0.1" ;
8687
8788type DesktopUpdateErrorContext = DesktopUpdateState [ "errorContext" ] ;
8889type LinuxDesktopNamedApp = Electron . App & {
@@ -142,6 +143,28 @@ function readPersistedBackendObservabilitySettings(): {
142143 }
143144}
144145
146+ function resolveConfiguredDesktopBackendPort ( rawPort : string | undefined ) : number | undefined {
147+ if ( ! rawPort ) {
148+ return undefined ;
149+ }
150+
151+ const parsedPort = Number . parseInt ( rawPort , 10 ) ;
152+ if ( ! Number . isInteger ( parsedPort ) || parsedPort < 1 || parsedPort > 65_535 ) {
153+ return undefined ;
154+ }
155+
156+ return parsedPort ;
157+ }
158+
159+ function resolveDesktopDevServerUrl ( ) : string {
160+ const devServerUrl = process . env . VITE_DEV_SERVER_URL ?. trim ( ) ;
161+ if ( ! devServerUrl ) {
162+ throw new Error ( "VITE_DEV_SERVER_URL is required in desktop development." ) ;
163+ }
164+
165+ return devServerUrl ;
166+ }
167+
145168function backendChildEnv ( ) : NodeJS . ProcessEnv {
146169 const env = { ...process . env } ;
147170 delete env . T3CODE_PORT ;
@@ -199,28 +222,6 @@ function getSafeTheme(rawTheme: unknown): DesktopTheme | null {
199222 return null ;
200223}
201224
202- function resolveDesktopBackendHost ( ) : string {
203- if ( ! isDevelopment ) {
204- return "127.0.0.1" ;
205- }
206-
207- const devServerUrl = process . env . VITE_DEV_SERVER_URL ;
208- if ( ! devServerUrl ) {
209- return "127.0.0.1" ;
210- }
211-
212- try {
213- const hostname = new URL ( devServerUrl ) . hostname . trim ( ) ;
214- if ( hostname === "localhost" || hostname === "127.0.0.1" ) {
215- return hostname ;
216- }
217- } catch {
218- // Fall through to the default loopback host.
219- }
220-
221- return "127.0.0.1" ;
222- }
223-
224225async function waitForBackendHttpReady ( baseUrl : string ) : Promise < void > {
225226 const deadline = Date . now ( ) + 10_000 ;
226227
@@ -588,10 +589,7 @@ function dispatchMenuAction(action: string): void {
588589 const send = ( ) => {
589590 if ( targetWindow . isDestroyed ( ) ) return ;
590591 targetWindow . webContents . send ( MENU_ACTION_CHANNEL , action ) ;
591- if ( ! targetWindow . isVisible ( ) ) {
592- targetWindow . show ( ) ;
593- }
594- targetWindow . focus ( ) ;
592+ revealWindow ( targetWindow ) ;
595593 } ;
596594
597595 if ( targetWindow . webContents . isLoadingMainFrame ( ) ) {
@@ -812,6 +810,26 @@ function clearUpdatePollTimer(): void {
812810 }
813811}
814812
813+ function revealWindow ( window : BrowserWindow ) : void {
814+ if ( window . isDestroyed ( ) ) {
815+ return ;
816+ }
817+
818+ if ( window . isMinimized ( ) ) {
819+ window . restore ( ) ;
820+ }
821+
822+ if ( ! window . isVisible ( ) ) {
823+ window . show ( ) ;
824+ }
825+
826+ if ( process . platform === "darwin" ) {
827+ app . focus ( { steal : true } ) ;
828+ }
829+
830+ window . focus ( ) ;
831+ }
832+
815833function emitUpdateState ( ) : void {
816834 for ( const window of BrowserWindow . getAllWindows ( ) ) {
817835 if ( window . isDestroyed ( ) ) continue ;
@@ -1081,7 +1099,7 @@ function startBackend(): void {
10811099 noBrowser : true ,
10821100 port : backendPort ,
10831101 t3Home : BASE_DIR ,
1084- host : resolveDesktopBackendHost ( ) ,
1102+ host : DESKTOP_LOOPBACK_HOST ,
10851103 desktopBootstrapToken : backendBootstrapToken ,
10861104 ...( backendObservabilitySettings . otlpTracesUrl
10871105 ? { otlpTracesUrl : backendObservabilitySettings . otlpTracesUrl }
@@ -1393,14 +1411,19 @@ function getIconOption(): { icon: string } | Record<string, never> {
13931411 return iconPath ? { icon : iconPath } : { } ;
13941412}
13951413
1414+ function getInitialWindowBackgroundColor ( ) : string {
1415+ return nativeTheme . shouldUseDarkColors ? "#0a0a0a" : "#ffffff" ;
1416+ }
1417+
13961418function createWindow ( ) : BrowserWindow {
13971419 const window = new BrowserWindow ( {
13981420 width : 1100 ,
13991421 height : 780 ,
14001422 minWidth : 840 ,
14011423 minHeight : 620 ,
1402- show : false ,
1424+ show : isDevelopment ,
14031425 autoHideMenuBar : true ,
1426+ backgroundColor : getInitialWindowBackgroundColor ( ) ,
14041427 ...getIconOption ( ) ,
14051428 title : APP_DISPLAY_NAME ,
14061429 titleBarStyle : "hiddenInset" ,
@@ -1457,13 +1480,18 @@ function createWindow(): BrowserWindow {
14571480 window . setTitle ( APP_DISPLAY_NAME ) ;
14581481 emitUpdateState ( ) ;
14591482 } ) ;
1460- window . once ( "ready-to-show" , ( ) => {
1461- window . show ( ) ;
1462- } ) ;
1483+ if ( ! isDevelopment ) {
1484+ window . once ( "ready-to-show" , ( ) => {
1485+ revealWindow ( window ) ;
1486+ } ) ;
1487+ }
14631488
14641489 if ( isDevelopment ) {
1465- void window . loadURL ( process . env . VITE_DEV_SERVER_URL as string ) ;
1490+ void window . loadURL ( resolveDesktopDevServerUrl ( ) ) ;
14661491 window . webContents . openDevTools ( { mode : "detach" } ) ;
1492+ setImmediate ( ( ) => {
1493+ revealWindow ( window ) ;
1494+ } ) ;
14671495 } else {
14681496 void window . loadURL ( resolveDesktopWindowUrl ( ) ) ;
14691497 }
@@ -1494,22 +1522,46 @@ configureAppIdentity();
14941522
14951523async function bootstrap ( ) : Promise < void > {
14961524 writeDesktopLogHeader ( "bootstrap start" ) ;
1497- const backendHost = resolveDesktopBackendHost ( ) ;
1498- backendPort = await Effect . service ( NetService ) . pipe (
1499- Effect . flatMap ( ( net ) => net . reserveLoopbackPort ( backendHost ) ) ,
1500- Effect . provide ( NetService . layer ) ,
1501- Effect . runPromise ,
1525+ const configuredBackendPort = resolveConfiguredDesktopBackendPort ( process . env . T3CODE_PORT ) ;
1526+ if ( isDevelopment && configuredBackendPort === undefined ) {
1527+ throw new Error ( "T3CODE_PORT is required in desktop development." ) ;
1528+ }
1529+
1530+ backendPort =
1531+ configuredBackendPort ??
1532+ ( await Effect . service ( NetService ) . pipe (
1533+ Effect . flatMap ( ( net ) => net . reserveLoopbackPort ( DESKTOP_LOOPBACK_HOST ) ) ,
1534+ Effect . provide ( NetService . layer ) ,
1535+ Effect . runPromise ,
1536+ ) ) ;
1537+ writeDesktopLogHeader (
1538+ configuredBackendPort === undefined
1539+ ? `reserved backend port via NetService port=${ backendPort } `
1540+ : `using configured backend port port=${ backendPort } ` ,
15021541 ) ;
1503- writeDesktopLogHeader ( `reserved backend port via NetService port=${ backendPort } ` ) ;
15041542 backendBootstrapToken = Crypto . randomBytes ( 24 ) . toString ( "hex" ) ;
1505- backendHttpUrl = `http://${ backendHost } :${ backendPort } ` ;
1506- backendWsUrl = `ws://${ backendHost } :${ backendPort } ` ;
1543+ backendHttpUrl = `http://${ DESKTOP_LOOPBACK_HOST } :${ backendPort } ` ;
1544+ backendWsUrl = `ws://${ DESKTOP_LOOPBACK_HOST } :${ backendPort } ` ;
15071545 writeDesktopLogHeader ( `bootstrap resolved backend endpoint baseUrl=${ backendHttpUrl } ` ) ;
15081546
15091547 registerIpcHandlers ( ) ;
15101548 writeDesktopLogHeader ( "bootstrap ipc handlers registered" ) ;
15111549 startBackend ( ) ;
15121550 writeDesktopLogHeader ( "bootstrap backend start requested" ) ;
1551+
1552+ if ( isDevelopment ) {
1553+ mainWindow = createWindow ( ) ;
1554+ writeDesktopLogHeader ( "bootstrap main window created" ) ;
1555+ void waitForBackendHttpReady ( backendHttpUrl )
1556+ . then ( ( ) => {
1557+ writeDesktopLogHeader ( "bootstrap backend ready" ) ;
1558+ } )
1559+ . catch ( ( error ) => {
1560+ handleFatalStartupError ( "bootstrap" , error ) ;
1561+ } ) ;
1562+ return ;
1563+ }
1564+
15131565 await waitForBackendHttpReady ( backendHttpUrl ) ;
15141566 writeDesktopLogHeader ( "bootstrap backend ready" ) ;
15151567 mainWindow = createWindow ( ) ;
0 commit comments