Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): run window moves and restores in order, and pick the smoke's test window outright

Richie McIlroy6e2ab57612f4
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index 8485df406..3bc52cd11 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -2159,13 +2159,13 @@ fn restore_arranged_windows(cx: &mut App) {
if arranged.is_empty() {
return;
}
- cx.background_executor()
- .spawn(async move {
- for window in arranged {
- crate::foreign_window::put_back(&window);
- }
- })
- .detach();
+ // Queued behind any move still running, which would otherwise land
+ // after the restore. The job runs whether or not this is awaited.
+ let _ = crate::foreign_window::in_order(move || {
+ for window in arranged {
+ crate::foreign_window::put_back(&window);
+ }
+ });
}
pub fn dismiss_target_overlays(cx: &mut App) {
diff --git a/apps/desktop-gpui/src/foreign_window.rs b/apps/desktop-gpui/src/foreign_window.rs
index 45a7159ef..ca92db980 100644
--- a/apps/desktop-gpui/src/foreign_window.rs
+++ b/apps/desktop-gpui/src/foreign_window.rs
@@ -26,6 +26,39 @@ pub fn raise(id: &WindowId) -> bool {
imp::raise(id)
}
+type Job = Box<dyn FnOnce() + Send>;
+
+/// Run `job` after every job queued before it, on one worker thread. Moves
+/// and restores go through here so a cancel's restore can never land before
+/// a move the user started first.
+pub fn in_order<T: Send + 'static>(
+ job: impl FnOnce() -> T + Send + 'static,
+) -> impl std::future::Future<Output = Option<T>> {
+ static QUEUE: std::sync::OnceLock<flume::Sender<Job>> = std::sync::OnceLock::new();
+ let queue = QUEUE.get_or_init(|| {
+ let (sender, receiver) = flume::unbounded::<Job>();
+ let spawned = std::thread::Builder::new()
+ .name("cap-window-arrange".into())
+ .spawn(move || {
+ for job in receiver.iter() {
+ job();
+ }
+ });
+ if let Err(error) = spawned {
+ tracing::error!(%error, "could not start the window arrange thread");
+ }
+ sender
+ });
+ let (done, result) = flume::bounded(1);
+ let queued = queue.send(Box::new(move || {
+ let _ = done.send(job());
+ }));
+ async move {
+ queued.ok()?;
+ result.recv_async().await.ok()
+ }
+}
+
/// How a window sits beyond its bounds (maximized), where bounds alone
/// cannot put it back.
#[derive(Clone, Copy)]
@@ -373,3 +406,26 @@ mod imp {
send(window, b"_NET_ACTIVE_WINDOW", [2, 0, 0, 0, 0]).is_ok()
}
}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn queued_jobs_run_in_the_order_they_were_queued() {
+ let order = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
+ let slow = order.clone();
+ let first = super::in_order(move || {
+ std::thread::sleep(std::time::Duration::from_millis(50));
+ slow.lock().unwrap().push("move");
+ });
+ let fast = order.clone();
+ let second = super::in_order(move || fast.lock().unwrap().push("restore"));
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .build()
+ .unwrap();
+ runtime.block_on(async {
+ assert_eq!(second.await, Some(()));
+ assert_eq!(first.await, Some(()));
+ });
+ assert_eq!(*order.lock().unwrap(), ["move", "restore"]);
+ }
+}
diff --git a/apps/desktop-gpui/src/picker_probe.rs b/apps/desktop-gpui/src/picker_probe.rs
index d8d68dd0f..57e6a5d07 100644
--- a/apps/desktop-gpui/src/picker_probe.rs
+++ b/apps/desktop-gpui/src/picker_probe.rs
@@ -128,11 +128,25 @@ async fn probe_window_arrangement(
if !crate::foreign_window::can_arrange() {
return serde_json::json!({ "skipped": "cannot arrange other apps' windows here" });
}
+ // The workflow opens one of these; pick it outright rather than relying
+ // on where the runner left the cursor.
+ let test_window = scap_targets::Window::list().into_iter().find(|window| {
+ window.owner_name().is_some_and(|owner| {
+ let owner = owner.to_lowercase();
+ ["textedit", "notepad", "xterm"]
+ .iter()
+ .any(|name| owner.contains(name))
+ })
+ });
let picked = cx.update(|cx| {
- crate::target_overlay::TargetSelect::global(cx)
- .read(cx)
- .active_window()
- .cloned()
+ let select = crate::target_overlay::TargetSelect::global(cx);
+ if let Some(window) = test_window
+ .as_ref()
+ .and_then(crate::target_overlay::HoveredWindow::from_window)
+ {
+ select.update(cx, |select, cx| select.set_pinned(window, cx));
+ }
+ select.read(cx).active_window().cloned()
});
let Some(picked) = picked else {
return serde_json::json!({ "passed": false, "error": "no window to arrange" });
diff --git a/apps/desktop-gpui/src/target_overlay.rs b/apps/desktop-gpui/src/target_overlay.rs
index 3a6dfa61c..e5778522d 100644
--- a/apps/desktop-gpui/src/target_overlay.rs
+++ b/apps/desktop-gpui/src/target_overlay.rs
@@ -2232,29 +2232,24 @@ impl OverlayWindow {
let requested = id.clone();
let select = self.select.clone();
cx.spawn(async move |this, cx| {
- let moved = cx
- .background_executor()
- .spawn(async move {
- let arranged = match arrangement {
- Arrangement::Frame(frame) => crate::foreign_window::arrange(
- &id,
- &display,
- frame.scaled(capture_scale),
- ),
- Arrangement::PutBack(original) => {
- crate::foreign_window::put_back(&original)
- }
- };
- if raise {
- crate::foreign_window::raise(&id);
+ let moved = crate::foreign_window::in_order(move || {
+ let arranged = match arrangement {
+ Arrangement::Frame(frame) => {
+ crate::foreign_window::arrange(&id, &display, frame.scaled(capture_scale))
}
- let window = scap_targets::Window::from_id(&id);
- (
- arranged,
- window.as_ref().and_then(HoveredWindow::from_window),
- )
- })
- .await;
+ Arrangement::PutBack(original) => crate::foreign_window::put_back(&original),
+ };
+ if raise {
+ crate::foreign_window::raise(&id);
+ }
+ let window = scap_targets::Window::from_id(&id);
+ (
+ arranged,
+ window.as_ref().and_then(HoveredWindow::from_window),
+ )
+ })
+ .await
+ .unwrap_or((false, None));
let (arranged, fresh) = moved;
if !arranged {
tracing::warn!("the picked window could not be arranged");