refactor: clarify content and tui ownership
This commit is contained in:
1 parent
078afe4b9d
commit
15d76c18a0
32 files changed
+893
-1144
No files matched your search
@@ -3,9 +3,8 @@ use std::sync::{Arc, Mutex};
|
||||
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
|
||||
use crate::instance::content::mods::ContentEntry;
|
||||
use crate::instance::{InstanceConfig, ModLoader};
|
||||
use crate::net::modrinth::{DiscoveryKind, DiscoveryProject, VersionInfo};
|
||||
use crate::instance::{ContentEntry, ContentKind, InstanceConfig, ModLoader};
|
||||
use crate::net::modrinth::{DiscoveryProject, VersionInfo};
|
||||
|
||||
use super::list::{ContentListState, ContentStream};
|
||||
|
||||
@@ -159,7 +158,7 @@ pub enum DiscoveryActionResult {
|
||||
pub(crate) type PendingActions = Arc<Mutex<Vec<DiscoveryActionResult>>>;
|
||||
|
||||
pub struct DiscoveryState {
|
||||
pub kind: DiscoveryKind,
|
||||
pub kind: ContentKind,
|
||||
pub list: ContentListState,
|
||||
pub search: crate::tui::widgets::search::SearchState,
|
||||
pub total_hits: usize,
|
||||
@@ -184,7 +183,7 @@ pub struct DiscoveryState {
|
||||
}
|
||||
|
||||
impl DiscoveryState {
|
||||
pub fn new(kind: DiscoveryKind) -> Self {
|
||||
pub fn new(kind: ContentKind) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
list: ContentListState::default(),
|
||||
@@ -859,7 +858,7 @@ pub(crate) fn project_entry(
|
||||
provider_icon,
|
||||
provider_description: false,
|
||||
path: PathBuf::from(project.id),
|
||||
icon_lines: Some(crate::instance::content::mods::fallback_icon()),
|
||||
icon_lines: Some(crate::instance::content::fallback_icon()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ use ratatui_image::{CropOptions, Resize, StatefulImage, protocol::StatefulProtoc
|
||||
use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView};
|
||||
|
||||
use crate::config::theme::THEME;
|
||||
use crate::instance::content::mods::{ContentEntry, IconCell};
|
||||
use crate::instance::content::{ContentEntry, IconCell};
|
||||
|
||||
type ScanOneFn = fn(&Path, &str, bool) -> ContentEntry;
|
||||
static PROVIDER_ICON_SLOTS: LazyLock<Arc<tokio::sync::Semaphore>> =
|
||||
@@ -249,7 +249,7 @@ impl ContentListState {
|
||||
if entry.provider_project.take().is_some() {
|
||||
if entry.provider_icon {
|
||||
entry.icon_bytes = None;
|
||||
entry.icon_lines = Some(crate::instance::content::mods::fallback_icon());
|
||||
entry.icon_lines = Some(crate::instance::content::fallback_icon());
|
||||
entry.provider_icon = false;
|
||||
invalidated_icons.push(entry.file_stem.clone());
|
||||
}
|
||||
@@ -268,7 +268,7 @@ impl ContentListState {
|
||||
if entry.provider_project != project {
|
||||
if entry.provider_icon {
|
||||
entry.icon_bytes = None;
|
||||
entry.icon_lines = Some(crate::instance::content::mods::fallback_icon());
|
||||
entry.icon_lines = Some(crate::instance::content::fallback_icon());
|
||||
entry.provider_icon = false;
|
||||
invalidated_icons.push(entry.file_stem.clone());
|
||||
}
|
||||
@@ -524,18 +524,18 @@ impl ContentListState {
|
||||
return PendingContentImage {
|
||||
file_stem,
|
||||
path,
|
||||
icon_lines: crate::instance::content::mods::fallback_icon(),
|
||||
icon_lines: crate::instance::content::fallback_icon(),
|
||||
image: None,
|
||||
};
|
||||
};
|
||||
let icon_lines = if use_quadrants {
|
||||
crate::instance::content::mods::make_icon_quadrants_from_image(
|
||||
crate::instance::content::make_icon_quadrants_from_image(
|
||||
&image,
|
||||
columns,
|
||||
rows as u16,
|
||||
)
|
||||
} else {
|
||||
crate::instance::content::mods::make_icon_pixels_from_image(
|
||||
crate::instance::content::make_icon_pixels_from_image(
|
||||
&image,
|
||||
columns,
|
||||
rows as u16,
|
||||
@@ -1161,37 +1161,18 @@ impl ContentListState {
|
||||
let Some(entry) = self.entries.get(index) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let new_path = if entry.enabled {
|
||||
let fname = match entry.path.file_name().and_then(|n| n.to_str()) {
|
||||
Some(n) => n,
|
||||
None => return,
|
||||
};
|
||||
let mut p = entry.path.clone();
|
||||
p.set_file_name(format!("{fname}.disabled"));
|
||||
p
|
||||
} else {
|
||||
let fname = match entry.path.file_name().and_then(|n| n.to_str()) {
|
||||
Some(n) => n,
|
||||
None => return,
|
||||
};
|
||||
let mut p = entry.path.clone();
|
||||
p.set_file_name(fname.trim_end_matches(".disabled"));
|
||||
p
|
||||
};
|
||||
|
||||
match std::fs::rename(&entry.path, &new_path) {
|
||||
Ok(()) => {
|
||||
match crate::instance::content::toggle_entry_path(entry) {
|
||||
Ok(Some(new_path)) => {
|
||||
let entry = &mut self.entries[index];
|
||||
entry.enabled = !entry.enabled;
|
||||
entry.path = new_path;
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(e) => {
|
||||
tracing::error!(
|
||||
"Failed to toggle '{}' from {} to {}: {}",
|
||||
"Failed to toggle '{}' at {}: {}",
|
||||
entry.file_stem,
|
||||
entry.path.display(),
|
||||
new_path.display(),
|
||||
e
|
||||
);
|
||||
}
|
||||
@@ -1329,42 +1310,18 @@ async fn load_provider_metadata(
|
||||
}
|
||||
|
||||
pub fn handle_key_no_toggle(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
|
||||
if handle_search_keys(key_event, state) {
|
||||
return true;
|
||||
}
|
||||
let filtered = state.filtered_indices();
|
||||
let count = filtered.len();
|
||||
|
||||
match key_event.code {
|
||||
KeyCode::Char('j') | KeyCode::Down => {
|
||||
if count == 0 {
|
||||
return true;
|
||||
}
|
||||
let current = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some((current + 1).min(count - 1));
|
||||
state.update_scrollbar();
|
||||
true
|
||||
}
|
||||
KeyCode::Char('k') | KeyCode::Up => {
|
||||
let current = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some(current.saturating_sub(1));
|
||||
state.update_scrollbar();
|
||||
true
|
||||
}
|
||||
KeyCode::Enter if key_event.modifiers.contains(KeyModifiers::SHIFT) => {
|
||||
if let Some(&real_idx) = state.list_state.selected.and_then(|i| filtered.get(i))
|
||||
&& let Some(dir) = state.entries[real_idx].path.parent()
|
||||
&& let Err(e) = open::that_detached(dir)
|
||||
{
|
||||
tracing::error!("Failed to open directory: {}", e);
|
||||
}
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
handle_key_inner(key_event, state, false)
|
||||
}
|
||||
|
||||
pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
|
||||
handle_key_inner(key_event, state, true)
|
||||
}
|
||||
|
||||
fn handle_key_inner(
|
||||
key_event: &KeyEvent,
|
||||
state: &mut ContentListState,
|
||||
toggle_on_enter: bool,
|
||||
) -> bool {
|
||||
if handle_search_keys(key_event, state) {
|
||||
return true;
|
||||
}
|
||||
@@ -1396,7 +1353,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
|
||||
}
|
||||
true
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
KeyCode::Enter if toggle_on_enter => {
|
||||
if let Some(&real_idx) = state.list_state.selected.and_then(|i| filtered.get(i)) {
|
||||
state.list_state.selected = Some(real_idx);
|
||||
state.toggle_selected();
|
||||
@@ -1998,7 +1955,7 @@ fn ellipsize(text: &str, max_width: usize) -> String {
|
||||
}
|
||||
|
||||
fn protocol_icon_columns(
|
||||
entry: &crate::instance::content::mods::ContentEntry,
|
||||
entry: &crate::instance::ContentEntry,
|
||||
picker: &ratatui_image::picker::Picker,
|
||||
) -> u16 {
|
||||
let rows = entry.icon_lines.as_ref().map_or(3, Vec::len) as u16;
|
||||
@@ -2124,7 +2081,7 @@ fn diff_event_paths(
|
||||
fn watcher_diff(
|
||||
toggled: Vec<(String, bool, std::path::PathBuf)>,
|
||||
removed: Vec<String>,
|
||||
added: Vec<crate::instance::content::mods::ContentEntry>,
|
||||
added: Vec<crate::instance::ContentEntry>,
|
||||
) -> Option<WatcherDiff> {
|
||||
if toggled.is_empty() && removed.is_empty() && added.is_empty() {
|
||||
None
|
||||
|
||||
+113
-132
@@ -17,6 +17,17 @@ use crate::tui::widgets::content::{ContentMode, DiscoveryState};
|
||||
|
||||
use crate::tui::widgets::styled_title;
|
||||
|
||||
type ContentScanner = fn(&std::path::Path, &str, bool) -> crate::instance::ContentEntry;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct DownloadableTab {
|
||||
directory: &'static str,
|
||||
extension: &'static str,
|
||||
scanner: ContentScanner,
|
||||
loading_text: &'static str,
|
||||
empty_text: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ContentTab {
|
||||
#[default]
|
||||
@@ -79,6 +90,33 @@ impl ContentTab {
|
||||
pub fn previous_for_mode(self, mode: ContentMode) -> Self {
|
||||
cycle_tab(self, visible_tabs(mode), false)
|
||||
}
|
||||
|
||||
fn downloadable(self) -> Option<DownloadableTab> {
|
||||
match self {
|
||||
Self::Mods => Some(DownloadableTab {
|
||||
directory: "mods",
|
||||
extension: ".jar",
|
||||
scanner: crate::instance::scan_one_mod,
|
||||
loading_text: "Loading mods...",
|
||||
empty_text: "No mods installed.",
|
||||
}),
|
||||
Self::ResourcePacks => Some(DownloadableTab {
|
||||
directory: "resourcepacks",
|
||||
extension: ".zip",
|
||||
scanner: crate::instance::scan_one_resource_pack,
|
||||
loading_text: "Loading resource packs...",
|
||||
empty_text: "No resource packs installed.",
|
||||
}),
|
||||
Self::Shaders => Some(DownloadableTab {
|
||||
directory: "shaderpacks",
|
||||
extension: ".zip",
|
||||
scanner: crate::instance::scan_one_shader,
|
||||
loading_text: "Loading shaders...",
|
||||
empty_text: "No shaders installed.",
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visible_tabs(mode: ContentMode) -> &'static [ContentTab] {
|
||||
@@ -340,140 +378,31 @@ pub fn render(
|
||||
content_area =
|
||||
crate::tui::widgets::popups::render_keybind_overflow(frame, content_area, &keybind_lines);
|
||||
|
||||
let downloadable_state = match tab {
|
||||
ContentTab::Mods => Some((mods_state, mods_discovery_state)),
|
||||
ContentTab::ResourcePacks => Some((resource_packs_state, resource_packs_discovery_state)),
|
||||
ContentTab::Shaders => Some((shaders_state, shaders_discovery_state)),
|
||||
_ => None,
|
||||
};
|
||||
if let Some((state, discovery_state)) = downloadable_state {
|
||||
render_downloadable(
|
||||
frame,
|
||||
content_area,
|
||||
instance,
|
||||
state,
|
||||
discovery_state,
|
||||
tab.downloadable().expect("downloadable tab config"),
|
||||
mode,
|
||||
is_focused,
|
||||
instances_dir,
|
||||
picker,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// lazy-load: only scan when switching to an instance that hasn't been loaded yet
|
||||
match tab {
|
||||
ContentTab::Mods => {
|
||||
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(crate::storage::MINECRAFT_DIR_NAME)
|
||||
.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 {
|
||||
render_discovery(
|
||||
frame,
|
||||
content_area,
|
||||
mods_discovery_state,
|
||||
instance,
|
||||
is_focused,
|
||||
"Searching Modrinth...",
|
||||
picker,
|
||||
);
|
||||
} else {
|
||||
super::list::render(
|
||||
frame,
|
||||
content_area,
|
||||
mods_state,
|
||||
is_focused,
|
||||
"Loading mods...",
|
||||
"No mods installed.",
|
||||
picker,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No instance selected.")
|
||||
.style(Style::default().fg(theme.text_dim())),
|
||||
content_area,
|
||||
);
|
||||
}
|
||||
}
|
||||
ContentTab::ResourcePacks => {
|
||||
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(crate::storage::MINECRAFT_DIR_NAME)
|
||||
.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 {
|
||||
render_discovery(
|
||||
frame,
|
||||
content_area,
|
||||
resource_packs_discovery_state,
|
||||
instance,
|
||||
is_focused,
|
||||
"Searching Modrinth...",
|
||||
picker,
|
||||
);
|
||||
} else {
|
||||
super::list::render(
|
||||
frame,
|
||||
content_area,
|
||||
resource_packs_state,
|
||||
is_focused,
|
||||
"Loading resource packs...",
|
||||
"No resource packs installed.",
|
||||
picker,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No instance selected.")
|
||||
.style(Style::default().fg(theme.text_dim())),
|
||||
content_area,
|
||||
);
|
||||
}
|
||||
}
|
||||
ContentTab::Shaders => {
|
||||
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(crate::storage::MINECRAFT_DIR_NAME)
|
||||
.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 {
|
||||
render_discovery(
|
||||
frame,
|
||||
content_area,
|
||||
shaders_discovery_state,
|
||||
instance,
|
||||
is_focused,
|
||||
"Searching Modrinth...",
|
||||
picker,
|
||||
);
|
||||
} else {
|
||||
super::list::render(
|
||||
frame,
|
||||
content_area,
|
||||
shaders_state,
|
||||
is_focused,
|
||||
"Loading shaders...",
|
||||
"No shaders installed.",
|
||||
picker,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No instance selected.")
|
||||
.style(Style::default().fg(theme.text_dim())),
|
||||
content_area,
|
||||
);
|
||||
}
|
||||
}
|
||||
ContentTab::Mods | ContentTab::ResourcePacks | ContentTab::Shaders => unreachable!(),
|
||||
ContentTab::Logs => {
|
||||
if let Some(instance) = instance {
|
||||
if logs_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||
@@ -547,6 +476,58 @@ pub fn render(
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_downloadable(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
instance: Option<&crate::instance::InstanceConfig>,
|
||||
state: &mut super::list::ContentListState,
|
||||
discovery_state: &mut DiscoveryState,
|
||||
tab: DownloadableTab,
|
||||
mode: ContentMode,
|
||||
is_focused: bool,
|
||||
instances_dir: &std::path::Path,
|
||||
picker: &ratatui_image::picker::Picker,
|
||||
) {
|
||||
let Some(instance) = instance else {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No instance selected.")
|
||||
.style(Style::default().fg(THEME.as_ref().text_dim())),
|
||||
area,
|
||||
);
|
||||
return;
|
||||
};
|
||||
if state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||
let content_dir = instances_dir
|
||||
.join(&instance.name)
|
||||
.join(crate::storage::MINECRAFT_DIR_NAME)
|
||||
.join(tab.directory);
|
||||
state.start_load(&content_dir, &instance.name, tab.scanner, tab.extension);
|
||||
state.watch_dir(content_dir);
|
||||
}
|
||||
if mode == ContentMode::Discover {
|
||||
render_discovery(
|
||||
frame,
|
||||
area,
|
||||
discovery_state,
|
||||
instance,
|
||||
is_focused,
|
||||
"Searching Modrinth...",
|
||||
picker,
|
||||
);
|
||||
} else {
|
||||
super::list::render(
|
||||
frame,
|
||||
area,
|
||||
state,
|
||||
is_focused,
|
||||
tab.loading_text,
|
||||
tab.empty_text,
|
||||
picker,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn render_discovery(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
|
||||
Reference in new issue
Block a user