kimun_notes/components/events.rs
1use std::num::NonZeroU64;
2use std::sync::Arc;
3use std::time::Duration;
4
5use ratatui::crossterm::event::{KeyEvent, MouseEvent};
6use tokio::sync::mpsc::UnboundedSender;
7
8use kimun_core::{NoteVault, nfs::VaultPath};
9
10use crate::components::file_list::{SortField, SortOrder};
11
12/// Which panel a sort selection applies to.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum SortTarget {
15 Sidebar,
16 Query,
17}
18
19/// The surface a save-current-query action sourced its query from. Carried
20/// through the save-search dialog so the editor knows whether the Query
21/// panel's breadcrumb should re-pin after the save — by identity, not by
22/// comparing query text (equal text from different surfaces must not collide).
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum SaveSource {
25 QueryPanel,
26 NoteBrowser,
27}
28
29/// All events that flow through the system — both input events (from crossterm)
30/// and app-level messages sent by components / screens to the main loop.
31#[derive(Debug, Clone)]
32pub enum AppEvent {
33 Input(InputEvent),
34 OpenScreen(ScreenEvent),
35
36 // ── App-level messages ───────────────────────────────────────────────────
37 Quit,
38 Redraw,
39 Autosave,
40 /// Background autosave task finished. `saved_revision` carries the
41 /// editor's `content_revision` at the moment the save was *issued*
42 /// on success, `None` if the write failed. The editor screen uses
43 /// `path` to ignore stale completions for notes the user has
44 /// already navigated away from, and `saved_revision` to clear the
45 /// dirty flag iff the buffer is still at that revision (i.e. no
46 /// edits during the save). `NonZeroU64` because the editor's
47 /// `content_revision` is never zero.
48 AutosaveCompleted {
49 path: VaultPath,
50 saved_revision: Option<NonZeroU64>,
51 /// The note's recomputed title (first body line) from the save, so the
52 /// sidebar row can be retitled. `None` when the save failed.
53 title: Option<String>,
54 },
55 /// Open a note (or directory) — `emphasis` carries the originating
56 /// query's needles when the open comes from a query result, so the
57 /// editor lights up the matched spans (spec §5.1). Use
58 /// [`AppEvent::open`] for the plain case.
59 OpenPath {
60 path: VaultPath,
61 emphasis: Option<Vec<String>>,
62 },
63 FocusSidebar,
64 /// Switch the drawer to the given view and reveal it (sent by the
65 /// activity rail and, later, by leader paths / mouse clicks).
66 OpenDrawerView(crate::components::drawer::DrawerView),
67 /// Run the query `#<label>` in the FIND drawer (sent by the TAGS drawer).
68 RunTagQuery(String),
69 /// Jump the editor cursor to the first heading with this text (sent by
70 /// the OUTLINE drawer).
71 JumpToHeading(String),
72 /// Run a leader-tree action (sent by the command palette after it has
73 /// closed itself, so the action sees no open overlay).
74 ExecuteLeaderAction(crate::keys::leader::LeaderAction),
75 /// Show a transient footer flash — async tasks report results with it.
76 FlashMessage(String),
77 /// Apply (and optionally persist) a resolved theme — sent by the theme
78 /// picker: previews on selection move, persists on Enter. Carries the
79 /// full `Theme` so applying never re-reads the themes directory.
80 ApplyTheme {
81 theme: Box<crate::settings::themes::Theme>,
82 persist: bool,
83 },
84 /// Async-loaded backlink count for the link target under the editor
85 /// cursor (status line 2's `→ target · N backlinks` affordance).
86 LinkTargetMeta {
87 target: String,
88 count: usize,
89 },
90 /// Async-loaded backlink count for the note at `path` (status line 2).
91 BacklinkCountLoaded {
92 path: VaultPath,
93 count: usize,
94 },
95 /// Async-loaded workspace git summary for the status bar, `None` when
96 /// the workspace is not a git repository.
97 GitStatusLoaded(Option<String>),
98 /// Sent by PreferencesScreen when user confirms Save. The shared settings
99 /// reference already contains the updated values.
100 PreferencesSaved,
101 /// Sent by OnboardingScreen when the user confirms Finish on the summary
102 /// step. The shared settings already contain the committed draft; main.rs
103 /// rebuilds the vault and navigates to Start (same as PreferencesSaved).
104 OnboardingFinished,
105 /// Sent by PreferencesScreen when user discards or closes unchanged.
106 ClosePreferences,
107 /// Sent by VaultSection; PreferencesScreen::handle_app_message intercepts.
108 OpenFileBrowser,
109 /// Sent by IndexingSection; PreferencesScreen intercepts.
110 TriggerFastReindex,
111 TriggerFullReindex,
112 /// Sent by indexing tokio task on completion.
113 IndexingDone(Result<Duration, String>),
114 /// Open (or create) today's journal entry and switch to it in the editor.
115 OpenJournal,
116 /// Dismiss the active editor overlay (note browser, Saved Searches modal,
117 /// or dialog). The single close path for everything owned by `OverlayHost`.
118 CloseOverlay,
119 /// Follow the link under the editor cursor: note name/path or external URL.
120 FollowLink(String),
121 /// Open the search modal pre-filled with `#<name>` to browse notes by label.
122 FollowLabel(String),
123 /// Insert raw text at the editor's cursor (replacing any active selection).
124 /// Used by the screen layer to deliver async results back to the editor —
125 /// e.g. the markdown link generated after a clipboard image is saved as an attachment.
126 InsertAtCursor(String),
127
128 // ── File-operation dialog messages ───────────────────────────────────────
129 /// Request to show the file-operations menu (delete / rename / move).
130 ShowFileOpsMenu(VaultPath),
131 /// Request to show the delete confirmation dialog for the given entry.
132 ShowDeleteDialog(VaultPath),
133 /// Request to show the rename dialog for the given entry.
134 ShowRenameDialog(VaultPath),
135 /// Request to show the move dialog for the given entry.
136 ShowMoveDialog(VaultPath),
137 /// Confirmation that the given entry was successfully deleted.
138 EntryDeleted(VaultPath),
139 /// Confirmation that an entry was successfully renamed.
140 EntryRenamed {
141 from: VaultPath,
142 to: VaultPath,
143 },
144 /// Confirmation that an entry was successfully moved.
145 EntryMoved {
146 from: VaultPath,
147 to: VaultPath,
148 },
149 /// Notification that a note was just created at this path. The current
150 /// screen refreshes its sidebar if it is browsing the note's directory.
151 /// Opening the note is a separate concern (the creator emits `OpenPath`).
152 EntryCreated(VaultPath),
153 /// A dialog operation failed; carries a human-readable error message.
154 DialogError(String),
155
156 /// A vault was found to be structurally unusable (conflicts, invalid layout, etc.).
157 /// Carries a formatted, human-readable error message.
158 ///
159 /// Handled by `handle_app_message` in `main.rs`, which clears the workspace,
160 /// saves settings, and opens the settings screen with an error overlay.
161 /// To add a new conflict source: emit this event from the detection site; no
162 /// other files need to change.
163 VaultConflict(String),
164
165 // ── Dialog async result messages ─────────────────────────────────────────
166 /// Rename dialog: name availability check result.
167 RenameValidation {
168 available: bool,
169 },
170 /// Move dialog: directory list has loaded.
171 MoveDirectoriesLoaded(Vec<VaultPath>),
172 /// Move dialog: fuzzy filter results are ready.
173 MoveFilterResults(Vec<VaultPath>),
174 /// Move dialog: destination existence check result.
175 MoveDestValidation {
176 available: bool,
177 },
178 /// Save-search dialog: existing saved-search names have loaded (drives
179 /// the update/overwrite/save-new hint).
180 SavedSearchNamesLoaded(Vec<String>),
181
182 // ── Workspace messages ──────────────────────────────────────────────
183 /// User switched to a different workspace. Carries the workspace name.
184 /// Handled by main.rs to rebuild the vault and navigate to StartScreen.
185 WorkspaceSwitched(String),
186
187 /// Persist a saved search (emitted by the save-search dialog on submit).
188 /// `source` is the surface the query was sourced from, decided when the
189 /// dialog opened — it drives whether the panel breadcrumb re-pins.
190 SaveSearchConfirmed {
191 name: String,
192 query: String,
193 source: SaveSource,
194 },
195
196 /// A saved search was written to disk (success path of
197 /// `SaveSearchConfirmed`). The editor re-pins the panel breadcrumb here —
198 /// only once the write actually succeeded.
199 SavedSearchPersisted {
200 name: String,
201 query: String,
202 source: SaveSource,
203 },
204
205 /// The background saved-search write failed; surface it to the user.
206 SavedSearchSaveFailed {
207 name: String,
208 },
209
210 /// A saved search was chosen in the Saved Searches modal.
211 SavedSearchSelected {
212 query: String,
213 name: String,
214 },
215
216 /// Sort selection changed in the sort dialog — apply live to `target`.
217 /// When `persist` is set (sidebar's "save as default"), also write the
218 /// choice to settings. `group_directories` is sidebar-only (the query panel
219 /// ignores it).
220 SortChanged {
221 target: SortTarget,
222 field: SortField,
223 order: SortOrder,
224 group_directories: bool,
225 persist: bool,
226 },
227}
228
229impl AppEvent {
230 pub fn send_input(event: InputEvent) -> Self {
231 AppEvent::Input(event)
232 }
233
234 /// `OpenPath` without query emphasis — the common case.
235 pub fn open(path: kimun_core::nfs::VaultPath) -> Self {
236 AppEvent::OpenPath {
237 path,
238 emphasis: None,
239 }
240 }
241}
242
243// ── Input events ────────────────────────────────────────────────────────
244#[derive(Debug, Clone)]
245pub enum InputEvent {
246 Key(KeyEvent),
247 Mouse(MouseEvent),
248 /// Bracketed-paste payload from the terminal. On macOS this is what
249 /// Cmd+V delivers, since the terminal intercepts Cmd combos before they
250 /// reach the TUI. The string may be empty when the clipboard holds only
251 /// non-text content (e.g. an image).
252 Paste(String),
253}
254
255// ── Screen events ────────────────────────────────────────────────────────
256#[derive(Debug, Clone)]
257pub enum ScreenEvent {
258 Start,
259 OpenPreferences,
260 /// Open the guided-setup (onboarding) screen.
261 OpenOnboarding,
262 /// Open the settings screen with an error overlay already shown.
263 OpenPreferencesWithError(String),
264 /// Navigate to the editor for the given vault root path.
265 OpenEditor(Arc<NoteVault>, VaultPath),
266 /// Navigate to the browse screen for the given vault root and directory path.
267 OpenBrowse(Arc<NoteVault>, VaultPath),
268}
269
270/// Convenience alias used throughout the codebase.
271pub type AppTx = UnboundedSender<AppEvent>;
272
273/// Sender helpers for the create-then-open sequence shared by every
274/// note-creation site (create dialog, quick note, note browser, sidebar,
275/// journal).
276pub trait AppTxExt {
277 /// Announce a freshly created note so sidebars browsing its directory
278 /// refresh, then open it. The notification is gated on `created` (an
279 /// already-existing note needs no refresh); the note is opened regardless.
280 fn announce_and_open(&self, path: VaultPath, created: bool);
281}
282
283impl AppTxExt for AppTx {
284 fn announce_and_open(&self, path: VaultPath, created: bool) {
285 if created {
286 self.send(AppEvent::EntryCreated(path.clone())).ok();
287 }
288 self.send(AppEvent::open(path)).ok();
289 }
290}
291
292/// Build a `Send + Sync` callback that fires `AppEvent::Redraw` on the
293/// app event bus. Used by long-lived components (autocomplete query
294/// task, etc.) that need to wake the render loop from a background
295/// thread but should not be aware of `AppEvent` themselves.
296pub fn redraw_callback(tx: AppTx) -> Arc<dyn Fn() + Send + Sync + 'static> {
297 Arc::new(move || {
298 let _ = tx.send(AppEvent::Redraw);
299 })
300}
301
302#[cfg(test)]
303mod tests {
304 use super::*;
305
306 fn _assert_new_variants_exist(e: AppEvent) {
307 match e {
308 AppEvent::ShowDeleteDialog(_) => {}
309 AppEvent::ShowRenameDialog(_) => {}
310 AppEvent::ShowMoveDialog(_) => {}
311 AppEvent::EntryDeleted(_) => {}
312 AppEvent::EntryRenamed { from: _, to: _ } => {}
313 AppEvent::EntryMoved { from: _, to: _ } => {}
314 AppEvent::DialogError(_) => {}
315 _ => {}
316 }
317 }
318
319 #[test]
320 fn sort_events_construct() {
321 use crate::components::file_list::{SortField, SortOrder};
322 let _ = AppEvent::SortChanged {
323 target: SortTarget::Sidebar,
324 field: SortField::Name,
325 order: SortOrder::Ascending,
326 group_directories: true,
327 persist: false,
328 };
329 let _ = AppEvent::SortChanged {
330 target: SortTarget::Query,
331 field: SortField::Title,
332 order: SortOrder::Descending,
333 group_directories: false,
334 persist: true,
335 };
336 }
337}