libcosmic/src/widget/list/column.rs
Ian Douglas Scott ece17c8d62 theme: Use boxed functions instead fn pointers
This is more general, and necessary if the custom theming is dynamically
generated.

Iced's builtin theme also uses `Box`ed `Fn`, or `Rc` where clone is
required, so this seems reasonable.

Only `Text` is left using `fn`, since it needs to be `Copy`. Hopefully
that can be changed in Iced at some point.

`::custom` methods are added to make these variants a little more
convenient to construct. This replaces a couple `From` implementations,
which are potentially problematic with a generic.
2023-04-27 15:29:39 -07:00

70 lines
1.9 KiB
Rust

// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::{theme, widget::divider, Element};
use apply::Apply;
use iced::{Background, Color};
#[must_use]
pub fn list_column<'a, Message: 'static>() -> ListColumn<'a, Message> {
ListColumn::default()
}
pub struct ListColumn<'a, Message> {
children: Vec<Element<'a, Message>>,
}
impl<'a, Message: 'static> Default for ListColumn<'a, Message> {
fn default() -> Self {
Self {
children: Vec::with_capacity(4),
}
}
}
impl<'a, Message: 'static> ListColumn<'a, Message> {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
#[allow(clippy::should_implement_trait)]
pub fn add(mut self, item: impl Into<Element<'a, Message>>) -> Self {
if !self.children.is_empty() {
self.children.push(divider::horizontal::light().into());
}
self.children.push(item.into());
self
}
#[must_use]
pub fn into_element(self) -> Element<'a, Message> {
iced::widget::column(self.children)
.spacing(12)
.apply(iced::widget::container)
.padding([16, 6])
.style(theme::Container::custom(style))
.into()
}
}
impl<'a, Message: 'static> From<ListColumn<'a, Message>> for Element<'a, Message> {
fn from(column: ListColumn<'a, Message>) -> Self {
column.into_element()
}
}
#[must_use]
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn style(theme: &crate::Theme) -> iced::widget::container::Appearance {
let container = &theme.current_container().component;
iced::widget::container::Appearance {
text_color: Some(container.on.into()),
background: Some(Background::Color(container.base.into())),
border_radius: 8.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
}
}