diff --git a/crates/capri-computer/src/ui.rs b/crates/capri-computer/src/ui.rs new file mode 100644 index 0000000..a3170d5 --- /dev/null +++ b/crates/capri-computer/src/ui.rs @@ -0,0 +1,107 @@ +//! Computer control and Workflows settings pages. Granting full access needs the owner's +//! confirmation, so the toggle returns a [`ComputerRequest`] for the app to run. +use capri_core::SettingsStore; + +use crate::access::{self, ApplicationAccess, ComputerAppearance, FullComputerAccess}; +use crate::tool::DesktopControl; + +pub enum ComputerRequest { + /// Ask the owner (3 s delayed danger confirmation), then enable. + EnableFullAccess, + RunWorkflow(String), + /// Stop button of the indicator / settings. + Stop, +} + +fn save(store: &SettingsStore, v: &T) { + if let Err(e) = store.set_section(v) { + tracing::warn!("Settings not saved: {e}"); + } +} + +pub fn settings_ui(ui: &mut egui::Ui, store: &SettingsStore, desktop: &DesktopControl) -> Vec { + let mut out = vec![]; + egui::CollapsingHeader::new("Computer control").default_open(true).show(ui, |ui| out.extend(computer_page(ui, store, desktop))); + egui::CollapsingHeader::new("Workflows").show(ui, |ui| out.extend(workflows_page(ui, desktop))); + out +} + +pub fn computer_page(ui: &mut egui::Ui, store: &SettingsStore, desktop: &DesktopControl) -> Vec { + let mut out = vec![]; + let full: FullComputerAccess = store.section(); + let mut enabled = full.enabled; + if ui.checkbox(&mut enabled, "Full computer access").changed() { + if enabled { + out.push(ComputerRequest::EnableFullAccess); + } else { + save(store, &FullComputerAccess { enabled: false, ..full }); + desktop.access_changed(); + } + } + let mut app_access: ApplicationAccess = store.section(); + if ui.checkbox(&mut app_access.enabled, "Application access").changed() { + save(store, &app_access); + } + if desktop.control.active() && ui.button("Stop computer control").clicked() { + out.push(ComputerRequest::Stop); + } + ui.separator(); + let before: ComputerAppearance = store.section(); + let mut a = before.clone(); + ui.checkbox(&mut a.enabled, "Indicator effects"); + ui.add(egui::Slider::new(&mut a.intensity, 0..=100).text("Intensity")); + ui.horizontal(|ui| { + ui.selectable_value(&mut a.theme, "capri".into(), "Capri"); + ui.selectable_value(&mut a.theme, "blackwall".into(), "Blackwall"); + }); + ui.checkbox(&mut a.cursor, "Cursor trail"); + ui.add(egui::Slider::new(&mut a.pointer_duration, 0..=1000).text("Pointer animation (ms)")); + if a != before { + desktop.control.pointer_duration.store(a.pointer_duration as u64, std::sync::atomic::Ordering::Relaxed); + save(store, &a); + } + ui.separator(); + ui.label("Applications"); + let apps = desktop.catalog().saved.clone(); + for app in apps { + ui.horizontal(|ui| { + ui.label(&app.name); + let (mut l, mut i, mut t) = (app.launch, app.inspect, app.text); + let changed = ui.checkbox(&mut l, "Launch").changed() | ui.checkbox(&mut i, "Inspect").changed() | ui.add_enabled(i, egui::Checkbox::new(&mut t, "Text")).changed(); + let remove = ui.button("Remove").clicked(); + if changed || remove { + let _ = desktop.catalog().update(&app.id, Some(l), Some(i), Some(t && i), remove); + } + }); + } + egui::CollapsingHeader::new("Action journal").show(ui, |ui| { + for op in desktop.journal.recent().unwrap_or_default() { + ui.label(format!("{} ยท {}", op["state"].as_str().unwrap_or(""), op["result"]["confirmation"].as_str().or(op["result"]["error"].as_str()).unwrap_or(""))); + } + }); + ui.small(if access::allowed(store) { "Capri can operate applications on request. Escape stops it at any time." } else { "Computer control is off." }); + out +} + +pub fn workflows_page(ui: &mut egui::Ui, desktop: &DesktopControl) -> Vec { + let mut out = vec![]; + let list = desktop.workflows.list(); + if list.is_empty() { + ui.label("No workflows yet."); + } + for w in list { + ui.horizontal(|ui| { + ui.label(format!("{} ({} steps)", w.name, w.steps.len())); + if ui.add_enabled(!desktop.workflows.playing(), egui::Button::new("Run")).clicked() { + out.push(ComputerRequest::RunWorkflow(w.id.clone())); + } + if ui.button("Remove").clicked() + && let Err(e) = desktop.workflows.remove(&w.id) + { + tracing::warn!("Workflow not removed: {e}"); + } + }); + } + ui.small("Recording new workflows is not available on Linux yet."); + out +}