Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Examples:
- **organise** (not organize)
- **behaviour** (not behavior)
- **favour** (not favor)
- **synchronisation** (not synchronization)

Code identifiers (variable names, CSS properties) should still respect platform and ecosystem conventions (e.g. `css: { color: 'red' }`), but comments and UI strings must use Canadian spelling (e.g. `// Update the colour`).

Expand Down
95 changes: 94 additions & 1 deletion apps/desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ sha2 = "0.10"
hex = "0.4"
unicode-segmentation = "1.10"
base64 = "0.22"
notify = "6.1.1"
notify-debouncer-mini = "0.4.1"
12 changes: 12 additions & 0 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use tauri::Manager;
mod vault;
mod settings;
mod plugins;
mod watcher;

use plugins::{PluginRegistry, tts::TtsPlugin};
use watcher::FileWatcher;

#[tauri::command]
fn get_linux_accent_colour() -> String {
Expand Down Expand Up @@ -82,6 +84,16 @@ pub fn run() {

app.manage(registry);

// Initialize watcher
let watcher = FileWatcher::new();
// Attempt to start if vault is already configured
if let Some(config) = vault::get_vault_config(app.handle().clone()) {
if let Err(e) = watcher.start(app.handle().clone(), std::path::PathBuf::from(config.root_path)) {
eprintln!("Failed to start watcher: {}", e);
}
}
app.manage(watcher);

if std::env::var("TAURI_FORCE_DEVTOOLS").is_ok() {
if let Some(main) = app.get_webview_window("main") {
main.open_devtools();
Expand Down
13 changes: 12 additions & 1 deletion apps/desktop/src-tauri/src/vault.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::watcher::FileWatcher;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Component, Path, PathBuf};
Expand Down Expand Up @@ -64,9 +65,19 @@ pub fn get_vault_config(app: AppHandle) -> Option<VaultConfig> {
#[tauri::command]
pub fn set_vault_config(app: AppHandle, root_path: String, name: String) -> Result<(), String> {
let config_path = get_config_path(&app)?;
let config = VaultConfig { root_path, name };
let config = VaultConfig {
root_path: root_path.clone(),
name,
};
let content = serde_json::to_string_pretty(&config).map_err(|e| e.to_string())?;
fs::write(config_path, content).map_err(|e| e.to_string())?;

// Restart watcher
let watcher = app.state::<FileWatcher>();
if let Err(e) = watcher.start(app.clone(), PathBuf::from(root_path)) {
eprintln!("Failed to start watcher: {}", e);
}

Ok(())
}

Expand Down
186 changes: 186 additions & 0 deletions apps/desktop/src-tauri/src/watcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
use notify::RecursiveMode;
use notify_debouncer_mini::{new_debouncer, DebounceEventResult};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tauri::{AppHandle, Emitter};
use walkdir::WalkDir;

// Wrapper struct to hold the watcher
pub struct FileWatcher {
debouncer: Arc<Mutex<Option<notify_debouncer_mini::Debouncer<notify::RecommendedWatcher>>>>,
known_paths: Arc<Mutex<HashSet<PathBuf>>>,
}

impl FileWatcher {
pub fn new() -> Self {
Self {
debouncer: Arc::new(Mutex::new(None)),
known_paths: Arc::new(Mutex::new(HashSet::new())),
}
}

pub fn start(&self, app: AppHandle, path: PathBuf) -> Result<(), String> {
// Stop existing watcher if any
self.stop();

let snapshot = build_known_paths_snapshot(&path);
{
let mut known_paths = self.known_paths.lock().unwrap();
*known_paths = snapshot;
}

let app_handle = app.clone();
let path_clone = path.clone();
let known_paths = self.known_paths.clone();

// 500ms debounce time
let mut debouncer = new_debouncer(
Duration::from_millis(500),
move |res: DebounceEventResult| match res {
Ok(events) => {
let mut known = known_paths.lock().unwrap();
for event in events {
handle_event(&app_handle, event.path, &path_clone, &mut known);
}
}
Err(e) => eprintln!("Watch error: {:?}", e),
},
)
.map_err(|e| format!("Failed to create watcher: {}", e))?;

debouncer
.watcher()
.watch(&path, RecursiveMode::Recursive)
.map_err(|e| format!("Failed to watch path: {}", e))?;

// Store the watcher
*self.debouncer.lock().unwrap() = Some(debouncer);

Ok(())
}

pub fn stop(&self) {
// Dropping the debouncer stops it
*self.debouncer.lock().unwrap() = None;
self.known_paths.lock().unwrap().clear();
}
}

fn handle_event(app: &AppHandle, path: PathBuf, root: &Path, known_paths: &mut HashSet<PathBuf>) {
if should_ignore(root, &path) {
return;
}

if path.exists() {
let event_name = if known_paths.insert(path.clone()) {
"vault:file-created"
} else {
"vault:file-modified"
};
emit_event(app, event_name, root, &path);
} else {
known_paths.remove(&path);
emit_event(app, "vault:file-deleted", root, &path);
}
}

fn should_ignore(root: &Path, path: &Path) -> bool {
let Ok(relative) = path.strip_prefix(root) else {
return true;
};

// Ignore .git, .liminal, etc.
for component in relative.components() {
if let Some(s) = component.as_os_str().to_str() {
if s.starts_with('.') && s != "." && s != ".." {
return true;
}
}
}
false
}

fn build_known_paths_snapshot(root: &Path) -> HashSet<PathBuf> {
let mut known_paths = HashSet::new();

for entry in WalkDir::new(root).into_iter().filter_map(Result::ok) {
let path = entry.path();
if path == root || should_ignore(root, path) {
continue;
}
known_paths.insert(path.to_path_buf());
}

known_paths
}

fn emit_event(app: &AppHandle, event_name: &str, root: &Path, full_path: &Path) {
// Convert to relative path
if let Ok(relative) = full_path.strip_prefix(root) {
let path_str = relative.to_string_lossy().replace("\\", "/");
let _ = app.emit(event_name, Payload { path: path_str });
}
}

#[derive(Clone, serde::Serialize)]
struct Payload {
path: String,
}

#[cfg(test)]
mod tests {
use super::{build_known_paths_snapshot, should_ignore};
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

#[test]
fn ignores_hidden_paths_inside_vault_root() {
let root = Path::new("/vault");
let path = Path::new("/vault/.git/config");
assert!(should_ignore(root, path));
}

#[test]
fn does_not_ignore_paths_when_root_itself_is_hidden() {
let root = Path::new("/home/user/.vault");
let path = Path::new("/home/user/.vault/notes/today.md");
assert!(!should_ignore(root, path));
}

#[test]
fn ignores_paths_outside_vault_root() {
let root = Path::new("/vault");
let path = Path::new("/elsewhere/note.md");
assert!(should_ignore(root, path));
}

#[test]
fn snapshot_excludes_hidden_entries() {
let temp_root = temp_dir("watcher-snapshot");

fs::create_dir_all(temp_root.join("notes")).unwrap();
fs::create_dir_all(temp_root.join(".git")).unwrap();
fs::write(temp_root.join("notes").join("visible.md"), "# visible").unwrap();
fs::write(temp_root.join(".git").join("config"), "hidden").unwrap();

let snapshot = build_known_paths_snapshot(&temp_root);
assert!(snapshot.contains(&temp_root.join("notes").join("visible.md")));
assert!(!snapshot.contains(&temp_root.join(".git").join("config")));

fs::remove_dir_all(temp_root).unwrap();
}

fn temp_dir(prefix: &str) -> PathBuf {
let suffix = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let path = std::env::temp_dir().join(format!("{prefix}-{suffix}"));
fs::create_dir_all(&path).unwrap();
path
}
}
Loading