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
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use super::*;
impl App {
#[expect(
clippy::too_many_lines,
reason = "the exhaustive modal match keeps each state transition together"
)]
pub(super) fn handle_picker_modal_key(
&mut self,
mut modal: Modal,
key: KeyEvent,
now: Instant,
) -> Vec<AppEffect> {
let mut effects = Vec::new();
match &mut modal {
Modal::Projects {
groups,
selected,
query,
loading,
} => {
if key.code == KeyCode::Esc {
return effects;
}
let visible = Self::filtered_project_rows(groups, &query.value);
let selected_tree = visible
.get(*selected)
.and_then(|(group_index, tree_index)| {
groups
.get(*group_index)
.and_then(|group| group.worktrees.get(*tree_index))
.cloned()
});
let selected_group = visible
.get(*selected)
.and_then(|(group_index, _)| groups.get(*group_index).cloned());
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
*selected = previous_list_index(*selected, visible.len());
}
KeyCode::Down | KeyCode::Char('j') => {
*selected = next_list_index(*selected, visible.len());
}
KeyCode::Enter if !*loading => {
if let Some(tree) = selected_tree.filter(|tree| !tree.current) {
effects.push(AppEffect::OpenRepository(tree.path));
}
return effects;
}
KeyCode::Delete if !*loading => {
if let Some(group) = selected_group
.filter(|group| !group.worktrees.iter().any(|tree| tree.current))
{
crate::state::forget_recent_project(&group.common_dir);
self.request_recent_projects(&mut effects);
}
self.modal = Some(modal);
return effects;
}
_ => {
edit_text(query, key, false);
*selected = 0;
}
}
self.modal = Some(modal);
}
Modal::PullRequestRepositories {
items,
selected,
query,
loading,
} => {
if key.code == KeyCode::Esc {
return effects;
}
let visible = Self::filtered_github_repositories(items, &query.value);
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
*selected = previous_list_index(*selected, visible.len());
}
KeyCode::Down | KeyCode::Char('j') => {
*selected = next_list_index(*selected, visible.len());
}
KeyCode::Enter if !*loading => {
if let Some(repository) = visible
.get(*selected)
.and_then(|index| items.get(*index))
.cloned()
{
self.pull_request_repository = Some(repository);
if let Some(number) = self.pull_request_exact_number.or_else(|| {
self.pull_request_lookup.value.trim().parse::<u64>().ok()
}) {
self.request_pull_request_lookup(
number,
false,
false,
&mut effects,
);
}
}
return effects;
}
_ => {
edit_text(query, key, false);
*selected = 0;
}
}
self.modal = Some(modal);
}
Modal::CommandPalette { query, selected } => {
if key.code == KeyCode::Esc {
return effects;
}
let commands = self.palette_commands(&query.value);
match key.code {
KeyCode::Up => *selected = previous_list_index(*selected, commands.len()),
KeyCode::Down => *selected = next_list_index(*selected, commands.len()),
KeyCode::Enter => {
if let Some(command) = commands.get(*selected).copied() {
self.execute_palette(command, &mut effects, now);
}
return effects;
}
_ => {
edit_text(query, key, false);
*selected = 0;
}
}
self.modal = Some(modal);
}
Modal::Themes { selected, original } => {
match key.code {
KeyCode::Esc => {
self.apply_theme(*original);
return effects;
}
KeyCode::Up | KeyCode::Char('k') => {
*selected = previous_list_index(*selected, ThemeName::ALL.len());
if let Some(name) = ThemeName::ALL.get(*selected).copied() {
self.apply_theme(name);
}
}
KeyCode::Down | KeyCode::Char('j') => {
*selected = next_list_index(*selected, ThemeName::ALL.len());
if let Some(name) = ThemeName::ALL.get(*selected).copied() {
self.apply_theme(name);
}
}
KeyCode::Enter => {
if let Some(name) = ThemeName::ALL.get(*selected).copied() {
self.apply_theme(name);
self.show_toast(
format!("Theme changed to {}", name.label()),
ToastLevel::Success,
now,
);
}
return effects;
}
_ => {}
}
self.modal = Some(modal);
}
Modal::Appearances {
selected,
original_choice,
original_appearance,
} => {
match key.code {
KeyCode::Esc => {
self.appearance_choice = *original_choice;
self.appearance = *original_appearance;
self.theme = Theme::new(self.theme_name, self.appearance);
return effects;
}
KeyCode::Up | KeyCode::Char('k') => {
*selected = previous_list_index(*selected, AppearanceChoice::ALL.len());
if let Some(choice) = AppearanceChoice::ALL.get(*selected).copied() {
self.set_theme_selection(self.theme_name, choice);
}
}
KeyCode::Down | KeyCode::Char('j') => {
*selected = next_list_index(*selected, AppearanceChoice::ALL.len());
if let Some(choice) = AppearanceChoice::ALL.get(*selected).copied() {
self.set_theme_selection(self.theme_name, choice);
}
}
KeyCode::Enter => {
if let Some(choice) = AppearanceChoice::ALL.get(*selected).copied() {
self.set_theme_selection(self.theme_name, choice);
self.show_toast(
format!("Appearance changed to {}", choice.label()),
ToastLevel::Success,
now,
);
}
return effects;
}
_ => {}
}
self.modal = Some(modal);
}
_ => {}
}
effects
}
}