Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): wait for pending window moves before recording and put moved windows back on every non-recording exit

Richie McIlroyd6c80f60b648
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index daf91a106..8ee10de1d 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -916,6 +916,7 @@ pub(crate) fn show_main_window_after_capture_pause(cx: &mut App) {
windows.main_hidden_for_picker || !windows.overlays.is_empty()
};
if picker_open {
+ restore_arranged_windows(cx);
close_target_overlays(cx);
}
cx.global_mut::<AppWindows>().main_hidden_for_picker = false;
@@ -1097,6 +1098,7 @@ pub fn request_close_main(cx: &mut App) {
}
close_camera_window(cx);
+ restore_arranged_windows(cx);
close_target_overlays(cx);
// Closing the main window explicitly is not a picker dismissal; a stale
// flag here would reveal the window the user just closed.
@@ -2146,30 +2148,27 @@ fn open_overlays_core(request: OverlayRequest, cx: &mut App) -> bool {
true
}
+/// Put back every window the picker moved or resized: the picker closed
+/// without recording them.
+fn restore_arranged_windows(cx: &mut App) {
+ let arranged = TargetSelect::global(cx).update(cx, |select, _| select.take_arranged());
+ crate::foreign_window::put_back_all(arranged, cx.background_executor());
+}
+
+/// A recording or screenshot is about to capture the windows the picker
+/// shaped, so they stay where they are.
+fn keep_arranged_windows(cx: &mut App) {
+ TargetSelect::global(cx).update(cx, |select, _| {
+ select.take_arranged();
+ });
+}
+
/// Close the overlays and clear the main window's armed target -- Escape, the
/// overlay's own close button, or the main window toggling the mode off.
///
/// A "cancelled" dismissal in the Tauri vocabulary, which is the kind that
/// reveals the main window again (`dismissalReveals`,
/// `new-main/index.tsx:2047-2058`).
-/// Put back every window the picker moved or resized. Off the main thread:
-/// `SetWindowPos` waits on the target app, which may be busy.
-fn restore_arranged_windows(cx: &mut App) {
- let arranged = TargetSelect::global(cx).update(cx, |select, _| select.take_arranged());
- if arranged.is_empty() {
- return;
- }
- // Queued behind any move still running, which would otherwise land
- // after the restore.
- cx.background_executor()
- .spawn(crate::foreign_window::in_order(move || {
- for window in arranged {
- crate::foreign_window::put_back(&window);
- }
- }))
- .detach();
-}
-
pub fn dismiss_target_overlays(cx: &mut App) {
restore_arranged_windows(cx);
if RecordingSession::global(cx).read(cx).phase == Phase::Idle
@@ -2437,7 +2436,31 @@ pub fn show_instant_sign_in(kind: crate::main_window::TargetType, cx: &mut App)
show_main_window(cx);
}
+/// How long Start waits for the picker's window moves to land. A move waits
+/// on the target app, and a hung app must not hold the recording forever.
+const ARRANGE_SETTLE_LIMIT: std::time::Duration = std::time::Duration::from_millis(1500);
+
pub fn start_recording_from_overlay(target: ScreenCaptureTarget, cx: &mut App) {
+ if !TargetSelect::global(cx).read(cx).has_arranged() {
+ start_recording_from_overlay_now(target, cx);
+ return;
+ }
+ // A size the user just chose may still be on its way to the window, and
+ // the capture reads the window's bounds when it starts.
+ let settled = crate::foreign_window::in_order(|| ());
+ cx.spawn(async move |cx| {
+ let limit = cx.background_executor().timer(ARRANGE_SETTLE_LIMIT);
+ futures_util::future::select(std::pin::pin!(settled), std::pin::pin!(limit)).await;
+ cx.update(|cx| {
+ if !cx.global::<AppWindows>().overlays.is_empty() {
+ start_recording_from_overlay_now(target, cx);
+ }
+ });
+ })
+ .detach();
+}
+
+fn start_recording_from_overlay_now(target: ScreenCaptureTarget, cx: &mut App) {
let main = cx.global::<AppWindows>().main;
if RecordingSession::global(cx).read(cx).phase != Phase::Idle
|| main
@@ -2446,10 +2469,7 @@ pub fn start_recording_from_overlay(target: ScreenCaptureTarget, cx: &mut App) {
{
return;
}
- // The user shaped the window to record it: it stays where it is.
- TargetSelect::global(cx).update(cx, |select, _| {
- select.take_arranged();
- });
+ keep_arranged_windows(cx);
// Collected before the windows go away: the capture starts a beat after
// this, and an overlay that has not finished closing must not end up in
// the recording.
@@ -3959,6 +3979,7 @@ fn begin_recording_after_preflight(config: StartConfig, cx: &mut App) {
if session.read(cx).phase != Phase::Idle {
return;
}
+ keep_arranged_windows(cx);
// `start_recording`'s first act (`src-tauri/src/recording.rs:1492-1494`):
// an armed editor recording target forces Studio, whatever mode the main
@@ -6681,6 +6702,7 @@ pub fn abort_editor_recording_flow(cx: &mut App) {
}) else {
return;
};
+ restore_arranged_windows(cx);
close_target_overlays(cx);
cx.global_mut::<AppWindows>().editor_hidden_for_picker = None;
cx.global_mut::<AppWindows>().main_hidden_for_picker = false;
@@ -6871,6 +6893,7 @@ pub fn prepare_for_screenshot_capture(cx: &mut App) -> bool {
// The screenshot branch of `onRecordingStart` nulls the saved camera bounds
// just like the recording branch does, so a parked bubble stays parked.
release_camera_park(cx);
+ keep_arranged_windows(cx);
close_target_overlays(cx);
true
}
diff --git a/apps/desktop-gpui/src/foreign_window.rs b/apps/desktop-gpui/src/foreign_window.rs
index ca92db980..5f267e130 100644
--- a/apps/desktop-gpui/src/foreign_window.rs
+++ b/apps/desktop-gpui/src/foreign_window.rs
@@ -68,6 +68,24 @@ pub fn placement(id: &WindowId) -> Option<Placement> {
imp::placement(id).map(Placement)
}
+/// Put `windows` back off the main thread (a move waits on the target app),
+/// queued behind any move still running so it cannot land after them.
+pub fn put_back_all(
+ windows: Vec<crate::target_overlay::ArrangedWindow>,
+ executor: &gpui::BackgroundExecutor,
+) {
+ if windows.is_empty() {
+ return;
+ }
+ executor
+ .spawn(in_order(move || {
+ for window in windows {
+ put_back(&window);
+ }
+ }))
+ .detach();
+}
+
pub fn put_back(original: &crate::target_overlay::ArrangedWindow) -> bool {
let window = &original.window;
if let Some(placement) = original.placement
diff --git a/apps/desktop-gpui/src/picker_probe.rs b/apps/desktop-gpui/src/picker_probe.rs
index 57e6a5d07..77150e955 100644
--- a/apps/desktop-gpui/src/picker_probe.rs
+++ b/apps/desktop-gpui/src/picker_probe.rs
@@ -196,27 +196,47 @@ async fn probe_window_arrangement(
(rect.x + rect.width / 2. - width / 2.).abs() < width * 0.04
});
screenshot(output, "window-focus", cx).await;
+ let near = |a: Option<AreaRect>, b: Option<AreaRect>| {
+ a.zip(b).is_some_and(|(a, b)| {
+ (a.x - b.x).abs() < 6.
+ && (a.y - b.y).abs() < 6.
+ && (a.width - b.width).abs() < 6.
+ && (a.height - b.height).abs() < 6.
+ })
+ };
overlay
.update(cx, |view, _, cx| view.set_focus(false, cx))
.ok();
- sleep(cx, 600).await;
+ sleep(cx, 1500).await;
+ let unfocused = read_window(&picked.id);
+ let unfocused_ok = !near(unfocused, focused) || near(focused, before);
+ // Dismissed with the window still moved: only the dismissal can put it
+ // back.
+ overlay
+ .update(cx, |view, _, cx| view.set_focus(true, cx))
+ .ok();
+ sleep(cx, 1500).await;
+ let moved = !near(read_window(&picked.id), before);
cx.update(app_windows::dismiss_target_overlays);
sleep(cx, 1500).await;
let restored = read_window(&picked.id);
- let restored_ok = restored.zip(before).is_some_and(|(now, then)| {
- (now.x - then.x).abs() < 6.
- && (now.y - then.y).abs() < 6.
- && (now.width - then.width).abs() < 6.
- && (now.height - then.height).abs() < 6.
- });
+ let restored_ok = moved && near(restored, before);
serde_json::json!({
- "passed": ratio_ok && inside(shaped) && centred && inside(focused) && restored_ok,
+ "passed": ratio_ok
+ && inside(shaped)
+ && centred
+ && inside(focused)
+ && unfocused_ok
+ && restored_ok,
"before": rect_json(before),
"sixteenByNine": rect_json(shaped),
"ratioOk": ratio_ok,
"focused": rect_json(focused),
"centred": centred,
+ "unfocused": rect_json(unfocused),
+ "unfocusedOk": unfocused_ok,
+ "movedBeforeDismiss": moved,
"restored": rect_json(restored),
"restoredOk": restored_ok,
})
diff --git a/apps/desktop-gpui/src/target_overlay.rs b/apps/desktop-gpui/src/target_overlay.rs
index 3676949c8..87e43a103 100644
--- a/apps/desktop-gpui/src/target_overlay.rs
+++ b/apps/desktop-gpui/src/target_overlay.rs
@@ -364,7 +364,9 @@ impl TargetSelect {
self.recording_mode = recording_mode;
self.pinned_window = pinned_window;
if self.mode.is_none() && mode.is_some() {
- self.arranged.clear();
+ // Left over from a picker that closed some way that neither put
+ // them back nor recorded them.
+ crate::foreign_window::put_back_all(self.take_arranged(), cx.background_executor());
self.focus_window = saved_focus();
}
if self.mode == mode {
@@ -415,6 +417,10 @@ impl TargetSelect {
self.arranged.get(&id.to_string())
}
+ pub fn has_arranged(&self) -> bool {
+ !self.arranged.is_empty()
+ }
+
/// Every window the picker moved, with where it was, forgetting them.
pub fn take_arranged(&mut self) -> Vec<ArrangedWindow> {
self.arranged.drain().map(|(_, window)| window).collect()