feat(content): manage required mod dependencies

This commit is contained in:
objz committed 2026-07-28 19:38:38 +02:00
1 parent de6defa1ff
commit 3b26581af5
27 files changed
+2487 -75

No files matched your search

+78
View File
@@ -95,8 +95,10 @@ pub struct VersionPopupState {
pub versions: Vec<VersionInfo>,
pub selected: usize,
pub loading: bool,
pub resolving_dependencies: bool,
pub confirming: bool,
pub installing: bool,
pub dependency_plan: Option<crate::instance::content::dependencies::DependencyPlan>,
pub error: Option<String>,
}
@@ -177,6 +179,14 @@ pub struct InstallRequest {
pub provider: String,
pub version: VersionInfo,
pub installed_path: Option<PathBuf>,
pub dependency_plan: Option<crate::instance::content::dependencies::DependencyPlan>,
pub pending: PendingActions,
}
pub struct DependencyRequest {
pub request_id: u64,
pub project_id: String,
pub root: crate::instance::content::dependencies::InstallRoot,
pub pending: PendingActions,
}
@@ -184,6 +194,7 @@ pub struct InstallCompletion {
pub path: PathBuf,
pub replaced: bool,
pub skipped: bool,
pub orphaned_dependencies: Vec<PathBuf>,
}
pub enum DiscoveryActionResult {
@@ -203,6 +214,11 @@ pub enum DiscoveryActionResult {
project_id: String,
result: Result<Vec<VersionInfo>, String>,
},
Dependencies {
request_id: u64,
project_id: String,
result: Result<crate::instance::content::dependencies::DependencyPlan, String>,
},
Install {
request_id: u64,
generation: u64,
@@ -230,6 +246,7 @@ pub struct DiscoveryState {
sources: std::collections::HashMap<String, Vec<crate::instance::ProviderProject>>,
pub project_page: Option<ProjectPageState>,
pub version_popup: Option<VersionPopupState>,
pending_orphan_cleanup: Option<Vec<PathBuf>>,
next_action_request_id: u64,
stream: Option<ContentStream>,
next_offset: usize,
@@ -259,6 +276,7 @@ impl DiscoveryState {
sources: std::collections::HashMap::new(),
project_page: None,
version_popup: None,
pending_orphan_cleanup: None,
next_action_request_id: 0,
stream: None,
next_offset: 0,
@@ -421,8 +439,10 @@ impl DiscoveryState {
versions: Vec::new(),
selected: 0,
loading: true,
resolving_dependencies: false,
confirming: false,
installing: false,
dependency_plan: None,
error: None,
});
Some(VersionsRequest {
@@ -451,7 +471,9 @@ impl DiscoveryState {
popup.versions.clear();
popup.selected = 0;
popup.loading = true;
popup.resolving_dependencies = false;
popup.confirming = false;
popup.dependency_plan = None;
popup.error = None;
Some(VersionsRequest {
request_id: popup.request_id,
@@ -616,6 +638,7 @@ impl DiscoveryState {
provider: popup.provider.clone(),
version,
installed_path: popup.installed_path.clone(),
dependency_plan: popup.dependency_plan.clone(),
pending: self.pending_actions.clone(),
};
self.version_popup = None;
@@ -638,6 +661,33 @@ impl DiscoveryState {
true
}
pub fn begin_dependency_resolution(&mut self) -> Option<DependencyRequest> {
let popup = self.version_popup.as_mut()?;
if popup.loading
|| popup.installing
|| popup.selecting_minecraft_version
|| popup.confirming
{
return None;
}
let version = popup.selected_version()?.clone();
popup.loading = true;
popup.resolving_dependencies = true;
popup.error = None;
Some(DependencyRequest {
request_id: popup.request_id,
project_id: popup.project_id.clone(),
root: crate::instance::content::dependencies::InstallRoot {
provider: popup.provider.clone(),
project_id: popup.project_id.clone(),
title: popup.project_title.clone(),
version,
installed_path: popup.installed_path.clone(),
},
pending: self.pending_actions.clone(),
})
}
pub fn select_minecraft_version(&mut self) -> bool {
let Some(popup) = self.version_popup.as_mut() else {
return false;
@@ -851,6 +901,27 @@ impl DiscoveryState {
Err(error) => popup.error = Some(error),
}
}
DiscoveryActionResult::Dependencies {
request_id,
project_id,
result,
} => {
let Some(popup) = self.version_popup.as_mut().filter(|popup| {
popup.request_id == request_id && popup.project_id == project_id
}) else {
continue;
};
popup.loading = false;
popup.resolving_dependencies = false;
match result {
Ok(plan) => {
popup.dependency_plan = Some(plan);
popup.confirming = true;
popup.error = None;
}
Err(error) => popup.error = Some(error),
}
}
DiscoveryActionResult::Install {
request_id,
generation,
@@ -893,6 +964,9 @@ impl DiscoveryState {
message: format!("{project_title}: {action}"),
pushed_at: std::time::Instant::now(),
});
if !completion.orphaned_dependencies.is_empty() {
self.pending_orphan_cleanup = Some(completion.orphaned_dependencies);
}
}
Err(error) => {
crate::feedback::errors::push_error(crate::feedback::errors::ErrorEvent {
@@ -915,6 +989,10 @@ impl DiscoveryState {
})
}
pub fn take_orphan_cleanup(&mut self) -> Option<Vec<PathBuf>> {
self.pending_orphan_cleanup.take()
}
fn should_load_more(&self) -> bool {
if self.page_loading
|| self.exhausted
+66 -16
View File
@@ -625,12 +625,14 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
let popup_area = area.centered(
Constraint::Percentage(50),
Constraint::Length(
version_popup_height(popup.confirming).min(area.height.saturating_sub(2)),
version_popup_height(popup.confirming, popup.dependency_plan.as_ref())
.min(area.height.saturating_sub(2)),
),
);
let theme = THEME.as_ref();
let title = popup.title();
let loading = popup.loading;
let resolving_dependencies = popup.resolving_dependencies;
let installing = popup.installing;
let confirming = popup.confirming;
let error = popup.error.clone();
@@ -654,6 +656,31 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
.unwrap_or_else(|| "Unknown".to_owned());
let replacing = popup.installed_path.is_some();
let provider_label = popup.provider_label().to_owned();
let dependency_installs = popup
.dependency_plan
.as_ref()
.map(|plan| {
plan.dependency_installs()
.map(|item| item.title.as_str())
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
let dependency_replacements = popup
.dependency_plan
.as_ref()
.map(|plan| {
plan.dependency_replacements()
.map(|item| format!("{} -> {}", item.title, item.version.version_number))
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
let optional_dependencies = popup
.dependency_plan
.as_ref()
.map(|plan| plan.optional_dependencies)
.unwrap_or_default();
let can_switch_provider = popup.sources.len() > 1;
let items = if selecting_minecraft_version {
popup
@@ -707,25 +734,37 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
.style(Style::default().fg(THEME.as_ref().text_dim()))
.render(area, buffer);
} else if loading {
Paragraph::new("Loading compatible versions...")
.style(Style::default().fg(THEME.as_ref().text_dim()))
.render(area, buffer);
Paragraph::new(if resolving_dependencies {
"Resolving required dependencies..."
} else {
"Loading compatible versions..."
})
.style(Style::default().fg(THEME.as_ref().text_dim()))
.render(area, buffer);
} else if let Some(error) = &error {
Paragraph::new(error.as_str())
.style(Style::default().fg(THEME.as_ref().error()))
.wrap(Wrap { trim: true })
.render(area, buffer);
} else if confirming {
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,
);
let mut rows = vec![
("Version", selected_version.as_str()),
("Minecraft", minecraft_versions.as_str()),
("Loader", loaders.as_str()),
("Released", release_date.as_str()),
];
if !dependency_installs.is_empty() {
rows.push(("Also installs", dependency_installs.as_str()));
}
if !dependency_replacements.is_empty() {
rows.push(("Also changes", dependency_replacements.as_str()));
}
let optional_text;
if optional_dependencies > 0 {
optional_text = optional_dependencies.to_string();
rows.push(("Optional not installed", optional_text.as_str()));
}
crate::tui::widgets::popups::base::render_summary(&rows, area, buffer);
} else if items.is_empty() {
Paragraph::new(if selecting_minecraft_version {
"No compatible Minecraft versions found."
@@ -747,8 +786,19 @@ fn render_version_popup(frame: &mut Frame, area: Rect, state: &DiscoveryState) {
frame.render_widget(popup, popup_area);
}
fn version_popup_height(confirming: bool) -> u16 {
if confirming { 6 } else { VERSION_POPUP_HEIGHT }
fn version_popup_height(
confirming: bool,
plan: Option<&crate::instance::content::dependencies::DependencyPlan>,
) -> u16 {
if !confirming {
return VERSION_POPUP_HEIGHT;
}
let Some(plan) = plan else {
return 6;
};
6 + u16::from(plan.dependency_installs().next().is_some())
+ u16::from(plan.dependency_replacements().next().is_some())
+ u16::from(plan.optional_dependencies > 0)
}
fn confirmation_values(values: &[String]) -> String {
+83 -16
View File
@@ -9,7 +9,7 @@ use ratatui::{
layout::Rect,
style::{Modifier, Style},
text::{Line, Span},
widgets::{Paragraph, Widget},
widgets::{Paragraph, Widget, Wrap},
};
use crate::config::theme::THEME;
@@ -37,22 +37,58 @@ pub enum ConfirmTarget {
Content {
name: String,
path: std::path::PathBuf,
dependents: Vec<String>,
},
OrphanDependencies {
paths: Vec<std::path::PathBuf>,
},
}
impl ConfirmTarget {
fn title(&self) -> String {
format!(" Delete '{}' ", self.name())
match self {
Self::OrphanDependencies { .. } => " Remove unused libraries ".to_owned(),
_ => format!(" Delete '{}' ", self.name()),
}
}
fn body(&self) -> &'static str {
fn body(&self) -> String {
match self {
ConfirmTarget::Instance { .. } => "This will permanently remove the instance",
ConfirmTarget::Account { .. } => "This will permanently remove this account",
ConfirmTarget::ConfigProfile { .. } => {
"This will permanently remove this config profile"
ConfirmTarget::Instance { .. } => {
"This will permanently remove the instance".to_owned()
}
ConfirmTarget::Account { .. } => "This will permanently remove this account".to_owned(),
ConfirmTarget::ConfigProfile { .. } => {
"This will permanently remove this config profile".to_owned()
}
ConfirmTarget::Content { dependents, .. } if !dependents.is_empty() => {
format!(
"This library is still required by:\n{}\n\nDeleting it may break those mods.",
dependents
.iter()
.map(|name| format!("- {name}"))
.collect::<Vec<_>>()
.join("\n")
)
}
ConfirmTarget::Content { .. } => {
"This will permanently remove the selected item".to_owned()
}
ConfirmTarget::OrphanDependencies { paths } => {
format!(
"These automatically installed libraries are no longer required:\n{}",
paths
.iter()
.map(|path| format!(
"- {}",
path.file_name()
.and_then(|name| name.to_str())
.unwrap_or("library")
))
.collect::<Vec<_>>()
.join("\n")
)
}
ConfirmTarget::Content { .. } => "This will permanently remove the selected item",
}
}
@@ -62,6 +98,15 @@ impl ConfirmTarget {
ConfirmTarget::Account { username, .. } => username,
ConfirmTarget::ConfigProfile { profile } => profile,
ConfirmTarget::Content { name, .. } => name,
ConfirmTarget::OrphanDependencies { .. } => "unused libraries",
}
}
fn confirm_label(&self) -> &'static str {
match self {
Self::Content { dependents, .. } if !dependents.is_empty() => " delete anyway",
Self::OrphanDependencies { .. } => " remove all",
_ => " confirm",
}
}
}
@@ -86,12 +131,25 @@ pub fn set_pending_instance_delete(name: impl Into<String>) {
}
pub fn set_pending_content_delete(name: impl Into<String>, path: impl Into<std::path::PathBuf>) {
set_pending_managed_content_delete(name, path, Vec::new());
}
pub fn set_pending_managed_content_delete(
name: impl Into<String>,
path: impl Into<std::path::PathBuf>,
dependents: Vec<String>,
) {
set_pending(ConfirmTarget::Content {
name: name.into(),
path: path.into(),
dependents,
});
}
pub fn set_pending_orphan_dependencies(paths: Vec<std::path::PathBuf>) {
set_pending(ConfirmTarget::OrphanDependencies { paths });
}
pub fn pending_target() -> Option<ConfirmTarget> {
match CONFIRM_STATE.lock() {
Ok(s) => s.target.clone(),
@@ -112,19 +170,25 @@ pub fn clear_pending() {
pub struct ConfirmPopup {
title: String,
body: &'static str,
body: String,
confirm_label: &'static str,
}
impl ConfirmPopup {
pub fn new(title: impl Into<String>, body: &'static str) -> Self {
pub fn new(
title: impl Into<String>,
body: impl Into<String>,
confirm_label: &'static str,
) -> Self {
Self {
title: title.into(),
body,
body: body.into(),
confirm_label,
}
}
pub fn for_target(target: &ConfirmTarget) -> Self {
Self::new(target.title(), target.body())
Self::new(target.title(), target.body(), target.confirm_label())
}
}
@@ -139,11 +203,12 @@ impl Widget for ConfirmPopup {
.fg(theme.text_dim())
.add_modifier(Modifier::BOLD),
)]);
let kb = keybind_line(&[("Enter", " confirm")]);
let kb = keybind_line(&[("Enter", self.confirm_label)]);
let border_color = theme.text_dim();
let bg_color = theme.surface();
let text_color = theme.text();
let body = self.body;
let popup = PopupFrame {
title,
border_color,
@@ -151,8 +216,9 @@ impl Widget for ConfirmPopup {
keybinds: Some(kb),
search_line: None,
content: Box::new(move |inner, buf| {
Paragraph::new(self.body)
Paragraph::new(body.as_str())
.style(Style::default().fg(text_color))
.wrap(Wrap { trim: true })
.render(inner, buf);
}),
};
@@ -165,10 +231,11 @@ pub fn confirm_popup_area(frame_area: Rect, target: &ConfirmTarget) -> Rect {
use super::word_wrap_size;
use ratatui::layout::Constraint;
const MAX_W: usize = 48;
let body = target.body();
let title_w = Span::raw(target.name()).width() + 12;
let (body_w, _) = word_wrap_size(target.body(), MAX_W);
let (body_w, _) = word_wrap_size(&body, MAX_W);
let inner_w = title_w.max(body_w).min(MAX_W);
let (_, lines) = word_wrap_size(target.body(), inner_w);
let (_, lines) = word_wrap_size(&body, inner_w);
let popup_w = ((inner_w + 2) as u16).min(frame_area.width.saturating_sub(4));
let popup_h = ((lines + 2) as u16).min(frame_area.height.saturating_sub(4));
frame_area.centered(Constraint::Length(popup_w), Constraint::Length(popup_h))
+16 -14
View File
@@ -42,23 +42,25 @@ pub fn word_wrap_size(text: &str, max_inner_width: usize) -> (usize, usize) {
return (0, 1);
}
let mut lines: usize = 1;
let mut current_line_len: usize = 0;
let mut widest_line: usize = 0;
for word in text.split_whitespace() {
let word_len = Span::raw(word).width().min(max_inner_width);
if current_line_len == 0 {
current_line_len = word_len;
} else if current_line_len + 1 + word_len <= max_inner_width {
current_line_len += 1 + word_len;
} else {
widest_line = widest_line.max(current_line_len);
lines += 1;
current_line_len = word_len;
let mut lines = 0;
for logical_line in text.split('\n') {
let mut current_line_len = 0;
lines += 1;
for word in logical_line.split_whitespace() {
let word_len = Span::raw(word).width().min(max_inner_width);
if current_line_len == 0 {
current_line_len = word_len;
} else if current_line_len + 1 + word_len <= max_inner_width {
current_line_len += 1 + word_len;
} else {
widest_line = widest_line.max(current_line_len);
lines += 1;
current_line_len = word_len;
}
}
widest_line = widest_line.max(current_line_len);
}
widest_line = widest_line.max(current_line_len);
(widest_line, lines)
}