From 24a8afe29b7552505067857e36311f9f02b7cd6a Mon Sep 17 00:00:00 2001 From: objz Date: Sat, 15 Nov 2025 17:31:07 +0100 Subject: [PATCH] feat(tui): add worlds tab with dynamic icon heights --- src/instance/mod.rs | 2 + src/instance/worlds.rs | 53 ++++++++++++++++++++++++++ src/tui/layout.rs | 17 +++++++++ src/tui/widgets/content.rs | 31 ++++++++++++--- src/tui/widgets/content_list.rs | 58 +++++++++++++++++++++++++---- src/tui/widgets/screenshots_grid.rs | 36 +++++++++++++++++- 6 files changed, 182 insertions(+), 15 deletions(-) create mode 100644 src/instance/worlds.rs diff --git a/src/instance/mod.rs b/src/instance/mod.rs index 84e6a60..4adbbd8 100644 --- a/src/instance/mod.rs +++ b/src/instance/mod.rs @@ -6,6 +6,7 @@ pub mod mods; pub mod resource_packs; pub mod screenshots; pub mod shaders; +pub mod worlds; pub use launch::LaunchError; pub use loader::{get_installer, GameVersion, ModLoaderInstaller, VanillaInstaller}; @@ -14,3 +15,4 @@ pub use models::{InstanceConfig, ModLoader}; pub use mods::{scan_mods, toggle_mod, ModEntry}; pub use resource_packs::scan_resource_packs; pub use shaders::scan_shaders; +pub use worlds::scan_worlds; diff --git a/src/instance/worlds.rs b/src/instance/worlds.rs new file mode 100644 index 0000000..5f79a32 --- /dev/null +++ b/src/instance/worlds.rs @@ -0,0 +1,53 @@ +use std::path::Path; + +use super::mods::{make_icon_pixels, ModEntry}; + +pub fn scan_worlds(instances_dir: &Path, instance_name: &str) -> Vec { + let saves_dir = instances_dir + .join(instance_name) + .join(".minecraft") + .join("saves"); + + let read_dir = match std::fs::read_dir(&saves_dir) { + Ok(rd) => rd, + Err(_) => return Vec::new(), + }; + + let mut entries = Vec::new(); + + for entry in read_dir.flatten() { + let path = entry.path(); + if !path.is_dir() { + continue; + } + + let file_name = match path.file_name().and_then(|n| n.to_str()) { + Some(n) => n.to_string(), + None => continue, + }; + + let (enabled, file_stem) = if file_name.ends_with(".disabled") { + (false, file_name.trim_end_matches(".disabled").to_string()) + } else { + (true, file_name.clone()) + }; + + let icon_bytes = std::fs::read(path.join("icon.png")).ok(); + let icon_lines = icon_bytes + .as_ref() + .and_then(|bytes| make_icon_pixels(bytes, 12, 6)); + + entries.push(ModEntry { + file_stem: file_stem.clone(), + name: file_stem, + description: String::new(), + enabled, + icon_bytes, + path, + icon_lines, + }); + } + + entries.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase())); + entries +} diff --git a/src/tui/layout.rs b/src/tui/layout.rs index b89223e..fbf18ee 100644 --- a/src/tui/layout.rs +++ b/src/tui/layout.rs @@ -34,6 +34,7 @@ pub struct App { mods_state: widgets::content_list::ContentListState, resource_packs_state: widgets::content_list::ContentListState, shaders_state: widgets::content_list::ContentListState, + worlds_state: widgets::content_list::ContentListState, screenshots_state: widgets::screenshots_grid::ScreenshotsState, picker: ratatui_image::picker::Picker, instance_manager: InstanceManager, @@ -95,6 +96,7 @@ impl App { mods_state: widgets::content_list::ContentListState::default(), resource_packs_state: widgets::content_list::ContentListState::default(), shaders_state: widgets::content_list::ContentListState::default(), + worlds_state: widgets::content_list::ContentListState::default(), screenshots_state: { let mut s = widgets::screenshots_grid::ScreenshotsState::default(); s.font_size = picker.font_size(); @@ -125,6 +127,7 @@ impl App { self.mods_state.drain_pending(); self.resource_packs_state.drain_pending(); self.shaders_state.drain_pending(); + self.worlds_state.drain_pending(); self.screenshots_state.drain_pending_entries(); self.screenshots_state.request_visible_loads(); self.create_screenshot_protocols(); @@ -175,6 +178,7 @@ impl App { &mut self.mods_state, &mut self.resource_packs_state, &mut self.shaders_state, + &mut self.worlds_state, &mut self.screenshots_state, &self.instance_manager.instances_dir, ); @@ -308,6 +312,13 @@ impl App { ) { return Ok(()); } + } else if self.content_tab == widgets::content::ContentTab::Worlds { + if widgets::content_list::handle_key_no_toggle( + &key_event, + &mut self.worlds_state, + ) { + return Ok(()); + } } else { let state = match self.content_tab { widgets::content::ContentTab::Mods => Some(&mut self.mods_state), @@ -387,6 +398,12 @@ impl App { KeyCode::Enter if self.focused == FocusedArea::Profiles && !self.profiles_state.search.active => + { + self.focused = FocusedArea::Content; + } + KeyCode::Char('l') + if self.focused == FocusedArea::Profiles + && !self.profiles_state.search.active => { if let Some(instance) = self.profiles_state.selected_instance().cloned() { let can_launch = matches!( diff --git a/src/tui/widgets/content.rs b/src/tui/widgets/content.rs index 8c33d77..036a042 100644 --- a/src/tui/widgets/content.rs +++ b/src/tui/widgets/content.rs @@ -71,6 +71,7 @@ pub fn render( mods_state: &mut super::content_list::ContentListState, resource_packs_state: &mut super::content_list::ContentListState, shaders_state: &mut super::content_list::ContentListState, + worlds_state: &mut super::content_list::ContentListState, screenshots_state: &mut super::screenshots_grid::ScreenshotsState, instances_dir: &std::path::Path, ) { @@ -217,12 +218,30 @@ pub fn render( ); } } - _ => { - frame.render_widget( - Paragraph::new("No worlds saved.") - .style(Style::default().fg(THEME.colors.text_idle)), - content_area, - ); + ContentTab::Worlds => { + if let Some(instance) = instance { + if worlds_state.loaded_for.as_deref() != Some(instance.name.as_str()) { + worlds_state.start_load( + instances_dir, + &instance.name, + crate::instance::scan_worlds, + ); + } + super::content_list::render( + frame, + content_area, + worlds_state, + is_focused, + "Loading worlds...", + "No worlds saved.", + ); + } else { + frame.render_widget( + Paragraph::new("No instance selected.") + .style(Style::default().fg(THEME.colors.text_idle)), + content_area, + ); + } } } } diff --git a/src/tui/widgets/content_list.rs b/src/tui/widgets/content_list.rs index 2fcc2a4..e709cd5 100644 --- a/src/tui/widgets/content_list.rs +++ b/src/tui/widgets/content_list.rs @@ -172,6 +172,47 @@ impl ContentListState { } } +pub fn handle_key_no_toggle(key_event: &KeyEvent, state: &mut ContentListState) -> bool { + match key_event.code { + KeyCode::Char('j') | KeyCode::Down => { + let count = state.entries.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)); + state.update_scrollbar(); + true + } + KeyCode::Char('k') | KeyCode::Up => { + let current = state.list_state.selected.unwrap_or(0); + state.list_state.selected = Some(current.saturating_sub(1)); + state.update_scrollbar(); + true + } + KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => { + if let Some(entry) = state + .list_state + .selected + .and_then(|i| state.entries.get(i)) + { + if let Some(dir) = entry.path.parent() { + if let Err(e) = std::process::Command::new("xdg-open") + .arg(dir) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + { + tracing::error!("Failed to open directory: {}", e); + } + } + } + true + } + _ => false, + } +} + /// Returns `true` if the key was consumed. pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool { match key_event.code { @@ -310,6 +351,9 @@ pub fn render( let item = Text::from(vec![Line::from(line)]).style(Style::default().bg(background)); (item, 1) } else if has_icon { + let icon_row_count = icon_pixels.as_ref().map(|r| r.len()).unwrap_or(0); + let height = icon_row_count.max(2) as u16; + let mut line_0 = icon_spans(icon_pixels.as_ref(), 0); line_0.push(Span::raw(" ")); if use_mc_colors { @@ -326,15 +370,13 @@ pub fn render( line_1.push(Span::styled(stripped_desc, description_style)); } - let line_2 = icon_spans(icon_pixels.as_ref(), 2); + let mut lines = vec![Line::from(line_0), Line::from(line_1)]; + for r in 2..icon_row_count { + lines.push(Line::from(icon_spans(icon_pixels.as_ref(), r))); + } - let item = Text::from(vec![ - Line::from(line_0), - Line::from(line_1), - Line::from(line_2), - ]) - .style(Style::default().bg(background)); - (item, 3) + let item = Text::from(lines).style(Style::default().bg(background)); + (item, height) } else { let mut line_0 = Vec::new(); line_0.push(Span::raw(" ")); diff --git a/src/tui/widgets/screenshots_grid.rs b/src/tui/widgets/screenshots_grid.rs index 5528acb..6202af8 100644 --- a/src/tui/widgets/screenshots_grid.rs +++ b/src/tui/widgets/screenshots_grid.rs @@ -7,7 +7,7 @@ use ratatui::{ layout::{Constraint, Layout, Rect}, style::{Modifier, Style}, text::Span, - widgets::Paragraph, + widgets::{Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState}, Frame, }; use ratatui_image::{protocol::StatefulProtocol, Resize, StatefulImage}; @@ -31,6 +31,7 @@ pub struct ScreenshotsState { pub loading: bool, cols: usize, visible_rows: usize, + pub scrollbar_state: ScrollbarState, pub font_size: (u16, u16), pending_entries: Arc)>>>, pending_images: Arc>>, @@ -48,6 +49,7 @@ impl Default for ScreenshotsState { loading: false, cols: 3, visible_rows: 2, + scrollbar_state: ScrollbarState::default(), font_size: (8, 16), pending_entries: Arc::new(Mutex::new(None)), pending_images: Arc::new(Mutex::new(Vec::new())), @@ -154,6 +156,16 @@ impl ScreenshotsState { } else if row >= self.scroll_row + self.visible_rows { self.scroll_row = row.saturating_sub(self.visible_rows - 1); } + + let total = self.total_rows().saturating_sub(1); + self.scrollbar_state = ScrollbarState::new(total).position(self.scroll_row); + } + + fn total_rows(&self) -> usize { + if self.cols == 0 { + return 0; + } + (self.entries.len() + self.cols - 1) / self.cols } } @@ -314,4 +326,26 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut ScreenshotsState, is_fo ); } } + + let scrollbar_area = Rect { + x: area.x + area.width.saturating_sub(0), + y: area.y + 1, + width: 1, + height: area.height.saturating_sub(2), + }; + frame.render_stateful_widget( + Scrollbar::default() + .orientation(ScrollbarOrientation::VerticalRight) + .begin_symbol(Some("\u{25b2}")) + .style( + Style::default() + .fg(THEME.colors.border_focused) + .add_modifier(Modifier::BOLD), + ) + .thumb_symbol("\u{2551}") + .track_symbol(Some("")) + .end_symbol(Some("\u{25bc}")), + scrollbar_area, + &mut state.scrollbar_state, + ); }