Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/desktop-gpui/src/app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,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),
Expand All @@ -2446,7 +2452,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() {
Expand Down
80 changes: 66 additions & 14 deletions apps/desktop-gpui/src/target_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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)
Expand Down Expand Up @@ -241,8 +260,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,
Expand Down Expand Up @@ -512,6 +531,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),
Expand Down Expand Up @@ -616,6 +644,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)>,
Expand Down Expand Up @@ -722,18 +751,24 @@ impl OverlayWindow {
)
.detach();

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
.raw_handle()
.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),
})
Expand All @@ -753,6 +788,7 @@ impl OverlayWindow {
.name()
.unwrap_or_else(|| format!("Display {}", display.id())),
display_size: logical,
capture_scale,
controls_work_area,
physical_size: display
.physical_size()
Expand Down Expand Up @@ -816,7 +852,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);
Expand All @@ -835,7 +871,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),
Expand Down Expand Up @@ -1499,7 +1535,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;

Expand Down Expand Up @@ -1551,12 +1587,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)),
)
Expand Down Expand Up @@ -2791,6 +2825,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.);
Expand Down
Loading