Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: select Linux recording windows beneath the picker

Richie McIlroy6de30521adad
diff --git a/apps/desktop/src-tauri/src/target_select_overlay.rs b/apps/desktop/src-tauri/src/target_select_overlay.rs
index a1c2212aa..db52a8ccb 100644
--- a/apps/desktop/src-tauri/src/target_select_overlay.rs
+++ b/apps/desktop/src-tauri/src/target_select_overlay.rs
@@ -183,23 +183,19 @@ pub async fn open_target_select_overlays(
|| {
focused_target
.as_ref()
- .map(|v| v.window().and_then(|id| scap_targets::Window::from_id(&id)))
- .unwrap_or_else(scap_targets::Window::get_topmost_at_cursor)
+ .map(|v| {
+ v.window()
+ .and_then(|id| scap_targets::Window::from_id(&id))
+ .and_then(|window| {
+ describe_selectable_window(window, &window_exclusions)
+ })
+ })
+ .unwrap_or_else(|| target_window_under_cursor(&window_exclusions))
},
) {
let _ = TargetUnderCursor {
display_id: display.map(|d| d.id()),
- window: window.and_then(|w| {
- if should_skip_window(&w, &window_exclusions) {
- return None;
- }
-
- Some(WindowUnderCursor {
- id: w.id(),
- bounds: w.display_relative_logical_bounds()?,
- app_name: w.owner_name()?,
- })
- }),
+ window,
}
.emit(&app);
@@ -221,6 +217,75 @@ pub async fn open_target_select_overlays(
Ok(())
}
+fn describe_selectable_window(
+ window: Window,
+ exclusions: &[WindowExclusion],
+) -> Option<WindowUnderCursor> {
+ if should_skip_window(&window, exclusions) {
+ return None;
+ }
+ Some(WindowUnderCursor {
+ id: window.id(),
+ bounds: window.display_relative_logical_bounds()?,
+ app_name: window.owner_name()?,
+ })
+}
+
+fn target_window_under_cursor(exclusions: &[WindowExclusion]) -> Option<WindowUnderCursor> {
+ #[cfg(target_os = "linux")]
+ {
+ let own_pid = std::process::id();
+ let candidates = Window::list_containing_cursor()
+ .into_iter()
+ .filter_map(|window| {
+ let metadata = window.raw_handle().selection_metadata()?;
+ let title = if metadata.owner_pid == Some(own_pid) {
+ window.name()
+ } else {
+ None
+ };
+ Some(LinuxPickerCandidate {
+ window,
+ is_viewable: metadata.is_viewable,
+ owner_pid: metadata.owner_pid,
+ title,
+ })
+ });
+ first_linux_picker_target(candidates, own_pid, |window| {
+ describe_selectable_window(window, exclusions)
+ })
+ }
+
+ #[cfg(not(target_os = "linux"))]
+ Window::get_topmost_at_cursor()
+ .and_then(|window| describe_selectable_window(window, exclusions))
+}
+
+#[cfg(any(target_os = "linux", test))]
+struct LinuxPickerCandidate<T> {
+ window: T,
+ is_viewable: bool,
+ owner_pid: Option<u32>,
+ title: Option<String>,
+}
+
+#[cfg(any(target_os = "linux", test))]
+fn first_linux_picker_target<T, R>(
+ candidates: impl IntoIterator<Item = LinuxPickerCandidate<T>>,
+ own_pid: u32,
+ mut describe: impl FnMut(T) -> Option<R>,
+) -> Option<R> {
+ candidates.into_iter().find_map(|candidate| {
+ if !candidate.is_viewable
+ || (candidate.owner_pid == Some(own_pid)
+ && candidate.title.as_deref() == Some("Cap Target Select"))
+ {
+ return None;
+ }
+ describe(candidate.window)
+ })
+}
+
fn finish_created_target_select_overlay(
window: &WebviewWindow,
should_focus: bool,
@@ -633,3 +698,111 @@ impl WindowFocusManager {
}
}
}
+
+#[cfg(test)]
+mod linux_picker_tests {
+ use super::{LinuxPickerCandidate, WindowExclusion, first_linux_picker_target};
+
+ fn candidate(
+ title: &'static str,
+ owner_pid: Option<u32>,
+ is_viewable: bool,
+ ) -> LinuxPickerCandidate<&'static str> {
+ LinuxPickerCandidate {
+ window: title,
+ is_viewable,
+ owner_pid,
+ title: Some(title.into()),
+ }
+ }
+
+ #[test]
+ fn selects_underlying_window_after_picker_and_configured_exclusion() {
+ let exclusion = WindowExclusion {
+ bundle_identifier: None,
+ owner_name: Some("Cap".into()),
+ window_title: Some("Cap Camera".into()),
+ };
+ let candidates = [
+ candidate("Cap Target Select", Some(42), true),
+ candidate("Cap Camera", Some(42), true),
+ candidate("Moving fixture", Some(100), true),
+ candidate("Lower window", Some(101), true),
+ ];
+ let mut described = Vec::new();
+
+ let selected = first_linux_picker_target(candidates, 42, |title| {
+ described.push(title);
+ (!exclusion.matches(None, Some("Cap"), Some(title))).then_some(title)
+ });
+
+ assert_eq!(selected, Some("Moving fixture"));
+ assert_eq!(described, ["Cap Camera", "Moving fixture"]);
+ }
+
+ #[test]
+ fn skips_own_picker_when_no_windows_are_configured_for_exclusion() {
+ let candidates = [
+ candidate("Cap Target Select", Some(42), true),
+ candidate("Cap Camera", Some(42), true),
+ candidate("Moving fixture", Some(100), true),
+ ];
+
+ assert_eq!(
+ first_linux_picker_target(candidates, 42, Some),
+ Some("Cap Camera")
+ );
+ }
+
+ #[test]
+ fn does_not_exclude_foreign_or_unknown_owner_by_picker_title() {
+ for owner_pid in [Some(100), None] {
+ let candidates = [
+ candidate("Cap Target Select", owner_pid, true),
+ candidate("Moving fixture", Some(101), true),
+ ];
+
+ assert_eq!(
+ first_linux_picker_target(candidates, 42, Some),
+ Some("Cap Target Select")
+ );
+ }
+ }
+
+ #[test]
+ fn bypasses_unmapped_and_undescribable_windows_without_reordering() {
+ let candidates = [
+ candidate("Hidden window", Some(100), false),
+ candidate("Destroyed window", Some(101), true),
+ candidate("Moving fixture", Some(102), true),
+ candidate("Lower window", Some(103), true),
+ ];
+ let mut described = Vec::new();
+
+ let selected = first_linux_picker_target(candidates, 42, |title| {
+ described.push(title);
+ (title != "Destroyed window").then_some(title)
+ });
+
+ assert_eq!(selected, Some("Moving fixture"));
+ assert_eq!(described, ["Destroyed window", "Moving fixture"]);
+ }
+
+ #[test]
+ fn returns_no_target_when_every_candidate_is_ineligible() {
+ let candidates = [
+ candidate("Cap Target Select", Some(42), true),
+ candidate("Hidden window", Some(100), false),
+ candidate("Excluded window", Some(101), true),
+ ];
+ let mut described = Vec::new();
+
+ let selected = first_linux_picker_target(candidates, 42, |title| {
+ described.push(title);
+ None::<&str>
+ });
+
+ assert!(selected.is_none());
+ assert_eq!(described, ["Excluded window"]);
+ }
+}
diff --git a/crates/scap-targets/src/platform/linux.rs b/crates/scap-targets/src/platform/linux.rs
index 751d23ce9..6f3ecefac 100644
--- a/crates/scap-targets/src/platform/linux.rs
+++ b/crates/scap-targets/src/platform/linux.rs
@@ -4,7 +4,7 @@ use x11rb::{
connection::Connection,
protocol::{
randr::ConnectionExt as RandrConnectionExt,
- xproto::{Atom, AtomEnum, ConnectionExt as XprotoConnectionExt, Window},
+ xproto::{Atom, AtomEnum, ConnectionExt as XprotoConnectionExt, MapState, Window},
},
rust_connection::RustConnection,
};
@@ -155,6 +155,11 @@ impl FromStr for DisplayIdImpl {
#[derive(Clone, Copy)]
pub struct WindowImpl(Window);
+pub struct WindowSelectionMetadata {
+ pub is_viewable: bool,
+ pub owner_pid: Option<u32>,
+}
+
impl WindowImpl {
pub fn list() -> Vec<Self> {
let Ok((conn, screen_num)) = x11_connection() else {
@@ -191,6 +196,15 @@ impl WindowImpl {
Self::list_containing_cursor().into_iter().next()
}
+ pub fn selection_metadata(&self) -> Option<WindowSelectionMetadata> {
+ let (conn, _) = x11_connection().ok()?;
+ let attributes = conn.get_window_attributes(self.0).ok()?.reply().ok()?;
+ Some(WindowSelectionMetadata {
+ is_viewable: attributes.map_state == MapState::VIEWABLE,
+ owner_pid: window_pid(&conn, self.0),
+ })
+ }
+
pub fn id(&self) -> WindowIdImpl {
WindowIdImpl(self.0)
}