2022-12-06 16:12:59 +01:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2022-12-07 03:03:41 +01:00
|
|
|
use crate::Element;
|
2025-03-21 13:33:07 +01:00
|
|
|
use crate::widget::{ListColumn, column, text};
|
2022-12-07 03:03:41 +01:00
|
|
|
use std::borrow::Cow;
|
2022-12-06 16:12:59 +01:00
|
|
|
|
|
|
|
|
/// A section within a settings view column.
|
2024-09-19 14:29:52 +02:00
|
|
|
#[deprecated(note = "use `settings::section().title()` instead")]
|
2022-12-07 03:03:41 +01:00
|
|
|
pub fn view_section<'a, Message: 'static>(title: impl Into<Cow<'a, str>>) -> Section<'a, Message> {
|
2025-08-21 10:51:36 -06:00
|
|
|
section().title(title)
|
2022-12-06 16:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
2024-09-19 14:29:52 +02:00
|
|
|
/// A section within a settings view column.
|
|
|
|
|
pub fn section<'a, Message: 'static>() -> Section<'a, Message> {
|
|
|
|
|
with_column(ListColumn::default())
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 17:22:45 +01:00
|
|
|
/// A section with a pre-defined list column.
|
2025-03-14 11:56:21 -04:00
|
|
|
pub fn with_column<Message: 'static>(children: ListColumn<'_, Message>) -> Section<'_, Message> {
|
2024-09-04 17:22:45 +01:00
|
|
|
Section {
|
2025-08-21 10:51:36 -06:00
|
|
|
header: None,
|
2024-09-04 17:22:45 +01:00
|
|
|
children,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2022-12-06 16:12:59 +01:00
|
|
|
pub struct Section<'a, Message> {
|
2025-08-21 10:51:36 -06:00
|
|
|
header: Option<Element<'a, Message>>,
|
2022-12-07 03:03:41 +01:00
|
|
|
children: ListColumn<'a, Message>,
|
2022-12-06 16:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message: 'static> Section<'a, Message> {
|
2024-10-19 01:06:10 +02:00
|
|
|
/// Define an optional title for the section.
|
|
|
|
|
pub fn title(mut self, title: impl Into<Cow<'a, str>>) -> Self {
|
2025-08-21 10:51:36 -06:00
|
|
|
self.header(text::heading(title.into()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Define an optional custom header for the section.
|
|
|
|
|
pub fn header(mut self, header: impl Into<Element<'a, Message>>) -> Self {
|
|
|
|
|
self.header = Some(header.into());
|
2024-10-19 01:06:10 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 17:22:45 +01:00
|
|
|
/// Add a child element to the section's list column.
|
2022-12-23 15:10:13 +01:00
|
|
|
#[allow(clippy::should_implement_trait)]
|
2022-12-06 16:12:59 +01:00
|
|
|
pub fn add(mut self, item: impl Into<Element<'a, Message>>) -> Self {
|
|
|
|
|
self.children = self.children.add(item.into());
|
|
|
|
|
self
|
|
|
|
|
}
|
2024-09-04 17:22:45 +01:00
|
|
|
|
2024-10-19 01:06:10 +02:00
|
|
|
/// Add a child element to the section's list column, if `Some`.
|
|
|
|
|
pub fn add_maybe(self, item: Option<impl Into<Element<'a, Message>>>) -> Self {
|
|
|
|
|
if let Some(item) = item {
|
|
|
|
|
self.add(item)
|
|
|
|
|
} else {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Extends the [`Section`] with the given children.
|
|
|
|
|
pub fn extend(
|
|
|
|
|
self,
|
|
|
|
|
children: impl IntoIterator<Item = impl Into<Element<'a, Message>>>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
children.into_iter().fold(self, Self::add)
|
2024-09-04 17:22:45 +01:00
|
|
|
}
|
2022-12-06 16:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message: 'static> From<Section<'a, Message>> for Element<'a, Message> {
|
|
|
|
|
fn from(data: Section<'a, Message>) -> Self {
|
2023-09-01 07:29:19 +02:00
|
|
|
column::with_capacity(2)
|
2022-12-06 16:12:59 +01:00
|
|
|
.spacing(8)
|
2025-08-21 10:51:36 -06:00
|
|
|
.push_maybe(data.header)
|
2023-09-01 07:29:19 +02:00
|
|
|
.push(data.children)
|
2022-12-06 16:12:59 +01:00
|
|
|
.into()
|
|
|
|
|
}
|
2022-12-07 03:03:41 +01:00
|
|
|
}
|