retrom_plugin_standalone/
lib.rs1mod commands;
2mod desktop;
3mod error;
4
5use desktop::Standalone;
6pub use error::{Error, Result};
7use retrom_plugin_config::ConfigExt;
8use tauri::{
9 plugin::{Builder, TauriPlugin},
10 Manager, Runtime,
11};
12use tokio::runtime::Handle;
13
14pub trait StandaloneExt<R: Runtime> {
16 fn standalone(&self) -> &Standalone<R>;
17}
18
19impl<R: Runtime, T: Manager<R>> crate::StandaloneExt<R> for T {
20 fn standalone(&self) -> &Standalone<R> {
21 self.state::<Standalone<R>>().inner()
22 }
23}
24
25pub fn init<R: Runtime>() -> TauriPlugin<R> {
28 Builder::new("standalone")
29 .invoke_handler(tauri::generate_handler![
30 commands::enable_standalone_mode,
31 commands::disable_standalone_mode
32 ])
33 .setup(|app, api| {
34 let standalone = desktop::init(app, api)?;
35 app.manage(standalone);
36
37 let app = app.clone();
38 let (tx, rx) = std::sync::mpsc::channel();
39
40 tauri::async_runtime::spawn_blocking(|| {
41 tauri::async_runtime::block_on(async move {
42 let standalone = app.standalone();
43
44 let client_config = app.config_manager().get_config().await;
45
46 let is_standalone = client_config.server.is_some_and(|c| c.standalone());
47 if is_standalone {
48 if let Err(why) = standalone.start_server().await {
49 tracing::error!("Failed to start standalone server: {:#?}", why);
50 }
51 }
52
53 tx.send(()).unwrap();
54 })
55 });
56
57 rx.recv().expect("Failed to receive from channel");
58
59 Ok(())
60 })
61 .on_event(|app, event| {
62 if let tauri::RunEvent::ExitRequested { .. } = event {
63 tokio::task::block_in_place(move || {
64 Handle::current().block_on(async move {
65 let standalone = app.standalone();
66 if let Err(why) = standalone.stop_server().await {
67 tracing::error!("Failed to stop standalone server: {:#?}", why);
68 }
69 });
70 });
71 }
72 })
73 .build()
74}