feat: polish settings controls

This commit is contained in:
objz committed 2026-09-04 12:48:48 +02:00
1 parent 4edc920912
commit d5d77c222e
7 files changed
+857 -99

No files matched your search

+20 -11
View File
@@ -292,13 +292,12 @@ impl State {
KeyCode::Enter => match self.selected {
0 => self.theme_picker = true,
1 => self.cycle_border(true),
2 | 3 => self.adjust_selected_memory(true),
2 | 3 => {
self.editing = Some(new_text_area(vec![self.value(self.selected)]));
}
4 => self.open_java_picker(),
field => self.editing = Some(new_text_area(vec![self.value(field)])),
},
KeyCode::Char('c') if matches!(self.selected, 2 | 3) => {
self.editing = Some(new_text_area(vec![self.value(self.selected)]));
}
KeyCode::Char('s') => {
if self.validate_before_save() {
return Action::Save(
@@ -380,8 +379,9 @@ pub fn popup_rect(area: Rect, state: &State) -> Rect {
} else {
7 + u16::from(state.error.is_some())
};
let width = if state.java_picker_open { 72 } else { 52 };
area.centered(
ratatui::layout::Constraint::Percentage(52),
ratatui::layout::Constraint::Percentage(width),
ratatui::layout::Constraint::Length(height.min(area.height.saturating_sub(4))),
)
}
@@ -399,7 +399,7 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) {
} else if matches!(state.selected, 2 | 3) {
super::keybind_line(&[
("h/l", " adjust"),
("c", " custom"),
("Enter", " exact"),
("s", " save"),
("Esc", " back"),
])
@@ -470,11 +470,11 @@ fn render_java_picker(frame: &mut Frame, area: Rect, state: &State) {
list_area.y = list_area.y.saturating_add(1);
list_area.height = list_area.height.saturating_sub(1);
}
render_picker(
frame,
list_area,
&state.java_picker.labels(),
super::select_list::render(
state.java_picker.items(),
state.java_picker.selected,
list_area,
frame.buffer_mut(),
);
}
@@ -553,6 +553,11 @@ fn global_field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a
}),
),
])
.style(Style::default().bg(if selected {
theme.stripe()
} else {
theme.surface()
}))
}
#[cfg(test)]
@@ -564,10 +569,14 @@ mod tests {
let mut state = State::new();
state.selected = 2;
let original = state.config.defaults.memory_min.clone();
state.handle_key(&KeyEvent::from(KeyCode::Enter));
state.handle_key(&KeyEvent::from(KeyCode::Char('l')));
assert_ne!(state.config.defaults.memory_min, original);
assert!(state.editing.is_none());
state.handle_key(&KeyEvent::from(KeyCode::Enter));
assert!(state.editing.is_some());
state.editing = None;
state.selected = 4;
state.handle_key(&KeyEvent::from(KeyCode::Enter));
assert!(state.java_picker_open);
+207 -68
View File
@@ -25,7 +25,8 @@ use crate::{
popups::{
LoadState,
settings_controls::{
JavaChoice, JavaPicker, adjust_memory, memory_kib, render_memory_gauge,
DisplayResolution, JavaChoice, JavaPicker, adjust_memory, badge,
display_resolutions, memory_kib, render_memory_gauge,
},
},
search::SearchState,
@@ -48,6 +49,38 @@ enum ChoicePicker {
Resolution,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum ResolutionChoice {
Default,
Display(DisplayResolution),
Preset(u32, u32),
Configured(u32, u32),
Custom,
}
impl ResolutionChoice {
fn resolution(&self) -> Option<(u32, u32)> {
match self {
Self::Display(display) => Some((display.width, display.height)),
Self::Preset(width, height) | Self::Configured(width, height) => {
Some((*width, *height))
}
Self::Default | Self::Custom => None,
}
}
fn label(&self) -> String {
self.resolution().map_or_else(
|| match self {
Self::Default => "Default".to_owned(),
Self::Custom => "Custom…".to_owned(),
_ => String::new(),
},
|(width, height)| format!("{width}x{height}"),
)
}
}
enum PickerLoad {
Idle,
Loading,
@@ -74,6 +107,7 @@ pub struct State {
choice_picker: Option<ChoicePicker>,
choice_index: usize,
java_picker: JavaPicker,
display_resolutions: Vec<DisplayResolution>,
}
pub enum Action {
@@ -109,6 +143,7 @@ impl State {
choice_picker: None,
choice_index: 0,
java_picker: JavaPicker::new(),
display_resolutions: display_resolutions(),
}
}
@@ -202,7 +237,9 @@ impl State {
}
2 => self.open_loader_picker(),
3 => self.open_choice_picker(ChoicePicker::Java),
4 | 5 => self.adjust_selected_memory(true),
4 | 5 => {
self.editing = Some(new_text_area(vec![self.effective_memory(self.selected)]));
}
6 => self.editing = Some(new_text_area(vec![self.value(self.selected)])),
7 => self.open_choice_picker(ChoicePicker::Resolution),
8 => self.desktop = !self.desktop,
@@ -222,14 +259,10 @@ impl State {
self.java_picker.initialize();
self.java_picker.selected
}
ChoicePicker::Resolution => resolution_values(self.draft.resolution)
ChoicePicker::Resolution => self
.resolution_choices()
.iter()
.position(|value| {
self.draft.resolution.map_or_else(
|| value == "Default",
|(width, height)| value == &format!("{width}x{height}"),
)
})
.position(|choice| choice.resolution() == self.draft.resolution)
.unwrap_or(0),
};
}
@@ -246,7 +279,11 @@ impl State {
.map(ToString::to_string)
.collect(),
ChoicePicker::Java => self.java_picker.labels(),
ChoicePicker::Resolution => resolution_values(self.draft.resolution),
ChoicePicker::Resolution => self
.resolution_choices()
.iter()
.map(ResolutionChoice::label)
.collect(),
}
}
@@ -295,16 +332,17 @@ impl State {
}
}
Some(ChoicePicker::Resolution) => {
let selected = resolution_values(self.draft.resolution)
let selected = self
.resolution_choices()
.get(self.choice_index)
.cloned()
.unwrap_or_else(|| "Default".to_owned());
match selected.as_str() {
"Default" => self.draft.resolution = None,
"Custom…" => {
.unwrap_or(ResolutionChoice::Default);
match selected {
ResolutionChoice::Default => self.draft.resolution = None,
ResolutionChoice::Custom => {
self.editing = Some(new_text_area(vec![self.value(7)]));
}
value => self.draft.resolution = parse_resolution(value).ok(),
choice => self.draft.resolution = choice.resolution(),
}
}
None => {}
@@ -462,6 +500,10 @@ impl State {
self.error = None;
}
fn resolution_choices(&self) -> Vec<ResolutionChoice> {
resolution_choices(self.draft.resolution, &self.display_resolutions)
}
fn handle_picker_key(&mut self, key: &KeyEvent) {
self.initialize_picker_index();
if self.picker_search.active {
@@ -597,9 +639,6 @@ impl State {
self.adjust_selected_memory(true);
}
KeyCode::Enter => self.begin_edit(),
KeyCode::Char('c') if matches!(self.selected, 4 | 5) => {
self.editing = Some(new_text_area(vec![self.effective_memory(self.selected)]));
}
KeyCode::Char('r') if matches!(self.selected, 4 | 5) => {
self.set_memory(self.selected, None);
}
@@ -647,27 +686,36 @@ fn runtime_label(config: &InstanceConfig) -> String {
}
}
fn resolution_values(current: Option<(u32, u32)>) -> Vec<String> {
let mut values = [
"Default",
"854x480",
"1280x720",
"1600x900",
"1920x1080",
"2560x1440",
"3840x2160",
"Custom…",
]
.into_iter()
.map(str::to_owned)
.collect::<Vec<_>>();
if let Some((width, height)) = current {
let current = format!("{width}x{height}");
if !values.contains(&current) {
values.insert(values.len() - 1, current);
fn resolution_choices(
current: Option<(u32, u32)>,
displays: &[DisplayResolution],
) -> Vec<ResolutionChoice> {
let mut choices = vec![ResolutionChoice::Default];
choices.extend(displays.iter().cloned().map(ResolutionChoice::Display));
for (width, height) in [
(854, 480),
(1280, 720),
(1600, 900),
(1920, 1080),
(2560, 1440),
(3840, 2160),
] {
if !choices
.iter()
.any(|choice| choice.resolution() == Some((width, height)))
{
choices.push(ResolutionChoice::Preset(width, height));
}
}
values
if let Some((width, height)) = current
&& !choices
.iter()
.any(|choice| choice.resolution() == Some((width, height)))
{
choices.push(ResolutionChoice::Configured(width, height));
}
choices.push(ResolutionChoice::Custom);
choices
}
fn new_text_area(lines: Vec<String>) -> TextArea<'static> {
@@ -693,8 +741,13 @@ pub fn popup_rect(area: Rect, state: &State) -> Rect {
} else {
11 + u16::from(state.error.is_some())
};
let width = match state.choice_picker {
Some(ChoicePicker::Java) => 72,
Some(ChoicePicker::Resolution) => 64,
_ => 58,
};
area.centered(
Constraint::Percentage(58),
Constraint::Percentage(width),
Constraint::Length(height.min(area.height.saturating_sub(4))),
)
}
@@ -731,7 +784,7 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) {
} else if matches!(state.selected, 4 | 5) {
super::keybind_line(&[
("h/l", " adjust"),
("c", " custom"),
("Enter", " exact"),
("r", " default"),
("s", " save"),
("Esc", " back"),
@@ -842,9 +895,13 @@ fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> {
Style::default().fg(theme.accent()),
),
Span::styled(
format!("{label:<18}"),
format!("{label:<16}"),
Style::default().fg(theme.text_dim()),
),
Span::styled(
if dirty { "* " } else { " " },
Style::default().fg(theme.accent()),
),
Span::styled(
value,
Style::default()
@@ -861,10 +918,6 @@ fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> {
Modifier::empty()
}),
),
Span::styled(
if dirty { " *" } else { "" },
Style::default().fg(theme.accent()),
),
];
if index == 6 && !editing {
if state.draft.jvm_args.is_empty() {
@@ -875,20 +928,27 @@ fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> {
} else {
for (argument_index, argument) in state.draft.jvm_args.iter().enumerate() {
if argument_index > 0 {
spans.push(Span::styled(" ", Style::default().fg(theme.text_dim())));
spans.push(Span::raw(" "));
}
spans.push(Span::styled(
argument.clone(),
Style::default().fg(if selected {
theme.accent()
} else {
theme.text()
}),
format!(" {argument} "),
Style::default()
.fg(if selected {
theme.accent()
} else {
theme.text()
})
.bg(theme.background())
.add_modifier(Modifier::BOLD),
));
}
}
}
Line::from(spans)
Line::from(spans).style(Style::default().bg(if selected {
theme.stripe()
} else {
theme.surface()
}))
}
fn field_dirty(state: &State, index: usize) -> bool {
@@ -908,7 +968,6 @@ fn field_dirty(state: &State, index: usize) -> bool {
fn render_choice_picker(frame: &mut Frame, area: Rect, state: &State) {
let theme = THEME.as_ref();
let values = state.choice_values();
let mut list_area = area;
if state.choice_picker == Some(ChoicePicker::Java)
&& let Some(status) = state.java_picker.status()
@@ -924,18 +983,74 @@ fn render_choice_picker(frame: &mut Frame, area: Rect, state: &State) {
list_area.y = list_area.y.saturating_add(1);
list_area.height = list_area.height.saturating_sub(1);
}
let items = values
.iter()
.map(|value| {
ListItem::new(Line::from(Span::styled(
value.clone(),
Style::default().fg(theme.text()),
)))
})
.collect();
let items = match state.choice_picker {
Some(ChoicePicker::Java) => state.java_picker.items(),
Some(ChoicePicker::Resolution) => resolution_items(&state.resolution_choices()),
_ => state
.choice_values()
.iter()
.map(|value| {
ListItem::new(Line::from(Span::styled(
value.clone(),
Style::default().fg(theme.text()),
)))
})
.collect(),
};
super::select_list::render(items, state.choice_index, list_area, frame.buffer_mut());
}
fn resolution_items(choices: &[ResolutionChoice]) -> Vec<ListItem<'static>> {
let theme = THEME.as_ref();
choices
.iter()
.map(|choice| {
let mut spans = vec![Span::styled(
choice.label(),
Style::default().fg(theme.text()),
)];
match choice {
ResolutionChoice::Default => {
spans.extend([Span::raw(" "), badge(" Inherit ", theme.info())]);
}
ResolutionChoice::Display(display) => {
spans.extend([
Span::raw(" "),
badge(
if display.primary {
" Primary "
} else {
" Current "
},
if display.primary {
theme.success()
} else {
theme.info()
},
),
]);
if !display.name.is_empty() {
spans.push(Span::styled(
format!(" {}", display.name),
Style::default().fg(theme.text_dim()),
));
}
}
ResolutionChoice::Preset(_, _) => {
spans.extend([Span::raw(" "), badge(" Preset ", theme.info())]);
}
ResolutionChoice::Configured(_, _) => {
spans.extend([Span::raw(" "), badge(" Configured ", theme.success())]);
}
ResolutionChoice::Custom => {
spans.extend([Span::raw(" "), badge(" Manual ", theme.warning())]);
}
}
ListItem::new(Line::from(spans))
})
.collect()
}
fn render_version_picker(frame: &mut Frame, area: Rect, state: &State) {
let theme = THEME.as_ref();
let status = match state.picker {
@@ -1055,7 +1170,7 @@ mod tests {
config.memory_min = Some("512M".to_owned());
let mut state = State::new(&config, temp.path());
state.selected = 4;
state.handle_key(&KeyEvent::from(KeyCode::Char('c')));
state.handle_key(&KeyEvent::from(KeyCode::Enter));
state.handle_key(&KeyEvent::from(KeyCode::Left));
state.handle_key(&KeyEvent::from(KeyCode::Char('0')));
state.handle_key(&KeyEvent::from(KeyCode::Enter));
@@ -1159,10 +1274,12 @@ mod tests {
let mut state = State::new(&instance(), temp.path());
state.selected = 4;
state.begin_edit();
assert!(state.editing.is_none());
state.handle_key(&KeyEvent::from(KeyCode::Char('l')));
assert!(state.draft.memory_min.is_some());
state.begin_edit();
assert!(state.editing.is_some());
state.editing = None;
state.selected = 6;
state.begin_edit();
assert!(state.editing.is_some());
@@ -1178,9 +1295,10 @@ mod tests {
state.selected = 7;
state.begin_edit();
assert_eq!(state.choice_picker, Some(ChoicePicker::Resolution));
state.choice_index = resolution_values(state.draft.resolution)
state.choice_index = state
.resolution_choices()
.iter()
.position(|value| value == "1920x1080")
.position(|choice| choice.resolution() == Some((1920, 1080)))
.unwrap();
state.apply_choice();
assert_eq!(state.draft.resolution, Some((1920, 1080)));
@@ -1209,4 +1327,25 @@ mod tests {
assert!(matches!(desktop.as_str(), "enabled" | "disabled"));
assert!(!desktop.contains('●') && !desktop.contains('○'));
}
#[test]
fn detected_resolutions_are_listed_before_presets_without_duplicates() {
let displays = vec![DisplayResolution {
width: 1920,
height: 1080,
name: "Primary display".to_owned(),
primary: true,
}];
let choices = resolution_choices(None, &displays);
assert!(matches!(choices[1], ResolutionChoice::Display(_)));
assert_eq!(
choices
.iter()
.filter(|choice| choice.resolution() == Some((1920, 1080)))
.count(),
1
);
}
}
+1
View File
@@ -27,6 +27,7 @@ pub(crate) fn render(items: Vec<ListItem<'_>>, selected: usize, area: Rect, buff
.highlight_style(
Style::default()
.fg(THEME.as_ref().accent())
.bg(THEME.as_ref().stripe())
.add_modifier(Modifier::BOLD),
)
.highlight_symbol(Span::styled(
+157 -17
View File
@@ -5,7 +5,13 @@
use std::sync::{Arc, Mutex};
use ratatui::{Frame, layout::Rect, style::Style, text::Span, widgets::Gauge};
use ratatui::{
Frame,
layout::Rect,
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{LineGauge, ListItem, Paragraph},
};
use crate::{
config::theme::THEME, instance::java::JavaInstallation, tui::widgets::popups::LoadState,
@@ -31,6 +37,14 @@ pub(crate) enum JavaChoice {
Custom,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct DisplayResolution {
pub width: u32,
pub height: u32,
pub name: String,
pub primary: bool,
}
impl JavaPicker {
pub(crate) fn new() -> Self {
Self {
@@ -121,6 +135,59 @@ impl JavaPicker {
.collect()
}
pub(crate) fn items(&self) -> Vec<ListItem<'static>> {
let theme = THEME.as_ref();
let installations = match &*self
.load
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
{
LoadState::Loaded(installations) => installations.clone(),
_ => Vec::new(),
};
self.choices()
.into_iter()
.map(|choice| match choice {
JavaChoice::Automatic => ListItem::new(Line::from(vec![
Span::styled("Automatic", Style::default().fg(theme.text())),
Span::raw(" "),
badge(" Auto ", theme.info()),
Span::styled(
format!(" {}", self.detected),
Style::default().fg(theme.text_dim()),
),
])),
JavaChoice::Installation(path) => {
let installation = installations
.iter()
.find(|installation| installation.path.to_string_lossy() == path);
let version = installation
.and_then(|installation| installation.version.as_deref())
.map_or_else(|| "Java".to_owned(), |version| format!("Java {version}"));
let current = self.current.as_deref() == Some(path.as_str());
ListItem::new(Line::from(vec![
Span::styled(version, Style::default().fg(theme.text())),
Span::raw(" "),
badge(
if current { " Current " } else { " Detected " },
if current {
theme.success()
} else {
theme.info()
},
),
Span::styled(format!(" {path}"), Style::default().fg(theme.text_dim())),
]))
}
JavaChoice::Custom => ListItem::new(Line::from(vec![
Span::styled("Custom path…", Style::default().fg(theme.text())),
Span::raw(" "),
badge(" Manual ", theme.warning()),
])),
})
.collect()
}
pub(crate) fn status(&self) -> Option<Result<&'static str, String>> {
match &*self
.load
@@ -217,27 +284,100 @@ pub(crate) fn render_memory_gauge(
.position(|step| memory_kib(step).is_some_and(|amount| current <= amount))
.unwrap_or(MEMORY_STEPS.len() - 1);
let ratio = (index + 1) as f64 / MEMORY_STEPS.len() as f64;
let gauge = Gauge::default()
let value_width = 17.min(area.width);
let value_area = Rect {
width: value_width,
..area
};
let line_area = Rect {
x: area.x.saturating_add(value_width).saturating_add(1),
width: area.width.saturating_sub(value_width.saturating_add(1)),
..area
};
let value_style = if selected {
Style::default()
.fg(theme.background())
.bg(theme.accent())
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(theme.text()).bg(theme.background())
};
frame.render_widget(
Paragraph::new(Line::from(Span::styled(format!(" {label} "), value_style))),
value_area,
);
let gauge = LineGauge::default()
.ratio(ratio)
.use_unicode(true)
.label(Span::styled(
label,
Style::default().fg(if selected {
theme.text()
} else {
theme.text_dim()
}),
))
.gauge_style(
Style::default()
.fg(if selected {
.label("")
.filled_symbol("━")
.unfilled_symbol("─")
.filled_style(Style::default().fg(if selected {
theme.accent()
} else {
theme.text_dim()
}))
.unfilled_style(Style::default().fg(theme.border()));
frame.render_widget(gauge, line_area);
if line_area.width > 0 {
let thumb_offset = ((line_area.width.saturating_sub(1)) as f64 * ratio).round() as u16;
frame.render_widget(
Paragraph::new(Span::styled(
"◆",
Style::default().fg(if selected {
theme.accent()
} else {
theme.text_dim()
})
.bg(theme.background()),
}),
)),
Rect {
x: line_area.x.saturating_add(thumb_offset),
width: 1,
..line_area
},
);
frame.render_widget(gauge, area);
}
}
pub(crate) fn display_resolutions() -> Vec<DisplayResolution> {
let Ok(displays) = display_info::DisplayInfo::all() else {
return Vec::new();
};
let mut resolutions = Vec::<DisplayResolution>::new();
for display in displays {
if display.width == 0 || display.height == 0 {
continue;
}
if let Some(existing) = resolutions.iter_mut().find(|resolution| {
resolution.width == display.width && resolution.height == display.height
}) {
existing.primary |= display.is_primary;
continue;
}
let name = if display.friendly_name.is_empty() {
display.name
} else {
display.friendly_name
};
resolutions.push(DisplayResolution {
width: display.width,
height: display.height,
name,
primary: display.is_primary,
});
}
resolutions.sort_by_key(|resolution| !resolution.primary);
resolutions
}
pub(crate) fn badge(text: &'static str, color: Color) -> Span<'static> {
Span::styled(
text,
Style::default()
.fg(THEME.as_ref().background())
.bg(color)
.add_modifier(Modifier::BOLD),
)
}
#[cfg(test)]