diff --git a/src/instance/import/refresh.rs b/src/instance/import/refresh.rs index 66897f7..00e766d 100644 --- a/src/instance/import/refresh.rs +++ b/src/instance/import/refresh.rs @@ -55,7 +55,7 @@ pub async fn prepare( if crate::instance::runtime::get(&instance.name) .is_some_and(|state| !matches!(state, crate::instance::runtime::RunState::Crashed(_))) { - return Err("Stop the instance before updating its modpack".to_owned()); + return Err("Stop the instance before changing its modpack".to_owned()); } let source = instance .modpack_source @@ -78,7 +78,7 @@ pub async fn prepare( let available = fs2::available_space(&manager.instances_dir).map_err(|e| e.to_string())?; if available < needed { return Err(format!( - "Not enough free space to stage this update (need about {}, available {})", + "Not enough free space to stage this modpack change (need about {}, available {})", format_bytes(needed), format_bytes(available) )); diff --git a/src/tui/app.rs b/src/tui/app.rs index 95e5f22..bd81595 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -54,6 +54,7 @@ pub struct App { pub(super) content_update_snapshot: Option<(String, crate::instance::content::updates::UpdateSnapshot)>, pub(super) content_update_popup: Option, + pub(super) modpack_versions_state: Option, pub(super) modpack_update_popup: Option, pub(super) provider_conflict: Option, pub(super) dismissed_provider_conflicts: HashSet, @@ -186,6 +187,7 @@ impl App { content_manifest: None, content_update_snapshot: None, content_update_popup: None, + modpack_versions_state: None, modpack_update_popup: None, provider_conflict: None, dismissed_provider_conflicts: HashSet::new(), diff --git a/src/tui/event.rs b/src/tui/event.rs index 88e9691..a7e9f5a 100644 --- a/src/tui/event.rs +++ b/src/tui/event.rs @@ -42,6 +42,9 @@ impl App { self.drain_pending_instances(); self.instances_state.drain_modpack_updates(); self.drain_pending_last_played(); + if let Some(state) = self.modpack_versions_state.as_mut() { + state.drain_pending(); + } let content_update_completed = self.content_update_popup.as_mut().and_then(|update| { update.drain(); update.list.request_image_loads(&self.picker); @@ -210,6 +213,7 @@ impl App { .is_some_and(widgets::content::update::State::visible), ) + usize::from(self.modpack_update_popup.is_some()) + + usize::from(self.modpack_versions_state.is_some()) + usize::from(!matches!( &self.account_state.add_mode, widgets::account::AddMode::None diff --git a/src/tui/input.rs b/src/tui/input.rs index 65fa81a..8df7d01 100644 --- a/src/tui/input.rs +++ b/src/tui/input.rs @@ -68,6 +68,10 @@ impl App { self.handle_content_update_key(key_event); return Ok(()); } + if self.modpack_versions_state.is_some() { + self.handle_modpack_versions_key(key_event); + return Ok(()); + } if self.modpack_update_popup.is_some() { self.handle_modpack_update_key(key_event); return Ok(()); @@ -800,6 +804,12 @@ impl App { { self.spawn_modpack_update(); } + KeyCode::Char('v') + if self.focused == FocusedArea::Instances + && !self.instances_state.search.active => + { + self.open_modpack_versions(); + } KeyCode::Char('r') if self.focused == FocusedArea::Instances && !self.instances_state.search.active => @@ -845,20 +855,115 @@ impl App { } fn spawn_modpack_update(&mut self) { - let Some(instance) = self.instances_state.selected_instance().cloned() else { - return; - }; let Some(target) = self.instances_state.selected_modpack_update() else { return; }; - let state = widgets::popups::modpack_update::State::preparing(); + self.spawn_modpack_refresh(target, widgets::popups::modpack_update::Action::Update); + } + + fn open_modpack_versions(&mut self) { + let Some(instance) = self.instances_state.selected_instance() else { + return; + }; + let Some(source) = instance.modpack_source.clone() else { + return; + }; + let mut state = widgets::content::DiscoveryState::new_modpacks(); + let Some(request) = state.begin_managed_modpack_versions(&instance.name, source) else { + return; + }; + self.modpack_versions_state = Some(state); + self.spawn_modpack_versions_request(request); + } + + fn spawn_modpack_versions_request( + &self, + request: widgets::content::discovery::VersionsRequest, + ) { + tokio::spawn(async move { + let source = crate::instance::ProviderProject { + provider: request.provider.clone(), + project_id: request.project_id.clone(), + version_id: request.current_version_id.clone().unwrap_or_default(), + }; + let result = crate::instance::import::provider_versions(&source) + .await + .map_err(|error| error.to_string()); + widgets::content::DiscoveryState::push_action_result( + &request.pending, + widgets::content::discovery::DiscoveryActionResult::Versions { + request_id: request.request_id, + project_id: request.project_id, + result, + }, + ); + }); + } + + fn handle_modpack_versions_key(&mut self, key_event: KeyEvent) { + if key_event.code == KeyCode::Tab { + let request = self + .modpack_versions_state + .as_mut() + .and_then(widgets::content::DiscoveryState::switch_version_source); + if let Some(request) = request { + self.spawn_modpack_versions_request(request); + } + return; + } + if key_event.code == KeyCode::Enter { + let target = self + .modpack_versions_state + .as_ref() + .and_then(|state| state.version_popup.as_ref()) + .filter(|popup| !popup.loading) + .and_then(|popup| popup.selected_version().cloned()); + if let Some(target) = target { + let action = if self + .instances_state + .selected_instance() + .and_then(|instance| instance.modpack_source.as_ref()) + .is_some_and(|source| source.version_id == target.id) + { + widgets::popups::modpack_update::Action::Reinstall + } else { + widgets::popups::modpack_update::Action::Change + }; + self.modpack_versions_state = None; + self.spawn_modpack_refresh(target, action); + } + return; + } + let Some(state) = self.modpack_versions_state.as_mut() else { + return; + }; + widgets::content::discovery::handle_key(&key_event, state); + if state.version_popup.is_none() { + self.modpack_versions_state = None; + } + } + + fn spawn_modpack_refresh( + &mut self, + target: crate::net::modrinth::VersionInfo, + action: widgets::popups::modpack_update::Action, + ) { + let Some(instance) = self.instances_state.selected_instance().cloned() else { + return; + }; + let state = widgets::popups::modpack_update::State::preparing(action); let pending = state.pending.clone(); self.modpack_update_popup = Some(state); let instances_dir = self.instance_manager.instances_dir.clone(); let meta_dir = self.instance_manager.meta_dir.clone(); tokio::spawn(async move { + let action = match action { + widgets::popups::modpack_update::Action::Update => "update", + widgets::popups::modpack_update::Action::Change => "change", + widgets::popups::modpack_update::Action::Reinstall => "reinstall", + }; let progress = crate::feedback::progress::ProgressTask::start(format!( - "Preparing modpack update for {}", + "Preparing modpack {action} for {}", instance.name )); let manager = crate::instance::InstanceManager::new(instances_dir, meta_dir); @@ -920,6 +1025,7 @@ impl App { let Some(state) = self.modpack_update_popup.as_mut() else { return; }; + let action = state.action; let replacements = state.replacements(); let Some(plan) = state.plan.take() else { return; @@ -927,7 +1033,11 @@ impl App { state.phase = widgets::popups::modpack_update::Phase::Applying; let pending = state.pending.clone(); tokio::spawn(async move { - let progress = crate::feedback::progress::ProgressTask::start("Updating modpack"); + let progress = crate::feedback::progress::ProgressTask::start(match action { + widgets::popups::modpack_update::Action::Update => "Updating modpack", + widgets::popups::modpack_update::Action::Change => "Changing modpack", + widgets::popups::modpack_update::Action::Reinstall => "Reinstalling modpack", + }); let result = tokio::task::spawn_blocking(move || { crate::instance::import::refresh::apply(plan, &replacements) }) diff --git a/src/tui/render.rs b/src/tui/render.rs index 0f1ae6f..e200a5b 100644 --- a/src/tui/render.rs +++ b/src/tui/render.rs @@ -79,6 +79,7 @@ impl App { } } if load_content { + let has_modpack_update = self.instances_state.selected_modpack_update().is_some(); widgets::content::render( frame, main_chunks[1], @@ -86,6 +87,7 @@ impl App { self.content_tab, self.content_mode, self.instances_state.selected_instance(), + has_modpack_update, &mut self.mods_state, &mut self.mods_discovery_state, &mut self.resource_packs_state, @@ -183,6 +185,15 @@ impl App { widgets::content::update::render(frame, update, &self.picker); } + if let Some(state) = self.modpack_versions_state.as_mut() { + widgets::content::tabs::render_version_popup( + frame, + main_chunks[1], + state, + &self.picker, + ); + } + if let Some(update) = &self.modpack_update_popup { widgets::popups::modpack_update::render(frame, update); } diff --git a/src/tui/tests/flows.rs b/src/tui/tests/flows.rs index 5a4f383..0b99afb 100644 --- a/src/tui/tests/flows.rs +++ b/src/tui/tests/flows.rs @@ -119,6 +119,89 @@ fn installed_version_hint_requires_selected_provider_match() { assert!(ui.screen().contains("[v] versions")); } +#[test] +fn managed_modpack_instance_exposes_direct_version_selector() { + let mut ui = UiHarness::new(); + ui.add_instance("Managed Pack"); + + ui.draw(); + assert!(!ui.screen().contains("[v] versions")); + ui.app.instances_state.instances[0].modpack_source = Some(ProviderProject { + provider: "unsupported".to_owned(), + project_id: "pack".to_owned(), + version_id: "current".to_owned(), + }); + + ui.draw(); + assert!(ui.screen().contains("[v] versions")); + ui.key(KeyCode::Char('v')); + let popup = ui + .app + .modpack_versions_state + .as_ref() + .and_then(|state| state.version_popup.as_ref()) + .unwrap(); + assert!(!popup.selecting_minecraft_version); + assert_eq!(popup.current_version_id.as_deref(), Some("current")); + + ui.key(KeyCode::Esc); + assert!(ui.app.modpack_versions_state.is_none()); + + ui.key(KeyCode::Char('v')); + let popup = ui + .app + .modpack_versions_state + .as_mut() + .and_then(|state| state.version_popup.as_mut()) + .unwrap(); + popup.loading = false; + popup.versions = vec![crate::net::modrinth::VersionInfo { + id: "current".to_owned(), + project_id: "pack".to_owned(), + name: "Current".to_owned(), + version_number: "1.0".to_owned(), + game_versions: vec!["1.21.1".to_owned()], + loaders: Vec::new(), + version_type: crate::net::modrinth::VersionType::Release, + dependencies: Vec::new(), + date_published: String::new(), + files: Vec::new(), + }]; + ui.key(KeyCode::Enter); + assert!(ui.app.modpack_versions_state.is_none()); + assert_eq!( + ui.app.modpack_update_popup.as_ref().unwrap().action, + crate::tui::widgets::popups::modpack_update::Action::Reinstall + ); + + ui.app.modpack_update_popup = None; + ui.key(KeyCode::Char('v')); + let popup = ui + .app + .modpack_versions_state + .as_mut() + .and_then(|state| state.version_popup.as_mut()) + .unwrap(); + popup.loading = false; + popup.versions = vec![crate::net::modrinth::VersionInfo { + id: "new".to_owned(), + project_id: "pack".to_owned(), + name: "New".to_owned(), + version_number: "2.0".to_owned(), + game_versions: vec!["1.21.1".to_owned()], + loaders: Vec::new(), + version_type: crate::net::modrinth::VersionType::Release, + dependencies: Vec::new(), + date_published: String::new(), + files: Vec::new(), + }]; + ui.key(KeyCode::Enter); + assert_eq!( + ui.app.modpack_update_popup.as_ref().unwrap().action, + crate::tui::widgets::popups::modpack_update::Action::Change + ); +} + #[test] fn world_datapack_version_action_requires_selected_provider_match() { let mut ui = UiHarness::new(); diff --git a/src/tui/tests/harness.rs b/src/tui/tests/harness.rs index 97b1e02..5ce2979 100644 --- a/src/tui/tests/harness.rs +++ b/src/tui/tests/harness.rs @@ -94,6 +94,7 @@ impl UiHarness { content_manifest: None, content_update_snapshot: None, content_update_popup: None, + modpack_versions_state: None, modpack_update_popup: None, provider_conflict: None, dismissed_provider_conflicts: HashSet::new(), diff --git a/src/tui/tests/widgets/content/discovery.rs b/src/tui/tests/widgets/content/discovery.rs index a7d5226..3c3955d 100644 --- a/src/tui/tests/widgets/content/discovery.rs +++ b/src/tui/tests/widgets/content/discovery.rs @@ -537,6 +537,29 @@ fn modpacks_choose_minecraft_before_filtering_pack_versions() { ); } +#[test] +fn managed_modpack_versions_open_directly_and_mark_reinstall() { + let mut state = DiscoveryState::new_modpacks(); + let source = crate::instance::ProviderProject { + provider: "modrinth".to_owned(), + project_id: "pack".to_owned(), + version_id: "current".to_owned(), + }; + + let request = state + .begin_managed_modpack_versions("Managed Pack", source) + .unwrap(); + assert_eq!(request.current_version_id.as_deref(), Some("current")); + let popup = state.version_popup.as_mut().unwrap(); + assert!(!popup.selecting_minecraft_version); + popup.loading = false; + popup.versions = vec![version("current"), version("older")]; + + assert_eq!(popup.title(), "Reinstall Managed Pack"); + popup.selected = 1; + assert_eq!(popup.title(), "Change Managed Pack version"); +} + #[test] fn project_page_loads_for_the_selected_discovery_entry() { let project = DiscoveryProject { diff --git a/src/tui/tests/widgets/instances.rs b/src/tui/tests/widgets/instances.rs index 3f26f9d..a37d304 100644 --- a/src/tui/tests/widgets/instances.rs +++ b/src/tui/tests/widgets/instances.rs @@ -79,38 +79,3 @@ fn instances_list_renders_empty() { insta::assert_snapshot!(terminal.backend()); } - -#[test] -fn managed_modpack_update_is_badged_in_the_instance_list() { - let mut state = State::with_instances(vec![synthetic_instance("Managed Pack")]); - state.modpack_updates.insert( - "Managed Pack".to_owned(), - crate::net::modrinth::VersionInfo { - id: "new".to_owned(), - project_id: "pack".to_owned(), - name: "New".to_owned(), - version_number: "2.0".to_owned(), - game_versions: Vec::new(), - loaders: Vec::new(), - version_type: crate::net::modrinth::VersionType::Release, - dependencies: Vec::new(), - date_published: String::new(), - files: Vec::new(), - }, - ); - let backend = TestBackend::new(40, 5); - let mut terminal = Terminal::new(backend).unwrap(); - - terminal - .draw(|frame| render(frame, frame.area(), FocusedArea::Instances, &mut state)) - .unwrap(); - - let text = terminal - .backend() - .buffer() - .content() - .iter() - .map(|cell| cell.symbol()) - .collect::(); - assert!(text.contains("Managed Pack Update")); -} diff --git a/src/tui/widgets/content/discovery.rs b/src/tui/widgets/content/discovery.rs index 75efd7f..30609af 100644 --- a/src/tui/widgets/content/discovery.rs +++ b/src/tui/widgets/content/discovery.rs @@ -393,14 +393,15 @@ pub struct VersionPopupState { impl VersionPopupState { pub fn title(&self) -> String { - if self.installed_path.is_some() + let installed = self.installed_path.is_some() || self.current_version_id.is_some(); + if installed && self.current_version_id.as_deref().is_some_and(|current| { self.selected_version() .is_some_and(|version| version.id == current) }) { format!("Reinstall {}", self.project_title) - } else if self.installed_path.is_some() { + } else if installed { format!("Change {} version", self.project_title) } else { format!("Install {}", self.project_title) @@ -736,7 +737,7 @@ impl DiscoveryState { .get(&entry.file_stem) .cloned() .unwrap_or_else(|| entry.provider_project.clone().into_iter().collect()); - self.open_version_popup(&entry, sources, installed_path, None) + self.open_version_popup(&entry.name, sources, installed_path, None) } pub fn begin_installed_versions( @@ -771,12 +772,22 @@ impl DiscoveryState { } } } - self.open_version_popup(entry, sources, Some(entry.path.clone()), target_world) + self.open_version_popup(&entry.name, sources, Some(entry.path.clone()), target_world) + } + + pub fn begin_managed_modpack_versions( + &mut self, + project_title: &str, + source: crate::instance::ProviderProject, + ) -> Option { + let request = self.open_version_popup(project_title, vec![source], None, None)?; + self.version_popup.as_mut()?.selecting_minecraft_version = false; + Some(request) } fn open_version_popup( &mut self, - entry: &ContentEntry, + project_title: &str, mut sources: Vec, installed_path: Option, target_world: Option<(String, PathBuf)>, @@ -791,7 +802,7 @@ impl DiscoveryState { request_id, project_id: source.project_id.clone(), provider: source.provider.clone(), - project_title: entry.name.clone(), + project_title: project_title.to_owned(), sources, source_index: 0, installed_path, diff --git a/src/tui/widgets/content/tabs.rs b/src/tui/widgets/content/tabs.rs index a98cd64..7222bb4 100644 --- a/src/tui/widgets/content/tabs.rs +++ b/src/tui/widgets/content/tabs.rs @@ -157,6 +157,7 @@ pub fn render( tab: ContentTab, mode: ContentMode, instance: Option<&crate::instance::InstanceConfig>, + has_modpack_update: bool, mods_state: &mut super::list::ContentListState, mods_discovery_state: &mut DiscoveryState, resource_packs_state: &mut super::list::ContentListState, @@ -420,6 +421,7 @@ pub fn render( Some(&[ ("l", " launch"), ("⏎", " content"), + ("v", " versions"), ("Shift+⏎", " open dir"), ("Esc", " kill"), ("a", " add"), @@ -440,6 +442,14 @@ pub fn render( if is_focused && mode == ContentMode::Installed && !can_change_version { keybinds.retain(|(key, _)| *key != "v"); } + if focused == FocusedArea::Instances + && instance.is_none_or(|instance| instance.modpack_source.is_none()) + { + keybinds.retain(|(key, _)| *key != "v"); + } + if focused == FocusedArea::Instances && !has_modpack_update { + keybinds.retain(|(key, _)| *key != "u"); + } if is_focused && !keybinds.iter().any(|(key, _)| key.contains("Esc")) { keybinds.push(("Esc", " back")); } @@ -768,7 +778,7 @@ fn render_discovery_body( } } -fn render_version_popup( +pub(crate) fn render_version_popup( frame: &mut Frame, area: Rect, state: &mut DiscoveryState, diff --git a/src/tui/widgets/instances.rs b/src/tui/widgets/instances.rs index 60a25c7..6c00134 100644 --- a/src/tui/widgets/instances.rs +++ b/src/tui/widgets/instances.rs @@ -349,18 +349,6 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S } else { let mut spans = vec![selector.clone()]; spans.extend(state.search.highlight_spans(&instance.name, name_style)); - if state.modpack_updates.contains_key(&instance.name) { - spans.extend([ - Span::raw(" "), - Span::styled( - "Update", - Style::default() - .fg(theme.background()) - .bg(theme.warning()) - .add_modifier(Modifier::BOLD), - ), - ]); - } Line::from(spans) }; diff --git a/src/tui/widgets/popups/modpack_update.rs b/src/tui/widgets/popups/modpack_update.rs index 231edc8..29e1f8f 100644 --- a/src/tui/widgets/popups/modpack_update.rs +++ b/src/tui/widgets/popups/modpack_update.rs @@ -26,8 +26,16 @@ pub enum Phase { Applying, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Action { + Update, + Change, + Reinstall, +} + pub struct State { pub phase: Phase, + pub action: Action, pub plan: Option, pub selected: usize, pub replace: Vec, @@ -37,9 +45,10 @@ pub struct State { } impl State { - pub fn preparing() -> Self { + pub fn preparing(action: Action) -> Self { Self { phase: Phase::Preparing, + action, plan: None, selected: 0, replace: Vec::new(), @@ -108,14 +117,31 @@ pub fn render(frame: &mut Frame, state: &State) { .flex(Flex::Center) .areas(area); frame.render_widget(Clear, area); + let action = match state.action { + Action::Update => "update", + Action::Change => "change", + Action::Reinstall => "reinstall", + }; let (title, footer) = match state.phase { - Phase::Preparing => (" Prepare modpack update ", " [Esc] cancel "), + Phase::Preparing => match state.action { + Action::Update => (" Prepare modpack update ", " [Esc] cancel "), + Action::Change => (" Prepare modpack change ", " [Esc] cancel "), + Action::Reinstall => (" Prepare modpack reinstall ", " [Esc] cancel "), + }, Phase::Conflicts => ( " Resolve modpack conflicts ", " [j/k] select [Space] keep/replace [Enter] continue [Esc] cancel ", ), - Phase::Review => (" Update modpack ", " [Enter] update [Esc] cancel "), - Phase::Applying => (" Updating modpack ", " Please wait "), + Phase::Review => match state.action { + Action::Update => (" Update modpack ", " [Enter] update [Esc] cancel "), + Action::Change => (" Change modpack ", " [Enter] change [Esc] cancel "), + Action::Reinstall => (" Reinstall modpack ", " [Enter] reinstall [Esc] cancel "), + }, + Phase::Applying => match state.action { + Action::Update => (" Updating modpack ", " Please wait "), + Action::Change => (" Changing modpack ", " Please wait "), + Action::Reinstall => (" Reinstalling modpack ", " Please wait "), + }, }; let block = Block::default() .title(title) @@ -126,7 +152,7 @@ pub fn render(frame: &mut Frame, state: &State) { .style(Style::default().fg(theme.text()).bg(theme.surface())); match state.phase { Phase::Preparing => frame.render_widget( - Paragraph::new("Downloading and validating the target pack…").block(block), + Paragraph::new(format!("Preparing the selected modpack {action}…")).block(block), area, ), Phase::Applying => frame.render_widget(