1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Selecting rows: the cursor, the multi-selection and its shift-anchor, Select All,
//! and the paths Cut, Copy, Delete and drag act on.
use super::*;
impl FileTree {
/// Map a screen y coordinate to a node index, if any. Screen rows map
/// straight onto consecutive node indices from the scroll offset.
pub fn node_at_y(&self, y: u16) -> Option<usize> {
if y < self.last_inner.y || y >= self.last_inner.y + self.last_inner.height {
return None;
}
let row = (y - self.last_inner.y) as usize;
let idx = self.scroll + row;
(idx < self.nodes.len()).then_some(idx)
}
pub fn select(&mut self, idx: usize) {
if idx < self.nodes.len() {
self.selected = idx;
}
}
/// Single-select: clear the multi-selection, set both selected and the
/// shift-anchor to `idx`.
pub fn select_replace(&mut self, idx: usize) {
if idx >= self.nodes.len() {
return;
}
self.selected = idx;
self.anchor = idx;
self.marked.clear();
}
/// Toggle whether the node at `idx` is in the multi-selection. Moves
/// the cursor to `idx` and resets the shift-anchor to it (matching VS
/// Code's Cmd/Ctrl+click behaviour). When entering multi-select mode
/// from a single selection, the previously-focused path is also added
/// to the marked set so the original cursor row is not silently lost.
pub fn toggle_mark(&mut self, idx: usize) {
if idx >= self.nodes.len() {
return;
}
let entering_multi = self.marked.is_empty();
if entering_multi
&& let Some(prev) = self.nodes.get(self.selected).map(|n| n.path.clone())
&& self.selected != idx
{
self.marked.insert(prev);
}
let path = self.nodes[idx].path.clone();
if !self.marked.remove(&path) {
self.marked.insert(path);
}
self.selected = idx;
self.anchor = idx;
}
/// Replace the multi-selection with the inclusive range between the
/// current shift-anchor and `idx`. Moves the cursor to `idx`.
pub fn extend_to(&mut self, idx: usize) {
if idx >= self.nodes.len() {
return;
}
let anchor = self.anchor.min(self.nodes.len().saturating_sub(1));
let (lo, hi) = if anchor <= idx {
(anchor, idx)
} else {
(idx, anchor)
};
self.marked.clear();
for i in lo..=hi {
self.marked.insert(self.nodes[i].path.clone());
}
self.selected = idx;
}
pub fn clear_marks(&mut self) {
self.marked.clear();
self.anchor = self.selected;
}
pub fn is_marked(&self, idx: usize) -> bool {
self.nodes
.get(idx)
.is_some_and(|n| self.marked.contains(&n.path))
}
/// Mark every visible node (root included). Anchor goes to 0, selected
/// stays where it is so the user's caret doesn't jump.
pub fn select_all_visible(&mut self) {
self.marked.clear();
for n in &self.nodes {
self.marked.insert(n.path.clone());
}
self.anchor = 0;
}
/// Paths that should be acted on by Cut/Copy/Delete/Drag. If the
/// multi-selection is non-empty, returns the marked paths in tree
/// order plus the cursor row (so a Cmd-toggled row never excludes the
/// originally-focused row). Otherwise returns just the cursor path.
pub fn action_paths(&self) -> Vec<PathBuf> {
let Some(focused) = self.nodes.get(self.selected).map(|n| n.path.clone()) else {
return Vec::new();
};
if self.marked.is_empty() {
return vec![focused];
}
let mut paths: Vec<PathBuf> = self
.nodes
.iter()
.filter(|n| self.marked.contains(&n.path) || n.path == focused)
.map(|n| n.path.clone())
.collect();
paths.dedup();
paths
}
pub fn selected_path(&self) -> Option<&Path> {
self.nodes.get(self.selected).map(|n| n.path.as_path())
}
/// Drop multi-selection entries whose paths no longer exist in the
/// flattened node list (e.g. their parent folder was collapsed). Marks
/// always reference visible rows so a stale path can't accidentally
/// participate in Cut/Copy/Drag.
pub(super) fn prune_marks(&mut self) {
if self.marked.is_empty() {
return;
}
let visible: BTreeSet<PathBuf> = self.nodes.iter().map(|n| n.path.clone()).collect();
self.marked.retain(|p| visible.contains(p));
}
}