feat(worlds): add metadata cards and quick launch
This commit is contained in:
1 parent
4eed312c3d
commit
ea7478eb27
22 files changed
+585
-88
No files matched your search
@@ -31,6 +31,7 @@ pub struct App {
|
||||
pub(super) shaders_state: widgets::content::list::ContentListState,
|
||||
pub(super) shaders_discovery_state: widgets::content::DiscoveryState,
|
||||
pub(super) worlds_state: widgets::content::list::ContentListState,
|
||||
pub(super) world_quick_play_support: Option<(String, String, bool)>,
|
||||
pub(super) screenshots_state: widgets::screenshots_grid::ScreenshotsState,
|
||||
pub(super) logs_state: widgets::logs_viewer::LogsState,
|
||||
pub(super) account_state: widgets::account::AccountState,
|
||||
@@ -80,6 +81,28 @@ pub enum FocusedArea {
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub(super) fn selected_instance_supports_quick_play(&mut self) -> bool {
|
||||
let Some((name, game_version)) = self
|
||||
.instances_state
|
||||
.selected_instance()
|
||||
.map(|instance| (instance.name.clone(), instance.game_version.clone()))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
if let Some((cached_name, cached_version, supported)) = &self.world_quick_play_support
|
||||
&& cached_name == &name
|
||||
&& cached_version == &game_version
|
||||
{
|
||||
return *supported;
|
||||
}
|
||||
let supported = crate::instance::launch::supports_quick_play(
|
||||
&self.instance_manager.meta_dir,
|
||||
&game_version,
|
||||
);
|
||||
self.world_quick_play_support = Some((name, game_version, supported));
|
||||
supported
|
||||
}
|
||||
|
||||
pub fn new(picker: ratatui_image::picker::Picker) -> Self {
|
||||
let instances_dir = crate::config::SETTINGS.paths.resolve_instances_dir();
|
||||
let meta_dir = crate::config::SETTINGS.paths.resolve_meta_dir();
|
||||
@@ -123,6 +146,7 @@ impl App {
|
||||
crate::instance::ContentKind::Shader,
|
||||
),
|
||||
worlds_state: widgets::content::list::ContentListState::default(),
|
||||
world_quick_play_support: None,
|
||||
logs_state: widgets::logs_viewer::LogsState::default(),
|
||||
account_state: widgets::account::AccountState::default(),
|
||||
settings_state: widgets::settings::SettingsState::new(manager.meta_dir.clone()),
|
||||
|
||||
+23
-2
@@ -552,7 +552,11 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn spawn_launch(&self, instance: crate::instance::InstanceConfig) {
|
||||
pub(super) fn spawn_launch(
|
||||
&self,
|
||||
instance: crate::instance::InstanceConfig,
|
||||
quick_play_world: Option<String>,
|
||||
) {
|
||||
use crate::instance::launch;
|
||||
use crate::instance::runtime;
|
||||
|
||||
@@ -569,13 +573,30 @@ impl App {
|
||||
}
|
||||
};
|
||||
|
||||
let can_launch = matches!(
|
||||
runtime::get(&instance.name),
|
||||
None | Some(runtime::RunState::Crashed(_))
|
||||
);
|
||||
if !can_launch {
|
||||
return;
|
||||
}
|
||||
runtime::remove(&instance.name);
|
||||
crate::instance::logs::live::clear(&instance.name);
|
||||
|
||||
runtime::set_state(&instance.name, runtime::RunState::Authenticating);
|
||||
|
||||
let instances_dir = self.instance_manager.instances_dir.clone();
|
||||
let meta_dir = self.instance_manager.meta_dir.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = launch::launch(&instance, &instances_dir, &meta_dir).await {
|
||||
if let Err(e) = launch::launch(
|
||||
&instance,
|
||||
&instances_dir,
|
||||
&meta_dir,
|
||||
quick_play_world.as_deref(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::error!("Failed to launch '{}': {}", instance.name, e);
|
||||
runtime::remove(&instance.name);
|
||||
}
|
||||
|
||||
+16
-9
@@ -366,6 +366,21 @@ impl App {
|
||||
return Ok(());
|
||||
}
|
||||
} else if self.content_tab == widgets::content::ContentTab::Worlds {
|
||||
if key_event.code == KeyCode::Char('q') && !self.worlds_state.search.active {
|
||||
if self.selected_instance_supports_quick_play() {
|
||||
let instance = self.instances_state.selected_instance().cloned();
|
||||
let world = self
|
||||
.worlds_state
|
||||
.selected_entry()
|
||||
.and_then(|entry| entry.path.file_name())
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::to_owned);
|
||||
if let (Some(instance), Some(world)) = (instance, world) {
|
||||
self.spawn_launch(instance, Some(world));
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if key_event.code == KeyCode::Char('d') && !self.worlds_state.search.active {
|
||||
if let Some(pending) = self.worlds_state.pending_delete() {
|
||||
self.confirm_content_delete(pending);
|
||||
@@ -631,15 +646,7 @@ impl App {
|
||||
&& !self.instances_state.search.active =>
|
||||
{
|
||||
if let Some(instance) = self.instances_state.selected_instance().cloned() {
|
||||
let can_launch = matches!(
|
||||
crate::instance::runtime::get(&instance.name),
|
||||
None | Some(crate::instance::runtime::RunState::Crashed(_))
|
||||
);
|
||||
if can_launch {
|
||||
crate::instance::runtime::remove(&instance.name);
|
||||
crate::instance::logs::live::clear(&instance.name);
|
||||
self.spawn_launch(instance);
|
||||
}
|
||||
self.spawn_launch(instance, None);
|
||||
}
|
||||
}
|
||||
KeyCode::Char('r')
|
||||
|
||||
@@ -60,6 +60,7 @@ impl App {
|
||||
self.instances_state.selected_instance(),
|
||||
&mut self.throbber_state,
|
||||
);
|
||||
let world_quick_play_supported = self.selected_instance_supports_quick_play();
|
||||
if load_content {
|
||||
widgets::content::render(
|
||||
frame,
|
||||
@@ -79,6 +80,7 @@ impl App {
|
||||
&mut self.logs_state,
|
||||
&self.instance_manager.instances_dir,
|
||||
&self.picker,
|
||||
world_quick_play_supported,
|
||||
);
|
||||
} else {
|
||||
frame.render_widget(
|
||||
|
||||
@@ -32,6 +32,41 @@ fn global_navigation_returns_from_log_overlay() {
|
||||
assert!(ui.app.exit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worlds_reserves_q_for_available_quick_launch() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.add_instance("Test Instance");
|
||||
ui.app.focused = FocusedArea::Content;
|
||||
ui.app.content_tab = ContentTab::Worlds;
|
||||
|
||||
ui.key(KeyCode::Char('q'));
|
||||
assert!(!ui.app.exit, "unsupported Quick Play must not quit rmcl");
|
||||
|
||||
let meta = serde_json::json!({
|
||||
"id": "1.21.1",
|
||||
"mainClass": "net.minecraft.client.main.Main",
|
||||
"arguments": {
|
||||
"game": [{
|
||||
"rules": [{
|
||||
"action": "allow",
|
||||
"features": { "is_quick_play_singleplayer": true }
|
||||
}],
|
||||
"value": ["--quickPlaySingleplayer", "${quickPlaySingleplayer}"]
|
||||
}],
|
||||
"jvm": []
|
||||
}
|
||||
});
|
||||
let meta_path = crate::storage::MetadataPaths::new(&ui.app.instance_manager.meta_dir)
|
||||
.versions()
|
||||
.join("1.21.1/meta.json");
|
||||
std::fs::create_dir_all(meta_path.parent().unwrap()).unwrap();
|
||||
std::fs::write(meta_path, serde_json::to_vec(&meta).unwrap()).unwrap();
|
||||
ui.app.world_quick_play_support = None;
|
||||
ui.draw();
|
||||
|
||||
assert!(ui.screen().contains("quick launch"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mouse_wheel_uses_the_focused_views_scroll_navigation() {
|
||||
let mut ui = UiHarness::new();
|
||||
|
||||
@@ -66,6 +66,7 @@ impl UiHarness {
|
||||
crate::instance::ContentKind::Shader,
|
||||
),
|
||||
worlds_state: widgets::content::ContentListState::default(),
|
||||
world_quick_play_support: None,
|
||||
screenshots_state: {
|
||||
let mut state = widgets::screenshots_grid::ScreenshotsState::default();
|
||||
state.font_size = (font_size.width, font_size.height);
|
||||
|
||||
@@ -417,6 +417,7 @@ fn rendering_visible_entries_restores_the_first_selection() {
|
||||
"Empty",
|
||||
&picker,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
})
|
||||
.unwrap();
|
||||
@@ -424,6 +425,37 @@ fn rendering_visible_entries_restores_the_first_selection() {
|
||||
assert_eq!(state.list_state.selected, Some(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiline_rendering_uses_the_space_beside_large_icons() {
|
||||
let mut world = entry("World");
|
||||
world.description =
|
||||
"Last played\nDifficulty: Normal\nMinecraft: 1.21.1\nCreated\nApprox. size".to_owned();
|
||||
world.icon_lines = Some(crate::instance::content::fallback_icon_large());
|
||||
let mut state = ContentListState::default();
|
||||
state.entries.push(world);
|
||||
state.rebuild_display_metadata();
|
||||
let picker = ratatui_image::picker::Picker::halfblocks();
|
||||
let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(50, 6)).unwrap();
|
||||
|
||||
terminal
|
||||
.draw(|frame| {
|
||||
super::render(
|
||||
frame,
|
||||
frame.area(),
|
||||
&mut state,
|
||||
true,
|
||||
"Loading...",
|
||||
"Empty",
|
||||
&picker,
|
||||
false,
|
||||
true,
|
||||
);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
insta::assert_snapshot!(terminal.backend().to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pager_tracks_viewport_pages_and_jumps_to_their_first_item() {
|
||||
assert_eq!(
|
||||
@@ -470,6 +502,7 @@ fn pager_tracks_viewport_pages_and_jumps_to_their_first_item() {
|
||||
"Empty",
|
||||
&picker,
|
||||
true,
|
||||
false,
|
||||
);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: src/tui/tests/widgets/content/list.rs
|
||||
expression: terminal.backend().to_string()
|
||||
---
|
||||
"▌▄▄▄▄▄▄▄▄▄▄▄▄ World "
|
||||
"▌▄▄▄▄▄▄▄▄▄▄▄▄ Last played "
|
||||
"▌▄▄▄▄▄▄▄▄▄▄▄▄ Difficulty: Normal "
|
||||
"▌▄▄▄▄▄▄▄▄▄▄▄▄ Minecraft: 1.21.1 "
|
||||
"▌▄▄▄▄▄▄▄▄▄▄▄▄ Created "
|
||||
"▌▄▄▄▄▄▄▄▄▄▄▄▄ Approx. size "
|
||||
@@ -1006,6 +1006,15 @@ impl ContentListState {
|
||||
.map(|entry| entry.file_stem.clone())
|
||||
}
|
||||
|
||||
pub fn selected_entry(&self) -> Option<&ContentEntry> {
|
||||
let filtered = self.filtered_indices();
|
||||
let index = self
|
||||
.list_state
|
||||
.selected
|
||||
.and_then(|index| filtered.get(index))?;
|
||||
self.entries.get(*index)
|
||||
}
|
||||
|
||||
fn restore_selected_file_stem(&mut self, file_stem: Option<&str>) {
|
||||
let filtered = self.filtered_indices();
|
||||
self.list_state.selected = file_stem
|
||||
@@ -1421,6 +1430,7 @@ pub fn render(
|
||||
empty_text: &str,
|
||||
picker: &ratatui_image::picker::Picker,
|
||||
paginate: bool,
|
||||
multiline_descriptions: bool,
|
||||
) {
|
||||
let theme = THEME.as_ref();
|
||||
state.pagination = None;
|
||||
@@ -1534,8 +1544,20 @@ pub fn render(
|
||||
let footer_label_style = Style::default().fg(theme.text());
|
||||
|
||||
let has_icon = icon_pixels.is_some();
|
||||
let stripped_desc = metadata.map_or("", |metadata| metadata.description.as_str());
|
||||
let has_description = metadata.is_some_and(|metadata| metadata.has_description);
|
||||
let mut descriptions = metadata
|
||||
.map(|metadata| {
|
||||
metadata
|
||||
.description
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.filter(|line| !line.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
if !multiline_descriptions {
|
||||
descriptions.truncate(1);
|
||||
}
|
||||
let has_description = !descriptions.is_empty();
|
||||
let rendered_icon_columns = if use_image_protocol && has_image {
|
||||
protocol_columns
|
||||
} else {
|
||||
@@ -1549,10 +1571,21 @@ pub fn render(
|
||||
rendered_icon_columns,
|
||||
has_icon,
|
||||
);
|
||||
let visible_description = ellipsize(
|
||||
stripped_desc,
|
||||
description_text_width(description_width, footer_label, has_description),
|
||||
);
|
||||
let visible_descriptions = descriptions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, description)| {
|
||||
ellipsize(
|
||||
description,
|
||||
if index == 0 {
|
||||
description_text_width(description_width, footer_label, has_description)
|
||||
} else {
|
||||
description_width
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let visible_description = visible_descriptions.first().map_or("", String::as_str);
|
||||
let compact = !has_icon && !has_description && footer_label.is_none();
|
||||
|
||||
let selector = if show_selected {
|
||||
@@ -1575,11 +1608,9 @@ pub fn render(
|
||||
(item, 1)
|
||||
} else if has_icon {
|
||||
let icon_row_count = icon_pixels.as_ref().map(|r| r.len()).unwrap_or(0);
|
||||
let text_rows = if has_description || footer_label.is_some() {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let text_rows = 1 + visible_descriptions
|
||||
.len()
|
||||
.max(usize::from(footer_label.is_some()));
|
||||
let height = icon_row_count.max(text_rows) as u16;
|
||||
|
||||
let pad = if show_selected {
|
||||
@@ -1605,22 +1636,24 @@ pub fn render(
|
||||
|
||||
let mut lines = vec![Line::from(line_0)];
|
||||
|
||||
if has_description || footer_label.is_some() {
|
||||
for r in 1..text_rows {
|
||||
let mut row = vec![pad.clone()];
|
||||
row.extend(icon_spans(
|
||||
icon_pixels.as_ref(),
|
||||
1,
|
||||
r,
|
||||
use_image_protocol && has_image,
|
||||
protocol_columns,
|
||||
));
|
||||
row.push(Span::raw(" "));
|
||||
if has_description {
|
||||
row.extend(search.highlight_spans(&visible_description, description_style));
|
||||
if let Some(description) = visible_descriptions.get(r - 1) {
|
||||
row.extend(search.highlight_spans(description, description_style));
|
||||
}
|
||||
if let Some(footer_label) = footer_label {
|
||||
if r == 1
|
||||
&& let Some(footer_label) = footer_label
|
||||
{
|
||||
row.extend(right_aligned_footer_spans(
|
||||
description_width,
|
||||
&visible_description,
|
||||
visible_description,
|
||||
has_description,
|
||||
footer_label,
|
||||
footer_label_style,
|
||||
@@ -1663,12 +1696,12 @@ pub fn render(
|
||||
let mut description = vec![pad];
|
||||
if has_description {
|
||||
description
|
||||
.extend(search.highlight_spans(&visible_description, description_style));
|
||||
.extend(search.highlight_spans(visible_description, description_style));
|
||||
}
|
||||
if let Some(footer_label) = footer_label {
|
||||
description.extend(right_aligned_footer_spans(
|
||||
description_width,
|
||||
&visible_description,
|
||||
visible_description,
|
||||
has_description,
|
||||
footer_label,
|
||||
footer_label_style,
|
||||
@@ -1994,7 +2027,7 @@ fn strip_mc_codes(text: &str) -> String {
|
||||
|
||||
fn display_metadata(entry: &ContentEntry) -> DisplayMetadata {
|
||||
let description = strip_mc_codes(&entry.description);
|
||||
let description = description.lines().next().unwrap_or("").trim().to_string();
|
||||
let description = description.trim().to_string();
|
||||
DisplayMetadata {
|
||||
has_description: !description.is_empty(),
|
||||
description,
|
||||
|
||||
@@ -165,6 +165,7 @@ pub fn render(
|
||||
logs_state: &mut crate::tui::widgets::logs_viewer::LogsState,
|
||||
instances_dir: &std::path::Path,
|
||||
picker: &ratatui_image::picker::Picker,
|
||||
world_quick_play_supported: bool,
|
||||
) {
|
||||
let theme = THEME.as_ref();
|
||||
let is_focused = focused == FocusedArea::Content;
|
||||
@@ -319,6 +320,15 @@ pub fn render(
|
||||
("/", " search"),
|
||||
("Tab", " discover"),
|
||||
],
|
||||
(ContentMode::Installed, ContentTab::Worlds) if world_quick_play_supported => &[
|
||||
("j/k", " navigate"),
|
||||
("q", " quick launch"),
|
||||
("d", " delete"),
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
("Tab", " discover"),
|
||||
],
|
||||
(ContentMode::Installed, ContentTab::Worlds) => &[
|
||||
("j/k", " navigate"),
|
||||
("d", " delete"),
|
||||
@@ -473,6 +483,7 @@ pub fn render(
|
||||
"No worlds saved.",
|
||||
picker,
|
||||
false,
|
||||
true,
|
||||
);
|
||||
} else {
|
||||
frame.render_widget(
|
||||
@@ -534,6 +545,7 @@ fn render_downloadable(
|
||||
tab.empty_text,
|
||||
picker,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -611,6 +623,7 @@ fn render_discovery_body(
|
||||
&empty_text,
|
||||
picker,
|
||||
paginate,
|
||||
false,
|
||||
);
|
||||
}
|
||||
if state.version_popup.is_some() {
|
||||
|
||||
Reference in new issue
Block a user