Skip to main content

devela/ui/view/
view.rs

1// devela/src/ui/view/view.rs
2//
3//! Defines [`UiViewFlags`], [`UiView`].
4//
5
6use crate::{UiId, UiLayer, UiRect};
7
8crate::set! {
9    #[doc = crate::_tags!(ui set)]
10    /// Visual presentation flags of a UI view.
11    #[doc = crate::_doc_meta! {
12        location("ui/view", struct UiViewFlags),
13        test_size_of(UiViewFlags = 1|8; niche !Option),
14    }]
15    #[must_use]
16    #[repr(transparent)]
17    pub struct UiViewFlags(u8) {
18        /// Not visually presented.
19        HIDDEN = 0;
20
21        /// Clips visual content to the view rectangle.
22        CLIPPED = 1;
23
24        /// Fully covers views behind it within its rectangle.
25        OPAQUE = 2;
26    }
27}
28
29#[doc = crate::_tags!(ui)]
30/// Frame-local visual record for a UI identity.
31#[doc = crate::_doc_meta! {
32    location("ui/view", struct UiView),
33    #[cfg(target_pointer_width = "32")]
34    test_size_of(UiView = 28|224; niche Option),
35    #[cfg(target_pointer_width = "64")]
36    test_size_of(UiView = 32|256; niche Option),
37}]
38/// Connects a resolved UI identity with the visual region and ordering layer
39/// used for presentation.
40#[must_use]
41#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
42pub struct UiView {
43    id: UiId,
44    rect: UiRect,
45    layer: UiLayer,
46    flags: UiViewFlags,
47}
48#[rustfmt::skip]
49impl UiView {
50    /* constructors */
51
52    /// Constructs a visual record on the base layer with no flags.
53    pub const fn new(id: UiId, rect: UiRect) -> Self {
54        Self { id, rect, layer: UiLayer::BASE, flags: UiViewFlags::new() }
55    }
56
57    /// Constructs a visual record from all parts.
58    pub const fn from_parts(id: UiId, rect: UiRect, layer: UiLayer, flags: UiViewFlags)
59        -> Self { Self { id, rect, layer, flags } }
60
61    /* queries */
62
63    /// Returns the UI identity.
64    pub const fn id(&self) -> UiId { self.id }
65
66    /// Returns the visual rectangle.
67    pub const fn rect(&self) -> UiRect { self.rect }
68
69    /// Returns the visual ordering layer.
70    pub const fn layer(&self) -> UiLayer { self.layer }
71
72    /// Returns the visual flags.
73    pub const fn flags(&self) -> UiViewFlags { self.flags }
74
75    /// Returns whether the view is visually hidden.
76    #[must_use]
77    pub const fn is_hidden(&self) -> bool { self.flags.has(UiViewFlags::HIDDEN) }
78    /// Returns whether the view is visually present.
79    #[must_use]
80    pub const fn is_visible(&self) -> bool { !self.is_hidden() }
81
82    /* modifiers */
83
84    /// Returns this view with another rectangle.
85    pub const fn with_rect(self, rect: UiRect) -> Self { Self { rect, ..self } }
86    /// Returns this view with another layer.
87    pub const fn with_layer(self, layer: UiLayer) -> Self { Self { layer, ..self } }
88
89
90    /// Returns this view with another flag set.
91    pub const fn replace_flags(self, flags: UiViewFlags) -> Self { Self { flags, ..self } }
92    /// Returns this view with `flags` included.
93    pub const fn with_flags(self, flags: UiViewFlags) -> Self {
94        Self { flags: self.flags.with(flags), ..self }
95    }
96    /// Returns this view with `flags` removed.
97    pub const fn without_flags(self, flags: UiViewFlags) -> Self {
98        Self { flags: self.flags.without(flags), ..self }
99    }
100}