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
#[allow(clippy::wildcard_imports)]
use super::*;
/// An open value picker inside the filter modal: the reused [`DropdownState`] and
/// the criterion row it is choosing for. `Some` exactly while a list overlays the
/// modal; at most one exists at a time.
///
/// App-owned rather than a field on [`crate::ui::filter_modal::FilterModal`] for the
/// same reason [`FormPicker`] is: the widget stays a pure view+model, and the
/// transient list, its key routing and its option sourcing live where the store and
/// config are reachable.
#[derive(Debug, Clone)]
pub(in crate::app) struct FilterPicker {
pub(in crate::app) field: FilterField,
pub(in crate::app) state: DropdownState,
}
impl App {
pub(in crate::app) fn handle_filter_key(&mut self, key: KeyEvent) -> Result<Action> {
match key.code {
KeyCode::Char('q') | KeyCode::Char('Q') | KeyCode::Esc => {
self.filter = None;
self.board = BoardState::build_with(
&self.store,
self.active_board_config(),
false,
self.show_children,
);
self.status_msg = None;
self.mode = Mode::Board;
}
// Commit the search and, when it narrowed the board to a single
// displayed issue, open that issue's action menu immediately
// (tix #1744) — the same `Enter` the board itself binds. A search
// with zero or several results just returns to the board, where the
// cursor decides as before.
KeyCode::Enter => {
self.mode = Mode::Board;
if self.board.sole_visible_card().is_some() {
return self.open_card_action_menu();
}
}
KeyCode::Char(c) => {
let mut next = match &self.mode {
Mode::Filter { buffer } => buffer.clone(),
_ => String::new(),
};
next.push(c);
self.mode = Mode::Filter {
buffer: next.clone(),
};
self.apply_filter(&next);
}
KeyCode::Backspace => {
let mut next = match &self.mode {
Mode::Filter { buffer } => buffer.clone(),
_ => String::new(),
};
next.pop();
self.mode = Mode::Filter {
buffer: next.clone(),
};
self.apply_filter(&next);
}
_ => {}
}
Ok(Action::None)
}
/// The structured filter to apply right now: the live in-progress criteria
/// while the modal is open, otherwise the committed one.
pub(in crate::app) fn active_structured_filter(&self) -> &search::StructuredFilter {
self.filter_modal
.as_ref()
.map(|m| m.active_filter())
.unwrap_or(&self.structured)
}
/// Open the structured filter modal seeded from the committed filter.
pub(in crate::app) fn open_filter_modal(&mut self) {
let active = self
.view_session
.as_ref()
.map(|session| session.marker.clone());
let feedback = if active.is_some() {
self.active_view_feedback()
} else {
None
};
let mut modal = crate::ui::filter_modal::FilterModal::with_filter(self.structured.clone());
modal.set_active(active, feedback);
self.filter_modal = Some(modal);
self.filter_picker = None;
self.mode = Mode::FilterModal;
}
/// Open the focused criterion's value picker: a single-select list of the
/// values that criterion can hold, or the calendar on a date row.
///
/// Criterion rows are picker-only, so this is the one way a value is entered.
/// The list starts on the criterion's current value when it has one.
pub(in crate::app) fn open_filter_picker(&mut self) {
let Some(modal) = self.filter_modal.as_ref() else {
return;
};
let field = modal.focus();
if field.is_date() {
self.open_date_picker(Mode::FilterModal);
return;
}
let current = modal.focused_value();
let mut state = DropdownState::single(self.filter_picker_options(field));
state.seed_single(¤t);
self.filter_picker = Some(FilterPicker { field, state });
}
/// The known values for one criterion, most-trusted source first, each once.
///
/// Every list ends with the criterion's current value (see
/// [`push_current_option`]), which is what makes removing free text safe: a value
/// a user could previously type and have match is still reachable.
#[allow(clippy::too_many_lines)]
pub(in crate::app) fn filter_picker_options(&self, field: FilterField) -> Vec<PickerItem> {
let current = self
.filter_modal
.as_ref()
.map(|modal| modal.focused_value())
.unwrap_or_default();
match field {
// Storage folders are the ground truth for who has issues; the shared
// triage helper adds configured team members and the current value.
FilterField::Assignee => {
crate::ui::triage::assignee_options(&self.store, Some(¤t))
}
// Configured team members, then every reviewer an issue already carries.
FilterField::Reviewer => {
let mut out = Vec::new();
let mut seen = std::collections::BTreeSet::new();
let mut push = |value: &str, out: &mut Vec<PickerItem>| {
let value = value.trim();
if value.is_empty() || !seen.insert(value.to_string()) {
return;
}
out.push(PickerItem {
id: value.to_string(),
label: value.to_string(),
});
};
for team in self.store.config.teams.values() {
for member in &team.members {
push(member, &mut out);
}
}
for issue in self.store.list().unwrap_or_default() {
if let Some(reviewer) = issue.reviewer.as_deref() {
push(reviewer, &mut out);
}
}
push_current_option(&mut out, ¤t);
out
}
// Exactly the active board's columns, in board order.
FilterField::Status => {
let columns = self.active_board_config().columns.clone();
let mut out = crate::ui::triage::status_options(&columns);
push_current_option(&mut out, ¤t);
out
}
// The built-ins first, then every priority the store already uses — the
// same "defaults plus what the project actually uses" rule the form's
// priority picker follows.
FilterField::Priority => {
const BUILT_INS: [&str; 3] = ["low", "medium", "high"];
let mut out: Vec<PickerItem> = BUILT_INS
.iter()
.map(|priority| PickerItem {
id: (*priority).to_string(),
label: (*priority).to_string(),
})
.collect();
for issue in self.store.list().unwrap_or_default() {
let priority = issue.priority.to_string();
if !priority.trim().is_empty() && !out.iter().any(|o| o.id == priority) {
out.push(PickerItem {
id: priority.clone(),
label: priority,
});
}
}
push_current_option(&mut out, ¤t);
out
}
// Every label in use, de-duplicated.
FilterField::Label => {
let mut labels = std::collections::BTreeSet::new();
for issue in self.store.list().unwrap_or_default() {
for label in issue.labels {
if !label.trim().is_empty() {
labels.insert(label);
}
}
}
let mut out: Vec<PickerItem> = labels
.into_iter()
.map(|label| PickerItem {
id: label.clone(),
label,
})
.collect();
push_current_option(&mut out, ¤t);
out
}
FilterField::Cycle => {
let root = self.project_root();
let planner = Planner::from_config(self.store.config.clone(), root);
let mut out: Vec<PickerItem> = planner
.cycle_names()
.into_iter()
.map(|name| PickerItem {
id: name.clone(),
label: name,
})
.collect();
push_current_option(&mut out, ¤t);
out
}
FilterField::Milestone => {
let root = self.project_root();
let planner = Planner::from_config(self.store.config.clone(), root);
let mut out: Vec<PickerItem> = planner
.milestone_names()
.unwrap_or_default()
.into_iter()
.map(|name| PickerItem {
id: name.clone(),
label: name,
})
.collect();
push_current_option(&mut out, ¤t);
out
}
// Every issue is a candidate parent; the criterion stores the canonical
// identity and the label carries the human number and title.
FilterField::Parent => {
let mut out: Vec<PickerItem> = self
.store
.list()
.unwrap_or_default()
.into_iter()
.map(|issue| PickerItem {
id: crate::id::canonical_ref(&issue).to_string(),
label: format!("#{} {}", issue.number, issue.title),
})
.collect();
push_current_option(&mut out, ¤t);
out
}
// The project's own closed vocabulary, in its declared order.
FilterField::Category => {
let mut out: Vec<PickerItem> = self
.store
.config
.categories
.order
.iter()
.map(|key| PickerItem {
id: key.clone(),
label: key.clone(),
})
.collect();
push_current_option(&mut out, ¤t);
out
}
// Date rows open the calendar, never a list; unreachable in practice
// because `open_filter_picker` routes them to the date picker first.
FilterField::StartDate | FilterField::DueDate => Vec::new(),
// WP06 T026: the service's project list, exactly as the form's picker
// lists it — id for the criterion's value, the shared `name (id)` form
// for the label — so `project:` in the modal and `project:` in a query
// mean the same set.
FilterField::Project => {
let mut options: Vec<PickerItem> = self
.live_projects()
.into_iter()
.map(|project| PickerItem {
id: project.id.to_string(),
label: crate::projects::project_display(&project.id, &project.name),
})
.collect();
push_current_option(&mut options, ¤t);
options
}
// WP06 T028/T029: the service's epic list, exactly as the form's picker
// lists it — id for the criterion's value, the shared `name (id)` form
// for the label — so `epic:` in the modal and `epic:` in a query mean the
// same set.
FilterField::Epic => {
let mut options: Vec<PickerItem> = self
.live_epics()
.into_iter()
.map(|epic| PickerItem {
id: epic.id.to_string(),
label: crate::epics::epic_display(&epic.id, &epic.name),
})
.collect();
push_current_option(&mut options, ¤t);
options
}
}
}
/// Write one criterion's value into the open modal's in-progress filter and
/// re-render the board live — the preview the spec requires before commit. An
/// empty `value` clears the criterion.
pub(in crate::app) fn set_filter_criterion(&mut self, field: FilterField, value: &str) {
let Some(modal) = self.filter_modal.as_mut() else {
return;
};
let filter = modal.active_filter_mut();
if value.is_empty() {
field.unset(filter);
} else {
*field.value_mut(filter) = value.to_string();
}
self.rebuild_board();
}
/// Keys for the open value picker. Called **before** the binding layer
/// canonicalizes (see [`App::handle_key`]), so the list sees the user's real key:
/// `l`, `enter` and `F2` would otherwise rewrite to the modal's open-picker
/// action and the list could neither be filtered nor chosen.
pub(in crate::app) fn handle_filter_picker_key(&mut self, key: KeyEvent) -> Result<Action> {
// A filter lists values that exist; `+`/Insert would offer to invent one,
// which is not what a filter is for. Swallow it before `DropdownState` turns
// it into an inline-create prompt.
if matches!(key.code, KeyCode::Char('+') | KeyCode::Insert)
&& self
.filter_picker
.as_ref()
.is_some_and(|picker| picker.state.mode != PickerMode::InlineCreate)
{
return Ok(Action::None);
}
let event = self
.filter_picker
.as_mut()
.map(|picker| (picker.field, picker.state.handle_key(key.code)));
let Some((field, event)) = event else {
return Ok(Action::None);
};
match event {
PickerEvent::None => {}
PickerEvent::Cancelled => self.filter_picker = None,
PickerEvent::Selected(ids) => {
self.filter_picker = None;
let value = ids.first().map(String::as_str).unwrap_or("");
self.set_filter_criterion(field, value);
}
// Inline create is refused above, so a `Created` event cannot arise.
PickerEvent::Created(_) => {}
}
Ok(Action::None)
}
/// Run a filter-modal pointer action recorded during the draw. Public so the
/// hit-region tests drive the same path the run loop does.
pub fn apply_filter_modal_action(&mut self, action: Action) -> Result<Action> {
match action {
// A row click focuses it and opens what the key would: the calendar on a
// date row, the value list otherwise.
Action::FilterModalFocus(field) => {
if let Some(modal) = self.filter_modal.as_mut() {
modal.focus_field(field);
}
self.open_filter_picker();
}
Action::FilterPickerClose => self.filter_picker = None,
Action::FilterPickerOption(row) => {
let chosen = self.filter_picker.as_ref().and_then(|picker| {
let index = *picker.state.visible_indices().get(row)?;
let value = picker.state.options.get(index)?.id.clone();
Some((picker.field, value))
});
self.filter_picker = None;
if let Some((field, value)) = chosen {
self.set_filter_criterion(field, &value);
}
}
_ => {}
}
Ok(Action::None)
}
/// Apply the modal's in-progress criteria to the board and close the modal.
///
/// The modal is **ad-hoc**: a selection takes effect on the board the moment it
/// is made, and closing **keeps** it. Both exits land here — `esc` and the
/// explicit apply key — because there is no discard path left to give `esc` a
/// different meaning. `r` clears every criterion and the cleared set is what the
/// next close persists.
pub(in crate::app) fn close_filter_modal_applying(&mut self) {
let mut filter = self
.filter_modal
.as_ref()
.map(|modal| modal.active_filter().clone())
.unwrap_or_default();
filter.normalize();
self.structured = filter;
self.filter_modal = None;
self.filter_picker = None;
self.mode = Mode::Board;
self.rebuild_board();
}
/// FilterModal keys, as the binding layer canonicalizes them: `Tab`/`Down` and
/// `BackTab`/`Up` move between criterion rows; `r` resets every criterion
/// (FR-014); `F(2)` — what `enter`, `l`, `right` and `F2` all rewrite to — opens
/// the focused row's value list or the calendar; `Esc` and the apply key (`a`,
/// what `Enter` here rewrites to) both close the modal with the current criteria
/// applied. Rows take no typed text: the open picker's handler owns typing. All
/// criteria run over the corpus in one pass as the query does (NFR-001) via
/// [`BoardState::build_filtered`].
pub(in crate::app) fn handle_filter_modal_key(&mut self, key: KeyEvent) -> Result<Action> {
if self.filter_modal.is_none() {
self.mode = Mode::Board;
return Ok(Action::None);
}
match key.code {
KeyCode::Esc | KeyCode::Enter => self.close_filter_modal_applying(),
KeyCode::Tab | KeyCode::Down => {
if let Some(modal) = self.filter_modal.as_mut() {
modal.focus_next();
}
}
KeyCode::BackTab | KeyCode::Up => {
if let Some(modal) = self.filter_modal.as_mut() {
modal.focus_prev();
}
}
// The binding layer rewrites `r` and `R` to this one event.
KeyCode::Char('r') => {
if let Some(modal) = self.filter_modal.as_mut() {
modal.clear();
}
self.rebuild_board();
}
KeyCode::F(2) => self.open_filter_picker(),
_ => {}
}
Ok(Action::None)
}
}