Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop): harden recording controls fake window hit testing

Richie McIlroyd10ecf27c56d
diff --git a/apps/desktop/src-tauri/src/fake_window.rs b/apps/desktop/src-tauri/src/fake_window.rs
index 200fac861..70634dfe6 100644
--- a/apps/desktop/src-tauri/src/fake_window.rs
+++ b/apps/desktop/src-tauri/src/fake_window.rs
@@ -21,6 +21,10 @@ const RECORDING_CONTROLS_HEIGHT: f64 = 150.0;
const RECORDING_CONTROLS_OFFSET_Y: f64 = 120.0;
const TICK_INTERVAL: Duration = Duration::from_millis(50);
const DEAD_WINDOW_ERROR_THRESHOLD: u8 = 5;
+const HIT_TEST_PADDING_PHYSICAL: f64 = 2.0;
+const RECORDING_CONTROLS_FALLBACK_PADDING_LOGICAL: f64 = 48.0;
+const MAX_INTERACTIVE_BOUNDS_WIDTH: f64 = 360.0;
+const MAX_INTERACTIVE_BOUNDS_HEIGHT: f64 = 220.0;
pub struct FakeWindowBounds(pub Arc<RwLock<HashMap<String, HashMap<String, LogicalBounds>>>>);
@@ -115,6 +119,120 @@ pub async fn remove_fake_window(
Ok(())
}
+fn should_ignore_cursor_events(
+ window_position: tauri::PhysicalPosition<i32>,
+ mouse_position: tauri::PhysicalPosition<f64>,
+ scale_factor: f64,
+ windows: &HashMap<String, LogicalBounds>,
+ default_ignore: bool,
+ allow_default_interaction: bool,
+) -> bool {
+ let mut saw_bounds = false;
+
+ for bounds in windows.values() {
+ let width = bounds.size().width();
+ let height = bounds.size().height();
+ if width <= 0.0
+ || height <= 0.0
+ || width > MAX_INTERACTIVE_BOUNDS_WIDTH
+ || height > MAX_INTERACTIVE_BOUNDS_HEIGHT
+ {
+ continue;
+ }
+
+ saw_bounds = true;
+ let x_min = (window_position.x as f64) + bounds.position().x() * scale_factor
+ - HIT_TEST_PADDING_PHYSICAL;
+ let x_max = (window_position.x as f64)
+ + (bounds.position().x() + bounds.size().width()) * scale_factor
+ + HIT_TEST_PADDING_PHYSICAL;
+ let y_min = (window_position.y as f64) + bounds.position().y() * scale_factor
+ - HIT_TEST_PADDING_PHYSICAL;
+ let y_max = (window_position.y as f64)
+ + (bounds.position().y() + bounds.size().height()) * scale_factor
+ + HIT_TEST_PADDING_PHYSICAL;
+
+ if mouse_position.x >= x_min
+ && mouse_position.x <= x_max
+ && mouse_position.y >= y_min
+ && mouse_position.y <= y_max
+ {
+ return false;
+ }
+ }
+
+ if saw_bounds {
+ return true;
+ }
+
+ default_ignore || !allow_default_interaction
+}
+
+fn recording_controls_size_allows_default_interaction(
+ window_size: tauri::PhysicalSize<u32>,
+ scale_factor: f64,
+) -> bool {
+ let scale_factor = scale_factor.max(1.0);
+ let max_width =
+ (RECORDING_CONTROLS_WIDTH + RECORDING_CONTROLS_FALLBACK_PADDING_LOGICAL) * scale_factor;
+ let max_height =
+ (RECORDING_CONTROLS_HEIGHT + RECORDING_CONTROLS_FALLBACK_PADDING_LOGICAL) * scale_factor;
+
+ (window_size.width as f64) <= max_width && (window_size.height as f64) <= max_height
+}
+
+fn prepare_recording_controls_default_interaction(window: &WebviewWindow) -> bool {
+ let (Ok(window_size), Ok(scale_factor)) = (window.outer_size(), window.scale_factor()) else {
+ let _ = window.set_ignore_cursor_events(true);
+ return false;
+ };
+
+ if recording_controls_size_allows_default_interaction(window_size, scale_factor) {
+ return true;
+ }
+
+ if window.set_ignore_cursor_events(true).is_err() {
+ let _ = window.hide();
+ return false;
+ }
+
+ let _ = window.set_size(tauri::LogicalSize::new(
+ RECORDING_CONTROLS_WIDTH,
+ RECORDING_CONTROLS_HEIGHT,
+ ));
+
+ false
+}
+
+fn spawn_recording_controls_sanity_checks(app: AppHandle, window: WebviewWindow) {
+ let label = window.label().to_string();
+
+ tokio::spawn(async move {
+ for delay in [
+ Duration::from_millis(250),
+ Duration::from_secs(2),
+ Duration::from_secs(5),
+ ] {
+ sleep(delay).await;
+
+ if crate::app_is_exiting(&app) {
+ break;
+ }
+
+ if crate::power_observer::is_system_asleep() {
+ continue;
+ }
+
+ let Some(window) = app.get_webview_window(&label) else {
+ break;
+ };
+
+ let ignore = !prepare_recording_controls_default_interaction(&window);
+ let _ = window.set_ignore_cursor_events(ignore);
+ }
+ });
+}
+
fn get_display_id_for_cursor() -> Option<DisplayId> {
Display::get_containing_cursor().map(|d| d.id())
}
@@ -167,13 +285,22 @@ pub fn calculate_recording_controls_position_for_target(
}
pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
- window.set_ignore_cursor_events(true).ok();
-
let label = window.label().to_string();
let is_recording_controls = label == RECORDING_CONTROLS_LABEL;
+ let default_ignore = !is_recording_controls;
+ let initial_ignore = if is_recording_controls {
+ !prepare_recording_controls_default_interaction(&window)
+ } else {
+ default_ignore
+ };
+ window.set_ignore_cursor_events(initial_ignore).ok();
let listeners = app.state::<FakeWindowListeners>();
let (listener_id, token) = listeners.register(label.clone());
+ if is_recording_controls {
+ spawn_recording_controls_sanity_checks(app.clone(), window.clone());
+ }
+
tokio::spawn(async move {
let listeners = app.state::<FakeWindowListeners>();
let state = app.state::<FakeWindowBounds>();
@@ -258,7 +385,12 @@ pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
let map = state.0.read().await;
let Some(windows) = map.get(&label) else {
- if window.set_ignore_cursor_events(true).is_err() {
+ let ignore = if is_recording_controls {
+ !prepare_recording_controls_default_interaction(&window)
+ } else {
+ default_ignore
+ };
+ if window.set_ignore_cursor_events(ignore).is_err() {
consecutive_errors = consecutive_errors.saturating_add(1);
if consecutive_errors >= DEAD_WINDOW_ERROR_THRESHOLD {
debug!(
@@ -286,31 +418,30 @@ pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
);
break;
}
- let _ = window.set_ignore_cursor_events(true);
+ let ignore = if is_recording_controls {
+ !prepare_recording_controls_default_interaction(&window)
+ } else {
+ default_ignore
+ };
+ let _ = window.set_ignore_cursor_events(ignore);
continue;
};
consecutive_errors = 0;
+ let allow_default_interaction = if is_recording_controls {
+ prepare_recording_controls_default_interaction(&window)
+ } else {
+ false
+ };
- let mut ignore = true;
-
- for bounds in windows.values() {
- let x_min = (window_position.x as f64) + bounds.position().x() * scale_factor;
- let x_max = (window_position.x as f64)
- + (bounds.position().x() + bounds.size().width()) * scale_factor;
- let y_min = (window_position.y as f64) + bounds.position().y() * scale_factor;
- let y_max = (window_position.y as f64)
- + (bounds.position().y() + bounds.size().height()) * scale_factor;
-
- if mouse_position.x >= x_min
- && mouse_position.x <= x_max
- && mouse_position.y >= y_min
- && mouse_position.y <= y_max
- {
- ignore = false;
- break;
- }
- }
+ let ignore = should_ignore_cursor_events(
+ window_position,
+ mouse_position,
+ scale_factor,
+ windows,
+ default_ignore,
+ allow_default_interaction,
+ );
window.set_ignore_cursor_events(ignore).ok();
@@ -324,6 +455,11 @@ pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
}
}
+ if is_recording_controls {
+ let ignore = !prepare_recording_controls_default_interaction(&window);
+ let _ = window.set_ignore_cursor_events(ignore);
+ }
+
listeners.finish(&label, listener_id);
{
@@ -333,6 +469,124 @@ pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
});
}
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use scap_targets::bounds::{LogicalPosition, LogicalSize};
+
+ fn bounds(x: f64, y: f64, width: f64, height: f64) -> LogicalBounds {
+ LogicalBounds::new(LogicalPosition::new(x, y), LogicalSize::new(width, height))
+ }
+
+ #[test]
+ fn recording_controls_without_bounds_are_interactive() {
+ assert!(!should_ignore_cursor_events(
+ tauri::PhysicalPosition::new(100, 200),
+ tauri::PhysicalPosition::new(150.0, 240.0),
+ 2.0,
+ &HashMap::new(),
+ false,
+ true,
+ ));
+ }
+
+ #[test]
+ fn recording_controls_without_bounds_pass_through_when_window_is_large() {
+ assert!(should_ignore_cursor_events(
+ tauri::PhysicalPosition::new(0, 0),
+ tauri::PhysicalPosition::new(150.0, 240.0),
+ 2.0,
+ &HashMap::new(),
+ false,
+ false,
+ ));
+ }
+
+ #[test]
+ fn non_recording_windows_without_bounds_pass_through() {
+ assert!(should_ignore_cursor_events(
+ tauri::PhysicalPosition::new(100, 200),
+ tauri::PhysicalPosition::new(150.0, 240.0),
+ 2.0,
+ &HashMap::new(),
+ true,
+ false,
+ ));
+ }
+
+ #[test]
+ fn cursor_inside_registered_bounds_is_interactive() {
+ let mut windows = HashMap::new();
+ windows.insert("controls".to_string(), bounds(10.0, 20.0, 100.0, 40.0));
+
+ assert!(!should_ignore_cursor_events(
+ tauri::PhysicalPosition::new(100, 200),
+ tauri::PhysicalPosition::new(150.0, 250.0),
+ 2.0,
+ &windows,
+ false,
+ true,
+ ));
+ }
+
+ #[test]
+ fn cursor_outside_registered_bounds_passes_through() {
+ let mut windows = HashMap::new();
+ windows.insert("controls".to_string(), bounds(10.0, 20.0, 100.0, 40.0));
+
+ assert!(should_ignore_cursor_events(
+ tauri::PhysicalPosition::new(100, 200),
+ tauri::PhysicalPosition::new(50.0, 250.0),
+ 2.0,
+ &windows,
+ false,
+ true,
+ ));
+ }
+
+ #[test]
+ fn zero_sized_recording_bounds_fail_open() {
+ let mut windows = HashMap::new();
+ windows.insert("controls".to_string(), bounds(10.0, 20.0, 0.0, 40.0));
+
+ assert!(!should_ignore_cursor_events(
+ tauri::PhysicalPosition::new(100, 200),
+ tauri::PhysicalPosition::new(50.0, 250.0),
+ 2.0,
+ &windows,
+ false,
+ true,
+ ));
+ }
+
+ #[test]
+ fn oversized_bounds_do_not_make_the_window_interactive() {
+ let mut windows = HashMap::new();
+ windows.insert("controls".to_string(), bounds(0.0, 0.0, 1440.0, 900.0));
+
+ assert!(should_ignore_cursor_events(
+ tauri::PhysicalPosition::new(0, 0),
+ tauri::PhysicalPosition::new(150.0, 240.0),
+ 2.0,
+ &windows,
+ false,
+ false,
+ ));
+ }
+
+ #[test]
+ fn recording_controls_default_interaction_is_size_limited() {
+ assert!(recording_controls_size_allows_default_interaction(
+ tauri::PhysicalSize::new(640, 300),
+ 2.0,
+ ));
+ assert!(!recording_controls_size_allows_default_interaction(
+ tauri::PhysicalSize::new(1920, 1080),
+ 2.0,
+ ));
+ }
+}
+
pub fn cancel_fake_window_listener(app: &AppHandle, label: &str) {
if let Some(listeners) = app.try_state::<FakeWindowListeners>() {
listeners.cancel(label);