Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(desktop): populate tray history in the background

Richie McIlroyb79731d6e472
diff --git a/apps/desktop/src-tauri/src/tray.rs b/apps/desktop/src-tauri/src/tray.rs
index 7ad9835b9..562ac852c 100644
--- a/apps/desktop/src-tauri/src/tray.rs
+++ b/apps/desktop/src-tauri/src/tray.rs
@@ -158,12 +158,29 @@ struct CachedPreviousItem {
thumbnail_width: u32,
thumbnail_height: u32,
item_type: PreviousItemType,
- created_at: std::time::SystemTime,
}
#[derive(Default)]
struct PreviousItemsCache {
items: Vec<CachedPreviousItem>,
+ revision: u64,
+ loaded: bool,
+}
+
+impl PreviousItemsCache {
+ fn invalidate(&mut self) {
+ self.revision = self.revision.wrapping_add(1);
+ }
+
+ fn finish_loading(&mut self, revision: u64, items: Vec<CachedPreviousItem>) -> bool {
+ if self.revision != revision {
+ return false;
+ }
+ self.items = items;
+ self.loaded = true;
+ self.invalidate();
+ true
+ }
}
fn screenshots_path(app: &AppHandle) -> PathBuf {
@@ -229,11 +246,6 @@ fn load_single_item(
}
let meta = RecordingMeta::load_for_project(path).ok()?;
- let created_at = path
- .metadata()
- .and_then(|m| m.created())
- .unwrap_or_else(|_| std::time::SystemTime::now());
-
let is_screenshot = path.extension().and_then(|s| s.to_str()) == Some("cap")
&& path.parent().map(|p| p == screenshots_dir).unwrap_or(false);
@@ -273,12 +285,11 @@ fn load_single_item(
thumbnail_width,
thumbnail_height,
item_type,
- created_at,
})
}
fn load_all_previous_items(app: &AppHandle, load_thumbnails: bool) -> Vec<CachedPreviousItem> {
- let mut items = Vec::new();
+ let mut paths = Vec::new();
let screenshots_dir = screenshots_path(app);
for recordings_dir in crate::recordings_locations::known_recordings_dirs(app) {
@@ -286,9 +297,7 @@ fn load_all_previous_items(app: &AppHandle, load_thumbnails: bool) -> Vec<Cached
continue;
};
for entry in entries.flatten() {
- if let Some(item) = load_single_item(&entry.path(), &screenshots_dir, load_thumbnails) {
- items.push(item);
- }
+ paths.push(entry.path());
}
}
@@ -297,17 +306,66 @@ fn load_all_previous_items(app: &AppHandle, load_thumbnails: bool) -> Vec<Cached
{
for entry in entries.flatten() {
let path = entry.path();
- if path.extension().and_then(|s| s.to_str()) == Some("cap")
- && let Some(item) = load_single_item(&path, &screenshots_dir, load_thumbnails)
- {
- items.push(item);
+ if path.extension().and_then(|s| s.to_str()) == Some("cap") {
+ paths.push(path);
}
}
}
- items.sort_by_key(|b| std::cmp::Reverse(b.created_at));
- items.truncate(MAX_PREVIOUS_ITEMS);
- items
+ newest_valid_items(paths, MAX_PREVIOUS_ITEMS, |path| {
+ load_single_item(path, &screenshots_dir, load_thumbnails)
+ })
+}
+
+fn newest_valid_items<T>(
+ paths: Vec<PathBuf>,
+ limit: usize,
+ mut load: impl FnMut(&PathBuf) -> Option<T>,
+) -> Vec<T> {
+ let mut candidates = paths
+ .into_iter()
+ .filter_map(|path| {
+ let metadata = path.metadata().ok()?;
+ metadata.is_dir().then(|| {
+ let created = metadata
+ .created()
+ .unwrap_or_else(|_| std::time::SystemTime::now());
+ (path, created)
+ })
+ })
+ .collect::<Vec<_>>();
+ candidates.sort_by_key(|(_, created)| std::cmp::Reverse(*created));
+ candidates
+ .into_iter()
+ .filter_map(|(path, _)| load(&path))
+ .take(limit)
+ .collect()
+}
+
+fn load_previous_items_in_background(app: AppHandle, cache: Arc<Mutex<PreviousItemsCache>>) {
+ tokio::spawn(async move {
+ if !crate::startup::wait_for_window(&app).await {
+ return;
+ }
+ let worker_app = app.clone();
+ let worker_cache = cache.clone();
+ let result = tokio::task::spawn_blocking(move || {
+ while !crate::app_is_exiting(&worker_app) {
+ let revision = worker_cache.lock().unwrap().revision;
+ let items = load_all_previous_items(&worker_app, true);
+ if worker_cache.lock().unwrap().finish_loading(revision, items) {
+ return;
+ }
+ }
+ })
+ .await;
+ if let Err(error) = result {
+ tracing::warn!(%error, "Failed to load tray history");
+ }
+ if !crate::app_is_exiting(&app) {
+ refresh_tray_menu(&app, &cache);
+ }
+ });
}
fn create_previous_submenu(
@@ -319,7 +377,11 @@ fn create_previous_submenu(
submenu.append(&MenuItem::with_id(
app,
"previous_empty",
- "No recent items",
+ if cache.loaded {
+ "No recent items"
+ } else {
+ "Loading recent items…"
+ },
false,
None::<&str>,
)?)?;
@@ -574,6 +636,7 @@ fn add_new_item_to_cache(cache: &Arc<Mutex<PreviousItemsCache>>, app: &AppHandle
cache_guard.items.insert(0, new_item);
cache_guard.items.truncate(MAX_PREVIOUS_ITEMS);
+ cache_guard.invalidate();
}
fn refresh_tray_menu(app: &AppHandle, cache: &Arc<Mutex<PreviousItemsCache>>) {
@@ -754,8 +817,7 @@ fn handle_mode_selection(
}
pub fn create_tray(app: &AppHandle) -> tauri::Result<()> {
- let items = load_all_previous_items(app, false);
- let cache = Arc::new(Mutex::new(PreviousItemsCache { items }));
+ let cache = Arc::new(Mutex::new(PreviousItemsCache::default()));
app.manage(TrayMenuCache {
cache: cache.clone(),
@@ -995,49 +1057,6 @@ pub fn create_tray(app: &AppHandle) -> tauri::Result<()> {
tracing::warn!("Failed to initialize Linux tray icon: {error}");
}
- {
- let app_clone = app.clone();
- let cache_clone = cache.clone();
- std::thread::spawn(move || {
- let screenshots_dir = screenshots_path(&app_clone);
- let items_needing_thumbnails: Vec<PathBuf> = {
- let cache_guard = cache_clone.lock().unwrap();
- cache_guard
- .items
- .iter()
- .filter(|item| item.thumbnail.is_none())
- .map(|item| item.path.clone())
- .collect()
- };
-
- if items_needing_thumbnails.is_empty() {
- return;
- }
-
- for path in items_needing_thumbnails {
- if let Some(updated_item) = load_single_item(&path, &screenshots_dir, true) {
- let mut cache_guard = cache_clone.lock().unwrap();
- if let Some(existing) = cache_guard.items.iter_mut().find(|i| i.path == path) {
- existing.thumbnail = updated_item.thumbnail;
- existing.thumbnail_width = updated_item.thumbnail_width;
- existing.thumbnail_height = updated_item.thumbnail_height;
- }
- }
- }
-
- let app_for_refresh = app_clone.clone();
- let cache_for_refresh = cache_clone.clone();
- let _ = app_clone.run_on_main_thread(move || {
- if let Some(tray) = app_for_refresh.tray_by_id("tray") {
- let cache_guard = cache_for_refresh.lock().unwrap();
- if let Ok(menu) = build_tray_menu(&app_for_refresh, &cache_guard) {
- let _ = tray.set_menu(Some(menu));
- }
- }
- });
- });
- }
-
RecordingStarted::listen_any(&app, {
let app = app.clone();
let is_recording = is_recording.clone();
@@ -1112,13 +1131,28 @@ pub fn create_tray(app: &AppHandle) -> tauri::Result<()> {
// Rebuild once when a storage-folder migration finishes: moved
// projects changed paths, so cached entries are stale.
if event.payload.done == event.payload.total {
- let items = load_all_previous_items(&app_handle, false);
- cache_clone.lock().unwrap().items = items;
- refresh_tray_menu(&app_handle, &cache_clone);
+ cache_clone.lock().unwrap().invalidate();
+ load_previous_items_in_background(app_handle.clone(), cache_clone.clone());
}
}
});
+ crate::RecordingDeleted::listen_any(&app, {
+ let app = app.clone();
+ let cache = cache.clone();
+ move |event| {
+ {
+ let mut cache = cache.lock().unwrap();
+ cache.items.retain(|item| item.path != event.payload.path);
+ cache.invalidate();
+ }
+ refresh_tray_menu(&app, &cache);
+ load_previous_items_in_background(app.clone(), cache.clone());
+ }
+ });
+
+ load_previous_items_in_background(app.clone(), cache.clone());
+
Ok(())
}
@@ -1131,6 +1165,51 @@ pub(crate) fn clean_stop_icon() -> Result<cap_utils::linux_recording_stop::StopT
cap_utils::linux_recording_stop::StopTrayIcon::from_rgba(32, 32, image.as_raw())
}
+#[cfg(test)]
+mod startup_history_tests {
+ use super::*;
+
+ #[test]
+ fn history_load_reads_only_enough_valid_metadata_for_the_menu() {
+ let directory = tempfile::tempdir().unwrap();
+ let paths = (0..100)
+ .map(|index| {
+ let path = directory.path().join(format!("{index}.cap"));
+ std::fs::create_dir(&path).unwrap();
+ path
+ })
+ .collect::<Vec<_>>();
+ let mut reads = 0;
+ let loaded = newest_valid_items(paths.clone(), 6, |_| {
+ reads += 1;
+ (reads > 2).then_some(reads)
+ });
+ assert_eq!(reads, 8);
+ assert_eq!(loaded, vec![3, 4, 5, 6, 7, 8]);
+
+ let mut reference = paths.clone();
+ reference
+ .sort_by_key(|path| std::cmp::Reverse(path.metadata().unwrap().created().unwrap()));
+ reference.truncate(6);
+ assert_eq!(
+ newest_valid_items(paths, 6, |path| Some(path.clone())),
+ reference
+ );
+ }
+
+ #[test]
+ fn a_late_history_scan_cannot_overwrite_new_items_or_moved_folders() {
+ let mut cache = PreviousItemsCache::default();
+ let old_revision = cache.revision;
+ cache.invalidate();
+ assert!(!cache.finish_loading(old_revision, Vec::new()));
+ assert!(!cache.loaded);
+ assert!(cache.finish_loading(cache.revision, Vec::new()));
+ assert!(cache.loaded);
+ assert!(!cache.finish_loading(old_revision, Vec::new()));
+ }
+}
+
#[cfg(target_os = "linux")]
pub(crate) async fn set_clean_stop_mode(
app: &AppHandle,