feat(content): install Modrinth project versions
This commit is contained in:
1 parent
725b52579b
commit
a05b030862
13 files changed
+1098
-117
No files matched your search
@@ -125,6 +125,8 @@ mod tests {
|
|||||||
ContentEntry {
|
ContentEntry {
|
||||||
file_stem: file_stem.to_string(),
|
file_stem: file_stem.to_string(),
|
||||||
name: file_stem.to_string(),
|
name: file_stem.to_string(),
|
||||||
|
source_slug: None,
|
||||||
|
installed_path: None,
|
||||||
title_suffix: None,
|
title_suffix: None,
|
||||||
footer_label: None,
|
footer_label: None,
|
||||||
description: String::new(),
|
description: String::new(),
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ pub struct IconCell {
|
|||||||
pub struct ContentEntry {
|
pub struct ContentEntry {
|
||||||
pub file_stem: String,
|
pub file_stem: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub source_slug: Option<String>,
|
||||||
|
pub installed_path: Option<PathBuf>,
|
||||||
pub title_suffix: Option<String>,
|
pub title_suffix: Option<String>,
|
||||||
pub footer_label: Option<String>,
|
pub footer_label: Option<String>,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
@@ -110,6 +112,8 @@ pub fn scan_one_mod(path: &Path, file_stem: &str, enabled: bool) -> ContentEntry
|
|||||||
ContentEntry {
|
ContentEntry {
|
||||||
file_stem: file_stem.to_owned(),
|
file_stem: file_stem.to_owned(),
|
||||||
name: display_name,
|
name: display_name,
|
||||||
|
source_slug: None,
|
||||||
|
installed_path: None,
|
||||||
title_suffix: None,
|
title_suffix: None,
|
||||||
footer_label: None,
|
footer_label: None,
|
||||||
description,
|
description,
|
||||||
@@ -705,6 +709,8 @@ mod tests {
|
|||||||
let entry = ContentEntry {
|
let entry = ContentEntry {
|
||||||
file_stem: "mymod".to_string(),
|
file_stem: "mymod".to_string(),
|
||||||
name: "mymod".to_string(),
|
name: "mymod".to_string(),
|
||||||
|
source_slug: None,
|
||||||
|
installed_path: None,
|
||||||
title_suffix: None,
|
title_suffix: None,
|
||||||
footer_label: None,
|
footer_label: None,
|
||||||
description: String::new(),
|
description: String::new(),
|
||||||
@@ -978,6 +984,8 @@ description = "A neoforge mod"
|
|||||||
let entry = ContentEntry {
|
let entry = ContentEntry {
|
||||||
file_stem: "mymod".to_string(),
|
file_stem: "mymod".to_string(),
|
||||||
name: "mymod".to_string(),
|
name: "mymod".to_string(),
|
||||||
|
source_slug: None,
|
||||||
|
installed_path: None,
|
||||||
title_suffix: None,
|
title_suffix: None,
|
||||||
footer_label: None,
|
footer_label: None,
|
||||||
description: String::new(),
|
description: String::new(),
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ pub fn scan_one_resource_pack(path: &Path, file_stem: &str, enabled: bool) -> Co
|
|||||||
ContentEntry {
|
ContentEntry {
|
||||||
file_stem: file_stem.to_owned(),
|
file_stem: file_stem.to_owned(),
|
||||||
name: display_name,
|
name: display_name,
|
||||||
|
source_slug: None,
|
||||||
|
installed_path: None,
|
||||||
title_suffix: None,
|
title_suffix: None,
|
||||||
footer_label: None,
|
footer_label: None,
|
||||||
description,
|
description,
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ pub fn scan_one_shader(path: &Path, file_stem: &str, enabled: bool) -> ContentEn
|
|||||||
ContentEntry {
|
ContentEntry {
|
||||||
file_stem: file_stem.to_owned(),
|
file_stem: file_stem.to_owned(),
|
||||||
name: file_stem.to_owned(),
|
name: file_stem.to_owned(),
|
||||||
|
source_slug: None,
|
||||||
|
installed_path: None,
|
||||||
title_suffix: None,
|
title_suffix: None,
|
||||||
footer_label: None,
|
footer_label: None,
|
||||||
description,
|
description,
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ pub fn scan_one_world(path: &Path, file_stem: &str, enabled: bool) -> ContentEnt
|
|||||||
ContentEntry {
|
ContentEntry {
|
||||||
name: file_stem.to_owned(),
|
name: file_stem.to_owned(),
|
||||||
file_stem: file_stem.to_owned(),
|
file_stem: file_stem.to_owned(),
|
||||||
|
source_slug: None,
|
||||||
|
installed_path: None,
|
||||||
title_suffix: None,
|
title_suffix: None,
|
||||||
footer_label: None,
|
footer_label: None,
|
||||||
description,
|
description,
|
||||||
|
|||||||
+235
-12
@@ -260,6 +260,125 @@ pub async fn fetch_versions(
|
|||||||
Ok(versions)
|
Ok(versions)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_content_versions(
|
||||||
|
client: &crate::net::HttpClient,
|
||||||
|
project_id: &str,
|
||||||
|
kind: DiscoveryKind,
|
||||||
|
game_version: &str,
|
||||||
|
loader: ModLoader,
|
||||||
|
) -> Result<Vec<VersionInfo>, crate::net::NetError> {
|
||||||
|
let url = content_versions_url(API_BASE, project_id, kind, game_version, loader);
|
||||||
|
tracing::debug!(
|
||||||
|
"Fetching compatible Modrinth versions for '{}' ({}, {})",
|
||||||
|
project_id,
|
||||||
|
game_version,
|
||||||
|
loader
|
||||||
|
);
|
||||||
|
client.get_json(&url).await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn content_versions_url(
|
||||||
|
api_base: &str,
|
||||||
|
project_id: &str,
|
||||||
|
kind: DiscoveryKind,
|
||||||
|
game_version: &str,
|
||||||
|
loader: ModLoader,
|
||||||
|
) -> String {
|
||||||
|
let mut params = vec![
|
||||||
|
"include_changelog=false".to_owned(),
|
||||||
|
format!(
|
||||||
|
"game_versions={}",
|
||||||
|
url_encode(&serde_json::to_string(&[game_version]).unwrap_or_default())
|
||||||
|
),
|
||||||
|
];
|
||||||
|
if kind == DiscoveryKind::Mod
|
||||||
|
&& let Some(loader) = loader_facet(loader)
|
||||||
|
{
|
||||||
|
params.push(format!(
|
||||||
|
"loaders={}",
|
||||||
|
url_encode(&serde_json::to_string(&[loader]).unwrap_or_default())
|
||||||
|
));
|
||||||
|
}
|
||||||
|
format!(
|
||||||
|
"{api_base}/project/{}/version?{}",
|
||||||
|
url_encode(project_id),
|
||||||
|
params.join("&")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select_primary_file(version: &VersionInfo) -> Result<&VersionFile, crate::net::NetError> {
|
||||||
|
version
|
||||||
|
.files
|
||||||
|
.iter()
|
||||||
|
.find(|file| file.primary)
|
||||||
|
.or_else(|| version.files.first())
|
||||||
|
.ok_or_else(|| crate::net::NetError::Parse("No files in version".to_owned()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum DownloadOutcome {
|
||||||
|
Downloaded(std::path::PathBuf),
|
||||||
|
SkippedExisting(std::path::PathBuf),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_version_file(
|
||||||
|
client: &crate::net::HttpClient,
|
||||||
|
version: &VersionInfo,
|
||||||
|
destination: &std::path::Path,
|
||||||
|
) -> Result<DownloadOutcome, crate::net::NetError> {
|
||||||
|
let file = select_primary_file(version)?;
|
||||||
|
let path = destination.join(&file.filename);
|
||||||
|
if path.exists() {
|
||||||
|
return Ok(DownloadOutcome::SkippedExisting(path));
|
||||||
|
}
|
||||||
|
crate::net::download_file(client, &file.url, &path, |_, _| {}).await?;
|
||||||
|
Ok(DownloadOutcome::Downloaded(path))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn download_version_file_for_update(
|
||||||
|
client: &crate::net::HttpClient,
|
||||||
|
version: &VersionInfo,
|
||||||
|
destination: &std::path::Path,
|
||||||
|
installed_path: &std::path::Path,
|
||||||
|
) -> Result<DownloadOutcome, crate::net::NetError> {
|
||||||
|
let file = select_primary_file(version)?;
|
||||||
|
let target = destination.join(&file.filename);
|
||||||
|
if target != installed_path {
|
||||||
|
return download_version_file(client, version, destination).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let temporary = destination.join(format!(".{}.{}.rmcl-download", file.filename, version.id));
|
||||||
|
let backup = destination.join(format!(".{}.{}.rmcl-backup", file.filename, version.id));
|
||||||
|
if temporary.exists() || backup.exists() {
|
||||||
|
return Err(crate::net::NetError::Parse(format!(
|
||||||
|
"A previous update of '{}' did not finish cleanly",
|
||||||
|
file.filename
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
crate::net::download_file(client, &file.url, &temporary, |_, _| {}).await?;
|
||||||
|
replace_installed_file(&temporary, &target, installed_path, &backup).await?;
|
||||||
|
Ok(DownloadOutcome::Downloaded(target))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn replace_installed_file(
|
||||||
|
temporary: &std::path::Path,
|
||||||
|
target: &std::path::Path,
|
||||||
|
installed_path: &std::path::Path,
|
||||||
|
backup: &std::path::Path,
|
||||||
|
) -> Result<(), crate::net::NetError> {
|
||||||
|
tokio::fs::rename(installed_path, &backup).await?;
|
||||||
|
if let Err(error) = tokio::fs::rename(&temporary, &target).await {
|
||||||
|
let _ = tokio::fs::rename(&backup, installed_path).await;
|
||||||
|
let _ = tokio::fs::remove_file(&temporary).await;
|
||||||
|
return Err(error.into());
|
||||||
|
}
|
||||||
|
if let Err(error) = tokio::fs::remove_file(&backup).await {
|
||||||
|
tracing::warn!("Failed to remove Modrinth update backup: {error}");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn fetch_version(
|
pub async fn fetch_version(
|
||||||
client: &crate::net::HttpClient,
|
client: &crate::net::HttpClient,
|
||||||
version_id: &str,
|
version_id: &str,
|
||||||
@@ -283,18 +402,7 @@ pub async fn download_mrpack(
|
|||||||
version: &VersionInfo,
|
version: &VersionInfo,
|
||||||
dest: &std::path::Path,
|
dest: &std::path::Path,
|
||||||
) -> Result<std::path::PathBuf, crate::net::NetError> {
|
) -> Result<std::path::PathBuf, crate::net::NetError> {
|
||||||
let file = version
|
let file = select_primary_file(version)?;
|
||||||
.files
|
|
||||||
.iter()
|
|
||||||
.find(|f| f.primary)
|
|
||||||
.or_else(|| {
|
|
||||||
tracing::warn!(
|
|
||||||
"Modrinth version '{}' has no primary file; using first file",
|
|
||||||
version.id
|
|
||||||
);
|
|
||||||
version.files.first()
|
|
||||||
})
|
|
||||||
.ok_or_else(|| crate::net::NetError::Parse("No files in version".to_string()))?;
|
|
||||||
|
|
||||||
let mrpack_path = dest.join(&file.filename);
|
let mrpack_path = dest.join(&file.filename);
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
@@ -331,6 +439,17 @@ pub fn parse_mrpack(path: &std::path::Path) -> Result<MrpackIndex, String> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
fn version_with_files(files: Vec<VersionFile>) -> VersionInfo {
|
||||||
|
VersionInfo {
|
||||||
|
id: "version-id".to_owned(),
|
||||||
|
name: "Version 1".to_owned(),
|
||||||
|
version_number: "1.0.0".to_owned(),
|
||||||
|
game_versions: vec!["1.21.1".to_owned()],
|
||||||
|
loaders: vec!["fabric".to_owned()],
|
||||||
|
files,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn discovery_mod_facets_include_instance_compatibility() {
|
fn discovery_mod_facets_include_instance_compatibility() {
|
||||||
let facets = discovery_facets(DiscoveryKind::Mod, "1.21.1", ModLoader::Fabric);
|
let facets = discovery_facets(DiscoveryKind::Mod, "1.21.1", ModLoader::Fabric);
|
||||||
@@ -353,6 +472,110 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compatible_mod_versions_filter_by_game_and_loader() {
|
||||||
|
let url = content_versions_url(
|
||||||
|
"https://example.test/v2",
|
||||||
|
"fabric-api",
|
||||||
|
DiscoveryKind::Mod,
|
||||||
|
"1.21.1",
|
||||||
|
ModLoader::Fabric,
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
url,
|
||||||
|
"https://example.test/v2/project/fabric-api/version?include_changelog=false&game_versions=%5B%221.21.1%22%5D&loaders=%5B%22fabric%22%5D"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compatible_resource_pack_versions_do_not_filter_by_loader() {
|
||||||
|
let url = content_versions_url(
|
||||||
|
"https://example.test/v2",
|
||||||
|
"stay-true",
|
||||||
|
DiscoveryKind::ResourcePack,
|
||||||
|
"1.21.1",
|
||||||
|
ModLoader::Fabric,
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
url,
|
||||||
|
"https://example.test/v2/project/stay-true/version?include_changelog=false&game_versions=%5B%221.21.1%22%5D"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn primary_file_selection_falls_back_to_first_file() {
|
||||||
|
let version = version_with_files(vec![
|
||||||
|
VersionFile {
|
||||||
|
url: "https://example.test/first.jar".to_owned(),
|
||||||
|
filename: "first.jar".to_owned(),
|
||||||
|
size: 1,
|
||||||
|
primary: false,
|
||||||
|
},
|
||||||
|
VersionFile {
|
||||||
|
url: "https://example.test/primary.jar".to_owned(),
|
||||||
|
filename: "primary.jar".to_owned(),
|
||||||
|
size: 1,
|
||||||
|
primary: true,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
assert_eq!(
|
||||||
|
select_primary_file(&version).unwrap().filename,
|
||||||
|
"primary.jar"
|
||||||
|
);
|
||||||
|
|
||||||
|
let fallback = version_with_files(vec![VersionFile {
|
||||||
|
url: "https://example.test/first.jar".to_owned(),
|
||||||
|
filename: "first.jar".to_owned(),
|
||||||
|
size: 1,
|
||||||
|
primary: false,
|
||||||
|
}]);
|
||||||
|
assert_eq!(
|
||||||
|
select_primary_file(&fallback).unwrap().filename,
|
||||||
|
"first.jar"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn content_download_skips_an_existing_filename() {
|
||||||
|
let directory = tempfile::tempdir().unwrap();
|
||||||
|
let path = directory.path().join("example.jar");
|
||||||
|
std::fs::write(&path, b"existing").unwrap();
|
||||||
|
let version = version_with_files(vec![VersionFile {
|
||||||
|
url: "https://example.test/example.jar".to_owned(),
|
||||||
|
filename: "example.jar".to_owned(),
|
||||||
|
size: 1,
|
||||||
|
primary: true,
|
||||||
|
}]);
|
||||||
|
|
||||||
|
let outcome =
|
||||||
|
download_version_file(&crate::net::HttpClient::new(), &version, directory.path())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(outcome, DownloadOutcome::SkippedExisting(path));
|
||||||
|
assert_eq!(
|
||||||
|
std::fs::read(directory.path().join("example.jar")).unwrap(),
|
||||||
|
b"existing"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn staged_update_replaces_the_old_file_and_cleans_its_backup() {
|
||||||
|
let directory = tempfile::tempdir().unwrap();
|
||||||
|
let installed = directory.path().join("example.jar");
|
||||||
|
let temporary = directory.path().join(".example.jar.rmcl-download");
|
||||||
|
let backup = directory.path().join(".example.jar.rmcl-backup");
|
||||||
|
std::fs::write(&installed, b"old version").unwrap();
|
||||||
|
std::fs::write(&temporary, b"new version").unwrap();
|
||||||
|
|
||||||
|
replace_installed_file(&temporary, &installed, &installed, &backup)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(std::fs::read(installed).unwrap(), b"new version");
|
||||||
|
assert_eq!(std::fs::read_dir(directory.path()).unwrap().count(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn discovery_search_ignores_new_modrinth_enum_values() {
|
fn discovery_search_ignores_new_modrinth_enum_values() {
|
||||||
let response: DiscoverySearchResponse = serde_json::from_str(
|
let response: DiscoverySearchResponse = serde_json::from_str(
|
||||||
|
|||||||
@@ -38,6 +38,15 @@ impl App {
|
|||||||
self.mods_state.drain_watcher();
|
self.mods_state.drain_watcher();
|
||||||
self.mods_state.request_image_loads(&self.picker);
|
self.mods_state.request_image_loads(&self.picker);
|
||||||
self.mods_state.drain_image_loads(&self.picker);
|
self.mods_state.drain_image_loads(&self.picker);
|
||||||
|
if self.mods_state.loaded_for.as_deref()
|
||||||
|
== self
|
||||||
|
.instances_state
|
||||||
|
.selected_instance()
|
||||||
|
.map(|instance| instance.name.as_str())
|
||||||
|
{
|
||||||
|
self.mods_discovery_state
|
||||||
|
.refresh_installed_labels(&self.mods_state.entries);
|
||||||
|
}
|
||||||
self.mods_discovery_state.drain_pending();
|
self.mods_discovery_state.drain_pending();
|
||||||
self.mods_discovery_state.list.drain_pending();
|
self.mods_discovery_state.list.drain_pending();
|
||||||
self.mods_discovery_state
|
self.mods_discovery_state
|
||||||
@@ -50,6 +59,15 @@ impl App {
|
|||||||
self.resource_packs_state.drain_watcher();
|
self.resource_packs_state.drain_watcher();
|
||||||
self.resource_packs_state.request_image_loads(&self.picker);
|
self.resource_packs_state.request_image_loads(&self.picker);
|
||||||
self.resource_packs_state.drain_image_loads(&self.picker);
|
self.resource_packs_state.drain_image_loads(&self.picker);
|
||||||
|
if self.resource_packs_state.loaded_for.as_deref()
|
||||||
|
== self
|
||||||
|
.instances_state
|
||||||
|
.selected_instance()
|
||||||
|
.map(|instance| instance.name.as_str())
|
||||||
|
{
|
||||||
|
self.resource_packs_discovery_state
|
||||||
|
.refresh_installed_labels(&self.resource_packs_state.entries);
|
||||||
|
}
|
||||||
self.resource_packs_discovery_state.drain_pending();
|
self.resource_packs_discovery_state.drain_pending();
|
||||||
self.resource_packs_discovery_state.list.drain_pending();
|
self.resource_packs_discovery_state.list.drain_pending();
|
||||||
self.resource_packs_discovery_state
|
self.resource_packs_discovery_state
|
||||||
@@ -62,6 +80,15 @@ impl App {
|
|||||||
self.shaders_state.drain_watcher();
|
self.shaders_state.drain_watcher();
|
||||||
self.shaders_state.request_image_loads(&self.picker);
|
self.shaders_state.request_image_loads(&self.picker);
|
||||||
self.shaders_state.drain_image_loads(&self.picker);
|
self.shaders_state.drain_image_loads(&self.picker);
|
||||||
|
if self.shaders_state.loaded_for.as_deref()
|
||||||
|
== self
|
||||||
|
.instances_state
|
||||||
|
.selected_instance()
|
||||||
|
.map(|instance| instance.name.as_str())
|
||||||
|
{
|
||||||
|
self.shaders_discovery_state
|
||||||
|
.refresh_installed_labels(&self.shaders_state.entries);
|
||||||
|
}
|
||||||
self.shaders_discovery_state.drain_pending();
|
self.shaders_discovery_state.drain_pending();
|
||||||
self.shaders_discovery_state.list.drain_pending();
|
self.shaders_discovery_state.list.drain_pending();
|
||||||
self.shaders_discovery_state
|
self.shaders_discovery_state
|
||||||
|
|||||||
+193
-19
@@ -146,6 +146,29 @@ impl App {
|
|||||||
if self.focused == FocusedArea::Content
|
if self.focused == FocusedArea::Content
|
||||||
&& self.content_mode == widgets::content::ContentMode::Discover
|
&& self.content_mode == widgets::content::ContentMode::Discover
|
||||||
{
|
{
|
||||||
|
let (search_active, popup_open) = match self.content_tab {
|
||||||
|
widgets::content::ContentTab::Mods => (
|
||||||
|
self.mods_discovery_state.search.active,
|
||||||
|
self.mods_discovery_state.version_popup.is_some(),
|
||||||
|
),
|
||||||
|
widgets::content::ContentTab::ResourcePacks => (
|
||||||
|
self.resource_packs_discovery_state.search.active,
|
||||||
|
self.resource_packs_discovery_state.version_popup.is_some(),
|
||||||
|
),
|
||||||
|
widgets::content::ContentTab::Shaders => (
|
||||||
|
self.shaders_discovery_state.search.active,
|
||||||
|
self.shaders_discovery_state.version_popup.is_some(),
|
||||||
|
),
|
||||||
|
_ => (false, false),
|
||||||
|
};
|
||||||
|
if !search_active && !popup_open && key_event.code == KeyCode::Char('i') {
|
||||||
|
self.spawn_active_discovery_versions();
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
if popup_open && key_event.code == KeyCode::Enter {
|
||||||
|
self.spawn_active_discovery_install();
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
let handled = {
|
let handled = {
|
||||||
let state = match self.content_tab {
|
let state = match self.content_tab {
|
||||||
widgets::content::ContentTab::Mods => Some(&mut self.mods_discovery_state),
|
widgets::content::ContentTab::Mods => Some(&mut self.mods_discovery_state),
|
||||||
@@ -547,16 +570,158 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn active_discovery_state_mut(
|
||||||
|
&mut self,
|
||||||
|
) -> Option<&mut widgets::content::discovery::DiscoveryState> {
|
||||||
|
match self.content_tab {
|
||||||
|
widgets::content::ContentTab::Mods => Some(&mut self.mods_discovery_state),
|
||||||
|
widgets::content::ContentTab::ResourcePacks => {
|
||||||
|
Some(&mut self.resource_packs_discovery_state)
|
||||||
|
}
|
||||||
|
widgets::content::ContentTab::Shaders => Some(&mut self.shaders_discovery_state),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_active_discovery_versions(&mut self) {
|
||||||
|
let Some(instance) = self.instances_state.selected_instance().cloned() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some(state) = self.active_discovery_state_mut() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let kind = state.kind;
|
||||||
|
let Some(request) = state.begin_versions() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let client = crate::net::HttpClient::new();
|
||||||
|
let result = crate::net::modrinth::fetch_content_versions(
|
||||||
|
&client,
|
||||||
|
&request.project_id,
|
||||||
|
kind,
|
||||||
|
&instance.game_version,
|
||||||
|
instance.loader,
|
||||||
|
)
|
||||||
|
.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 spawn_active_discovery_install(&mut self) {
|
||||||
|
let Some(instance) = self.instances_state.selected_instance().cloned() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let kind = match self.content_tab {
|
||||||
|
widgets::content::ContentTab::Mods => crate::net::modrinth::DiscoveryKind::Mod,
|
||||||
|
widgets::content::ContentTab::ResourcePacks => {
|
||||||
|
crate::net::modrinth::DiscoveryKind::ResourcePack
|
||||||
|
}
|
||||||
|
widgets::content::ContentTab::Shaders => crate::net::modrinth::DiscoveryKind::Shader,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
let Some(request) = self
|
||||||
|
.active_discovery_state_mut()
|
||||||
|
.and_then(widgets::content::DiscoveryState::begin_install)
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let destination = self
|
||||||
|
.instance_manager
|
||||||
|
.instances_dir
|
||||||
|
.join(&instance.name)
|
||||||
|
.join(".minecraft")
|
||||||
|
.join(match kind {
|
||||||
|
crate::net::modrinth::DiscoveryKind::Mod => "mods",
|
||||||
|
crate::net::modrinth::DiscoveryKind::ResourcePack => "resourcepacks",
|
||||||
|
crate::net::modrinth::DiscoveryKind::Shader => "shaderpacks",
|
||||||
|
});
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let client = crate::net::HttpClient::new();
|
||||||
|
let result = async {
|
||||||
|
tokio::fs::create_dir_all(&destination)
|
||||||
|
.await
|
||||||
|
.map_err(crate::net::NetError::from)?;
|
||||||
|
let outcome = if let Some(installed_path) = request.installed_path.as_deref() {
|
||||||
|
crate::net::modrinth::download_version_file_for_update(
|
||||||
|
&client,
|
||||||
|
&request.version,
|
||||||
|
&destination,
|
||||||
|
installed_path,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
crate::net::modrinth::download_version_file(
|
||||||
|
&client,
|
||||||
|
&request.version,
|
||||||
|
&destination,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
let (path, skipped) = match outcome {
|
||||||
|
crate::net::modrinth::DownloadOutcome::Downloaded(path) => (path, false),
|
||||||
|
crate::net::modrinth::DownloadOutcome::SkippedExisting(path) => (path, true),
|
||||||
|
};
|
||||||
|
let replaced = request.installed_path.is_some() && !skipped;
|
||||||
|
if !skipped
|
||||||
|
&& let Some(old_path) = request
|
||||||
|
.installed_path
|
||||||
|
.as_ref()
|
||||||
|
.filter(|old_path| old_path.as_path() != path.as_path())
|
||||||
|
{
|
||||||
|
delete_content_path(old_path).map_err(crate::net::NetError::from)?;
|
||||||
|
}
|
||||||
|
Ok::<_, crate::net::NetError>(widgets::content::discovery::InstallCompletion {
|
||||||
|
path,
|
||||||
|
replaced,
|
||||||
|
skipped,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
.await
|
||||||
|
.map_err(|error| error.to_string());
|
||||||
|
widgets::content::DiscoveryState::push_action_result(
|
||||||
|
&request.pending,
|
||||||
|
widgets::content::discovery::DiscoveryActionResult::Install {
|
||||||
|
request_id: request.request_id,
|
||||||
|
project_id: request.project_id,
|
||||||
|
result,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
fn spawn_active_discovery_search(&mut self) {
|
fn spawn_active_discovery_search(&mut self) {
|
||||||
let Some(instance) = self.instances_state.selected_instance().cloned() else {
|
let Some(instance) = self.instances_state.selected_instance().cloned() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let installed_entries = match self.content_tab {
|
let installed_entries = match self.content_tab {
|
||||||
widgets::content::ContentTab::Mods => self.mods_state.entries.clone(),
|
widgets::content::ContentTab::Mods
|
||||||
widgets::content::ContentTab::ResourcePacks => {
|
if self.mods_state.loaded_for.as_deref() == Some(instance.name.as_str()) =>
|
||||||
|
{
|
||||||
|
self.mods_state.entries.clone()
|
||||||
|
}
|
||||||
|
widgets::content::ContentTab::ResourcePacks
|
||||||
|
if self.resource_packs_state.loaded_for.as_deref()
|
||||||
|
== Some(instance.name.as_str()) =>
|
||||||
|
{
|
||||||
self.resource_packs_state.entries.clone()
|
self.resource_packs_state.entries.clone()
|
||||||
}
|
}
|
||||||
widgets::content::ContentTab::Shaders => self.shaders_state.entries.clone(),
|
widgets::content::ContentTab::Shaders
|
||||||
|
if self.shaders_state.loaded_for.as_deref() == Some(instance.name.as_str()) =>
|
||||||
|
{
|
||||||
|
self.shaders_state.entries.clone()
|
||||||
|
}
|
||||||
|
widgets::content::ContentTab::Mods
|
||||||
|
| widgets::content::ContentTab::ResourcePacks
|
||||||
|
| widgets::content::ContentTab::Shaders => Vec::new(),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
let state = match self.content_tab {
|
let state = match self.content_tab {
|
||||||
@@ -576,11 +741,25 @@ impl App {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let installed_entries = match self.content_tab {
|
let installed_entries = match self.content_tab {
|
||||||
widgets::content::ContentTab::Mods => self.mods_state.entries.clone(),
|
widgets::content::ContentTab::Mods
|
||||||
widgets::content::ContentTab::ResourcePacks => {
|
if self.mods_state.loaded_for.as_deref() == Some(instance.name.as_str()) =>
|
||||||
|
{
|
||||||
|
self.mods_state.entries.clone()
|
||||||
|
}
|
||||||
|
widgets::content::ContentTab::ResourcePacks
|
||||||
|
if self.resource_packs_state.loaded_for.as_deref()
|
||||||
|
== Some(instance.name.as_str()) =>
|
||||||
|
{
|
||||||
self.resource_packs_state.entries.clone()
|
self.resource_packs_state.entries.clone()
|
||||||
}
|
}
|
||||||
widgets::content::ContentTab::Shaders => self.shaders_state.entries.clone(),
|
widgets::content::ContentTab::Shaders
|
||||||
|
if self.shaders_state.loaded_for.as_deref() == Some(instance.name.as_str()) =>
|
||||||
|
{
|
||||||
|
self.shaders_state.entries.clone()
|
||||||
|
}
|
||||||
|
widgets::content::ContentTab::Mods
|
||||||
|
| widgets::content::ContentTab::ResourcePacks
|
||||||
|
| widgets::content::ContentTab::Shaders => Vec::new(),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
let state = match self.content_tab {
|
let state = match self.content_tab {
|
||||||
@@ -633,23 +812,18 @@ impl App {
|
|||||||
let icon_slots = std::sync::Arc::new(tokio::sync::Semaphore::new(8));
|
let icon_slots = std::sync::Arc::new(tokio::sync::Semaphore::new(8));
|
||||||
for mut project in results.projects {
|
for mut project in results.projects {
|
||||||
returned_stems.insert(project.id.clone());
|
returned_stems.insert(project.id.clone());
|
||||||
let installed = widgets::content::discovery::project_is_installed(
|
let installed_path = widgets::content::discovery::installed_project_path(
|
||||||
&project,
|
&project,
|
||||||
&installed_entries,
|
&installed_entries,
|
||||||
);
|
);
|
||||||
let icon = (!loaded_icon_stems.contains(&project.id))
|
let icon_url = (!loaded_icon_stems.contains(&project.id))
|
||||||
.then(|| project.icon_url.take())
|
.then(|| project.icon_url.take())
|
||||||
.flatten()
|
.flatten();
|
||||||
.map(|url| {
|
let entry =
|
||||||
(
|
widgets::content::discovery::project_entry(project, installed_path);
|
||||||
url,
|
let icon =
|
||||||
project.id.clone(),
|
icon_url.map(|url| (url, entry.file_stem.clone(), entry.path.clone()));
|
||||||
std::path::PathBuf::from(&project.id),
|
if !stream.upsert(entry) {
|
||||||
)
|
|
||||||
});
|
|
||||||
if !stream.upsert(widgets::content::discovery::project_entry(
|
|
||||||
project, installed,
|
|
||||||
)) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if let Some((url, file_stem, path)) = icon {
|
if let Some((url, file_stem, path)) = icon {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crossterm::event::{KeyCode, KeyEvent};
|
|||||||
|
|
||||||
use crate::instance::content::mods::ContentEntry;
|
use crate::instance::content::mods::ContentEntry;
|
||||||
use crate::instance::{InstanceConfig, ModLoader};
|
use crate::instance::{InstanceConfig, ModLoader};
|
||||||
use crate::net::modrinth::{DiscoveryKind, DiscoveryProject};
|
use crate::net::modrinth::{DiscoveryKind, DiscoveryProject, VersionInfo};
|
||||||
|
|
||||||
use super::list::{ContentListState, ContentStream};
|
use super::list::{ContentListState, ContentStream};
|
||||||
|
|
||||||
@@ -66,6 +66,63 @@ pub struct DiscoveryRequest {
|
|||||||
|
|
||||||
pub(crate) type PendingDiscovery = Arc<Mutex<Vec<PendingDiscoveryResult>>>;
|
pub(crate) type PendingDiscovery = Arc<Mutex<Vec<PendingDiscoveryResult>>>;
|
||||||
|
|
||||||
|
pub struct VersionPopupState {
|
||||||
|
request_id: u64,
|
||||||
|
pub project_id: String,
|
||||||
|
pub project_title: String,
|
||||||
|
pub installed_path: Option<PathBuf>,
|
||||||
|
pub versions: Vec<VersionInfo>,
|
||||||
|
pub selected: usize,
|
||||||
|
pub loading: bool,
|
||||||
|
pub installing: bool,
|
||||||
|
pub error: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VersionPopupState {
|
||||||
|
pub fn title(&self) -> String {
|
||||||
|
if self.installed_path.is_some() {
|
||||||
|
format!("Change {} version", self.project_title)
|
||||||
|
} else {
|
||||||
|
format!("Install {}", self.project_title)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct VersionsRequest {
|
||||||
|
pub request_id: u64,
|
||||||
|
pub project_id: String,
|
||||||
|
pub pending: PendingActions,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct InstallRequest {
|
||||||
|
pub request_id: u64,
|
||||||
|
pub project_id: String,
|
||||||
|
pub version: VersionInfo,
|
||||||
|
pub installed_path: Option<PathBuf>,
|
||||||
|
pub pending: PendingActions,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct InstallCompletion {
|
||||||
|
pub path: PathBuf,
|
||||||
|
pub replaced: bool,
|
||||||
|
pub skipped: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum DiscoveryActionResult {
|
||||||
|
Versions {
|
||||||
|
request_id: u64,
|
||||||
|
project_id: String,
|
||||||
|
result: Result<Vec<VersionInfo>, String>,
|
||||||
|
},
|
||||||
|
Install {
|
||||||
|
request_id: u64,
|
||||||
|
project_id: String,
|
||||||
|
result: Result<InstallCompletion, String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) type PendingActions = Arc<Mutex<Vec<DiscoveryActionResult>>>;
|
||||||
|
|
||||||
pub struct DiscoveryState {
|
pub struct DiscoveryState {
|
||||||
pub kind: DiscoveryKind,
|
pub kind: DiscoveryKind,
|
||||||
pub list: ContentListState,
|
pub list: ContentListState,
|
||||||
@@ -75,6 +132,9 @@ pub struct DiscoveryState {
|
|||||||
context: Option<String>,
|
context: Option<String>,
|
||||||
generation: u64,
|
generation: u64,
|
||||||
pending: PendingDiscovery,
|
pending: PendingDiscovery,
|
||||||
|
pending_actions: PendingActions,
|
||||||
|
pub version_popup: Option<VersionPopupState>,
|
||||||
|
next_action_request_id: u64,
|
||||||
stream: Option<ContentStream>,
|
stream: Option<ContentStream>,
|
||||||
next_offset: usize,
|
next_offset: usize,
|
||||||
page_loading: bool,
|
page_loading: bool,
|
||||||
@@ -96,6 +156,9 @@ impl DiscoveryState {
|
|||||||
context: None,
|
context: None,
|
||||||
generation: 0,
|
generation: 0,
|
||||||
pending: Arc::new(Mutex::new(Vec::new())),
|
pending: Arc::new(Mutex::new(Vec::new())),
|
||||||
|
pending_actions: Arc::new(Mutex::new(Vec::new())),
|
||||||
|
version_popup: None,
|
||||||
|
next_action_request_id: 0,
|
||||||
stream: None,
|
stream: None,
|
||||||
next_offset: 0,
|
next_offset: 0,
|
||||||
page_loading: false,
|
page_loading: false,
|
||||||
@@ -113,6 +176,7 @@ impl DiscoveryState {
|
|||||||
|
|
||||||
pub fn begin_search(&mut self, instance: &InstanceConfig) -> DiscoveryRequest {
|
pub fn begin_search(&mut self, instance: &InstanceConfig) -> DiscoveryRequest {
|
||||||
self.generation = self.generation.wrapping_add(1);
|
self.generation = self.generation.wrapping_add(1);
|
||||||
|
self.version_popup = None;
|
||||||
let context = discovery_context(instance);
|
let context = discovery_context(instance);
|
||||||
let reconcile =
|
let reconcile =
|
||||||
self.context.as_deref() == Some(context.as_str()) && !self.list.entries.is_empty();
|
self.context.as_deref() == Some(context.as_str()) && !self.list.entries.is_empty();
|
||||||
@@ -180,6 +244,71 @@ impl DiscoveryState {
|
|||||||
self.viewport_rows = rows;
|
self.viewport_rows = rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn begin_versions(&mut self) -> Option<VersionsRequest> {
|
||||||
|
let filtered = self.list.filtered_indices();
|
||||||
|
let index = self
|
||||||
|
.list
|
||||||
|
.list_state
|
||||||
|
.selected
|
||||||
|
.and_then(|selected| filtered.get(selected))?;
|
||||||
|
let entry = self.list.entries.get(*index)?;
|
||||||
|
let installed_path = entry.installed_path.clone();
|
||||||
|
let project_id = entry.file_stem.clone();
|
||||||
|
self.next_action_request_id = self.next_action_request_id.wrapping_add(1);
|
||||||
|
let request_id = self.next_action_request_id;
|
||||||
|
self.version_popup = Some(VersionPopupState {
|
||||||
|
request_id,
|
||||||
|
project_id: project_id.clone(),
|
||||||
|
project_title: entry.name.clone(),
|
||||||
|
installed_path,
|
||||||
|
versions: Vec::new(),
|
||||||
|
selected: 0,
|
||||||
|
loading: true,
|
||||||
|
installing: false,
|
||||||
|
error: None,
|
||||||
|
});
|
||||||
|
Some(VersionsRequest {
|
||||||
|
request_id,
|
||||||
|
project_id,
|
||||||
|
pending: self.pending_actions.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn refresh_installed_labels(&mut self, installed_entries: &[ContentEntry]) {
|
||||||
|
let mut changed = false;
|
||||||
|
for entry in &mut self.list.entries {
|
||||||
|
let installed_path = entry
|
||||||
|
.source_slug
|
||||||
|
.as_deref()
|
||||||
|
.and_then(|slug| installed_path_for_identity(&entry.name, slug, installed_entries));
|
||||||
|
if entry.installed_path != installed_path {
|
||||||
|
entry.title_suffix = installed_path.is_some().then(|| "Installed".to_owned());
|
||||||
|
entry.installed_path = installed_path;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
crate::tui::request_redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn begin_install(&mut self) -> Option<InstallRequest> {
|
||||||
|
let popup = self.version_popup.as_mut()?;
|
||||||
|
if popup.loading || popup.installing {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let version = popup.versions.get(popup.selected)?.clone();
|
||||||
|
popup.installing = true;
|
||||||
|
popup.error = None;
|
||||||
|
Some(InstallRequest {
|
||||||
|
request_id: popup.request_id,
|
||||||
|
project_id: popup.project_id.clone(),
|
||||||
|
version,
|
||||||
|
installed_path: popup.installed_path.clone(),
|
||||||
|
pending: self.pending_actions.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn search_due(&self) -> bool {
|
pub fn search_due(&self) -> bool {
|
||||||
self.search_changed_at
|
self.search_changed_at
|
||||||
.is_some_and(|changed| changed.elapsed() >= SEARCH_DEBOUNCE)
|
.is_some_and(|changed| changed.elapsed() >= SEARCH_DEBOUNCE)
|
||||||
@@ -207,6 +336,13 @@ impl DiscoveryState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn push_action_result(pending: &PendingActions, result: DiscoveryActionResult) {
|
||||||
|
if let Ok(mut pending) = pending.lock() {
|
||||||
|
pending.push(result);
|
||||||
|
crate::tui::request_redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn drain_pending(&mut self) {
|
pub fn drain_pending(&mut self) {
|
||||||
let results = match self.pending.lock() {
|
let results = match self.pending.lock() {
|
||||||
Ok(mut pending) => std::mem::take(&mut *pending),
|
Ok(mut pending) => std::mem::take(&mut *pending),
|
||||||
@@ -256,6 +392,81 @@ impl DiscoveryState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.drain_action_results();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drain_action_results(&mut self) {
|
||||||
|
let results = match self.pending_actions.lock() {
|
||||||
|
Ok(mut pending) => std::mem::take(&mut *pending),
|
||||||
|
Err(_) => return,
|
||||||
|
};
|
||||||
|
for result in results {
|
||||||
|
match result {
|
||||||
|
DiscoveryActionResult::Versions {
|
||||||
|
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;
|
||||||
|
match result {
|
||||||
|
Ok(versions) => {
|
||||||
|
popup.versions = versions;
|
||||||
|
popup.selected = 0;
|
||||||
|
popup.error = None;
|
||||||
|
}
|
||||||
|
Err(error) => popup.error = Some(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DiscoveryActionResult::Install {
|
||||||
|
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.installing = false;
|
||||||
|
match result {
|
||||||
|
Ok(completion) => {
|
||||||
|
if 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;
|
||||||
|
}
|
||||||
|
Err(error) => popup.error = Some(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn empty_text(&self) -> &str {
|
pub fn empty_text(&self) -> &str {
|
||||||
@@ -287,6 +498,21 @@ impl DiscoveryState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool {
|
pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool {
|
||||||
|
if let Some(popup) = state.version_popup.as_mut() {
|
||||||
|
match key_event.code {
|
||||||
|
KeyCode::Esc if !popup.installing => state.version_popup = None,
|
||||||
|
KeyCode::Char('j') | KeyCode::Down if !popup.loading && !popup.installing => {
|
||||||
|
if popup.selected + 1 < popup.versions.len() {
|
||||||
|
popup.selected += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char('k') | KeyCode::Up if !popup.loading && !popup.installing => {
|
||||||
|
popup.selected = popup.selected.saturating_sub(1);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if state.search.active {
|
if state.search.active {
|
||||||
let previous_query = state.search.query.clone();
|
let previous_query = state.search.query.clone();
|
||||||
match key_event.code {
|
match key_event.code {
|
||||||
@@ -328,11 +554,16 @@ fn loader_slug(loader: ModLoader) -> Option<&'static str> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn project_entry(project: DiscoveryProject, installed: bool) -> ContentEntry {
|
pub(crate) fn project_entry(
|
||||||
|
project: DiscoveryProject,
|
||||||
|
installed_path: Option<PathBuf>,
|
||||||
|
) -> ContentEntry {
|
||||||
ContentEntry {
|
ContentEntry {
|
||||||
file_stem: project.id.clone(),
|
file_stem: project.id.clone(),
|
||||||
name: project.title,
|
name: project.title,
|
||||||
title_suffix: installed.then(|| "Installed".to_owned()),
|
source_slug: Some(project.slug),
|
||||||
|
installed_path: installed_path.clone(),
|
||||||
|
title_suffix: installed_path.is_some().then(|| "Installed".to_owned()),
|
||||||
footer_label: Some(format!("{} downloads", format_downloads(project.downloads))),
|
footer_label: Some(format!("{} downloads", format_downloads(project.downloads))),
|
||||||
description: project.description,
|
description: project.description,
|
||||||
enabled: true,
|
enabled: true,
|
||||||
@@ -342,15 +573,24 @@ pub(crate) fn project_entry(project: DiscoveryProject, installed: bool) -> Conte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn project_is_installed(
|
pub(crate) fn installed_project_path(
|
||||||
project: &DiscoveryProject,
|
project: &DiscoveryProject,
|
||||||
installed_entries: &[ContentEntry],
|
installed_entries: &[ContentEntry],
|
||||||
) -> bool {
|
) -> Option<PathBuf> {
|
||||||
installed_entries.iter().any(|entry| {
|
installed_path_for_identity(&project.title, &project.slug, installed_entries)
|
||||||
identity_matches(&entry.name, &project.title)
|
}
|
||||||
|| identity_matches(&entry.name, &project.slug)
|
|
||||||
|| filename_matches_project(&entry.file_stem, &project.slug)
|
fn installed_path_for_identity(
|
||||||
|| filename_matches_project(&entry.file_stem, &project.title)
|
title: &str,
|
||||||
|
slug: &str,
|
||||||
|
installed_entries: &[ContentEntry],
|
||||||
|
) -> Option<PathBuf> {
|
||||||
|
installed_entries.iter().find_map(|entry| {
|
||||||
|
let matches = identity_matches(&entry.name, title)
|
||||||
|
|| identity_matches(&entry.name, slug)
|
||||||
|
|| filename_matches_project(&entry.file_stem, slug)
|
||||||
|
|| filename_matches_project(&entry.file_stem, title);
|
||||||
|
matches.then(|| entry.path.clone())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,6 +646,17 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn version(id: &str) -> VersionInfo {
|
||||||
|
VersionInfo {
|
||||||
|
id: id.to_owned(),
|
||||||
|
name: format!("Version {id}"),
|
||||||
|
version_number: id.to_owned(),
|
||||||
|
game_versions: vec!["1.21.1".to_owned()],
|
||||||
|
loaders: vec!["fabric".to_owned()],
|
||||||
|
files: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn content_mode_toggles_both_ways() {
|
fn content_mode_toggles_both_ways() {
|
||||||
assert_eq!(ContentMode::Installed.toggle(), ContentMode::Discover);
|
assert_eq!(ContentMode::Installed.toggle(), ContentMode::Discover);
|
||||||
@@ -424,12 +675,14 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: None,
|
icon_bytes: None,
|
||||||
},
|
},
|
||||||
true,
|
Some(PathBuf::from("example.jar")),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(entry.title_suffix.as_deref(), Some("Installed"));
|
assert_eq!(entry.title_suffix.as_deref(), Some("Installed"));
|
||||||
assert_eq!(entry.footer_label.as_deref(), Some("1.2K downloads"));
|
assert_eq!(entry.footer_label.as_deref(), Some("1.2K downloads"));
|
||||||
assert_eq!(entry.description, "Project description");
|
assert_eq!(entry.description, "Project description");
|
||||||
|
assert_eq!(entry.path, PathBuf::from("example"));
|
||||||
|
assert_eq!(entry.installed_path, Some(PathBuf::from("example.jar")));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -443,20 +696,164 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: None,
|
icon_bytes: None,
|
||||||
};
|
};
|
||||||
let mut local = project_entry(project.clone(), false);
|
let mut local = project_entry(project.clone(), None);
|
||||||
local.name = "Fabric API".to_owned();
|
local.name = "Fabric API".to_owned();
|
||||||
local.file_stem = "unrelated-file".to_owned();
|
local.file_stem = "unrelated-file".to_owned();
|
||||||
assert!(project_is_installed(&project, &[local]));
|
assert!(installed_project_path(&project, &[local]).is_some());
|
||||||
|
|
||||||
let mut local = project_entry(project.clone(), false);
|
let mut local = project_entry(project.clone(), None);
|
||||||
local.name = "Unknown".to_owned();
|
local.name = "Unknown".to_owned();
|
||||||
local.file_stem = "fabric-api-0.116.0+1.21.1".to_owned();
|
local.file_stem = "fabric-api-0.116.0+1.21.1".to_owned();
|
||||||
assert!(project_is_installed(&project, &[local]));
|
assert!(installed_project_path(&project, &[local]).is_some());
|
||||||
|
|
||||||
let mut local = project_entry(project.clone(), false);
|
let mut local = project_entry(project.clone(), None);
|
||||||
local.name = "Fabric Language Kotlin".to_owned();
|
local.name = "Fabric Language Kotlin".to_owned();
|
||||||
local.file_stem = "fabric-language-kotlin".to_owned();
|
local.file_stem = "fabric-language-kotlin".to_owned();
|
||||||
assert!(!project_is_installed(&project, &[local]));
|
assert!(installed_project_path(&project, &[local]).is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn install_and_change_version_popups_only_differ_in_title() {
|
||||||
|
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.clone(), None));
|
||||||
|
state.list.list_state.selected = Some(0);
|
||||||
|
state.begin_versions().unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
state.version_popup.as_ref().unwrap().title(),
|
||||||
|
"Install Project"
|
||||||
|
);
|
||||||
|
|
||||||
|
state.version_popup = None;
|
||||||
|
state.list.entries[0] = project_entry(project, Some(PathBuf::from("mods/project.jar")));
|
||||||
|
state.begin_versions().unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
state.version_popup.as_ref().unwrap().title(),
|
||||||
|
"Change Project version"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compatible_versions_populate_the_open_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 request = state.begin_versions().unwrap();
|
||||||
|
DiscoveryState::push_action_result(
|
||||||
|
&request.pending,
|
||||||
|
DiscoveryActionResult::Versions {
|
||||||
|
request_id: request.request_id,
|
||||||
|
project_id: request.project_id,
|
||||||
|
result: Ok(vec![version("1.0.0"), version("1.1.0")]),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
state.drain_pending();
|
||||||
|
|
||||||
|
let popup = state.version_popup.as_ref().unwrap();
|
||||||
|
assert!(!popup.loading);
|
||||||
|
assert_eq!(popup.versions.len(), 2);
|
||||||
|
assert_eq!(popup.selected, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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 request = state.begin_versions().unwrap();
|
||||||
|
DiscoveryState::push_action_result(
|
||||||
|
&request.pending,
|
||||||
|
DiscoveryActionResult::Install {
|
||||||
|
request_id: request.request_id,
|
||||||
|
project_id: request.project_id,
|
||||||
|
result: Ok(InstallCompletion {
|
||||||
|
path: PathBuf::from("mods/project.jar"),
|
||||||
|
replaced: false,
|
||||||
|
skipped: false,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
state.drain_pending();
|
||||||
|
|
||||||
|
assert!(state.version_popup.is_none());
|
||||||
|
assert_eq!(
|
||||||
|
state.list.entries[0].title_suffix.as_deref(),
|
||||||
|
Some("Installed")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
state.list.entries[0].installed_path,
|
||||||
|
Some(PathBuf::from("mods/project.jar"))
|
||||||
|
);
|
||||||
|
assert_eq!(state.list.entries[0].path, PathBuf::from("project"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn installed_labels_follow_the_current_instance_entries() {
|
||||||
|
let project = DiscoveryProject {
|
||||||
|
id: "project-id".to_owned(),
|
||||||
|
slug: "example-project".to_owned(),
|
||||||
|
title: "Example 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));
|
||||||
|
|
||||||
|
let mut installed = state.list.entries[0].clone();
|
||||||
|
installed.name = "Example Project".to_owned();
|
||||||
|
installed.file_stem = "example-project-1.0.0".to_owned();
|
||||||
|
installed.path = PathBuf::from("first/mods/example-project-1.0.0.jar");
|
||||||
|
installed.source_slug = None;
|
||||||
|
installed.installed_path = None;
|
||||||
|
installed.title_suffix = None;
|
||||||
|
|
||||||
|
state.refresh_installed_labels(&[installed]);
|
||||||
|
assert_eq!(
|
||||||
|
state.list.entries[0].title_suffix.as_deref(),
|
||||||
|
Some("Installed")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
state.list.entries[0].installed_path,
|
||||||
|
Some(PathBuf::from("first/mods/example-project-1.0.0.jar"))
|
||||||
|
);
|
||||||
|
|
||||||
|
state.refresh_installed_labels(&[]);
|
||||||
|
assert_eq!(state.list.entries[0].title_suffix, None);
|
||||||
|
assert_eq!(state.list.entries[0].installed_path, None);
|
||||||
|
assert_eq!(state.list.entries[0].path, PathBuf::from("project-id"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -523,7 +920,7 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: None,
|
icon_bytes: None,
|
||||||
},
|
},
|
||||||
false
|
None
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
state.list.drain_pending();
|
state.list.drain_pending();
|
||||||
@@ -560,7 +957,7 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: None,
|
icon_bytes: None,
|
||||||
},
|
},
|
||||||
false
|
None
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
state.list.drain_pending();
|
state.list.drain_pending();
|
||||||
@@ -593,7 +990,7 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: None,
|
icon_bytes: None,
|
||||||
},
|
},
|
||||||
false
|
None
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
state.list.drain_pending();
|
state.list.drain_pending();
|
||||||
@@ -632,7 +1029,7 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: (title == "Sodium").then(|| vec![1, 2, 3]),
|
icon_bytes: (title == "Sodium").then(|| vec![1, 2, 3]),
|
||||||
},
|
},
|
||||||
false
|
None
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
state.list.drain_pending();
|
state.list.drain_pending();
|
||||||
@@ -664,7 +1061,7 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: None,
|
icon_bytes: None,
|
||||||
},
|
},
|
||||||
false
|
None
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
state.list.drain_pending();
|
state.list.drain_pending();
|
||||||
@@ -692,7 +1089,7 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: None,
|
icon_bytes: None,
|
||||||
},
|
},
|
||||||
false
|
None
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
state.list.drain_pending();
|
state.list.drain_pending();
|
||||||
@@ -727,7 +1124,7 @@ mod tests {
|
|||||||
icon_url: None,
|
icon_url: None,
|
||||||
icon_bytes: None,
|
icon_bytes: None,
|
||||||
},
|
},
|
||||||
false
|
None
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
state.list.drain_pending();
|
state.list.drain_pending();
|
||||||
|
|||||||
@@ -1180,10 +1180,13 @@ pub fn render(
|
|||||||
row.extend(search.highlight_spans(&visible_description, description_style));
|
row.extend(search.highlight_spans(&visible_description, description_style));
|
||||||
}
|
}
|
||||||
if let Some(footer_label) = footer_label {
|
if let Some(footer_label) = footer_label {
|
||||||
if has_description {
|
row.extend(right_aligned_footer_spans(
|
||||||
row.push(Span::styled(" • ", description_style));
|
description_width,
|
||||||
}
|
&visible_description,
|
||||||
row.push(Span::styled(footer_label.to_owned(), footer_label_style));
|
has_description,
|
||||||
|
footer_label,
|
||||||
|
footer_label_style,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
lines.push(Line::from(row));
|
lines.push(Line::from(row));
|
||||||
}
|
}
|
||||||
@@ -1225,10 +1228,13 @@ pub fn render(
|
|||||||
.extend(search.highlight_spans(&visible_description, description_style));
|
.extend(search.highlight_spans(&visible_description, description_style));
|
||||||
}
|
}
|
||||||
if let Some(footer_label) = footer_label {
|
if let Some(footer_label) = footer_label {
|
||||||
if has_description {
|
description.extend(right_aligned_footer_spans(
|
||||||
description.push(Span::styled(" • ", description_style));
|
description_width,
|
||||||
}
|
&visible_description,
|
||||||
description.push(Span::styled(footer_label.to_owned(), footer_label_style));
|
has_description,
|
||||||
|
footer_label,
|
||||||
|
footer_label_style,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
lines.push(Line::from(description));
|
lines.push(Line::from(description));
|
||||||
}
|
}
|
||||||
@@ -1515,11 +1521,29 @@ fn description_text_width(
|
|||||||
has_description: bool,
|
has_description: bool,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let footer_width = footer_label.map_or(0, |footer_label| {
|
let footer_width = footer_label.map_or(0, |footer_label| {
|
||||||
Span::raw(footer_label).width() + if has_description { 3 } else { 0 }
|
Span::raw(footer_label).width() + usize::from(has_description)
|
||||||
});
|
});
|
||||||
available_width.saturating_sub(footer_width)
|
available_width.saturating_sub(footer_width)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn right_aligned_footer_spans(
|
||||||
|
available_width: usize,
|
||||||
|
description: &str,
|
||||||
|
has_description: bool,
|
||||||
|
footer_label: &str,
|
||||||
|
footer_style: Style,
|
||||||
|
) -> Vec<Span<'static>> {
|
||||||
|
let description_width = has_description
|
||||||
|
.then(|| Span::raw(description).width())
|
||||||
|
.unwrap_or(0);
|
||||||
|
let footer_width = Span::raw(footer_label).width();
|
||||||
|
let padding = available_width.saturating_sub(description_width + footer_width);
|
||||||
|
vec![
|
||||||
|
Span::raw(" ".repeat(padding)),
|
||||||
|
Span::styled(footer_label.to_owned(), footer_style),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
fn ellipsize(text: &str, max_width: usize) -> String {
|
fn ellipsize(text: &str, max_width: usize) -> String {
|
||||||
if Span::raw(text).width() <= max_width {
|
if Span::raw(text).width() <= max_width {
|
||||||
return text.to_owned();
|
return text.to_owned();
|
||||||
@@ -1603,19 +1627,21 @@ mod tests {
|
|||||||
buffer::Buffer,
|
buffer::Buffer,
|
||||||
layout::Rect,
|
layout::Rect,
|
||||||
style::{Color, Modifier, Style},
|
style::{Color, Modifier, Style},
|
||||||
text::{Line, Text},
|
text::{Line, Span, Text},
|
||||||
widgets::Widget,
|
widgets::Widget,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
ContentListState, available_description_width, description_text_width, ellipsize,
|
ContentListState, available_description_width, description_text_width, ellipsize,
|
||||||
square_icon_columns, title_suffix_spans,
|
right_aligned_footer_spans, square_icon_columns, title_suffix_spans,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn entry(name: &str) -> ContentEntry {
|
fn entry(name: &str) -> ContentEntry {
|
||||||
ContentEntry {
|
ContentEntry {
|
||||||
file_stem: name.to_lowercase(),
|
file_stem: name.to_lowercase(),
|
||||||
name: name.to_owned(),
|
name: name.to_owned(),
|
||||||
|
source_slug: None,
|
||||||
|
installed_path: None,
|
||||||
title_suffix: None,
|
title_suffix: None,
|
||||||
footer_label: None,
|
footer_label: None,
|
||||||
description: String::new(),
|
description: String::new(),
|
||||||
@@ -1697,11 +1723,27 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn description_width_reserves_the_download_metadata() {
|
fn description_width_reserves_the_download_metadata() {
|
||||||
assert_eq!(description_text_width(40, Some("1.2K downloads"), true), 23);
|
assert_eq!(description_text_width(40, Some("1.2K downloads"), true), 25);
|
||||||
assert_eq!(description_text_width(10, Some("1.2K downloads"), true), 0);
|
assert_eq!(description_text_width(10, Some("1.2K downloads"), true), 0);
|
||||||
assert_eq!(description_text_width(40, None, true), 40);
|
assert_eq!(description_text_width(40, None, true), 40);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn footer_metadata_is_right_aligned_without_a_separator() {
|
||||||
|
let mut spans = vec![Span::raw("Description")];
|
||||||
|
spans.extend(right_aligned_footer_spans(
|
||||||
|
30,
|
||||||
|
"Description",
|
||||||
|
true,
|
||||||
|
"1.2K downloads",
|
||||||
|
Style::default(),
|
||||||
|
));
|
||||||
|
let line = Line::from(spans);
|
||||||
|
|
||||||
|
assert_eq!(line.width(), 30);
|
||||||
|
assert_eq!(line.to_string(), "Description 1.2K downloads");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn content_stream_inserts_entries_and_icons_incrementally() {
|
fn content_stream_inserts_entries_and_icons_incrementally() {
|
||||||
let mut state = ContentListState::default();
|
let mut state = ContentListState::default();
|
||||||
|
|||||||
+133
-40
@@ -7,7 +7,7 @@ use ratatui::{
|
|||||||
layout::{Alignment, Constraint, Layout, Rect},
|
layout::{Alignment, Constraint, Layout, Rect},
|
||||||
style::{Modifier, Style},
|
style::{Modifier, Style},
|
||||||
text::{Line, Span},
|
text::{Line, Span},
|
||||||
widgets::{Block, Borders, Paragraph},
|
widgets::{Block, Borders, ListItem, Paragraph, Widget, Wrap},
|
||||||
};
|
};
|
||||||
use throbber_widgets_tui::{Throbber, ThrobberState};
|
use throbber_widgets_tui::{Throbber, ThrobberState};
|
||||||
|
|
||||||
@@ -217,6 +217,7 @@ pub fn render(
|
|||||||
Some(match (mode, tab) {
|
Some(match (mode, tab) {
|
||||||
(ContentMode::Discover, _) => &[
|
(ContentMode::Discover, _) => &[
|
||||||
("j/k", " navigate"),
|
("j/k", " navigate"),
|
||||||
|
("i", " versions"),
|
||||||
("h/l", " tabs"),
|
("h/l", " tabs"),
|
||||||
("/", " search"),
|
("/", " search"),
|
||||||
("Tab", " installed"),
|
("Tab", " installed"),
|
||||||
@@ -302,6 +303,19 @@ pub fn render(
|
|||||||
match tab {
|
match tab {
|
||||||
ContentTab::Mods => {
|
ContentTab::Mods => {
|
||||||
if let Some(instance) = instance {
|
if let Some(instance) = instance {
|
||||||
|
if mods_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||||
|
let content_dir = instances_dir
|
||||||
|
.join(&instance.name)
|
||||||
|
.join(".minecraft")
|
||||||
|
.join("mods");
|
||||||
|
mods_state.start_load(
|
||||||
|
&content_dir,
|
||||||
|
&instance.name,
|
||||||
|
crate::instance::scan_one_mod,
|
||||||
|
".jar",
|
||||||
|
);
|
||||||
|
mods_state.watch_dir(content_dir);
|
||||||
|
}
|
||||||
if mode == ContentMode::Discover {
|
if mode == ContentMode::Discover {
|
||||||
render_discovery(
|
render_discovery(
|
||||||
frame,
|
frame,
|
||||||
@@ -312,19 +326,6 @@ pub fn render(
|
|||||||
picker,
|
picker,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
if mods_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
|
||||||
let content_dir = instances_dir
|
|
||||||
.join(&instance.name)
|
|
||||||
.join(".minecraft")
|
|
||||||
.join("mods");
|
|
||||||
mods_state.start_load(
|
|
||||||
&content_dir,
|
|
||||||
&instance.name,
|
|
||||||
crate::instance::scan_one_mod,
|
|
||||||
".jar",
|
|
||||||
);
|
|
||||||
mods_state.watch_dir(content_dir);
|
|
||||||
}
|
|
||||||
super::list::render(
|
super::list::render(
|
||||||
frame,
|
frame,
|
||||||
content_area,
|
content_area,
|
||||||
@@ -345,6 +346,19 @@ pub fn render(
|
|||||||
}
|
}
|
||||||
ContentTab::ResourcePacks => {
|
ContentTab::ResourcePacks => {
|
||||||
if let Some(instance) = instance {
|
if let Some(instance) = instance {
|
||||||
|
if resource_packs_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||||
|
let content_dir = instances_dir
|
||||||
|
.join(&instance.name)
|
||||||
|
.join(".minecraft")
|
||||||
|
.join("resourcepacks");
|
||||||
|
resource_packs_state.start_load(
|
||||||
|
&content_dir,
|
||||||
|
&instance.name,
|
||||||
|
crate::instance::scan_one_resource_pack,
|
||||||
|
".zip",
|
||||||
|
);
|
||||||
|
resource_packs_state.watch_dir(content_dir);
|
||||||
|
}
|
||||||
if mode == ContentMode::Discover {
|
if mode == ContentMode::Discover {
|
||||||
render_discovery(
|
render_discovery(
|
||||||
frame,
|
frame,
|
||||||
@@ -355,19 +369,6 @@ pub fn render(
|
|||||||
picker,
|
picker,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
if resource_packs_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
|
||||||
let content_dir = instances_dir
|
|
||||||
.join(&instance.name)
|
|
||||||
.join(".minecraft")
|
|
||||||
.join("resourcepacks");
|
|
||||||
resource_packs_state.start_load(
|
|
||||||
&content_dir,
|
|
||||||
&instance.name,
|
|
||||||
crate::instance::scan_one_resource_pack,
|
|
||||||
".zip",
|
|
||||||
);
|
|
||||||
resource_packs_state.watch_dir(content_dir);
|
|
||||||
}
|
|
||||||
super::list::render(
|
super::list::render(
|
||||||
frame,
|
frame,
|
||||||
content_area,
|
content_area,
|
||||||
@@ -388,6 +389,19 @@ pub fn render(
|
|||||||
}
|
}
|
||||||
ContentTab::Shaders => {
|
ContentTab::Shaders => {
|
||||||
if let Some(instance) = instance {
|
if let Some(instance) = instance {
|
||||||
|
if shaders_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||||
|
let content_dir = instances_dir
|
||||||
|
.join(&instance.name)
|
||||||
|
.join(".minecraft")
|
||||||
|
.join("shaderpacks");
|
||||||
|
shaders_state.start_load(
|
||||||
|
&content_dir,
|
||||||
|
&instance.name,
|
||||||
|
crate::instance::scan_one_shader,
|
||||||
|
".zip",
|
||||||
|
);
|
||||||
|
shaders_state.watch_dir(content_dir);
|
||||||
|
}
|
||||||
if mode == ContentMode::Discover {
|
if mode == ContentMode::Discover {
|
||||||
render_discovery(
|
render_discovery(
|
||||||
frame,
|
frame,
|
||||||
@@ -398,19 +412,6 @@ pub fn render(
|
|||||||
picker,
|
picker,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
if shaders_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
|
||||||
let content_dir = instances_dir
|
|
||||||
.join(&instance.name)
|
|
||||||
.join(".minecraft")
|
|
||||||
.join("shaderpacks");
|
|
||||||
shaders_state.start_load(
|
|
||||||
&content_dir,
|
|
||||||
&instance.name,
|
|
||||||
crate::instance::scan_one_shader,
|
|
||||||
".zip",
|
|
||||||
);
|
|
||||||
shaders_state.watch_dir(content_dir);
|
|
||||||
}
|
|
||||||
super::list::render(
|
super::list::render(
|
||||||
frame,
|
frame,
|
||||||
content_area,
|
content_area,
|
||||||
@@ -521,6 +522,84 @@ fn render_discovery(
|
|||||||
&empty_text,
|
&empty_text,
|
||||||
picker,
|
picker,
|
||||||
);
|
);
|
||||||
|
if state.version_popup.is_some() {
|
||||||
|
render_version_popup(frame, area, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 theme = THEME.as_ref();
|
||||||
|
let title = popup.title();
|
||||||
|
let loading = popup.loading;
|
||||||
|
let installing = popup.installing;
|
||||||
|
let error = popup.error.clone();
|
||||||
|
let selected = popup.selected;
|
||||||
|
let items = popup
|
||||||
|
.versions
|
||||||
|
.iter()
|
||||||
|
.map(|version| {
|
||||||
|
ListItem::new(discovery_version_label(version)).style(Style::default().fg(theme.text()))
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
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(&[
|
||||||
|
("j/k", " navigate"),
|
||||||
|
("Enter", " install"),
|
||||||
|
("Esc", " close"),
|
||||||
|
])),
|
||||||
|
search_line: None,
|
||||||
|
content: Box::new(move |area, buffer| {
|
||||||
|
if installing {
|
||||||
|
Paragraph::new("Installing...")
|
||||||
|
.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);
|
||||||
|
} 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 items.is_empty() {
|
||||||
|
Paragraph::new("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(),
|
||||||
|
selected,
|
||||||
|
area,
|
||||||
|
buffer,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
frame.render_widget(popup, popup_area);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn discovery_version_label(version: &crate::net::modrinth::VersionInfo) -> String {
|
||||||
|
version.version_number.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
// the header bar above the content tabs, showing instance name, loader info,
|
// the header bar above the content tabs, showing instance name, loader info,
|
||||||
@@ -659,4 +738,18 @@ mod tests {
|
|||||||
ContentTab::ResourcePacks
|
ContentTab::ResourcePacks
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn discovery_version_rows_only_show_the_version_number() {
|
||||||
|
let version = crate::net::modrinth::VersionInfo {
|
||||||
|
id: "version-id".to_owned(),
|
||||||
|
name: "A descriptive release title".to_owned(),
|
||||||
|
version_number: "3.2.4-fabric-26.1".to_owned(),
|
||||||
|
game_versions: vec![],
|
||||||
|
loaders: vec![],
|
||||||
|
files: vec![],
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(discovery_version_label(&version), "3.2.4-fabric-26.1");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
mod render;
|
mod render;
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
|
pub(crate) use render::render_select_list;
|
||||||
pub use render::{popup_rect, render};
|
pub use render::{popup_rect, render};
|
||||||
pub use state::{LoadState, WizardParams, WizardState, WizardStep, handle_key, take_result};
|
pub use state::{LoadState, WizardParams, WizardState, WizardStep, handle_key, take_result};
|
||||||
@@ -193,20 +193,28 @@ fn render_version_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffe
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let list = List::new(items)
|
render_select_list(items, state.version_idx, area, buf);
|
||||||
.highlight_style(
|
|
||||||
Style::default()
|
|
||||||
.fg(theme.accent())
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
)
|
|
||||||
.highlight_symbol("▶ ");
|
|
||||||
|
|
||||||
let mut list_state = ListState::default().with_selected(Some(state.version_idx));
|
|
||||||
StatefulWidget::render(list, area, buf, &mut list_state);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn render_select_list(
|
||||||
|
items: Vec<ListItem<'_>>,
|
||||||
|
selected: usize,
|
||||||
|
area: Rect,
|
||||||
|
buffer: &mut ratatui::buffer::Buffer,
|
||||||
|
) {
|
||||||
|
let list = List::new(items)
|
||||||
|
.highlight_style(
|
||||||
|
Style::default()
|
||||||
|
.fg(THEME.as_ref().accent())
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
)
|
||||||
|
.highlight_symbol("▶ ");
|
||||||
|
let mut state = ListState::default().with_selected(Some(selected));
|
||||||
|
StatefulWidget::render(list, area, buffer, &mut state);
|
||||||
|
}
|
||||||
|
|
||||||
fn render_loader_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffer::Buffer) {
|
fn render_loader_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffer::Buffer) {
|
||||||
let theme = THEME.as_ref();
|
let theme = THEME.as_ref();
|
||||||
let loaders = [
|
let loaders = [
|
||||||
|
|||||||
Reference in new issue
Block a user