feat(content): manage discovery installs incrementally

This commit is contained in:
objz committed 2026-07-26 14:05:49 +02:00
1 parent f26a3542b9
commit 130661f144
9 files changed
+477 -153

No files matched your search

+190 -41
View File
@@ -97,7 +97,9 @@ pub struct VersionsRequest {
pub struct InstallRequest {
pub request_id: u64,
pub generation: u64,
pub project_id: String,
pub project_title: String,
pub version: VersionInfo,
pub installed_path: Option<PathBuf>,
pub pending: PendingActions,
@@ -117,7 +119,9 @@ pub enum DiscoveryActionResult {
},
Install {
request_id: u64,
generation: u64,
project_id: String,
project_title: String,
result: Result<InstallCompletion, String>,
},
}
@@ -302,21 +306,65 @@ impl DiscoveryState {
}
}
pub fn selected_is_installed(&self) -> bool {
self.selected_installed_entry().is_some()
}
pub fn pending_installed_delete(
&self,
) -> Option<crate::tui::widgets::content::list::PendingContentDelete> {
let entry = self.selected_installed_entry()?;
Some(crate::tui::widgets::content::list::PendingContentDelete {
name: entry.name.clone(),
path: entry.installed_path.clone()?,
})
}
pub fn clear_installed_path(&mut self, path: &std::path::Path) -> bool {
let Some(entry) = self
.list
.entries
.iter_mut()
.find(|entry| entry.installed_path.as_deref() == Some(path))
else {
return false;
};
entry.installed_path = None;
entry.title_suffix = None;
crate::tui::request_redraw();
true
}
fn selected_installed_entry(&self) -> Option<&ContentEntry> {
let filtered = self.list.filtered_indices();
let index = self
.list
.list_state
.selected
.and_then(|selected| filtered.get(selected))?;
self.list
.entries
.get(*index)
.filter(|entry| entry.installed_path.is_some())
}
pub fn begin_install(&mut self) -> Option<InstallRequest> {
let popup = self.version_popup.as_mut()?;
let popup = self.version_popup.as_ref()?;
if popup.loading || popup.installing || !popup.confirming {
return None;
}
let version = popup.versions.get(popup.selected)?.clone();
popup.installing = true;
popup.error = None;
Some(InstallRequest {
let request = InstallRequest {
request_id: popup.request_id,
generation: self.generation,
project_id: popup.project_id.clone(),
project_title: popup.project_title.clone(),
version,
installed_path: popup.installed_path.clone(),
pending: self.pending_actions.clone(),
})
};
self.version_popup = None;
Some(request)
}
pub fn begin_confirmation(&mut self) -> bool {
@@ -447,47 +495,49 @@ impl DiscoveryState {
}
DiscoveryActionResult::Install {
request_id,
generation,
project_id,
project_title,
result,
} => {
let Some(popup) = self.version_popup.as_mut().filter(|popup| {
popup.request_id == request_id && popup.project_id == project_id
}) else {
continue;
};
popup.installing = false;
popup.confirming = false;
match result {
Ok(completion) => {
if let Some(entry) = self
} => match result {
Ok(completion) => {
if generation == self.generation
&& let Some(entry) = self
.list
.entries
.iter_mut()
.find(|entry| entry.file_stem == project_id)
{
entry.title_suffix = Some("Installed".to_owned());
entry.installed_path = Some(completion.path.clone());
}
let action = if completion.skipped {
"already installed"
} else if completion.replaced {
"version changed"
} else {
"installed"
};
crate::tui::error_buffer::push_error(
crate::tui::error_buffer::ErrorEvent {
id: 0,
level: tracing::Level::INFO,
message: format!("{}: {action}", popup.project_title),
pushed_at: std::time::Instant::now(),
},
);
self.version_popup = None;
{
entry.title_suffix = Some("Installed".to_owned());
entry.installed_path = Some(completion.path.clone());
}
Err(error) => popup.error = Some(error),
let action = if completion.skipped {
"already installed"
} else if completion.replaced {
"version changed"
} else {
"installed"
};
crate::tui::error_buffer::push_error(
crate::tui::error_buffer::ErrorEvent {
id: request_id,
level: tracing::Level::INFO,
message: format!("{project_title}: {action}"),
pushed_at: std::time::Instant::now(),
},
);
}
}
Err(error) => {
crate::tui::error_buffer::push_error(
crate::tui::error_buffer::ErrorEvent {
id: request_id,
level: tracing::Level::ERROR,
message: format!("{project_title}: {error}"),
pushed_at: std::time::Instant::now(),
},
);
}
},
}
}
}
@@ -522,6 +572,14 @@ impl DiscoveryState {
pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool {
if let Some(popup) = state.version_popup.as_mut() {
if popup.confirming
&& matches!(key_event.code, KeyCode::Left | KeyCode::Char('h'))
&& !popup.installing
{
popup.confirming = false;
popup.error = None;
return true;
}
match key_event.code {
KeyCode::Esc if !popup.installing => state.version_popup = None,
KeyCode::Char('j') | KeyCode::Down
@@ -650,6 +708,7 @@ mod tests {
version_number: id.to_owned(),
game_versions: vec!["1.21.1".to_owned()],
loaders: vec!["fabric".to_owned()],
date_published: "2026-01-02T12:00:00Z".to_owned(),
files: Vec::new(),
}
}
@@ -747,7 +806,7 @@ mod tests {
}
#[test]
fn successful_install_marks_the_project_and_closes_the_popup() {
fn confirmation_can_return_to_version_selection() {
let project = DiscoveryProject {
id: "project".to_owned(),
slug: "project".to_owned(),
@@ -763,9 +822,100 @@ mod tests {
let request = state.begin_versions().unwrap();
DiscoveryState::push_action_result(
&request.pending,
DiscoveryActionResult::Install {
DiscoveryActionResult::Versions {
request_id: request.request_id,
project_id: request.project_id,
result: Ok(vec![version("1.0.0")]),
},
);
state.drain_pending();
assert!(state.begin_confirmation());
handle_key(
&KeyEvent::new(KeyCode::Char('h'), KeyModifiers::NONE),
&mut state,
);
assert!(!state.version_popup.as_ref().unwrap().confirming);
assert!(state.version_popup.is_some());
}
#[test]
fn discovery_delete_only_clears_the_matching_installed_badge() {
let first_path = PathBuf::from("mods/first.jar");
let second_path = PathBuf::from("mods/second.jar");
let project = |id: &str| DiscoveryProject {
id: id.to_owned(),
slug: id.to_owned(),
title: id.to_owned(),
description: String::new(),
downloads: 0,
icon_url: None,
icon_bytes: None,
};
let mut state = DiscoveryState::new(DiscoveryKind::Mod);
state
.list
.entries
.push(project_entry(project("first"), Some(first_path.clone())));
state
.list
.entries
.push(project_entry(project("second"), Some(second_path.clone())));
state.list.list_state.selected = Some(0);
let pending = state.pending_installed_delete().unwrap();
assert_eq!(pending.path, first_path);
assert!(state.clear_installed_path(&pending.path));
assert_eq!(state.list.entries.len(), 2);
assert!(state.list.entries[0].installed_path.is_none());
assert!(state.list.entries[0].title_suffix.is_none());
assert_eq!(
state.list.entries[1].installed_path.as_deref(),
Some(second_path.as_path())
);
assert_eq!(
state.list.entries[1].title_suffix.as_deref(),
Some("Installed")
);
assert!(state.pending_installed_delete().is_none());
}
#[test]
fn successful_install_marks_the_project_and_closes_the_popup() {
let project = DiscoveryProject {
id: "project".to_owned(),
slug: "project".to_owned(),
title: "Project".to_owned(),
description: String::new(),
downloads: 0,
icon_url: None,
icon_bytes: None,
};
let mut state = DiscoveryState::new(DiscoveryKind::Mod);
state.list.entries.push(project_entry(project, None));
state.list.list_state.selected = Some(0);
let versions_request = state.begin_versions().unwrap();
DiscoveryState::push_action_result(
&versions_request.pending,
DiscoveryActionResult::Versions {
request_id: versions_request.request_id,
project_id: versions_request.project_id,
result: Ok(vec![version("1.0.0")]),
},
);
state.drain_pending();
assert!(state.begin_confirmation());
let install = state.begin_install().unwrap();
assert!(state.version_popup.is_none());
DiscoveryState::push_action_result(
&install.pending,
DiscoveryActionResult::Install {
request_id: install.request_id,
generation: install.generation,
project_id: install.project_id,
project_title: install.project_title,
result: Ok(InstallCompletion {
path: PathBuf::from("mods/project.jar"),
replaced: false,
@@ -775,7 +925,6 @@ mod tests {
);
state.drain_pending();
assert!(state.version_popup.is_none());
assert_eq!(
state.list.entries[0].title_suffix.as_deref(),
+129 -29
View File
@@ -211,10 +211,25 @@ pub fn render(
block = block.title_top(sl);
}
let discovery_can_delete = match tab {
ContentTab::Mods => mods_discovery_state.selected_is_installed(),
ContentTab::ResourcePacks => resource_packs_discovery_state.selected_is_installed(),
ContentTab::Shaders => shaders_discovery_state.selected_is_installed(),
_ => false,
};
// keybinds change depending on which tab is active and whether
// the content panel or instances panel has focus
let kb: Option<&[(&str, &str)]> = if is_focused {
Some(match (mode, tab) {
(ContentMode::Discover, _) if discovery_can_delete => &[
("j/k", " navigate"),
("i", " versions"),
("d", " delete"),
("h/l", " tabs"),
("/", " search"),
("Tab", " installed"),
],
(ContentMode::Discover, _) => &[
("j/k", " navigate"),
("i", " versions"),
@@ -531,18 +546,16 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
let Some(popup) = state.version_popup.as_ref() else {
return;
};
let [_, vertical, _] = Layout::vertical([
Constraint::Percentage(10),
Constraint::Percentage(80),
Constraint::Percentage(10),
])
.areas(area);
let [_, popup_area, _] = Layout::horizontal([
Constraint::Percentage(12),
Constraint::Percentage(76),
Constraint::Percentage(12),
])
.areas(vertical);
let desired_height = version_popup_height(
popup.versions.len(),
popup.confirming,
popup.loading || popup.installing,
popup.error.is_some(),
);
let popup_area = area.centered(
Constraint::Percentage(50),
Constraint::Length(desired_height.min(area.height.saturating_sub(2))),
);
let theme = THEME.as_ref();
let title = popup.title();
let loading = popup.loading;
@@ -555,7 +568,21 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
.get(selected)
.map(|version| version.version_number.clone())
.unwrap_or_default();
let project_title = popup.project_title.clone();
let minecraft_versions = popup
.versions
.get(selected)
.map(|version| confirmation_values(&version.game_versions))
.unwrap_or_else(|| "Unknown".to_owned());
let loaders = popup
.versions
.get(selected)
.map(|version| confirmation_loaders(&version.loaders))
.unwrap_or_else(|| "Unknown".to_owned());
let release_date = popup
.versions
.get(selected)
.map(|version| confirmation_release_date(&version.date_published))
.unwrap_or_else(|| "Unknown".to_owned());
let replacing = popup.installed_path.is_some();
let items = popup
.versions
@@ -564,20 +591,24 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
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"),
("Enter", if replacing { " change" } else { " install" }),
])
} else {
crate::tui::widgets::popups::keybind_line(&[
("j/k", " navigate"),
("Enter", " continue"),
("Esc", " close"),
])
};
let popup = crate::tui::widgets::popups::base::PopupFrame {
title: styled_title(&title, false),
border_color: theme.accent(),
bg: Some(theme.surface()),
keybinds: Some(crate::tui::widgets::popups::keybind_line(if confirming {
&[("Enter", " confirm"), ("Esc", " cancel")]
} else {
&[
("j/k", " navigate"),
("Enter", " continue"),
("Esc", " close"),
]
})),
keybinds: Some(keybinds),
search_line: None,
content: Box::new(move |area, buffer| {
if installing {
@@ -594,13 +625,16 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
.wrap(Wrap { trim: true })
.render(area, buffer);
} else if confirming {
let action = if replacing { "Change" } else { "Install" };
Paragraph::new(format!(
"{action} {project_title} version {selected_version}?\n\nThe file is changed only after the download succeeds."
))
.style(Style::default().fg(THEME.as_ref().text()))
.wrap(Wrap { trim: true })
.render(area, buffer);
crate::tui::widgets::popups::base::render_summary(
&[
("Version", selected_version.as_str()),
("Minecraft", minecraft_versions.as_str()),
("Loader", loaders.as_str()),
("Released", release_date.as_str()),
],
area,
buffer,
);
} else if items.is_empty() {
Paragraph::new("No compatible versions found.")
.style(Style::default().fg(THEME.as_ref().text_dim()))
@@ -618,6 +652,49 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
frame.render_widget(popup, popup_area);
}
fn version_popup_height(version_count: usize, confirming: bool, compact: bool, error: bool) -> u16 {
if confirming || error {
8
} else if compact {
5
} else {
(version_count as u16).saturating_add(2).clamp(6, 18)
}
}
fn confirmation_values(values: &[String]) -> String {
if values.is_empty() {
"Unknown".to_owned()
} else {
values.join(", ")
}
}
fn confirmation_loaders(loaders: &[String]) -> String {
let loaders = loaders
.iter()
.map(|loader| match loader.as_str() {
"fabric" => "Fabric",
"forge" => "Forge",
"neoforge" => "NeoForge",
"quilt" => "Quilt",
"minecraft" => "Minecraft",
other => other,
})
.map(str::to_owned)
.collect::<Vec<_>>();
confirmation_values(&loaders)
}
fn confirmation_release_date(value: &str) -> String {
if value.is_empty() {
return "Unknown".to_owned();
}
chrono::DateTime::parse_from_rfc3339(value)
.map(|date| date.format("%Y-%m-%d").to_string())
.unwrap_or_else(|_| value.to_owned())
}
fn discovery_version_label(version: &crate::net::modrinth::VersionInfo) -> String {
version.version_number.clone()
}
@@ -768,9 +845,32 @@ mod tests {
version_number: "3.2.4-fabric-26.1".to_owned(),
game_versions: vec![],
loaders: vec![],
date_published: String::new(),
files: vec![],
};
assert_eq!(discovery_version_label(&version), "3.2.4-fabric-26.1");
}
#[test]
fn discovery_version_popup_uses_compact_heights() {
assert_eq!(version_popup_height(1, false, false, false), 6);
assert_eq!(version_popup_height(100, false, false, false), 18);
assert_eq!(version_popup_height(1, true, false, false), 8);
assert_eq!(version_popup_height(0, false, true, false), 5);
assert_eq!(version_popup_height(0, false, false, true), 8);
}
#[test]
fn confirmation_metadata_is_human_readable() {
assert_eq!(
confirmation_loaders(&["fabric".to_owned(), "neoforge".to_owned()]),
"Fabric, NeoForge"
);
assert_eq!(
confirmation_release_date("2026-07-26T14:30:00Z"),
"2026-07-26"
);
assert_eq!(confirmation_values(&[]), "Unknown");
}
}