Skip to main content

dioxus_swdir_tree_core/
config.rs

1//! Widget configuration: display filter modes and the per-tree settings.
2
3use std::path::PathBuf;
4
5use crate::entry::LoadedEntry;
6
7/// Which scanned entries become visible tree nodes.
8///
9/// Switching the mode at runtime via
10/// [`crate::DirectoryTree::set_filter`] is instant and issues **zero
11/// I/O**: child lists are re-derived from the raw entries kept in the
12/// [`crate::TreeCache`].
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
14pub enum DisplayFilter {
15    /// Non-hidden directories only. Note that a *hidden directory* is
16    /// hidden under this mode too — the mode is "folders only", not
17    /// "everything that is a folder".
18    FoldersOnly,
19    /// Non-hidden files and directories. The default.
20    #[default]
21    FilesAndFolders,
22    /// Everything the scan returned, hidden entries included.
23    AllIncludingHidden,
24}
25
26impl DisplayFilter {
27    /// `true` if `entry` survives this filter mode.
28    ///
29    /// The filter applies to children only; the root node is always
30    /// visible regardless of mode.
31    pub fn admits(self, entry: &LoadedEntry) -> bool {
32        match self {
33            Self::FoldersOnly => entry.is_dir && !entry.is_hidden,
34            Self::FilesAndFolders => !entry.is_hidden,
35            Self::AllIncludingHidden => true,
36        }
37    }
38}
39
40/// Basenames skipped by the prefetch heuristic (S8.5) — directories
41/// whose names match any entry here (ASCII case-insensitive) are never
42/// speculatively scanned.
43///
44/// This list only affects **prefetch**; user-initiated scans always
45/// proceed regardless of the skip list.
46pub const DEFAULT_PREFETCH_SKIP: &[&str] = &[
47    ".git",
48    ".hg",
49    ".svn",
50    "node_modules",
51    "__pycache__",
52    ".venv",
53    "venv",
54    "target",
55    "build",
56    "dist",
57];
58
59/// Settings fixed at construction or mutated by the application at
60/// runtime.
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub struct TreeConfig {
63    /// The mounted root. The tree never navigates above it.
64    pub root_path: PathBuf,
65    /// Active display filter.
66    pub filter: DisplayFilter,
67    /// Maximum load depth measured in components below the root;
68    /// `None` is unbounded. `Some(0)` means only the root's direct
69    /// children are ever loaded.
70    pub max_depth: Option<u32>,
71    /// How many direct folder-children to prefetch after each
72    /// user-initiated scan. `0` (the default) disables prefetch
73    /// entirely (S8.1).
74    pub prefetch_per_parent: u32,
75    /// Basenames to skip during prefetch target selection (S8.5).
76    /// Defaults to [`DEFAULT_PREFETCH_SKIP`].
77    pub prefetch_skip: Vec<String>,
78}
79
80impl TreeConfig {
81    /// Configuration with default filter, unbounded depth, and
82    /// prefetch disabled.
83    pub fn new(root_path: impl Into<PathBuf>) -> Self {
84        Self {
85            root_path: root_path.into(),
86            filter: DisplayFilter::default(),
87            max_depth: None,
88            prefetch_per_parent: 0,
89            prefetch_skip: DEFAULT_PREFETCH_SKIP
90                .iter()
91                .map(|s| s.to_string())
92                .collect(),
93        }
94    }
95}