Skip to main content

gizmo_ui/
components.rs

1use gizmo_math::{Vec2, Vec4};
2
3/// Layout style of a UI element.
4///
5/// This is a newtype wrapper around [`taffy::style::Style`] and derefs to it,
6/// so all taffy style fields (flexbox, grid, sizing, spacing, ...) are
7/// available directly.
8#[derive(Clone, Debug, PartialEq)]
9#[derive(Default)]
10pub struct Style(pub taffy::style::Style);
11
12unsafe impl Send for Style {}
13unsafe impl Sync for Style {}
14
15
16impl std::ops::Deref for Style {
17    type Target = taffy::style::Style;
18
19    fn deref(&self) -> &Self::Target {
20        &self.0
21    }
22}
23
24impl std::ops::DerefMut for Style {
25    fn deref_mut(&mut self) -> &mut Self::Target {
26        &mut self.0
27    }
28}
29
30/// Computed layout of a UI element, written back each frame by the layout system.
31///
32/// `size` is the element's width/height and `position` is its top-left corner,
33/// both in window pixel coordinates.
34#[derive(Clone, Copy, Debug, PartialEq)]
35pub struct Node {
36    /// Computed width and height in pixels.
37    pub size: Vec2,
38    /// Computed top-left position in window pixel coordinates.
39    pub position: Vec2,
40}
41
42impl Default for Node {
43    fn default() -> Self {
44        Self {
45            size: Vec2::ZERO,
46            position: Vec2::ZERO,
47        }
48    }
49}
50
51/// Current pointer interaction state of a UI element, updated each frame by the
52/// interaction system.
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54#[derive(Default)]
55#[non_exhaustive]
56pub enum Interaction {
57    /// The pointer is neither over nor pressing the element.
58    #[default]
59    None,
60    /// The pointer is over the element but not pressed.
61    Hovered,
62    /// The pointer is over the element and the primary button is held.
63    Pressed,
64}
65
66
67/// Fill color of a UI element, stored as a linear RGBA vector with each
68/// channel in the `0.0..=1.0` range.
69#[derive(Clone, Copy, Debug, PartialEq)]
70pub struct BackgroundColor(pub Vec4);
71
72impl Default for BackgroundColor {
73    fn default() -> Self {
74        Self(Vec4::new(1.0, 1.0, 1.0, 1.0))
75    }
76}
77
78/// Marker component for the root of a UI tree.
79#[derive(Clone, Copy, Debug, PartialEq, Eq)]
80pub struct UiRoot;
81gizmo_core::impl_component!(Style, Node, Interaction, BackgroundColor, UiRoot);