robin/src-tauri/src/commands.rs

81 lines
2.1 KiB
Rust

use crate::config::{MigrationConfig, NotificationLevel, RobinConfig, SourceOs};
use std::sync::Mutex;
use tauri::State;
pub struct AppState {
pub config: Mutex<RobinConfig>,
}
#[tauri::command]
pub fn get_config(state: State<'_, AppState>) -> Result<RobinConfig, String> {
state
.config
.lock()
.map(|c| c.clone())
.map_err(|e| e.to_string())
}
#[tauri::command]
pub fn needs_onboarding(state: State<'_, AppState>) -> bool {
state
.config
.lock()
.map(|c| c.needs_onboarding())
.unwrap_or(true)
}
#[tauri::command]
pub fn complete_onboarding(
source_os: String,
distro: String,
state: State<'_, AppState>,
) -> Result<(), String> {
let source = match source_os.to_lowercase().as_str() {
"macos" | "mac" => SourceOs::Macos,
"windows" => SourceOs::Windows,
"linux" => SourceOs::Linux,
_ => SourceOs::Unknown,
};
let detected = if distro == "unknown" || distro.is_empty() {
crate::distro::detect()
} else {
distro
};
let mut config = state.config.lock().map_err(|e| e.to_string())?;
config.migration = Some(MigrationConfig {
source_os: source,
distro: detected,
fluency_level: 0,
});
config.save().map_err(|e| e.to_string())
}
#[tauri::command]
pub fn update_notification_level(level: String, state: State<'_, AppState>) -> Result<(), String> {
let parsed = match level.as_str() {
"off" => NotificationLevel::Off,
"badge_only" => NotificationLevel::BadgeOnly,
"badge_and_toast" => NotificationLevel::BadgeAndToast,
_ => return Err(format!("unknown notification level: {level}")),
};
let mut config = state.config.lock().map_err(|e| e.to_string())?;
config.display.notification_level = parsed;
config.save().map_err(|e| e.to_string())
}
#[tauri::command]
pub fn get_pending_events() -> Vec<crate::patterns::MatchedEvent> {
crate::notify::take_pending()
}
#[tauri::command]
pub fn panel_opened() {
crate::notify::set_panel_open(true);
}
#[tauri::command]
pub fn panel_closed() {
crate::notify::set_panel_open(false);
}