cosmic/widget/list/
column.rs

1// Copyright 2022 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4use iced_core::Padding;
5use iced_widget::container::Catalog;
6
7use crate::{
8    Apply, Element, theme,
9    widget::{container, divider, vertical_space},
10};
11
12#[inline]
13pub fn list_column<'a, Message: 'static>() -> ListColumn<'a, Message> {
14    ListColumn::default()
15}
16
17#[must_use]
18pub struct ListColumn<'a, Message> {
19    spacing: u16,
20    padding: Padding,
21    list_item_padding: Padding,
22    divider_padding: u16,
23    style: theme::Container<'a>,
24    children: Vec<Element<'a, Message>>,
25}
26
27impl<Message: 'static> Default for ListColumn<'_, Message> {
28    fn default() -> Self {
29        let cosmic_theme::Spacing {
30            space_xxs, space_m, ..
31        } = theme::spacing();
32
33        Self {
34            spacing: 0,
35            padding: Padding::from(0),
36            divider_padding: 16,
37            list_item_padding: [space_xxs, space_m].into(),
38            style: theme::Container::List,
39            children: Vec::with_capacity(4),
40        }
41    }
42}
43
44impl<'a, Message: 'static> ListColumn<'a, Message> {
45    #[inline]
46    pub fn new() -> Self {
47        Self::default()
48    }
49
50    #[allow(clippy::should_implement_trait)]
51    pub fn add(self, item: impl Into<Element<'a, Message>>) -> Self {
52        #[inline(never)]
53        fn inner<'a, Message: 'static>(
54            mut this: ListColumn<'a, Message>,
55            item: Element<'a, Message>,
56        ) -> ListColumn<'a, Message> {
57            if !this.children.is_empty() {
58                this.children.push(
59                    container(divider::horizontal::default())
60                        .padding([0, this.divider_padding])
61                        .into(),
62                );
63            }
64
65            // Ensure a minimum height of 32.
66            let list_item = iced::widget::row![
67                container(item).align_y(iced::Alignment::Center),
68                vertical_space().height(iced::Length::Fixed(32.))
69            ]
70            .padding(this.list_item_padding)
71            .align_y(iced::Alignment::Center);
72
73            this.children.push(list_item.into());
74            this
75        }
76
77        inner(self, item.into())
78    }
79
80    #[inline]
81    pub fn spacing(mut self, spacing: u16) -> Self {
82        self.spacing = spacing;
83        self
84    }
85
86    /// Sets the style variant of this [`Circular`].
87    #[inline]
88    pub fn style(mut self, style: <crate::Theme as Catalog>::Class<'a>) -> Self {
89        self.style = style;
90        self
91    }
92
93    #[inline]
94    pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
95        self.padding = padding.into();
96        self
97    }
98
99    #[inline]
100    pub fn divider_padding(mut self, padding: u16) -> Self {
101        self.divider_padding = padding;
102        self
103    }
104
105    pub fn list_item_padding(mut self, padding: impl Into<Padding>) -> Self {
106        self.list_item_padding = padding.into();
107        self
108    }
109
110    #[must_use]
111    pub fn into_element(self) -> Element<'a, Message> {
112        crate::widget::column::with_children(self.children)
113            .spacing(self.spacing)
114            .padding(self.padding)
115            .apply(container)
116            .padding([self.spacing, 0])
117            .class(self.style)
118            .width(iced::Length::Fill)
119            .into()
120    }
121}
122
123impl<'a, Message: 'static> From<ListColumn<'a, Message>> for Element<'a, Message> {
124    fn from(column: ListColumn<'a, Message>) -> Self {
125        column.into_element()
126    }
127}