1use notify::{Error, Event, Watcher};
2
3pub struct State {
4 path: String,
5 title: String,
6 cold_start: bool,
7 rx: std::sync::mpsc::Receiver<Result<Event, Error>>,
8 _watcher: notify::RecommendedWatcher,
9}
10
11impl State {
12 pub fn new(path: String, title: String) -> Self {
13 let path_t = std::path::PathBuf::from(&path);
14 let (tx, rx) = std::sync::mpsc::channel();
15 let mut watcher = notify::RecommendedWatcher::new(tx, notify::Config::default()).unwrap();
16 watcher
17 .watch(&path_t, notify::RecursiveMode::Recursive)
18 .unwrap();
19
20 Self {
21 path,
22 title,
23 cold_start: true,
24 rx,
25 _watcher: watcher,
26 }
27 }
28
29 pub fn has_file_changed(&mut self) -> bool {
31 if self.cold_start {
32 self.cold_start = false;
33 return true;
34 }
35 let mut has_changed = false;
36 while let Ok(event) = self.rx.try_recv() {
37 if let Ok(Event {
38 kind: notify::EventKind::Modify(_),
39 ..
40 }) = event
41 {
42 has_changed = true;
43 }
44 }
45 has_changed
46 }
47
48 pub fn title(&self) -> String {
49 self.title.clone()
50 }
51
52 pub fn path(&self) -> String {
53 self.path.clone()
54 }
55}