Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(linux): editor background image picker opens the portal file chooser (#3282)

Richie McIlroy2fc199a7f04d
diff --git a/.github/workflows/linux-desktop-reliability.yml b/.github/workflows/linux-desktop-reliability.yml
index eeb232f7c..892e339c2 100644
--- a/.github/workflows/linux-desktop-reliability.yml
+++ b/.github/workflows/linux-desktop-reliability.yml
@@ -210,6 +210,19 @@ jobs:
grep -qx main_raised=yes "$RUNNER_TEMP/out/gnome-raise-$window/result.txt"
done
+ - name: GNOME 50 editor background image from the portal file chooser
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: pick-background
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-pick-background" GNOME_UNSAFE=1 LAUNCH=dock APPROVE_SHORTCUT_AFTER=6
+ cat "$RUNNER_TEMP/out/gnome-pick-background/result.txt"
+ for line in recorded=yes editor_opened=yes chooser_opened=yes background_chosen=yes cap_alive=yes; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-pick-background/result.txt"
+ done
+
- name: Check results
run: |
bash scripts/linux-desktop-harness/check-results.sh \
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index 26f750e90..a14547076 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -5800,6 +5800,8 @@ async fn drive_auto_sidebar(handle: WindowHandle<EditorWindow>, cx: &mut gpui::A
let add_track = std::env::var("CAP_GPUI_AUTO_ADD_TRACK").ok();
let generate_zoom =
std::env::var("CAP_GPUI_AUTO_GENERATE_ZOOM").is_ok_and(|value| value == "1");
+ let pick_background =
+ std::env::var("CAP_GPUI_AUTO_PICK_BACKGROUND").is_ok_and(|value| value == "1");
if tab.is_none()
&& select.is_none()
&& canvas.is_none()
@@ -5808,6 +5810,7 @@ async fn drive_auto_sidebar(handle: WindowHandle<EditorWindow>, cx: &mut gpui::A
&& camera3d.is_none()
&& add_track.is_none()
&& !generate_zoom
+ && !pick_background
{
return;
}
@@ -5842,6 +5845,14 @@ async fn drive_auto_sidebar(handle: WindowHandle<EditorWindow>, cx: &mut gpui::A
.ok();
}
+ if pick_background {
+ handle
+ .update(cx, |view, window, cx| {
+ view.pick_background_image(window, cx)
+ })
+ .ok();
+ }
+
if let Some(spec) = select {
handle
.update(cx, |view, _window, cx| view.auto_select_segments(&spec, cx))
diff --git a/apps/desktop-gpui/src/editor_sidebar.rs b/apps/desktop-gpui/src/editor_sidebar.rs
index d3520ec29..0c8ee5fad 100644
--- a/apps/desktop-gpui/src/editor_sidebar.rs
+++ b/apps/desktop-gpui/src/editor_sidebar.rs
@@ -2423,7 +2423,7 @@ impl EditorWindow {
/// goes through a task rather than the click's update, and the chosen file
/// is copied into the app-data dir as `bg-<ts>-<name>` exactly as the
/// `<input type="file">` handler does (`:2513-2541`).
- fn pick_background_image(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+ pub(crate) fn pick_background_image(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.sidebar.picking_image {
return;
}
@@ -2431,6 +2431,14 @@ impl EditorWindow {
let style_target = self.sidebar.style_target;
let style_fingerprint = self.style_target_fingerprint();
self.sidebar.picker_task = Some(cx.spawn_in(window, async move |this, cx| {
+ #[cfg(target_os = "linux")]
+ let picked = crate::platform::open_file_panel_async(
+ &[("Image", &BACKGROUND_IMAGE_EXTENSIONS[..])],
+ None,
+ cx,
+ )
+ .await;
+ #[cfg(not(target_os = "linux"))]
let picked = cx
.update(|_, _| crate::platform::open_image_panel(&BACKGROUND_IMAGE_EXTENSIONS))
.ok()
@@ -2478,6 +2486,7 @@ impl EditorWindow {
}
match stored {
Ok(path) => {
+ tracing::info!(path = %path.display(), "background image chosen");
this.edit_background(
"image",
|project| {
diff --git a/apps/desktop-gpui/src/screenshot_editor.rs b/apps/desktop-gpui/src/screenshot_editor.rs
index c764e814d..617f91ecd 100644
--- a/apps/desktop-gpui/src/screenshot_editor.rs
+++ b/apps/desktop-gpui/src/screenshot_editor.rs
@@ -1537,7 +1537,16 @@ impl ScreenshotEditorWindow {
fn pick_background_image(&mut self, window: &mut Window, cx: &mut Context<Self>) {
cx.spawn_in(window, async move |this, cx| {
- let Some(path) = crate::platform::open_image_panel(&BACKGROUND_IMAGE_EXTENSIONS) else {
+ #[cfg(target_os = "linux")]
+ let picked = crate::platform::open_file_panel_async(
+ &[("Image", &BACKGROUND_IMAGE_EXTENSIONS[..])],
+ None,
+ cx,
+ )
+ .await;
+ #[cfg(not(target_os = "linux"))]
+ let picked = crate::platform::open_image_panel(&BACKGROUND_IMAGE_EXTENSIONS);
+ let Some(path) = picked else {
return;
};
this.update_in(cx, |this, window, cx| {
diff --git a/scripts/linux-desktop-harness/README.md b/scripts/linux-desktop-harness/README.md
index 3be777a53..e51fddb2d 100644
--- a/scripts/linux-desktop-harness/README.md
+++ b/scripts/linux-desktop-harness/README.md
@@ -45,6 +45,12 @@ onboarding, or the main window with `RAISE_WINDOW=main`, to the front.
window's own close button (GNOME draws no title bar for it). Clicks go through
`mutter-pointer.py`, one virtual pointer that exists before Cap starts: gpui
binds the seat's pointer once, so a pointer created per click never reaches it.
+`SCENARIO=pick-background` (with `GNOME_UNSAFE=1`) records a clip, opens it in
+the editor and presses the Image background's picker
+(`CAP_GPUI_AUTO_PICK_BACKGROUND=1`), then types a picture's path into the
+portal's file chooser through `mutter-keys.py --type` and presses Select.
+Mutter's RemoteDesktop drops the first key of each session, so `mutter-keys.py`
+leads every session with a Shift tap.
`report.sh <out-dir>` prints the results with Cap's own log lines. The
Hyprland image needs `modprobe vkms` on the host. With `GNOME_UNSAFE=1`,
diff --git a/scripts/linux-desktop-harness/mutter-keys.py b/scripts/linux-desktop-harness/mutter-keys.py
index 7ee3bd5e4..787ce77aa 100644
--- a/scripts/linux-desktop-harness/mutter-keys.py
+++ b/scripts/linux-desktop-harness/mutter-keys.py
@@ -3,6 +3,7 @@
keyboard reaches GNOME Shell (and so its global shortcut grabs).
Usage: mutter-keys.py Control_L+Shift_L+F9 [--hold-ms 120]
+ mutter-keys.py --type TEXT (printable ASCII, one key per character)
"""
import sys
import time
@@ -23,8 +24,14 @@ for n in range(1, 13):
KEYSYMS[f"F{n}"] = 0xFFBE + n - 1
+def keysym(key: str) -> int:
+ # A printable ASCII character's keysym is its code point.
+ return KEYSYMS[key] if key in KEYSYMS else ord(key)
+
+
def main() -> int:
- chord = sys.argv[1].split("+")
+ typing = sys.argv[1] == "--type"
+ chord = [] if typing else sys.argv[1].split("+")
hold = 0.12
if "--hold-ms" in sys.argv:
hold = int(sys.argv[sys.argv.index("--hold-ms") + 1]) / 1000
@@ -54,12 +61,27 @@ def main() -> int:
call("Start")
time.sleep(0.3)
+ # A fresh session can drop its first key, so a Shift tap nothing reacts
+ # to goes first.
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (KEYSYMS["Shift_L"], True)))
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (KEYSYMS["Shift_L"], False)))
+ time.sleep(0.05)
+ if typing:
+ for character in sys.argv[2]:
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (keysym(character), True)))
+ time.sleep(0.02)
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (keysym(character), False)))
+ time.sleep(0.03)
+ time.sleep(0.2)
+ call("Stop")
+ print(f"typed {sys.argv[2]!r}")
+ return 0
for key in chord:
- call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (KEYSYMS[key], True)))
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (keysym(key), True)))
time.sleep(0.03)
time.sleep(hold)
for key in reversed(chord):
- call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (KEYSYMS[key], False)))
+ call("NotifyKeyboardKeysym", GLib.Variant("(ub)", (keysym(key), False)))
time.sleep(0.03)
time.sleep(0.2)
call("Stop")
diff --git a/scripts/linux-desktop-harness/scenario-pick-background.sh b/scripts/linux-desktop-harness/scenario-pick-background.sh
new file mode 100755
index 000000000..c1e45c3fb
--- /dev/null
+++ b/scripts/linux-desktop-harness/scenario-pick-background.sh
@@ -0,0 +1,88 @@
+#!/usr/bin/env bash
+# The editor's background image, chosen the way a person chooses it: record a
+# Studio clip, open it in the editor, press the Image tab's "choose an image"
+# (CAP_GPUI_AUTO_PICK_BACKGROUND), type a picture's path into the desktop
+# portal's file chooser and press Return. GNOME only (needs GNOME_UNSAFE=1 for
+# the recording's dialogs). Prints key=value results to $HARNESS_OUT/result.txt.
+set -uo pipefail
+here=$(cd "$(dirname "$0")" && pwd)
+export XDG_RUNTIME_DIR=/run/user/$(id -u)
+export DBUS_SESSION_BUS_ADDRESS=unix:path=$XDG_RUNTIME_DIR/bus
+out=${HARNESS_OUT:-/out}
+
+bash "$here/scenario-start-stop.sh" >/dev/null
+recorded=$(sed -n 's/^attempt1_finished=//p' "$out/result.txt")
+mv "$out/result.txt" "$out/recording-result.txt"
+: >"$out/result.txt"
+result() { printf '%s=%s\n' "$1" "$2" >>"$out/result.txt"; }
+result recorded "${recorded:-no}"
+[[ $recorded == yes ]] || exit 0
+
+eval "$(systemctl --user show-environment | grep -E "^(WAYLAND_DISPLAY|XDG_SESSION_TYPE|XDG_CURRENT_DESKTOP|XDG_SESSION_DESKTOP|LIBGL_ALWAYS_SOFTWARE)=" | sed 's/^/export /')"
+picture=/tmp/cap-background.png
+python3 - "$picture" <<'PY'
+import sys
+
+import gi
+
+gi.require_version("GdkPixbuf", "2.0")
+from gi.repository import GdkPixbuf
+
+image = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, 320, 180)
+image.fill(0x3366CCFF)
+image.savev(sys.argv[1], "png", [], [])
+PY
+
+wait_log() {
+ for _ in $(seq $(($2 * 4))); do
+ grep -aq -- "$1" "$out/cap2.log" 2>/dev/null && return 0
+ sleep 0.25
+ done
+ return 1
+}
+
+bash "$here/launch-cap.sh" cap2 CAP_GPUI_AUTO_EDITOR=1 CAP_GPUI_AUTO_PICK_BACKGROUND=1 >/dev/null
+if ! wait_log "editor project validated" 60; then
+ result editor_opened no
+ exit 0
+fi
+result editor_opened yes
+gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval 'Main.overview.hide()' >/dev/null 2>&1
+
+chooser=no
+for _ in $(seq 60); do
+ if gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval "global.display.list_all_windows().some(w => /xdg-desktop-portal-gnome|org.gnome.Nautilus/i.test(w.get_wm_class() || ''))" 2>/dev/null |
+ grep -q "'true'"; then
+ chooser=yes
+ break
+ fi
+ sleep 0.5
+done
+result chooser_opened "$chooser"
+gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval \
+ "global.display.list_all_windows().map(w => (w.get_title() || '') + ' [' + (w.get_wm_class() || '') + ']').join(' | ')" >>"$out/actions.log" 2>&1
+if [[ $chooser == yes ]]; then
+ sleep 1
+ gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval \
+ "global.display.list_all_windows().filter(w => /xdg-desktop-portal-gnome|org.gnome.Nautilus/i.test(w.get_wm_class() || '')).forEach(w => w.activate(global.display.get_current_time_roundtrip()))" >/dev/null 2>&1
+ sleep 1
+ # Ctrl+L opens the location field; typing alone starts a search.
+ python3 "$here/mutter-keys.py" Control_L+l >>"$out/actions.log" 2>&1
+ sleep 0.5
+ python3 "$here/mutter-keys.py" --type "$picture" >>"$out/actions.log" 2>&1
+ sleep 1
+ bash "$here/shot.sh" "$out/chooser.png" >/dev/null 2>&1
+ # Return takes the completion; Select returns the file.
+ python3 "$here/mutter-keys.py" Return >>"$out/actions.log" 2>&1
+ sleep 1
+ python3 "$here/atspi-press.py" "Nautilus|Files|nautilus" "^Select$" 10 >>"$out/actions.log" 2>&1
+fi
+if wait_log "background image chosen" 20; then
+ result background_chosen yes
+else
+ result background_chosen no
+fi
+result cap_alive "$(kill -0 "$(cat "$XDG_RUNTIME_DIR/cap2.pid" 2>/dev/null)" 2>/dev/null && echo yes || echo no)"
+cat "$out/result.txt"