Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(linux): record with visible controls when there is no tray or Stop shortcut (#3273)

Richie McIlroy3045e3b848cc
diff --git a/.github/workflows/linux-desktop-reliability.yml b/.github/workflows/linux-desktop-reliability.yml
index d1f23dd34..6ba6abe2c 100644
--- a/.github/workflows/linux-desktop-reliability.yml
+++ b/.github/workflows/linux-desktop-reliability.yml
@@ -140,6 +140,23 @@ jobs:
grep -qx attempt1_control=shortcut "$RUNNER_TEMP/out/gnome-terminal/result.txt"
grep -qx app_id_required=0 "$RUNNER_TEMP/out/gnome-terminal/result.txt"
+ # Fedora's GNOME without the AppIndicator extension, launched where the
+ # portal grants no shortcut: no tray and no Stop shortcut. Cap records
+ # with its controls visible instead of refusing to start.
+ - name: GNOME 50 without a tray or a shortcut records with visible controls
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-no-control" LAUNCH=terminal GNOME_TRAY=0 CAP_GPUI_PORTAL_APP_ID=0 \
+ GNOME_UNSAFE=1
+ cat "$RUNNER_TEMP/out/gnome-no-control/result.txt"
+ for line in attempt1_control=visible visible_controls_shown=yes attempt1_finished=yes blocked=0 \
+ restoration_unconfirmed=0 panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-no-control/result.txt"
+ done
+
- name: GNOME 50 settings window closes from its own button
env:
DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index 30bc16d12..4c0e1cc97 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -279,6 +279,9 @@ struct CleanCaptureUi {
retained_windows: Vec<(gpui::AnyWindowHandle, bool)>,
restoring: bool,
restoration_error: Option<String>,
+ /// A start that had no Stop control to hide behind: it runs with Cap's
+ /// controls visible once the windows this lease hid are back.
+ visible_fallback: Option<StartConfig>,
}
#[cfg(target_os = "linux")]
@@ -3216,7 +3219,12 @@ pub fn handle_owned_clean_capture_shortcut(
}
#[cfg(target_os = "linux")]
-pub fn clean_capture_stop_unavailable(generation: u64, error: String, cx: &mut App) {
+pub fn clean_capture_stop_unavailable(
+ generation: u64,
+ error: String,
+ stop_was_ready: bool,
+ cx: &mut App,
+) {
if !clean_capture_generation_matches(generation, cx) {
return;
}
@@ -3230,6 +3238,29 @@ pub fn clean_capture_stop_unavailable(generation: u64, error: String, cx: &mut A
}
let session = RecordingSession::global(cx);
if session.read(cx).phase == Phase::Idle {
+ if let Some(config) = visible_fallback_start(&error, stop_was_ready, cx) {
+ // Without a tray or a granted shortcut, nothing could stop a
+ // recording that hides Cap. Record with the controls on screen
+ // instead of not at all.
+ tracing::warn!(%error, "no recording Stop control is available; recording with Cap's controls visible");
+ let wayland = cx
+ .global::<AppWindows>()
+ .clean_capture
+ .as_ref()
+ .is_some_and(|lease| lease.wayland);
+ if wayland {
+ if let Some(lease) = cx.global_mut::<AppWindows>().clean_capture.as_mut() {
+ lease.visible_fallback = Some(config);
+ }
+ cancel_clean_capture(cx);
+ } else {
+ cancel_clean_capture(cx);
+ if cx.global::<AppWindows>().clean_capture.is_none() {
+ begin_recording_after_preflight(config, cx);
+ }
+ }
+ return;
+ }
cancel_clean_capture(cx);
report_clean_capture_error(anyhow::Error::msg(error), cx);
} else {
@@ -3240,6 +3271,32 @@ pub fn clean_capture_stop_unavailable(generation: u64, error: String, cx: &mut A
}
}
+/// The start a Stop control failure can still record, with visible controls:
+/// only before the control ever worked, when its registration was cleaned up,
+/// and not for a recording appended to an open editor.
+#[cfg(target_os = "linux")]
+fn visible_fallback_start(error: &str, stop_was_ready: bool, cx: &mut App) -> Option<StartConfig> {
+ if !visible_fallback_allowed(error, stop_was_ready)
+ || RecordingSession::global(cx)
+ .read(cx)
+ .editor_recording_target()
+ .is_some()
+ {
+ return None;
+ }
+ cx.global_mut::<AppWindows>()
+ .clean_capture
+ .as_mut()
+ .and_then(|lease| lease.config.take())
+}
+
+#[cfg(target_os = "linux")]
+fn visible_fallback_allowed(error: &str, stop_was_ready: bool) -> bool {
+ !stop_was_ready
+ && !error.contains("could not be cleaned up safely")
+ && !error.contains("cleanup is unconfirmed")
+}
+
#[cfg(target_os = "linux")]
fn defer_window_until_capture_safe(cx: &mut App) -> bool {
if !cx
@@ -3308,6 +3365,7 @@ fn prepare_clean_capture(config: StartConfig, cx: &mut App) -> anyhow::Result<()
wayland,
restoring: false,
restoration_error: None,
+ visible_fallback: None,
retained_windows: if wayland {
vec![(main.into(), main_was_visible)]
} else {
@@ -3579,13 +3637,19 @@ fn begin_retained_capture_restore(cx: &mut App) {
Ok(()) => {
tracing::info!("recording UI restored after a clean capture");
crate::hotkeys::complete_clean_capture_stop(generation, cx);
- let _restored = cx.global_mut::<AppWindows>().clean_capture.take();
+ let restored = cx.global_mut::<AppWindows>().clean_capture.take();
update_camera_presentation(true, cx);
RecordingSession::global(cx).update(cx, |_, cx| cx.notify());
+ if let Some(config) = restored.and_then(|lease| lease.visible_fallback) {
+ begin_recording_after_preflight(config, cx);
+ }
}
Err(error) => {
if let Some(lease) = cx.global_mut::<AppWindows>().clean_capture.as_mut() {
lease.restoration_error = Some(error.clone());
+ // A later retry only brings the windows back; it does
+ // not start a recording nobody is asking for any more.
+ lease.visible_fallback = None;
}
tracing::error!(%error, "Recording UI restoration or Stop cleanup is unconfirmed");
RecordingSession::global(cx).update(cx, |session, cx| {
@@ -6940,6 +7004,33 @@ mod tests {
use super::*;
use crate::store::{DEFAULT_EXCLUDED_WINDOW_TITLES, WindowExclusion, default_excluded_windows};
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn a_start_without_any_stop_control_records_with_visible_controls() {
+ // No tray watcher and no granted shortcut, before either ever worked.
+ assert!(visible_fallback_allowed(
+ "Portal request failed: org.freedesktop.portal.Error.NotAllowed: An app id is required",
+ false,
+ ));
+ assert!(visible_fallback_allowed(
+ "The recording Stop worker ended",
+ false
+ ));
+ // A control that worked and then vanished keeps the existing stop.
+ assert!(!visible_fallback_allowed(
+ "The recording Stop tray is no longer available",
+ true
+ ));
+ // A registration that may still be live is not left behind.
+ assert!(!visible_fallback_allowed(
+ "The recording shortcut registration could not be cleaned up safely: timeout",
+ false,
+ ));
+ assert!(!visible_fallback_allowed(
+ "Recording Stop tray registration cleanup is unconfirmed: timeout",
+ false,
+ ));
+ }
#[test]
fn small_display_keeps_editor_and_minimum_size_inside_the_work_area() {
let available = inset_work_area(Bounds {
@@ -7216,6 +7307,7 @@ mod tests {
retained_windows: Vec::new(),
restoring: false,
restoration_error: None,
+ visible_fallback: None,
}
}
diff --git a/apps/desktop-gpui/src/hotkeys.rs b/apps/desktop-gpui/src/hotkeys.rs
index 850d3464b..54331af3a 100644
--- a/apps/desktop-gpui/src/hotkeys.rs
+++ b/apps/desktop-gpui/src/hotkeys.rs
@@ -430,21 +430,26 @@ fn reserve_wayland_stop(generation: u64, cx: &mut App) -> anyhow::Result<()> {
);
}
WaylandStopEvent::Unavailable(error) => {
+ let was_ready = stop.ready;
stop.ready = false;
- app_windows::clean_capture_stop_unavailable(generation, error, cx);
+ app_windows::clean_capture_stop_unavailable(
+ generation, error, was_ready, cx,
+ );
}
_ => {}
}
});
}
cx.update(|cx| {
- if cx
+ if let Some(was_ready) = cx
.try_global::<WaylandStop>()
- .is_some_and(|stop| stop.generation == generation && stop.cleanup.is_none())
+ .filter(|stop| stop.generation == generation && stop.cleanup.is_none())
+ .map(|stop| stop.ready)
{
app_windows::clean_capture_stop_unavailable(
generation,
"The recording Stop worker ended".into(),
+ was_ready,
cx,
);
}
@@ -487,15 +492,37 @@ async fn run_wayland_stop(
if let Err(error) = result {
tracing::info!(%error, "Using the recording Stop tray instead of GlobalShortcuts");
}
- let result = run_tray_stop(generation, &cancel, &events)
- .await
- .map_err(|error| error.to_string());
- if let Err(error) = &result {
- let _ = events.send(WaylandStopEvent::Unavailable(error.clone()));
+ match run_tray_stop(generation, &cancel, &events).await {
+ Ok(()) => Ok(()),
+ Err(error) => {
+ let _ = events.send(WaylandStopEvent::Unavailable(error.to_string()));
+ // A tray that never registered left nothing to clean up; failing
+ // here would block every later recording behind a cleanup error.
+ if error.is::<StopTrayNeverRegistered>() {
+ Ok(())
+ } else {
+ Err(error.to_string())
+ }
+ }
+ }
+}
+
+/// The recording Stop tray could not register at all (no StatusNotifier
+/// watcher, as on GNOME without the AppIndicator extension).
+#[cfg(target_os = "linux")]
+#[derive(Debug)]
+struct StopTrayNeverRegistered(String);
+
+#[cfg(target_os = "linux")]
+impl std::fmt::Display for StopTrayNeverRegistered {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str(&self.0)
}
- result
}
+#[cfg(target_os = "linux")]
+impl std::error::Error for StopTrayNeverRegistered {}
+
#[cfg(target_os = "linux")]
fn wayland_stop_icon() -> anyhow::Result<cap_utils::linux_recording_stop::StopTrayIcon> {
let image = image::load_from_memory(include_bytes!("../assets/tray/tray-stop-icon.png"))?
@@ -526,10 +553,11 @@ async fn run_tray_stop(
events: &flume::Sender<WaylandStopEvent>,
) -> anyhow::Result<()> {
use cap_utils::linux_recording_stop::{StopTray, StopTrayEvent};
- let icon = wayland_stop_icon()?;
+ let icon = wayland_stop_icon()
+ .map_err(|error| anyhow::Error::new(StopTrayNeverRegistered(error.to_string())))?;
let tray = StopTray::open(generation, icon).await.map_err(|error| {
if error.can_fallback() {
- anyhow::Error::msg(error)
+ anyhow::Error::new(StopTrayNeverRegistered(error.to_string()))
} else {
anyhow::anyhow!("Recording Stop tray registration cleanup is unconfirmed: {error}")
}
diff --git a/scripts/linux-desktop-harness/README.md b/scripts/linux-desktop-harness/README.md
index 5ce305aa1..57d63f11b 100644
--- a/scripts/linux-desktop-harness/README.md
+++ b/scripts/linux-desktop-harness/README.md
@@ -27,6 +27,11 @@ Each run (`run-desktop.sh <desktop> <package> <out-dir> [KEY=VALUE...]`):
`mutter-pointer.py`);
4. writes `result.txt`, which `check-results.sh` turns into pass or fail.
+`GNOME_TRAY=0` runs GNOME in its plain session without the AppIndicator
+extension and without the harness's own tray host, as Fedora ships it. With
+`CAP_GPUI_PORTAL_APP_ID=0` and `LAUNCH=terminal` the portal grants no shortcut
+either, and the scenario expects Cap to record with its controls visible
+(`attempt1_control=visible`): it finds the controls window and clicks their red Stop button with the pointer (needs `GNOME_UNSAFE=1`).
`SCENARIO=reopen` launches Cap from a terminal and again from the app grid,
which must hand over to the running instance. `SCENARIO=raise` (with
`GNOME_UNSAFE=1`) focuses Cap's settings, then has GNOME Shell launch Cap the
diff --git a/scripts/linux-desktop-harness/gnome-up.sh b/scripts/linux-desktop-harness/gnome-up.sh
index 08e5a6e4b..b74190b8e 100644
--- a/scripts/linux-desktop-harness/gnome-up.sh
+++ b/scripts/linux-desktop-harness/gnome-up.sh
@@ -22,7 +22,15 @@ Before=graphical-session.target
UNIT
systemctl --user daemon-reload
systemctl --user start cap-harness-session.target
-gsettings set org.gnome.shell enabled-extensions "['[email protected]']"
+# GNOME_TRAY=0 runs GNOME as Fedora ships it: no AppIndicator extension, so
+# no tray for the recording Stop control.
+if [[ ${GNOME_TRAY:-1} == 0 ]]; then
+ gsettings set org.gnome.shell enabled-extensions "[]"
+ gsettings set org.gnome.shell disabled-extensions "['[email protected]']"
+ systemctl --user set-environment GNOME_SHELL_SESSION_MODE=user
+else
+ gsettings set org.gnome.shell enabled-extensions "['[email protected]']"
+fi
# The virtual pointer starts at the top-left corner; the hot corner would open
# the overview under every injected click.
gsettings set org.gnome.desktop.interface enable-hot-corners false
diff --git a/scripts/linux-desktop-harness/scenario-start-stop.sh b/scripts/linux-desktop-harness/scenario-start-stop.sh
index fe9517714..8867e2b1d 100644
--- a/scripts/linux-desktop-harness/scenario-start-stop.sh
+++ b/scripts/linux-desktop-harness/scenario-start-stop.sh
@@ -66,6 +66,25 @@ use_control() {
;;
shortcut) python3 "$here/mutter-keys.py" Control_L+Shift_L+F9 >>"$out/actions.log" 2>&1 ;;
approve) python3 "$here/atspi-press.py" "${2:-Add Keyboard Shortcuts}" "${3:-^Add$}" 20 >>"$out/actions.log" 2>&1 ;;
+ # No tray and no shortcut: Cap records with its controls on screen. Find
+ # that window, then click the red Stop button in it with the pointer.
+ visible)
+ local rect
+ # GNOME starts in the overview, which draws windows elsewhere.
+ gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval 'Main.overview.hide()' >/dev/null 2>&1
+ sleep 1
+ rect=$(gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell \
+ --method org.gnome.Shell.Eval "(() => { const w = global.display.list_all_windows().find(w => w.get_wm_class() === 'Cap' && w.showing_on_its_workspace() && w.get_frame_rect().width === 320 && w.get_frame_rect().height === 150); if (!w) return ''; const r = w.get_frame_rect(); return [r.x, r.y, r.width, r.height].join(' '); })()" \
+ 2>/dev/null | sed -n "s/^(true, '\\(.*\\)')$/\\1/p" | tr -d '"')
+ echo "controls window: ${rect:-not shown}" >>"$out/actions.log"
+ result visible_controls_shown "$([[ -n "$rect" ]] && echo yes || echo no)"
+ if [[ -n "$rect" ]]; then
+ bash "$here/shot.sh" "$out/controls.png" >/dev/null 2>&1
+ # shellcheck disable=SC2086
+ point "$(python3 "$here/stop-locate.py" "$out/controls.png" $rect)"
+ fi
+ ;;
esac
}
@@ -96,6 +115,7 @@ attempt() {
if grep "recording Stop control ready" "$log" | tail -1 | grep -q "tray icon"; then control=tray; else control=shortcut; fi
break
fi
+ if [[ $(count "recording with Cap's controls visible") -gt 0 ]]; then control=visible; break; fi
if [[ $(tray_items) -gt 0 ]]; then control=tray; break; fi
if [[ -n "${PRESS_AFTER:-}" && $((SECONDS - began)) -ge $PRESS_AFTER ]]; then control=shortcut; break; fi
done
@@ -134,9 +154,12 @@ attempt() {
fi
}
-# One tray host connection for the whole run, like a panel.
-coproc TRAY_HOST { python3 -u "$here/sni-click.py" "$out"; }
-if [[ ${TRAY_CLICK:-host} == pointer ]]; then
+# One tray host connection for the whole run, like a panel (none without a
+# tray: GNOME_TRAY=0).
+if [[ ${GNOME_TRAY:-1} != 0 ]]; then
+ coproc TRAY_HOST { python3 -u "$here/sni-click.py" "$out"; }
+fi
+if [[ ${TRAY_CLICK:-host} == pointer || ${GNOME_TRAY:-1} == 0 ]]; then
# The pointer must exist before Cap starts (see mutter-pointer.py).
coproc POINTER { python3 -u "$here/mutter-pointer.py" 2>>"$out/actions.log"; }
read -r -t 10 ready <&"${POINTER[0]}"
diff --git a/scripts/linux-desktop-harness/stop-locate.py b/scripts/linux-desktop-harness/stop-locate.py
new file mode 100755
index 000000000..3ad06e80c
--- /dev/null
+++ b/scripts/linux-desktop-harness/stop-locate.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+"""Where a person clicks to stop a recording in Cap's on-screen controls.
+
+The controls draw their Stop button (icon and elapsed time) in Cap's red,
+#ff4766. Given a screenshot and the controls window's frame, prints the centre
+of the red pixels inside it as "x y", or nothing when there are none.
+
+Usage: stop-locate.py <screenshot.png> <x> <y> <width> <height>
+"""
+import sys
+
+import gi
+
+gi.require_version("GdkPixbuf", "2.0")
+from gi.repository import GdkPixbuf # noqa: E402
+
+RED = (0xFF, 0x47, 0x66)
+TOLERANCE = 40
+
+
+def main() -> int:
+ path = sys.argv[1]
+ left, top, width, height = (int(value) for value in sys.argv[2:6])
+ image = GdkPixbuf.Pixbuf.new_from_file(path)
+ pixels = image.get_pixels()
+ stride = image.get_rowstride()
+ channels = image.get_n_channels()
+ xs, ys = [], []
+ for y in range(max(top, 0), min(top + height, image.get_height())):
+ row = y * stride
+ for x in range(max(left, 0), min(left + width, image.get_width())):
+ offset = row + x * channels
+ r, g, b = pixels[offset], pixels[offset + 1], pixels[offset + 2]
+ if all(abs(channel - want) <= TOLERANCE for channel, want in zip((r, g, b), RED)):
+ xs.append(x)
+ ys.append(y)
+ if not xs:
+ return 1
+ print(f"{sum(xs) // len(xs)} {sum(ys) // len(ys)}")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())