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
//! Settings screen — the Choice-field picker popup and resetting a field to its default.
//! Part of the [`super`] module; split out of settings.rs.
use super::helpers::*;
use super::spec::{Access, FieldSpec, field_spec};
use super::*;
impl SettingsScreen {
// ---------- Choice-field picker popup / reset to default ----------
/// The Choice field's option list + the index of the current one (`None` — not a
/// Choice field).
pub(super) fn choice_menu(&self, id: FieldId) -> Option<(Vec<String>, usize)> {
// Config Choice fields (mode/flash-attn/spec-type/theme) — from the access table.
if let Some(FieldSpec {
access: Access::Choice { options, .. },
..
}) = field_spec(id)
{
return Some(options(&self.config, self.loc()));
}
match id {
// MCP servers — the inventory in `config.mcp.servers`, by id.
FieldId::McpSelect => {
let opts: Vec<String> = self
.config
.mcp
.servers
.iter()
.map(|s| s.id.clone())
.collect();
(!opts.is_empty()).then_some((opts, self.mcp_server_idx))
}
// Sampling (Thinking/Reasoning/Verbosity) and profile selection — their own sources.
FieldId::S(
p @ (SamplingParam::Thinking | SamplingParam::Reasoning | SamplingParam::Verbosity),
) => Some(sampling_choice_menu(
&self.config.default_sampling,
p,
self.loc(),
)),
FieldId::IS(
p @ (SamplingParam::Thinking | SamplingParam::Reasoning | SamplingParam::Verbosity),
) => Some(sampling_choice_menu(
&self.config.impersonation_sampling,
p,
self.loc(),
)),
FieldId::PSelect => {
let opts: Vec<String> = self.profiles.iter().map(|p| p.name.clone()).collect();
(!opts.is_empty()).then_some((opts, self.profile_idx))
}
FieldId::IpSelect => {
let opts: Vec<String> = self
.config
.impersonation_profiles
.iter()
.map(|ip| ip.name.clone())
.collect();
(!opts.is_empty()).then_some((opts, self.imp_profile_idx))
}
// The reference to an impersonation profile: "not set" first, then the profiles.
FieldId::PImpProfile => {
let cur = self.imp_profile_choice_index()?;
let mut opts = vec![
self.loc()
.t("ui.settings.value.imp_profile_none")
.to_string(),
];
opts.extend(
self.config
.impersonation_profiles
.iter()
.map(|ip| ip.name.clone()),
);
Some((opts, cur))
}
// Profile scaffold language (axis A): options — all known languages (built-in +
// external from data/locales/).
FieldId::PLanguage => {
let p = self.profiles.get(self.profile_idx)?;
let all = crate::shared::i18n::Lang::all();
let opts: Vec<String> = all.iter().map(|l| l.label().to_string()).collect();
let cur = all.iter().position(|l| *l == p.language).unwrap_or(0);
Some((opts, cur))
}
_ => None,
}
}
pub(super) fn open_choice(&mut self, id: FieldId) {
if let Some((options, selected)) = self.choice_menu(id)
&& !options.is_empty()
{
self.choice = Some(ChoiceState {
field: id,
options,
selected,
scroll: ListScroll::default(),
});
}
}
/// Applies picking an option by index via the existing cycle (`cycle_field`):
/// steps forward as many times as needed from the current one to the target.
pub(super) fn apply_choice(&mut self, id: FieldId, target: usize) -> Option<SettingsIntent> {
let (opts, cur) = self.choice_menu(id)?;
let n = opts.len();
if n == 0 {
return None;
}
let steps = (target + n - cur) % n;
let mut intent = None;
for _ in 0..steps {
if let Some(i) = self.cycle_field(id, 1) {
intent = Some(i);
}
}
intent
}
pub(super) fn handle_choice_key(&mut self, key: KeyEvent) -> Option<SettingsIntent> {
let st = self.choice.as_mut()?;
match key.code {
KeyCode::Esc => {
self.choice = None;
None
}
KeyCode::Up | KeyCode::Left => {
st.selected = st.selected.saturating_sub(1);
None
}
KeyCode::Down | KeyCode::Right => {
if st.selected + 1 < st.options.len() {
st.selected += 1;
}
None
}
KeyCode::Enter => {
let (id, target) = (st.field, st.selected);
self.choice = None;
self.apply_choice(id, target)
}
_ => None,
}
}
/// Fields of the current section/subsection built from the **default** config
/// (for the "modified" marker and reset). Profiles are the same (they have no
/// config default).
///
/// The rows are compared **as the text they show**, so the defaults are
/// written in the language this screen is in. A default config is in the
/// default language, and a row built from it spells `auto` in Russian
/// where the screen says "auto" — so under any other interface language
/// every localized value at its default read as changed, and `Del`,
/// looking for the default's label among the options, found nothing to go
/// back to. The language row alone keeps the default language's own name:
/// there the language *is* the value.
pub(super) fn default_fields(&self) -> Vec<FieldRow> {
let mut defaults = AppConfig::default();
let default_language = defaults.interface.language;
defaults.interface.language = self.config.interface.language;
let mut tmp = SettingsScreen::new(
defaults,
self.profiles.clone(),
self.language_locked.clone(),
);
// Copy the MCP snapshot: otherwise `PTool` rows for MCP tools wouldn't find
// a match in the default field set.
tmp.mcp = self.mcp.clone();
tmp.section_idx = self.section_idx;
tmp.model_sub = self.model_sub;
tmp.sampling_sub = self.sampling_sub;
tmp.profile_sub = self.profile_sub;
tmp.profile_idx = self.profile_idx;
let mut rows = tmp.fields();
// The theme row is one of two fields, picked by the colour mode, and a
// default config is in the default mode — so the other mode's field has
// no row there, and would go without its `•` marker and its `Del`.
// Its default is the default config *seen under the current mode*; the
// mode's own row keeps the plain default above, or it could never read
// as changed.
let mode = self.config.interface.mode();
if mode != tmp.config.interface.mode() {
tmp.config.interface.set_mode(mode);
for row in tmp.fields() {
if !rows.iter().any(|r| r.id == row.id) {
rows.push(row);
}
}
}
for row in &mut rows {
if row.id == FieldId::ILanguage {
row.kind = FieldKind::Choice(default_language.label().to_string());
}
}
rows
}
/// Resets a config field to its default value. Profile fields and values already
/// at the default — a no-op (no redundant save).
pub(super) fn reset_field(&mut self, id: FieldId) -> Option<SettingsIntent> {
// Secret field: "reset" = delete the stored secret (an empty value). The
// comparison against the default below doesn't apply — the row's value is a
// status, not the value. Checked **before** `is_profile_field`: an MCP
// environment value is user data (no `•` marker, nothing to reset to) and
// yet its `Del` has a real meaning.
if let Some(key) = self.secret_field_key(id) {
return self
.secret_present(Some(&key))
.then(|| SettingsIntent::SetSecret {
key,
value: String::new(),
});
}
if is_profile_field(id) {
return None;
}
let cur_kind = self
.fields()
.into_iter()
.find(|f| f.id == id)
.map(|f| f.kind)?;
let default_kind = self
.default_fields()
.into_iter()
.find(|d| d.id == id)
.map(|d| d.kind)?;
// Already matches the default — do nothing.
if value_text(&cur_kind, self.loc()) == value_text(&default_kind, self.loc()) {
return None;
}
match default_kind {
FieldKind::Toggle(_) => self.toggle_field(id),
FieldKind::Choice(def_label) => {
let (opts, _) = self.choice_menu(id)?;
let idx = opts.iter().position(|o| *o == def_label)?;
self.apply_choice(id, idx)
}
FieldKind::Text(def) => {
// "—" is the empty placeholder (`Option::None`); clear the field.
let text = if def == "—" { "" } else { &def };
self.apply_text(id, text)
}
}
}
}