From d676a2f5a42029570a8b37f268b4a06b372d8816 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Fri, 29 May 2026 09:17:47 -0600 Subject: [PATCH] =?UTF-8?q?feat(locallibrary):=20Folders=20tab=20complete?= =?UTF-8?q?=20=E2=80=94=20flat=20toolbar=20+=20tree=20two-pane?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Flat mode now has the full toolbar: search, sort (artist/title/year), group-by (off/letter/artist) and a grid/list view toggle, driven by a derive_folders() pass that mirrors favorites::derive_albums over the directory-grouped album set (folders-visible / folders-grouped). Tree mode is a new two-pane filesystem browser: a flattened FolderNode list in the left rail (lazy one-level expand via list_folder_children, collapse drops the descendant block, recursive track counts gate the chevron) and a detail pane on the right showing the selected folder's direct tracks plus immediate subfolders, with Play all (recursive) and per-track play scoped to the folder's direct list. A flat/tree segmented toggle switches modes; the tree loads its roots lazily on first switch. Slint has no native TreeView, so the tree is a depth-indented ListView of visible nodes spliced on expand/collapse. No pills (ADR-008): underline tabs, small-radius rectangles and an accent selection bar. cc --- crates/qbz-slint/src/local_library.rs | 292 ++++++++- crates/qbz-slint/src/main.rs | 122 ++++ crates/qbz-slint/src/playback.rs | 58 ++ .../ui/locallibrary/LocalLibraryView.slint | 573 ++++++++++++++++-- crates/qbz-slint/ui/state.slint | 48 +- 5 files changed, 1056 insertions(+), 37 deletions(-) diff --git a/crates/qbz-slint/src/local_library.rs b/crates/qbz-slint/src/local_library.rs index db2ffec94..7688fc54c 100644 --- a/crates/qbz-slint/src/local_library.rs +++ b/crates/qbz-slint/src/local_library.rs @@ -16,7 +16,10 @@ use std::sync::atomic::{AtomicU64, Ordering}; use slint::{ComponentHandle, Model, ModelRc, VecModel}; use crate::artwork::{ArtworkJob, ArtworkTarget, ImageCache}; -use crate::{AlbumCardItem, AppWindow, LocalArtistItem, LocalLibraryState, TrackItem}; +use crate::{ + AlbumCardItem, AppWindow, DiscoverSection, FolderNode, LocalArtistItem, LocalLibraryState, + TrackItem, +}; /// The four browse tabs. Order mirrors Tauri's default tab order /// (`tracks / folders / albums / artists`); the visible order is a user @@ -715,6 +718,92 @@ fn apply_folders(window: &AppWindow, cards: Vec) { s.set_folders(ModelRc::new(VecModel::from(items))); s.set_folders_loading(false); s.set_folders_load_failed(false); + derive_folders(window); +} + +/// First-letter bucket key for alpha grouping (`#` for non-alphabetic). +/// Mirrors favorites' `album_alpha_key` so the two surfaces sort identically. +fn folder_alpha_key(title: &str) -> String { + title + .chars() + .find(|c| c.is_alphanumeric()) + .map(|c| { + let up = c.to_uppercase().next().unwrap_or(c); + if up.is_ascii_digit() { + "#".to_string() + } else { + up.to_string() + } + }) + .unwrap_or_else(|| "#".to_string()) +} + +/// Re-derive the flat-mode visible / grouped folder models from the full +/// `folders` set, applying the toolbar's search query, sort key and group +/// mode. Mirrors `favorites::derive_albums` so behaviour is identical to the +/// metadata Albums tab; the only difference is the source model (directory +/// grouping instead of metadata grouping). +pub fn derive_folders(window: &AppWindow) { + let s = window.global::(); + let query_owned = s.get_folders_search().to_lowercase(); + let query = query_owned.trim(); + let sort = s.get_folders_sort().to_string(); + let group = s.get_folders_group().to_string(); + let all = s.get_folders(); + let empty_sections = || ModelRc::new(VecModel::from(Vec::::new())); + + let mut filtered: Vec = (0..all.row_count()) + .filter_map(|i| all.row_data(i)) + .filter(|a| { + query.is_empty() + || a.title.to_lowercase().contains(query) + || a.artist.to_lowercase().contains(query) + }) + .collect(); + crate::album_map::sort_album_items(&mut filtered, &sort); + + if group == "off" { + s.set_folders_visible(ModelRc::new(VecModel::from(filtered))); + s.set_folders_grouped(empty_sections()); + return; + } + + // Grouped: bucket by artist name, or by the title's first letter + // (`#` for non-alphabetic). Sections ordered alphabetically, `#` last. + let mut map: Vec<(String, Vec)> = Vec::new(); + let mut index: std::collections::HashMap = std::collections::HashMap::new(); + for item in filtered { + let key = if group == "artist" { + let a = item.artist.to_string(); + if a.is_empty() { + "Unknown".to_string() + } else { + a + } + } else { + folder_alpha_key(item.title.as_str()) + }; + let idx = *index.entry(key.clone()).or_insert_with(|| { + map.push((key.clone(), Vec::new())); + map.len() - 1 + }); + map[idx].1.push(item); + } + map.sort_by(|(a, _), (b, _)| match (a == "#", b == "#") { + (true, false) => std::cmp::Ordering::Greater, + (false, true) => std::cmp::Ordering::Less, + _ => a.to_lowercase().cmp(&b.to_lowercase()), + }); + let sections: Vec = map + .into_iter() + .map(|(key, items)| DiscoverSection { + title: key.into(), + endpoint: "".into(), + albums: ModelRc::new(VecModel::from(items)), + }) + .collect(); + s.set_folders_grouped(ModelRc::new(VecModel::from(sections))); + s.set_folders_visible(ModelRc::new(VecModel::from(Vec::::new()))); } /// Load the folder-grouped album grid on first visit (re-entry keeps it). @@ -744,6 +833,207 @@ pub fn ensure_folders_loaded( }); } +// ============================ Folders (tree) ============================== +// +// Slint has no native TreeView and no self-recursive component, so the tree +// is rendered as a flattened list of visible nodes (`FolderNode`) in a +// ListView: each node carries its `depth` (drives the indent) and an +// `expanded` flag. Expanding fetches one level lazily via +// `list_folder_children`; collapsing drops the contiguous descendant block. + +/// Last path component for display (the registered root paths are absolute). +fn path_basename(path: &str) -> String { + path.trim_end_matches('/') + .rsplit('/') + .next() + .filter(|s| !s.is_empty()) + .unwrap_or(path) + .to_string() +} + +/// Map a backend tree entry to a flattened `FolderNode` at the given depth. +fn entry_to_node(entry: &qbz_library::FolderTreeEntry, depth: i32) -> FolderNode { + match entry { + qbz_library::FolderTreeEntry::Folder { + path, + segment, + track_count_under, + .. + } => FolderNode { + path: path.clone().into(), + segment: segment.clone().into(), + depth, + is_folder: true, + expanded: false, + can_expand: *track_count_under > 0, + track_count: *track_count_under as i32, + }, + qbz_library::FolderTreeEntry::Track { path, segment } => FolderNode { + path: path.clone().into(), + segment: segment.clone().into(), + depth, + is_folder: false, + expanded: false, + can_expand: false, + track_count: 0, + }, + } +} + +/// Load the tree roots (registered library folders) on first switch to tree +/// mode. Each root gets a recursive track count so the rail can show totals +/// and gate the expand affordance. +pub fn ensure_folder_tree_loaded(weak: slint::Weak, handle: tokio::runtime::Handle) { + let _ = weak.upgrade_in_event_loop(move |w| { + let s = w.global::(); + if s.get_folder_tree().row_count() != 0 || s.get_folder_tree_loading() { + return; + } + s.set_folder_tree_loading(true); + let weak2 = w.as_weak(); + handle.spawn(async move { + let roots = tokio::task::spawn_blocking(|| { + crate::library_db::with_db(|db| { + let paths = db.get_folders()?; + let mut out: Vec<(String, u32)> = Vec::with_capacity(paths.len()); + for p in paths { + let cnt = db.count_folder_tracks_recursive(&p, false)?; + out.push((p, cnt)); + } + Ok::<_, qbz_library::LibraryError>(out) + }) + .unwrap_or_default() + }) + .await + .unwrap_or_default(); + let _ = weak2.upgrade_in_event_loop(move |w| { + let nodes: Vec = roots + .into_iter() + .map(|(p, cnt)| FolderNode { + segment: path_basename(&p).into(), + path: p.into(), + depth: 0, + is_folder: true, + expanded: false, + can_expand: cnt > 0, + track_count: cnt as i32, + }) + .collect(); + let s = w.global::(); + s.set_folder_tree(ModelRc::new(VecModel::from(nodes))); + s.set_folder_tree_loading(false); + }); + }); + }); +} + +/// Collect the current flattened tree into a plain vec for splicing. +fn collect_tree(s: &LocalLibraryState) -> Vec { + let m = s.get_folder_tree(); + (0..m.row_count()).filter_map(|i| m.row_data(i)).collect() +} + +/// Expand or collapse a folder node. Collapsing is pure UI (drop the +/// contiguous descendant block); expanding fetches one child level lazily. +pub fn toggle_folder_node( + weak: slint::Weak, + handle: tokio::runtime::Handle, + path: String, + expand: bool, +) { + if !expand { + let _ = weak.upgrade_in_event_loop(move |w| { + let s = w.global::(); + let mut nodes = collect_tree(&s); + if let Some(pos) = nodes.iter().position(|n| n.path == path) { + let depth = nodes[pos].depth; + nodes[pos].expanded = false; + let mut end = pos + 1; + while end < nodes.len() && nodes[end].depth > depth { + end += 1; + } + nodes.drain(pos + 1..end); + s.set_folder_tree(ModelRc::new(VecModel::from(nodes))); + } + }); + return; + } + // Expand: fetch this level's children off-thread, then splice them in. + let path_for_fetch = path.clone(); + handle.spawn(async move { + let children = tokio::task::spawn_blocking(move || { + crate::library_db::with_db(|db| db.list_folder_children(&path_for_fetch, false)) + .unwrap_or_default() + }) + .await + .unwrap_or_default(); + let _ = weak.upgrade_in_event_loop(move |w| { + let s = w.global::(); + let mut nodes = collect_tree(&s); + if let Some(pos) = nodes.iter().position(|n| n.path == path) { + let depth = nodes[pos].depth; + nodes[pos].expanded = true; + let child_nodes: Vec = children + .iter() + .map(|e| entry_to_node(e, depth + 1)) + .collect(); + for (i, cn) in child_nodes.into_iter().enumerate() { + nodes.insert(pos + 1 + i, cn); + } + s.set_folder_tree(ModelRc::new(VecModel::from(nodes))); + } + }); + }); +} + +/// Select a folder in the tree: load its detail pane — direct child tracks +/// plus immediate subfolders (for in-pane drill-down). +pub fn select_folder( + weak: slint::Weak, + handle: tokio::runtime::Handle, + path: String, + segment: String, +) { + let _ = weak.upgrade_in_event_loop({ + let path = path.clone(); + let segment = segment.clone(); + move |w| { + let s = w.global::(); + s.set_folders_selected_path(path.clone().into()); + s.set_folders_selected_name(segment.clone().into()); + s.set_folder_detail_loading(true); + } + }); + let path_for_fetch = path.clone(); + handle.spawn(async move { + let (tracks, subfolders) = tokio::task::spawn_blocking(move || { + let tracks = crate::library_db::with_db(|db| { + db.list_folder_tracks(&path_for_fetch, false) + }) + .unwrap_or_default(); + let children = crate::library_db::with_db(|db| { + db.list_folder_children(&path_for_fetch, false) + }) + .unwrap_or_default(); + (tracks, children) + }) + .await + .unwrap_or_default(); + let _ = weak.upgrade_in_event_loop(move |w| { + let track_items: Vec = tracks.into_iter().map(map_local_track).collect(); + let sub_nodes: Vec = subfolders + .iter() + .filter(|e| matches!(e, qbz_library::FolderTreeEntry::Folder { .. })) + .map(|e| entry_to_node(e, 0)) + .collect(); + let s = w.global::(); + s.set_folder_detail_tracks(ModelRc::new(VecModel::from(track_items))); + s.set_folder_detail_subfolders(ModelRc::new(VecModel::from(sub_nodes))); + s.set_folder_detail_loading(false); + }); + }); +} + // ============================== Artists tab =============================== // // A full-loaded grid of artists (name + album/track counts). Images + diff --git a/crates/qbz-slint/src/main.rs b/crates/qbz-slint/src/main.rs index 5772527ed..cd585b4b2 100644 --- a/crates/qbz-slint/src/main.rs +++ b/crates/qbz-slint/src/main.rs @@ -4187,6 +4187,128 @@ fn main() -> Result<(), Box> { }); } + // ---- Folders tab actions ---- + { + let weak = window.as_weak(); + window + .global::() + .on_folders_search(move |_query| { + // Query is two-way bound to folders-search; re-derive in place. + if let Some(w) = weak.upgrade() { + local_library::derive_folders(&w); + } + }); + } + { + let weak = window.as_weak(); + window + .global::() + .on_folders_set_sort(move |sort| { + if let Some(w) = weak.upgrade() { + w.global::().set_folders_sort(sort); + local_library::derive_folders(&w); + } + }); + } + { + let weak = window.as_weak(); + window + .global::() + .on_folders_set_group(move |group| { + if let Some(w) = weak.upgrade() { + w.global::().set_folders_group(group); + local_library::derive_folders(&w); + } + }); + } + { + let weak = window.as_weak(); + let handle = tokio_rt.handle().clone(); + window + .global::() + .on_folders_set_mode(move |mode| { + if let Some(w) = weak.upgrade() { + w.global::() + .set_folders_view_mode(mode.clone()); + } + // Lazy-load the tree roots the first time tree mode is shown. + if mode.as_str() == "tree" { + local_library::ensure_folder_tree_loaded(weak.clone(), handle.clone()); + } + }); + } + { + let weak = window.as_weak(); + let handle = tokio_rt.handle().clone(); + window + .global::() + .on_folders_toggle_node(move |path, expand| { + local_library::toggle_folder_node( + weak.clone(), + handle.clone(), + path.to_string(), + expand, + ); + }); + } + { + let weak = window.as_weak(); + let handle = tokio_rt.handle().clone(); + window + .global::() + .on_folders_select(move |path, segment| { + local_library::select_folder( + weak.clone(), + handle.clone(), + path.to_string(), + segment.to_string(), + ); + }); + } + { + let runtime = app_runtime.clone(); + let weak = window.as_weak(); + let handle = tokio_rt.handle().clone(); + window + .global::() + .on_folders_play_node(move |path| { + playback::play_local_folder_recursive( + runtime.clone(), + weak.clone(), + handle.clone(), + path.to_string(), + ); + }); + } + { + let runtime = app_runtime.clone(); + let weak = window.as_weak(); + let handle = tokio_rt.handle().clone(); + window + .global::() + .on_folders_play_track(move |id| { + if let Ok(row_id) = id.parse::() { + let path = weak + .upgrade() + .map(|w| { + w.global::() + .get_folders_selected_path() + .to_string() + }) + .unwrap_or_default(); + if !path.is_empty() { + playback::play_local_folder_tracks_from( + runtime.clone(), + weak.clone(), + handle.clone(), + path, + row_id, + ); + } + } + }); + } + // Discover "View all" — open the full-list page for a section, // recording it as a history entry (mirrors the favorites branch // of on_header_menu_navigate). diff --git a/crates/qbz-slint/src/playback.rs b/crates/qbz-slint/src/playback.rs index 2d7a8b298..50679293a 100644 --- a/crates/qbz-slint/src/playback.rs +++ b/crates/qbz-slint/src/playback.rs @@ -242,6 +242,64 @@ pub fn play_local_album( }); } +/// Play everything under a folder (recursive), in path order — the whole +/// subtree becomes the queue. Mirrors `play_local_album` but sources the +/// tracks from the folder hierarchy instead of a metadata group. +pub fn play_local_folder_recursive( + runtime: Runtime, + weak: slint::Weak, + handle: tokio::runtime::Handle, + folder_path: String, +) { + handle.spawn(async move { + let tracks = tokio::task::spawn_blocking(move || { + let mut tracks = crate::library_db::with_db(|db| { + db.list_folder_tracks_recursive(&folder_path, false) + }) + .unwrap_or_default(); + fill_missing_covers(&mut tracks); + tracks + }) + .await + .unwrap_or_default(); + if tracks.is_empty() { + return; + } + play_local_tracks_now(&runtime, &weak, tracks, 0).await; + }); +} + +/// Play a folder's DIRECT tracks (non-recursive) starting at `start_track_id` +/// — the folder's own track list becomes the queue. Used by the tree-mode +/// detail pane when a track row is clicked. +pub fn play_local_folder_tracks_from( + runtime: Runtime, + weak: slint::Weak, + handle: tokio::runtime::Handle, + folder_path: String, + start_track_id: i64, +) { + handle.spawn(async move { + let tracks = tokio::task::spawn_blocking(move || { + let mut tracks = + crate::library_db::with_db(|db| db.list_folder_tracks(&folder_path, false)) + .unwrap_or_default(); + fill_missing_covers(&mut tracks); + tracks + }) + .await + .unwrap_or_default(); + if tracks.is_empty() { + return; + } + let start = tracks + .iter() + .position(|t| t.id == start_track_id) + .unwrap_or(0); + play_local_tracks_now(&runtime, &weak, tracks, start).await; + }); +} + /// Play the Tracks-tab list starting at `start_track_id`: the matching set /// (current search) becomes the queue, so playback continues down the list. pub fn play_local_tracks_from( diff --git a/crates/qbz-slint/ui/locallibrary/LocalLibraryView.slint b/crates/qbz-slint/ui/locallibrary/LocalLibraryView.slint index 0688ed723..2bbb1a42e 100644 --- a/crates/qbz-slint/ui/locallibrary/LocalLibraryView.slint +++ b/crates/qbz-slint/ui/locallibrary/LocalLibraryView.slint @@ -18,7 +18,7 @@ import { Theme } from "../foundation/semantic-colors.slint"; import { Typography } from "../foundation/typography.slint"; -import { LocalLibraryState, LocalLibraryActions } from "../state.slint"; +import { LocalLibraryState, LocalLibraryActions, FolderNode } from "../state.slint"; import { NavButtons } from "../shell/NavButtons.slint"; import { QbzIcon } from "../primitives/QbzIcon.slint"; import { AlbumCollectionView } from "../discover/AlbumCollectionView.slint"; @@ -27,6 +27,7 @@ import { QbzSelect } from "../primitives/QbzSelect.slint"; import { LoadingSpinner } from "../primitives/LoadingSpinner.slint"; import { ListScrollbar } from "../primitives/ListScrollbar.slint"; import { TrackRow } from "../primitives/TrackRow.slint"; +import { ToggleButton } from "../primitives/ToggleButton.slint"; import { ListView } from "std-widgets.slint"; // One tab in the Local Library tab menu — underline style (ADR-008: NO @@ -144,6 +145,160 @@ component CircleIconBtn inherits Rectangle { } } +// Grid/list segmented toggle for the Folders flat mode (mirrors Favorites' +// ViewToggle). ADR-008: small-radius rectangle, NOT a pill. +component ViewToggle inherits Rectangle { + in property view-mode; // "grid" | "list" + callback set-view(string); + width: 60px; + height: 30px; + border-radius: 6px; + background: Theme.surface-elevated; + clip: true; + HorizontalLayout { + ToggleButton { + icon: @image-url("../assets/icons/layout-grid.svg"); + active: root.view-mode == "grid"; + sm: true; + clicked => { root.set-view("grid"); } + } + ToggleButton { + icon: @image-url("../assets/icons/list.svg"); + active: root.view-mode == "list"; + sm: true; + clicked => { root.set-view("list"); } + } + } +} + +// Flat/tree mode segmented toggle for the Folders tab. flat = the album grid +// grouped by directory; tree = the two-pane filesystem browser. +component ModeToggle inherits Rectangle { + in property mode; // "flat" | "tree" + callback set-mode(string); + width: 60px; + height: 30px; + border-radius: 6px; + background: Theme.surface-elevated; + clip: true; + HorizontalLayout { + ToggleButton { + icon: @image-url("../assets/icons/disc.svg"); + active: root.mode == "flat"; + sm: true; + clicked => { root.set-mode("flat"); } + } + ToggleButton { + icon: @image-url("../assets/icons/folder.svg"); + active: root.mode == "tree"; + sm: true; + clicked => { root.set-mode("tree"); } + } + } +} + +// One row of the flattened folder tree (tree mode, left rail). Carries its +// `depth` (indent), an expand chevron for folders with content, a folder / +// music icon, the segment name and (for folders) the recursive track count. +component TreeRow inherits Rectangle { + in property node; + in property selected; + callback toggle(); // chevron — expand/collapse + callback activate(); // row body — select folder (load detail) + height: 32px; + border-radius: 6px; + background: root.selected + ? Theme.surface-elevated + : (row-ta.has-hover ? Theme.surface-hover : transparent); + + row-ta := TouchArea { + mouse-cursor: root.node.is-folder ? pointer : default; + clicked => { + if (root.node.is-folder) { root.activate(); } + } + } + + // Accent bar marking the selected folder (mirrors the underline-tab + // accent language; ADR-008: a thin rectangle, not a pill). + if root.selected: Rectangle { + x: 0; + width: 3px; + height: parent.height - 8px; + y: 4px; + background: Theme.accent; + border-radius: 1.5px; + } + + HorizontalLayout { + padding-left: 8px + root.node.depth * 16px; + padding-right: 10px; + spacing: 6px; + + // Expand chevron — its own hit target so toggling doesn't select. + Rectangle { + width: 18px; + VerticalLayout { + alignment: center; + HorizontalLayout { + alignment: center; + if root.node.can-expand: QbzIcon { + source: root.node.expanded + ? @image-url("../assets/icons/chevron-down.svg") + : @image-url("../assets/icons/chevron-right.svg"); + width: 13px; + height: 13px; + tint: Theme.text-muted; + } + } + } + chev-ta := TouchArea { + mouse-cursor: root.node.can-expand ? pointer : default; + clicked => { + if (root.node.can-expand) { root.toggle(); } + } + } + } + + // Folder / track icon. + VerticalLayout { + alignment: center; + QbzIcon { + source: root.node.is-folder + ? (root.node.expanded + ? @image-url("../assets/icons/folder-open.svg") + : @image-url("../assets/icons/folder.svg")) + : @image-url("../assets/icons/music.svg"); + width: 16px; + height: 16px; + tint: root.node.is-folder ? Theme.accent : Theme.text-muted; + } + } + + // Name. + VerticalLayout { + alignment: center; + horizontal-stretch: 1; + Text { + text: root.node.segment; + color: root.selected ? Theme.text-primary : Theme.text-secondary; + font-size: Typography.body; + font-weight: root.selected ? Typography.semibold : Typography.regular; + overflow: elide; + } + } + + // Recursive track count (folders only). + if root.node.is-folder && root.node.track-count > 0: VerticalLayout { + alignment: center; + Text { + text: root.node.track-count; + color: Theme.text-muted; + font-size: Typography.legal; + } + } + } +} + export component LocalLibraryView inherits Rectangle { background: Theme.surface-main; @@ -268,6 +423,97 @@ export component LocalLibraryView inherits Rectangle { } } } + + // Folders toolbar — search (both modes); flat adds sort + group + + // grid/list; a flat/tree mode toggle sits at the far right. + if LocalLibraryState.active-tab == "folders" + && (LocalLibraryState.folders.length > 0 + || LocalLibraryState.folders-search != ""): HorizontalLayout { + spacing: 8px; + VerticalLayout { + alignment: center; + ExpandableSearch { + placeholder: @tr("Search"); + sm: true; + query <=> LocalLibraryState.folders-search; + edited(q) => { + LocalLibraryActions.folders-search(q); + } + } + } + // Flat-only controls: sort, group-by, grid/list. + if LocalLibraryState.folders-view-mode == "flat": HorizontalLayout { + spacing: 8px; + VerticalLayout { + alignment: center; + QbzSelect { + menu-width: 180px; + options: [ + @tr("Artist A-Z"), + @tr("Artist Z-A"), + @tr("Title A-Z"), + @tr("Title Z-A"), + @tr("Year (newest)"), + @tr("Year (oldest)"), + ]; + sm: true; + current-index: LocalLibraryState.folders-sort == "artist-asc" ? 0 + : LocalLibraryState.folders-sort == "artist-desc" ? 1 + : LocalLibraryState.folders-sort == "title-asc" ? 2 + : LocalLibraryState.folders-sort == "title-desc" ? 3 + : LocalLibraryState.folders-sort == "year-desc" ? 4 + : 5; + selected(i) => { + LocalLibraryActions.folders-set-sort( + i == 0 ? "artist-asc" + : i == 1 ? "artist-desc" + : i == 2 ? "title-asc" + : i == 3 ? "title-desc" + : i == 4 ? "year-desc" + : "year-asc"); + } + } + } + VerticalLayout { + alignment: center; + QbzSelect { + menu-width: 160px; + options: [ + @tr("No grouping"), + @tr("Group by letter"), + @tr("Group by artist"), + ]; + sm: true; + current-index: LocalLibraryState.folders-group == "alpha" ? 1 + : LocalLibraryState.folders-group == "artist" ? 2 + : 0; + selected(i) => { + LocalLibraryActions.folders-set-group( + i == 1 ? "alpha" : i == 2 ? "artist" : "off"); + } + } + } + VerticalLayout { + alignment: center; + ViewToggle { + view-mode: LocalLibraryState.folders-grid-view; + set-view(v) => { + LocalLibraryState.folders-grid-view = v; + } + } + } + } + // Flat/tree mode toggle (always visible). + VerticalLayout { + alignment: center; + ModeToggle { + mode: LocalLibraryState.folders-view-mode; + set-mode(m) => { + LocalLibraryActions.folders-set-mode(m); + } + } + } + } } Rectangle { height: 14px; } @@ -537,8 +783,9 @@ export component LocalLibraryView inherits Rectangle { } } - // -------- Folders tab (flat) — folder-grouped album grid ----- + // -------- Folders tab — flat grid/list OR tree two-pane ------ if LocalLibraryState.active-tab == "folders": Rectangle { + // Initial flat load. if LocalLibraryState.folders-loading: VerticalLayout { alignment: center; HorizontalLayout { @@ -546,8 +793,10 @@ export component LocalLibraryView inherits Rectangle { LoadingSpinner { size: 36px; } } } + // Empty — nothing indexed. if !LocalLibraryState.folders-loading - && LocalLibraryState.folders.length == 0: VerticalLayout { + && LocalLibraryState.folders.length == 0 + && LocalLibraryState.folders-search == "": VerticalLayout { alignment: center; Text { text: @tr("No folders in your local library yet."); @@ -556,44 +805,298 @@ export component LocalLibraryView inherits Rectangle { horizontal-alignment: center; } } + + // ===================== FLAT MODE ===================== if !LocalLibraryState.folders-loading - && LocalLibraryState.folders.length > 0: Rectangle { - folders-flick := Flickable { - viewport-width: self.width; - viewport-height: folders-page.preferred-height; - folders-page := VerticalLayout { - width: parent.width; - padding-left: 32px; - padding-right: 32px; - padding-top: 16px; - padding-bottom: 100px; - alignment: start; - AlbumCollectionView { - albums: LocalLibraryState.folders; - is-grouped: false; - view-mode: "grid"; - show-source: false; - show-favorite: false; - show-source-badge: true; - open-album(id) => { - LocalLibraryActions.open-album(id); + && LocalLibraryState.folders.length > 0 + && LocalLibraryState.folders-view-mode == "flat": Rectangle { + // Search matched nothing. + if LocalLibraryState.folders-visible.length == 0 + && LocalLibraryState.folders-grouped.length == 0 + && LocalLibraryState.folders-search != "": VerticalLayout { + alignment: center; + Text { + text: @tr("No folders match your search."); + color: Theme.text-muted; + font-size: Typography.body; + horizontal-alignment: center; + } + } + if LocalLibraryState.folders-visible.length > 0 + || LocalLibraryState.folders-grouped.length > 0: Rectangle { + folders-flick := Flickable { + viewport-width: self.width; + viewport-height: folders-page.preferred-height; + folders-page := VerticalLayout { + width: parent.width; + padding-left: 32px; + padding-right: 32px; + padding-top: 16px; + padding-bottom: 100px; + alignment: start; + AlbumCollectionView { + albums: LocalLibraryState.folders-visible; + grouped: LocalLibraryState.folders-grouped; + is-grouped: LocalLibraryState.folders-group != "off"; + view-mode: LocalLibraryState.folders-grid-view; + show-source: false; + show-favorite: false; + show-source-badge: true; + open-album(id) => { + LocalLibraryActions.open-album(id); + } + open-artist(id) => { + LocalLibraryActions.open-artist(id); + } + media-action(kind, id, action) => { + LocalLibraryActions.album-action(id, action); + } } - open-artist(id) => { - LocalLibraryActions.open-artist(id); - } - media-action(kind, id, action) => { - LocalLibraryActions.album-action(id, action); + } + } + ListScrollbar { + x: parent.width - self.width - 4px; + width: 14px; + height: 100%; + viewport-height: folders-flick.viewport-height; + visible-height: folders-flick.height; + viewport-y <=> folders-flick.viewport-y; + } + } + } + + // ===================== TREE MODE ===================== + if !LocalLibraryState.folders-loading + && LocalLibraryState.folders.length > 0 + && LocalLibraryState.folders-view-mode == "tree": HorizontalLayout { + spacing: 0px; + + // ---- Left rail: the flattened folder tree ---- + Rectangle { + width: 340px; + if LocalLibraryState.folder-tree-loading: VerticalLayout { + alignment: center; + HorizontalLayout { + alignment: center; + LoadingSpinner { size: 28px; } + } + } + if !LocalLibraryState.folder-tree-loading: VerticalLayout { + padding-left: 24px; + padding-right: 8px; + padding-top: 12px; + tree-list := ListView { + for node[i] in LocalLibraryState.folder-tree: TreeRow { + node: node; + selected: node.path == LocalLibraryState.folders-selected-path; + toggle => { + LocalLibraryActions.folders-toggle-node(node.path, !node.expanded); + } + activate => { + LocalLibraryActions.folders-select(node.path, node.segment); + } } } } } - ListScrollbar { - x: parent.width - self.width - 4px; - width: 14px; - height: 100%; - viewport-height: folders-flick.viewport-height; - visible-height: folders-flick.height; - viewport-y <=> folders-flick.viewport-y; + + // Divider. + Rectangle { + width: 1px; + background: Theme.border-subtle; + } + + // ---- Right pane: selected folder detail ---- + Rectangle { + horizontal-stretch: 1; + + // Nothing selected yet. + if LocalLibraryState.folders-selected-path == "": VerticalLayout { + alignment: center; + Text { + text: @tr("Select a folder to see its contents."); + color: Theme.text-muted; + font-size: Typography.body; + horizontal-alignment: center; + } + } + + if LocalLibraryState.folders-selected-path != "": Rectangle { + detail-flick := Flickable { + viewport-width: self.width; + viewport-height: detail-page.preferred-height; + detail-page := VerticalLayout { + width: parent.width; + padding-left: 28px; + padding-right: 28px; + padding-top: 16px; + padding-bottom: 100px; + spacing: 14px; + alignment: start; + + // Header: folder name + Play all. + HorizontalLayout { + spacing: 12px; + VerticalLayout { + alignment: center; + horizontal-stretch: 1; + Text { + text: LocalLibraryState.folders-selected-name; + color: Theme.text-primary; + font-size: Typography.section; + font-weight: Typography.bold; + overflow: elide; + } + } + VerticalLayout { + alignment: center; + Rectangle { + width: playall-row.preferred-width + 28px; + height: 34px; + border-radius: 6px; + background: playall-ta.has-hover + ? Theme.accent-hover : Theme.accent; + playall-row := HorizontalLayout { + x: (parent.width - self.width) / 2; + spacing: 7px; + VerticalLayout { + alignment: center; + QbzIcon { + source: @image-url("../assets/icons/play-fill.svg"); + width: 13px; + height: 13px; + tint: Theme.accent-text; + } + } + VerticalLayout { + alignment: center; + Text { + text: @tr("Play all"); + color: Theme.accent-text; + font-size: Typography.legal; + font-weight: Typography.semibold; + } + } + } + playall-ta := TouchArea { + mouse-cursor: pointer; + clicked => { + LocalLibraryActions.folders-play-node( + LocalLibraryState.folders-selected-path); + } + } + } + } + } + + if LocalLibraryState.folder-detail-loading: HorizontalLayout { + alignment: center; + padding-top: 30px; + LoadingSpinner { size: 28px; } + } + + // Subfolders (drill-down), if any. + if !LocalLibraryState.folder-detail-loading + && LocalLibraryState.folder-detail-subfolders.length > 0: VerticalLayout { + spacing: 6px; + Text { + text: @tr("Subfolders"); + color: Theme.text-muted; + font-size: Typography.legal; + font-weight: Typography.semibold; + } + for sub[si] in LocalLibraryState.folder-detail-subfolders: Rectangle { + height: 34px; + border-radius: 6px; + background: sub-ta.has-hover ? Theme.surface-hover : transparent; + HorizontalLayout { + padding-left: 8px; + padding-right: 10px; + spacing: 8px; + VerticalLayout { + alignment: center; + QbzIcon { + source: @image-url("../assets/icons/folder.svg"); + width: 16px; + height: 16px; + tint: Theme.accent; + } + } + VerticalLayout { + alignment: center; + horizontal-stretch: 1; + Text { + text: sub.segment; + color: Theme.text-secondary; + font-size: Typography.body; + overflow: elide; + } + } + if sub.track-count > 0: VerticalLayout { + alignment: center; + Text { + text: sub.track-count; + color: Theme.text-muted; + font-size: Typography.legal; + } + } + } + sub-ta := TouchArea { + mouse-cursor: pointer; + clicked => { + LocalLibraryActions.folders-select(sub.path, sub.segment); + } + } + } + } + + // Direct tracks. + if !LocalLibraryState.folder-detail-loading + && LocalLibraryState.folder-detail-tracks.length > 0: VerticalLayout { + spacing: 0px; + for track[ti] in LocalLibraryState.folder-detail-tracks: TrackRow { + track: track; + index: ti; + show-artwork: false; + show-album: true; + show-favorite: false; + show-download: false; + show-source: true; + media-action(kind, id, action) => { + if (kind == "album" && action == "open") { + LocalLibraryActions.open-album(id); + } else if (kind == "artist" && action == "open") { + LocalLibraryActions.open-artist(id); + } else { + LocalLibraryActions.folders-play-track(id); + } + } + } + } + + // Folder has no direct tracks (only subfolders). + if !LocalLibraryState.folder-detail-loading + && LocalLibraryState.folder-detail-tracks.length == 0 + && LocalLibraryState.folder-detail-subfolders.length == 0: VerticalLayout { + padding-top: 24px; + Text { + text: @tr("This folder has no tracks."); + color: Theme.text-muted; + font-size: Typography.body; + horizontal-alignment: center; + } + } + } + } + ListScrollbar { + x: parent.width - self.width - 4px; + width: 14px; + height: 100%; + viewport-height: detail-flick.viewport-height; + visible-height: detail-flick.height; + viewport-y <=> detail-flick.viewport-y; + } + } } } } diff --git a/crates/qbz-slint/ui/state.slint b/crates/qbz-slint/ui/state.slint index f1564b3d6..7236ed81e 100644 --- a/crates/qbz-slint/ui/state.slint +++ b/crates/qbz-slint/ui/state.slint @@ -347,6 +347,18 @@ export struct LocalArtistItem { subtitle: string, } +// One visible row in the Folders tree (flattened-model lazy tree — Slint has +// no native TreeView). Folders expand to fetch children lazily. +export struct FolderNode { + path: string, // absolute filesystem path (the node id) + segment: string, // last path component (display) + depth: int, // indent level + is-folder: bool, // folder vs. track row + expanded: bool, // folder open? + can-expand: bool, // has children (track_count_under > 0) + track-count: int, // recursive track count under a folder +} + export global LocalLibraryState { // "albums" | "artists" | "folders" | "tracks" in-out property active-tab: "albums"; @@ -388,10 +400,29 @@ export global LocalLibraryState { in-out property tracks-next-offset: 0; in-out property tracks-search: ""; - // --- Folders tab (flat) — albums grouped by directory (full load) --- + // --- Folders tab --------------------------------------------------- + // flat = album grid grouped by directory; tree = two-pane lazy filesystem. + in-out property folders-view-mode: "flat"; // "flat" | "tree" + // Flat mode: `folders` is the full set (artwork target); `folders-visible` + // / `folders-grouped` are the search/sort/group-derived render sets. in property <[AlbumCardItem]> folders: []; in property folders-loading: false; in property folders-load-failed: false; + in property <[AlbumCardItem]> folders-visible: []; + in property <[DiscoverSection]> folders-grouped: []; + in-out property folders-search: ""; + in-out property folders-sort: "title-asc"; // title-asc|title-desc|artist-asc|year-desc + in-out property folders-group: "off"; // off | alpha | artist + in-out property folders-grid-view: "grid"; // grid | list + // Tree mode: a flattened visible-node list + the selected folder's detail + // (its direct tracks; subfolders shown as a grid). + in property <[FolderNode]> folder-tree: []; + in property folder-tree-loading: false; + in-out property folders-selected-path: ""; + in property folders-selected-name: ""; + in property <[FolderNode]> folder-detail-subfolders: []; + in property <[TrackItem]> folder-detail-tracks: []; + in property folder-detail-loading: false; // --- Artists tab — name + counts grid (full load; images later) ----- in property <[LocalArtistItem]> artists: []; @@ -429,6 +460,21 @@ export global LocalLibraryActions { callback tracks-retry(); // Per-row play/queue — local playback lands with the source-aware slice. callback track-action(string /* track id */, string /* action */); + + // --- Folders tab --- + callback folders-search(string /* query */); + callback folders-set-sort(string /* sort key */); + callback folders-set-group(string /* off|alpha|artist */); + callback folders-set-mode(string /* flat|tree */); + // Tree: expand/collapse a folder node (lazy child fetch on expand). + callback folders-toggle-node(string /* path */, bool /* now-expanded */); + // Tree: select a folder -> load its detail (direct tracks + subfolders). + callback folders-select(string /* path */, string /* segment */); + // Tree: play everything under a folder (recursive). + callback folders-play-node(string /* path */); + // Tree: play one track from the selected folder's direct tracks (the + // folder's direct list becomes the queue, starting at this track). + callback folders-play-track(string /* track id */); } // Global toast state, driven by crate::toast. The Toast overlay (mounted