libcosmic/src/widget/settings/section.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::ext::CollectionWidget;
use crate::widget::{column, text, ListColumn};
use crate::Element;
use std::borrow::Cow;
/// A section within a settings view column.
#[must_use]
pub fn view_section<'a, Message: 'static>(title: impl Into<Cow<'a, str>>) -> Section<'a, Message> {
Section {
title: title.into(),
children: ListColumn::default(),
}
}
pub struct Section<'a, Message> {
title: Cow<'a, str>,
children: ListColumn<'a, Message>,
}
impl<'a, Message: 'static> Section<'a, Message> {
#[must_use]
2022-12-23 15:10:13 +01:00
#[allow(clippy::should_implement_trait)]
pub fn add(mut self, item: impl Into<Element<'a, Message>>) -> Self {
self.children = self.children.add(item.into());
self
}
}
impl<'a, Message: 'static> From<Section<'a, Message>> for Element<'a, Message> {
fn from(data: Section<'a, Message>) -> Self {
column::with_capacity(2)
.spacing(8)
.push_maybe(if data.title.is_empty() {
None
} else {
Some(text::heading(data.title))
})
.push(data.children)
.into()
}
}