feat(tui): add worlds tab with dynamic icon heights
This commit is contained in:
1 parent
c3a26eaac2
commit
24a8afe29b
6 files changed
+182
-15
No files matched your search
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(" "));
|
||||
|
||||
@@ -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<Mutex<Option<(String, Vec<ScreenshotEntry>)>>>,
|
||||
pending_images: Arc<Mutex<Vec<(usize, image::DynamicImage)>>>,
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user