1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use crate::{
environment::LayoutEnvironment,
event::{Event, EventResult},
focus::{DefaultFocus, FocusAction, FocusDirection},
layout::{Alignment, ResolvedLayout},
primitives::{Point, ProposedDimension, ProposedDimensions},
view::{ViewLayout, ViewMarker},
};
/// A view that uses the layout of the modified view, rendering the overlay
/// on top of it.
#[derive(Debug, Clone)]
pub struct OverlayView<T, U> {
foreground: T,
overlay: U,
alignment: Alignment,
}
impl<T: ViewMarker, U: ViewMarker> OverlayView<T, U> {
pub const fn new(foreground: T, overlay: U, alignment: Alignment) -> Self {
Self {
foreground,
overlay,
alignment,
}
}
}
/// Focus tree for overlay view - tracks which child has focus
#[derive(Debug, Clone)]
pub enum OverlayFocus<T, U> {
/// Focus is on the foreground
Foreground(T),
/// Focus is on the overlay
Overlay(U),
}
impl<T: DefaultFocus, U: DefaultFocus> DefaultFocus for OverlayFocus<T, U> {
fn default_first() -> Self {
Self::Overlay(U::default_first())
}
fn default_last() -> Self {
Self::Foreground(T::default_last())
}
}
impl<T, U> ViewMarker for OverlayView<T, U>
where
T: ViewMarker,
U: ViewMarker,
{
type Renderables = (T::Renderables, U::Renderables);
type Transition = T::Transition;
}
impl<Captures: ?Sized, T, U> ViewLayout<Captures> for OverlayView<T, U>
where
T: ViewLayout<Captures>,
U: ViewLayout<Captures>,
{
type Sublayout = ResolvedLayout<T::Sublayout>;
// Tuples are rendered first to last
type State = (T::State, U::State);
type FocusTree = OverlayFocus<T::FocusTree, U::FocusTree>;
fn priority(&self) -> i8 {
self.foreground.priority()
}
fn is_empty(&self) -> bool {
self.foreground.is_empty()
}
fn transition(&self) -> Self::Transition {
self.foreground.transition()
}
fn build_state(&self, captures: &mut Captures) -> Self::State {
(
self.foreground.build_state(captures),
self.overlay.build_state(captures),
)
}
fn layout(
&self,
offer: &ProposedDimensions,
env: &impl LayoutEnvironment,
captures: &mut Captures,
state: &mut Self::State,
) -> ResolvedLayout<Self::Sublayout> {
self.foreground
.layout(offer, env, captures, &mut state.0)
.nested()
}
fn render_tree(
&self,
layout: &Self::Sublayout,
origin: Point,
env: &impl LayoutEnvironment,
captures: &mut Captures,
state: &mut Self::State,
) -> Self::Renderables {
let foreground_size = layout.resolved_size;
// Propose the foreground size to the overlay
// This would benefit from splitting layout into separate functions for the various offers
let overlay_offer = ProposedDimensions {
width: ProposedDimension::Exact(foreground_size.width.into()),
height: ProposedDimension::Exact(foreground_size.height.into()),
};
let overlay_layout = self
.overlay
.layout(&overlay_offer, env, captures, &mut state.1);
let new_origin = origin
+ Point::new(
self.alignment.horizontal().align(
layout.resolved_size.width.into(),
overlay_layout.resolved_size.width.into(),
),
self.alignment.vertical().align(
layout.resolved_size.height.into(),
overlay_layout.resolved_size.height.into(),
),
);
(
self.foreground
.render_tree(&layout.sublayouts, origin, env, captures, &mut state.0),
self.overlay.render_tree(
&overlay_layout.sublayouts,
new_origin,
env,
captures,
&mut state.1,
),
)
}
#[expect(clippy::too_many_lines)]
fn handle_event(
&self,
event: &crate::view::Event,
context: &crate::event::EventContext,
render_tree: &mut Self::Renderables,
captures: &mut Captures,
state: &mut Self::State,
focus: &mut Self::FocusTree,
) -> EventResult {
// Handle focus events specially - they need to route through the focus tree
if let Event::Focus {
action: focus_event,
group,
} = event
{
match focus {
OverlayFocus::Overlay(overlay_focus) => {
let result = self.overlay.handle_event(
event,
context,
&mut render_tree.1,
captures,
&mut state.1,
overlay_focus,
);
if result.is_handled() || *focus_event == FocusAction::Teardown {
return result;
}
// Overlay exhausted - only move to foreground on forward navigation
match focus_event {
FocusAction::Focus(FocusDirection::Forward) | FocusAction::Next => {
// Move to foreground
let mut foreground_focus = DefaultFocus::default_first();
let result = self.foreground.handle_event(
&Event::Focus {
action: FocusAction::Focus(FocusDirection::Forward),
group: *group,
},
context,
&mut render_tree.0,
captures,
&mut state.0,
&mut foreground_focus,
);
*focus = OverlayFocus::Foreground(foreground_focus);
result
}
FocusAction::Focus(FocusDirection::Backward)
| FocusAction::Previous
| FocusAction::Blur
| FocusAction::Select
| FocusAction::Teardown => EventResult::Deferred,
}
}
OverlayFocus::Foreground(foreground_focus) => {
let result = self.foreground.handle_event(
event,
context,
&mut render_tree.0,
captures,
&mut state.0,
foreground_focus,
);
if result.is_handled() || *focus_event == FocusAction::Teardown {
return result;
}
// Foreground exhausted - only move to overlay on backward navigation
match focus_event {
FocusAction::Focus(FocusDirection::Backward) | FocusAction::Previous => {
let mut overlay_focus = DefaultFocus::default_last();
let result = self.overlay.handle_event(
&Event::Focus {
action: FocusAction::Focus(FocusDirection::Backward),
group: *group,
},
context,
&mut render_tree.1,
captures,
&mut state.1,
&mut overlay_focus,
);
*focus = OverlayFocus::Overlay(overlay_focus);
result
}
FocusAction::Focus(FocusDirection::Forward)
| FocusAction::Next
| FocusAction::Select
| FocusAction::Blur
| FocusAction::Teardown => EventResult::Deferred,
}
}
}
} else {
// For non-focus events (touch, scroll, etc.), perform DFS
match focus {
OverlayFocus::Overlay(_) => {
// Start with overlay
let overlay_result = self.overlay.handle_event(
event,
context,
&mut render_tree.1,
captures,
&mut state.1,
match focus {
OverlayFocus::Overlay(focus) => focus,
OverlayFocus::Foreground(_) => unreachable!(),
},
);
if overlay_result.is_handled() {
return overlay_result;
}
// Then foreground
*focus = OverlayFocus::Foreground(DefaultFocus::default_first());
self.foreground.handle_event(
event,
context,
&mut render_tree.0,
captures,
&mut state.0,
match focus {
OverlayFocus::Foreground(focus) => focus,
OverlayFocus::Overlay(_) => unreachable!(),
},
)
}
OverlayFocus::Foreground(_) => {
// Start with overlay
*focus = OverlayFocus::Overlay(DefaultFocus::default_first());
let overlay_result = self.overlay.handle_event(
event,
context,
&mut render_tree.1,
captures,
&mut state.1,
match focus {
OverlayFocus::Overlay(focus) => focus,
OverlayFocus::Foreground(_) => unreachable!(),
},
);
if overlay_result.is_handled() {
return overlay_result;
}
// Then foreground
*focus = OverlayFocus::Foreground(DefaultFocus::default_first());
self.foreground.handle_event(
event,
context,
&mut render_tree.0,
captures,
&mut state.0,
match focus {
OverlayFocus::Foreground(focus) => focus,
OverlayFocus::Overlay(_) => unreachable!(),
},
)
}
}
}
}
}