1use crate::layout::layout_ext::{apply_layout_size, finish_view_sized};
4use ailloli_ui_core::event::pointer::{MouseButton, PointerEvent};
5use ailloli_ui_core::event::{Event, Key, KeyState, NamedKey};
6use ailloli_ui_core::geometry::{Constraints, Rect, Size};
7use ailloli_ui_core::style::{Border, FlexItemStyle, LayoutSizeHint, LayoutStyle, Radius};
8use ailloli_ui_core::{Color, FontId, IconId, TextStyle, Theme};
9use ailloli_ui_runtime::component::{Binding, IntoView, View, Widget};
10use ailloli_ui_runtime::input::{ClickAction, EventCtx, FocusPolicy, IntoClickAction};
11use ailloli_ui_runtime::layout::{LayoutChild, LayoutCtx, LayoutResult};
12use ailloli_ui_runtime::scene::PaintCtx;
13use ailloli_ui_runtime::{DrawBorder, DrawCmd, DrawImage, DrawRRect, DrawText};
14use ailloli_ui_text::{PreparedTextLayout, TextLayoutParams, WrapMode};
15use lucide_icons::Icon as LucideIcon;
16use std::sync::Arc;
17
18#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
19pub enum DisclosureRowVariant {
28 #[default]
30 Default,
31 Danger,
33}
34
35#[derive(Clone, Debug, PartialEq)]
36pub struct DisclosureRowStyle {
48 pub height: f32,
50 pub padding_x: f32,
52 pub gap: f32,
54 pub icon_size: f32,
56 pub chevron_size: f32,
58 pub radius: Radius,
60 pub background: Color,
62 pub hover_background: Color,
64 pub pressed_background: Color,
66 pub selected_background: Color,
68 pub text: TextStyle,
70 pub trailing_text: TextStyle,
72 pub danger_text: TextStyle,
74 pub icon_tint: Color,
76 pub chevron_tint: Color,
78 pub focus_ring: Border,
80 pub disabled_opacity: f32,
82}
83
84impl Default for DisclosureRowStyle {
85 fn default() -> Self {
86 Self::from_theme(Theme::default())
87 }
88}
89
90impl DisclosureRowStyle {
91 pub fn from_theme(theme: Theme) -> Self {
106 let palette = theme.palette();
107 Self {
108 height: 38.0,
109 padding_x: 12.0,
110 gap: 8.0,
111 icon_size: 16.0,
112 chevron_size: 16.0,
113 radius: Radius::uniform(theme.radius().md),
114 background: Color::TRANSPARENT,
115 hover_background: palette.surface_elevated,
116 pressed_background: Color::hex_rgb(0x20252A),
117 selected_background: palette.accent.with_alpha(0.18),
118 text: TextStyle::new(FontId::Ui, 13, palette.text),
119 trailing_text: TextStyle::new(FontId::Ui, 12, palette.text_muted),
120 danger_text: TextStyle::new(FontId::Ui, 13, palette.danger),
121 icon_tint: palette.text_muted,
122 chevron_tint: palette.text_muted,
123 focus_ring: Border::new(1.0, palette.focus),
124 disabled_opacity: 0.42,
125 }
126 }
127}
128
129pub struct DisclosureRow<A = ()> {
144 pub(crate) layout: LayoutStyle,
146 pub(crate) flex_item: FlexItemStyle,
148 label: String,
150 leading_icon: Option<IconId>,
152 trailing_text: Option<String>,
154 selected: Binding<bool>,
156 disabled: Binding<bool>,
158 variant: DisclosureRowVariant,
160 style: DisclosureRowStyle,
162 show_chevron: bool,
164 on_select: Option<ClickAction<A>>,
166}
167
168crate::impl_layout_builders!(DisclosureRow);
169
170impl<A: 'static> DisclosureRow<A> {
171 pub fn new(label: impl Into<String>) -> Self {
183 Self {
184 layout: LayoutStyle::default(),
185 flex_item: FlexItemStyle::default(),
186 label: label.into(),
187 leading_icon: None,
188 trailing_text: None,
189 selected: Binding::Static(false),
190 disabled: Binding::Static(false),
191 variant: DisclosureRowVariant::Default,
192 style: DisclosureRowStyle::default(),
193 show_chevron: true,
194 on_select: None,
195 }
196 }
197
198 pub fn leading_icon(mut self, icon: IconId) -> Self {
209 self.leading_icon = Some(icon);
210 self
211 }
212
213 pub fn trailing_text(mut self, text: impl Into<String>) -> Self {
226 self.trailing_text = Some(text.into());
227 self
228 }
229
230 pub fn selected(mut self, selected: impl Into<Binding<bool>>) -> Self {
243 self.selected = selected.into();
244 self
245 }
246
247 pub fn disabled(mut self, disabled: impl Into<Binding<bool>>) -> Self {
260 self.disabled = disabled.into();
261 self
262 }
263
264 pub fn variant(mut self, variant: DisclosureRowVariant) -> Self {
275 self.variant = variant;
276 self
277 }
278
279 pub fn disclosure_row_style(mut self, style: DisclosureRowStyle) -> Self {
294 self.style = style;
295 self
296 }
297
298 pub fn show_chevron(mut self, show: bool) -> Self {
308 self.show_chevron = show;
309 self
310 }
311
312 pub fn on_select(mut self, action: impl IntoClickAction<A>) -> Self
327 where
328 A: Clone,
329 {
330 self.on_select = Some(action.into_click_action());
331 self
332 }
333
334 pub fn on_select_ctx(mut self, f: impl Fn(&mut EventCtx<A>) + 'static) -> Self {
347 self.on_select = Some(ClickAction::handler(f));
348 self
349 }
350}
351
352struct DisclosureRowWidget<A> {
354 layout: LayoutStyle,
356 label: String,
358 leading_icon: Option<IconId>,
360 trailing_text: Option<String>,
362 selected: Binding<bool>,
364 disabled: Binding<bool>,
366 variant: DisclosureRowVariant,
368 style: DisclosureRowStyle,
370 show_chevron: bool,
372 on_select: Option<ClickAction<A>>,
374}
375
376impl<A: 'static> Widget<A> for DisclosureRowWidget<A> {
377 fn debug_name(&self) -> &'static str {
378 "DisclosureRow"
379 }
380
381 fn layout(
382 &self,
383 _engine: &mut ailloli_ui_runtime::layout::LayoutEngine<'_, A>,
384 ctx: &mut LayoutCtx<'_>,
385 _children: &mut [LayoutChild],
386 constraints: Constraints,
387 ) -> LayoutResult {
388 let label_w = measure_text(ctx, &self.label, self.label_style()).unwrap_or(96.0);
389 let icon_w = self
390 .leading_icon
391 .as_ref()
392 .map(|_| self.style.icon_size + self.style.gap)
393 .unwrap_or(0.0);
394 let trailing_w = self
395 .trailing_text
396 .as_deref()
397 .and_then(|text| measure_text(ctx, text, self.style.trailing_text))
398 .map(|w| w + self.style.gap)
399 .unwrap_or(0.0);
400 let chevron_w = if self.show_chevron {
401 self.style.chevron_size
402 } else {
403 0.0
404 };
405 let intrinsic = Size::new(
406 self.style.padding_x * 2.0 + icon_w + label_w + trailing_w + chevron_w,
407 self.style.height,
408 );
409 let size = apply_layout_size(intrinsic, self.layout, constraints);
410 LayoutResult {
411 size,
412 children: Vec::new(),
413 paint_bounds: Rect::new(0.0, 0.0, size.w, size.h),
414 visual_bounds: Rect::new(0.0, 0.0, size.w, size.h),
415 overlay_hit_bounds: Vec::new(),
416 clip: None,
417 is_window_root_clip: false,
418 artifact: None,
419 }
420 }
421
422 fn paint(&self, ctx: &mut PaintCtx<'_>, bounds: Rect, _layout: &LayoutResult) {
423 let selected = self.selected.read();
424 let disabled = self.disabled.read();
425 let interaction = ctx.interaction();
426 let mut bg = self.style.background;
427 if selected {
428 bg = self.style.selected_background;
429 } else if interaction.pressed && self.on_select.is_some() && !disabled {
430 bg = self.style.pressed_background;
431 } else if interaction.hovered && self.on_select.is_some() && !disabled {
432 bg = self.style.hover_background;
433 }
434
435 let opacity = if disabled {
436 self.style.disabled_opacity
437 } else {
438 1.0
439 };
440 if bg.a > 0.0 {
441 ctx.push(DrawCmd::RRect(DrawRRect {
442 rect: bounds,
443 radius: self.style.radius.tl,
444 color: bg.with_alpha(bg.a * opacity),
445 }));
446 }
447
448 let mut x = bounds.x + self.style.padding_x;
449 if let Some(icon) = &self.leading_icon {
450 let icon_rect = Rect::new(
451 x,
452 bounds.y + (bounds.h - self.style.icon_size) * 0.5,
453 self.style.icon_size,
454 self.style.icon_size,
455 );
456 ctx.push(DrawCmd::Image(DrawImage {
457 rect: icon_rect,
458 icon: icon.clone(),
459 tint: self
460 .style
461 .icon_tint
462 .with_alpha(self.style.icon_tint.a * opacity),
463 rotation_rad: 0.0,
464 }));
465 x += self.style.icon_size + self.style.gap;
466 }
467
468 paint_text_centered(ctx, &self.label, self.label_style(), bounds, x, opacity);
469
470 let mut right = bounds.right() - self.style.padding_x;
471 if self.show_chevron {
472 let chevron_rect = Rect::new(
473 right - self.style.chevron_size,
474 bounds.y + (bounds.h - self.style.chevron_size) * 0.5,
475 self.style.chevron_size,
476 self.style.chevron_size,
477 );
478 ctx.push(DrawCmd::Image(DrawImage {
479 rect: chevron_rect,
480 icon: IconId::Lucide(LucideIcon::ChevronRight),
481 tint: self
482 .style
483 .chevron_tint
484 .with_alpha(self.style.chevron_tint.a * opacity),
485 rotation_rad: 0.0,
486 }));
487 right = chevron_rect.x - self.style.gap;
488 }
489
490 if let Some(trailing) = &self.trailing_text {
491 paint_trailing_text(
492 ctx,
493 trailing,
494 self.style.trailing_text,
495 bounds,
496 right,
497 opacity,
498 );
499 }
500
501 if interaction.focused && self.on_select.is_some() && !disabled {
502 ctx.push(DrawCmd::Border(DrawBorder {
503 rect: bounds,
504 radius: self.style.radius,
505 border: self.style.focus_ring,
506 }));
507 }
508 }
509
510 fn event(&self, ctx: &mut EventCtx<A>, event: &Event, bounds: Rect, _layout: &LayoutResult) {
511 if self.disabled.read() {
512 return;
513 }
514 match event {
515 Event::Pointer(PointerEvent::Button {
516 pos,
517 button: MouseButton::Left,
518 pressed: false,
519 ..
520 }) if bounds.contains(pos.x, pos.y) => self.run_action(ctx),
521 Event::Keyboard(key) if key.state == KeyState::Pressed => {
522 if matches!(
523 &key.key,
524 Key::Named(NamedKey::Enter) | Key::Named(NamedKey::Space)
525 ) {
526 self.run_action(ctx);
527 }
528 }
529 _ => {}
530 }
531 }
532
533 fn focus_policy(&self) -> FocusPolicy {
534 if self.on_select.is_some() && !self.disabled.read() {
535 FocusPolicy::Focusable
536 } else {
537 FocusPolicy::NotFocusable
538 }
539 }
540}
541
542impl<A> DisclosureRowWidget<A> {
543 fn run_action(&self, ctx: &mut EventCtx<A>) {
545 if let Some(on_select) = &self.on_select {
546 on_select.run(ctx);
547 ctx.stop_propagation();
548 }
549 }
550
551 fn label_style(&self) -> TextStyle {
553 if self.variant == DisclosureRowVariant::Danger {
554 self.style.danger_text
555 } else {
556 self.style.text
557 }
558 }
559}
560
561impl<A: 'static> IntoView<A> for DisclosureRow<A> {
562 fn into_view(self) -> View<A> {
563 finish_view_sized(
564 View::leaf(DisclosureRowWidget {
565 layout: self.layout,
566 label: self.label,
567 leading_icon: self.leading_icon,
568 trailing_text: self.trailing_text,
569 selected: self.selected,
570 disabled: self.disabled,
571 variant: self.variant,
572 style: self.style,
573 show_chevron: self.show_chevron,
574 on_select: self.on_select,
575 }),
576 self.flex_item,
577 LayoutSizeHint::from_layout(self.layout),
578 )
579 }
580}
581
582fn measure_text(ctx: &mut LayoutCtx<'_>, text: &str, style: TextStyle) -> Option<f32> {
584 ctx.text_system.as_deref_mut().map(|text_system| {
585 text_system
586 .layout_cached(TextLayoutParams {
587 text,
588 style,
589 max_width: None,
590 wrap_mode: WrapMode::NoWrap,
591 })
592 .metrics
593 .width
594 })
595}
596
597fn layout_text(
599 ctx: &mut PaintCtx<'_>,
600 text: &str,
601 style: TextStyle,
602) -> Option<Arc<PreparedTextLayout>> {
603 ctx.text_system.as_deref_mut().map(|text_system| {
604 text_system.layout_cached(TextLayoutParams {
605 text,
606 style,
607 max_width: None,
608 wrap_mode: WrapMode::NoWrap,
609 })
610 })
611}
612
613fn paint_text_centered(
615 ctx: &mut PaintCtx<'_>,
616 text: &str,
617 style: TextStyle,
618 bounds: Rect,
619 x: f32,
620 opacity: f32,
621) {
622 let Some(layout) = layout_text(ctx, text, style) else {
623 return;
624 };
625 let baseline = layout
626 .lines
627 .first()
628 .map(|line| line.baseline_y)
629 .unwrap_or(0.0);
630 let y = bounds.y + (bounds.h - layout.metrics.height) * 0.5 + baseline;
631 ctx.push(DrawCmd::Text(DrawText {
632 pos: [x, y],
633 color: style.color.with_alpha(style.color.a * opacity),
634 decoration: ailloli_ui_core::TextDecoration::None,
635 layout,
636 }));
637}
638
639fn paint_trailing_text(
641 ctx: &mut PaintCtx<'_>,
642 text: &str,
643 style: TextStyle,
644 bounds: Rect,
645 right: f32,
646 opacity: f32,
647) {
648 let Some(layout) = layout_text(ctx, text, style) else {
649 return;
650 };
651 let baseline = layout
652 .lines
653 .first()
654 .map(|line| line.baseline_y)
655 .unwrap_or(0.0);
656 let x = right - layout.metrics.width;
657 let y = bounds.y + (bounds.h - layout.metrics.height) * 0.5 + baseline;
658 ctx.push(DrawCmd::Text(DrawText {
659 pos: [x, y],
660 color: style.color.with_alpha(style.color.a * opacity),
661 decoration: ailloli_ui_core::TextDecoration::None,
662 layout,
663 }));
664}