cosmic/widget/settings/
section.rs

1// Copyright 2022 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4use crate::Element;
5use crate::widget::{ListColumn, column, text};
6use std::borrow::Cow;
7
8/// A section within a settings view column.
9pub fn section<'a, Message: 'static>() -> Section<'a, Message> {
10    with_column(ListColumn::default())
11}
12
13/// A section with a pre-defined list column.
14pub fn with_column<Message: 'static>(children: ListColumn<'_, Message>) -> Section<'_, Message> {
15    Section {
16        header: None,
17        children,
18    }
19}
20
21#[must_use]
22pub struct Section<'a, Message> {
23    header: Option<Element<'a, Message>>,
24    children: ListColumn<'a, Message>,
25}
26
27impl<'a, Message: 'static> Section<'a, Message> {
28    /// Define an optional title for the section.
29    pub fn title(mut self, title: impl Into<Cow<'a, str>>) -> Self {
30        self.header(text::heading(title.into()))
31    }
32
33    /// Define an optional custom header for the section.
34    pub fn header(mut self, header: impl Into<Element<'a, Message>>) -> Self {
35        self.header = Some(header.into());
36        self
37    }
38
39    /// Add a child element to the section's list column.
40    #[allow(clippy::should_implement_trait)]
41    pub fn add(mut self, item: impl Into<Element<'a, Message>>) -> Self {
42        self.children = self.children.add(item.into());
43        self
44    }
45
46    /// Add a child element to the section's list column, if `Some`.
47    pub fn add_maybe(self, item: Option<impl Into<Element<'a, Message>>>) -> Self {
48        if let Some(item) = item {
49            self.add(item)
50        } else {
51            self
52        }
53    }
54
55    /// Extends the [`Section`] with the given children.
56    pub fn extend(
57        self,
58        children: impl IntoIterator<Item = impl Into<Element<'a, Message>>>,
59    ) -> Self {
60        children.into_iter().fold(self, Self::add)
61    }
62}
63
64impl<'a, Message: 'static> From<Section<'a, Message>> for Element<'a, Message> {
65    fn from(data: Section<'a, Message>) -> Self {
66        column::with_capacity(2)
67            .spacing(8)
68            .push_maybe(data.header)
69            .push(data.children)
70            .into()
71    }
72}