122 lines
5.4 KiB
Rust
122 lines
5.4 KiB
Rust
// file: crates/ksp-app-solprices-desk/src/tauri.rs
|
|
// version: 1
|
|
|
|
//! Tauri runtime assembly for the KSP SOL prices desktop application.
|
|
|
|
/// Runs the SOL prices desktop application.
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
pub fn run(arguments: &[std::ffi::OsString]) -> ksp_core_lib::Result<()> {
|
|
let context = tauri::generate_context!();
|
|
let runtime_layout = configure_packaged_runtime(&context);
|
|
if let std::result::Result::Err(error) = runtime_layout {
|
|
return std::result::Result::Err(error);
|
|
}
|
|
let app_state = crate::AppState::initialize(arguments);
|
|
let app_state = match app_state {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let mut builder = tauri::Builder::default();
|
|
builder = configure_state(builder, app_state);
|
|
builder = configure_plugins(builder);
|
|
builder = configure_commands(builder);
|
|
builder = configure_setup(builder);
|
|
let run_result = builder.run(context);
|
|
return match run_result {
|
|
std::result::Result::Ok(()) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(
|
|
ksp_core_lib::Error::new(crate::ERROR_CODE_TAURI_RUNTIME_FAILED, "Cannot run KSP SOL Prices Desk Tauri runtime")
|
|
.with_context("tauri_error", error.to_string()),
|
|
),
|
|
};
|
|
}
|
|
|
|
fn configure_packaged_runtime(context: &tauri::Context<tauri::Wry>) -> ksp_core_lib::Result<()> {
|
|
if cfg!(debug_assertions) {
|
|
return std::result::Result::Ok(());
|
|
}
|
|
let resource_root = tauri::utils::platform::resource_dir(context.package_info(), &tauri::Env::default());
|
|
let resource_root = match resource_root {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => {
|
|
return std::result::Result::Err(
|
|
ksp_core_lib::Error::new(crate::ERROR_CODE_TAURI_RUNTIME_FAILED, "Cannot resolve packaged Tauri resource directory")
|
|
.with_context("tauri_error", error.to_string()),
|
|
);
|
|
},
|
|
};
|
|
let layout = ksp_config_lib::prepare_packaged_runtime(resource_root.as_path());
|
|
let layout = match layout {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
|
};
|
|
let working_directory = std::env::set_current_dir(layout.runtime_root());
|
|
return match working_directory {
|
|
std::result::Result::Ok(()) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(
|
|
ksp_core_lib::Error::new(crate::ERROR_CODE_TAURI_RUNTIME_FAILED, "Cannot activate packaged KSP runtime directory")
|
|
.with_context("runtime_root", layout.runtime_root().to_string_lossy().into_owned())
|
|
.with_source(error),
|
|
),
|
|
};
|
|
}
|
|
|
|
fn configure_state(builder: tauri::Builder<tauri::Wry>, app_state: crate::AppState) -> tauri::Builder<tauri::Wry> {
|
|
return builder.manage(app_state);
|
|
}
|
|
|
|
fn configure_plugins(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<tauri::Wry> {
|
|
let tracing_plugin = tauri_plugin_tracing::Builder::new().build::<tauri::Wry>();
|
|
return builder.plugin(tracing_plugin);
|
|
}
|
|
|
|
#[allow(clippy::question_mark_used)] // Tauri generates the question-mark operator internally for async command dispatch.
|
|
fn configure_commands(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<tauri::Wry> {
|
|
return builder.invoke_handler(tauri::generate_handler![emit_frontend_log, runtime_status, splash_frontend_ready]);
|
|
}
|
|
|
|
fn configure_setup(builder: tauri::Builder<tauri::Wry>) -> tauri::Builder<tauri::Wry> {
|
|
return builder.setup(|app| {
|
|
let splash = crate::require_splash_window(app);
|
|
if let std::result::Result::Err(error) = splash {
|
|
return std::result::Result::Err(std::boxed::Box::new(error));
|
|
}
|
|
let main = crate::require_main_window(app);
|
|
if let std::result::Result::Err(error) = main {
|
|
return std::result::Result::Err(std::boxed::Box::new(error));
|
|
}
|
|
return std::result::Result::Ok(());
|
|
});
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn emit_frontend_log(payload: crate::FrontendLogPayloadDto) -> std::result::Result<(), crate::CommandErrorDto> {
|
|
let result = crate::emit_frontend_log_event(payload);
|
|
return match result {
|
|
std::result::Result::Ok(()) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(crate::CommandErrorDto::from_error(&error)),
|
|
};
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn runtime_status(state: tauri::State<'_, crate::AppState>) -> std::result::Result<crate::RuntimeStatusDto, crate::CommandErrorDto> {
|
|
let result = state.runtime_status();
|
|
return match result {
|
|
std::result::Result::Ok(value) => std::result::Result::Ok(value),
|
|
std::result::Result::Err(error) => std::result::Result::Err(crate::CommandErrorDto::from_error(&error)),
|
|
};
|
|
}
|
|
|
|
#[tauri::command]
|
|
async fn splash_frontend_ready(
|
|
app: tauri::AppHandle,
|
|
webview_window: tauri::WebviewWindow,
|
|
state: tauri::State<'_, crate::AppState>,
|
|
) -> std::result::Result<(), crate::CommandErrorDto> {
|
|
let result = crate::splash_frontend_ready_service(app, webview_window, &state).await;
|
|
return match result {
|
|
std::result::Result::Ok(()) => std::result::Result::Ok(()),
|
|
std::result::Result::Err(error) => std::result::Result::Err(crate::CommandErrorDto::from_error(&error)),
|
|
};
|
|
}
|