Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: size X11 target overlays in logical pixels

jatmnf0a8217aebc3
diff --git a/apps/desktop-gpui/docs/x11-overlay-hidpi.png b/apps/desktop-gpui/docs/x11-overlay-hidpi.png
new file mode 100644
index 000000000..fd9723696
Binary files /dev/null and b/apps/desktop-gpui/docs/x11-overlay-hidpi.png differ
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index ae2ecce79..c2f4b6ae1 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -2386,6 +2386,12 @@ fn open_overlay(
};
let width = bounds.size().width();
let height = bounds.size().height();
+ let main = cx.global::<AppWindows>().main;
+ let capture_scale = main
+ .update(cx, |_, window, _| {
+ crate::target_overlay::capture_scale_factor(window)
+ })
+ .unwrap_or(1.);
#[cfg(target_os = "windows")]
let origin = point(
px(bounds.position().x() as f32),
@@ -2395,7 +2401,10 @@ fn open_overlay(
let origin = point(px(0.), px(0.));
let window_bounds = WindowBounds::Windowed(Bounds {
origin,
- size: size(px(width as f32), px(height as f32)),
+ size: size(
+ px(width as f32 / capture_scale),
+ px(height as f32 / capture_scale),
+ ),
});
#[cfg(target_os = "linux")]
let overlay_display = if uses_wayland() {
diff --git a/apps/desktop-gpui/src/target_overlay.rs b/apps/desktop-gpui/src/target_overlay.rs
index feb0e2a44..7df0afa33 100644
--- a/apps/desktop-gpui/src/target_overlay.rs
+++ b/apps/desktop-gpui/src/target_overlay.rs
@@ -87,6 +87,25 @@ fn overlay_viewport_size(viewport: (f32, f32), previous: (f32, f32)) -> (f32, f3
}
}
+pub(crate) fn capture_scale_factor(window: &Window) -> f32 {
+ #[cfg(target_os = "linux")]
+ {
+ use raw_window_handle::{HasWindowHandle, RawWindowHandle};
+
+ if HasWindowHandle::window_handle(window).is_ok_and(|handle| {
+ matches!(
+ handle.as_raw(),
+ RawWindowHandle::Xlib(_) | RawWindowHandle::Xcb(_)
+ )
+ }) {
+ // X11 capture bounds are physical pixels, even when scap calls them logical.
+ return window.scale_factor();
+ }
+ }
+ let _ = window;
+ 1.
+}
+
fn recording_devices_available(cx: &App) -> bool {
crate::session::RecordingSession::global(cx).read(cx).phase == crate::session::Phase::Idle
&& !app_windows::clean_capture_owned(cx)
@@ -222,8 +241,8 @@ pub struct HoveredWindow {
pub id: WindowId,
pub app_name: String,
pub display_id: DisplayId,
- /// Bounds relative to the display's top-left, in logical points -- exactly
- /// what the overlay window's own coordinate space is. Carried as our own
+ /// Bounds relative to the display's top-left in capture coordinates;
+ /// X11 overlays convert these physical pixels to logical points. Carried as our own
/// rect rather than `LogicalBounds` so the poll can compare frames for
/// equality (`scap_targets`' bounds are not `PartialEq`).
pub bounds: AreaRect,
@@ -479,6 +498,15 @@ impl AreaRect {
})
}
+ fn scaled(self, scale: f32) -> Self {
+ Self {
+ x: self.x * scale,
+ y: self.y * scale,
+ width: self.width * scale,
+ height: self.height * scale,
+ }
+ }
+
fn to_bounds(self) -> LogicalBounds {
LogicalBounds::new(
LogicalPosition::new(self.x as f64, self.y as f64),
@@ -583,6 +611,7 @@ pub struct OverlayWindow {
/// Logical size, which is this window's size and the space every
/// coordinate here lives in.
display_size: (f32, f32),
+ capture_scale: f32,
controls_work_area: AreaRect,
/// Physical pixels, for the `1920x1080 · 60FPS` line.
physical_size: Option<(u32, u32)>,
@@ -648,9 +677,15 @@ impl OverlayWindow {
.ok()
.map(|main| cx.observe(&main, |_, _, cx| cx.notify()));
+ let capture_scale = capture_scale_factor(window);
let logical = display
.logical_size()
- .map(|size| (size.width() as f32, size.height() as f32))
+ .map(|size| {
+ (
+ size.width() as f32 / capture_scale,
+ size.height() as f32 / capture_scale,
+ )
+ })
.unwrap_or((1920., 1080.));
let controls_work_area = display
@@ -658,8 +693,8 @@ impl OverlayWindow {
.logical_bounds()
.zip(app_windows::display_work_area(Some(display), cx))
.map(|(bounds, available)| AreaRect {
- x: f32::from(available.origin.x) - bounds.position().x() as f32,
- y: f32::from(available.origin.y) - bounds.position().y() as f32,
+ x: f32::from(available.origin.x) - bounds.position().x() as f32 / capture_scale,
+ y: f32::from(available.origin.y) - bounds.position().y() as f32 / capture_scale,
width: f32::from(available.size.width),
height: f32::from(available.size.height),
})
@@ -679,6 +714,7 @@ impl OverlayWindow {
.name()
.unwrap_or_else(|| format!("Display {}", display.id())),
display_size: logical,
+ capture_scale,
controls_work_area,
physical_size: display
.physical_size()
@@ -738,7 +774,7 @@ impl OverlayWindow {
if self.recording_area {
return self.crop.map(|crop| ScreenCaptureTarget::Area {
screen: self.display_id.clone(),
- bounds: crop.to_bounds(),
+ bounds: crop.scaled(self.capture_scale).to_bounds(),
});
}
let select = self.select.read(cx);
@@ -757,7 +793,7 @@ impl OverlayWindow {
TargetType::Area => self.crop.filter(|crop| crop.is_valid_for(min)).map(|crop| {
ScreenCaptureTarget::Area {
screen: self.display_id.clone(),
- bounds: crop.to_bounds(),
+ bounds: crop.scaled(self.capture_scale).to_bounds(),
}
}),
TargetType::CameraOnly => Some(ScreenCaptureTarget::CameraOnly),
@@ -833,7 +869,7 @@ impl OverlayWindow {
return;
}
let display = self.display_id.clone();
- let crop = self.crop;
+ let crop = self.crop.map(|crop| crop.scaled(self.capture_scale));
let epoch = app_windows::camera_picker_epoch(cx);
cx.defer(move |cx| {
if app_windows::camera_picker_epoch(cx) == epoch {
@@ -1323,7 +1359,7 @@ impl OverlayWindow {
let Some(active) = active else {
return div().size_full().into_any_element();
};
- let bounds = active.bounds;
+ let bounds = active.bounds.scaled(1. / self.capture_scale);
let width = bounds.width;
let height = bounds.height;
@@ -1375,12 +1411,10 @@ impl OverlayWindow {
.font_weight(FontWeight::BOLD)
.child(SharedString::from(active.app_name.clone())),
)
- .child(
- div()
- .mb(px(8.))
- .text_size(px(12.))
- .child(format!("{}x{}", width as u32, height as u32)),
- ),
+ .child(div().mb(px(8.)).text_size(px(12.)).child(format!(
+ "{}x{}",
+ active.bounds.width as u32, active.bounds.height as u32
+ ))),
)
.child(self.render_controls_cluster(self.target(cx), false, cx)),
)
@@ -2582,6 +2616,24 @@ mod tests {
assert_eq!(bounds.size().height(), 600.);
}
+ #[test]
+ fn scaled_overlay_selections_preserve_the_physical_capture_region() {
+ let capture = rect(300., 240., 900., 600.);
+ for (scale, expected) in [
+ (1., rect(300., 240., 900., 600.)),
+ (1.5, rect(200., 160., 600., 400.)),
+ (2., rect(150., 120., 450., 300.)),
+ ] {
+ let overlay = capture.scaled(1. / scale);
+ assert_eq!(overlay, expected);
+ let bounds = overlay.scaled(scale).to_bounds();
+ assert_eq!(bounds.position().x(), 300.);
+ assert_eq!(bounds.position().y(), 240.);
+ assert_eq!(bounds.size().width(), 900.);
+ assert_eq!(bounds.size().height(), 600.);
+ }
+ }
+
#[test]
fn moving_area_snaps_to_display_centres_and_edges() {
let display = (1000., 800.);