feat(discovery): select Minecraft version for modpacks
This commit is contained in:
1 parent
cccf65981a
commit
de6defa1ff
8 files changed
+197
-41
No files matched your search
@@ -89,6 +89,9 @@ pub struct VersionPopupState {
|
||||
pub sources: Vec<crate::instance::ProviderProject>,
|
||||
pub source_index: usize,
|
||||
pub installed_path: Option<PathBuf>,
|
||||
pub minecraft_versions: Vec<String>,
|
||||
pub selected_minecraft_version: Option<String>,
|
||||
pub selecting_minecraft_version: bool,
|
||||
pub versions: Vec<VersionInfo>,
|
||||
pub selected: usize,
|
||||
pub loading: bool,
|
||||
@@ -112,6 +115,31 @@ impl VersionPopupState {
|
||||
_ => "Modrinth",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn visible_versions(&self) -> impl Iterator<Item = &VersionInfo> {
|
||||
self.versions.iter().filter(|version| {
|
||||
self.selected_minecraft_version
|
||||
.as_ref()
|
||||
.is_none_or(|selected| {
|
||||
version
|
||||
.game_versions
|
||||
.iter()
|
||||
.any(|version| version == selected)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn selected_version(&self) -> Option<&VersionInfo> {
|
||||
self.visible_versions().nth(self.selected)
|
||||
}
|
||||
|
||||
fn item_count(&self) -> usize {
|
||||
if self.selecting_minecraft_version {
|
||||
self.minecraft_versions.len()
|
||||
} else {
|
||||
self.visible_versions().count()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VersionsRequest {
|
||||
@@ -387,6 +415,9 @@ impl DiscoveryState {
|
||||
sources,
|
||||
source_index: 0,
|
||||
installed_path,
|
||||
minecraft_versions: Vec::new(),
|
||||
selected_minecraft_version: None,
|
||||
selecting_minecraft_version: self.modpacks,
|
||||
versions: Vec::new(),
|
||||
selected: 0,
|
||||
loading: true,
|
||||
@@ -403,6 +434,7 @@ impl DiscoveryState {
|
||||
}
|
||||
|
||||
pub fn switch_version_source(&mut self) -> Option<VersionsRequest> {
|
||||
let selecting_minecraft_version = self.modpacks;
|
||||
let popup = self.version_popup.as_mut()?;
|
||||
if popup.loading || popup.installing || popup.sources.len() < 2 {
|
||||
return None;
|
||||
@@ -413,6 +445,9 @@ impl DiscoveryState {
|
||||
popup.request_id = self.next_action_request_id;
|
||||
popup.project_id.clone_from(&source.project_id);
|
||||
popup.provider.clone_from(&source.provider);
|
||||
popup.minecraft_versions.clear();
|
||||
popup.selected_minecraft_version = None;
|
||||
popup.selecting_minecraft_version = selecting_minecraft_version;
|
||||
popup.versions.clear();
|
||||
popup.selected = 0;
|
||||
popup.loading = true;
|
||||
@@ -572,7 +607,7 @@ impl DiscoveryState {
|
||||
if popup.loading || popup.installing || !popup.confirming {
|
||||
return None;
|
||||
}
|
||||
let version = popup.versions.get(popup.selected)?.clone();
|
||||
let version = popup.selected_version()?.clone();
|
||||
let request = InstallRequest {
|
||||
request_id: popup.request_id,
|
||||
generation: self.generation,
|
||||
@@ -591,7 +626,11 @@ impl DiscoveryState {
|
||||
let Some(popup) = self.version_popup.as_mut() else {
|
||||
return false;
|
||||
};
|
||||
if popup.loading || popup.installing || popup.versions.get(popup.selected).is_none() {
|
||||
if popup.loading
|
||||
|| popup.installing
|
||||
|| popup.selecting_minecraft_version
|
||||
|| popup.selected_version().is_none()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
popup.confirming = true;
|
||||
@@ -599,6 +638,23 @@ impl DiscoveryState {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn select_minecraft_version(&mut self) -> bool {
|
||||
let Some(popup) = self.version_popup.as_mut() else {
|
||||
return false;
|
||||
};
|
||||
if popup.loading || popup.installing || !popup.selecting_minecraft_version {
|
||||
return false;
|
||||
}
|
||||
let Some(version) = popup.minecraft_versions.get(popup.selected).cloned() else {
|
||||
return false;
|
||||
};
|
||||
popup.selected_minecraft_version = Some(version);
|
||||
popup.selecting_minecraft_version = false;
|
||||
popup.selected = 0;
|
||||
popup.error = None;
|
||||
true
|
||||
}
|
||||
|
||||
pub fn search_due(&self) -> bool {
|
||||
self.search_changed_at
|
||||
.is_some_and(|changed| changed.elapsed() >= SEARCH_DEBOUNCE)
|
||||
@@ -787,6 +843,7 @@ impl DiscoveryState {
|
||||
popup.loading = false;
|
||||
match result {
|
||||
Ok(versions) => {
|
||||
popup.minecraft_versions = minecraft_versions(&versions);
|
||||
popup.versions = versions;
|
||||
popup.selected = 0;
|
||||
popup.error = None;
|
||||
@@ -890,12 +947,31 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool {
|
||||
popup.error = None;
|
||||
return true;
|
||||
}
|
||||
if !popup.selecting_minecraft_version
|
||||
&& popup.selected_minecraft_version.is_some()
|
||||
&& matches!(key_event.code, KeyCode::Left | KeyCode::Char('h'))
|
||||
&& !popup.loading
|
||||
&& !popup.installing
|
||||
{
|
||||
popup.selecting_minecraft_version = true;
|
||||
popup.selected = popup
|
||||
.selected_minecraft_version
|
||||
.as_ref()
|
||||
.and_then(|selected| {
|
||||
popup
|
||||
.minecraft_versions
|
||||
.iter()
|
||||
.position(|version| version == selected)
|
||||
})
|
||||
.unwrap_or(0);
|
||||
return true;
|
||||
}
|
||||
match key_event.code {
|
||||
KeyCode::Esc if !popup.installing => state.version_popup = None,
|
||||
KeyCode::Char('j') | KeyCode::Down
|
||||
if !popup.loading && !popup.installing && !popup.confirming =>
|
||||
{
|
||||
if popup.selected + 1 < popup.versions.len() {
|
||||
if popup.selected + 1 < popup.item_count() {
|
||||
popup.selected += 1;
|
||||
}
|
||||
}
|
||||
@@ -959,6 +1035,27 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn minecraft_versions(versions: &[VersionInfo]) -> Vec<String> {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut minecraft_versions = versions
|
||||
.iter()
|
||||
.flat_map(|version| &version.game_versions)
|
||||
.filter(|game_version| {
|
||||
!versions.iter().any(|version| {
|
||||
version
|
||||
.loaders
|
||||
.iter()
|
||||
.any(|loader| loader.eq_ignore_ascii_case(game_version))
|
||||
})
|
||||
})
|
||||
.filter(|game_version| seen.insert((*game_version).clone()))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
minecraft_versions
|
||||
.sort_by(|a, b| crate::tui::widgets::popups::compare_game_versions(b.as_str(), a.as_str()));
|
||||
minecraft_versions
|
||||
}
|
||||
|
||||
pub(crate) fn page_key_direction(key_event: &KeyEvent) -> Option<bool> {
|
||||
match key_event.code {
|
||||
KeyCode::Char('[') => Some(false),
|
||||
|
||||
@@ -635,36 +635,41 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
|
||||
let confirming = popup.confirming;
|
||||
let error = popup.error.clone();
|
||||
let selected = popup.selected;
|
||||
let selecting_minecraft_version = popup.selecting_minecraft_version;
|
||||
let selected_version = popup
|
||||
.versions
|
||||
.get(selected)
|
||||
.selected_version()
|
||||
.map(|version| version.version_number.clone())
|
||||
.unwrap_or_default();
|
||||
let minecraft_versions = popup
|
||||
.versions
|
||||
.get(selected)
|
||||
.selected_version()
|
||||
.map(|version| confirmation_values(&version.game_versions))
|
||||
.unwrap_or_else(|| "Unknown".to_owned());
|
||||
let loaders = popup
|
||||
.versions
|
||||
.get(selected)
|
||||
.selected_version()
|
||||
.map(|version| confirmation_loaders(&version.loaders))
|
||||
.unwrap_or_else(|| "Unknown".to_owned());
|
||||
let release_date = popup
|
||||
.versions
|
||||
.get(selected)
|
||||
.selected_version()
|
||||
.map(|version| confirmation_release_date(&version.date_published))
|
||||
.unwrap_or_else(|| "Unknown".to_owned());
|
||||
let replacing = popup.installed_path.is_some();
|
||||
let provider_label = popup.provider_label().to_owned();
|
||||
let can_switch_provider = popup.sources.len() > 1;
|
||||
let items = popup
|
||||
.versions
|
||||
.iter()
|
||||
.map(|version| {
|
||||
ListItem::new(discovery_version_label(version)).style(Style::default().fg(theme.text()))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let items = if selecting_minecraft_version {
|
||||
popup
|
||||
.minecraft_versions
|
||||
.iter()
|
||||
.map(|version| ListItem::new(version.clone()).style(Style::default().fg(theme.text())))
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
popup
|
||||
.visible_versions()
|
||||
.map(|version| {
|
||||
ListItem::new(discovery_version_label(version))
|
||||
.style(Style::default().fg(theme.text()))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let keybinds = if confirming {
|
||||
crate::tui::widgets::popups::keybind_line(&[
|
||||
("h", " back"),
|
||||
@@ -675,6 +680,9 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
|
||||
if can_switch_provider {
|
||||
keybinds.push(("Tab", " source"));
|
||||
}
|
||||
if popup.selected_minecraft_version.is_some() {
|
||||
keybinds.push(("h", " back"));
|
||||
}
|
||||
keybinds.extend([("Enter", " continue"), ("Esc", " close")]);
|
||||
crate::tui::widgets::popups::keybind_line(&keybinds)
|
||||
};
|
||||
@@ -719,9 +727,13 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
|
||||
buffer,
|
||||
);
|
||||
} else if items.is_empty() {
|
||||
Paragraph::new("No compatible versions found.")
|
||||
.style(Style::default().fg(THEME.as_ref().text_dim()))
|
||||
.render(area, buffer);
|
||||
Paragraph::new(if selecting_minecraft_version {
|
||||
"No compatible Minecraft versions found."
|
||||
} else {
|
||||
"No compatible versions found."
|
||||
})
|
||||
.style(Style::default().fg(THEME.as_ref().text_dim()))
|
||||
.render(area, buffer);
|
||||
} else {
|
||||
crate::tui::widgets::popups::new_instance::render_select_list(
|
||||
items.clone(),
|
||||
|
||||
@@ -189,6 +189,9 @@ fn handle_discovery_key(key_event: &KeyEvent, instances_state: &mut instances::S
|
||||
}
|
||||
}
|
||||
KeyCode::Enter if popup_open => {
|
||||
if discovery.select_minecraft_version() {
|
||||
return;
|
||||
}
|
||||
let request = take_discovered_version(&mut discovery);
|
||||
drop(discovery);
|
||||
if let Some(request) = request {
|
||||
|
||||
@@ -17,6 +17,24 @@ use ratatui::{
|
||||
widgets::Paragraph,
|
||||
};
|
||||
|
||||
pub(crate) fn compare_game_versions(a: &str, b: &str) -> std::cmp::Ordering {
|
||||
let parse_parts = |version: &str| {
|
||||
version
|
||||
.split('.')
|
||||
.map(|part| part.parse::<u64>().unwrap_or(0))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let a_parts = parse_parts(a);
|
||||
let b_parts = parse_parts(b);
|
||||
for (a, b) in a_parts.iter().zip(&b_parts) {
|
||||
match a.cmp(b) {
|
||||
std::cmp::Ordering::Equal => {}
|
||||
ordering => return ordering,
|
||||
}
|
||||
}
|
||||
a_parts.len().cmp(&b_parts.len())
|
||||
}
|
||||
|
||||
// figures out the (width, height) a text block will need after word wrapping.
|
||||
// used to size popups before rendering so they fit their content snugly.
|
||||
pub fn word_wrap_size(text: &str, max_inner_width: usize) -> (usize, usize) {
|
||||
|
||||
@@ -463,27 +463,8 @@ pub(crate) fn ensure_loader_versions_loaded(
|
||||
});
|
||||
}
|
||||
|
||||
// quick and dirty semver compare. doesn't handle pre-release tags or anything
|
||||
// fancy, just splits on dots and compares numerically. good enough for mc versions.
|
||||
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 [GameVersion]) {
|
||||
versions.sort_by(|a, b| compare_semver(&b.id, &a.id));
|
||||
versions.sort_by(|a, b| super::super::compare_game_versions(&b.id, &a.id));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user