1
- import { Browser , LoadState , LoadStatus , Tab , TabEventStream } from "cef-client" ;
1
+ import { Browser , connect , LoadState , LoadStatus , Tab , TabEventStream } from "cef-client" ;
2
2
import { createStore , SetStoreFunction } from "solid-js/store" ;
3
3
import { BrowserPlugin } from "./plugins/plugin" ;
4
4
import { ProfileManager } from "./profiles" ;
5
5
import { isURL , isFQDN } from "validator" ;
6
+ import { invoke } from "@tauri-apps/api/core" ;
7
+ import { Setter } from "solid-js" ;
6
8
7
9
type TabId = number ;
8
10
@@ -242,4 +244,77 @@ export class AppState {
242
244
}
243
245
return `https://www.google.com/search?q=${ searchString } ` ;
244
246
}
247
+ }
248
+
249
+ export interface Arguments {
250
+ profiles_enabled : boolean ;
251
+ cef_manager : string ;
252
+ cef : string ;
253
+ }
254
+
255
+ export interface AppEvent {
256
+ message : string ;
257
+ type : 'info' | 'error' ;
258
+ }
259
+
260
+ async function launchCEF ( setEvent : Setter < AppEvent > ) : Promise < AppState | null > {
261
+ try {
262
+ setEvent ( { message : 'Checking CEF presence...' , type : 'info' } ) ;
263
+ let present = await invoke ( 'is_cef_present' ) ;
264
+ if ( ! present ) {
265
+ setEvent ( { message : 'CEF not found, downloading...' , type : 'info' } ) ;
266
+ await invoke ( 'download_cef' ) ;
267
+ }
268
+
269
+ setEvent ( { message : 'Launching CEF...' , type : 'info' } ) ;
270
+ let addr = await invoke ( 'launch_cef' ) ;
271
+ let browser = await connect ( addr as string ) ;
272
+ return new AppState ( browser ) ;
273
+ } catch ( e ) {
274
+ const errorMessage = "Failed to launch CEF: " + ( e instanceof Error ? e . message : String ( e ) ) ;
275
+ setEvent ( { message : errorMessage , type : 'error' } ) ;
276
+ return null ;
277
+ }
278
+ }
279
+
280
+ async function connectToCefInstance ( cefAddress : string , setEvent : Setter < AppEvent > ) : Promise < AppState | null > {
281
+ try {
282
+ setEvent ( { message : 'Connecting to CEF instance...' , type : 'info' } ) ;
283
+ let browser = await connect ( cefAddress ) ;
284
+ return new AppState ( browser ) ;
285
+ } catch ( e ) {
286
+ const errorMessage = "Failed to connect to CEF instance: " + ( e instanceof Error ? e . message : String ( e ) ) ;
287
+ setEvent ( { message : errorMessage , type : 'error' } ) ;
288
+ return null ;
289
+ }
290
+ }
291
+
292
+ async function connectToManager ( managerAddress : string , setEvent : Setter < AppEvent > ) : Promise < AppState | null > {
293
+ try {
294
+ setEvent ( { message : 'Connecting...' , type : 'info' } ) ;
295
+ const profileManager = new ProfileManager ( managerAddress ) ;
296
+ const profiles = await profileManager . getProfiles ( ) ;
297
+ if ( profiles . length === 0 ) {
298
+ setEvent ( { message : 'No profiles found' , type : 'error' } ) ;
299
+ return null ;
300
+ }
301
+
302
+ profileManager . setSelected ( profiles [ 0 ] ) ;
303
+ const client = await profileManager . connect ( profiles [ 0 ] ) ;
304
+ return new AppState ( client , profileManager ) ;
305
+ } catch ( e ) {
306
+ const errorMessage = "Failed to connect to manager: " + ( e instanceof Error ? e . message : String ( e ) ) ;
307
+ setEvent ( { message : errorMessage , type : 'error' } ) ;
308
+ return null ;
309
+ }
310
+ }
311
+
312
+ export async function initializeApp ( args : Arguments , setEvent : Setter < AppEvent > ) : Promise < AppState | null > {
313
+ if ( args . profiles_enabled ) {
314
+ return await connectToManager ( args . cef_manager , setEvent ) ;
315
+ } else if ( args . cef !== "" ) {
316
+ return await connectToCefInstance ( args . cef , setEvent ) ;
317
+ } else {
318
+ return await launchCEF ( setEvent ) ;
319
+ }
245
320
}
0 commit comments