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
//! Theme selector modal key handler
use crossterm::event::{KeyCode, KeyEvent};
use crate::studio::events::SideEffect;
use crate::studio::state::{Modal, Notification, StudioState};
use crate::theme;
/// Visible items in the list (modal height - header - footer - variant separators)
const VISIBLE_ITEMS: usize = 16;
/// Handle key events in theme selector modal
pub fn handle(state: &mut StudioState, key: KeyEvent) -> Vec<SideEffect> {
// Verify we're in the right modal
let Some(Modal::ThemeSelector {
themes, selected, ..
}) = &state.modal
else {
return vec![];
};
let themes = themes.clone();
let selected = *selected;
match key.code {
KeyCode::Esc => {
state.close_modal();
vec![]
}
KeyCode::Enter => {
// Apply selected theme
if let Some(theme_info) = themes.get(selected) {
// Load the theme
if theme::load_theme_by_name(&theme_info.id).is_ok() {
// Theme has been applied (done above)
state.notify(Notification::success(format!(
"Theme: {}",
theme_info.display_name
)));
} else {
state.notify(Notification::error(format!(
"Failed to load theme: {}",
theme_info.id
)));
}
}
state.close_modal();
vec![]
}
KeyCode::Up | KeyCode::Char('k') => {
if let Some(Modal::ThemeSelector {
selected,
scroll,
themes,
input,
}) = &mut state.modal
{
// Get filtered indices
let filtered_indices: Vec<usize> = themes
.iter()
.enumerate()
.filter(|(_, t)| {
input.is_empty()
|| t.display_name
.to_lowercase()
.contains(&input.to_lowercase())
|| t.author.to_lowercase().contains(&input.to_lowercase())
})
.map(|(i, _)| i)
.collect();
// Find current position in filtered list
if let Some(pos) = filtered_indices.iter().position(|&i| i == *selected)
&& pos > 0
{
*selected = filtered_indices[pos - 1];
// Scroll up if selection goes above visible area
if pos - 1 < *scroll {
*scroll = pos - 1;
}
// Apply live preview
if let Some(theme_info) = themes.get(*selected) {
let _ = theme::load_theme_by_name(&theme_info.id);
}
}
}
state.mark_dirty();
vec![]
}
KeyCode::Down | KeyCode::Char('j') => {
if let Some(Modal::ThemeSelector {
selected,
scroll,
themes,
input,
}) = &mut state.modal
{
// Get filtered indices
let filtered_indices: Vec<usize> = themes
.iter()
.enumerate()
.filter(|(_, t)| {
input.is_empty()
|| t.display_name
.to_lowercase()
.contains(&input.to_lowercase())
|| t.author.to_lowercase().contains(&input.to_lowercase())
})
.map(|(i, _)| i)
.collect();
// Find current position in filtered list
if let Some(pos) = filtered_indices.iter().position(|&i| i == *selected)
&& pos + 1 < filtered_indices.len()
{
*selected = filtered_indices[pos + 1];
// Scroll down if selection goes below visible area
if pos + 1 >= *scroll + VISIBLE_ITEMS {
*scroll = pos + 1 - VISIBLE_ITEMS + 1;
}
// Apply live preview
if let Some(theme_info) = themes.get(*selected) {
let _ = theme::load_theme_by_name(&theme_info.id);
}
}
}
state.mark_dirty();
vec![]
}
KeyCode::Char(c) => {
if let Some(Modal::ThemeSelector {
input,
selected,
scroll,
themes,
}) = &mut state.modal
{
input.push(c);
*scroll = 0;
// Select first matching theme
let first_match = themes
.iter()
.enumerate()
.find(|(_, t)| {
input.is_empty()
|| t.display_name
.to_lowercase()
.contains(&input.to_lowercase())
|| t.author.to_lowercase().contains(&input.to_lowercase())
})
.map(|(i, _)| i);
*selected = first_match.unwrap_or(0);
}
state.mark_dirty();
vec![]
}
KeyCode::Backspace => {
if let Some(Modal::ThemeSelector {
input,
selected,
scroll,
themes,
}) = &mut state.modal
{
input.pop();
*scroll = 0;
// Select first matching theme
let first_match = themes
.iter()
.enumerate()
.find(|(_, t)| {
input.is_empty()
|| t.display_name
.to_lowercase()
.contains(&input.to_lowercase())
|| t.author.to_lowercase().contains(&input.to_lowercase())
})
.map(|(i, _)| i);
*selected = first_match.unwrap_or(0);
}
state.mark_dirty();
vec![]
}
_ => vec![],
}
}