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