1use ahash::HashMap;
2
3use crate::{Id, IdMap, LayerId, Rect, Sense, WidgetInfo};
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub struct WidgetRect {
10 pub id: Id,
19
20 pub layer_id: LayerId,
22
23 pub rect: Rect,
25
26 pub interact_rect: Rect,
30
31 pub sense: Sense,
40
41 pub enabled: bool,
43}
44
45impl WidgetRect {
46 pub fn transform(self, transform: emath::TSTransform) -> Self {
47 let Self {
48 id,
49 layer_id,
50 rect,
51 interact_rect,
52 sense,
53 enabled,
54 } = self;
55 Self {
56 id,
57 layer_id,
58 rect: transform * rect,
59 interact_rect: transform * interact_rect,
60 sense,
61 enabled,
62 }
63 }
64}
65
66#[derive(Default, Clone)]
71pub struct WidgetRects {
72 by_layer: HashMap<LayerId, Vec<WidgetRect>>,
74
75 by_id: IdMap<(usize, WidgetRect)>,
77
78 infos: IdMap<WidgetInfo>,
83}
84
85impl PartialEq for WidgetRects {
86 fn eq(&self, other: &Self) -> bool {
87 self.by_layer == other.by_layer
88 }
89}
90
91impl WidgetRects {
92 pub fn layer_ids(&self) -> impl ExactSizeIterator<Item = LayerId> + '_ {
94 self.by_layer.keys().copied()
95 }
96
97 pub fn layers(&self) -> impl Iterator<Item = (&LayerId, &[WidgetRect])> + '_ {
98 self.by_layer
99 .iter()
100 .map(|(layer_id, rects)| (layer_id, &rects[..]))
101 }
102
103 #[inline]
104 pub fn get(&self, id: Id) -> Option<&WidgetRect> {
105 self.by_id.get(&id).map(|(_, w)| w)
106 }
107
108 pub fn order(&self, id: Id) -> Option<(LayerId, usize)> {
110 self.by_id.get(&id).map(|(idx, w)| (w.layer_id, *idx))
111 }
112
113 #[inline]
114 pub fn contains(&self, id: Id) -> bool {
115 self.by_id.contains_key(&id)
116 }
117
118 #[inline]
120 pub fn get_layer(&self, layer_id: LayerId) -> impl Iterator<Item = &WidgetRect> + '_ {
121 self.by_layer.get(&layer_id).into_iter().flatten()
122 }
123
124 pub fn clear(&mut self) {
126 let Self {
127 by_layer,
128 by_id,
129 infos,
130 } = self;
131
132 for rects in by_layer.values_mut() {
133 rects.clear();
134 }
135
136 by_id.clear();
137
138 infos.clear();
139 }
140
141 pub fn insert(&mut self, layer_id: LayerId, widget_rect: WidgetRect) {
143 let Self {
144 by_layer,
145 by_id,
146 infos: _,
147 } = self;
148
149 let layer_widgets = by_layer.entry(layer_id).or_default();
150
151 match by_id.entry(widget_rect.id) {
152 std::collections::hash_map::Entry::Vacant(entry) => {
153 let idx_in_layer = layer_widgets.len();
155 entry.insert((idx_in_layer, widget_rect));
156 layer_widgets.push(widget_rect);
157 }
158 std::collections::hash_map::Entry::Occupied(mut entry) => {
159 let (idx_in_layer, existing) = entry.get_mut();
162
163 debug_assert!(
164 existing.layer_id == widget_rect.layer_id,
165 "Widget {:?} changed layer_id during the frame from {:?} to {:?}",
166 widget_rect.id,
167 existing.layer_id,
168 widget_rect.layer_id
169 );
170
171 existing.rect = widget_rect.rect; existing.interact_rect = widget_rect.interact_rect; existing.sense |= widget_rect.sense;
175 existing.enabled |= widget_rect.enabled;
176
177 if existing.layer_id == widget_rect.layer_id {
178 layer_widgets[*idx_in_layer] = *existing;
179 }
180 }
181 }
182 }
183
184 pub fn set_info(&mut self, id: Id, info: WidgetInfo) {
185 self.infos.insert(id, info);
186 }
187
188 pub fn info(&self, id: Id) -> Option<&WidgetInfo> {
189 self.infos.get(&id)
190 }
191}