Skip to main content

bhoo/systems/android/xml/
render.rs

1use super::*;
2
3impl Node {
4    /// Set text in a TextSwitcher, optionally switching to its next child.
5    /// The switcher must have a stable XML ID when nested in another layout.
6    pub fn set_switcher_text(&self, state: &mut LayoutState, text: &str, advance: bool) -> bool {
7        if !matches!(self.kind, Kind::TextSwitcher) || self.children.is_empty() { return false; }
8        let key = if self.id.is_empty() { format!("#{}", view_id(self.id)) } else { self.id.to_string() };
9        let selected = state.displayed_children.entry(key).or_default();
10        if *selected >= self.children.len() { *selected = 0; }
11        if advance { *selected = (*selected + 1) % self.children.len(); }
12        let child = &self.children[*selected];
13        let child_key = if child.id.is_empty() { format!("#{}", anonymous_child_id(view_id(self.id), *selected)) } else { child.id.to_string() };
14        state.text.insert(child_key, text.to_owned());
15        true
16    }
17    /// Set the image of the next ImageSwitcher child and select it.
18    pub fn set_switcher_image(&self, state: &mut LayoutState, source: &str) -> bool {
19        if !matches!(self.kind, Kind::ImageSwitcher) || self.children.is_empty() || !state.images.contains_key(source) { return false; }
20        let key = if self.id.is_empty() { format!("#{}", view_id(self.id)) } else { self.id.to_string() };
21        let selected = state.displayed_children.entry(key).or_default();
22        if *selected >= self.children.len() { *selected = 0; }
23        *selected = (*selected + 1) % self.children.len();
24        let child = &self.children[*selected];
25        let child_key = if child.id.is_empty() { format!("#{}", anonymous_child_id(view_id(self.id), *selected)) } else { child.id.to_string() };
26        state.image_sources.insert(child_key, source.to_owned());
27        true
28    }
29    /// Paint an explicitly shown floating MediaController at the bottom of its anchor.
30    pub fn render_floating_media_controller(&'static self,f:&mut Frame<'_>,anchor:Rect,height:f32,state:&mut LayoutState)->Option<&'static str> {
31        if !matches!(self.kind,Kind::MediaController)||self.id.is_empty()||!height.is_finite(){return None;}
32        let key=self.id;let now=f.time();let slider_id=anonymous_child_id(view_id(key),view_id("mediacontroller_progress") as usize);
33        let mut floating=state.media_floating.remove(key)?;if !floating.visible{return None;}
34        let was_dragging=floating.dragging;let dragging_before=f.ui.active==Some(slider_id);
35        if !dragging_before&&!was_dragging&&floating.deadline.is_some_and(|deadline|now>=deadline){return None;}
36        let h=height.max(0.0).min(anchor.height);let bounds=Rect{x:anchor.x,y:anchor.y+anchor.height-h,width:anchor.width,height:h};
37        let start=f.commands.len();let action=self.render(f,bounds,state);f.ui.overlays.extend(f.commands.drain(start..));
38        let dragging=f.ui.active==Some(slider_id)&&!f.input.released&&!f.input.cancelled&&!f.input.blurred;floating.dragging=dragging;
39        if action.is_some()||was_dragging&&!dragging {floating.deadline=Some(now+3.0);}
40        if dragging {f.continuous(bounds);} else if let Some(deadline)=floating.deadline {f.wake_at(deadline);}
41        state.media_floating.insert(key.to_owned(),floating);action
42    }
43    pub fn render(
44        &'static self,
45        f: &mut Frame<'_>,
46        bounds: Rect,
47        state: &mut LayoutState,
48    ) -> Option<&'static str> {
49        let previous_theme = state
50            .theme_style
51            .bhoo_theme()
52            .map(|theme| std::mem::replace(&mut f.ui.theme, theme));
53        let action = self.render_inner(f, bounds, state);
54        if let Some(theme) = previous_theme {
55            f.ui.theme = theme;
56        }
57        action
58    }
59    pub(super) fn render_inner(
60        &'static self,
61        f: &mut Frame<'_>,
62        bounds: Rect,
63        state: &mut LayoutState,
64    ) -> Option<&'static str> {
65        state.frame_time = f.time();
66        state.frame_reduced_motion = f.reduced_motion();
67        state.frame_scroll_dragging = f.any_scroll_dragging();
68        state.spinner_anchor=None;state.autocomplete_anchor=None;state.til_dropdown_anchor=None;
69        state.search_bar_links.clear();
70        collect_search_view_links(self, view_id(self.id), &mut state.search_bar_links);
71        let mut action=None;
72        let blocked=state.spinner_open.is_some() || state.spinner_closing.is_some();
73        let popup_pointer=state.autocomplete_open.is_some() && state.autocomplete_panel.is_some_and(|panel|f.input.pointer.is_some_and(|point|panel.contains(point))) && (f.input.pressed || f.input.released);
74        let popup_key=state.autocomplete_open.is_some() && f.input.key.is_some_and(|key|matches!(key.key,Key::Up|Key::Down|Key::Enter|Key::Escape));
75        let til_popup_pointer = state.til_dropdown_open.is_some()
76            && state.til_dropdown_panel.is_some_and(|panel| f.input.pointer.is_some_and(|point| panel.contains(point)))
77            && (f.input.pressed || f.input.released);
78        let til_popup_key = state.til_dropdown_open.is_some()
79            && f.input.key.is_some_and(|key| matches!(key.key, Key::Escape));
80        if let Some(window_bg) = state.theme_style.window_background() {
81            let root_has_bg = self.background.is_some()
82                || self.dynamic_drawable_bg().is_some()
83                || self.has_animated_vector(false)
84                || self.has_animated_selector(false)
85                || self.relative_rules.iter().any(|r| {
86                    r.kind == 96
87                        || (!matches!(self.kind, Kind::Button) && r.kind == RULE_BACKGROUND_TINT)
88                        || r.kind == RULE_DRAWABLE_LAYER
89                        || r.kind == RULE_STATEFUL_DRAWABLE_LAYER
90                        || r.kind == RULE_BACKGROUND_IMAGE
91                        || r.kind == RULE_STATEFUL_BG_IMAGE
92                        || (!self.is_image_src_kind() && r.kind == RULE_ANIMATION_LIST && parse_animation_list(r.anchor).is_some())
93                        || (r.kind == RULE_GRADIENT && parse_gradient(r.anchor).is_some())
94                });
95            if !root_has_bg && bounds.width > 0.0 && bounds.height > 0.0 {
96                f.paint_rect(bounds, 0.0, window_bg);
97            }
98        }
99        state.at_root_draw = true;
100        f.without_input(blocked || popup_pointer || popup_key || til_popup_pointer || til_popup_key,|f| {action=self.draw(f,bounds,state,view_id(self.id));});
101        state.at_root_draw = false;
102        if let Some(chosen)=render_autocomplete_popup(f,state) {action=Some(chosen);}
103        if let Some(chosen)=render_til_dropdown_popup(f,state) {action=Some(chosen);}
104        if let Some(SpinnerAnchor {key,id,bounds:anchor,xml_id,dialog,prompt,rtl,width:requested_width,offset_x,offset_y,padding})=state.spinner_anchor.clone() && (state.spinner_open.as_deref()==Some(key.as_str()) || dialog && state.spinner_closing.as_deref()==Some(key.as_str())) {
105            let Some(entries)=state.spinner_items.get(&key) else {state.spinner_open=None;return action;};
106            if entries.is_empty() {state.spinner_open=None;return action;}
107            let flags=state.spinner_enabled.get(&key).cloned().unwrap_or_default();
108            let enabled_at=|i:usize|flags.get(i).copied().unwrap_or(true);
109            let templates=state.spinner_templates.get(&key).copied();
110            let mut template_state=templates.map(|_|spinner_template_state(state,rtl));
111            if dialog {
112                let viewport=f.viewport();let title_height=if prompt.is_empty(){0.0}else{56.0};
113                let width=(viewport.width-48.0).clamp(0.0,400.0);
114                let rows=spinner_row_offsets(&mut state.spinner_row_cache,&key,entries,templates,&mut template_state,(width-32.0).max(0.0),f.ui.text_metrics,id,0.0);
115                let list_height=rows[entries.len()].min((viewport.height-88.0-title_height).max(48.0));let height=(32.0+title_height+list_height).min(viewport.height);
116                let dialog_bounds=Rect{x:viewport.x+(viewport.width-width)*0.5,y:viewport.y+(viewport.height-height)*0.5,width,height};
117                let mut overlay=state.spinner_dialogs.remove(&key).unwrap_or_default();
118                let was_closing=state.spinner_closing.as_deref()==Some(key.as_str());
119                overlay.kind=OverlayKind::Dialog;overlay.open=state.spinner_open.as_deref()==Some(key.as_str());overlay.dismiss_policy=OverlayDismissPolicy::EscapeOrOutside;
120                let selected=state.spinner_selected.get(&key).copied().unwrap_or(0).min(entries.len()-1);
121                let highlighted=state.spinner_highlight.get(&key).copied().unwrap_or(selected).min(entries.len()-1);
122                let mut choice=None;
123                let scroll=state.spinner_scroll.entry(key.clone()).or_insert_with(|| {let mut physics=crate::scroll::ScrollPhysics::default();physics.offset=rows[highlighted].max(0.0);physics});
124                f.overlay(anonymous_child_id(id,511),&mut overlay,dialog_bounds,true,|f,open| {
125                    let content=f.available();
126                    if !prompt.is_empty() {f.styled_text(Rect{x:content.x,y:content.y,width:content.width,height:title_height},prompt,f.theme().text,TextStyle {size:20.0,..TextStyle::default()});}
127                    let list=Rect{x:content.x,y:content.y+title_height,width:content.width,height:(content.height-title_height).max(0.0)};
128                    let max_scroll=(rows[entries.len()]-list.height).max(0.0);
129                    if f.input.pressed && f.input.pointer_kind==PointerKind::Touch && f.pointer_in(list) && let Some(point)=f.input.pointer {scroll.begin(point[1],f.time());}
130                    if scroll.touching() && let Some(point)=f.input.pointer {scroll.drag(point[1],f.time(),max_scroll,list.height);}
131                    let scrolling=scroll.dragging;if scrolling {f.ui.active=None;}
132                    if f.input.released && scroll.touching() {scroll.release(f.time(),f.reduced_motion());}
133                    if f.pointer_in(list) && f.input.scroll[1]!=0.0 {scroll.wheel(f.input.scroll[1],max_scroll,f.reduced_motion());}
134                    if scroll.advance(f.time(),max_scroll,f.reduced_motion()) {f.ui.continuous=true;}
135                    let offset=scroll.offset.clamp(0.0,max_scroll);let (start,end)=spinner_visible_rows(&rows,offset,list.height);
136                    f.without_input(scrolling,|f| f.within(list,|f| for (i,label) in entries.iter().enumerate().skip(start).take(end.saturating_sub(start)) {
137                        let row=Rect{x:list.x,y:list.y+rows[i]-offset,width:list.width,height:rows[i+1]-rows[i]};
138                        if i==highlighted && highlighted!=selected {f.paint_rect(row,0.0,f.theme().primary_container);}
139                        let r=f.target(anonymous_child_id(id,i),row,label,enabled_at(i),Role::Button);f.state_layer(r,0.0,f.theme().text);
140                        if let (Some(templates),Some(row_state))=(templates,template_state.as_mut()) {
141                            paint_spinner_template(f,templates.dropdown,templates.text_id,label,row,row_state,anonymous_child_id(id,1024+i),r,i==selected,None);
142                        } else {
143                            let circle=Rect{x:row.x+8.0,y:row.y+14.0,width:20.0,height:20.0};let t=f.theme();
144                            let alpha=if r.enabled {1.0}else{0.38};
145                            f.paint_rect(circle,10.0,if i==selected {t.primary.opacity(alpha)}else{t.outline.opacity(alpha)});f.paint_rect(circle.inset(2.0),8.0,t.surface_high);
146                            if i==selected {f.paint_rect(circle.inset(5.0),5.0,t.primary.opacity(alpha));}
147                            f.styled_text(Rect{x:row.x+40.0,width:(row.width-40.0).max(0.0),..row},label,t.text.opacity(alpha),TextStyle::default());
148                        }
149                        if r.clicked {choice=Some(i);*open=false;}
150                    }));
151                    if blocked && (f.pressed_key(Key::Down)||f.pressed_key(Key::Up)) {
152                        let next=spinner_next_enabled(&flags,entries.len(),highlighted,f.pressed_key(Key::Down)).unwrap_or(highlighted);
153                        state.spinner_highlight.insert(key.clone(),next);let top=rows[next];let bottom=rows[next+1];
154                        if top<scroll.offset {scroll.offset=top;}else if bottom>scroll.offset+list.height {scroll.offset=(bottom-list.height).min(max_scroll);}f.ui.continuous=true;
155                    }
156                    if blocked && f.pressed_key(Key::Enter) && enabled_at(highlighted) {choice=Some(highlighted);*open=false;}
157                });
158                if let Some(index)=choice {state.spinner_selected.insert(key.clone(),index);action=Some(xml_id);}
159                if !overlay.open {
160                    state.spinner_open=None;
161                    if was_closing && !overlay.moving() {state.spinner_closing=None;} else {state.spinner_closing=Some(key.clone());}
162                }
163                state.spinner_dialogs.insert(key,overlay);
164                return action;
165            }
166            let viewport=f.viewport();
167            let inner_width=(anchor.width-padding[0]-padding[2]).max(0.0);
168            let selected_for_width=state.spinner_selected.get(&key).copied().unwrap_or(0).min(entries.len()-1);
169            let end=(selected_for_width+15).min(entries.len());let start=selected_for_width.saturating_sub(15-(end-selected_for_width));
170            // Android measures up to 15 adapter *selected* views, including their XML padding.
171            let measured=if requested_width=="wrap_content" {
172                if let (Some(template),Some(row_state))=(templates,template_state.as_mut()) {
173                    entries[start..end].iter().enumerate().map(|(i,label)| {
174                        row_state.text.insert(template.text_id.to_owned(),label.clone());
175                        template.selected.intrinsic(true,row_state,anonymous_child_id(id,1023+i))
176                    }).fold(0.0_f32,f32::max)
177                } else {entries[start..end].iter().map(|s|f.text_width(s,TextStyle::default())+32.0).fold(0.0_f32,f32::max)}
178            } else {0.0};
179            let wanted=match requested_width {"match_parent"=>inner_width,"wrap_content"=>inner_width.max(measured),v=>v.parse::<f32>().unwrap_or(inner_width)};
180            let width=wanted.clamp(0.0,viewport.width.max(0.0));
181            let rows=spinner_row_offsets(&mut state.spinner_row_cache,&key,entries,templates,&mut template_state,width,f.ui.text_metrics,id,0.0);
182            let desired=rows[entries.len()].min(viewport.height);
183            let below_start=(anchor.y+anchor.height+offset_y).clamp(viewport.y,viewport.y+viewport.height);
184            let above_end=(anchor.y+offset_y).clamp(viewport.y,viewport.y+viewport.height);
185            let below=(viewport.y+viewport.height-below_start).max(0.0);let above=(above_end-viewport.y).max(0.0);
186            let use_below=below>=desired || below>=above;
187            let height=desired.min(if use_below {below} else {above});let y=if use_below {below_start} else {above_end-height};
188            let raw_x=if rtl {anchor.x+anchor.width-padding[2]-width-offset_x} else {anchor.x+padding[0]+offset_x};
189            let panel=Rect{x:raw_x.clamp(viewport.x,viewport.x+(viewport.width-width).max(0.0)),y,width,height};
190            if f.input.cancelled || f.input.blurred || f.pressed_key(Key::Escape) || f.input.pressed && !f.pointer_in(panel) {state.spinner_open=None;return action;}
191            let selected=state.spinner_selected.get(&key).copied().unwrap_or(0).min(entries.len()-1);
192            let highlighted=state.spinner_highlight.get(&key).copied().unwrap_or(selected).min(entries.len()-1);
193            let max_scroll=(rows[entries.len()]-panel.height).max(0.0);
194            let scroll=state.spinner_scroll.entry(key.clone()).or_insert_with(|| {let mut physics=crate::scroll::ScrollPhysics::default();physics.offset=rows[highlighted].clamp(0.0,max_scroll);physics});
195            if f.input.pressed && f.input.pointer_kind==PointerKind::Touch && f.pointer_in(panel) && let Some(point)=f.input.pointer {scroll.begin(point[1],f.time());}
196            if scroll.touching() && let Some(point)=f.input.pointer {scroll.drag(point[1],f.time(),max_scroll,panel.height);}
197            let scrolling=scroll.dragging;
198            if scrolling {f.ui.active=None;}
199            if f.input.released && scroll.touching() {scroll.release(f.time(),f.reduced_motion());}
200            if f.pointer_in(panel) && f.input.scroll[1]!=0.0 {scroll.wheel(f.input.scroll[1],max_scroll,f.reduced_motion());}
201            if scroll.advance(f.time(),max_scroll,f.reduced_motion()) {f.ui.continuous=true;}
202            let offset=scroll.offset.clamp(0.0,max_scroll);
203            let (start,end)=spinner_visible_rows(&rows,offset,panel.height);
204            f.elevation(panel,4.0,2);f.paint_rect(panel,4.0,f.theme().surface_highest);
205            f.without_input(scrolling,|f|f.within(panel,|f| for (i,label) in entries.iter().enumerate().skip(start).take(end.saturating_sub(start)) {
206                let r=Rect{x:panel.x,y:panel.y+rows[i]-offset,width:panel.width,height:rows[i+1]-rows[i]};
207                let response=f.target(anonymous_child_id(id,i),r,label,enabled_at(i),Role::Button);
208                if i==highlighted {f.paint_rect(r,0.0,f.theme().primary_container);}
209                f.state_layer(response,0.0,f.theme().text);
210                if let (Some(templates),Some(row_state))=(templates,template_state.as_mut()) {
211                    paint_spinner_template(f,templates.dropdown,templates.text_id,label,r,row_state,anonymous_child_id(id,1024+i),response,i==selected,None);
212                } else {f.styled_text(r,label,f.theme().text.opacity(if response.enabled {1.0}else{0.38}),TextStyle {padding:16.0,..TextStyle::default()});}
213                if response.clicked {state.spinner_selected.insert(key.clone(),i);state.spinner_open=None;action=Some(xml_id);}
214            }));
215            if blocked && (f.pressed_key(Key::Down) || f.pressed_key(Key::Up)) {
216                let next=spinner_next_enabled(&flags,entries.len(),highlighted,f.pressed_key(Key::Down)).unwrap_or(highlighted);
217                state.spinner_highlight.insert(key.clone(),next);
218                let item_top=rows[next];let item_bottom=rows[next+1];
219                if item_top<scroll.offset {scroll.offset=item_top;} else if item_bottom>scroll.offset+panel.height {scroll.offset=(item_bottom-panel.height).min(max_scroll);}
220                f.ui.continuous=true;
221            }
222            if blocked && f.pressed_key(Key::Enter) && enabled_at(highlighted) {state.spinner_selected.insert(key.clone(),highlighted);state.spinner_open=None;action=Some(xml_id);}
223        } else if state.spinner_open.is_some() || state.spinner_closing.is_some() {state.spinner_open=None;state.spinner_closing=None;}
224        action
225    }
226    pub(super) fn draw(
227        &'static self,
228        f: &mut Frame<'_>,
229        b: Rect,
230        state: &mut LayoutState,
231        id: u64,
232    ) -> Option<&'static str> {
233        state.frame_time = f.time();
234        state.frame_reduced_motion = f.reduced_motion();
235        state.frame_scroll_dragging = f.any_scroll_dragging();
236        let visible = self.effective_visibility(state) == Visibility::Visible
237            && !matches!(self.kind, Kind::Space);
238        let rt_scale = if visible && !self.id.is_empty() {
239            state.view_scale.get(self.id).copied()
240        } else {
241            None
242        };
243        let rt_trans = if visible && !self.id.is_empty() {
244            state.view_translation.get(self.id).copied()
245        } else {
246            None
247        };
248        let rt_alpha = if visible && !self.id.is_empty() {
249            state.view_alpha.get(self.id).copied().unwrap_or(1.0)
250        } else {
251            1.0
252        };
253        let xml_tr = if visible { self.view_transform() } else { None };
254        let pushed_transform = if visible && (xml_tr.is_some() || rt_scale.is_some() || rt_trans.is_some()) {
255            let (origin, xml_tx, xml_ty, xml_sx, xml_sy, rot_deg) = if let Some(tr) = xml_tr {
256                (
257                    [
258                        b.x + tr.px.resolve(b.width, state),
259                        b.y + tr.py.resolve(b.height, state),
260                    ],
261                    tr.tx_dp,
262                    tr.ty_dp,
263                    tr.sx,
264                    tr.sy,
265                    tr.rot_deg,
266                )
267            } else {
268                ([b.x + b.width * 0.5, b.y + b.height * 0.5], 0.0, 0.0, 1.0, 1.0, 0.0)
269            };
270            let [rtx, rty] = rt_trans.unwrap_or([0.0, 0.0]);
271            let [rsx, rsy] = rt_scale.unwrap_or([1.0, 1.0]);
272            let tx = xml_tx + rtx;
273            let ty = xml_ty + rty;
274            let sx = xml_sx * rsx;
275            let sy = xml_sy * rsy;
276            if tx.abs() > 1e-4
277                || ty.abs() > 1e-4
278                || (sx - 1.0).abs() > 1e-4
279                || (sy - 1.0).abs() > 1e-4
280                || rot_deg.abs() > 1e-4
281            {
282                f.push_transform(
283                    origin,
284                    [state.dp(tx), state.dp(ty)],
285                    [sx, sy],
286                    rot_deg.to_radians(),
287                );
288                true
289            } else {
290                false
291            }
292        } else {
293            false
294        };
295        let effective_alpha = (self.view_alpha * rt_alpha).clamp(0.0, 1.0);
296        let pushed_layer = if visible && effective_alpha < 1.0 {
297            let opacity = effective_alpha;
298            let has_elev = self.relative_rules.iter().any(|r| r.kind == RULE_ELEVATION);
299            let layer_bounds = if self.clip_children_disabled || self.clip_to_padding_disabled {
300                let min_x = b.x.min(f.clip.x);
301                let min_y = b.y.min(f.clip.y);
302                let max_x = (b.x + b.width).max(f.clip.x + f.clip.width);
303                let max_y = (b.y + b.height).max(f.clip.y + f.clip.height);
304                Rect {
305                    x: min_x,
306                    y: min_y,
307                    width: (max_x - min_x).max(0.0),
308                    height: (max_y - min_y).max(0.0),
309                }
310            } else if has_elev {
311                let pad = state.dp(24.0);
312                Rect {
313                    x: b.x - pad,
314                    y: b.y - pad,
315                    width: b.width + pad * 2.0,
316                    height: b.height + pad * 2.0,
317                }
318                .intersect(f.clip)
319            } else {
320                b
321            };
322            f.push_layer(layer_bounds, opacity);
323            true
324        } else {
325            false
326        };
327        let fade=if matches!(self.kind,Kind::ZoomControls) {
328            let key=if self.id.is_empty() { format!("#{id}") } else { self.id.to_owned() };
329            state.zoom_controls_fades.get(&key).copied().map(|fade|(key,fade))
330        } else { None };
331        let mut action=if let Some((key,fade))=fade {
332            let done=f.reduced_motion() || f.time()-fade.start>=0.5;
333            let opacity=if done { f32::from(fade.target_visible) } else { fade.value(f.time()) };
334            let mut action=None;
335            f.with_opacity(opacity,|f| f.without_input(!fade.target_visible,|f| { action=self.draw_contents(f,b,state,id,false); }));
336            if done {
337                state.zoom_controls_fades.remove(&key);
338                if !fade.target_visible {
339                    state.visibility.insert(key,Visibility::Gone);
340                    // One more frame removes the now-gone view from the parent's layout.
341                    f.wake_at(f.time()+0.001);
342                }
343            } else { f.continuous(b); }
344            action
345        } else { self.draw_contents(f,b,state,id,false) };
346        if self.id=="android:tabcontent"&&matches!(self.kind,Kind::Frame)&&self.effective_visibility(state)==Visibility::Visible {
347            if let Some(host_id)=state.active_tab_host.clone() {if let Some(host)=state.tab_hosts.get(&host_id).cloned() {if let Some(factory)=host.factories.get(host.selected).copied().flatten() {
348                let cache_key=(host_id,host.selected);let node=if let Some(node)=state.tab_host_factory_cache.get(&cache_key){*node}else{let node=factory(&host.tags[host.selected]);state.tab_host_factory_cache.insert(cache_key,node);node};
349                let mut child_action=None;f.within(b,|f|{child_action=node.draw(f,b,state,child_view_id(id,host.selected,node));});if child_action.is_some(){action=child_action;}
350            }}}
351        }
352        if !self.is_search_view_closed(state, id) {
353            self.draw_post(f,b,state);
354        }
355        if pushed_layer {
356            f.pop_layer();
357        }
358        if pushed_transform {
359            f.pop_transform();
360        }
361        action
362    }
363    pub(super) fn draw_post(&self,f:&mut Frame<'_>,b:Rect,state:&LayoutState) {
364        if self.effective_visibility(state)==Visibility::Visible && !matches!(self.kind,Kind::Space) {
365            let padding=self.resolved_view_padding(state);
366            let area=if self.foreground_excludes_padding { Rect { x:b.x+padding[0],y:b.y+padding[1],width:(b.width-padding[0]-padding[2]).max(0.0),height:(b.height-padding[1]-padding[3]).max(0.0) } } else { b };
367            let rtl=match self.layout_direction { 1=>false,2=>true,_=>state.rtl };
368            let [h,v]=self.foreground_gravity;
369            let width=if h==5 { area.width } else { state.dp(self.foreground_size[0].max(0.0)) };
370            let height=if v==5 { area.height } else { state.dp(self.foreground_size[1].max(0.0)) };
371            let right=match h {0=>rtl,2=>!rtl,4=>true,_=>false};
372            let x=if h==1 { area.x+(area.width-width)*0.5 } else if right { area.x+area.width-width } else { area.x };
373            let y=match v {1=>area.y+(area.height-height)*0.5,2=>area.y+area.height-height,_=>area.y};
374            let foreground_bounds=Rect { x,y,width,height };
375            let fg_radii = (self.foreground_radius == 0.0)
376                .then(|| self.resolved_corner_radii(state))
377                .flatten();
378            let fg_radius=if self.foreground_radius<0.0 { self.foreground_radius } else { state.dp(self.foreground_radius) };
379            if let Some(color)=self.foreground {
380                if let Some(radii) = fg_radii {
381                    paint_rounded_rect_radii(f, foreground_bounds, radii, color);
382                } else if fg_radius<0.0 {
383                    paint_oval(f,foreground_bounds,color);
384                } else {
385                    f.paint_rect(foreground_bounds,fg_radius,color);
386                }
387            }
388            if let Some(color)=self.foreground_stroke {
389                let sw = state.dp(self.foreground_stroke_width);
390                if let Some(radii) = fg_radii {
391                    paint_outline_radii(f, foreground_bounds, radii, sw, color);
392                } else {
393                    f.commands.push(DrawCommand::Outline { bounds:foreground_bounds,radius:fg_radius,width:sw,color });
394                }
395            }
396        }
397    }
398    pub(super) fn draw_recycler_ext(
399        &'static self,
400        f: &mut Frame<'_>,
401        inner: Rect,
402        state: &mut LayoutState,
403        id: u64,
404        ext: RecyclerLayoutExt,
405        t: Theme,
406    ) -> Option<&'static str> {
407        let key = if self.id.is_empty() { format!("#{id}") } else { self.id.to_owned() };
408        let template = state.list_templates.get(&key).copied();
409        let mut row_state = if template.is_some() || state.list_item_bindings.contains_key(&key) {
410            Some(spinner_template_state(state, state.rtl))
411        } else {
412            None
413        };
414        let d = state.effective_density();
415        let divider = state.dp(self.divider_thickness.max(0.0));
416        let h_gap = self
417            .relative_rules
418            .iter()
419            .find(|r| r.kind == 93)
420            .and_then(|r| r.anchor.parse::<f32>().ok())
421            .unwrap_or(0.0)
422            .max(0.0)
423            * d;
424        let v_gap = self
425            .relative_rules
426            .iter()
427            .find(|r| r.kind == 94)
428            .and_then(|r| r.anchor.parse::<f32>().ok())
429            .unwrap_or(0.0)
430            .max(0.0)
431            * d;
432        let main_gap = match ext.manager {
433            RecyclerLayoutManager::Linear => {
434                if divider > 0.0 {
435                    divider
436                } else if ext.horizontal {
437                    h_gap
438                } else {
439                    v_gap
440                }
441            }
442            RecyclerLayoutManager::Grid | RecyclerLayoutManager::Staggered => {
443                if ext.horizontal { h_gap.max(divider) } else { v_gap.max(divider) }
444            }
445        };
446        let cross_gap = if ext.horizontal { v_gap } else { h_gap };
447        let span = match ext.manager {
448            RecyclerLayoutManager::Linear => 1,
449            RecyclerLayoutManager::Grid | RecyclerLayoutManager::Staggered => ext.span_count.clamp(1, 256),
450        };
451        let entries = state.list_items.entry(key.clone()).or_insert_with(|| {
452            self.relative_rules
453                .iter()
454                .filter(|r| r.kind == 82)
455                .map(|r| r.anchor.to_owned())
456                .collect()
457        });
458        let entry_count = entries.len();
459        let checked = state.list_checked.entry(key.clone()).or_default();
460        checked.retain(|i| *i < entry_count);
461        let flags = state.list_enabled.get(&key);
462        let item_ids = state.list_item_ids.get(&key);
463        let bindings = state.list_item_bindings.get(&key);
464        let mode = self.relative_rules.iter().find(|r| r.kind == 88).map_or("none", |r| r.anchor);
465        let metrics = f.ui.text_metrics;
466        let cross_avail = if ext.horizontal { inner.height } else { inner.width };
467        let track_size = ((cross_avail - (span - 1) as f32 * cross_gap) / span as f32).max(1.0);
468        let visual_order: Vec<usize> = if ext.reverse {
469            (0..entry_count).rev().collect()
470        } else {
471            (0..entry_count).collect()
472        };
473        let measure_item = |index: usize, row_state: &mut Option<LayoutState>| -> (f32, f32) {
474            let label = &entries[index];
475            let binding = bindings.and_then(|items| items.get(&index));
476            let item_tmpl = adapter_item_template(template, binding);
477            let cid = anonymous_child_id(id, 1024 + index);
478            if let (Some(tmpl), Some(rs)) = (item_tmpl, row_state.as_mut()) {
479                with_adapter_binding(rs, tmpl.text_id, label, binding, |rs| {
480                    if ext.horizontal {
481                        let h = match tmpl.row.height {
482                            Size::Match => track_size,
483                            Size::Dp(v) => rs.dp(v).min(track_size),
484                            Size::Wrap => tmpl.row.layout_intrinsic(false, rs, inner.width.max(48.0), metrics, cid).min(track_size),
485                        }
486                        .max(1.0);
487                        let w = match tmpl.row.width {
488                            Size::Match => rs.dp(120.0).min(inner.width.max(48.0)),
489                            Size::Dp(v) => rs.dp(v),
490                            Size::Wrap => tmpl.row.layout_intrinsic(true, rs, inner.width.max(48.0), metrics, cid),
491                        };
492                        (if w.is_finite() { w.clamp(1.0, 4096.0) } else { rs.dp(120.0) }, h)
493                    } else {
494                        let w = match tmpl.row.width {
495                            Size::Match => track_size,
496                            Size::Dp(v) => rs.dp(v).min(track_size),
497                            Size::Wrap => tmpl.row.layout_intrinsic(true, rs, track_size, metrics, cid).min(track_size),
498                        }
499                        .max(1.0);
500                        let h = match tmpl.row.height {
501                            Size::Match => rs.dp(48.0),
502                            Size::Dp(v) => rs.dp(v),
503                            Size::Wrap => tmpl.row.layout_intrinsic(false, rs, w, metrics, cid),
504                        };
505                        (w, if h.is_finite() { h.clamp(1.0, 4096.0) } else { rs.dp(48.0) })
506                    }
507                })
508            } else if ext.horizontal {
509                let tw = metrics.width(label, TextStyle::default());
510                let w = (tw.ceil() + 34.0).clamp(48.0, 4096.0);
511                (w, track_size)
512            } else {
513                let lines = wrapped_text_lines(label, (track_size - 32.0).max(1.0), TextStyle::default(), metrics, |_| {}).max(1);
514                (track_size, (24.0 * lines as f32).clamp(48.0, 4096.0))
515            }
516        };
517        // Each tuple: (adapter_index, main_pos, cross_pos, item_w, item_h)
518        let mut placed: Vec<(usize, f32, f32, f32, f32)> = Vec::with_capacity(entry_count);
519        let content_extent = match ext.manager {
520            RecyclerLayoutManager::Linear => {
521                let mut cursor = 0.0_f32;
522                for &idx in &visual_order {
523                    let (w, h) = measure_item(idx, &mut row_state);
524                    let main_len = if ext.horizontal { w } else { h };
525                    placed.push((idx, cursor, 0.0, w, h));
526                    cursor += main_len + main_gap;
527                }
528                if entry_count == 0 { 0.0 } else { (cursor - main_gap).max(0.0) }
529            }
530            RecyclerLayoutManager::Grid => {
531                let mut cursor = 0.0_f32;
532                for chunk in visual_order.chunks(span) {
533                    let mut chunk_max = 0.0_f32;
534                    let mut sizes = Vec::with_capacity(chunk.len());
535                    for &idx in chunk {
536                        let (w, h) = measure_item(idx, &mut row_state);
537                        chunk_max = chunk_max.max(if ext.horizontal { w } else { h });
538                        sizes.push((idx, w, h));
539                    }
540                    for (lane, (idx, w, h)) in sizes.into_iter().enumerate() {
541                        let cross_pos = lane as f32 * (track_size + cross_gap);
542                        placed.push((idx, cursor, cross_pos, w, h));
543                    }
544                    cursor += chunk_max + main_gap;
545                }
546                if entry_count == 0 { 0.0 } else { (cursor - main_gap).max(0.0) }
547            }
548            RecyclerLayoutManager::Staggered => {
549                let mut track_ends = vec![0.0_f32; span];
550                let mut track_counts = vec![0usize; span];
551                for &idx in &visual_order {
552                    let mut best = 0usize;
553                    let mut best_end = track_ends[0];
554                    for (lane, &end_val) in track_ends.iter().enumerate().skip(1) {
555                        if end_val + 0.01 < best_end {
556                            best = lane;
557                            best_end = end_val;
558                        }
559                    }
560                    let (w, h) = measure_item(idx, &mut row_state);
561                    let main_len = if ext.horizontal { w } else { h };
562                    let main_pos = if track_counts[best] > 0 {
563                        track_ends[best] + main_gap
564                    } else {
565                        0.0
566                    };
567                    let cross_pos = best as f32 * (track_size + cross_gap);
568                    placed.push((idx, main_pos, cross_pos, w, h));
569                    track_ends[best] = main_pos + main_len;
570                    track_counts[best] += 1;
571                }
572                track_ends.into_iter().fold(0.0_f32, f32::max)
573            }
574        };
575        let viewport_main = if ext.horizontal { inner.width } else { inner.height };
576        let max_scroll = (content_extent - viewport_main).max(0.0);
577        let offset = state.offsets.entry(key.clone()).or_default();
578        if !offset.is_finite() {
579            *offset = 0.0;
580        }
581        let rtl = state.rtl;
582        let dragging = if ext.horizontal {
583            f.scroll_viewport_horizontal_rtl(id, inner, offset, max_scroll, rtl)
584        } else {
585            f.scroll_viewport(id, inner, offset, max_scroll)
586        };
587        let scroll = *offset;
588        let pad_start = if ext.stack_end {
589            (viewport_main - content_extent).max(0.0)
590        } else {
591            0.0
592        };
593        let long_enabled = state.list_long_click_enabled.get(&key).copied().unwrap_or(false);
594        let hold = state.list_holds.entry(key.clone()).or_default();
595        if !long_enabled
596            || dragging
597            || f.input.cancelled
598            || f.input.blurred
599            || f.input.released
600            || f.input.scroll != [0.0, 0.0]
601        {
602            *hold = None;
603        }
604        let mut chosen = None;
605        let mut long_choice = None;
606        let mut control_choice = None;
607        let row_state_environment = row_state.as_ref().cloned();
608        let control_states = &mut state.list_control_states;
609        let (stretch_s, stretch_sign) = f.scroll_stretch(id);
610        f.cancel_pointer_input(dragging, |f| {
611            f.within(inner, |f| {
612                f.with_edge_stretch(inner, ext.horizontal, stretch_s, stretch_sign, |f| {
613                for (vis_pos, &(index, main_pos, cross_pos, item_w, item_h)) in placed.iter().enumerate() {
614                    let bounds = if ext.horizontal {
615                        let logical_x = pad_start + main_pos - scroll;
616                        let x = if rtl {
617                            inner.x + inner.width - logical_x - item_w
618                        } else {
619                            inner.x + logical_x
620                        };
621                        Rect { x, y: inner.y + cross_pos, width: item_w, height: item_h }
622                    } else {
623                        let x = if rtl {
624                            inner.x + inner.width - cross_pos - item_w
625                        } else {
626                            inner.x + cross_pos
627                        };
628                        Rect { x, y: inner.y + pad_start + main_pos - scroll, width: item_w, height: item_h }
629                    };
630                    let clip_vis = bounds.intersect(inner);
631                    if clip_vis.width <= 0.0 || clip_vis.height <= 0.0 {
632                        continue;
633                    }
634                    if checked.contains(&index) {
635                        f.paint_rect(bounds, 0.0, t.primary_container);
636                    }
637                    let item_key = item_ids
638                        .and_then(|ids| ids.get(index))
639                        .map_or(index as u64, |value| *value as u64);
640                    let enabled = self.enabled && flags.and_then(|v| v.get(index)).copied().unwrap_or(true);
641                    let row_id = id.wrapping_mul(0x9e3779b97f4a7c15).wrapping_add(item_key);
642                    let binding = bindings.and_then(|items| items.get(&index));
643                    let item_template = adapter_item_template(template, binding);
644                    let interactive = item_template
645                        .is_some_and(|tmpl| inspect_expandable_template(tmpl.row, tmpl.text_id).2);
646                    if interactive {
647                        let tmpl = item_template.unwrap();
648                        let controls = control_states.entry((key.clone(), item_key as i64)).or_default();
649                        let mut local = row_state_environment.clone().unwrap_or_default();
650                        local.adapter_namespace = Some(row_id);
651                        local.rtl = rtl;
652                        local.checked = std::mem::take(controls);
653                        with_adapter_binding(&mut local, tmpl.text_id, &entries[index], binding, |rs| {
654                            f.within(bounds, |f| {
655                                f.with_enabled(enabled, |f| {
656                                    if let Some(view) = tmpl.row.draw(f, bounds, rs, anonymous_child_id(row_id, 0)) {
657                                        control_choice = Some((index, view));
658                                    }
659                                });
660                            });
661                        });
662                        *controls = local.checked;
663                    }
664                    let r = f.target(row_id, bounds, &entries[index], enabled, Role::Button);
665                    if adapter_item_long_press(f, r, hold, long_enabled) {
666                        long_choice = Some(index);
667                    }
668                    f.state_layer(r, 0.0, t.text);
669                    if interactive {
670                    } else if let (Some(tmpl), Some(rs)) = (item_template, row_state.as_mut()) {
671                        paint_spinner_template(
672                            f,
673                            tmpl.row,
674                            tmpl.text_id,
675                            &entries[index],
676                            bounds,
677                            rs,
678                            anonymous_child_id(id, 1024 + index),
679                            r,
680                            checked.contains(&index),
681                            binding,
682                        );
683                    } else {
684                        let style = TextStyle { align: if rtl { 2 } else { 0 }, ..TextStyle::default() };
685                        let text_w = (bounds.width - 32.0).max(1.0);
686                        let lines = wrapped_text_lines(&entries[index], text_w, style, metrics, |_| {}).max(1);
687                        let mut line_y = bounds.y + (bounds.height - lines as f32 * 24.0).max(0.0) * 0.5;
688                        wrapped_text_lines(&entries[index], text_w, style, metrics, |line| {
689                            f.styled_text(
690                                Rect { x: bounds.x + 16.0, y: line_y, width: text_w, height: 24.0 },
691                                &line.text,
692                                t.text.opacity(if r.enabled { 1.0 } else { 0.38 }),
693                                style,
694                            );
695                            line_y += 24.0;
696                        });
697                    }
698                    if r.clicked {
699                        chosen = Some(index);
700                    }
701                    if ext.manager == RecyclerLayoutManager::Linear
702                        && vis_pos + 1 < placed.len()
703                        && divider > 0.0
704                        && let Some(color) = self.divider_color
705                    {
706                        if ext.horizontal {
707                            let dx = if rtl { bounds.x - divider } else { bounds.x + bounds.width };
708                            f.paint_rect(Rect { x: dx, y: bounds.y, width: divider, height: bounds.height }, 0.0, color);
709                        } else {
710                            f.paint_rect(
711                                Rect { x: bounds.x, y: bounds.y + bounds.height, width: bounds.width, height: divider },
712                                0.0,
713                                color,
714                            );
715                        }
716                    }
717                }
718                });
719            });
720        });
721        let mut action = None;
722        if let Some(control) = control_choice {
723            state.list_control_clicked.insert(key.clone(), control);
724            chosen = None;
725            long_choice = None;
726            action = Some(self.id);
727        }
728        if let Some(index) = long_choice {
729            state.list_long_clicked.insert(key.clone(), index);
730            action = Some(self.id);
731        }
732        if let Some(index) = chosen {
733            match mode {
734                "singleChoice" => {
735                    checked.clear();
736                    checked.push(index);
737                }
738                "multipleChoice" => {
739                    if let Some(pos) = checked.iter().position(|v| *v == index) {
740                        checked.remove(pos);
741                    } else {
742                        checked.push(index);
743                    }
744                }
745                _ => {}
746            }
747            state.list_clicked.insert(key, index);
748            action = Some(self.id);
749        }
750        action
751    }
752    #[allow(clippy::too_many_arguments)]
753    pub(super) fn draw_search_view_ext(
754        &'static self,
755        f: &mut Frame<'_>,
756        b: Rect,
757        state: &mut LayoutState,
758        id: u64,
759        spec: SearchViewExtSpec<'static>,
760        t: Theme,
761        background_tint: Option<Color>,
762        explicit_background: Option<Color>,
763        scaled_radius: f32,
764    ) -> Option<&'static str> {
765        let view_key = if self.id.is_empty() {
766            format!("#{id}")
767        } else {
768            self.id.to_owned()
769        };
770        let is_open = state.search_open.get(&view_key).copied().unwrap_or(false);
771        let now = if f.time().is_finite() { f.time().max(0.0) } else { 0.0 };
772        let prev = state.search_view_motion.get(&view_key).copied().unwrap_or_default();
773        let (next_m, anim) =
774            eval_binary_ease_out_motion(prev, is_open, 0.24, now, f.reduced_motion());
775        if state.search_view_motion.len() >= 64
776            && !state.search_view_motion.contains_key(&view_key)
777        {
778            state.search_view_motion.clear();
779        }
780        state.search_view_motion.insert(view_key.clone(), next_m);
781        if anim {
782            f.continuous(b);
783        }
784        let progress = next_m.progress;
785        if !is_open && progress <= 0.001 {
786            return None;
787        }
788        let enabled = self.enabled && f.enabled;
789        let rtl = state.rtl;
790        let mut action = None;
791        state.text.entry(view_key.clone()).or_default();
792        state.input_types.insert(view_key.clone(), 0);
793        if state.search_pending.remove(&view_key) {
794            action = Some(self.id);
795        }
796        let surface_color = background_tint
797            .or(explicit_background)
798            .unwrap_or(match state.theme_style {
799                AndroidTheme::Material3Light => Color(0xEC, 0xE6, 0xF0, 255),
800                AndroidTheme::Material3Dark => Color(0x2B, 0x29, 0x30, 255),
801                _ => t.surface_high,
802            });
803        let pushed_layer = if progress < 0.999 {
804            f.push_layer(b, progress);
805            true
806        } else {
807            false
808        };
809        let block_input = !is_open || progress < 0.01;
810        let mut closed_now = false;
811        f.without_input(block_input, |f| {
812            f.paint_rect(b, scaled_radius.max(0.0), surface_color);
813            let header_h = state.dp(56.0).min(b.height);
814            let header_rect = Rect {
815                x: b.x,
816                y: b.y,
817                width: b.width,
818                height: header_h,
819            };
820            let mut query = state.text.get(&view_key).cloned().unwrap_or_default();
821            let mut show_clear = !query.is_empty();
822            let btn_s = state.dp(48.0).min(header_h).min(header_rect.width);
823            let edge_pad = state.dp(4.0);
824            if !spec.hide_nav && btn_s > 0.0 {
825                let back_x = if rtl {
826                    header_rect.x + header_rect.width - edge_pad - btn_s
827                } else {
828                    header_rect.x + edge_pad
829                };
830                let back_rect = Rect {
831                    x: back_x,
832                    y: header_rect.y + (header_h - btn_s) * 0.5,
833                    width: btn_s,
834                    height: btn_s,
835                };
836                let back_hit = f.target(
837                    anonymous_child_id(id, 9810),
838                    back_rect,
839                    "Back",
840                    enabled,
841                    Role::Button,
842                );
843                f.state_layer(back_hit, btn_s * 0.5, t.text);
844                f.focus_ring(back_hit);
845                let icon_s = state.dp(24.0).min(btn_s);
846                let icon_rect = Rect {
847                    x: back_rect.x + (back_rect.width - icon_s) * 0.5,
848                    y: back_rect.y + (back_rect.height - icon_s) * 0.5,
849                    width: icon_s,
850                    height: icon_s,
851                };
852                let back_icon = if spec.use_drawer_arrow && progress < 0.5 {
853                    MaterialIcon::Menu
854                } else if rtl {
855                    MaterialIcon::ArrowForward
856                } else {
857                    MaterialIcon::ArrowBack
858                };
859                f.paint_material_icon(
860                    icon_rect,
861                    back_icon,
862                    t.text.opacity(if enabled { 1.0 } else { 0.38 }),
863                );
864                if back_hit.clicked {
865                    closed_now = true;
866                }
867            }
868            if enabled && is_open && f.pressed_key(Key::Escape) {
869                closed_now = true;
870            }
871            if show_clear && btn_s > 0.0 {
872                let clear_x = if rtl {
873                    header_rect.x + edge_pad
874                } else {
875                    header_rect.x + header_rect.width - edge_pad - btn_s
876                };
877                let clear_rect = Rect {
878                    x: clear_x,
879                    y: header_rect.y + (header_h - btn_s) * 0.5,
880                    width: btn_s,
881                    height: btn_s,
882                };
883                let clear_hit = f.target(
884                    anonymous_child_id(id, 9811),
885                    clear_rect,
886                    "Clear search",
887                    enabled,
888                    Role::Button,
889                );
890                f.state_layer(clear_hit, btn_s * 0.5, t.text);
891                f.focus_ring(clear_hit);
892                let icon_s = state.dp(24.0).min(btn_s);
893                let icon_rect = Rect {
894                    x: clear_rect.x + (clear_rect.width - icon_s) * 0.5,
895                    y: clear_rect.y + (clear_rect.height - icon_s) * 0.5,
896                    width: icon_s,
897                    height: icon_s,
898                };
899                f.paint_material_icon(
900                    icon_rect,
901                    MaterialIcon::Close,
902                    t.on_surface_variant.opacity(if enabled { 1.0 } else { 0.38 }),
903                );
904                if clear_hit.clicked {
905                    query.clear();
906                    show_clear = false;
907                    state.text.insert(view_key.clone(), String::new());
908                    state.search_focus_requested.insert(view_key.clone());
909                    f.request_focus(id);
910                    action = Some(self.id);
911                    f.continuous(b);
912                }
913            }
914            let lead = if spec.hide_nav {
915                state.dp(16.0)
916            } else {
917                state.dp(56.0)
918            };
919            let trail = if show_clear {
920                state.dp(56.0)
921            } else {
922                state.dp(16.0)
923            };
924            let field_x = header_rect.x + if rtl { trail } else { lead };
925            let field_w = (header_rect.width - lead - trail).max(0.0);
926            let field_rect = Rect {
927                x: field_x,
928                y: header_rect.y,
929                width: field_w,
930                height: header_h,
931            };
932            let hint = if !spec.hint.is_empty() {
933                spec.hint
934            } else if !self.hint.is_empty() {
935                self.hint
936            } else {
937                "Search"
938            };
939            let field_resp = f.target(id, field_rect, hint, enabled, Role::TextField);
940            f.focus_ring(field_resp);
941            if field_resp.clicked {
942                f.request_focus(id);
943                state.search_focus_requested.insert(view_key.clone());
944            }
945            if field_resp.focused
946                && f.pressed_key(Key::Enter)
947                && state.submit_search(&view_key)
948            {
949                state.search_pending.remove(&view_key);
950                action = Some(self.id);
951            }
952            let (display, text_color) = if query.is_empty() {
953                (
954                    hint,
955                    t.on_surface_variant.opacity(if enabled { 1.0 } else { 0.38 }),
956                )
957            } else {
958                (
959                    query.as_str(),
960                    self.color
961                        .unwrap_or(t.text)
962                        .opacity(if enabled { 1.0 } else { 0.38 }),
963                )
964            };
965            let text_size = if (self.text_size - 16.0).abs() < 0.001 {
966                state.sp(16.0)
967            } else {
968                self.scaled_text_size(state)
969            };
970            f.within(field_rect, |f| {
971                f.styled_text(
972                    field_rect,
973                    display,
974                    text_color,
975                    TextStyle {
976                        size: text_size,
977                        weight: if self.text_bold { 700 } else { 400 },
978                        align: if rtl { 2 } else { 0 },
979                        letter_spacing: self.letter_spacing_em * text_size,
980                        ..TextStyle::default()
981                    },
982                );
983            });
984            let div_h = state.dp(1.0).min((b.height - header_h).max(0.0));
985            if div_h > 0.0 {
986                f.paint_rect(
987                    Rect {
988                        x: b.x,
989                        y: b.y + header_h,
990                        width: b.width,
991                        height: div_h,
992                    },
993                    0.0,
994                    t.outline_variant,
995                );
996            }
997            let body_clip = Rect {
998                x: b.x,
999                y: b.y + header_h + div_h,
1000                width: b.width,
1001                height: (b.height - header_h - div_h).max(0.0),
1002            };
1003            if body_clip.width > 0.0 && body_clip.height > 0.0 && !self.children.is_empty() {
1004                let padding = self.resolved_padding(state);
1005                let body_inner = Rect {
1006                    x: body_clip.x + padding[0],
1007                    y: body_clip.y + padding[1],
1008                    width: (body_clip.width - padding[0] - padding[2]).max(0.0),
1009                    height: (body_clip.height - padding[1] - padding[3]).max(0.0),
1010                };
1011                let metrics = f.ui.text_metrics;
1012                f.within(body_clip, |f| {
1013                    if matches!(self.kind, Kind::Linear) {
1014                        let horiz = self.horizontal;
1015                        let main_avail = if horiz { body_inner.width } else { body_inner.height };
1016                        let lengths = self.linear_main_lengths(
1017                            horiz,
1018                            state,
1019                            main_avail,
1020                            body_inner.width,
1021                            metrics,
1022                            id,
1023                        );
1024                        let mut cursor = if horiz && rtl {
1025                            body_inner.x + body_inner.width
1026                        } else if horiz {
1027                            body_inner.x
1028                        } else {
1029                            body_inner.y
1030                        };
1031                        for (i, n) in self.children.iter().enumerate() {
1032                            if n.effective_visibility(state) == Visibility::Gone {
1033                                continue;
1034                            }
1035                            let cid = child_view_id(id, i, n);
1036                            let m = n.resolved_margin(state);
1037                            let len = lengths[i];
1038                            let mut r = Rect {
1039                                x: body_inner.x + m[0],
1040                                y: body_inner.y + m[1],
1041                                width: (body_inner.width - m[0] - m[2]).max(0.0),
1042                                height: (body_inner.height - m[1] - m[3]).max(0.0),
1043                            };
1044                            if horiz {
1045                                r.x = if rtl { cursor - m[2] - len } else { cursor + m[0] };
1046                                r.width = len;
1047                                let ext = len + m[0] + m[2];
1048                                if rtl { cursor -= ext; } else { cursor += ext; }
1049                                if let Size::Dp(h) = n.height {
1050                                    r.height = state.dp(h).min(r.height);
1051                                } else if matches!(n.height, Size::Wrap) {
1052                                    r.height = n
1053                                        .layout_intrinsic(false, state, r.width, metrics, cid)
1054                                        .min(r.height);
1055                                }
1056                            } else {
1057                                r.y = cursor + m[1];
1058                                r.height = len;
1059                                cursor += len + m[1] + m[3];
1060                                if let Size::Dp(w) = n.width {
1061                                    r.width = state.dp(w).min(r.width);
1062                                } else if matches!(n.width, Size::Wrap) {
1063                                    r.width = n
1064                                        .layout_intrinsic(true, state, r.width, metrics, cid)
1065                                        .min(r.width);
1066                                }
1067                                let right = match n.layout_gravity[0] {
1068                                    0 => rtl,
1069                                    2 => !rtl,
1070                                    4 => true,
1071                                    _ => false,
1072                                };
1073                                r.x = if n.layout_gravity[0] == 1 {
1074                                    body_inner.x + (body_inner.width - r.width) * 0.5 + m[0] - m[2]
1075                                } else if right {
1076                                    body_inner.x + body_inner.width - r.width - m[2]
1077                                } else {
1078                                    body_inner.x + m[0]
1079                                };
1080                            }
1081                            let mut child_act = None;
1082                            if self.clip_children_disabled {
1083                                child_act = n.draw(f, r, state, cid);
1084                            } else {
1085                                f.within(r, |f| {
1086                                    child_act = n.draw(f, r, state, cid);
1087                                });
1088                            }
1089                            if child_act.is_some() {
1090                                action = child_act;
1091                            }
1092                        }
1093                    } else {
1094                        for (i, n) in self.children.iter().enumerate() {
1095                            if n.effective_visibility(state) == Visibility::Gone {
1096                                continue;
1097                            }
1098                            let cid = child_view_id(id, i, n);
1099                            let m = n.resolved_margin(state);
1100                            let mut r = Rect {
1101                                x: body_inner.x + m[0],
1102                                y: body_inner.y + m[1],
1103                                width: (body_inner.width - m[0] - m[2]).max(0.0),
1104                                height: (body_inner.height - m[1] - m[3]).max(0.0),
1105                            };
1106                            if let Size::Dp(w) = n.width {
1107                                r.width = state.dp(w);
1108                            } else if matches!(n.width, Size::Wrap) {
1109                                r.width = n
1110                                    .layout_intrinsic(true, state, r.width, metrics, cid)
1111                                    .min(r.width);
1112                            }
1113                            if let Size::Dp(h) = n.height {
1114                                r.height = state.dp(h);
1115                            } else if matches!(n.height, Size::Wrap) {
1116                                r.height = n
1117                                    .layout_intrinsic(false, state, r.width, metrics, cid)
1118                                    .min(r.height);
1119                            }
1120                            let right = match n.layout_gravity[0] {
1121                                0 => rtl,
1122                                2 => !rtl,
1123                                4 => true,
1124                                _ => false,
1125                            };
1126                            r.x = if n.layout_gravity[0] == 1 {
1127                                body_inner.x + (body_inner.width - r.width) * 0.5 + m[0] - m[2]
1128                            } else if right {
1129                                body_inner.x + body_inner.width - r.width - m[2]
1130                            } else {
1131                                body_inner.x + m[0]
1132                            };
1133                            r.y = match n.layout_gravity[1] {
1134                                1 => body_inner.y + (body_inner.height - r.height) * 0.5 + m[1] - m[3],
1135                                2 => body_inner.y + body_inner.height - r.height - m[3],
1136                                _ => body_inner.y + m[1],
1137                            };
1138                            let mut child_act = None;
1139                            if self.clip_children_disabled {
1140                                child_act = n.draw(f, r, state, cid);
1141                            } else {
1142                                f.within(r, |f| {
1143                                    child_act = n.draw(f, r, state, cid);
1144                                });
1145                            }
1146                            if child_act.is_some() {
1147                                action = child_act;
1148                            }
1149                        }
1150                    }
1151                });
1152            }
1153        });
1154        if closed_now {
1155            state.search_open.insert(view_key.clone(), false);
1156            let (closing_m, _closing_anim) =
1157                eval_binary_ease_out_motion(next_m, false, 0.24, now, f.reduced_motion());
1158            state.search_view_motion.insert(view_key, closing_m);
1159            f.continuous(b);
1160        }
1161        if pushed_layer {
1162            f.pop_layer();
1163        }
1164        action
1165    }
1166    pub(super) fn draw_carousel_layout(
1167        &'static self,
1168        f: &mut Frame<'_>,
1169        inner: Rect,
1170        state: &mut LayoutState,
1171        id: u64,
1172        spec: CarouselLayoutSpec,
1173        t: Theme,
1174    ) -> Option<&'static str> {
1175        let key = if self.id.is_empty() {
1176            format!("#{id}")
1177        } else {
1178            self.id.to_owned()
1179        };
1180        let (child_source, child_parent_id): (Option<&'static [Node]>, u64) =
1181            if matches!(self.kind, Kind::HorizontalScroll) {
1182                if self.children.len() == 1
1183                    && matches!(self.children[0].kind, Kind::Linear)
1184                    && !self.children[0].children.is_empty()
1185                    && !self.children[0].is_carousel_item()
1186                {
1187                    (
1188                        Some(self.children[0].children),
1189                        child_view_id(id, 0, &self.children[0]),
1190                    )
1191                } else {
1192                    (Some(self.children), id)
1193                }
1194            } else if matches!(self.kind, Kind::Linear) {
1195                (Some(self.children), id)
1196            } else if !self.children.is_empty() {
1197                (Some(self.children), id)
1198            } else {
1199                (None, id)
1200            };
1201
1202        let visible_children: Vec<(usize, &'static Node)> = if let Some(nodes) = child_source {
1203            nodes
1204                .iter()
1205                .enumerate()
1206                .filter(|(_, n)| n.effective_visibility(state) != Visibility::Gone)
1207                .collect()
1208        } else {
1209            Vec::new()
1210        };
1211        let adapter_entries: Vec<String> = if child_source.is_none() {
1212            state
1213                .list_items
1214                .entry(key.clone())
1215                .or_insert_with(|| {
1216                    self.relative_rules
1217                        .iter()
1218                        .filter(|r| r.kind == 82)
1219                        .map(|r| r.anchor.to_owned())
1220                        .collect()
1221                })
1222                .clone()
1223        } else {
1224            Vec::new()
1225        };
1226        let n = if child_source.is_some() {
1227            visible_children.len()
1228        } else {
1229            adapter_entries.len()
1230        };
1231        if n == 0 {
1232            return None;
1233        }
1234        let viewport_w = inner.width.max(1.0);
1235        let gap = state.dp(spec.spacing_dp.max(0.0));
1236        let (focal_w, step, max_scroll) = match spec.strategy {
1237            CarouselStrategy::FullScreen => {
1238                let fw = viewport_w;
1239                let st = fw + gap;
1240                (fw, st, ((n - 1) as f32 * st).max(0.0))
1241            }
1242            CarouselStrategy::Uncontained => {
1243                let fw = (viewport_w * 0.60).max(1.0);
1244                let st = fw + gap;
1245                let ms = if spec.align == CarouselAlign::Center {
1246                    ((n - 1) as f32 * st).max(0.0)
1247                } else {
1248                    (n as f32 * fw + (n - 1) as f32 * gap - viewport_w).max(0.0)
1249                };
1250                (fw, st, ms)
1251            }
1252            CarouselStrategy::Hero => {
1253                let fw = (viewport_w * 0.78).max(1.0);
1254                let st = fw + gap;
1255                (fw, st, ((n - 1) as f32 * st).max(0.0))
1256            }
1257            CarouselStrategy::MultiBrowse => {
1258                let fw = (viewport_w * 0.55).max(1.0);
1259                let st = fw + gap;
1260                (fw, st, ((n - 1) as f32 * st).max(0.0))
1261            }
1262        };
1263        let _ = focal_w;
1264        let now = if f.time().is_finite() { f.time().max(0.0) } else { 0.0 };
1265        let rtl = state.rtl;
1266        let enabled = self.enabled && f.enabled;
1267        let mut cm = state.carousel_motion.remove(&key).unwrap_or_default();
1268        let mut offset = state
1269            .offsets
1270            .get(&key)
1271            .copied()
1272            .filter(|v| v.is_finite())
1273            .unwrap_or(0.0)
1274            .clamp(0.0, max_scroll);
1275        if let Some((start, from, to, dur)) = cm.snap {
1276            if f.reduced_motion() || dur <= 0.0 {
1277                offset = to.clamp(0.0, max_scroll);
1278                cm.snap = None;
1279            } else {
1280                let p = ((now - start) / dur).clamp(0.0, 1.0) as f32;
1281                let ease = 1.0 - (1.0 - p) * (1.0 - p);
1282                offset = (from + (to - from) * ease).clamp(0.0, max_scroll);
1283                if p >= 1.0 {
1284                    offset = to.clamp(0.0, max_scroll);
1285                    cm.snap = None;
1286                } else {
1287                    f.continuous(inner);
1288                }
1289            }
1290        }
1291        let prev_input = f.input;
1292        let offset_before_sv = offset;
1293        let touch_dragging = f.scroll_viewport_horizontal_rtl(id, inner, &mut offset, max_scroll, rtl);
1294        let wheel_moved = (offset - offset_before_sv).abs() > 0.01 && !touch_dragging;
1295        if wheel_moved {
1296            cm.snap = None;
1297        }
1298        let compute_snap_target = |cur_scroll: f32, v_scroll: f32| -> f32 {
1299            if max_scroll <= 0.0 || n <= 1 {
1300                return 0.0;
1301            }
1302            let mut candidates: Vec<f32> = (0..n)
1303                .map(|k| (k as f32 * step).clamp(0.0, max_scroll))
1304                .collect();
1305            if !candidates.last().is_some_and(|&v| (v - max_scroll).abs() < 0.5) {
1306                candidates.push(max_scroll);
1307            }
1308            let flick_v = state.dp(400.0);
1309            let hint = if v_scroll > flick_v {
1310                (cur_scroll + step * 0.45).min(max_scroll)
1311            } else if v_scroll < -flick_v {
1312                (cur_scroll - step * 0.45).max(0.0)
1313            } else {
1314                cur_scroll
1315            };
1316            candidates
1317                .into_iter()
1318                .min_by(|a, b| {
1319                    (a - hint)
1320                        .abs()
1321                        .partial_cmp(&(b - hint).abs())
1322                        .unwrap_or(std::cmp::Ordering::Equal)
1323                })
1324                .unwrap_or(0.0)
1325        };
1326        if touch_dragging {
1327            cm.was_touch_dragging = true;
1328            cm.snap = None;
1329        }
1330        if cm.was_touch_dragging
1331            && (prev_input.released || prev_input.cancelled || prev_input.blurred)
1332        {
1333            cm.was_touch_dragging = false;
1334            f.cancel_scroll(id);
1335            let target = compute_snap_target(offset, 0.0);
1336            if f.reduced_motion() || (offset - target).abs() <= 0.5 {
1337                offset = target;
1338                cm.snap = None;
1339            } else {
1340                cm.snap = Some((now, offset, target, 0.24));
1341                f.continuous(inner);
1342            }
1343        }
1344        let mut mouse_was_dragging = false;
1345        if enabled && prev_input.pointer_kind != PointerKind::Touch {
1346            let inside = prev_input
1347                .pointer
1348                .is_some_and(|p| inner.intersect(f.clip).contains(p));
1349            if prev_input.pressed && inside {
1350                cm.origin = prev_input.pointer;
1351                cm.drag_base = offset;
1352                cm.dragging = false;
1353                cm.snap = None;
1354                cm.samples.clear();
1355                if let Some(p) = prev_input.pointer {
1356                    cm.samples.push((now, p[0]));
1357                }
1358            }
1359            if let (Some(origin), Some(ptr)) = (cm.origin, prev_input.pointer)
1360                && !prev_input.cancelled
1361                && !prev_input.blurred
1362            {
1363                if cm.samples.last().map_or(true, |&(_, x)| (x - ptr[0]).abs() > 0.01) {
1364                    cm.samples.push((now, ptr[0]));
1365                    if cm.samples.len() > 8 {
1366                        cm.samples.remove(0);
1367                    }
1368                }
1369                let dx = ptr[0] - origin[0];
1370                let dy = ptr[1] - origin[1];
1371                let slop = state.dp(8.0);
1372                if !cm.dragging && dx.abs() > slop && dx.abs() >= dy.abs() {
1373                    cm.dragging = true;
1374                    f.ui.active = None;
1375                }
1376                if cm.dragging {
1377                    let delta_scroll = if rtl { dx } else { -dx };
1378                    offset = (cm.drag_base + delta_scroll).clamp(0.0, max_scroll);
1379                    if !prev_input.released {
1380                        f.continuous(inner);
1381                    }
1382                }
1383            }
1384            mouse_was_dragging = cm.dragging;
1385            if cm.origin.is_some()
1386                && (prev_input.released || prev_input.cancelled || prev_input.blurred)
1387            {
1388                if cm.dragging {
1389                    let v_scroll = if !prev_input.cancelled && !prev_input.blurred {
1390                        let vx = pointer_velocity(&cm.samples, now);
1391                        if rtl { vx } else { -vx }
1392                    } else {
1393                        0.0
1394                    };
1395                    let target = compute_snap_target(offset, v_scroll);
1396                    if f.reduced_motion() || (offset - target).abs() <= 0.5 {
1397                        offset = target;
1398                        cm.snap = None;
1399                    } else {
1400                        cm.snap = Some((now, offset, target, 0.24));
1401                        f.continuous(inner);
1402                    }
1403                }
1404                cm.origin = None;
1405                cm.dragging = false;
1406                cm.samples.clear();
1407            }
1408        } else if !enabled {
1409            cm.origin = None;
1410            cm.dragging = false;
1411            cm.samples.clear();
1412        }
1413        offset = offset.clamp(0.0, max_scroll);
1414        state.offsets.insert(key.clone(), offset);
1415        let f_idx = if step > 0.0 {
1416            (offset / step).clamp(0.0, (n - 1) as f32)
1417        } else {
1418            0.0
1419        };
1420        state
1421            .selected_index
1422            .insert(key.clone(), (f_idx.round() as usize).min(n - 1));
1423        if state.carousel_motion.len() >= 64 && !state.carousel_motion.contains_key(&key) {
1424            state.carousel_motion.clear();
1425        }
1426        state.carousel_motion.insert(key.clone(), cm);
1427
1428        let widths: Vec<f32> = (0..n)
1429            .map(|i| match spec.strategy {
1430                CarouselStrategy::FullScreen => viewport_w,
1431                CarouselStrategy::Uncontained => (viewport_w * 0.60).max(1.0),
1432                CarouselStrategy::Hero => {
1433                    let large = viewport_w * 0.78;
1434                    let small = viewport_w * 0.18;
1435                    let d = (i as f32 - f_idx).abs();
1436                    if d <= 1.0 {
1437                        large + (small - large) * d
1438                    } else {
1439                        small
1440                    }
1441                }
1442                CarouselStrategy::MultiBrowse => {
1443                    let large = viewport_w * 0.55;
1444                    let med = viewport_w * 0.26;
1445                    let small = viewport_w * 0.12;
1446                    let d = (i as f32 - f_idx).abs();
1447                    if d <= 1.0 {
1448                        large + (med - large) * d
1449                    } else if d <= 2.0 {
1450                        med + (small - med) * (d - 1.0)
1451                    } else {
1452                        small
1453                    }
1454                }
1455            })
1456            .collect();
1457        let mut raw_x = Vec::with_capacity(n);
1458        let mut cursor_x = 0.0_f32;
1459        for &w in &widths {
1460            raw_x.push(cursor_x);
1461            cursor_x += w + gap;
1462        }
1463        let logical_x: Vec<f32> = match spec.strategy {
1464            CarouselStrategy::FullScreen => raw_x.iter().map(|&x| x - offset).collect(),
1465            CarouselStrategy::Uncontained if spec.align == CarouselAlign::Start => {
1466                raw_x.iter().map(|&x| x - offset).collect()
1467            }
1468            _ => {
1469                let k0 = (f_idx.floor() as usize).min(n - 1);
1470                let k1 = (k0 + 1).min(n - 1);
1471                let frac = (f_idx - k0 as f32).clamp(0.0, 1.0);
1472                let shift = if spec.align == CarouselAlign::Center {
1473                    let c0 = raw_x[k0] + widths[k0] * 0.5;
1474                    let c1 = raw_x[k1] + widths[k1] * 0.5;
1475                    viewport_w * 0.5 - (c0 + (c1 - c0) * frac)
1476                } else {
1477                    let s0 = raw_x[k0];
1478                    let s1 = raw_x[k1];
1479                    -(s0 + (s1 - s0) * frac)
1480                };
1481                raw_x.iter().map(|&x| x + shift).collect()
1482            }
1483        };
1484
1485        let any_dragging = touch_dragging || mouse_was_dragging;
1486        let mut action = None;
1487        let default_mask_r = state.dp(28.0);
1488        if child_source.is_some() {
1489            f.without_input(any_dragging, |f| {
1490                f.within(inner, |f| {
1491                    for (pos, &(orig_idx, child)) in visible_children.iter().enumerate() {
1492                        let w = widths[pos];
1493                        let phys_x = if rtl {
1494                            inner.x + inner.width - logical_x[pos] - w
1495                        } else {
1496                            inner.x + logical_x[pos]
1497                        };
1498                        let m = child.resolved_margin(state);
1499                        let avail_h = (inner.height - m[1] - m[3]).max(0.0);
1500                        let cid = child_view_id(child_parent_id, orig_idx, child);
1501                        let h = match child.height {
1502                            Size::Dp(v) => state.dp(v).min(avail_h),
1503                            Size::Wrap => child
1504                                .layout_intrinsic(false, state, w, f.ui.text_metrics, cid)
1505                                .min(avail_h)
1506                                .max(1.0),
1507                            Size::Match => avail_h,
1508                        };
1509                        let y = match child.layout_gravity[1] {
1510                            1 => inner.y + (inner.height - h) * 0.5 + m[1] - m[3],
1511                            2 => inner.y + inner.height - h - m[3],
1512                            _ => inner.y + m[1],
1513                        };
1514                        let item_rect = Rect { x: phys_x, y, width: w, height: h };
1515                        let vis = item_rect.intersect(inner);
1516                        if vis.width <= 0.0 || vis.height <= 0.0 {
1517                            continue;
1518                        }
1519                        let (is_oval, mask_radii) = if let Some(cr) = child.resolved_corner_radii(state) {
1520                            (false, cr)
1521                        } else if child.radius < 0.0 {
1522                            (true, [0.0; 4])
1523                        } else if child.radius > 0.0 {
1524                            (false, [state.dp(child.radius); 4])
1525                        } else {
1526                            (false, [default_mask_r; 4])
1527                        };
1528                        let cmd_start = f.commands.len();
1529                        let mut child_act = None;
1530                        f.within(item_rect, |f| {
1531                            child_act = child.draw(f, item_rect, state, cid);
1532                        });
1533                        clip_commands_to_rounded_outline(f, cmd_start, item_rect, is_oval, mask_radii);
1534                        if child_act.is_some() {
1535                            action = child_act;
1536                        }
1537                    }
1538                });
1539            });
1540        } else {
1541            let template = state.list_templates.get(&key).copied();
1542            let mut row_state = if template.is_some() || state.list_item_bindings.contains_key(&key)
1543            {
1544                Some(spinner_template_state(state, rtl))
1545            } else {
1546                None
1547            };
1548            let bindings = state.list_item_bindings.get(&key).cloned();
1549            let flags = state.list_enabled.get(&key).cloned();
1550            let item_ids = state.list_item_ids.get(&key).cloned();
1551            let checked = state.list_checked.get(&key).cloned().unwrap_or_default();
1552            let mut chosen = None;
1553            f.without_input(any_dragging, |f| {
1554                f.within(inner, |f| {
1555                    for (pos, label) in adapter_entries.iter().enumerate() {
1556                        let w = widths[pos];
1557                        let phys_x = if rtl {
1558                            inner.x + inner.width - logical_x[pos] - w
1559                        } else {
1560                            inner.x + logical_x[pos]
1561                        };
1562                        let item_rect = Rect {
1563                            x: phys_x,
1564                            y: inner.y,
1565                            width: w,
1566                            height: inner.height,
1567                        };
1568                        let vis = item_rect.intersect(inner);
1569                        if vis.width <= 0.0 || vis.height <= 0.0 {
1570                            continue;
1571                        }
1572                        let item_key = item_ids
1573                            .as_ref()
1574                            .and_then(|ids| ids.get(pos))
1575                            .map_or(pos as u64, |v| *v as u64);
1576                        let item_enabled =
1577                            enabled && flags.as_ref().and_then(|v| v.get(pos)).copied().unwrap_or(true);
1578                        let row_id = id.wrapping_mul(0x9e3779b97f4a7c15).wrapping_add(item_key);
1579                        let binding = bindings.as_ref().and_then(|items| items.get(&pos));
1580                        let item_template = adapter_item_template(template, binding);
1581                        let mask_r = item_template
1582                            .filter(|tmpl| tmpl.row.radius > 0.0)
1583                            .map_or(default_mask_r, |tmpl| state.dp(tmpl.row.radius));
1584                        let cmd_start = f.commands.len();
1585                        f.within(item_rect, |f| {
1586                            let r = f.target(row_id, item_rect, label, item_enabled, Role::Button);
1587                            if let (Some(tmpl), Some(rs)) = (item_template, row_state.as_mut()) {
1588                                paint_spinner_template(
1589                                    f,
1590                                    tmpl.row,
1591                                    tmpl.text_id,
1592                                    label,
1593                                    item_rect,
1594                                    rs,
1595                                    anonymous_child_id(id, 1024 + pos),
1596                                    r,
1597                                    checked.contains(&pos),
1598                                    binding,
1599                                );
1600                            } else {
1601                                let fill = if checked.contains(&pos) {
1602                                    t.primary_container
1603                                } else {
1604                                    t.surface_high
1605                                };
1606                                f.paint_rect(item_rect, mask_r, fill);
1607                                f.state_layer(r, mask_r, t.text);
1608                                let style = TextStyle {
1609                                    align: if rtl { 2 } else { 0 },
1610                                    padding: state.dp(12.0),
1611                                    ..TextStyle::default()
1612                                };
1613                                f.styled_text(
1614                                    item_rect,
1615                                    label,
1616                                    t.text.opacity(if r.enabled { 1.0 } else { 0.38 }),
1617                                    style,
1618                                );
1619                            }
1620                            if r.clicked {
1621                                chosen = Some(pos);
1622                            }
1623                        });
1624                        clip_commands_to_rounded_outline(f, cmd_start, item_rect, false, [mask_r; 4]);
1625                    }
1626                });
1627            });
1628            if let Some(idx) = chosen {
1629                state.list_clicked.insert(key, idx);
1630                action = Some(self.id);
1631            }
1632        }
1633        f.input = prev_input;
1634        action
1635    }
1636}