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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use tao::event_loop::EventLoopProxy;
/// Sent from background threads to wake the event loop.
#[derive(Debug)]
pub enum UserEvent {
/// One or more watched files changed on disk.
FsChanged {
kind: FsChangeKind,
paths: Vec<PathBuf>,
},
/// Another attn invocation wants to open a new path.
OpenPath(PathBuf),
/// Switch to a project root and refresh sidebar content.
SwitchProject(PathBuf),
/// Request lazy loading of a folder's direct children.
LoadChildren(PathBuf),
/// A background scan for one directory's direct children completed.
ChildrenLoaded {
root: PathBuf,
parent: PathBuf,
children: Vec<crate::files::TreeNode>,
},
/// Take a screenshot and send the path back through the channel.
#[cfg(debug_assertions)]
Screenshot(std::sync::mpsc::Sender<String>),
/// Request daemon info (binary path, PID) and send back through the channel.
Info(std::sync::mpsc::Sender<String>),
/// Evaluate JavaScript and send the result back through the channel.
#[cfg(debug_assertions)]
Eval(String, std::sync::mpsc::Sender<String>),
/// Open webview devtools (debug builds only).
OpenDevtools,
/// The user started dragging a custom title bar region.
DragWindow,
/// Show and focus the main window.
ShowWindow,
/// Hide the main window.
HideWindow,
/// Increase global font scale (browser-style zoom in).
FontScaleIncrease,
/// Decrease global font scale (browser-style zoom out).
FontScaleDecrease,
/// Reset global font scale to default.
FontScaleReset,
/// Exit the app event loop.
Quit,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FsChangeKind {
Create,
Remove,
Modify,
Rename,
}
pub struct FileWatcher {
watcher: RecommendedWatcher,
watched_root: Option<PathBuf>,
}
/// Debounce interval — ignore duplicate events within this window.
const DEBOUNCE_MS: u128 = 100;
fn should_ignore_component(component: &str) -> bool {
if component.starts_with('.') {
return true;
}
matches!(
component,
"node_modules" | "target" | "dist" | "build" | "out" | "coverage" | "__pycache__" | "venv"
)
}
fn should_ignore_path(path: &Path) -> bool {
path.components().any(|component| {
component
.as_os_str()
.to_str()
.is_some_and(should_ignore_component)
})
}
impl FileWatcher {
/// Start watching `path` (directory-recursive, or parent directory for files).
/// Sends `UserEvent::FsChanged` through the proxy with changed paths,
/// with basic debouncing.
pub fn new(path: &Path, proxy: EventLoopProxy<UserEvent>) -> Result<Self, notify::Error> {
let last_event = Arc::new(Mutex::new(
Instant::now() - std::time::Duration::from_secs(1),
));
let watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
if let Ok(event) = res {
let change_kind = match &event.kind {
EventKind::Create(_) => FsChangeKind::Create,
EventKind::Remove(_) => FsChangeKind::Remove,
EventKind::Modify(notify::event::ModifyKind::Name(_)) => FsChangeKind::Rename,
EventKind::Modify(_) => FsChangeKind::Modify,
_ => {
return;
}
};
if !matches!(
event.kind,
EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_)
) {
return;
}
if event.paths.is_empty() {
return;
}
let filtered_paths: Vec<PathBuf> = event
.paths
.into_iter()
.filter(|path| !should_ignore_path(path))
.collect();
if filtered_paths.is_empty() {
return;
}
// Debounce: skip if last event was within the window
let Ok(mut last) = last_event.lock() else {
return;
};
let now = Instant::now();
if now.duration_since(*last).as_millis() < DEBOUNCE_MS {
return;
}
*last = now;
let _ = proxy.send_event(UserEvent::FsChanged {
kind: change_kind,
paths: filtered_paths,
});
}
})?;
let mut this = Self {
watcher,
watched_root: None,
};
this.update_root(path)?;
Ok(this)
}
/// Retarget the watcher to a new project root.
pub fn update_root(&mut self, path: &Path) -> Result<(), notify::Error> {
let watch_path = if path.is_file() {
path.parent().unwrap_or(path)
} else {
path
};
let next_root = watch_path.to_path_buf();
if self
.watched_root
.as_ref()
.is_some_and(|current| current == &next_root)
{
return Ok(());
}
if let Some(current) = &self.watched_root {
let _ = self.watcher.unwatch(current);
}
self.watcher.watch(&next_root, RecursiveMode::Recursive)?;
self.watched_root = Some(next_root);
Ok(())
}
}