diff --git a/apps/vectorcraft/src/host_services.rs b/apps/vectorcraft/src/host_services.rs index 94419b1049..2ed939245f 100644 --- a/apps/vectorcraft/src/host_services.rs +++ b/apps/vectorcraft/src/host_services.rs @@ -1,5 +1,7 @@ #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))] use super::portal_save; +#[cfg(windows)] +use super::windows_screen_color; #[cfg(all(windows, not(target_vendor = "win7")))] use super::windows_stylus; use super::{ClipboardProbeFactory, FilePick, PickRequest, Services, clipboard, cursor_theme, fileio, printing}; @@ -354,7 +356,7 @@ pub(crate) fn services(parent: Parent, ui: egui::Context) -> Services { .is_some_and(|h| matches!(h.as_raw(), RawWindowHandle::Xlib(_) | RawWindowHandle::Xcb(_))); let (clip, clip_probe) = (parent.clone(), parent.clone()); let (p1, p2, p3, p4, p5) = (parent.clone(), parent.clone(), parent.clone(), parent.clone(), parent); - #[cfg(any(target_os = "macos", target_os = "linux"))] + #[cfg(any(target_os = "macos", target_os = "linux", windows))] let sampled_ui = ui.clone(); #[cfg(target_os = "linux")] let sampled_parent = p1.clone(); @@ -413,6 +415,8 @@ pub(crate) fn services(parent: Parent, ui: egui::Context) -> Services { screen_color: Some(mac_screen_color::sampler(sampled_ui)), #[cfg(target_os = "linux")] screen_color: Some(linux_screen_color::sampler(sampled_ui, sampled_parent)), + #[cfg(windows)] + screen_color: Some(windows_screen_color::sampler(sampled_ui)), // File → Print: the system's printers and print queue. print: Some(Box::new(printing::SystemPrint)), ..Default::default() diff --git a/apps/vectorcraft/src/main.rs b/apps/vectorcraft/src/main.rs index 8a892a381a..adc52f9d8c 100644 --- a/apps/vectorcraft/src/main.rs +++ b/apps/vectorcraft/src/main.rs @@ -77,6 +77,7 @@ mod window_frame; mod windows_alt_codes; #[cfg(windows)] mod windows_cores; +mod windows_screen_color; mod windows_stylus; mod xwayland; diff --git a/apps/vectorcraft/src/windows_screen_color.rs b/apps/vectorcraft/src/windows_screen_color.rs new file mode 100644 index 0000000000..9c44bd6e53 --- /dev/null +++ b/apps/vectorcraft/src/windows_screen_color.rs @@ -0,0 +1,606 @@ +//! Windows: Sample Color from Screen (`vectorcraft_ui_egui::screen_color`). Windows has no system +//! color sampler, so a thread of its own copies the virtual screen, the bounding rectangle of all +//! the monitors (), +//! into a bitmap with `BitBlt` from the screen's device context +//! (), then covers +//! the virtual screen with a borderless topmost window that shows that copy under a crosshair +//! cursor. The window has its own message loop on that thread, so the UI keeps drawing. +//! +//! A left click answers the copied pixel under the pointer as sRGB; Escape, a click of any other +//! button, closing the window, and the window losing the keyboard focus or the mouse capture answer +//! `None`. Each +//! click is answered when its button is released, so the release does not reach the window +//! underneath. The color comes from the copy taken before the window was shown, so the window +//! never samples itself, and moving content shows the frame it had when the copy was taken. +//! +//! The pixels are 8-bit GDI colors, read as sRGB: GDI content is taken to be sRGB when the display +//! does not use Advanced Color +//! (). The colors +//! of HDR and wide-gamut displays are not converted. +//! +//! winit makes the process per-monitor DPI aware, so the virtual screen, the pointer and the copy +//! are in physical pixels on every monitor, whatever its scale, and a monitor left of or above the +//! main one has negative coordinates +//! (). +//! +//! The Win32 calls have no safe binding; [`overlay`] allows `unsafe_code`. +#![cfg_attr(not(windows), allow(dead_code))] + +/// The status message for every failure; the details go to the log. +const FAILED: &str = "the screen color sampler failed"; + +/// The largest copy of the virtual screen, in bytes of 32-bit pixels: 1 GiB, eight 7680 × 4320 +/// monitors. VectorCraft's own limit. +const MAX_SNAPSHOT_BYTES: u64 = 1 << 30; + +/// `CLR_INVALID` (`wingdi.h`): what `GetPixel` answers for a pixel it can't read. +const CLR_INVALID: u32 = 0xFFFF_FFFF; + +/// The virtual screen in physical pixels: its top-left corner (negative when a monitor is left of +/// or above the main one) and its size. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct VirtualScreen { + left: i32, + top: i32, + width: i32, + height: i32, +} + +impl VirtualScreen { + /// The virtual screen `GetSystemMetrics` reports, checked before any memory is taken for its + /// copy: an error when it is empty, larger than [`MAX_SNAPSHOT_BYTES`], or reaches past the + /// coordinates Windows can address. + fn new(left: i32, top: i32, width: i32, height: i32) -> Result { + if width <= 0 || height <= 0 { + return Err(format!("the virtual screen is {width} × {height} pixels")); + } + let bytes = u64::from(width.unsigned_abs()).saturating_mul(u64::from(height.unsigned_abs())).saturating_mul(4); + if bytes > MAX_SNAPSHOT_BYTES { + return Err(format!("the virtual screen ({width} × {height} pixels) is larger than the {MAX_SNAPSHOT_BYTES}-byte limit")); + } + if left.checked_add(width).is_none() || top.checked_add(height).is_none() { + return Err(format!("the virtual screen at ({left}, {top}) reaches past the coordinate range")); + } + Ok(Self { left, top, width, height }) + } + + /// The pixel of the copy at the screen point (`x`, `y`), or `None` when the point is outside the + /// virtual screen. + fn pixel(&self, x: i32, y: i32) -> Option<(i32, i32)> { + let (px, py) = (x.checked_sub(self.left)?, y.checked_sub(self.top)?); + ((0..self.width).contains(&px) && (0..self.height).contains(&py)).then_some((px, py)) + } +} + +/// The sRGB components (0 to 1) of a `COLORREF` (`0x00bbggrr`), or `None` for `CLR_INVALID` or any +/// other value with its high byte set. +fn srgb(colorref: u32) -> Option<[f32; 3]> { + if colorref == CLR_INVALID { + return None; + } + let [r, g, b, high] = colorref.to_le_bytes(); + (high == 0).then(|| [r, g, b].map(|v| f32::from(v) / 255.0)) +} + +/// A mouse button: the left one picks, and any other one cancels. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Button { + Left, + Right, + /// The middle button or a side button. + Other, +} + +/// What the person did in the sampler's window. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Input { + Down(Button), + Up(Button), + Escape, + /// The window became the active window. + Activated, + /// The window stopped being the active window. + Deactivated, + /// Another window took the mouse capture. + CaptureLost, + /// The window was asked to close (Alt+F4). + Closed, +} + +/// What the sampler does next. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Step { + Wait, + Pick, + Cancel, +} + +/// The mouse button last pressed in the window, and whether the window has been active. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +struct Press { + down: Option