1use crate::layout::layout_ext::{apply_layout_size, finish_view_sized};
4use crate::layout::{Column, Container};
5use ailloli_ui_core::event::pointer::{MouseButton, PointerEvent};
6use ailloli_ui_core::event::{Event, Key, KeyState, NamedKey};
7use ailloli_ui_core::geometry::{Constraints, Rect, Size};
8use ailloli_ui_core::style::{
9 AlignItems, Border, FlexItemStyle, LayoutSizeHint, LayoutStyle, Radius,
10};
11use ailloli_ui_core::{Color, FontId, IconId, TextStyle, Theme};
12use ailloli_ui_runtime::component::{Binding, IntoView, View, Widget};
13use ailloli_ui_runtime::input::{
14 ActivationPolicy, ClickAction, EventCtx, FocusPolicy, IntoClickAction,
15};
16use ailloli_ui_runtime::layout::{LayoutChild, LayoutCtx, LayoutResult};
17use ailloli_ui_runtime::scene::PaintCtx;
18use ailloli_ui_runtime::{DrawBorder, DrawCmd, DrawImage, DrawRRect, DrawText};
19use ailloli_ui_text::{PreparedTextLayout, TextLayoutParams, WrapMode};
20use std::sync::Arc;
21
22#[derive(Clone, Debug, PartialEq)]
23pub struct ListViewStyle {
38 pub background: Color,
40 pub border: Border,
42 pub radius: Radius,
44 pub padding: f32,
46 pub gap: f32,
48 pub width: f32,
50}
51
52impl Default for ListViewStyle {
53 fn default() -> Self {
54 Self::from_theme(Theme::default())
55 }
56}
57
58impl ListViewStyle {
59 pub fn from_theme(theme: Theme) -> Self {
71 let palette = theme.palette();
72 Self {
73 background: palette.surface,
74 border: Border::new(1.0, palette.border),
75 radius: theme.radius().button(),
76 padding: theme.spacing().xs,
77 gap: 2.0,
78 width: 260.0,
79 }
80 }
81}
82
83pub struct ListView<A = ()> {
97 pub(crate) layout: LayoutStyle,
99 pub(crate) flex_item: FlexItemStyle,
101 style: ListViewStyle,
103 items: Vec<View<A>>,
105}
106
107crate::impl_layout_builders!(ListView);
108
109impl<A: 'static> Default for ListView<A> {
110 fn default() -> Self {
111 Self::new()
112 }
113}
114
115impl<A: 'static> ListView<A> {
116 pub fn new() -> Self {
126 let style = ListViewStyle::default();
127 Self {
128 layout: LayoutStyle::default().width(style.width),
129 flex_item: FlexItemStyle::default(),
130 style,
131 items: Vec::new(),
132 }
133 }
134
135 pub fn list_view_style(mut self, style: ListViewStyle) -> Self {
150 self.layout = self.layout.width(style.width);
151 self.style = style;
152 self
153 }
154
155 pub fn item(mut self, item: ListItem<A>) -> Self {
169 self.items.push(item.fill_width().into_view());
170 self
171 }
172}
173
174impl<A: 'static> IntoView<A> for ListView<A> {
175 fn into_view(self) -> View<A> {
176 let mut content = Column::new()
177 .gap(self.style.gap)
178 .align_items(AlignItems::Stretch);
179 for item in self.items {
180 content = content.child(item);
181 }
182
183 let mut container = Container::<A>::new()
184 .background(self.style.background)
185 .radius(self.style.radius.tl)
186 .border(self.style.border.widths.top, self.style.border.colors.top)
187 .padding(self.style.padding)
188 .child(content);
189 container.layout = self.layout;
190 container.flex_item = self.flex_item;
191 container.into_view()
192 }
193}
194
195#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
196pub enum ListItemVariant {
205 #[default]
207 Default,
208 Danger,
210}
211
212#[derive(Clone, Debug, PartialEq)]
213pub struct ListItemStyle {
225 pub height: f32,
227 pub subtitle_height: f32,
229 pub padding_x: f32,
231 pub gap: f32,
233 pub icon_size: f32,
235 pub radius: Radius,
237 pub background: Color,
239 pub hover_background: Color,
241 pub pressed_background: Color,
243 pub selected_background: Color,
245 pub title_text: TextStyle,
247 pub subtitle_text: TextStyle,
249 pub trailing_text: TextStyle,
251 pub danger_text: TextStyle,
253 pub icon_tint: Color,
255 pub selected_icon_tint: Color,
257 pub badge_background: Color,
259 pub badge_text: TextStyle,
261 pub focus_ring: Border,
263 pub disabled_opacity: f32,
265}
266
267impl Default for ListItemStyle {
268 fn default() -> Self {
269 Self::from_theme(Theme::default())
270 }
271}
272
273impl ListItemStyle {
274 pub fn from_theme(theme: Theme) -> Self {
286 let palette = theme.palette();
287 Self {
288 height: 36.0,
289 subtitle_height: 52.0,
290 padding_x: 10.0,
291 gap: 8.0,
292 icon_size: 16.0,
293 radius: Radius::uniform(theme.radius().md),
294 background: Color::TRANSPARENT,
295 hover_background: palette.surface_elevated,
296 pressed_background: Color::hex_rgb(0x20252A),
297 selected_background: palette.accent.with_alpha(0.18),
298 title_text: TextStyle::new(FontId::Ui, 13, palette.text),
299 subtitle_text: TextStyle::new(FontId::Ui, 12, palette.text_muted),
300 trailing_text: TextStyle::new(FontId::Ui, 12, palette.text_muted),
301 danger_text: TextStyle::new(FontId::Ui, 13, palette.danger),
302 icon_tint: palette.text_muted,
303 selected_icon_tint: palette.accent,
304 badge_background: palette.surface_elevated,
305 badge_text: TextStyle::new(FontId::Ui, 11, palette.text),
306 focus_ring: Border::new(1.0, palette.focus),
307 disabled_opacity: 0.42,
308 }
309 }
310}
311
312pub struct ListItem<A = ()> {
327 pub(crate) layout: LayoutStyle,
329 pub(crate) flex_item: FlexItemStyle,
331 title: String,
333 subtitle: Option<String>,
335 leading_icon: Option<IconId>,
337 trailing_text: Option<String>,
339 badge: Option<u32>,
341 selected: Binding<bool>,
343 disabled: Binding<bool>,
345 variant: ListItemVariant,
347 style: ListItemStyle,
349 on_select: Option<ClickAction<A>>,
351}
352
353crate::impl_layout_builders!(ListItem);
354
355impl<A: 'static> ListItem<A> {
356 pub fn new(title: impl Into<String>) -> Self {
366 Self {
367 layout: LayoutStyle::default(),
368 flex_item: FlexItemStyle::default(),
369 title: title.into(),
370 subtitle: None,
371 leading_icon: None,
372 trailing_text: None,
373 badge: None,
374 selected: Binding::Static(false),
375 disabled: Binding::Static(false),
376 variant: ListItemVariant::Default,
377 style: ListItemStyle::default(),
378 on_select: None,
379 }
380 }
381
382 pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
394 self.subtitle = Some(subtitle.into());
395 self
396 }
397
398 pub fn leading_icon(mut self, icon: IconId) -> Self {
409 self.leading_icon = Some(icon);
410 self
411 }
412
413 pub fn trailing_text(mut self, text: impl Into<String>) -> Self {
423 self.trailing_text = Some(text.into());
424 self
425 }
426
427 pub fn badge(mut self, count: u32) -> Self {
440 self.badge = Some(count);
441 self
442 }
443
444 pub fn selected(mut self, selected: impl Into<Binding<bool>>) -> Self {
457 self.selected = selected.into();
458 self
459 }
460
461 pub fn disabled(mut self, disabled: impl Into<Binding<bool>>) -> Self {
474 self.disabled = disabled.into();
475 self
476 }
477
478 pub fn variant(mut self, variant: ListItemVariant) -> Self {
488 self.variant = variant;
489 self
490 }
491
492 pub fn list_item_style(mut self, style: ListItemStyle) -> Self {
506 self.style = style;
507 self
508 }
509
510 pub fn on_select(mut self, action: impl IntoClickAction<A>) -> Self
525 where
526 A: Clone,
527 {
528 self.on_select = Some(action.into_click_action());
529 self
530 }
531
532 pub fn on_select_ctx(mut self, f: impl Fn(&mut EventCtx<A>) + 'static) -> Self {
544 self.on_select = Some(ClickAction::handler(f));
545 self
546 }
547}
548
549struct ListItemWidget<A> {
551 layout: LayoutStyle,
553 title: String,
555 subtitle: Option<String>,
557 leading_icon: Option<IconId>,
559 trailing_text: Option<String>,
561 badge: Option<u32>,
563 selected: Binding<bool>,
565 disabled: Binding<bool>,
567 variant: ListItemVariant,
569 style: ListItemStyle,
571 on_select: Option<ClickAction<A>>,
573}
574
575impl<A: 'static> Widget<A> for ListItemWidget<A> {
576 fn debug_name(&self) -> &'static str {
577 "ListItem"
578 }
579
580 fn layout(
581 &self,
582 _engine: &mut ailloli_ui_runtime::layout::LayoutEngine<'_, A>,
583 ctx: &mut LayoutCtx<'_>,
584 _children: &mut [LayoutChild],
585 constraints: Constraints,
586 ) -> LayoutResult {
587 let title_w = measure_text(ctx, &self.title, self.title_style(false)).unwrap_or(80.0);
588 let subtitle_w = self
589 .subtitle
590 .as_deref()
591 .and_then(|text| measure_text(ctx, text, self.style.subtitle_text))
592 .unwrap_or(0.0);
593 let icon_w = self
594 .leading_icon
595 .as_ref()
596 .map(|_| self.style.icon_size + self.style.gap)
597 .unwrap_or(0.0);
598 let trailing_w = self
599 .trailing_text
600 .as_deref()
601 .and_then(|text| measure_text(ctx, text, self.style.trailing_text))
602 .unwrap_or(0.0);
603 let badge_w = self
604 .badge
605 .map(|count| measure_badge(ctx, count, &self.style))
606 .unwrap_or(0.0);
607 let height = if self.subtitle.is_some() {
608 self.style.subtitle_height
609 } else {
610 self.style.height
611 };
612 let intrinsic = Size::new(
613 self.style.padding_x * 2.0
614 + icon_w
615 + title_w.max(subtitle_w)
616 + trailing_w
617 + badge_w
618 + self.style.gap,
619 height,
620 );
621 let size = apply_layout_size(intrinsic, self.layout, constraints);
622 LayoutResult {
623 size,
624 children: Vec::new(),
625 paint_bounds: Rect::new(0.0, 0.0, size.w, size.h),
626 visual_bounds: Rect::new(0.0, 0.0, size.w, size.h),
627 overlay_hit_bounds: Vec::new(),
628 clip: None,
629 is_window_root_clip: false,
630 artifact: None,
631 }
632 }
633
634 fn paint(&self, ctx: &mut PaintCtx<'_>, bounds: Rect, _layout: &LayoutResult) {
635 let selected = self.selected.read();
636 let disabled = self.disabled.read();
637 let interaction = ctx.interaction();
638 let mut bg = self.style.background;
639 if selected {
640 bg = self.style.selected_background;
641 } else if interaction.pressed && self.on_select.is_some() && !disabled {
642 bg = self.style.pressed_background;
643 } else if interaction.hovered && self.on_select.is_some() && !disabled {
644 bg = self.style.hover_background;
645 }
646
647 let opacity = if disabled {
648 self.style.disabled_opacity
649 } else {
650 1.0
651 };
652 if bg.a > 0.0 {
653 ctx.push(DrawCmd::RRect(DrawRRect {
654 rect: bounds,
655 radius: self.style.radius.tl,
656 color: bg.with_alpha(bg.a * opacity),
657 }));
658 }
659
660 let mut x = bounds.x + self.style.padding_x;
661 if let Some(icon) = &self.leading_icon {
662 let rect = Rect::new(
663 x,
664 bounds.y + (bounds.h - self.style.icon_size) * 0.5,
665 self.style.icon_size,
666 self.style.icon_size,
667 );
668 let tint = if selected {
669 self.style.selected_icon_tint
670 } else {
671 self.style.icon_tint
672 };
673 ctx.push(DrawCmd::Image(DrawImage {
674 rect,
675 icon: icon.clone(),
676 tint: tint.with_alpha(tint.a * opacity),
677 rotation_rad: 0.0,
678 }));
679 x += self.style.icon_size + self.style.gap;
680 }
681
682 if let Some(subtitle) = &self.subtitle {
683 paint_text_at(
684 ctx,
685 &self.title,
686 self.title_style(selected),
687 x,
688 bounds.y + 19.0,
689 opacity,
690 );
691 paint_text_at(
692 ctx,
693 subtitle,
694 self.style.subtitle_text,
695 x,
696 bounds.y + 37.0,
697 opacity,
698 );
699 } else {
700 paint_text_centered(
701 ctx,
702 &self.title,
703 self.title_style(selected),
704 bounds,
705 x,
706 opacity,
707 );
708 }
709
710 let mut right = bounds.right() - self.style.padding_x;
711 if let Some(count) = self.badge {
712 right = paint_badge(ctx, count, bounds, right, &self.style, opacity) - self.style.gap;
713 }
714 if let Some(trailing) = &self.trailing_text {
715 paint_trailing_text(
716 ctx,
717 trailing,
718 self.style.trailing_text,
719 bounds,
720 right,
721 opacity,
722 );
723 }
724
725 if interaction.focused && self.on_select.is_some() && !disabled {
726 ctx.push(DrawCmd::Border(DrawBorder {
727 rect: bounds,
728 radius: self.style.radius,
729 border: self.style.focus_ring,
730 }));
731 }
732 }
733
734 fn event(&self, ctx: &mut EventCtx<A>, event: &Event, bounds: Rect, _layout: &LayoutResult) {
735 if self.disabled.read() {
736 return;
737 }
738 match event {
739 Event::Pointer(PointerEvent::Button {
740 pos,
741 button: MouseButton::Left,
742 pressed: false,
743 ..
744 }) if bounds.contains(pos.x, pos.y) => self.run_action(ctx),
745 Event::Keyboard(key) if key.state == KeyState::Pressed => {
746 if matches!(
747 &key.key,
748 Key::Named(NamedKey::Enter) | Key::Named(NamedKey::Space)
749 ) {
750 self.run_action(ctx);
751 }
752 }
753 _ => {}
754 }
755 }
756
757 fn focus_policy(&self) -> FocusPolicy {
758 if self.on_select.is_some() && !self.disabled.read() {
759 FocusPolicy::Focusable
760 } else {
761 FocusPolicy::NotFocusable
762 }
763 }
764
765 fn activation_policy(&self) -> ActivationPolicy {
766 ActivationPolicy::SuppressOnFocusOnly
767 }
768}
769
770impl<A> ListItemWidget<A> {
771 fn run_action(&self, ctx: &mut EventCtx<A>) {
773 if let Some(on_select) = &self.on_select {
774 on_select.run(ctx);
775 ctx.stop_propagation();
776 }
777 }
778
779 fn title_style(&self, selected: bool) -> TextStyle {
781 if self.variant == ListItemVariant::Danger {
782 self.style.danger_text
783 } else if selected {
784 TextStyle {
785 color: self.style.selected_icon_tint,
786 ..self.style.title_text
787 }
788 } else {
789 self.style.title_text
790 }
791 }
792}
793
794impl<A: 'static> IntoView<A> for ListItem<A> {
795 fn into_view(self) -> View<A> {
796 finish_view_sized(
797 View::leaf(ListItemWidget {
798 layout: self.layout,
799 title: self.title,
800 subtitle: self.subtitle,
801 leading_icon: self.leading_icon,
802 trailing_text: self.trailing_text,
803 badge: self.badge,
804 selected: self.selected,
805 disabled: self.disabled,
806 variant: self.variant,
807 style: self.style,
808 on_select: self.on_select,
809 }),
810 self.flex_item,
811 LayoutSizeHint::from_layout(self.layout),
812 )
813 }
814}
815
816fn measure_text(ctx: &mut LayoutCtx<'_>, text: &str, style: TextStyle) -> Option<f32> {
818 ctx.text_system.as_deref_mut().map(|text_system| {
819 text_system
820 .layout_cached(TextLayoutParams {
821 text,
822 style,
823 max_width: None,
824 wrap_mode: WrapMode::NoWrap,
825 })
826 .metrics
827 .width
828 })
829}
830
831fn layout_text(
833 ctx: &mut PaintCtx<'_>,
834 text: &str,
835 style: TextStyle,
836) -> Option<Arc<PreparedTextLayout>> {
837 ctx.text_system.as_deref_mut().map(|text_system| {
838 text_system.layout_cached(TextLayoutParams {
839 text,
840 style,
841 max_width: None,
842 wrap_mode: WrapMode::NoWrap,
843 })
844 })
845}
846
847fn paint_text_at(
849 ctx: &mut PaintCtx<'_>,
850 text: &str,
851 style: TextStyle,
852 x: f32,
853 baseline_y: f32,
854 opacity: f32,
855) {
856 let Some(layout) = layout_text(ctx, text, style) else {
857 return;
858 };
859 ctx.push(DrawCmd::Text(DrawText {
860 pos: [x, baseline_y],
861 color: style.color.with_alpha(style.color.a * opacity),
862 decoration: ailloli_ui_core::TextDecoration::None,
863 layout,
864 }));
865}
866
867fn paint_text_centered(
869 ctx: &mut PaintCtx<'_>,
870 text: &str,
871 style: TextStyle,
872 bounds: Rect,
873 x: f32,
874 opacity: f32,
875) {
876 let Some(layout) = layout_text(ctx, text, style) else {
877 return;
878 };
879 let baseline = layout
880 .lines
881 .first()
882 .map(|line| line.baseline_y)
883 .unwrap_or(0.0);
884 let y = bounds.y + (bounds.h - layout.metrics.height) * 0.5 + baseline;
885 ctx.push(DrawCmd::Text(DrawText {
886 pos: [x, y],
887 color: style.color.with_alpha(style.color.a * opacity),
888 decoration: ailloli_ui_core::TextDecoration::None,
889 layout,
890 }));
891}
892
893fn paint_trailing_text(
895 ctx: &mut PaintCtx<'_>,
896 text: &str,
897 style: TextStyle,
898 bounds: Rect,
899 right: f32,
900 opacity: f32,
901) {
902 let Some(layout) = layout_text(ctx, text, style) else {
903 return;
904 };
905 let baseline = layout
906 .lines
907 .first()
908 .map(|line| line.baseline_y)
909 .unwrap_or(0.0);
910 let x = right - layout.metrics.width;
911 let y = bounds.y + (bounds.h - layout.metrics.height) * 0.5 + baseline;
912 ctx.push(DrawCmd::Text(DrawText {
913 pos: [x, y],
914 color: style.color.with_alpha(style.color.a * opacity),
915 decoration: ailloli_ui_core::TextDecoration::None,
916 layout,
917 }));
918}
919
920fn measure_badge(ctx: &mut LayoutCtx<'_>, count: u32, style: &ListItemStyle) -> f32 {
922 let label = count.to_string();
923 let text_w = measure_text(ctx, &label, style.badge_text).unwrap_or(8.0);
924 text_w.max(8.0) + 16.0
925}
926
927fn paint_badge(
929 ctx: &mut PaintCtx<'_>,
930 count: u32,
931 bounds: Rect,
932 right: f32,
933 style: &ListItemStyle,
934 opacity: f32,
935) -> f32 {
936 let label = count.to_string();
937 let Some(layout) = layout_text(ctx, &label, style.badge_text) else {
938 return right;
939 };
940 let w = (layout.metrics.width + 12.0).max(20.0);
941 let h = 18.0;
942 let rect = Rect::new(right - w, bounds.y + (bounds.h - h) * 0.5, w, h);
943 ctx.push(DrawCmd::RRect(DrawRRect {
944 rect,
945 radius: h * 0.5,
946 color: style
947 .badge_background
948 .with_alpha(style.badge_background.a * opacity),
949 }));
950 let baseline = layout
951 .lines
952 .first()
953 .map(|line| line.baseline_y)
954 .unwrap_or(0.0);
955 let x = rect.x + (rect.w - layout.metrics.width) * 0.5;
956 let y = rect.y + (rect.h - layout.metrics.height) * 0.5 + baseline;
957 ctx.push(DrawCmd::Text(DrawText {
958 pos: [x, y],
959 color: style
960 .badge_text
961 .color
962 .with_alpha(style.badge_text.color.a * opacity),
963 decoration: ailloli_ui_core::TextDecoration::None,
964 layout,
965 }));
966 rect.x
967}