claude-usage-widget/src-tauri/src/commands.rs

72 lines
2.1 KiB
Rust

//! Tauri command surface — every JS-callable function lives here.
use crate::paths::{list_wsl_distros, resolve_roots, ResolvedRoots};
use crate::settings::{save as save_settings, Settings};
use crate::state::SharedState;
use crate::usage::{build_snapshot, UsageSnapshot};
use crate::watch::refresh_and_emit;
#[tauri::command]
pub async fn get_snapshot(state: tauri::State<'_, SharedState>) -> Result<UsageSnapshot, String> {
let mut events = state.collect_events();
events.sort_by_key(|e| e.ts);
let caps = state.settings.read().caps.clone();
Ok(build_snapshot(&events, &caps, chrono::Utc::now()))
}
#[tauri::command]
pub async fn get_settings(state: tauri::State<'_, SharedState>) -> Result<Settings, String> {
Ok(state.settings.read().clone())
}
#[tauri::command]
pub async fn set_settings(
state: tauri::State<'_, SharedState>,
app: tauri::AppHandle,
new: Settings,
) -> Result<(), String> {
{
*state.settings.write() = new.clone();
}
save_settings(&new).map_err(|e| e.to_string())?;
// If roots-related settings changed, force a re-resolve + rescan.
{
let mut roots = state.roots.write();
roots.clear();
}
refresh_and_emit(&app, state.inner(), None)
.await
.map_err(|e| e.to_string())?;
Ok(())
}
#[tauri::command]
pub async fn list_distros() -> Result<Vec<String>, String> {
list_wsl_distros().map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn get_roots(state: tauri::State<'_, SharedState>) -> Result<ResolvedRoots, String> {
let s = state.settings.read().clone();
Ok(resolve_roots(s.wsl_distro_override.as_deref(), s.include_native))
}
#[tauri::command]
pub async fn force_rescan(
state: tauri::State<'_, SharedState>,
app: tauri::AppHandle,
) -> Result<(), String> {
// Wipe caches so every file is reparsed from offset 0.
state.files.write().clear();
state.seen_ids.write().clear();
refresh_and_emit(&app, state.inner(), None)
.await
.map_err(|e| e.to_string())
}
#[tauri::command]
pub async fn quit_app(app: tauri::AppHandle) -> Result<(), String> {
app.exit(0);
Ok(())
}