Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: keep recording windows reachable across macOS displays

Richie McIlroy32f9130750ac
diff --git a/apps/desktop/src-tauri/src/clean_capture.rs b/apps/desktop/src-tauri/src/clean_capture.rs
index 9c48fe691..509ec1aff 100644
--- a/apps/desktop/src-tauri/src/clean_capture.rs
+++ b/apps/desktop/src-tauri/src/clean_capture.rs
@@ -690,6 +690,11 @@ fn reveal_now_with_options(
return Ok(false);
}
let Some(state) = app.try_state::<State>() else {
+ #[cfg(target_os = "macos")]
+ if window.label() == CapWindowId::Main.label() {
+ crate::platform::constrain_main_window_to_visible_frame(&window.as_ref().window())
+ .map_err(|error| tauri::Error::Io(std::io::Error::other(error)))?;
+ }
window.show()?;
return Ok(true);
};
@@ -714,6 +719,11 @@ fn reveal_now_with_options(
// Mapping runs on the UI thread; a new capture must acknowledge its queued hide
// before starting. Do not retain this mutex across synchronous GTK callbacks.
drop(inner);
+ #[cfg(target_os = "macos")]
+ if window.label() == CapWindowId::Main.label() {
+ crate::platform::constrain_main_window_to_visible_frame(&window.as_ref().window())
+ .map_err(|error| tauri::Error::Io(std::io::Error::other(error)))?;
+ }
set_native_visibility(window, true)
.map_err(|error| tauri::Error::Io(std::io::Error::other(error)))?;
if unminimize {
diff --git a/apps/desktop/src-tauri/src/fake_window.rs b/apps/desktop/src-tauri/src/fake_window.rs
index ec5b011cb..7bb49faf4 100644
--- a/apps/desktop/src-tauri/src/fake_window.rs
+++ b/apps/desktop/src-tauri/src/fake_window.rs
@@ -170,6 +170,30 @@ fn should_ignore_cursor_events(
default_ignore || !allow_default_interaction
}
+#[cfg(any(target_os = "macos", test))]
+fn macos_cursor_in_window_scale(
+ cursor: tauri::PhysicalPosition<f64>,
+ primary_scale: f64,
+ window_scale: f64,
+) -> Option<tauri::PhysicalPosition<f64>> {
+ if !primary_scale.is_finite()
+ || primary_scale <= 0.0
+ || !window_scale.is_finite()
+ || window_scale <= 0.0
+ || !cursor.x.is_finite()
+ || !cursor.y.is_finite()
+ {
+ return None;
+ }
+ // Tao scales global mouse coordinates by the primary display, but window
+ // positions and DOM bounds use the window's display scale.
+ let factor = window_scale / primary_scale;
+ Some(tauri::PhysicalPosition::new(
+ cursor.x * factor,
+ cursor.y * factor,
+ ))
+}
+
fn recording_controls_size_allows_default_interaction(
window_size: tauri::PhysicalSize<u32>,
scale_factor: f64,
@@ -466,6 +490,18 @@ pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
};
consecutive_errors = 0;
+ #[cfg(target_os = "macos")]
+ let mouse_position = match window.primary_monitor().ok().flatten().and_then(|monitor| {
+ macos_cursor_in_window_scale(mouse_position, monitor.scale_factor(), scale_factor)
+ }) {
+ Some(position) => position,
+ None => {
+ let ignore = !is_recording_controls
+ || !prepare_recording_controls_default_interaction(&window);
+ let _ = window.set_ignore_cursor_events(ignore);
+ continue;
+ }
+ };
let allow_default_interaction = if is_recording_controls {
prepare_recording_controls_default_interaction(&window)
} else {
@@ -533,6 +569,48 @@ mod tests {
LogicalBounds::new(LogicalPosition::new(x, y), LogicalSize::new(width, height))
}
+ #[test]
+ fn macos_hit_testing_uses_the_same_scale_on_secondary_displays() {
+ let mut windows = HashMap::new();
+ windows.insert("controls".to_string(), bounds(10.0, 20.0, 100.0, 40.0));
+ for (origin, primary_scale, window_scale) in [
+ ((1920.0, 100.0), 2.0, 1.0),
+ ((-1280.0, 100.0), 1.0, 2.0),
+ ((0.0, -900.0), 2.0, 1.0),
+ ((0.0, 0.0), 2.0, 2.0),
+ ] {
+ let position =
+ tauri::LogicalPosition::new(origin.0, origin.1).to_physical::<i32>(window_scale);
+ for (offset, ignored) in [((30.0, 30.0), false), ((150.0, 80.0), true)] {
+ let mouse = tauri::LogicalPosition::new(origin.0 + offset.0, origin.1 + offset.1)
+ .to_physical::<f64>(primary_scale);
+ let mouse =
+ macos_cursor_in_window_scale(mouse, primary_scale, window_scale).unwrap();
+ assert_eq!(
+ should_ignore_cursor_events(
+ position,
+ mouse,
+ window_scale,
+ &windows,
+ false,
+ true,
+ ),
+ ignored,
+ );
+ }
+ }
+ }
+
+ #[test]
+ fn macos_hit_testing_rejects_invalid_coordinate_scales() {
+ for scale in [0.0, -1.0, f64::NAN, f64::INFINITY] {
+ assert!(
+ macos_cursor_in_window_scale(tauri::PhysicalPosition::new(20.0, 30.0), scale, 2.0,)
+ .is_none()
+ );
+ }
+ }
+
#[test]
fn recording_controls_without_bounds_are_interactive() {
assert!(!should_ignore_cursor_events(
diff --git a/apps/desktop/src-tauri/src/platform/macos/mod.rs b/apps/desktop/src-tauri/src/platform/macos/mod.rs
index 0dc6904bc..e4072177d 100644
--- a/apps/desktop/src-tauri/src/platform/macos/mod.rs
+++ b/apps/desktop/src-tauri/src/platform/macos/mod.rs
@@ -58,6 +58,79 @@ pub fn constrain_main_window_to_visible_top(
)
}
+fn constrain_frame_to_visible_area(
+ frame: objc2_foundation::NSRect,
+ screen_frame: objc2_foundation::NSRect,
+ visible_frame: objc2_foundation::NSRect,
+ safe_area_top_inset: f64,
+) -> Option<objc2_foundation::NSRect> {
+ use objc2_foundation::{NSPoint, NSRect, NSSize};
+
+ let minimum_x = visible_frame.origin.x;
+ let minimum_y = visible_frame.origin.y;
+ let maximum_x = minimum_x + visible_frame.size.width;
+ let maximum_y = (minimum_y + visible_frame.size.height)
+ .min(screen_frame.origin.y + screen_frame.size.height - safe_area_top_inset.max(0.0));
+ if ![
+ frame.origin.x,
+ frame.origin.y,
+ frame.size.width,
+ frame.size.height,
+ minimum_x,
+ minimum_y,
+ maximum_x,
+ maximum_y,
+ ]
+ .into_iter()
+ .all(f64::is_finite)
+ || frame.size.width <= 0.0
+ || frame.size.height <= 0.0
+ || maximum_x <= minimum_x
+ || maximum_y <= minimum_y
+ {
+ return None;
+ }
+
+ let width = frame.size.width.min(maximum_x - minimum_x);
+ let height = frame.size.height.min(maximum_y - minimum_y);
+ let constrained = NSRect::new(
+ NSPoint::new(
+ frame.origin.x.clamp(minimum_x, maximum_x - width),
+ frame.origin.y.clamp(minimum_y, maximum_y - height),
+ ),
+ NSSize::new(width, height),
+ );
+ (constrained != frame).then_some(constrained)
+}
+
+pub fn constrain_main_window_to_visible_frame(window: &tauri::Window) -> Result<(), String> {
+ use objc2::{MainThreadMarker, runtime::NSObjectProtocol, sel};
+ use objc2_app_kit::{NSScreen, NSWindow};
+
+ let main_thread =
+ MainThreadMarker::new().ok_or("Main window bounds must be checked on the main thread")?;
+ let ns_window = window.ns_window().map_err(|error| error.to_string())? as *const NSWindow;
+ let ns_window = unsafe { ns_window.as_ref() }.ok_or("Main window is unavailable")?;
+ let screen = ns_window
+ .screen()
+ .or_else(|| NSScreen::mainScreen(main_thread))
+ .ok_or("Main window screen is unavailable")?;
+ let safe_area_top_inset = if screen.respondsToSelector(sel!(safeAreaInsets)) {
+ unsafe { screen.safeAreaInsets().top }
+ } else {
+ 0.0
+ };
+ if let Some(frame) = constrain_frame_to_visible_area(
+ ns_window.frame(),
+ screen.frame(),
+ screen.visibleFrame(),
+ safe_area_top_inset,
+ ) {
+ ns_window.setFrame_display(frame, true);
+ }
+ Ok(())
+}
+
pub fn set_window_level(window: tauri::Window, level: objc2_app_kit::NSWindowLevel) {
let c_window = window.clone();
_ = window.run_on_main_thread(move || unsafe {
@@ -610,9 +683,69 @@ pub async fn teardown_all_liquid_glass(app: &tauri::AppHandle) -> Result<(), Str
#[cfg(test)]
mod tests {
- use super::constrain_position_to_visible_top;
+ use super::{constrain_frame_to_visible_area, constrain_position_to_visible_top};
+ use objc2_foundation::{NSPoint, NSRect, NSSize};
use tauri::PhysicalPosition;
+ fn rect(x: f64, y: f64, width: f64, height: f64) -> NSRect {
+ NSRect::new(NSPoint::new(x, y), NSSize::new(width, height))
+ }
+
+ #[test]
+ fn fits_the_actual_resized_frame_inside_the_work_area() {
+ let screen = rect(0.0, 0.0, 1440.0, 900.0);
+ let visible = rect(0.0, 24.0, 1440.0, 851.0);
+ let constrained = constrain_frame_to_visible_area(
+ rect(1300.0, 800.0, 330.0, 395.0),
+ screen,
+ visible,
+ 0.0,
+ )
+ .unwrap();
+
+ assert_eq!(constrained, rect(1110.0, 480.0, 330.0, 395.0));
+ assert_eq!(
+ constrain_frame_to_visible_area(constrained, screen, visible, 0.0),
+ None
+ );
+ }
+
+ #[test]
+ fn fits_expanded_frames_below_a_notch_with_a_hidden_menu_bar() {
+ let screen = rect(0.0, 0.0, 1440.0, 900.0);
+ assert_eq!(
+ constrain_frame_to_visible_area(rect(100.0, 650.0, 480.0, 700.0), screen, screen, 37.0,),
+ Some(rect(100.0, 163.0, 480.0, 700.0))
+ );
+ }
+
+ #[test]
+ fn keeps_native_coordinates_on_a_display_above_and_left_of_primary() {
+ assert_eq!(
+ constrain_frame_to_visible_area(
+ rect(-2000.0, 1800.0, 480.0, 700.0),
+ rect(-1920.0, 900.0, 1920.0, 1080.0),
+ rect(-1880.0, 900.0, 1880.0, 1050.0),
+ 37.0,
+ ),
+ Some(rect(-1880.0, 1243.0, 480.0, 700.0))
+ );
+ }
+
+ #[test]
+ fn fits_an_oversized_restored_frame_without_negative_clamp_ranges() {
+ let visible = rect(40.0, 24.0, 1400.0, 851.0);
+ assert_eq!(
+ constrain_frame_to_visible_area(
+ rect(-100.0, -100.0, 2000.0, 1200.0),
+ rect(0.0, 0.0, 1440.0, 900.0),
+ visible,
+ 0.0,
+ ),
+ Some(visible)
+ );
+ }
+
#[test]
fn moves_a_window_below_a_notched_visible_frame() {
let position = PhysicalPosition::new(640, 0);
diff --git a/apps/desktop/src-tauri/src/windows.rs b/apps/desktop/src-tauri/src/windows.rs
index 03823274f..dfe3ad5f4 100644
--- a/apps/desktop/src-tauri/src/windows.rs
+++ b/apps/desktop/src-tauri/src/windows.rs
@@ -1510,16 +1510,9 @@ impl ShowCapWindow {
move || {
let _panel_activation_guard = panel_activation_guard;
use tauri_nspanel::cocoa::appkit::NSWindowCollectionBehavior;
- use tauri_nspanel::panel_delegate;
use crate::panel_manager::try_to_panel;
- let delegate = panel_delegate!(MainPanelDelegate {
- window_did_become_key,
- window_did_resign_key
- });
-
- delegate.set_listener(Box::new(|_delegate_name: String| {}));
-
+ // Tao's delegate carries the native events from queued AppKit resizes.
let panel = match try_to_panel(&window) {
Ok(p) => p,
Err(e) => {
@@ -1534,10 +1527,19 @@ impl ShowCapWindow {
| NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenPrimary,
);
- panel.set_delegate(delegate);
-
panel.set_level(MAIN_PANEL_LEVEL);
+ let resized_window = window.clone();
+ window.on_window_event(move |event| {
+ if matches!(event, tauri::WindowEvent::Resized(_) | tauri::WindowEvent::ScaleFactorChanged { .. })
+ && let Err(error) = crate::platform::constrain_main_window_to_visible_frame(
+ &resized_window.as_ref().window(),
+ )
+ {
+ warn!(%error, "Failed to fit resized Main window to its visible screen area");
+ }
+ });
+
let _ = window.set_position(main_position);
crate::platform::apply_squircle_corners(&window, 16.0);