Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(desktop-gpui): probe how the picker overlays stack against the camera

Richie McIlroy83c1e0d185e9
diff --git a/.github/workflows/picker-smoke.yml b/.github/workflows/picker-smoke.yml
new file mode 100644
index 000000000..2cb6d47ce
--- /dev/null
+++ b/.github/workflows/picker-smoke.yml
@@ -0,0 +1,134 @@
+name: Picker smoke (real app)
+# Opens the real GPUI app's target picker on macOS, Windows and Linux (X11
+# with a window manager) through `CAP_PICKER_PROBE_OUTPUT` and checks how the
+# picker's windows stack against the camera bubble.
+
+on:
+ pull_request:
+ types: [opened, synchronize, reopened, ready_for_review]
+ paths:
+ - "apps/desktop-gpui/src/target_overlay.rs"
+ - "apps/desktop-gpui/src/app_windows.rs"
+ - "apps/desktop-gpui/src/camera_window.rs"
+ - "apps/desktop-gpui/src/picker_probe.rs"
+ - "apps/desktop-gpui/src/platform.rs"
+ - "apps/desktop-gpui/src/platform/**"
+ - "scripts/picker-smoke/**"
+ - ".github/workflows/picker-smoke.yml"
+ workflow_dispatch:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: picker-smoke-${{ github.event.pull_request.number || github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ smoke:
+ name: Picker smoke (${{ matrix.settings.name }})
+ timeout-minutes: 60
+ strategy:
+ fail-fast: false
+ matrix:
+ settings:
+ - name: macOS
+ slug: macos
+ target: aarch64-apple-darwin
+ runner: blacksmith-6vcpu-macos-15
+ - name: Windows
+ slug: windows
+ target: x86_64-pc-windows-msvc
+ runner: blacksmith-8vcpu-windows-2025
+ - name: Linux X11
+ slug: linux-x11
+ target: x86_64-unknown-linux-gnu
+ runner: blacksmith-8vcpu-ubuntu-2404
+ runs-on: ${{ matrix.settings.runner }}
+ env:
+ CARGO_PROFILE_DEV_DEBUG: "0"
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Install Linux desktop dependencies
+ if: ${{ runner.os == 'Linux' }}
+ uses: ./.github/actions/install-desktop-deps
+
+ - name: Install a window manager and a test window
+ if: ${{ runner.os == 'Linux' }}
+ run: sudo apt-get install -y openbox xterm
+
+ - name: Rust setup
+ uses: dtolnay/[email protected]
+ with:
+ targets: ${{ matrix.settings.target }}
+
+ - uses: ./.github/actions/setup-rust-cache
+ with:
+ target: ${{ matrix.settings.target }}
+ prefix-key: v1-rust-gpui
+ save-cache: "false"
+ workspaces: apps/desktop-gpui -> target
+
+ - name: Run setup
+ shell: bash
+ run: node scripts/setup.js
+ env:
+ RUST_TARGET_TRIPLE: ${{ matrix.settings.target }}
+
+ - name: Prepare GPUI dependency
+ shell: bash
+ run: |
+ node scripts/sync-desktop-versions.mjs
+ ./scripts/prepare-gpui-dependency.sh
+
+ - name: Build the app
+ shell: bash
+ run: |
+ cargo +1.95.0 build --manifest-path apps/desktop-gpui/Cargo.toml --locked \
+ --target ${{ matrix.settings.target }} --bin cap-gpui
+ if [[ "$RUNNER_OS" == "macOS" ]]; then
+ frameworks="$GITHUB_WORKSPACE/apps/desktop-gpui/target/${{ matrix.settings.target }}/debug/Frameworks"
+ mkdir -p "$frameworks"
+ ln -sfn "$GITHUB_WORKSPACE/target/Frameworks/Spacedrive.framework" "$frameworks/Spacedrive.framework"
+ fi
+
+ - name: Picker probe
+ shell: bash
+ run: |
+ app="$GITHUB_WORKSPACE/apps/desktop-gpui/target/${{ matrix.settings.target }}/debug/cap-gpui"
+ out="$RUNNER_TEMP/picker-smoke"
+ case "$RUNNER_OS" in
+ macOS)
+ caffeinate -u -t 2
+ open -a TextEdit
+ sleep 2
+ scripts/picker-smoke/run.sh "$app" "$out"
+ ;;
+ Windows)
+ export PATH="$(cygpath "$GITHUB_WORKSPACE/target/debug"):$PATH"
+ notepad.exe &
+ sleep 2
+ scripts/picker-smoke/run.sh "$app.exe" "$out"
+ ;;
+ Linux)
+ export LD_LIBRARY_PATH="$GITHUB_WORKSPACE/target/native-deps/lib:$GITHUB_WORKSPACE/target/debug${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
+ Xvfb :99 -screen 0 1920x1080x24 -noreset &
+ export DISPLAY=:99
+ sleep 1
+ openbox &
+ sleep 1
+ xterm -geometry 100x30+300+200 &
+ sleep 1
+ scripts/picker-smoke/run.sh "$app" "$out"
+ ;;
+ esac
+
+ - name: Upload logs
+ if: always()
+ uses: actions/upload-artifact@v4
+ with:
+ name: picker-smoke-${{ matrix.settings.slug }}
+ path: ${{ runner.temp }}/picker-smoke
+ retention-days: 7
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index db32a0fa6..448506de1 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -69,6 +69,8 @@ mod permissions_ui;
#[cfg(test)]
mod picker_benchmark;
#[cfg(debug_assertions)]
+mod picker_probe;
+#[cfg(debug_assertions)]
mod picker_ui_benchmark;
mod platform;
mod presets;
@@ -806,6 +808,8 @@ fn main() {
// synthetic clicks are dropped.
#[cfg(debug_assertions)]
picker_ui_benchmark::run(window_handle, cx);
+ #[cfg(debug_assertions)]
+ picker_probe::run(window_handle, cx);
if let Ok(auto) = std::env::var("CAP_GPUI_AUTO_RECORD")
&& let Some((mode, secs)) = parse_auto_record(&auto)
{
diff --git a/apps/desktop-gpui/src/picker_probe.rs b/apps/desktop-gpui/src/picker_probe.rs
new file mode 100644
index 000000000..108ec8c49
--- /dev/null
+++ b/apps/desktop-gpui/src/picker_probe.rs
@@ -0,0 +1,241 @@
+use std::time::Duration;
+
+use gpui::{App, AsyncApp, WindowHandle};
+
+use crate::{
+ app_windows,
+ main_window::{MainWindow, TargetType},
+ target_overlay::AreaRect,
+};
+
+/// `CAP_PICKER_PROBE_OUTPUT=<path>`: open the camera bubble, arm each picker
+/// mode, activate an overlay the way a click does, and write the native
+/// front-to-back order of Cap's windows so CI can check the camera stays on
+/// top.
+pub fn run(main: WindowHandle<MainWindow>, cx: &mut App) {
+ let Some(output) = std::env::var_os("CAP_PICKER_PROBE_OUTPUT") else {
+ return;
+ };
+ cx.spawn(async move |cx| {
+ let delay = std::env::var("CAP_PICKER_PROBE_DELAY_MS")
+ .ok()
+ .and_then(|delay| delay.parse().ok())
+ .unwrap_or(2000);
+ sleep(cx, delay).await;
+ cx.update(app_windows::open_camera_window);
+ sleep(cx, 800).await;
+
+ let mut modes = Vec::new();
+ for (label, kind) in [
+ ("display", TargetType::Display),
+ ("window", TargetType::Window),
+ ("area", TargetType::Area),
+ ] {
+ main.update(cx, |view, _, cx| view.arm_overlay(kind, cx))
+ .ok();
+ sleep(cx, 800).await;
+ if kind == TargetType::Area {
+ cx.update(|cx| {
+ app_windows::seed_area_selection(
+ None,
+ AreaRect {
+ x: 200.,
+ y: 200.,
+ width: 640.,
+ height: 360.,
+ },
+ cx,
+ )
+ });
+ }
+ cx.update(|cx| {
+ for (_, overlay) in cx.global::<app_windows::AppWindows>().overlays.clone() {
+ overlay
+ .update(cx, |_, window, _| window.activate_window())
+ .ok();
+ }
+ });
+ sleep(cx, 800).await;
+ modes.push(cx.update(|cx| probe_mode(label, cx)));
+ cx.update(app_windows::dismiss_target_overlays);
+ sleep(cx, 400).await;
+ }
+
+ let passed = modes
+ .iter()
+ .all(|mode| mode["cameraAboveOverlays"].as_bool() == Some(true));
+ let report = serde_json::json!({ "passed": passed, "modes": modes });
+ if let Err(error) = std::fs::write(output, report.to_string()) {
+ tracing::error!(%error, "Could not write picker probe results");
+ }
+ cx.update(|cx| cx.quit());
+ })
+ .detach();
+}
+
+async fn sleep(cx: &mut AsyncApp, millis: u64) {
+ cx.background_executor()
+ .timer(Duration::from_millis(millis))
+ .await;
+}
+
+fn probe_mode(label: &str, cx: &mut App) -> serde_json::Value {
+ let windows = cx.global::<app_windows::AppWindows>();
+ let camera = windows.camera;
+ let overlays = windows.overlays.clone();
+ let camera_id = camera.and_then(|camera| {
+ camera
+ .update(cx, |_, window, _| native_id(window))
+ .ok()
+ .flatten()
+ });
+ let overlay_ids: Vec<isize> = overlays
+ .iter()
+ .filter_map(|(_, overlay)| {
+ overlay
+ .update(cx, |_, window, _| native_id(window))
+ .ok()
+ .flatten()
+ })
+ .collect();
+ let stacking = own_window_stacking();
+ let position = |id: isize| stacking.iter().position(|current| *current == id);
+ let camera_position = camera_id.and_then(position);
+ let overlay_positions: Vec<Option<usize>> =
+ overlay_ids.iter().map(|id| position(*id)).collect();
+ let camera_above = camera_position.is_some_and(|camera| {
+ !overlay_positions.is_empty()
+ && overlay_positions
+ .iter()
+ .all(|overlay| overlay.is_some_and(|overlay| camera < overlay))
+ });
+ serde_json::json!({
+ "mode": label,
+ "camera": camera_id,
+ "overlays": overlay_ids,
+ "stackingFrontToBack": stacking,
+ "cameraAboveOverlays": camera_above,
+ })
+}
+
+#[cfg(target_os = "macos")]
+fn native_id(window: &gpui::Window) -> Option<isize> {
+ crate::platform::window_number(window)
+}
+
+#[cfg(target_os = "windows")]
+fn native_id(window: &gpui::Window) -> Option<isize> {
+ crate::platform::window_number(window)
+}
+
+#[cfg(target_os = "linux")]
+fn native_id(window: &gpui::Window) -> Option<isize> {
+ use raw_window_handle::{HasWindowHandle, RawWindowHandle};
+
+ match HasWindowHandle::window_handle(window).ok()?.as_raw() {
+ RawWindowHandle::Xlib(handle) => isize::try_from(handle.window).ok(),
+ RawWindowHandle::Xcb(handle) => isize::try_from(handle.window.get()).ok(),
+ _ => None,
+ }
+}
+
+#[cfg(target_os = "macos")]
+fn own_window_stacking() -> Vec<isize> {
+ use core_foundation::array::{CFArray, CFArrayRef};
+ use core_foundation::base::{CFType, TCFType};
+ use core_foundation::dictionary::CFDictionary;
+ use core_foundation::number::CFNumber;
+ use core_foundation::string::{CFString, CFStringRef};
+
+ #[link(name = "CoreGraphics", kind = "framework")]
+ unsafe extern "C" {
+ fn CGWindowListCopyWindowInfo(option: u32, relative_to: u32) -> CFArrayRef;
+ static kCGWindowOwnerPID: CFStringRef;
+ static kCGWindowNumber: CFStringRef;
+ }
+ const ON_SCREEN_ONLY: u32 = 1 << 0;
+
+ let list = unsafe {
+ let raw = CGWindowListCopyWindowInfo(ON_SCREEN_ONLY, 0);
+ if raw.is_null() {
+ return Vec::new();
+ }
+ CFArray::<CFDictionary<CFString, CFType>>::wrap_under_create_rule(raw)
+ };
+ let (owner_key, number_key) = unsafe {
+ (
+ CFString::wrap_under_get_rule(kCGWindowOwnerPID),
+ CFString::wrap_under_get_rule(kCGWindowNumber),
+ )
+ };
+ let pid = i64::from(std::process::id());
+ list.iter()
+ .filter_map(|window| {
+ let field = |key: &CFString| window.find(key)?.downcast::<CFNumber>()?.to_i64();
+ (field(&owner_key)? == pid).then_some(())?;
+ isize::try_from(field(&number_key)?).ok()
+ })
+ .collect()
+}
+
+#[cfg(target_os = "windows")]
+fn own_window_stacking() -> Vec<isize> {
+ use windows_sys::Win32::{
+ Foundation::{BOOL, HWND, LPARAM, TRUE},
+ UI::WindowsAndMessaging::{EnumWindows, GetWindowThreadProcessId, IsWindowVisible},
+ };
+
+ unsafe extern "system" fn collect(window: HWND, data: LPARAM) -> BOOL {
+ let windows = unsafe { &mut *(data as *mut Vec<isize>) };
+ let mut owner = 0;
+ unsafe { GetWindowThreadProcessId(window, &mut owner) };
+ if owner == std::process::id() && unsafe { IsWindowVisible(window) } != 0 {
+ windows.push(window as isize);
+ }
+ TRUE
+ }
+
+ let mut windows = Vec::new();
+ unsafe {
+ EnumWindows(Some(collect), std::ptr::addr_of_mut!(windows) as isize);
+ }
+ windows
+}
+
+#[cfg(target_os = "linux")]
+fn own_window_stacking() -> Vec<isize> {
+ use x11rb::connection::Connection as _;
+ use x11rb::protocol::xproto::{AtomEnum, ConnectionExt as _};
+
+ let Ok((connection, screen)) = x11rb::connect(None) else {
+ return Vec::new();
+ };
+ let root = connection.setup().roots[screen].root;
+ let stacking = connection
+ .intern_atom(false, b"_NET_CLIENT_LIST_STACKING")
+ .ok()
+ .and_then(|cookie| cookie.reply().ok())
+ .and_then(|atom| {
+ connection
+ .get_property(false, root, atom.atom, AtomEnum::WINDOW, 0, u32::MAX)
+ .ok()?
+ .reply()
+ .ok()
+ })
+ .and_then(|reply| reply.value32().map(|values| values.collect::<Vec<u32>>()))
+ .filter(|values| !values.is_empty());
+ let bottom_to_top = match stacking {
+ Some(values) => values,
+ None => connection
+ .query_tree(root)
+ .ok()
+ .and_then(|cookie| cookie.reply().ok())
+ .map(|tree| tree.children)
+ .unwrap_or_default(),
+ };
+ bottom_to_top
+ .into_iter()
+ .rev()
+ .filter_map(|window| isize::try_from(window).ok())
+ .collect()
+}
diff --git a/scripts/picker-smoke/run.sh b/scripts/picker-smoke/run.sh
new file mode 100755
index 000000000..7241a4981
--- /dev/null
+++ b/scripts/picker-smoke/run.sh
@@ -0,0 +1,40 @@
+#!/usr/bin/env bash
+# Runs cap-gpui's picker probe (src/picker_probe.rs) in a throwaway profile and
+# fails unless every check in its report passed.
+#
+# scripts/picker-smoke/run.sh <cap-gpui binary> <out dir>
+set -euo pipefail
+
+APP=$1
+OUT=$2
+mkdir -p "$OUT/profile/recordings" "$OUT/profile/logs"
+cat > "$OUT/profile/store" <<'JSON'
+{"general_settings":{"hasCompletedOnboarding":true,"hasCompletedStartup":true,"enableNotifications":false,"enableTelemetry":false,"recordingCountdown":0},"recording_start_safety":{"confirmBeforeRecordingWithoutMicrophone":false}}
+JSON
+report="$OUT/report.json"
+rm -f "$report"
+
+env CAP_GPUI_APP_DATA_DIR="$OUT/profile" \
+ CAP_GPUI_RECORDINGS_DIR="$OUT/profile/recordings" \
+ CAP_GPUI_LOG_DIR="$OUT/profile/logs" \
+ CAP_GPUI_AUTO_NO_MIC=1 \
+ CAP_PICKER_PROBE_OUTPUT="$report" \
+ RUST_LOG="${RUST_LOG:-cap_gpui=info}" \
+ "$APP" > "$OUT/app.log" 2>&1 &
+pid=$!
+
+for _ in $(seq 1 240); do
+ [ -s "$report" ] && break
+ kill -0 "$pid" 2>/dev/null || break
+ sleep 0.5
+done
+kill "$pid" 2>/dev/null || true
+
+if [ ! -s "$report" ]; then
+ echo "picker probe wrote no report" >&2
+ tail -n 80 "$OUT/app.log" >&2 || true
+ exit 1
+fi
+cat "$report"
+echo
+grep -q '"passed":true' "$report"