This commit is contained in:
2026-04-20 20:14:40 +02:00
parent 4261291ac1
commit 176fe3db99
21 changed files with 1445 additions and 132 deletions

View File

@@ -1,136 +1,145 @@
// file: kb_app/src/lib.rs
//#![deny(unreachable_pub)]
//#![warn(missing_docs)]
//! Tauri application library for `khadhroony-bobobot`.
//!
//! This crate is intentionally thin. It loads the shared configuration,
//! initializes shared tracing from `kb_lib`, and wires the desktop shell
//! to the reusable backend logic.
#![deny(unreachable_pub)]
#![warn(missing_docs)]
mod splash;
pub use crate::splash::SplashOrder;
use tauri::Emitter;
use tauri::{Manager};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
fn setup_logger() -> tauri_plugin_tracing::Builder {
let log_dir = std::env::temp_dir().join("kb_app");
match std::fs::create_dir_all(&log_dir) {
Ok(_) => {},
Err(err) => {
eprintln!("failed to create log directory: {:?}", err);
},
}
let file_appender = tracing_appender::rolling::daily(&log_dir, "app");
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
std::mem::forget(guard);
let targets = tracing_subscriber::filter::Targets::new()
.with_default(tracing::Level::DEBUG)
.with_target("hyper", tracing::Level::WARN)
.with_target("reqwest", tracing::Level::WARN)
.with_target("tao", tracing::Level::WARN)
.with_target("wry", tracing::Level::WARN);
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_ansi(true))
.with(
tracing_subscriber::fmt::layer()
.with_writer(tauri_plugin_tracing::StripAnsiWriter::new(non_blocking))
.with_ansi(false),
)
.with(targets)
.init();
tauri_plugin_tracing::Builder::new()
}
use tauri::Manager;
/// Runs the desktop application.
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let tracing_builder = setup_logger();
let config_path = kb_lib::KbConfig::default_path();
let config_result = kb_lib::KbConfig::load_from_path(&config_path);
let config = match config_result {
Ok(config) => config,
Err(error) => {
eprintln!(
"kb_app configuration load error from '{}': {}",
config_path.display(),
error
);
return;
}
};
let prepare_result = config.prepare_filesystem();
if let Err(error) = prepare_result {
eprintln!("kb_app filesystem preparation error: {error}");
return;
}
let tracing_guard_result = kb_lib::init_tracing(&config.logging);
let _tracing_guard = match tracing_guard_result {
Ok(guard) => guard,
Err(error) => {
eprintln!("kb_app tracing initialization error: {error}");
return;
}
};
tracing::info!(
app_name = %config.app.name,
environment = %config.app.environment,
"starting desktop application"
);
let tracing_builder = tauri_plugin_tracing::Builder::new();
let mut tauri_builder = tauri::Builder::default();
tauri_builder = tauri_builder.plugin(tracing_builder.build::<tauri::Wry>());
tauri_builder = tauri_builder.setup(|app| {
let app_handle = app.handle().clone();
tauri::async_runtime::spawn(async move {
let splash_window = app_handle.get_webview_window("splash").unwrap();
let main_window = app_handle.get_webview_window("main").unwrap();
//main_window.set_title(&app_name).unwrap();
let splash_window_option = app_handle.get_webview_window("splash");
let splash_window = match splash_window_option {
Some(window) => window,
None => {
tracing::error!("splash window not found");
return;
}
};
let main_window_option = app_handle.get_webview_window("main");
let main_window = match main_window_option {
Some(window) => window,
None => {
tracing::error!("main window not found");
return;
}
};
let is_debug = cfg!(debug_assertions);
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
if is_debug {
let _ = splash_window.emit(
"splash",
splash::SplashOrder {
order: "add_log".to_string(),
msg: Some("Start Fade-In".to_string()),
status: None,
},
);
emit_splash_order(&splash_window, "add_log", Some("Start Fade-In"), None);
}
let _ = splash_window.emit(
"splash",
splash::SplashOrder {
order: "fadein".to_string(),
msg: None,
status: None,
},
);
let _ = splash_window.emit(
"splash",
splash::SplashOrder {
order: "add_msg".to_string(),
msg: Some("Initialisation...".to_string()),
status: Some("info".to_string()),
},
emit_splash_order(&splash_window, "fadein", None, None);
emit_splash_order(
&splash_window,
"add_msg",
Some("Initialisation..."),
Some("info"),
);
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
let _ = splash_window.emit(
"splash",
splash::SplashOrder {
order: "add_msg".to_string(),
msg: Some("Loading resources...".to_string()),
status: Some("info".to_string()),
},
emit_splash_order(
&splash_window,
"add_msg",
Some("Loading resources..."),
Some("info"),
);
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
let _ = splash_window.emit(
"splash",
splash::SplashOrder {
order: "add_msg".to_string(),
msg: Some("Loading complete...".to_string()),
status: Some("success".to_string()),
},
emit_splash_order(
&splash_window,
"add_msg",
Some("Loading complete..."),
Some("success"),
);
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
tracing::debug!("Start fadeout");
tracing::debug!("start splash fadeout");
if is_debug {
let _ = splash_window.emit(
"splash",
splash::SplashOrder {
order: "add_log".to_string(),
msg: Some("Start Fade-out".to_string()),
status: None,
},
);
emit_splash_order(&splash_window, "add_log", Some("Start Fade-out"), None);
}
let _ = splash_window.emit(
"splash",
splash::SplashOrder {
order: "fadeout".to_string(),
msg: None,
status: None,
},
);
tracing::debug!("End fadeout");
emit_splash_order(&splash_window, "fadeout", None, None);
tracing::debug!("end splash fadeout");
tokio::time::sleep(std::time::Duration::from_millis(3100)).await;
if let Err(err) = splash_window.close() {
tracing::error!("Error closing splash window: {:?}", err);
let close_result = splash_window.close();
if let Err(error) = close_result {
tracing::error!("error closing splash window: {error:?}");
}
if let Err(err) = main_window.show() {
tracing::error!("Error showing main window: {:?}", err);
let show_result = main_window.show();
if let Err(error) = show_result {
tracing::error!("error showing main window: {error:?}");
} else {
let _ = main_window.emit("setupTray", ());
let emit_result = main_window.emit("setupTray", ());
if let Err(error) = emit_result {
tracing::error!("error emitting setupTray event: {error:?}");
}
}
});
Ok(())
});
if let Err(err) = tauri_builder.run(tauri::generate_context!()) {
tracing::error!("error while running tauri application: {:?}", err);
let run_result = tauri_builder.run(tauri::generate_context!());
if let Err(error) = run_result {
tracing::error!("error while running tauri application: {error:?}");
}
}
fn emit_splash_order(
splash_window: &tauri::WebviewWindow,
order: &str,
msg: std::option::Option<&str>,
status: std::option::Option<&str>,
) {
let payload = crate::SplashOrder {
order: order.to_string(),
msg: msg.map(std::string::ToString::to_string),
status: status.map(std::string::ToString::to_string),
};
let emit_result = splash_window.emit("splash", payload);
if let Err(error) = emit_result {
tracing::error!("error emitting splash event '{order}': {error:?}");
}
}