Skip to main content

orbok_ui/
state.rs

1//! Headless UI state (view models) and the message vocabulary.
2//!
3//! Everything here is plain data โ€” testable without a display server.
4//! `orbok-app` populates these structs from backend services; views
5//! render them; `update` mutates them. No iced types appear in this
6//! module so state logic stays UI-framework-agnostic.
7
8use crate::i18n::Locale;
9use orbok_models::SearchCapability;
10use orbok_search::SearchMode;
11
12/// Top-level navigation group for the two-level sidebar + tab layout.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum NavGroup {
15    Search,
16    Ai,
17    Settings,
18}
19
20/// Top-level pages (GUI external design ยง3.1 order).
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum ViewId {
23    Search,
24    Sources,
25    Indexing,
26    Storage,
27    Models,
28    Settings,
29}
30
31impl ViewId {
32    pub const ALL: &'static [ViewId] = &[
33        ViewId::Search,
34        ViewId::Sources,
35        ViewId::Indexing,
36        ViewId::Storage,
37        ViewId::Models,
38        ViewId::Settings,
39    ];
40
41    /// Which top-level navigation group this view belongs to.
42    pub fn group(self) -> NavGroup {
43        match self {
44            ViewId::Search | ViewId::Sources => NavGroup::Search,
45            ViewId::Indexing | ViewId::Storage | ViewId::Models => NavGroup::Ai,
46            ViewId::Settings => NavGroup::Settings,
47        }
48    }
49
50    /// Default view to activate when the user first enters a group.
51    pub fn group_default(group: NavGroup) -> Self {
52        match group {
53            NavGroup::Search => ViewId::Search,
54            NavGroup::Ai => ViewId::Indexing,
55            NavGroup::Settings => ViewId::Settings,
56        }
57    }
58}
59
60
61/// Sidebar index-health summary.
62#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
63pub struct IndexHealth {
64    pub indexed: u64,
65    pub stale: u64,
66    pub failed: u64,
67    pub queued: u64,
68}
69
70/// One source card for the Sources view.
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct SourceCard {
73    pub display_name: String,
74    pub display_path: String,
75    pub indexed: u64,
76    pub stale: u64,
77    pub failed: u64,
78    pub active: bool,
79    pub source_id: String,
80}
81
82/// A search result ready for display โ€” pure data, no backend types
83/// (RFC-027 boundary rule).
84#[derive(Debug, Clone, PartialEq)]
85pub struct SearchResultDisplay {
86    pub display_path: String,
87    pub title: Option<String>,
88    pub heading_path: Option<String>,
89    pub snippet: Option<String>,
90    pub keyword_rank: u32,
91    pub badges: Vec<String>,
92}
93
94
95/// One required file and its check result shown in the wizard.
96#[derive(Debug, Clone, PartialEq)]
97pub struct WizardFileCheck {
98    pub relative_path: String,
99    pub found: bool,
100    pub size_mb: Option<f64>,
101}
102
103/// Which stage of the startup wizard the user is on.
104#[derive(Debug, Clone, PartialEq)]
105pub enum WizardState {
106    /// First launch or model never configured.
107    NotConfigured,
108    /// Was configured, but files are gone.
109    FileMissing { previous_dir: String, checks: Vec<WizardFileCheck> },
110    /// User submitted a path; file checks complete.
111    Checked { model_dir: String, checks: Vec<WizardFileCheck>, all_ok: bool },
112    /// All files verified โ€” ready to proceed.
113    Ready { model_dir: String },
114}
115
116/// The whole-app view model.
117#[derive(Debug, Clone)]
118pub struct AppState {
119    pub active_view: ViewId,
120    pub locale: Locale,
121    pub query: String,
122    pub last_query: Option<String>,
123    pub search_mode: SearchMode,
124    pub search_results: Vec<SearchResultDisplay>,
125    pub search_running: bool,
126    pub selected_result: Option<usize>,
127    pub storage_rows: Vec<(String, u64, u64)>,
128    pub health: IndexHealth,
129    pub sources: Vec<SourceCard>,
130    pub capability: SearchCapability,
131    pub storage_total_bytes: u64,
132    /// Active startup wizard, or `None` when startup succeeded.
133    pub wizard: Option<WizardState>,
134    /// Text-input path the user is typing in the wizard.
135    pub wizard_path_input: String,
136    /// Text input for the "add source" path field.
137    pub source_path_input: String,
138}
139
140impl Default for AppState {
141    fn default() -> Self {
142        Self {
143            active_view: ViewId::Search,
144            locale: Locale::default(),
145            query: String::new(),
146            last_query: None,
147            search_mode: SearchMode::Auto,
148            search_results: Vec::new(),
149            search_running: false,
150            selected_result: None,
151            storage_rows: Vec::new(),
152            health: IndexHealth::default(),
153            sources: Vec::new(),
154            capability: SearchCapability::KeywordOnly,
155            storage_total_bytes: 0,
156            wizard: None,
157            wizard_path_input: String::new(),
158            source_path_input: String::new(),
159        }
160    }
161}
162
163/// UI messages.
164#[derive(Debug, Clone)]
165pub enum Message {
166    Switch(ViewId),
167    SwitchGroup(NavGroup),
168    QueryChanged(String),
169    SubmitSearch,
170    SearchResultsReady(Vec<SearchResultDisplay>),
171    SearchError(String),
172    SelectResult(usize),
173    OpenSourceFile(String),
174    SetSearchMode(SearchMode),
175    PersistLocale(Locale),
176    SetLocale(Locale),
177    StorageDataReady(Vec<(String, u64, u64)>),
178    // Startup wizard
179    WizardPathChanged(String),
180    WizardValidate,
181    WizardChecked { model_dir: String, checks: Vec<WizardFileCheck>, all_ok: bool },
182    WizardAccept,
183    WizardSkip,
184    // Source management
185    SourcePathChanged(String),
186    RequestAddSource,
187    SourceAdded(SourceCard),
188    SourceRemoved(String),   // source_id
189    ScanCompleted(IndexHealth),
190    // Startup population
191    HealthUpdated(IndexHealth),
192    SourcesLoaded(Vec<SourceCard>),
193}
194
195impl AppState {
196    pub fn update(&mut self, message: &Message) {
197        match message {
198            Message::Switch(view) => self.active_view = *view,
199            Message::SwitchGroup(group) => self.active_view = ViewId::group_default(*group),
200            Message::QueryChanged(query) => self.query = query.clone(),
201            Message::SubmitSearch => {
202                let trimmed = self.query.trim();
203                if !trimmed.is_empty() {
204                    self.last_query = Some(trimmed.to_string());
205                    self.search_running = true;
206                    self.search_results.clear();
207                    self.selected_result = None;
208                }
209            }
210            Message::SearchResultsReady(results) => {
211                self.search_results = results.clone();
212                self.search_running = false;
213                self.selected_result = None;
214            }
215            Message::SearchError(_) => {
216                self.search_running = false;
217            }
218            Message::SelectResult(idx) => self.selected_result = Some(*idx),
219            Message::OpenSourceFile(_) => {} // handled by orbok-app
220            Message::SetSearchMode(mode) => self.search_mode = *mode,
221            Message::PersistLocale(locale) | Message::SetLocale(locale) => self.locale = *locale,
222            Message::StorageDataReady(rows) => self.storage_rows = rows.clone(),
223            Message::WizardPathChanged(p) => self.wizard_path_input = p.clone(),
224            Message::WizardValidate => {} // handled in orbok-app update
225            Message::WizardChecked { model_dir, checks, all_ok } => {
226                self.wizard = Some(if *all_ok {
227                    WizardState::Ready { model_dir: model_dir.clone() }
228                } else {
229                    WizardState::Checked {
230                        model_dir: model_dir.clone(),
231                        checks: checks.clone(),
232                        all_ok: false,
233                    }
234                });
235            }
236            Message::WizardAccept => {
237                // orbok-app writes the model dir to OrbokSettings; ui
238                // transitions to full capability.
239                self.capability = SearchCapability::Hybrid;
240                self.wizard = None;
241                self.wizard_path_input = String::new();
242            }
243            Message::WizardSkip => {
244                self.capability = SearchCapability::KeywordOnly;
245                self.wizard = None;
246                self.wizard_path_input = String::new();
247            }
248            Message::SourcePathChanged(p) => self.source_path_input = p.clone(),
249            Message::RequestAddSource => {} // handled in orbok-app
250            Message::SourceAdded(card) => {
251                self.sources.push(card.clone());
252                self.source_path_input = String::new();
253            }
254            Message::SourceRemoved(id) => self.sources.retain(|s| s.source_id != *id),
255            Message::ScanCompleted(health) | Message::HealthUpdated(health) => {
256                self.health = *health;
257                // Update per-source counts from the fresh health data.
258            }
259            Message::SourcesLoaded(cards) => self.sources = cards.clone(),
260        }
261    }
262}