kernel/geometry.rs
1// Copyright 2026 Mark Alan Boykin
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.
5// SPDX-License-Identifier: MPL-2.0
6
7//! Portable 2-D geometry types used at the host ↔ runtime boundary.
8//!
9//! All three aliases are thin wrappers over `euclid::default::*<f32>`.
10//! They appear on:
11//!
12//! - `FrameHostInput.pointer_hover: Option<PortablePoint>` — hover
13//! position relative to the host viewport.
14//! - `FrameHostInput.viewport_size: PortableSize` — current viewport
15//! extents.
16//! - `FrameViewModel.active_pane_rects: Vec<(PaneId, NodeKey, PortableRect)>`
17//! — pane layout rects.
18//! - `DegradedReceiptSpec.tile_rect: PortableRect` — degraded-mode
19//! overlay placement.
20//!
21//! Egui hosts convert between these and `egui::{Pos2, Vec2, Rect}`
22//! at the widget boundary via helpers in the shell crate's
23//! `compositor_adapter` module; iced hosts consume them directly.
24//! The type aliases themselves are fully portable — `euclid` has no
25//! platform or framework dependencies.
26//!
27//! Pre-M4 slice 8 (2026-04-22) these aliases lived in
28//! `shell/desktop/workbench/compositor_adapter.rs` alongside the
29//! egui conversion helpers. Promoting them here makes the
30//! view-model types they appear in portable without importing the
31//! shell-crate compositor module.
32
33/// Portable rectangle type. `origin` is the top-left corner; `size` is
34/// the rect extent. Uses `f32` throughout.
35pub type PortableRect = euclid::default::Rect<f32>;
36
37/// Portable 2-D point, `f32`-valued. Typically represents a pointer
38/// position or a layout anchor in host-viewport coordinates.
39pub type PortablePoint = euclid::default::Point2D<f32>;
40
41/// Portable 2-D size (width × height), `f32`-valued. Typically
42/// represents a viewport or pane extent in host-viewport coordinates.
43pub type PortableSize = euclid::default::Size2D<f32>;
44
45/// Portable 2-D vector, `f32`-valued. Typically represents a layout or
46/// physics delta in graph/canvas coordinates.
47pub type PortableVector = euclid::default::Vector2D<f32>;
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn rect_constructs_with_origin_and_size() {
55 let r = PortableRect::new(
56 PortablePoint::new(10.0, 20.0),
57 PortableSize::new(300.0, 200.0),
58 );
59 assert_eq!(r.origin.x, 10.0);
60 assert_eq!(r.origin.y, 20.0);
61 assert_eq!(r.size.width, 300.0);
62 assert_eq!(r.size.height, 200.0);
63 }
64
65 #[test]
66 fn rect_contains_point_inside_bounds() {
67 let r = PortableRect::new(PortablePoint::new(0.0, 0.0), PortableSize::new(100.0, 50.0));
68 assert!(r.contains(PortablePoint::new(50.0, 25.0)));
69 assert!(!r.contains(PortablePoint::new(-1.0, 25.0)));
70 assert!(!r.contains(PortablePoint::new(50.0, 51.0)));
71 }
72
73 #[test]
74 fn portable_types_are_send_sync() {
75 fn assert_send_sync<T: Send + Sync>() {}
76 assert_send_sync::<PortableRect>();
77 assert_send_sync::<PortablePoint>();
78 assert_send_sync::<PortableSize>();
79 }
80}