diff --git a/crates/capri-tools/src/ui.rs b/crates/capri-tools/src/ui.rs new file mode 100644 index 0000000..a1e2d3b --- /dev/null +++ b/crates/capri-tools/src/ui.rs @@ -0,0 +1,155 @@ +//! Settings pages owned by this crate. Plain egui; styling belongs to the UI crate. +use std::sync::Arc; + +use capri_core::SettingsStore; +use serde_json::json; + +use crate::files::{Access, INSTALLATION_ID}; +use crate::{TimeService, ToolsSettings}; + +fn notice(ui: &mut egui::Ui, key: &str, result: anyhow::Result<()>) { + let id = ui.scope_id().with(("capri-tools-notice", key)); + if let Err(e) = result { + ui.data_mut(|d| d.insert_temp(id, e.to_string())); + } + if let Some(message) = ui.data(|d| d.get_temp::(id)) { + ui.colored_label(ui.visuals().warn_fg_color, message); + } +} + +/// File access, Application access, Timers and reminders options, Internet search. +pub fn settings_ui(ui: &mut egui::Ui, store: &Arc) { + let mut s: ToolsSettings = store.section(); + let before = s.clone(); + + ui.heading("Application access"); + ui.checkbox(&mut s.application_access, "Application access enabled") + .on_hover_text("When off, application, Spotify and trash actions are paused. Individual grants are kept."); + ui.checkbox(&mut s.notepad, "Text editor (Notepad): open it for Capri"); + ui.checkbox(&mut s.calculator, "Calculator: precise calculations and opening the calculator"); + // TODO: plain toggle; Capri asked "Enable Spotify control?" in a modal first. Add it once the UI crate exposes confirmations. + ui.checkbox(&mut s.spotify, "Spotify (BETA): control the Spotify desktop app") + .on_hover_text("Only the Spotify desktop app is controlled, over MPRIS. Named search opens Spotify's own search."); + + ui.separator(); + ui.heading("Timers and reminders"); + ui.checkbox(&mut s.wake_on_reminder, "Wake Capri and prepare her voice for reminders"); + ui.label("Timers survive model unloading and are restored after restart. Keep Capri running for notifications. Missed reminders appear next time you open Capri."); + + if s != before { + notice(ui, "apps", store.set_section(&s)); + } + + ui.separator(); + ui.heading("File access"); + file_access(ui, store); + + ui.separator(); + ui.heading("Internet search"); + ui.label("Internet mode is switched per conversation with the globe button. It searches the web for the current request only. Chat history, screenshots and files are not sent automatically. Requests that look like passwords or keys are never sent."); +} + +fn file_access(ui: &mut egui::Ui, store: &Arc) { + let access = match Access::new(store.clone(), store.paths(), Arc::new(|| json!({}))) { + Ok(access) => access, + Err(e) => return notice(ui, "files", Err(e)), + }; + ui.label("Capri may read the folders below. \"Allow file changes\" lets her create, replace, move and trash files there; outside the installation folder she asks first."); + let scopes = access.scopes(); + if scopes.is_empty() { + ui.label("No folders are shared with Capri."); + } + let mut result = Ok(()); + for scope in scopes { + ui.horizontal(|ui| { + let label = if scope.id == INSTALLATION_ID { format!("Capri data ({})", scope.root.display()) } else { scope.root.display().to_string() }; + ui.label(label); + let mut write = scope.write; + if ui.checkbox(&mut write, "Allow file changes").changed() { + result = access.set_write(&scope.id, write); + } + if ui.button("Revoke folder access").clicked() { + result = access.revoke(Some(&scope.id)); + } + }); + } + let id = ui.scope_id().with("capri-tools-folder"); + let mut folder: String = ui.data(|d| d.get_temp(id)).unwrap_or_default(); + ui.horizontal(|ui| { + // TODO: typed path instead of the portal folder picker; add rfd/ashpd in the UI crate. + ui.add(egui::TextEdit::singleline(&mut folder).hint_text("/home/you/Documents")); + if ui.button("Choose a folder…").clicked() && !folder.trim().is_empty() { + result = access.grant(std::path::Path::new(folder.trim())).map_err(|_| { + anyhow::anyhow!("This folder cannot be added. Choose a personal folder outside system and Capri application data.") + }); + folder.clear(); + } + }); + ui.data_mut(|d| d.insert_temp(id, folder)); + if ui.button("Revoke all access").clicked() { + result = access.revoke(None); + } + notice(ui, "files", result); +} + +/// The Timers and reminders panel: create, list and control clocks. +pub fn timers_ui(ui: &mut egui::Ui, time: &TimeService) { + let id = ui.scope_id().with("capri-tools-timer-form"); + let (mut kind, mut title, mut h, mut m, mut sec): (usize, String, u32, u32, u32) = + ui.data(|d| d.get_temp(id)).unwrap_or((1, String::new(), 0, 5, 0)); + let mut result = Ok(()); + let run = |args: serde_json::Value| time.execute(&uuid::Uuid::new_v4().to_string(), &args).map(|_| ()); + ui.horizontal(|ui| { + egui::ComboBox::from_id_salt("capri-tools-kind").show_index(ui, &mut kind, 3, |i| ["Reminder", "Timer", "Stopwatch"][i]); + ui.add(egui::TextEdit::singleline(&mut title).char_limit(1000).hint_text("Title")); + }); + ui.add_enabled_ui(kind != 2, |ui| { + ui.horizontal(|ui| { + ui.add(egui::DragValue::new(&mut h).range(0..=8784).suffix(" h")); + ui.add(egui::DragValue::new(&mut m).range(0..=59).suffix(" min")); + ui.add(egui::DragValue::new(&mut sec).range(0..=59).suffix(" s")); + }); + }); + if ui.button("Start").clicked() { + let action = ["remind", "timer", "stopwatch"][kind]; + let seconds = (h * 3600 + m * 60 + sec).min(366 * 86400); + let mut args = json!({"action": action, "title": title}); + if kind != 2 { + args["seconds"] = seconds.into(); + } + result = run(args); + } + ui.data_mut(|d| d.insert_temp(id, (kind, title, h, m, sec))); + ui.separator(); + let objects: Vec<_> = time.snapshot().into_iter().filter(|o| o["state"] != "cancelled" && o["state"] != "acknowledged").take(100).collect(); + if objects.is_empty() { + ui.label("No active timers or reminders."); + } + for o in objects { + let (oid, state, kind) = (o["id"].as_str().unwrap_or(""), o["state"].as_str().unwrap_or(""), o["kind"].as_str().unwrap_or("")); + let shown = if kind == "stopwatch" { o["elapsedSeconds"].as_i64().unwrap_or(0) } else { (o["remainingMs"].as_f64().unwrap_or(0.0) / 1000.0).ceil() as i64 }; + let label = o["title"].as_str().filter(|t| !t.is_empty()).unwrap_or(kind); + let status = match state { + "due" => "Due now", + "paused" => "Paused", + _ => "Running", + }; + ui.horizontal(|ui| { + ui.label(format!("{label} · {status} · {:02}:{:02}:{:02}", shown / 3600, shown / 60 % 60, shown % 60)); + if kind != "reminder" && state == "running" && ui.button("Pause").clicked() { + result = run(json!({"action": "pause", "id": oid})); + } + if state == "paused" && ui.button("Resume").clicked() { + result = run(json!({"action": "resume", "id": oid})); + } + if state == "due" && kind != "stopwatch" && ui.button("Snooze 5 minutes").clicked() { + result = run(json!({"action": "snooze", "id": oid})); + } + if ui.button("Dismiss").clicked() { + result = run(json!({"action": "cancel", "id": oid})); + } + }); + } + notice(ui, "timers", result); + ui.ctx().request_repaint_after(std::time::Duration::from_secs(1)); +}