Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): hold quit until a pending Studio stop thumbnail is written

Richie McIlroy01e832d40e3d
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index 720d5b548..dd23420cf 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -553,6 +553,7 @@ pub fn init(main: WindowHandle<MainWindow>, session: Entity<RecordingSession>, c
cx.update(|cx| {
crate::tray::refresh_previous(cx);
refresh_library_after_delete(cx);
+ crate::menus::recheck_pending_quit(cx);
});
}
})
diff --git a/apps/desktop-gpui/src/menus.rs b/apps/desktop-gpui/src/menus.rs
index 8e3996369..c8e2bed70 100644
--- a/apps/desktop-gpui/src/menus.rs
+++ b/apps/desktop-gpui/src/menus.rs
@@ -428,10 +428,11 @@ pub fn quit(cx: &mut App) {
}
let phase = session.read(cx).phase;
let ui_cleanup_pending = app_windows::clean_capture_owned(cx);
+ let finishing = ui_cleanup_pending || crate::recording::studio_thumbnails_pending();
let Some((generation, action)) = cx
.global_mut::<QuitCoordinator>()
.gate
- .request(quit_phase(phase, ui_cleanup_pending))
+ .request(quit_phase(phase, finishing))
else {
return;
};
@@ -457,9 +458,23 @@ pub fn quit(cx: &mut App) {
}
}
+pub(crate) fn recheck_pending_quit(cx: &mut App) {
+ let Some(generation) = cx
+ .try_global::<QuitCoordinator>()
+ .and_then(|coordinator| coordinator.gate.pending)
+ .map(|(generation, _)| generation)
+ else {
+ return;
+ };
+ advance_pending_quit(generation, false, cx);
+}
+
fn advance_pending_quit(generation: u64, timed_out: bool, cx: &mut App) {
let session = RecordingSession::global(cx);
- let phase = quit_phase(session.read(cx).phase, app_windows::clean_capture_owned(cx));
+ let phase = quit_phase(
+ session.read(cx).phase,
+ app_windows::clean_capture_owned(cx) || crate::recording::studio_thumbnails_pending(),
+ );
let failed = session.read(cx).error.is_some();
let action = cx
.global_mut::<QuitCoordinator>()
diff --git a/apps/desktop-gpui/src/recording.rs b/apps/desktop-gpui/src/recording.rs
index e7ab1dd66..e5da9a597 100644
--- a/apps/desktop-gpui/src/recording.rs
+++ b/apps/desktop-gpui/src/recording.rs
@@ -474,11 +474,12 @@ async fn finalize_studio(
}
let project_path = completed.project_path.clone();
+ let pending = PendingStudioThumbnail::new();
tokio::task::spawn_blocking(move || {
+ let _pending = pending;
let thumbnail_started = std::time::Instant::now();
if let Some(display_path) = studio_display_path(&project_path) {
write_bundle_thumbnail(&project_path, &display_path);
- let _ = studio_thumbnail_channel().0.send(());
}
tracing::info!(
elapsed_ms = thumbnail_started.elapsed().as_secs_f64() * 1000.0,
@@ -1451,8 +1452,28 @@ fn studio_display_path(project_path: &std::path::Path) -> Option<PathBuf> {
path.is_file().then_some(path)
}
-/// A Studio stop returns before its thumbnail is written, so the library views
-/// that scanned on the way to idle rescan once it lands.
+static PENDING_STUDIO_THUMBNAILS: std::sync::atomic::AtomicUsize =
+ std::sync::atomic::AtomicUsize::new(0);
+
+/// A Studio stop returns before its thumbnail is written: quit waits while one
+/// is pending, and the library views that scanned on the way to idle rescan
+/// once it lands.
+struct PendingStudioThumbnail;
+
+impl PendingStudioThumbnail {
+ fn new() -> Self {
+ PENDING_STUDIO_THUMBNAILS.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
+ Self
+ }
+}
+
+impl Drop for PendingStudioThumbnail {
+ fn drop(&mut self) {
+ PENDING_STUDIO_THUMBNAILS.fetch_sub(1, std::sync::atomic::Ordering::AcqRel);
+ let _ = studio_thumbnail_channel().0.send(());
+ }
+}
+
fn studio_thumbnail_channel() -> &'static (flume::Sender<()>, flume::Receiver<()>) {
static CHANNEL: std::sync::OnceLock<(flume::Sender<()>, flume::Receiver<()>)> =
std::sync::OnceLock::new();
@@ -1463,6 +1484,10 @@ pub(crate) fn studio_thumbnail_events() -> flume::Receiver<()> {
studio_thumbnail_channel().1.clone()
}
+pub(crate) fn studio_thumbnails_pending() -> bool {
+ PENDING_STUDIO_THUMBNAILS.load(std::sync::atomic::Ordering::Acquire) > 0
+}
+
/// Write `<bundle>/screenshots/display.jpg` -- the file both apps' Recents
/// cards draw. `cap-recording` does not produce it, so without this a project
/// recorded here would show the icon fallback forever, including in the
@@ -4023,6 +4048,7 @@ mod studio_finalization_tests {
.await
.unwrap()
.unwrap();
+ assert!(!studio_thumbnails_pending());
let thumbnail = image::open(crate::library::bundle_thumbnail_path(&root))
.unwrap()
.into_rgb8();