Skip to content

Commit 9e04feb

Browse files
allow downloading CEF if it's not present
Signed-off-by: NikitaSkrynnik <[email protected]>
1 parent d219c88 commit 9e04feb

File tree

4 files changed

+92
-171
lines changed

4 files changed

+92
-171
lines changed

src-tauri/src/cef.rs

Lines changed: 0 additions & 114 deletions
This file was deleted.

src-tauri/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ pub fn run() {
5353
}
5454
}
5555
})
56-
.invoke_handler(tauri::generate_handler!(get_args, cef::launch_cef))
56+
.invoke_handler(tauri::generate_handler!(
57+
get_args,
58+
cef::is_cef_present,
59+
cef::download_cef,
60+
cef::launch_cef
61+
))
5762
.plugin(tauri_plugin_opener::init())
5863
.run(tauri::generate_context!())
5964
.expect("error while running tauri application");

src/App.tsx

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,28 @@
11
import { createSignal, onMount, Show } from "solid-js";
2-
import { AppState } from "./state/state";
2+
import { AppEvent, AppState, Arguments, initializeApp } from "./state/state";
33
import Browser from "./components/Browser";
44
import Notification from "./components/Notification";
55
import "./App.css";
6-
import { connect } from "cef-client";
7-
import { Channel, invoke } from "@tauri-apps/api/core";
8-
import { ProfileManager } from "./state/profiles";
9-
import { ShortcutPlugin } from "./state/plugins/shortcut";
6+
import { invoke } from "@tauri-apps/api/core";
107
import Sidebar from "./components/sidebar";
8+
import { ShortcutPlugin } from "./state/plugins/shortcut";
119

12-
interface Arguments {
13-
profiles_enabled: boolean;
14-
cef_manager: string;
15-
cef: string;
16-
}
17-
18-
async function launchCEF(): Promise<AppState> {
19-
let addr = await invoke('launch_cef');
20-
let browser = await connect(addr as string);
21-
return new AppState(browser);
22-
}
23-
24-
async function connectToManager(managerAddress: string): Promise<AppState> {
25-
let profileManager = new ProfileManager(managerAddress);
26-
let profiles = await profileManager.getProfiles();
27-
if (profiles.length === 0) {
28-
throw new Error('No profiles found');
29-
}
30-
31-
profileManager.setSelected(profiles[0]);
32-
let client = await profileManager.connect(profiles[0]);
33-
return new AppState(client, profileManager);
3410

35-
}
3611

37-
interface Event {
38-
message: string;
39-
type: 'info' | 'error';
40-
}
4112

4213
function App() {
43-
let [event, setEvent] = createSignal<Event>({ message: "", type: "info" });
14+
let [event, setEvent] = createSignal<AppEvent>({ message: "Initializing App", type: "info" });
4415
let [app, setApp] = createSignal<AppState | null>(null);
4516

4617
onMount(async () => {
47-
try {
48-
const args = await invoke("get_args") as Arguments;
49-
50-
let appState: AppState;
51-
if (args.profiles_enabled) {
52-
setEvent({ message: `Connecting to Huly CEF Manager on address ${args.cef_manager}`, type: "info" });
53-
appState = await connectToManager(args.cef_manager);
54-
} else if (args.cef !== "") {
55-
setEvent({ message: `Connecting to Huly CEF on address ${args.cef}`, type: "info" });
56-
const browser = await connect(args.cef);
57-
appState = new AppState(browser);
58-
} else {
59-
appState = await launchCEF();
60-
}
61-
62-
appState.addPlugin(new ShortcutPlugin());
63-
64-
setApp(appState);
65-
} catch (error) {
66-
const errorMessage = error instanceof Error ? error.message : String(error);
67-
setEvent({ message: `Failed to initialize: ${errorMessage}`, type: "error" });
68-
}
18+
const args = await invoke("get_args") as Arguments;
19+
let app = await initializeApp(args, setEvent);
20+
if (!app) return;
21+
app.addPlugin(new ShortcutPlugin());
22+
setApp(app);
6923
});
7024

25+
7126
return (
7227
<Show when={app()} fallback={<Notification message={event().message} type={event().type} />}>
7328
{(app) =>

src/state/state.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { Browser, LoadState, LoadStatus, Tab, TabEventStream } from "cef-client";
1+
import { Browser, connect, LoadState, LoadStatus, Tab, TabEventStream } from "cef-client";
22
import { createStore, SetStoreFunction } from "solid-js/store";
33
import { BrowserPlugin } from "./plugins/plugin";
44
import { ProfileManager } from "./profiles";
55
import { isURL, isFQDN } from "validator";
6+
import { invoke } from "@tauri-apps/api/core";
7+
import { Setter } from "solid-js";
68

79
type TabId = number;
810

@@ -242,4 +244,77 @@ export class AppState {
242244
}
243245
return `https://www.google.com/search?q=${searchString}`;
244246
}
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+
}
245320
}

0 commit comments

Comments
 (0)