hex/src/rune_ui/meeting_capsules/screen.rs
Dax 9a00e8e944
fix(meetings): snap a dragged capsule to the new corner on windows and linux x11 (#1978)
* fix(meetings): snap a dragged capsule on Windows and Linux X11

The recording pill (and a panel capsule) followed the pointer but always
came back to the corner it started in off macOS: primary_screen_size
returned None there, so a release had no screen to measure the throw
against. It moves to meeting_capsules/screen.rs and gains a Windows
implementation (the primary monitor over its DPI scale) and an X11 one
(the primary RandR monitor over Xft.dpi, the root window without RandR).
Wayland still reports no screen, so a capsule there returns to its corner.

* fix(meetings): measure a capsule's screen in Rune's own points

Windows places a capsule's top and bottom edges inside the primary
monitor's work area, so its height is the work area's, while its x uses
the whole monitor. The scale now comes from the view's Cx::env(), the
same scale Rune divides pointer pixels by, instead of a copy of winit's
Xft.dpi rules; a capsule floating on XWayland keeps the X11 path, since
Rune places it by RandR there. The screen lookup is a field on the
capsule, so the drag tests stub it and assert the exact corners they
always did, and a new test covers a release that lands in another corner.

* docs(meetings): name where a capsule drag snaps

A capsule snaps on Windows, on X11 desktops, and floated on XWayland
with the GNOME call-recording Labs option; only a native Wayland capsule
still returns to its corner, so the docs say that instead of blaming
Wayland as a whole. The test runtime's missing screen is named for what
it is, and its scale starts where Rune's own starts.
2026-11-18 18:36:40 -05:00

89 lines
3.4 KiB
Rust

//! The screen a capsule's drag is measured on.
//!
//! Rune places an overlay's window on the primary display's edge, and a
//! release lands in the corner nearest the window's centre (the throw in
//! `throw`), so the size wanted here is that display's in points:
//! [`rune::Drag::dx`] and a capsule's window size are both in points, device
//! pixels over the window's scale. That scale is Rune's own, read from the
//! view's [`rune::Cx::env`], so a capsule measures the display the way Rune
//! laid it out.
/// The primary screen in points, where Rune places the capsules. macOS
/// already answers in points.
#[cfg(target_os = "macos")]
pub(super) fn primary_screen_size(_scale: f32) -> Option<(f32, f32)> {
let mtm = objc2::MainThreadMarker::new()?;
let screen = objc2_app_kit::NSScreen::screens(mtm).iter().next()?;
let size = screen.frame().size;
Some((size.width as f32, size.height as f32))
}
/// Windows places an overlay's `x` on the whole primary monitor and its top
/// or bottom edge inside that monitor's work area, so the height is the work
/// area's.
#[cfg(target_os = "windows")]
pub(super) fn primary_screen_size(scale: f32) -> Option<(f32, f32)> {
let monitor = scrawl_capture::win::primary_monitor()?;
Some((
points(monitor.rect[2] as f32, scale),
points(monitor.work[3] as f32, scale),
))
}
/// X11's primary RandR monitor in points, which is what Rune places an X11 or
/// XWayland overlay against. A capsule on a native Wayland surface has no
/// RandR behind it, so that is not supported on Wayland yet and its drag
/// returns to the corner it started in.
#[cfg(target_os = "linux")]
pub(super) fn primary_screen_size(scale: f32) -> Option<(f32, f32)> {
use x11rb::connection::Connection as _;
use x11rb::protocol::randr::ConnectionExt as _;
// A capsule floating on XWayland is an X11 window placed by RandR, even
// though the session is Wayland.
if crate::linux::session::LinuxSession::detect().is_wayland()
&& !crate::linux::meeting_capsules::on_xwayland()
{
return None;
}
let (connection, screen) = x11rb::connect(None).ok()?;
let root = &connection.setup().roots[screen];
// RandR 1.5 names the primary monitor; without it the root window spans
// every display. Only the size decides a corner, not the origin.
let monitors = connection
.randr_get_monitors(root.root, true)
.ok()
.and_then(|cookie| cookie.reply().ok())
.map(|reply| reply.monitors)
.unwrap_or_default();
let (width, height) = monitors
.iter()
.find(|monitor| monitor.primary)
.or_else(|| monitors.first())
.map_or((root.width_in_pixels, root.height_in_pixels), |monitor| {
(monitor.width, monitor.height)
});
Some((
points(f32::from(width), scale),
points(f32::from(height), scale),
))
}
/// Device pixels to points. A scale Rune hasn't set yet (0) counts as 1.
#[cfg_attr(not(any(target_os = "windows", target_os = "linux")), allow(dead_code))]
fn points(pixels: f32, scale: f32) -> f32 {
pixels / if scale > 0.0 { scale } else { 1.0 }
}
#[cfg(test)]
mod tests {
use super::points;
#[test]
fn device_pixels_become_points() {
assert_eq!(points(2880.0, 2.0), 1440.0);
assert_eq!(points(1920.0, 1.0), 1920.0);
// Before Rune sets the view's scale, points are pixels.
assert_eq!(points(1920.0, 0.0), 1920.0);
}
}