Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(linux): GNOME screenshots after a terminal launch of the packaged app (#3348)

Richie McIlroy7c3bd99cbd01
diff --git a/.github/workflows/linux-desktop-reliability.yml b/.github/workflows/linux-desktop-reliability.yml
index 5e2e45009..bcf160af5 100644
--- a/.github/workflows/linux-desktop-reliability.yml
+++ b/.github/workflows/linux-desktop-reliability.yml
@@ -281,6 +281,19 @@ jobs:
grep -qx "$line" "$RUNNER_TEMP/out/gnome-screenshot/result.txt"
done
+ - name: GNOME 50 screenshot from a terminal launch (Cap names itself so.cap.Cap)
+ env:
+ DEV_BIN: ${{ github.workspace }}/apps/desktop-gpui/target/debug/cap-gpui
+ DEV_LIBS: ${{ github.workspace }}/target/native-deps/lib
+ SCENARIO: screenshot
+ run: |
+ sudo -E bash scripts/linux-desktop-harness/run-desktop.sh gnome "$RUNNER_TEMP/cap.deb" \
+ "$RUNNER_TEMP/out/gnome-screenshot-terminal" LAUNCH=terminal PRELAUNCH=1 GNOME_UNSAFE=1
+ cat "$RUNNER_TEMP/out/gnome-screenshot-terminal/result.txt" "$RUNNER_TEMP/out/gnome-screenshot-terminal/actions.log"
+ for line in screenshot_taken=yes screenshot_size=yes capture_failed=0 cap_alive=yes panics=0; do
+ grep -qx "$line" "$RUNNER_TEMP/out/gnome-screenshot-terminal/result.txt"
+ done
+
- name: GNOME 50 area recording (portal stream cropped to the area)
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 d25b9e520..aafc67eb9 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -1234,7 +1234,7 @@ pub fn open_settings(page: Page, cx: &mut App) {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// A real titlebar this time, transparent with the buttons moved
@@ -1415,7 +1415,7 @@ pub fn open_onboarding(cx: &mut App) {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
titlebar: None,
@@ -1568,7 +1568,7 @@ pub fn open_mode_select(cx: &mut App) -> bool {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// `traffic_lights_position` has no ModeSelect arm, so it takes the
@@ -1690,7 +1690,7 @@ pub fn open_teleprompter(cx: &mut App) {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// `decorations: true`, `titleBarStyle: "overlay"`, `hiddenTitle:
@@ -2521,7 +2521,7 @@ fn open_overlay(
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
// The origin is corrected right after opening -- gpui's own
// window-origin math cannot express "cover this display" (see
// `platform::set_window_frame_cg`). The size is honoured, and it is
@@ -2975,6 +2975,18 @@ pub fn restore_content_protection(cx: &mut App) {
#[cfg(target_os = "linux")]
pub(crate) const APP_ID: &str = "Cap";
+/// The app id Cap's windows carry. GNOME Shell asks the portals' questions
+/// (screenshot access) only for the focused app, matched as the window's app
+/// id against the id Cap gave the portals, so a launch that named itself
+/// `so.cap.Cap` there names its windows the same.
+#[cfg(target_os = "linux")]
+pub(crate) fn app_id() -> &'static str {
+ if crate::linux_portal_id::windows_use_portal_app_id() {
+ return crate::linux_portal_id::PORTAL_APP_ID;
+ }
+ APP_ID
+}
+
#[cfg(target_os = "linux")]
fn clean_capture_supported(
mode: RecordingMode,
@@ -4047,7 +4059,7 @@ pub fn open_camera_window(cx: &mut App) {
..Default::default()
}),
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
#[cfg(target_os = "linux")]
window_decorations: Some(gpui::WindowDecorations::Client),
// Non-activating panel, same as the bar: the bubble is clickable
@@ -4907,7 +4919,7 @@ fn open_editor_window(
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// Native traffic lights, inset to (20, 32):
@@ -6709,7 +6721,7 @@ fn open_controls(
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
// No titlebar at all: with one, the panel still draws standard
@@ -6890,7 +6902,7 @@ pub fn open_screenshot_editor(path: PathBuf, cx: &mut App) {
let handle = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(bounds)),
display_id,
titlebar: Some(gpui::TitlebarOptions {
diff --git a/apps/desktop-gpui/src/linux_dialog.rs b/apps/desktop-gpui/src/linux_dialog.rs
index 359d817d3..6b6165923 100644
--- a/apps/desktop-gpui/src/linux_dialog.rs
+++ b/apps/desktop-gpui/src/linux_dialog.rs
@@ -116,7 +116,7 @@ fn open(request: Request, cx: &mut App) {
title: Some(title.into()),
..Default::default()
}),
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
focus: true,
show: true,
is_resizable: false,
diff --git a/apps/desktop-gpui/src/linux_portal_id.rs b/apps/desktop-gpui/src/linux_portal_id.rs
index 647ef18a2..f501e0695 100644
--- a/apps/desktop-gpui/src/linux_portal_id.rs
+++ b/apps/desktop-gpui/src/linux_portal_id.rs
@@ -15,6 +15,22 @@ use std::{
};
pub const PORTAL_APP_ID: &str = "so.cap.Cap";
+
+static REGISTERED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+
+/// Whether Cap's windows take [`PORTAL_APP_ID`]: only after this launch
+/// registered it, and only on GNOME, the one desktop that matches windows to
+/// it. Elsewhere the windows keep their usual id, which window rules may name.
+pub fn windows_use_portal_app_id() -> bool {
+ REGISTERED.load(std::sync::atomic::Ordering::Acquire)
+ && is_gnome(&std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default())
+}
+
+fn is_gnome(current_desktop: &str) -> bool {
+ current_desktop
+ .split(':')
+ .any(|desktop| desktop.eq_ignore_ascii_case("gnome"))
+}
const PACKAGED_APP_ID: &str = "Cap";
/// The app id xdg-desktop-portal reads from a launcher's systemd unit:
@@ -172,6 +188,7 @@ pub async fn register() {
.await
{
Ok(Ok(())) => {
+ REGISTERED.store(true, std::sync::atomic::Ordering::Release);
tracing::info!(
app_id = PORTAL_APP_ID,
attempt,
@@ -269,6 +286,17 @@ mod tests {
assert!(application_dirs(Some("relative".into()), None, None).is_none());
}
+ #[test]
+ fn windows_follow_the_portal_id_only_on_gnome() {
+ assert!(is_gnome("ubuntu:GNOME"));
+ assert!(is_gnome("GNOME"));
+ assert!(is_gnome("GNOME-Classic:GNOME"));
+ assert!(!is_gnome("KDE"));
+ assert!(!is_gnome("Hyprland"));
+ assert!(!is_gnome("niri"));
+ assert!(!is_gnome(""));
+ }
+
#[test]
fn only_a_missing_entry_is_retried() {
assert!(app_info_not_found(
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index d0fa0e978..e2edff04f 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -436,7 +436,7 @@ fn main() {
..Default::default()
}),
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
#[cfg(target_os = "linux")]
window_min_size: Some(size(px(MAIN_WINDOW_WIDTH), px(MAIN_WINDOW_HEIGHT))),
#[cfg(target_os = "linux")]
diff --git a/apps/desktop-gpui/src/permission_guide.rs b/apps/desktop-gpui/src/permission_guide.rs
index f9b766a3b..5bd32bdcb 100644
--- a/apps/desktop-gpui/src/permission_guide.rs
+++ b/apps/desktop-gpui/src/permission_guide.rs
@@ -113,7 +113,7 @@ pub fn start(permission: OSPermission, cx: &mut App) {
let opened = cx.open_window(
WindowOptions {
#[cfg(target_os = "linux")]
- app_id: Some(crate::app_windows::APP_ID.into()),
+ app_id: Some(crate::app_windows::app_id().into()),
window_bounds: Some(WindowBounds::Windowed(Bounds {
origin: point(px(0.), px(0.)),
size: size(px(PANEL_W), px(PANEL_H)),
diff --git a/scripts/linux-desktop-harness/README.md b/scripts/linux-desktop-harness/README.md
index 0fa7add3e..42e7d24a1 100644
--- a/scripts/linux-desktop-harness/README.md
+++ b/scripts/linux-desktop-harness/README.md
@@ -57,6 +57,8 @@ screenshot mode does, with Cap focused as after a click, and allows it when
the portal asks (GNOME Shell's question is answered from the Shell, so it needs
`GNOME_UNSAFE=1`; elsewhere the permission is granted up front because the GTK
portal's dialog aborts in the container). It checks a PNG of the display's size (`SCREEN`, 1920x1080) was saved.
+`PRELAUNCH=1` runs Cap once before, so a `LAUNCH=terminal` run finds the
+`so.cap.Cap.desktop` entry GNOME matches Cap's windows to.
`SCENARIO=wallpaper` records a clip, opens it in the editor and imports the
desktop's wallpaper as its background (`CAP_GPUI_AUTO_DESKTOP_BACKGROUND=1`).
GNOME keeps its default JPEG XL wallpaper; `WALLPAPER=swaybg` starts swaybg
diff --git a/scripts/linux-desktop-harness/scenario-screenshot.sh b/scripts/linux-desktop-harness/scenario-screenshot.sh
index 14bce51ce..512d63c93 100755
--- a/scripts/linux-desktop-harness/scenario-screenshot.sh
+++ b/scripts/linux-desktop-harness/scenario-screenshot.sh
@@ -20,6 +20,15 @@ if [[ $XDG_CURRENT_DESKTOP != *GNOME* ]]; then
done
fi
+# PRELAUNCH=1 runs Cap once first, as on a system Cap has run on: a shell
+# launch writes so.cap.Cap.desktop, which GNOME reads a few seconds later.
+if [[ ${PRELAUNCH:-} == 1 ]]; then
+ first=$(bash "$here/launch-cap.sh" first)
+ sleep 8
+ kill "$first" 2>/dev/null
+ sleep 3
+fi
+
# LAUNCH=dock starts Cap in the unit GNOME's app grid gives it, as
# scenario-start-stop.sh does; LAUNCH=terminal from a shell.
if [[ ${LAUNCH:-dock} == dock ]]; then
@@ -32,7 +41,7 @@ pid=$(bash "$here/launch-cap.sh" cap CAP_GPUI_AUTO_RECORD=screenshot CAP_GPUI_AU
focus_cap() {
if [[ $XDG_CURRENT_DESKTOP == *GNOME* ]]; then
gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval \
- "global.display.list_all_windows().filter(w => w.get_wm_class() === 'Cap' && w.showing_on_its_workspace()).forEach(w => w.activate(global.display.get_current_time_roundtrip()))" \
+ "global.display.list_all_windows().filter(w => /^(Cap|so\.cap\.Cap)$/.test(w.get_wm_class() || '') && w.showing_on_its_workspace()).forEach(w => w.activate(global.display.get_current_time_roundtrip()))" \
>/dev/null 2>&1
elif [[ -n ${HYPRLAND_INSTANCE_SIGNATURE:-} ]]; then
hyprctl dispatch focuswindow class:Cap >/dev/null 2>&1
diff --git a/scripts/linux-desktop-harness/scenario-start-stop.sh b/scripts/linux-desktop-harness/scenario-start-stop.sh
index 7264bc8a7..40dc60dbe 100644
--- a/scripts/linux-desktop-harness/scenario-start-stop.sh
+++ b/scripts/linux-desktop-harness/scenario-start-stop.sh
@@ -79,7 +79,7 @@ use_control() {
--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(' '); })()" \
+ --method org.gnome.Shell.Eval "(() => { const w = global.display.list_all_windows().find(w => /^(Cap|so\.cap\.Cap)$/.test(w.get_wm_class() || '') && 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)"