#[cfg(feature = "displays")]
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg(feature = "displays")]
pub struct DisplayRect {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
}
#[cfg(feature = "displays")]
impl DisplayRect {
pub const fn new(x: i32, y: i32, width: i32, height: i32) -> Self {
Self {
x,
y,
width,
height,
}
}
pub fn right(&self) -> i32 {
self.x + self.width
}
pub fn bottom(&self) -> i32 {
self.y + self.height
}
pub fn contains(&self, x: i32, y: i32) -> bool {
x >= self.x && x < self.right() && y >= self.y && y < self.bottom()
}
pub fn contains_rect(&self, other: &DisplayRect) -> bool {
other.x >= self.x
&& other.y >= self.y
&& other.right() <= self.right()
&& other.bottom() <= self.bottom()
}
}
#[cfg(feature = "displays")]
impl fmt::Display for DisplayRect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}+{}+{}", self.width, self.height, self.x, self.y)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg(feature = "displays")]
pub struct DisplayInfo {
pub id: u32,
pub name: String,
pub bounds: DisplayRect,
pub work_area: DisplayRect,
pub scale_factor: f64,
pub primary: bool,
}
#[cfg(feature = "displays")]
impl DisplayInfo {
pub fn new(
id: u32,
name: &str,
bounds: DisplayRect,
work_area: DisplayRect,
scale_factor: f64,
primary: bool,
) -> Self {
Self {
id,
name: name.to_owned(),
bounds,
work_area,
scale_factor,
primary,
}
}
pub fn dpi(&self) -> u32 {
(self.scale_factor * 96.0).round() as u32
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct DisplayCapabilities {
pub enumeration: bool,
pub dpi_change_events: bool,
}
impl DisplayCapabilities {
pub const fn none() -> Self {
Self {
enumeration: false,
dpi_change_events: false,
}
}
pub const fn all() -> Self {
Self {
enumeration: true,
dpi_change_events: true,
}
}
pub const fn is_supported(&self) -> bool {
self.enumeration || self.dpi_change_events
}
}
pub fn logical_pointer_projection(x: f32, y: f32, scale: f64) -> (f32, f32) {
let scale = scale.max(1.0) as f32;
(x / scale, y / scale)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rect_math_and_containment_are_half_open() {
let rect = DisplayRect::new(10, 20, 100, 50);
assert_eq!(rect.right(), 110);
assert_eq!(rect.bottom(), 70);
assert!(rect.contains(10, 20));
assert!(rect.contains(109, 69));
assert!(!rect.contains(110, 69));
assert!(!rect.contains(109, 70));
assert!(!rect.contains(9, 20));
assert!(rect.contains_rect(&DisplayRect::new(10, 20, 100, 50)));
assert!(rect.contains_rect(&DisplayRect::new(60, 40, 10, 10)));
assert!(!rect.contains_rect(&DisplayRect::new(60, 40, 60, 10)));
assert_eq!(rect.to_string(), "100x50+10+20");
}
#[test]
fn display_info_derives_dpi_from_scale() {
let display = DisplayInfo::new(
7,
"mock-primary",
DisplayRect::new(0, 0, 1920, 1080),
DisplayRect::new(0, 0, 1920, 1040),
1.25,
true,
);
assert_eq!(display.dpi(), 120);
assert!(display.primary);
assert_eq!(
display.work_area,
DisplayRect::new(0, 0, 1920, 1040),
"work area keeps its own rectangle"
);
let secondary = DisplayInfo::new(
8,
"mock-secondary",
DisplayRect::new(1920, 0, 1280, 1024),
DisplayRect::new(1920, 0, 1280, 984),
1.0,
false,
);
assert_eq!(secondary.dpi(), 96);
assert_ne!(display, secondary);
}
#[test]
fn capabilities_default_to_none_and_expose_all() {
assert_eq!(DisplayCapabilities::default(), DisplayCapabilities::none());
assert!(!DisplayCapabilities::default().is_supported());
assert!(DisplayCapabilities::all().enumeration);
assert!(DisplayCapabilities::all().dpi_change_events);
assert!(DisplayCapabilities::all().is_supported());
let enumeration_only = DisplayCapabilities {
enumeration: true,
dpi_change_events: false,
};
assert!(enumeration_only.is_supported());
assert!(!DisplayCapabilities::none().is_supported());
}
#[test]
fn logical_pointer_projection_divides_by_scale() {
assert_eq!(
logical_pointer_projection(125.0, 250.0, 1.25),
(100.0, 200.0)
);
assert_eq!(
logical_pointer_projection(300.0, 150.0, 1.5),
(200.0, 100.0)
);
assert_eq!(
logical_pointer_projection(400.0, 400.0, 2.0),
(200.0, 200.0)
);
}
#[test]
fn logical_pointer_projection_is_identity_at_100_percent() {
assert_eq!(logical_pointer_projection(10.0, 20.0, 1.0), (10.0, 20.0));
assert_eq!(logical_pointer_projection(10.0, 20.0, 0.5), (10.0, 20.0));
assert_eq!(logical_pointer_projection(10.0, 20.0, 0.0), (10.0, 20.0));
assert_eq!(logical_pointer_projection(10.0, 20.0, -3.0), (10.0, 20.0));
}
#[test]
fn logical_pointer_projection_preserves_negative_and_zero() {
assert_eq!(logical_pointer_projection(-125.0, 0.0, 1.25), (-100.0, 0.0));
assert_eq!(logical_pointer_projection(0.0, -250.0, 1.25), (0.0, -200.0));
}
}