1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#[allow(clippy::wildcard_imports)]
use super::*;
impl App {
/// The project ids a view's project scope resolves against.
///
/// The TUI has no first-class project registry in this build — the same
/// reason `commands::views::project_directory` returns nothing — so a
/// project-scoped view is refused by the registry's own diagnostic rather
/// than silently accepted. When the project registry lands, only this
/// function changes.
pub(in crate::app) fn view_projects() -> BTreeSet<String> {
BTreeSet::new()
}
/// The repository-owned `tix.toml` a view mutation commits to.
pub(in crate::app) fn view_config_path(&self) -> PathBuf {
self.store
.config_path()
.unwrap_or_else(|_| PathBuf::from(".").join("tix.toml"))
}
/// The board configuration the **live** board is built from: the active
/// board's, with the applied view's visibility snapshot overlaid while that
/// overlay is on (T025).
///
/// Overlaying rather than writing is what keeps "apply a view" read-only
/// (C-005) while still honouring a plan's `hidden_columns`/`show_mismatch`.
pub(in crate::app) fn board_config(&self) -> BoardConfiguration {
match &self.view_session {
Some(session) if session.visibility => session
.marker
.applied_plan()
.effective_board(self.active_board_config()),
_ => self.active_board_config().clone(),
}
}
/// The stable id of the active saved view (WP05, T024), or `None` when no
/// view is associated with the live board.
///
/// Public because the **identity** is what a surface or a test asserts
/// (C-002): the name is a label and renaming a view must not change what is
/// active.
pub fn active_view_id(&self) -> Option<&ViewId> {
self.view_session
.as_ref()
.map(|session| session.marker.view_id())
}
/// The **live** normalized plan (C-006): the criteria in force, the text
/// query (which carries the sort/group control tokens), and the visibility
/// actually applied to the board.
///
/// Visibility is set only when a view's snapshot is overlaid, which mirrors
/// what the plan of a view that leaves visibility alone *means* ("inherit the
/// live board") — so applying such a view reports `in sync` rather than a
/// difference that is not one.
pub fn live_plan(&self) -> ViewPlan {
let mut plan = ViewPlan::from_parts(
self.active_structured_filter().clone(),
self.filter.as_deref().unwrap_or_default(),
);
if let Some(session) = &self.view_session
&& session.visibility
&& let Some(visibility) = session.marker.applied_plan().visibility()
{
plan = plan.with_visibility(visibility.clone());
}
plan
}
/// The plan a **create/update from live** saves: [`Self::live_plan`] plus the
/// visibility the user is actually looking at.
///
/// Unlike divergence (which asks "does the plan still describe the board?"),
/// saving asks "what is on screen?", so an absent snapshot is captured as a
/// real one: `hidden_columns` and `show_mismatch` as configured right now.
pub fn capture_plan(&self) -> ViewPlan {
let board = self.board_config();
self.live_plan().with_visibility(ViewVisibility::new(
board.hidden_columns.clone(),
Some(board.show_mismatch),
))
}
/// The divergence of the live board from the **saved definition** of the
/// active view, or `None` when no view is marked (T026).
///
/// The three cases are the ones `ViewService::divergence` decides — a deleted
/// view and an uninterpretable plan are explicit diagnostics, never a silent
/// `in_sync` — derived here from the registry because rendering borrows
/// `&App`. `tests/tui_state.rs` pins this report against the service's own
/// for the same inputs, so the two cannot drift.
pub fn view_divergence(&self) -> Option<ViewDivergence> {
let session = self.view_session.as_ref()?;
let live = self.live_plan();
Some(
match self.store.config.views.get(session.marker.view_id()) {
None => ViewDivergence::deleted(session.marker.view_id(), &live),
Some(view) => match ViewPlan::from_toml(&view.plan) {
Ok(saved) => session.marker.divergence(&saved, &live),
Err(refusal) => ViewDivergence::unreadable(
session.marker.view_id(),
&live,
refusal.diagnostics().to_vec(),
),
},
},
)
}
/// The board's active-view indicator row (T024), or `None` when no view is
/// marked. The same string the filter modal and the views modal show, so the
/// three surfaces cannot describe one state three ways.
pub fn view_indicator(&self) -> Option<String> {
let session = self.view_session.as_ref()?;
Some(match self.view_divergence() {
Some(divergence) => crate::ui::views::indicator_line(&session.name, &divergence),
None => format!("view {} · (no comparison)", session.name),
})
}
/// The rendered feedback for the **active** view (T026): its scope, its plan,
/// the live-vs-saved divergence, and every diagnostic the registry reports for
/// it.
pub fn active_view_feedback(&self) -> Option<ViewFeedback> {
let id = self.view_session.as_ref()?.marker.view_id().clone();
Some(self.view_feedback_for(&id, true))
}
/// The rendered feedback for one stored view, by stable id (T026).
///
/// Non-mutating by construction: every input is read from the registry or the
/// live state, and the plan is only *parsed* — a malformed one is reported as
/// its refusal rather than repaired.
pub(in crate::app) fn view_feedback_for(&self, id: &ViewId, is_active: bool) -> ViewFeedback {
let Some(view) = self.store.config.views.get(id) else {
let diagnostic = ViewDiagnostic::new(
crate::views::registry::ViewDiagnosticKind::UnknownView,
format!("no saved view with id `{id}`"),
);
return ViewFeedback::new(
format!("view {id} · unknown"),
vec![crate::ui::views::diagnostic_line(&diagnostic)],
);
};
let projects = Self::view_projects();
let mut lines = vec![format!(
"scope: {}",
scope_summary(&view.scope, view.scope.resolution(&projects))
)];
match ViewPlan::from_toml(&view.plan) {
Ok(plan) => {
lines.push(crate::ui::views::plan_summary(&plan));
// Unknown metadata at any plan level, then every field this
// board/category vocabulary rejects — reported, never repaired
// (NFR-002), and never silently applied (a refusal happens on
// apply, where a menu must not explode).
lines.extend(
plan.diagnostics()
.iter()
.map(crate::ui::views::diagnostic_line),
);
if let Err(refusal) = plan.validate(self.active_board_config(), &self.store.config)
{
lines.extend(
refusal
.diagnostics()
.iter()
.map(crate::ui::views::diagnostic_line),
);
}
}
Err(refusal) => lines.extend(
refusal
.diagnostics()
.iter()
.map(crate::ui::views::diagnostic_line),
),
}
let divergence = if is_active {
self.view_divergence()
} else {
None
};
let indicator = match &divergence {
Some(divergence) => crate::ui::views::indicator_line(&view.name, divergence),
None => format!("view {} · id {id}", view.name),
};
if let Some(divergence) = &divergence {
if divergence.in_sync() {
lines.push("live: in sync with the saved plan".to_string());
} else {
lines.push("live: diverged from the saved plan".to_string());
lines.extend(crate::ui::views::difference_lines(divergence));
lines.extend(
divergence
.diagnostics()
.iter()
.map(crate::ui::views::diagnostic_line),
);
}
}
// Scope conflicts, missing projects, and preserved unknown metadata are
// reported for a record the registry already holds — reported, never
// silently rewritten (NFR-002/C-003).
lines.extend(
self.store
.config
.views
.diagnostics(&projects)
.into_iter()
.filter(|diagnostic| diagnostic.view_id() == Some(id))
.map(|diagnostic| crate::ui::views::diagnostic_line(&diagnostic)),
);
ViewFeedback::new(indicator, lines)
}
/// Open the saved-views modal (`v`).
pub(in crate::app) fn open_views(&mut self) {
self.views = Some(ViewsModal::new(self.view_rows()));
self.refresh_views_feedback();
self.mode = Mode::Views;
}
/// One row per stored view, in registry order, with the active view marked.
pub(crate) fn view_rows(&self) -> Vec<ViewRow> {
let projects = Self::view_projects();
let active = self
.view_session
.as_ref()
.map(|session| session.marker.view_id().clone());
self.store
.config
.views
.iter()
.map(|view| ViewRow {
active: active.as_ref() == Some(&view.id),
id: view.id.clone(),
name: view.name.clone(),
scope: scope_summary(&view.scope, view.scope.resolution(&projects)),
})
.collect()
}
/// Re-read the registry into the modal, keeping the cursor on the same
/// **stable id** where it still exists — so a rename or a duplicate leaves the
/// cursor where the user put it.
pub(in crate::app) fn refresh_views_rows(&mut self) {
let rows = self.view_rows();
let Some(modal) = self.views.as_mut() else {
return;
};
let previous = modal.selected_row().map(|row| row.id.clone());
modal.rows = rows;
if let Some(id) = previous
&& let Some(index) = modal.rows.iter().position(|row| row.id == id)
{
modal.selected = index;
}
modal.move_selected(0);
}
/// Recompute the feedback panel for the row under the cursor (T026).
pub(in crate::app) fn refresh_views_feedback(&mut self) {
let selected = self
.views
.as_ref()
.and_then(|modal| modal.selected_row())
.map(|row| (row.id.clone(), row.active));
let feedback = selected.map(|(id, active)| self.view_feedback_for(&id, active));
if let Some(modal) = self.views.as_mut() {
modal.feedback = feedback;
}
}
/// Record the outcome of one view operation on the modal's note line and the
/// status bar, then re-read the registry.
///
/// A **refusal** is a decision, not a crash: it is rendered through the
/// registry's own refusal text and nothing is written (C-008). The note is
/// shown on the modal rather than only in the status bar, because the status
/// bar is one truncated row and a reason is worth reading.
pub(in crate::app) fn note_view_outcome(&mut self, note: String) {
if let Some(modal) = self.views.as_mut() {
modal.note = Some(note.clone());
}
self.status_msg = Some(note);
}
/// The note for a refused or failed view write.
pub(in crate::app) fn view_write_note(error: &ViewWriteError) -> String {
match error {
ViewWriteError::Refused(refusal) => refusal.render(),
ViewWriteError::Failed(error) => error.to_string(),
}
}
/// Apply the view under the cursor, or the one named by `id` (T025).
pub(crate) fn apply_view(&mut self, reference: &str) -> Result<Action> {
let path = self.view_config_path();
let projects = Self::view_projects();
let board_config = self.board_config();
let resolution = self.workflow_resolution();
// The service borrows `store.config` mutably (an accepted *mutation* is a
// config change); `apply` only reads, and borrows the corpus and the
// board. Disjoint fields, one scope.
let outcome = {
let service = ViewService::new(&mut self.store.config, &projects, &path);
service.apply(reference, &board_config, &self.corpus, &resolution)
};
match outcome {
Ok(result) => {
let plan = result.plan().clone();
let id = result.view_id().cloned();
let name = id
.as_ref()
.and_then(|id| self.store.config.views.get(id))
.map_or_else(|| reference.to_string(), |view| view.name.clone());
// The marker goes on **before** the rebuild: the live board must be
// built with the applied plan's visibility overlay, and the
// indicator/divergence the rebuild refreshes describe the state the
// apply just installed.
if let Some(id) = id {
self.view_session = Some(ViewSession {
visibility: plan.visibility().is_some(),
marker: ActiveViewMarker::new(id, plan.clone(), Some(ViewSurface::Tui)),
name: name.clone(),
});
}
self.install_live_plan(&plan);
self.refresh_views_rows();
self.refresh_views_feedback();
self.sync_filter_modal_feedback();
self.note_view_outcome(format!("Applied view '{name}'"));
}
Err(refusal) => {
// Refused: nothing applied, no issue touched, the live filter
// state left exactly as it was (C-005/C-008).
self.sync_filter_modal_feedback();
self.note_view_outcome(refusal.render());
}
}
Ok(Action::None)
}
/// Install an applied plan into the **live** filter state through the
/// existing query path (T025).
///
/// The typed criteria become the committed structured filter, and the plan's
/// query — free text plus its `sort:`/`group:` control tokens, re-spelled by
/// the plan itself rather than here — is what `apply_filter` replays. So the
/// board after an apply is the board the existing path builds, which is the
/// same path (and the same result) the service used.
pub(in crate::app) fn install_live_plan(&mut self, plan: &ViewPlan) {
let selected = self
.board
.get_selected_card()
.map(|card| card.filename.clone());
self.structured = plan.filter().clone();
let query = plan.effective_query();
self.apply_filter(&query);
// Keep the cursor on the card the user was standing on when that card
// survives the new plan, exactly as a board switch does: an apply that
// silently jumped the cursor to the first column would be a navigation
// change the user did not ask for (T025).
if let Some(filename) = selected {
self.select_card_by_filename(&filename);
}
}
/// Keep the filter modal's active-view indicator in step with live state
/// (T026).
///
/// Called from the single funnel every live change passes through
/// ([`Self::rebuild_board`]), so the indicator inside the modal cannot
/// describe a board the user has already changed.
pub(in crate::app) fn sync_filter_modal_feedback(&mut self) {
if self.filter_modal.is_none() {
return;
}
let active = self
.view_session
.as_ref()
.map(|session| session.marker.clone());
let feedback = if active.is_some() {
self.active_view_feedback()
} else {
None
};
if let Some(modal) = self.filter_modal.as_mut() {
modal.set_active(active, feedback);
}
}
/// Create a view from the live plan (T027).
///
/// The scope is **explicit**: the TUI creates repository-scoped views. It has
/// no project registry to scope against and no host identity to share with,
/// and inventing either is an escalation trigger (C-003/C-009); project and
/// user/share scopes are managed by `tix view share`.
pub(in crate::app) fn create_view_from_live(&mut self, name: String) {
let definition = ViewDefinition::new(name, ViewScope::repository(), self.capture_plan());
let path = self.view_config_path();
let projects = Self::view_projects();
let outcome = {
let mut service = ViewService::new(&mut self.store.config, &projects, &path);
service.create(definition)
};
match outcome {
Ok(outcome) => {
let audit = outcome.audit().map(|event| event.view_name().to_string());
let id = outcome.value().clone();
self.refresh_views_rows();
// Select the row just created, so the next action applies to what
// the user just made rather than to whatever was under the cursor —
// and close the prompt only now, so a refusal left the typed name
// in place to be corrected.
if let Some(modal) = self.views.as_mut() {
modal.clear_prompt();
if let Some(index) = modal.rows.iter().position(|row| row.id == id) {
modal.selected = index;
}
}
self.refresh_views_feedback();
self.note_view_outcome(format!(
"Created {id}{}",
audit.map_or_else(String::new, |name| format!(" '{name}'"))
));
}
Err(error) => {
let note = Self::view_write_note(&error);
self.note_view_outcome(note);
}
}
}
/// Rename the view under the cursor (T027).
///
/// `ViewService::update` replaces the whole definition, so the stored scope
/// and plan are carried over from `show` — a rename can never drop either, and
/// the stable id is untouched (C-002).
pub(in crate::app) fn rename_view(&mut self, id: &ViewId, name: String) {
let path = self.view_config_path();
let projects = Self::view_projects();
let outcome = {
let mut service = ViewService::new(&mut self.store.config, &projects, &path);
match service.show(id.as_str()) {
Ok(detail) => {
let definition = ViewDefinition::new(
name,
detail.view().scope.clone(),
detail.plan().clone(),
);
service.update(id.as_str(), definition).map(|outcome| {
(
outcome.is_noop(),
outcome.value().clone(),
outcome.audit().map(|event| event.view_name().to_string()),
)
})
}
Err(refusal) => Err(ViewWriteError::Refused(refusal)),
}
};
match outcome {
Ok((noop, id, name)) => {
if let Some(modal) = self.views.as_mut() {
modal.clear_prompt();
}
self.refresh_views_rows();
self.refresh_views_feedback();
let word = if noop { "Unchanged" } else { "Renamed" };
self.note_view_outcome(format!(
"{word} {id}{}",
name.map_or_else(String::new, |name| format!(" '{name}'"))
));
}
Err(error) => {
let note = Self::view_write_note(&error);
self.note_view_outcome(note);
}
}
}
/// Replace the view under the cursor's plan with the live plan (T027) — the
/// "save what I am looking at into this view" action, the counterpart of
/// create.
pub(in crate::app) fn update_view_from_live(&mut self, id: &ViewId) {
let plan = self.capture_plan();
let path = self.view_config_path();
let projects = Self::view_projects();
let outcome = {
let mut service = ViewService::new(&mut self.store.config, &projects, &path);
match service.show(id.as_str()) {
Ok(detail) => {
let definition = ViewDefinition::new(
detail.view().name.clone(),
detail.view().scope.clone(),
plan,
);
service.update(id.as_str(), definition).map(|outcome| {
(
outcome.is_noop(),
outcome.value().clone(),
outcome.audit().map(|event| event.view_name().to_string()),
)
})
}
Err(refusal) => Err(ViewWriteError::Refused(refusal)),
}
};
match outcome {
Ok((noop, id, name)) => {
self.refresh_views_rows();
self.refresh_views_feedback();
let word = if noop { "Unchanged" } else { "Updated" };
self.note_view_outcome(format!(
"{word} {id}{}",
name.map_or_else(String::new, |name| format!(" '{name}'"))
));
}
Err(error) => {
let note = Self::view_write_note(&error);
self.note_view_outcome(note);
}
}
}
/// Duplicate the view under the cursor under a fresh stable id (T027).
pub(in crate::app) fn duplicate_view(&mut self, id: &ViewId) {
let path = self.view_config_path();
let projects = Self::view_projects();
let outcome = {
let mut service = ViewService::new(&mut self.store.config, &projects, &path);
service.duplicate(id.as_str(), None)
};
match outcome {
Ok(outcome) => {
let copy = outcome.value().clone();
self.refresh_views_rows();
if let Some(modal) = self.views.as_mut()
&& let Some(index) = modal.rows.iter().position(|row| row.id == copy)
{
modal.selected = index;
}
self.refresh_views_feedback();
self.note_view_outcome(format!("Duplicated {id} -> {copy}"));
}
Err(error) => {
let note = Self::view_write_note(&error);
self.note_view_outcome(note);
}
}
}
/// Delete the view under the cursor's **definition**, after confirmation
/// (T027/C-005).
///
/// Only the definition goes: issues, comments, relationships, and the live
/// filter state are all untouched. If the deleted view was the active one the
/// marker is cleared — the association is gone with the definition — and the
/// board says so, rather than silently keeping a marker that names nothing.
pub(in crate::app) fn delete_view(&mut self, id: &ViewId) {
let path = self.view_config_path();
let projects = Self::view_projects();
let outcome = {
let mut service = ViewService::new(&mut self.store.config, &projects, &path);
service.delete(id.as_str(), DeleteConfirmed::confirmed())
};
match outcome {
Ok(outcome) => {
let removed = outcome.value();
let name = removed.view().name.clone();
// The service built above is ephemeral (`active: None`), so its
// `marker_cleared()` is always false; ask the App's own marker
// instead, or a deleted active view leaves a dangling id.
let cleared = self.active_view_id() == Some(id);
if cleared {
self.view_session = None;
}
self.refresh_views_rows();
self.refresh_views_feedback();
self.sync_filter_modal_feedback();
self.note_view_outcome(if cleared {
format!("Deleted '{name}' (definition only; active marker cleared)")
} else {
format!("Deleted '{name}' (definition only)")
});
}
Err(error) => {
let note = Self::view_write_note(&error);
self.note_view_outcome(note);
}
}
}
/// The saved-views modal's keys (WP05, T024–T027).
///
/// Two layers, exactly as the settings modal has: an open prompt takes the key
/// first, and the list has it otherwise. Every branch either moves a cursor
/// that belongs to *this* modal, calls the shared service, or applies through
/// [`Self::apply_view`]; nothing here writes an issue.
#[allow(clippy::too_many_lines)]
pub(in crate::app) fn handle_views_key(&mut self, key: KeyEvent) -> Result<Action> {
if self.views.is_none() {
self.mode = Mode::Board;
return Ok(Action::None);
}
let prompt = self.views.as_ref().and_then(|modal| modal.prompt.clone());
match prompt {
Some(ViewsPrompt::Create(buffer)) => match key.code {
KeyCode::Esc => {
if let Some(modal) = self.views.as_mut() {
modal.clear_prompt();
}
}
KeyCode::Enter => self.create_view_from_live(buffer),
KeyCode::Backspace => {
if let Some(modal) = self.views.as_mut()
&& let Some(ViewsPrompt::Create(buffer)) = modal.prompt.as_mut()
{
buffer.pop();
}
}
KeyCode::Char(c) => {
if let Some(modal) = self.views.as_mut()
&& let Some(ViewsPrompt::Create(buffer)) = modal.prompt.as_mut()
{
buffer.push(c);
}
}
_ => {}
},
Some(ViewsPrompt::Rename { id, buffer }) => match key.code {
KeyCode::Esc => {
if let Some(modal) = self.views.as_mut() {
modal.clear_prompt();
}
}
KeyCode::Enter => self.rename_view(&id, buffer),
KeyCode::Backspace => {
if let Some(modal) = self.views.as_mut()
&& let Some(ViewsPrompt::Rename { buffer, .. }) = modal.prompt.as_mut()
{
buffer.pop();
}
}
KeyCode::Char(c) => {
if let Some(modal) = self.views.as_mut()
&& let Some(ViewsPrompt::Rename { buffer, .. }) = modal.prompt.as_mut()
{
buffer.push(c);
}
}
_ => {}
},
// The confirmation borrows the shared `ConfirmDelete` context, whose
// canonical confirm key is `y` and whose cancel is `esc`.
Some(ViewsPrompt::ConfirmDelete { id, .. }) => {
if let Some(modal) = self.views.as_mut() {
modal.clear_prompt();
}
match key.code {
KeyCode::Char('y') => self.delete_view(&id),
KeyCode::Esc => self.refresh_views_feedback(),
_ => {}
}
}
None => match key.code {
KeyCode::Char('q') | KeyCode::Char('Q') | KeyCode::Esc => {
self.views = None;
self.mode = Mode::Board;
}
KeyCode::Up | KeyCode::Char('k') => {
if let Some(modal) = self.views.as_mut() {
modal.move_selected(-1);
// The note describes the last operation, not the row under
// the cursor: moving on retires it rather than letting it
// read as a fact about the newly selected view.
modal.note = None;
}
self.refresh_views_feedback();
}
KeyCode::Down | KeyCode::Char('j') => {
if let Some(modal) = self.views.as_mut() {
modal.move_selected(1);
modal.note = None;
}
self.refresh_views_feedback();
}
KeyCode::Enter => {
if let Some(id) = self
.views
.as_ref()
.and_then(|modal| modal.selected_row())
.map(|row| row.id.clone())
{
self.apply_view(id.as_str())?;
}
}
KeyCode::Char('n') => {
if let Some(modal) = self.views.as_mut() {
modal.note = None;
modal.prompt = Some(ViewsPrompt::Create(String::new()));
}
}
KeyCode::Char('e') => {
let selected = self
.views
.as_ref()
.and_then(|modal| modal.selected_row())
.map(|row| (row.id.clone(), row.name.clone()));
if let Some((id, name)) = selected
&& let Some(modal) = self.views.as_mut()
{
modal.note = None;
modal.prompt = Some(ViewsPrompt::Rename { id, buffer: name });
}
}
KeyCode::Char('u') => {
let selected = self
.views
.as_ref()
.and_then(|modal| modal.selected_row())
.map(|row| row.id.clone());
if let Some(id) = selected {
self.update_view_from_live(&id);
}
}
KeyCode::Char('d') => {
let selected = self
.views
.as_ref()
.and_then(|modal| modal.selected_row())
.map(|row| row.id.clone());
if let Some(id) = selected {
self.duplicate_view(&id);
}
}
KeyCode::Char('x') | KeyCode::Char('X') => {
let selected = self
.views
.as_ref()
.and_then(|modal| modal.selected_row())
.map(|row| (row.id.clone(), row.name.clone()));
if let Some((id, name)) = selected
&& let Some(modal) = self.views.as_mut()
{
modal.note = None;
modal.prompt = Some(ViewsPrompt::ConfirmDelete { id, name });
}
}
_ => {}
},
}
Ok(Action::None)
}
/// The saved-views modal's pointer handling (WP05, T025).
///
/// A left click on a row selects that row and runs the same Enter action the
/// keyboard would — the D6 rule the settings modal's rows follow. The row
/// geometry comes from the widget's own [`ViewsModal::row_hit_regions`], so the
/// click cannot drift from what was drawn. Clicks land here rather than
/// through a recorded region because the effect (an apply) belongs to this
/// handler, exactly as card selection belongs to `handle_board_mouse`.
pub(in crate::app) fn handle_views_mouse(&mut self, mouse: MouseEvent) -> Result<Action> {
if !matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) {
return Ok(Action::None);
}
if self.views.as_ref().is_some_and(ViewsModal::is_prompting) {
return Ok(Action::None);
}
let area = self.frame_size.get();
let hit = self.views.as_ref().and_then(|modal| {
modal
.row_hit_regions(area)
.into_iter()
.find(|(rect, _)| rect.contains(Position::new(mouse.column, mouse.row)))
.map(|(_, index)| index)
});
let Some(index) = hit else {
return Ok(Action::None);
};
let selected = {
let Some(modal) = self.views.as_mut() else {
return Ok(Action::None);
};
modal.move_selected_to(index);
modal.selected_row().map(|row| row.id.clone())
};
if let Some(id) = selected {
self.apply_view(id.as_str())?;
}
Ok(Action::None)
}
}