Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Fix HiDPI overlay sizing on Windows

Richie McIlroyfce5a85f3c4f
diff --git a/apps/desktop/src-tauri/src/windows.rs b/apps/desktop/src-tauri/src/windows.rs
index 37714c64a..d8505bf06 100644
--- a/apps/desktop/src-tauri/src/windows.rs
+++ b/apps/desktop/src-tauri/src/windows.rs
@@ -2401,12 +2401,15 @@ impl ShowCapWindow {
.position(bounds.position().x(), bounds.position().y());
}
+ // On Windows a window's DPI scale isn't known until it's placed on a
+ // monitor, so sizing/positioning from logical bounds at build time is
+ // unreliable across monitors with different DPIs — the overlay ends up
+ // sized for the wrong monitor and no longer covers the target display,
+ // which truncates area selections on HiDPI secondary monitors. Build a
+ // placeholder and fix the geometry up after the window exists (below),
+ // mirroring the TargetSelectOverlay path.
#[cfg(windows)]
- if let Some(bounds) = display.raw_handle().logical_bounds() {
- window_builder = window_builder
- .inner_size(bounds.size().width(), bounds.size().height())
- .position(bounds.position().x(), bounds.position().y());
- } else {
+ {
window_builder = window_builder.inner_size(100.0, 100.0).position(0.0, 0.0);
}
@@ -2420,6 +2423,49 @@ impl ShowCapWindow {
let window = window_builder.build()?;
lock_window_text_scale(&window);
+ // Fix up the overlay geometry now that the window exists and its real
+ // per-monitor DPI is known: position with physical coordinates (which are
+ // unambiguous across monitors), then set the logical size so the window
+ // covers the full display. Verify the resulting physical size matches the
+ // display and re-apply once if the initial placement raced the DPI change.
+ #[cfg(windows)]
+ {
+ let Some(position) = display.raw_handle().physical_position() else {
+ warn!(display_id = %screen_id, "Missing display position for capture area overlay");
+ return Err(tauri::Error::WindowNotFound);
+ };
+ let Some(logical_size) = display.logical_size() else {
+ warn!(display_id = %screen_id, "Missing display logical size for capture area overlay");
+ return Err(tauri::Error::WindowNotFound);
+ };
+ let Some(physical_size) = display.physical_size() else {
+ warn!(display_id = %screen_id, "Missing display physical size for capture area overlay");
+ return Err(tauri::Error::WindowNotFound);
+ };
+ use tauri::{LogicalSize, PhysicalPosition};
+ let _ = window.set_size(LogicalSize::new(
+ logical_size.width(),
+ logical_size.height(),
+ ));
+ let _ = window.set_position(PhysicalPosition::new(position.x(), position.y()));
+ tokio::time::sleep(std::time::Duration::from_millis(5)).await;
+
+ match window.inner_size() {
+ Ok(actual_physical_size)
+ if physical_size.width() != actual_physical_size.width as f64 =>
+ {
+ let _ = window.set_size(LogicalSize::new(
+ logical_size.width(),
+ logical_size.height(),
+ ));
+ }
+ Ok(_) => {}
+ Err(err) => {
+ warn!(%err, "Failed to read capture area overlay inner size");
+ }
+ }
+ }
+
#[cfg(target_os = "linux")]
if let Some(bounds) = display.raw_handle().physical_bounds() {
use tauri::{LogicalSize, PhysicalPosition};
diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs
index a78474184..f0b9643d5 100644
--- a/crates/recording/src/sources/screen_capture/mod.rs
+++ b/crates/recording/src/sources/screen_capture/mod.rs
@@ -505,6 +505,24 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureConfig<TCaptureFormat> {
.ok_or(ScreenCaptureInitError::NoBounds)?;
let output_size = constrain_capture_size(native_output_size, max_capture_size);
+ // Diagnostic anchor for area/crop capture: `crop_bounds` is the region the
+ // capturer will read (physical px on Windows/Linux, logical pts on macOS).
+ // For a selection spanning the whole monitor the crop size should match the
+ // display's physical size — a smaller value here means the overlay/crop was
+ // computed against the wrong coordinate space.
+ // (Hoisted into a local because `use tracing::*` glob-imports a `display`
+ // helper that would otherwise shadow the `display` binding inside the macro.)
+ let display_physical_size = display.physical_size();
+ tracing::info!(
+ crop_bounds = ?crop_bounds,
+ ?display_physical_size,
+ native_output_width = native_output_size.width(),
+ native_output_height = native_output_size.height(),
+ output_width = output_size.width(),
+ output_height = output_size.height(),
+ "Screen capture configured"
+ );
+
Ok(Self {
config: Config {
display: display.id(),
@@ -709,4 +727,69 @@ mod tests {
assert_eq!(physical.size().width(), 2.0);
assert_eq!(physical.size().height(), 2.0);
}
+
+ // Regression coverage for the HiDPI secondary-monitor area bug: a selection
+ // that spans the entire overlay (== the display's logical size) must map to
+ // the entire physical display, with no truncation, at every common scale.
+ // These pin the invariant that made "records ~70% of the selection" possible
+ // once the overlay is correctly sized to cover the monitor.
+ #[test]
+ fn full_selection_covers_full_display_at_every_scale() {
+ // (logical_size, physical_size) pairs for 100/125/150/175/200% scaling.
+ let scales = [
+ (LogicalSize::new(1920.0, 1080.0), PhysicalSize::new(1920.0, 1080.0)), // 100%
+ (LogicalSize::new(1536.0, 864.0), PhysicalSize::new(1920.0, 1080.0)), // 125%
+ (LogicalSize::new(1280.0, 720.0), PhysicalSize::new(1920.0, 1080.0)), // 150%
+ (
+ LogicalSize::new(1097.142_857_142_857, 617.142_857_142_857),
+ PhysicalSize::new(1920.0, 1080.0),
+ ), // 175%
+ (LogicalSize::new(1280.0, 720.0), PhysicalSize::new(2560.0, 1440.0)), // 200%
+ ];
+
+ for (logical, physical) in scales {
+ let selection = LogicalBounds::new(LogicalPosition::new(0.0, 0.0), logical);
+ let crop = logical_area_to_physical_bounds(selection, logical, physical).unwrap();
+
+ assert_eq!(
+ crop.position().x(),
+ 0.0,
+ "full selection must start at x=0 (logical {logical:?})"
+ );
+ assert_eq!(
+ crop.position().y(),
+ 0.0,
+ "full selection must start at y=0 (logical {logical:?})"
+ );
+ assert_eq!(
+ crop.size().width(),
+ physical.width(),
+ "full selection must span full physical width (logical {logical:?})"
+ );
+ assert_eq!(
+ crop.size().height(),
+ physical.height(),
+ "full selection must span full physical height (logical {logical:?})"
+ );
+ }
+ }
+
+ #[test]
+ fn partial_selection_scales_by_display_dpi() {
+ // 150% display: logical 1280x720 backed by physical 1920x1080.
+ let logical = LogicalSize::new(1280.0, 720.0);
+ let physical = PhysicalSize::new(1920.0, 1080.0);
+ let selection = LogicalBounds::new(
+ LogicalPosition::new(100.0, 100.0),
+ LogicalSize::new(500.0, 300.0),
+ );
+
+ let crop = logical_area_to_physical_bounds(selection, logical, physical).unwrap();
+
+ // Every axis multiplied by the 1.5 device scale.
+ assert_eq!(crop.position().x(), 150.0);
+ assert_eq!(crop.position().y(), 150.0);
+ assert_eq!(crop.size().width(), 750.0);
+ assert_eq!(crop.size().height(), 450.0);
+ }
}