feat(tui): add mods tab with icons and toggle, fix version sorting
This commit is contained in:
1 parent
2adac8306b
commit
f65341c0a2
9 files changed
+885
-19
No files matched your search
@@ -31,6 +31,7 @@ pub struct App {
|
||||
pre_overlay_focused: FocusedArea,
|
||||
content_tab: widgets::content::ContentTab,
|
||||
profiles_state: profiles::State,
|
||||
mods_state: widgets::mods_list::ModsState,
|
||||
instance_manager: InstanceManager,
|
||||
log_list_state: tui_logger::TuiWidgetState,
|
||||
throbber_state: throbber_widgets_tui::ThrobberState,
|
||||
@@ -87,6 +88,7 @@ impl Default for App {
|
||||
pre_overlay_focused: FocusedArea::default(),
|
||||
content_tab: widgets::content::ContentTab::default(),
|
||||
profiles_state,
|
||||
mods_state: widgets::mods_list::ModsState::default(),
|
||||
instance_manager: manager,
|
||||
log_list_state: tui_logger::TuiWidgetState::new()
|
||||
.set_default_display_level(log::LevelFilter::Debug),
|
||||
@@ -152,6 +154,8 @@ impl App {
|
||||
self.focused,
|
||||
self.content_tab,
|
||||
self.profiles_state.selected_instance(),
|
||||
&mut self.mods_state,
|
||||
&self.instance_manager.instances_dir,
|
||||
);
|
||||
|
||||
let bottom_chunks = Layout::default()
|
||||
@@ -275,6 +279,18 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
if self.focused == FocusedArea::Content
|
||||
&& self.content_tab == widgets::content::ContentTab::Mods
|
||||
{
|
||||
if widgets::mods_list::handle_key(
|
||||
&key_event,
|
||||
&mut self.mods_state,
|
||||
&self.instance_manager.instances_dir,
|
||||
) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
match self.focused {
|
||||
FocusedArea::Popup => {
|
||||
new_instance::handle_key(&key_event, &mut self.profiles_state);
|
||||
|
||||
@@ -68,6 +68,8 @@ pub fn render(
|
||||
focused: FocusedArea,
|
||||
tab: ContentTab,
|
||||
instance: Option<&crate::instance::InstanceConfig>,
|
||||
mods_state: &mut super::mods_list::ModsState,
|
||||
instances_dir: &std::path::Path,
|
||||
) {
|
||||
let border_color = if focused == FocusedArea::Content {
|
||||
THEME.colors.border_focused
|
||||
@@ -102,6 +104,20 @@ pub fn render(
|
||||
frame.render_widget(tabs, tabs_area);
|
||||
|
||||
match tab {
|
||||
ContentTab::Mods => {
|
||||
if let Some(instance) = instance {
|
||||
if mods_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||
mods_state.load_mods(instances_dir, &instance.name);
|
||||
}
|
||||
super::mods_list::render(frame, content_area, mods_state);
|
||||
} else {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No instance selected.")
|
||||
.style(Style::default().fg(THEME.colors.text_idle)),
|
||||
content_area,
|
||||
);
|
||||
}
|
||||
}
|
||||
ContentTab::Logs => {
|
||||
let lines = instance
|
||||
.map(|i| crate::instance_logs::get_all(&i.name))
|
||||
@@ -123,7 +139,7 @@ pub fn render(
|
||||
}
|
||||
_ => {
|
||||
let body = match tab {
|
||||
ContentTab::Mods => "No mods installed.",
|
||||
ContentTab::Mods => unreachable!(),
|
||||
ContentTab::ResourcePacks => "No resource packs installed.",
|
||||
ContentTab::Shaders => "No shaders installed.",
|
||||
ContentTab::Screenshots => "No screenshots.",
|
||||
@@ -190,7 +206,7 @@ pub fn title(
|
||||
.fg(THEME.colors.success)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
)
|
||||
.throbber_set(throbber_widgets_tui::PARENTHESIS)
|
||||
.throbber_set(throbber_widgets_tui::BRAILLE_EIGHT_DOUBLE)
|
||||
.use_type(throbber_widgets_tui::WhichUse::Spin);
|
||||
frame.render_stateful_widget(throbber, left_area, throbber_state);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use crate::tui::theme::THEME;
|
||||
pub mod account;
|
||||
pub mod content;
|
||||
pub mod details;
|
||||
pub mod mods_list;
|
||||
pub mod popups;
|
||||
pub mod profiles;
|
||||
pub mod search;
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
use std::path::Path;
|
||||
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use ratatui::{
|
||||
layout::Rect,
|
||||
style::{Color, Modifier, Style},
|
||||
text::{Line, Span, Text},
|
||||
widgets::Paragraph,
|
||||
Frame,
|
||||
};
|
||||
use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView};
|
||||
|
||||
use crate::instance::mods::{scan_mods, toggle_mod, ModEntry};
|
||||
use crate::tui::theme::THEME;
|
||||
|
||||
type IconCell = (u8, u8, u8, u8, u8, u8);
|
||||
|
||||
pub struct ModsState {
|
||||
pub mods: Vec<ModEntry>,
|
||||
pub list_state: TuiListState,
|
||||
pub loaded_for: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for ModsState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mods: Vec::new(),
|
||||
list_state: TuiListState::default(),
|
||||
loaded_for: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ModsState {
|
||||
pub fn load_mods(&mut self, instances_dir: &Path, instance_name: &str) {
|
||||
self.mods = scan_mods(instances_dir, instance_name);
|
||||
self.loaded_for = Some(instance_name.to_string());
|
||||
self.list_state = TuiListState::default();
|
||||
if !self.mods.is_empty() {
|
||||
self.list_state.selected = Some(0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle_selected(&mut self, instances_dir: &Path) {
|
||||
let Some(index) = self.list_state.selected else {
|
||||
return;
|
||||
};
|
||||
let Some(entry) = self.mods.get(index).cloned() else {
|
||||
return;
|
||||
};
|
||||
|
||||
match toggle_mod(&entry) {
|
||||
Ok(()) => {
|
||||
let loaded_for = self.loaded_for.clone();
|
||||
if let Some(instance_name) = loaded_for.as_deref() {
|
||||
self.load_mods(instances_dir, instance_name);
|
||||
if !self.mods.is_empty() {
|
||||
self.list_state.selected = Some(index.min(self.mods.len() - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::error!("Failed to toggle mod '{}': {}", entry.file_stem, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the key was consumed by the mods list.
|
||||
pub fn handle_key(key_event: &KeyEvent, state: &mut ModsState, instances_dir: &Path) -> bool {
|
||||
match key_event.code {
|
||||
KeyCode::Char('j') | KeyCode::Down => {
|
||||
let count = state.mods.len();
|
||||
if count == 0 {
|
||||
return true;
|
||||
}
|
||||
let current = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some((current + 1).min(count - 1));
|
||||
true
|
||||
}
|
||||
KeyCode::Char('k') | KeyCode::Up => {
|
||||
let current = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some(current.saturating_sub(1));
|
||||
true
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
state.toggle_selected(instances_dir);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(frame: &mut Frame, area: Rect, state: &mut ModsState) {
|
||||
if state.mods.is_empty() {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No mods installed.").style(Style::default().fg(THEME.colors.text_idle)),
|
||||
area,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let count = state.mods.len();
|
||||
const ITEM_HEIGHT: u16 = 2;
|
||||
|
||||
let mods_snapshot: Vec<(String, String, bool, Option<Vec<Vec<IconCell>>>)> = state
|
||||
.mods
|
||||
.iter()
|
||||
.map(|mod_entry| {
|
||||
(
|
||||
mod_entry.name.clone(),
|
||||
mod_entry.description.clone(),
|
||||
mod_entry.enabled,
|
||||
mod_entry.icon_lines.clone(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let builder = ListBuilder::new(move |context| {
|
||||
let (name, description, enabled, icon_pixels) = &mods_snapshot[context.index];
|
||||
|
||||
let (name_style, description_style, background) = if context.is_selected {
|
||||
(
|
||||
Style::default()
|
||||
.fg(THEME.colors.row_highlight)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
Style::default().fg(THEME.colors.row_highlight),
|
||||
THEME.colors.row_background,
|
||||
)
|
||||
} else {
|
||||
(
|
||||
Style::default()
|
||||
.fg(THEME.colors.foreground)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
Style::default().fg(THEME.colors.text_idle),
|
||||
THEME.colors.row_alternate_bg,
|
||||
)
|
||||
};
|
||||
|
||||
let (status_text, status_style) = if *enabled {
|
||||
("✓", Style::default().fg(THEME.colors.success))
|
||||
} else {
|
||||
("✗", Style::default().fg(THEME.colors.error))
|
||||
};
|
||||
|
||||
let icon_row_0 = icon_spans(icon_pixels.as_ref(), 0);
|
||||
let icon_row_1 = icon_spans(icon_pixels.as_ref(), 1);
|
||||
|
||||
let mut line_0 = icon_row_0;
|
||||
line_0.push(Span::raw(" "));
|
||||
line_0.push(Span::styled(name.clone(), name_style));
|
||||
line_0.push(Span::raw(" "));
|
||||
line_0.push(Span::styled(status_text, status_style));
|
||||
|
||||
let mut line_1 = icon_row_1;
|
||||
line_1.push(Span::raw(" "));
|
||||
line_1.push(Span::styled(description.clone(), description_style));
|
||||
|
||||
let item = Text::from(vec![Line::from(line_0), Line::from(line_1)])
|
||||
.style(Style::default().bg(background));
|
||||
(item, ITEM_HEIGHT)
|
||||
});
|
||||
|
||||
let list = ListView::new(builder, count);
|
||||
frame.render_stateful_widget(list, area, &mut state.list_state);
|
||||
}
|
||||
|
||||
fn icon_spans(icon_pixels: Option<&Vec<Vec<IconCell>>>, row: usize) -> Vec<Span<'static>> {
|
||||
match icon_pixels.and_then(|rows| rows.get(row)) {
|
||||
Some(cols) => cols
|
||||
.iter()
|
||||
.map(|&(fg_r, fg_g, fg_b, bg_r, bg_g, bg_b)| {
|
||||
Span::styled(
|
||||
"▄",
|
||||
Style::default()
|
||||
.fg(Color::Rgb(fg_r, fg_g, fg_b))
|
||||
.bg(Color::Rgb(bg_r, bg_g, bg_b)),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
None => vec![Span::styled(
|
||||
" ",
|
||||
Style::default().fg(THEME.colors.text_idle),
|
||||
)],
|
||||
}
|
||||
}
|
||||
@@ -705,8 +705,9 @@ fn ensure_versions_loaded(state: &mut WizardState) {
|
||||
let client = crate::net::HttpClient::new();
|
||||
let installer = get_installer(loader);
|
||||
match installer.get_game_versions(&client).await {
|
||||
Ok(versions) => match versions_arc.lock() {
|
||||
Ok(mut versions) => match versions_arc.lock() {
|
||||
Ok(mut s) => {
|
||||
sort_versions_semver(&mut versions);
|
||||
s.versions = LoadState::Loaded(versions);
|
||||
clamp_version_index(&mut s);
|
||||
}
|
||||
@@ -761,3 +762,25 @@ fn ensure_loader_versions_loaded(state: &mut WizardState, loader: ModLoader, gam
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn compare_semver(a: &str, b: &str) -> std::cmp::Ordering {
|
||||
let parse_parts = |s: &str| -> Vec<u64> {
|
||||
s.split('.')
|
||||
.map(|p| p.parse::<u64>().unwrap_or(0))
|
||||
.collect()
|
||||
};
|
||||
let a_parts = parse_parts(a);
|
||||
let b_parts = parse_parts(b);
|
||||
for (ap, bp) in a_parts.iter().zip(b_parts.iter()) {
|
||||
match ap.cmp(bp) {
|
||||
std::cmp::Ordering::Equal => continue,
|
||||
other => return other,
|
||||
}
|
||||
}
|
||||
a_parts.len().cmp(&b_parts.len())
|
||||
}
|
||||
|
||||
fn sort_versions_semver(versions: &mut Vec<GameVersion>) {
|
||||
// Sort newest first (descending), so compare b vs a
|
||||
versions.sort_by(|a, b| compare_semver(&b.id, &a.id));
|
||||
}
|
||||
Reference in new issue
Block a user