feat: add in-TUI settings editors
This commit is contained in:
1 parent
8c94e126c3
commit
c95f2d17ff
47 files changed
+2574
-741
No files matched your search
@@ -138,6 +138,7 @@ pub(crate) fn spawn_provider_search(
|
||||
search_provider(
|
||||
registry.get("modrinth"),
|
||||
crate::config::SETTINGS
|
||||
.read()
|
||||
.content
|
||||
.discovery_provider_enabled("modrinth"),
|
||||
&target,
|
||||
@@ -148,6 +149,7 @@ pub(crate) fn spawn_provider_search(
|
||||
search_provider(
|
||||
registry.get("curseforge"),
|
||||
crate::config::SETTINGS
|
||||
.read()
|
||||
.content
|
||||
.discovery_provider_enabled("curseforge"),
|
||||
&target,
|
||||
@@ -177,7 +179,7 @@ pub(crate) fn spawn_provider_search(
|
||||
}
|
||||
let mut merged = merge_provider_results(
|
||||
pages,
|
||||
crate::config::SETTINGS.content.preferred_provider(),
|
||||
crate::config::SETTINGS.read().content.preferred_provider(),
|
||||
known_projects,
|
||||
);
|
||||
refresh_source_installed_versions(&mut merged.sources, &target);
|
||||
@@ -795,7 +797,11 @@ impl DiscoveryState {
|
||||
installed_path: Option<PathBuf>,
|
||||
target_world: Option<(String, PathBuf)>,
|
||||
) -> Option<VersionsRequest> {
|
||||
let preferred = crate::config::SETTINGS.content.preferred_provider();
|
||||
let preferred = crate::config::SETTINGS
|
||||
.read()
|
||||
.content
|
||||
.preferred_provider()
|
||||
.to_owned();
|
||||
sources.sort_by_key(|source| source.provider != preferred);
|
||||
let source = sources.first()?.clone();
|
||||
self.next_action_request_id = self.next_action_request_id.wrapping_add(1);
|
||||
|
||||
@@ -542,7 +542,7 @@ impl ContentListState {
|
||||
|
||||
let use_image_protocol =
|
||||
picker.protocol_type() != ratatui_image::picker::ProtocolType::Halfblocks;
|
||||
let use_quadrants = crate::config::SETTINGS.ui.image_protocol
|
||||
let use_quadrants = crate::config::SETTINGS.read().ui.image_protocol
|
||||
== crate::config::settings::ImageProtocol::Quadrants;
|
||||
if !use_image_protocol {
|
||||
self.image_protocols.clear();
|
||||
|
||||
@@ -504,7 +504,10 @@ pub fn render(
|
||||
}
|
||||
let loading_text = format!(
|
||||
"Searching {}...",
|
||||
crate::config::SETTINGS.content.discovery_provider_label()
|
||||
crate::config::SETTINGS
|
||||
.read()
|
||||
.content
|
||||
.discovery_provider_label()
|
||||
);
|
||||
render_discovery(
|
||||
frame,
|
||||
@@ -671,7 +674,10 @@ fn render_downloadable(
|
||||
if mode == ContentMode::Discover {
|
||||
let loading_text = format!(
|
||||
"Searching {}...",
|
||||
crate::config::SETTINGS.content.discovery_provider_label()
|
||||
crate::config::SETTINGS
|
||||
.read()
|
||||
.content
|
||||
.discovery_provider_label()
|
||||
);
|
||||
render_discovery(
|
||||
frame,
|
||||
|
||||
@@ -509,33 +509,34 @@ fn render_list(frame: &mut Frame, area: Rect, state: &mut LogsState, is_focused:
|
||||
})
|
||||
.collect();
|
||||
let search = &state.search;
|
||||
let success = theme.success();
|
||||
let accent = theme.accent();
|
||||
let text = theme.text();
|
||||
let background = theme.background();
|
||||
let stripe = theme.stripe();
|
||||
|
||||
let builder = ListBuilder::new(move |context| {
|
||||
let (name, is_live) = &entries_snapshot[context.index];
|
||||
let show_selected = list_focused && context.is_selected;
|
||||
|
||||
let style = if *is_live && show_selected {
|
||||
Style::default()
|
||||
.fg(theme.success())
|
||||
.add_modifier(Modifier::BOLD)
|
||||
Style::default().fg(success).add_modifier(Modifier::BOLD)
|
||||
} else if *is_live {
|
||||
Style::default().fg(theme.success())
|
||||
Style::default().fg(success)
|
||||
} else if show_selected {
|
||||
Style::default()
|
||||
.fg(theme.accent())
|
||||
.add_modifier(Modifier::BOLD)
|
||||
Style::default().fg(accent).add_modifier(Modifier::BOLD)
|
||||
} else {
|
||||
Style::default().fg(theme.text())
|
||||
Style::default().fg(text)
|
||||
};
|
||||
|
||||
let bg = if context.index % 2 == 0 {
|
||||
theme.background()
|
||||
background
|
||||
} else {
|
||||
theme.stripe()
|
||||
stripe
|
||||
};
|
||||
|
||||
let selector = if show_selected {
|
||||
Span::styled("\u{258c} ", Style::default().fg(theme.accent()))
|
||||
Span::styled("\u{258c} ", Style::default().fg(accent))
|
||||
} else {
|
||||
Span::raw(" ")
|
||||
};
|
||||
|
||||
@@ -1651,7 +1651,7 @@ fn render_image(
|
||||
width: area.width,
|
||||
height: full_height,
|
||||
protocol: picker.protocol_type(),
|
||||
mode: crate::config::SETTINGS.ui.image_protocol,
|
||||
mode: crate::config::SETTINGS.read().ui.image_protocol,
|
||||
};
|
||||
if image
|
||||
.prepared
|
||||
|
||||
@@ -74,7 +74,7 @@ pub fn popup_area(frame_area: Rect, message: &str, base_y: u16, elapsed_ms: u128
|
||||
const MAX_W: usize = 58;
|
||||
const MIN_W: usize = 22;
|
||||
|
||||
if elapsed_ms >= SETTINGS.ui.error_auto_dismiss_ms as u128 {
|
||||
if elapsed_ms >= SETTINGS.read().ui.error_auto_dismiss_ms as u128 {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,435 @@
|
||||
// SPDX-FileCopyrightText: 2026 Constantin Bauer
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
// modal editor for launcher-wide settings.
|
||||
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::Rect,
|
||||
style::{Modifier, Style},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Clear, Paragraph},
|
||||
};
|
||||
use ratatui_textarea::{CursorMove, TextArea};
|
||||
|
||||
use crate::{
|
||||
config::{
|
||||
Config,
|
||||
theme::{BORDER_STYLE, BorderStyle, THEME, ThemeConfig},
|
||||
},
|
||||
instance::models::normalize_memory_value,
|
||||
};
|
||||
|
||||
pub struct State {
|
||||
pub config: Config,
|
||||
pub theme: ThemeConfig,
|
||||
selected: usize,
|
||||
editing: Option<TextArea<'static>>,
|
||||
error: Option<String>,
|
||||
config_dirty: bool,
|
||||
confirm_close: bool,
|
||||
themes: Vec<String>,
|
||||
theme_picker: bool,
|
||||
theme_index: usize,
|
||||
}
|
||||
|
||||
pub enum Action {
|
||||
None,
|
||||
Save(Box<Config>, String, BorderStyle),
|
||||
OpenRaw(std::path::PathBuf),
|
||||
Close,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new() -> Self {
|
||||
let theme = crate::config::theme::current_theme_config();
|
||||
let themes = available_themes();
|
||||
let theme_index = themes
|
||||
.iter()
|
||||
.position(|candidate| candidate == &theme.theme)
|
||||
.unwrap_or(0);
|
||||
Self {
|
||||
config: crate::config::SETTINGS.read().clone(),
|
||||
theme,
|
||||
selected: 0,
|
||||
editing: None,
|
||||
error: None,
|
||||
config_dirty: false,
|
||||
confirm_close: false,
|
||||
themes,
|
||||
theme_picker: false,
|
||||
theme_index,
|
||||
}
|
||||
}
|
||||
|
||||
fn value(&self, field: usize) -> String {
|
||||
match field {
|
||||
0 => self.theme.theme.clone(),
|
||||
1 => format!("{:?}", self.theme.border_style).to_lowercase(),
|
||||
2 => self.config.defaults.memory_min.clone(),
|
||||
3 => self.config.defaults.memory_max.clone(),
|
||||
4 => self.config.paths.java_path.clone().unwrap_or_default(),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn display_value(&self, field: usize) -> String {
|
||||
if field == 4 && self.config.paths.java_path.is_none() {
|
||||
"auto-detect".to_owned()
|
||||
} else {
|
||||
self.value(field)
|
||||
}
|
||||
}
|
||||
|
||||
fn commit_edit(&mut self) {
|
||||
let Some(editor) = self.editing.take() else {
|
||||
return;
|
||||
};
|
||||
let raw = editor.lines().join("");
|
||||
let value = raw.trim();
|
||||
self.error = None;
|
||||
let invalid = |state: &mut Self, message: String| {
|
||||
state.error = Some(message);
|
||||
state.editing = Some(new_text_area(editor.lines().to_vec()));
|
||||
};
|
||||
match self.selected {
|
||||
2 | 3 if normalize_memory_value(value).is_none() => invalid(
|
||||
self,
|
||||
"memory must be a positive number with K, M, or G".to_owned(),
|
||||
),
|
||||
2 => {
|
||||
self.config.defaults.memory_min = normalize_memory_value(value).unwrap();
|
||||
self.config_dirty = true;
|
||||
}
|
||||
3 => {
|
||||
self.config.defaults.memory_max = normalize_memory_value(value).unwrap();
|
||||
self.config_dirty = true;
|
||||
}
|
||||
4 => {
|
||||
self.config.paths.java_path = (!value.is_empty()).then(|| value.to_owned());
|
||||
self.config_dirty = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn select_theme(&mut self) {
|
||||
let previous = self.theme.theme.clone();
|
||||
self.theme.theme = self.themes[self.theme_index].clone();
|
||||
self.error = None;
|
||||
if let Err(error) = crate::config::theme::apply_theme(
|
||||
self.theme.theme.clone(),
|
||||
self.theme.border_style.clone(),
|
||||
) {
|
||||
self.error = Some(error.to_string());
|
||||
self.theme.theme = previous;
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_theme_picker_key(&mut self, key: &KeyEvent) {
|
||||
match key.code {
|
||||
KeyCode::Esc => self.theme_picker = false,
|
||||
KeyCode::Char('j') | KeyCode::Down => {
|
||||
self.theme_index = (self.theme_index + 1).min(self.themes.len() - 1);
|
||||
}
|
||||
KeyCode::Char('k') | KeyCode::Up => {
|
||||
self.theme_index = self.theme_index.saturating_sub(1);
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
self.select_theme();
|
||||
if self.error.is_none() {
|
||||
self.theme_picker = false;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn cycle_border(&mut self) {
|
||||
let previous = self.theme.border_style.clone();
|
||||
self.theme.border_style = match self.theme.border_style {
|
||||
BorderStyle::Rounded => BorderStyle::Plain,
|
||||
BorderStyle::Plain => BorderStyle::Double,
|
||||
BorderStyle::Double => BorderStyle::Thick,
|
||||
BorderStyle::Thick => BorderStyle::Rounded,
|
||||
};
|
||||
self.error = None;
|
||||
if let Err(error) = crate::config::theme::apply_theme(
|
||||
self.theme.theme.clone(),
|
||||
self.theme.border_style.clone(),
|
||||
) {
|
||||
self.error = Some(error.to_string());
|
||||
self.theme.border_style = previous;
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_before_save(&mut self) -> bool {
|
||||
self.error = None;
|
||||
let min = memory_kib(&self.config.defaults.memory_min);
|
||||
let max = memory_kib(&self.config.defaults.memory_max);
|
||||
if min.zip(max).is_some_and(|(min, max)| min > max) {
|
||||
self.error = Some("minimum memory cannot exceed maximum memory".to_owned());
|
||||
}
|
||||
self.error.is_none()
|
||||
}
|
||||
|
||||
pub fn handle_key(&mut self, key: &KeyEvent) -> Action {
|
||||
if self.theme_picker {
|
||||
self.handle_theme_picker_key(key);
|
||||
return Action::None;
|
||||
}
|
||||
if self.confirm_close {
|
||||
match key.code {
|
||||
KeyCode::Char('y') | KeyCode::Enter => return Action::Close,
|
||||
KeyCode::Char('n') | KeyCode::Esc => self.confirm_close = false,
|
||||
_ => {}
|
||||
}
|
||||
return Action::None;
|
||||
}
|
||||
if let Some(input) = &mut self.editing {
|
||||
match key.code {
|
||||
KeyCode::Enter => self.commit_edit(),
|
||||
KeyCode::Esc => self.editing = None,
|
||||
_ => {
|
||||
input.input(*key);
|
||||
}
|
||||
}
|
||||
return Action::None;
|
||||
}
|
||||
match key.code {
|
||||
KeyCode::Char('j') | KeyCode::Down => self.selected = (self.selected + 1).min(4),
|
||||
KeyCode::Char('k') | KeyCode::Up => self.selected = self.selected.saturating_sub(1),
|
||||
KeyCode::Enter => match self.selected {
|
||||
0 => self.theme_picker = true,
|
||||
1 => self.cycle_border(),
|
||||
field => self.editing = Some(new_text_area(vec![self.value(field)])),
|
||||
},
|
||||
KeyCode::Char('s') => {
|
||||
if self.validate_before_save() {
|
||||
return Action::Save(
|
||||
Box::new(self.config.clone()),
|
||||
self.theme.theme.clone(),
|
||||
self.theme.border_style.clone(),
|
||||
);
|
||||
}
|
||||
}
|
||||
KeyCode::Char('E') if self.config_dirty => {
|
||||
self.error = Some(
|
||||
"save or discard launcher defaults before opening the raw file".to_owned(),
|
||||
);
|
||||
}
|
||||
KeyCode::Char('E') => {
|
||||
let file = if self.selected <= 1 {
|
||||
"theme.toml"
|
||||
} else {
|
||||
"config.toml"
|
||||
};
|
||||
return Action::OpenRaw(crate::config::get_config_path().join(file));
|
||||
}
|
||||
KeyCode::Esc if self.config_dirty => self.confirm_close = true,
|
||||
KeyCode::Esc => return Action::Close,
|
||||
_ => {}
|
||||
}
|
||||
Action::None
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn memory_kib(value: &str) -> Option<u64> {
|
||||
let normalized = normalize_memory_value(value)?;
|
||||
let (number, suffix) = normalized.split_at(normalized.len().saturating_sub(1));
|
||||
let number = number.parse::<u64>().ok()?;
|
||||
match suffix {
|
||||
"K" => Some(number),
|
||||
"M" => number.checked_mul(1024),
|
||||
"G" => number.checked_mul(1024 * 1024),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn available_themes() -> Vec<String> {
|
||||
let mut themes: Vec<String> = ratatui_themekit::available_theme_ids()
|
||||
.into_iter()
|
||||
.map(str::to_owned)
|
||||
.collect();
|
||||
let theme_dir = crate::config::get_config_path().join("theme");
|
||||
if let Ok(entries) = std::fs::read_dir(theme_dir) {
|
||||
themes.extend(entries.flatten().filter_map(|entry| {
|
||||
let path = entry.path();
|
||||
(path.extension().and_then(|ext| ext.to_str()) == Some("toml"))
|
||||
.then(|| path.file_stem()?.to_str().map(str::to_owned))
|
||||
.flatten()
|
||||
}));
|
||||
}
|
||||
themes.sort();
|
||||
themes.dedup();
|
||||
let current = crate::config::theme::current_theme_config().theme;
|
||||
if !themes.contains(¤t) {
|
||||
themes.push(current);
|
||||
themes.sort();
|
||||
}
|
||||
themes
|
||||
}
|
||||
|
||||
fn new_text_area(lines: Vec<String>) -> TextArea<'static> {
|
||||
let theme = THEME.as_ref();
|
||||
let mut editor = TextArea::new(if lines.is_empty() {
|
||||
vec![String::new()]
|
||||
} else {
|
||||
lines
|
||||
});
|
||||
editor.set_style(Style::default().fg(theme.text()).bg(theme.surface()));
|
||||
editor.set_cursor_line_style(Style::default());
|
||||
editor.set_cursor_style(Style::default().fg(theme.background()).bg(theme.accent()));
|
||||
editor.move_cursor(CursorMove::Bottom);
|
||||
editor.move_cursor(CursorMove::End);
|
||||
editor
|
||||
}
|
||||
|
||||
pub fn render(frame: &mut Frame, area: Rect, state: &State) {
|
||||
let theme = THEME.as_ref();
|
||||
frame.render_widget(Clear, area);
|
||||
let keybinds = if state.theme_picker {
|
||||
super::keybind_line(&[("j/k", " move"), ("Enter", " apply"), ("Esc", " back")])
|
||||
} else {
|
||||
super::keybind_line(&[
|
||||
("j/k", " field"),
|
||||
("Enter", " edit"),
|
||||
("s", " save"),
|
||||
("E", " raw"),
|
||||
("Esc", " back"),
|
||||
])
|
||||
};
|
||||
let block = Block::default()
|
||||
.title(if state.config_dirty {
|
||||
" Launcher settings * "
|
||||
} else {
|
||||
" Launcher settings "
|
||||
})
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BORDER_STYLE.to_border_type())
|
||||
.border_style(Style::default().fg(theme.accent()))
|
||||
.style(Style::default().bg(theme.background()))
|
||||
.title_bottom(keybinds);
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
if state.theme_picker {
|
||||
let mut lines = vec![Line::from(Span::styled(
|
||||
"Select theme",
|
||||
Style::default()
|
||||
.fg(theme.text())
|
||||
.add_modifier(Modifier::BOLD),
|
||||
))];
|
||||
let visible_rows = inner.height.saturating_sub(1) as usize;
|
||||
let start = state
|
||||
.theme_index
|
||||
.saturating_sub(visible_rows.saturating_sub(1));
|
||||
for (index, name) in state
|
||||
.themes
|
||||
.iter()
|
||||
.enumerate()
|
||||
.skip(start)
|
||||
.take(visible_rows)
|
||||
{
|
||||
let selected = index == state.theme_index;
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(
|
||||
if selected { "▸ " } else { " " },
|
||||
Style::default().fg(theme.accent()),
|
||||
),
|
||||
Span::styled(
|
||||
name.clone(),
|
||||
Style::default()
|
||||
.fg(if selected {
|
||||
theme.accent()
|
||||
} else {
|
||||
theme.text()
|
||||
})
|
||||
.add_modifier(if selected {
|
||||
Modifier::BOLD
|
||||
} else {
|
||||
Modifier::empty()
|
||||
}),
|
||||
),
|
||||
]));
|
||||
}
|
||||
frame.render_widget(Paragraph::new(lines), inner);
|
||||
return;
|
||||
}
|
||||
let labels = [
|
||||
"Theme",
|
||||
"Border style",
|
||||
"Default memory min",
|
||||
"Default memory max",
|
||||
"Java path",
|
||||
];
|
||||
let mut lines = Vec::new();
|
||||
for (index, label) in labels.iter().enumerate() {
|
||||
let selected = index == state.selected;
|
||||
let displayed = if selected {
|
||||
state.editing.as_ref().map_or_else(
|
||||
|| state.display_value(index),
|
||||
|_| "editing below".to_owned(),
|
||||
)
|
||||
} else {
|
||||
state.display_value(index)
|
||||
};
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(
|
||||
if selected { "▸ " } else { " " },
|
||||
Style::default().fg(theme.accent()),
|
||||
),
|
||||
Span::styled(
|
||||
format!("{label:<20}"),
|
||||
Style::default().fg(theme.text_dim()),
|
||||
),
|
||||
Span::styled(
|
||||
displayed,
|
||||
Style::default()
|
||||
.fg(if selected {
|
||||
theme.accent()
|
||||
} else {
|
||||
theme.text()
|
||||
})
|
||||
.add_modifier(if selected {
|
||||
Modifier::BOLD
|
||||
} else {
|
||||
Modifier::empty()
|
||||
}),
|
||||
),
|
||||
]));
|
||||
}
|
||||
if let Some(error) = &state.error {
|
||||
lines.push(Line::from(Span::styled(
|
||||
error,
|
||||
Style::default().fg(theme.error()),
|
||||
)));
|
||||
} else if state.confirm_close {
|
||||
lines.push(Line::from(Span::styled(
|
||||
"Discard unsaved launcher defaults? [y] yes [n] no",
|
||||
Style::default().fg(theme.warning()),
|
||||
)));
|
||||
}
|
||||
frame.render_widget(Paragraph::new(lines), inner);
|
||||
if let Some(editor) = state.editing.as_ref() {
|
||||
let editor_area = Rect {
|
||||
x: inner.x,
|
||||
y: inner.y.saturating_add(7),
|
||||
width: inner.width,
|
||||
height: 3.min(inner.height.saturating_sub(7)).max(1),
|
||||
};
|
||||
let editor_block = Block::default()
|
||||
.title(" Value — Enter applies ")
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(theme.accent()));
|
||||
let editor_inner = editor_block.inner(editor_area);
|
||||
frame.render_widget(editor_block, editor_area);
|
||||
frame.render_widget(editor, editor_inner);
|
||||
}
|
||||
}
|
||||
@@ -480,7 +480,7 @@ async fn resolve_version_id(
|
||||
) {
|
||||
match modrinth::fetch_version(client, version_id).await {
|
||||
Ok(version) => {
|
||||
let meta_dir = crate::config::SETTINGS.paths.resolve_meta_dir();
|
||||
let meta_dir = crate::config::SETTINGS.read().paths.resolve_meta_dir();
|
||||
let tmp_dir = crate::storage::MetadataPaths::new(&meta_dir).temporary();
|
||||
if let Err(e) = tokio::fs::create_dir_all(&tmp_dir).await {
|
||||
set_error_and_back(
|
||||
@@ -558,7 +558,7 @@ fn start_version_download(state: &mut ImportWizardState) {
|
||||
|
||||
tokio::spawn(async move {
|
||||
let client = crate::net::HttpClient::new();
|
||||
let meta_dir = crate::config::SETTINGS.paths.resolve_meta_dir();
|
||||
let meta_dir = crate::config::SETTINGS.read().paths.resolve_meta_dir();
|
||||
let tmp_dir = crate::storage::MetadataPaths::new(&meta_dir).temporary();
|
||||
if let Err(e) = tokio::fs::create_dir_all(&tmp_dir).await {
|
||||
set_error_and_back(
|
||||
@@ -618,7 +618,7 @@ fn spawn_discovery_request_with_query(
|
||||
crate::tui::widgets::content::discovery::spawn_provider_search(
|
||||
query,
|
||||
crate::tui::widgets::content::discovery::DiscoveryTarget::Modpacks,
|
||||
crate::config::SETTINGS.paths.resolve_meta_dir(),
|
||||
crate::config::SETTINGS.read().paths.resolve_meta_dir(),
|
||||
request,
|
||||
);
|
||||
}
|
||||
@@ -667,9 +667,10 @@ fn start_discovered_download(request: crate::tui::widgets::content::discovery::I
|
||||
tokio::spawn(async move {
|
||||
let client = crate::net::HttpClient::new();
|
||||
let registry = crate::instance::content::provider::ProviderRegistry::configured(client);
|
||||
let tmp_dir =
|
||||
crate::storage::MetadataPaths::new(crate::config::SETTINGS.paths.resolve_meta_dir())
|
||||
.temporary();
|
||||
let tmp_dir = crate::storage::MetadataPaths::new(
|
||||
crate::config::SETTINGS.read().paths.resolve_meta_dir(),
|
||||
)
|
||||
.temporary();
|
||||
let result: Result<crate::instance::import::ImportSummary, crate::net::NetError> = async {
|
||||
let source = crate::instance::ProviderProject {
|
||||
provider: request.provider.clone(),
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -7,10 +7,13 @@
|
||||
pub mod base;
|
||||
pub mod confirm;
|
||||
pub mod error;
|
||||
pub mod global_settings;
|
||||
pub mod import_modpack;
|
||||
pub mod instance_settings;
|
||||
mod load_state;
|
||||
pub mod modpack_update;
|
||||
pub mod new_instance;
|
||||
pub mod version_lists;
|
||||
|
||||
pub use load_state::LoadState;
|
||||
|
||||
|
||||
@@ -5,10 +5,7 @@
|
||||
// flow: Name -> Loader -> Version -> LoaderVersion -> Confirm
|
||||
// version lists are fetched lazily from the network when you reach that step.
|
||||
|
||||
use crate::instance::{
|
||||
loader::{GameVersion, get_installer},
|
||||
models::ModLoader,
|
||||
};
|
||||
use crate::instance::{loader::GameVersion, models::ModLoader};
|
||||
use crate::tui::widgets::instances;
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use std::sync::LazyLock;
|
||||
@@ -405,12 +402,9 @@ pub(crate) fn ensure_versions_loaded(state: &mut WizardState) {
|
||||
let versions_arc = WIZARD_STATE.clone();
|
||||
let loader = state.selected_loader();
|
||||
tokio::spawn(async move {
|
||||
let client = crate::net::HttpClient::new();
|
||||
let installer = get_installer(loader);
|
||||
match installer.get_game_versions(&client).await {
|
||||
Ok(mut versions) => match versions_arc.lock() {
|
||||
match super::super::version_lists::game_versions(loader).await {
|
||||
Ok(versions) => match versions_arc.lock() {
|
||||
Ok(mut s) => {
|
||||
sort_versions_semver(&mut versions);
|
||||
s.versions = LoadState::Loaded(versions);
|
||||
clamp_version_index(&mut s);
|
||||
}
|
||||
@@ -442,9 +436,7 @@ pub(crate) fn ensure_loader_versions_loaded(
|
||||
state.loader_versions = LoadState::Loading;
|
||||
let versions_arc = WIZARD_STATE.clone();
|
||||
tokio::spawn(async move {
|
||||
let client = crate::net::HttpClient::new();
|
||||
let installer = get_installer(loader);
|
||||
match installer.get_versions(&client, &game_version).await {
|
||||
match super::super::version_lists::loader_versions(loader, &game_version).await {
|
||||
Ok(versions) => match versions_arc.lock() {
|
||||
Ok(mut s) => {
|
||||
s.loader_versions = LoadState::Loaded(versions);
|
||||
@@ -466,10 +458,6 @@ pub(crate) fn ensure_loader_versions_loaded(
|
||||
});
|
||||
}
|
||||
|
||||
fn sort_versions_semver(versions: &mut [GameVersion]) {
|
||||
versions.sort_by(|a, b| super::super::compare_game_versions(&b.id, &a.id));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../../../tests/widgets/popups/new_instance/state.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,27 @@
|
||||
// SPDX-FileCopyrightText: 2026 Constantin Bauer
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
// shared network loading for Minecraft and mod-loader version pickers.
|
||||
|
||||
use crate::instance::{
|
||||
loader::{GameVersion, get_installer},
|
||||
models::ModLoader,
|
||||
};
|
||||
|
||||
pub async fn game_versions(loader: ModLoader) -> Result<Vec<GameVersion>, String> {
|
||||
let client = crate::net::HttpClient::new();
|
||||
let mut versions = get_installer(loader)
|
||||
.get_game_versions(&client)
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
versions.sort_by(|a, b| super::compare_game_versions(&b.id, &a.id));
|
||||
Ok(versions)
|
||||
}
|
||||
|
||||
pub async fn loader_versions(loader: ModLoader, game_version: &str) -> Result<Vec<String>, String> {
|
||||
let client = crate::net::HttpClient::new();
|
||||
get_installer(loader)
|
||||
.get_versions(&client, game_version)
|
||||
.await
|
||||
.map_err(|error| error.to_string())
|
||||
}
|
||||
+45
-491
@@ -1,23 +1,8 @@
|
||||
// SPDX-FileCopyrightText: 2026 Constantin Bauer
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
// settings panel: manages config profiles and shows compact instance info.
|
||||
// also provides keybinds to open config files in $EDITOR.
|
||||
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
style::{Modifier, Style},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Paragraph, Widget},
|
||||
};
|
||||
use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView};
|
||||
// compact, read-only summary of the selected instance. editing lives in the
|
||||
// instance and launcher settings popups.
|
||||
|
||||
use crate::config::{
|
||||
SETTINGS,
|
||||
@@ -25,181 +10,24 @@ use crate::config::{
|
||||
};
|
||||
use crate::instance::models::InstanceConfig;
|
||||
use crate::tui::app::FocusedArea;
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::Rect,
|
||||
style::{Modifier, Style},
|
||||
text::{Line, Span},
|
||||
widgets::{Block, Borders, Paragraph},
|
||||
};
|
||||
|
||||
use super::styled_title;
|
||||
|
||||
const LOCAL_PROFILE_LABEL: &str = "instance default";
|
||||
|
||||
#[derive(Default)]
|
||||
pub enum AddMode {
|
||||
#[default]
|
||||
None,
|
||||
ProfileName(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum SettingsPane {
|
||||
#[default]
|
||||
Profile,
|
||||
Info,
|
||||
}
|
||||
|
||||
pub struct SettingsState {
|
||||
pub list_state: TuiListState,
|
||||
pub profiles: Vec<String>,
|
||||
pub add_mode: AddMode,
|
||||
pub pane: SettingsPane,
|
||||
meta_dir: PathBuf,
|
||||
active_profile: Option<String>,
|
||||
instance_name: Option<String>,
|
||||
java_key: Option<String>,
|
||||
java_source: Option<String>,
|
||||
java_label: String,
|
||||
}
|
||||
|
||||
impl SettingsState {
|
||||
pub fn new(meta_dir: PathBuf) -> Self {
|
||||
let profiles = crate::instance::config_sync::list_profiles(&meta_dir).unwrap_or_else(|e| {
|
||||
tracing::warn!("Failed to load config sync profiles: {}", e);
|
||||
Vec::new()
|
||||
});
|
||||
let mut state = Self {
|
||||
list_state: TuiListState::default(),
|
||||
profiles,
|
||||
add_mode: AddMode::None,
|
||||
pane: SettingsPane::Profile,
|
||||
meta_dir,
|
||||
active_profile: None,
|
||||
instance_name: None,
|
||||
java_key: None,
|
||||
java_source: None,
|
||||
java_label: "unknown".to_string(),
|
||||
};
|
||||
state.select_active();
|
||||
state
|
||||
}
|
||||
|
||||
fn reload_profiles(&mut self) {
|
||||
match crate::instance::config_sync::list_profiles(&self.meta_dir) {
|
||||
Ok(mut profiles) => {
|
||||
add_active_profile(&mut profiles, self.active_profile.as_deref());
|
||||
self.profiles = profiles;
|
||||
}
|
||||
Err(e) => tracing::warn!("Failed to reload config sync profiles: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
fn select_active(&mut self) {
|
||||
self.list_state.selected = Some(
|
||||
self.active_profile
|
||||
.as_deref()
|
||||
.and_then(|active| self.profiles.iter().position(|profile| profile == active))
|
||||
.map(|idx| idx + 1)
|
||||
.unwrap_or(0),
|
||||
);
|
||||
}
|
||||
|
||||
fn count(&self) -> usize {
|
||||
self.profiles.len() + 1
|
||||
}
|
||||
|
||||
pub fn remove_profile(&mut self, profile: &str) {
|
||||
self.profiles.retain(|candidate| candidate != profile);
|
||||
if self.active_profile.as_deref() == Some(profile) {
|
||||
self.active_profile = None;
|
||||
}
|
||||
let last = self.count().saturating_sub(1);
|
||||
self.list_state.selected = Some(self.list_state.selected.unwrap_or(0).min(last));
|
||||
}
|
||||
|
||||
fn update_for_instance(&mut self, instance: Option<&InstanceConfig>) {
|
||||
let instance_name = instance.map(|inst| inst.name.clone());
|
||||
let active_profile = instance.and_then(|inst| inst.config_sync_profile.clone());
|
||||
let active_profile = active_profile
|
||||
.filter(|profile| self.profiles.iter().any(|candidate| candidate == profile));
|
||||
if self.instance_name != instance_name || self.active_profile != active_profile {
|
||||
self.instance_name = instance_name;
|
||||
self.active_profile = active_profile;
|
||||
add_active_profile(&mut self.profiles, self.active_profile.as_deref());
|
||||
self.select_active();
|
||||
}
|
||||
|
||||
let java_key = instance.map(java_path_key);
|
||||
if self.java_key != java_key {
|
||||
let java_source = instance.map(effective_java_path);
|
||||
self.java_label = java_source
|
||||
.as_deref()
|
||||
.map(java_version_label)
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
self.java_key = java_key;
|
||||
self.java_source = java_source;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_active_profile(profiles: &mut Vec<String>, active_profile: Option<&str>) {
|
||||
if let Some(active) = active_profile
|
||||
&& !profiles.iter().any(|profile| profile == active)
|
||||
{
|
||||
profiles.push(active.to_string());
|
||||
profiles.sort_unstable();
|
||||
}
|
||||
}
|
||||
|
||||
fn java_path_key(instance: &InstanceConfig) -> String {
|
||||
instance
|
||||
.java_path
|
||||
.as_deref()
|
||||
.filter(|path| !path.is_empty())
|
||||
.or_else(|| SETTINGS.paths.effective_java_path())
|
||||
.unwrap_or("<auto>")
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn effective_java_path(instance: &InstanceConfig) -> String {
|
||||
instance
|
||||
.java_path
|
||||
.clone()
|
||||
.or_else(|| SETTINGS.paths.effective_java_path().map(str::to_string))
|
||||
.unwrap_or_else(crate::instance::java::detect_java_path)
|
||||
}
|
||||
|
||||
fn java_version_label(java_path: &str) -> String {
|
||||
let output = Command::new(java_path).arg("-version").output();
|
||||
let Ok(output) = output else {
|
||||
return "unknown".to_string();
|
||||
};
|
||||
let raw = String::from_utf8_lossy(if output.stderr.is_empty() {
|
||||
&output.stdout
|
||||
} else {
|
||||
&output.stderr
|
||||
});
|
||||
let first_line = raw.lines().next().unwrap_or_default();
|
||||
let Some(version) = first_line.split('"').nth(1) else {
|
||||
return "unknown".to_string();
|
||||
};
|
||||
let major = if let Some(stripped) = version.strip_prefix("1.") {
|
||||
stripped.split('.').next().unwrap_or(stripped)
|
||||
} else {
|
||||
version.split('.').next().unwrap_or(version)
|
||||
};
|
||||
if major.is_empty() {
|
||||
"unknown".to_string()
|
||||
} else {
|
||||
format!("jdk{major}")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
focused: FocusedArea,
|
||||
state: &mut SettingsState,
|
||||
instance: Option<&InstanceConfig>,
|
||||
_instances_dir: &Path,
|
||||
) {
|
||||
state.update_for_instance(instance);
|
||||
|
||||
let theme = THEME.as_ref();
|
||||
let color = if focused == FocusedArea::Settings {
|
||||
theme.accent()
|
||||
@@ -214,23 +42,7 @@ pub fn render(
|
||||
.border_style(Style::default().fg(color));
|
||||
|
||||
let keybind_line = if focused == FocusedArea::Settings {
|
||||
let keybinds: &[(&str, &str)] = match state.pane {
|
||||
SettingsPane::Profile => &[
|
||||
("⏎", " select"),
|
||||
("a", " add"),
|
||||
("d", " del"),
|
||||
("j/k", " move"),
|
||||
("h/l", " tab"),
|
||||
("Esc", " back"),
|
||||
],
|
||||
SettingsPane::Info => &[
|
||||
("e", " inst"),
|
||||
("g", " global"),
|
||||
("d", " desk"),
|
||||
("h/l", " tab"),
|
||||
("Esc", " back"),
|
||||
],
|
||||
};
|
||||
let keybinds: &[(&str, &str)] = &[("E", " instance"), ("G", " launcher"), ("Esc", " back")];
|
||||
Some(super::popups::keybind_line_fitted(
|
||||
keybinds,
|
||||
area.width.saturating_sub(2),
|
||||
@@ -245,49 +57,10 @@ pub fn render(
|
||||
let inner = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
|
||||
if inner.width >= 42 {
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Percentage(50),
|
||||
Constraint::Length(3),
|
||||
Constraint::Percentage(50),
|
||||
])
|
||||
.split(inner);
|
||||
render_profile_list(frame, chunks[0], focused, state);
|
||||
render_separator(frame, chunks[1], focused, state.pane);
|
||||
render_instance_info(frame, chunks[2], focused, state, instance);
|
||||
} else {
|
||||
render_profile_list(frame, inner, focused, state);
|
||||
}
|
||||
|
||||
if let AddMode::ProfileName(name) = &state.add_mode {
|
||||
render_add_profile_popup(frame, name);
|
||||
}
|
||||
render_instance_info(frame, inner, instance);
|
||||
}
|
||||
|
||||
fn render_separator(frame: &mut Frame, area: Rect, focused: FocusedArea, pane: SettingsPane) {
|
||||
let theme = THEME.as_ref();
|
||||
let color = if focused == FocusedArea::Settings && pane == SettingsPane::Info {
|
||||
theme.accent()
|
||||
} else {
|
||||
theme.border()
|
||||
};
|
||||
let line = if area.width >= 3 {
|
||||
" │ \n".repeat(area.height as usize)
|
||||
} else {
|
||||
"│\n".repeat(area.height as usize)
|
||||
};
|
||||
frame.render_widget(Paragraph::new(line).style(Style::default().fg(color)), area);
|
||||
}
|
||||
|
||||
fn render_instance_info(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
focused: FocusedArea,
|
||||
state: &SettingsState,
|
||||
instance: Option<&InstanceConfig>,
|
||||
) {
|
||||
fn render_instance_info(frame: &mut Frame, area: Rect, instance: Option<&InstanceConfig>) {
|
||||
let theme = THEME.as_ref();
|
||||
let label_style = Style::default().fg(theme.text_dim());
|
||||
let value_style = Style::default()
|
||||
@@ -302,278 +75,59 @@ fn render_instance_info(
|
||||
return;
|
||||
};
|
||||
|
||||
let settings = SETTINGS.read();
|
||||
let memory_min = inst
|
||||
.memory_min
|
||||
.as_deref()
|
||||
.unwrap_or(&SETTINGS.defaults.memory_min);
|
||||
.unwrap_or(&settings.defaults.memory_min);
|
||||
let memory_max = inst
|
||||
.memory_max
|
||||
.as_deref()
|
||||
.unwrap_or(&SETTINGS.defaults.memory_max);
|
||||
let active_style = if focused == FocusedArea::Settings && state.pane == SettingsPane::Info {
|
||||
value_style.fg(theme.accent())
|
||||
} else {
|
||||
value_style
|
||||
};
|
||||
.unwrap_or(&settings.defaults.memory_max);
|
||||
let active_style = value_style;
|
||||
let desktop = if crate::instance::desktop::exists(&inst.name) {
|
||||
"yes"
|
||||
} else {
|
||||
"no"
|
||||
};
|
||||
let java_source = if inst
|
||||
.java_path
|
||||
.as_deref()
|
||||
.is_some_and(|path| !path.is_empty())
|
||||
{
|
||||
"instance java"
|
||||
} else if settings.paths.effective_java_path().is_some() {
|
||||
"global java"
|
||||
} else {
|
||||
"auto java"
|
||||
};
|
||||
let lines = vec![
|
||||
Line::from(vec![
|
||||
Span::styled("Memory ", label_style),
|
||||
Span::styled(format!("{memory_min} - {memory_max}"), active_style),
|
||||
Span::styled("Version ", label_style),
|
||||
Span::styled(
|
||||
format!("{} / {}", inst.game_version, inst.loader),
|
||||
active_style,
|
||||
),
|
||||
]),
|
||||
Line::from(vec![
|
||||
Span::styled("Java ", label_style),
|
||||
Span::styled(state.java_label.as_str(), active_style),
|
||||
Span::styled("Runtime ", label_style),
|
||||
Span::styled(
|
||||
format!("{memory_min}-{memory_max}, {java_source}"),
|
||||
active_style,
|
||||
),
|
||||
]),
|
||||
Line::from(vec![
|
||||
Span::styled("Desktop ", label_style),
|
||||
Span::styled("Profile ", label_style),
|
||||
Span::styled(
|
||||
inst.config_sync_profile
|
||||
.as_deref()
|
||||
.unwrap_or(LOCAL_PROFILE_LABEL),
|
||||
active_style,
|
||||
),
|
||||
Span::styled(" / desk ", label_style),
|
||||
Span::styled(desktop, active_style),
|
||||
]),
|
||||
];
|
||||
|
||||
frame.render_widget(Paragraph::new(lines), area);
|
||||
}
|
||||
|
||||
fn render_profile_list(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
focused: FocusedArea,
|
||||
state: &mut SettingsState,
|
||||
) {
|
||||
let is_focused = focused == FocusedArea::Settings;
|
||||
let pane = state.pane;
|
||||
let active_profile = state.active_profile.clone();
|
||||
let profiles = state.profiles.clone();
|
||||
let count = profiles.len() + 1;
|
||||
let last = count.saturating_sub(1);
|
||||
state.list_state.selected = Some(state.list_state.selected.unwrap_or(0).min(last));
|
||||
|
||||
let builder = ListBuilder::new(move |context| {
|
||||
let theme = THEME.as_ref();
|
||||
let name = if context.index == 0 {
|
||||
LOCAL_PROFILE_LABEL.to_string()
|
||||
} else {
|
||||
profiles.get(context.index - 1).cloned().unwrap_or_default()
|
||||
};
|
||||
let is_active = if context.index == 0 {
|
||||
active_profile.is_none()
|
||||
} else {
|
||||
active_profile.as_deref() == Some(name.as_str())
|
||||
};
|
||||
let show_selected = is_focused && pane == SettingsPane::Profile && context.is_selected;
|
||||
let marker = if is_active { "\u{25b8} " } else { " " };
|
||||
let background = if show_selected {
|
||||
theme.stripe()
|
||||
} else {
|
||||
theme.background()
|
||||
};
|
||||
let style = if show_selected {
|
||||
Style::default()
|
||||
.fg(theme.accent())
|
||||
.add_modifier(Modifier::BOLD)
|
||||
} else if is_active {
|
||||
Style::default()
|
||||
.fg(theme.text())
|
||||
.add_modifier(Modifier::BOLD)
|
||||
} else {
|
||||
Style::default().fg(theme.text())
|
||||
};
|
||||
let line = Line::from(vec![
|
||||
Span::styled(marker, Style::default().fg(theme.success())),
|
||||
Span::styled(name, style),
|
||||
]);
|
||||
(
|
||||
ratatui::text::Text::from(line).style(Style::default().bg(background)),
|
||||
1,
|
||||
)
|
||||
});
|
||||
|
||||
frame.render_stateful_widget(ListView::new(builder, count), area, &mut state.list_state);
|
||||
}
|
||||
|
||||
fn render_add_profile_popup(frame: &mut Frame, name: &str) {
|
||||
use super::popups::{base::PopupFrame, keybind_line};
|
||||
let theme = THEME.as_ref();
|
||||
let area = popup_area(frame, 42, 5);
|
||||
let name = name.to_string();
|
||||
|
||||
let border_color = theme.text_dim();
|
||||
let bg_color = theme.surface();
|
||||
let dim_color = theme.text_dim();
|
||||
let text_color = theme.text();
|
||||
|
||||
PopupFrame {
|
||||
title: Line::from(Span::styled(
|
||||
" Config Profile ",
|
||||
Style::default()
|
||||
.fg(border_color)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
))
|
||||
.centered(),
|
||||
border_color,
|
||||
bg: Some(bg_color),
|
||||
keybinds: Some(keybind_line(&[("Enter", " create"), ("Esc", " cancel")])),
|
||||
search_line: None,
|
||||
content: Box::new(move |inner, buf| {
|
||||
let line = if name.is_empty() {
|
||||
Line::from(vec![
|
||||
Span::styled("Profile name...", Style::default().fg(dim_color)),
|
||||
Span::styled(
|
||||
"\u{2588}",
|
||||
Style::default()
|
||||
.fg(border_color)
|
||||
.add_modifier(Modifier::SLOW_BLINK),
|
||||
),
|
||||
])
|
||||
} else {
|
||||
Line::from(vec![
|
||||
Span::styled(name.as_str(), Style::default().fg(text_color)),
|
||||
Span::styled(
|
||||
"\u{2588}",
|
||||
Style::default()
|
||||
.fg(border_color)
|
||||
.add_modifier(Modifier::SLOW_BLINK),
|
||||
),
|
||||
])
|
||||
};
|
||||
Paragraph::new(line).render(inner, buf);
|
||||
}),
|
||||
}
|
||||
.render(area, frame.buffer_mut());
|
||||
}
|
||||
|
||||
fn popup_area(frame: &Frame, width: u16, height: u16) -> Rect {
|
||||
let area = frame.area();
|
||||
let x = area.x + (area.width.saturating_sub(width)) / 2;
|
||||
let y = area.y + (area.height.saturating_sub(height)) / 2;
|
||||
Rect {
|
||||
x,
|
||||
y,
|
||||
width: width.min(area.width),
|
||||
height: height.min(area.height),
|
||||
}
|
||||
}
|
||||
|
||||
pub enum SettingsAction {
|
||||
None,
|
||||
EditInstance(PathBuf),
|
||||
EditGlobal(PathBuf),
|
||||
ToggleDesktop,
|
||||
SelectProfile(Option<String>),
|
||||
ConfirmDeleteProfile(String),
|
||||
Error(String),
|
||||
}
|
||||
|
||||
pub fn handle_key(
|
||||
key_event: &KeyEvent,
|
||||
state: &mut SettingsState,
|
||||
instance: Option<&InstanceConfig>,
|
||||
instances_dir: &Path,
|
||||
) -> SettingsAction {
|
||||
if let AddMode::ProfileName(name) = &state.add_mode {
|
||||
match key_event.code {
|
||||
KeyCode::Enter => {
|
||||
let name = name.trim().to_string();
|
||||
state.add_mode = AddMode::None;
|
||||
if name.is_empty() {
|
||||
return SettingsAction::None;
|
||||
}
|
||||
return match crate::instance::config_sync::create_profile(&state.meta_dir, &name) {
|
||||
Ok(profile) => {
|
||||
state.reload_profiles();
|
||||
state.active_profile = Some(profile);
|
||||
state.select_active();
|
||||
SettingsAction::SelectProfile(state.active_profile.clone())
|
||||
}
|
||||
Err(e) => SettingsAction::Error(e.to_string()),
|
||||
};
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
state.add_mode = AddMode::None;
|
||||
return SettingsAction::None;
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
let mut new_name = name.clone();
|
||||
super::search::backspace(&mut new_name, key_event.modifiers);
|
||||
state.add_mode = AddMode::ProfileName(new_name);
|
||||
return SettingsAction::None;
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
let mut new_name = name.clone();
|
||||
new_name.push(c);
|
||||
state.add_mode = AddMode::ProfileName(new_name);
|
||||
return SettingsAction::None;
|
||||
}
|
||||
_ => return SettingsAction::None,
|
||||
}
|
||||
}
|
||||
|
||||
match key_event.code {
|
||||
KeyCode::Char('h') | KeyCode::Left => {
|
||||
state.pane = SettingsPane::Profile;
|
||||
SettingsAction::None
|
||||
}
|
||||
KeyCode::Char('l') | KeyCode::Right => {
|
||||
state.pane = SettingsPane::Info;
|
||||
SettingsAction::None
|
||||
}
|
||||
KeyCode::Enter if state.pane == SettingsPane::Profile => {
|
||||
let selected = state.list_state.selected.unwrap_or(0);
|
||||
let profile = if selected == 0 {
|
||||
None
|
||||
} else {
|
||||
state.profiles.get(selected - 1).cloned()
|
||||
};
|
||||
SettingsAction::SelectProfile(profile)
|
||||
}
|
||||
KeyCode::Char('a') if state.pane == SettingsPane::Profile => {
|
||||
state.add_mode = AddMode::ProfileName(String::new());
|
||||
SettingsAction::None
|
||||
}
|
||||
KeyCode::Char('d') if state.pane == SettingsPane::Profile => {
|
||||
let selected = state.list_state.selected.unwrap_or(0);
|
||||
if selected == 0 {
|
||||
SettingsAction::None
|
||||
} else if let Some(profile) = state.profiles.get(selected - 1) {
|
||||
SettingsAction::ConfirmDeleteProfile(profile.clone())
|
||||
} else {
|
||||
SettingsAction::None
|
||||
}
|
||||
}
|
||||
KeyCode::Char('j') | KeyCode::Down if state.pane == SettingsPane::Profile => {
|
||||
let count = state.count();
|
||||
if count > 0 {
|
||||
let cur = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some((cur + 1).min(count - 1));
|
||||
}
|
||||
SettingsAction::None
|
||||
}
|
||||
KeyCode::Char('k') | KeyCode::Up if state.pane == SettingsPane::Profile => {
|
||||
let cur = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some(cur.saturating_sub(1));
|
||||
SettingsAction::None
|
||||
}
|
||||
KeyCode::Char('e') if state.pane == SettingsPane::Info => {
|
||||
if let Some(inst) = instance {
|
||||
let path = instances_dir.join(&inst.name).join("instance.json");
|
||||
SettingsAction::EditInstance(path)
|
||||
} else {
|
||||
SettingsAction::None
|
||||
}
|
||||
}
|
||||
KeyCode::Char('g') if state.pane == SettingsPane::Info => {
|
||||
let path = crate::config::get_config_path().join("config.toml");
|
||||
SettingsAction::EditGlobal(path)
|
||||
}
|
||||
KeyCode::Char('d') if state.pane == SettingsPane::Info => SettingsAction::ToggleDesktop,
|
||||
_ => SettingsAction::None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../tests/widgets/settings.rs"]
|
||||
mod tests;
|
||||
Reference in new issue
Block a user