feat(settings): settings item builder API
This commit is contained in:
parent
4b7509e1c3
commit
18a21937b4
2 changed files with 61 additions and 3 deletions
|
|
@ -4,9 +4,10 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use crate::{widget::text, Element, Renderer};
|
||||
use iced::widget::{horizontal_space, row, Row};
|
||||
use derive_setters::Setters;
|
||||
use iced::widget::{column, horizontal_space, row, Row};
|
||||
|
||||
/// A setting within a settings view section.
|
||||
/// A settings item aligned in a row
|
||||
#[must_use]
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
pub fn item<'a, Message: 'static>(
|
||||
|
|
@ -29,3 +30,60 @@ pub fn item_row<Message>(children: Vec<Element<Message>>) -> Row<Message, Render
|
|||
.padding([0, 18])
|
||||
.spacing(12)
|
||||
}
|
||||
|
||||
/// Creates a builder for an item, beginning with the title.
|
||||
pub fn builder<'a, Message: 'static>(title: impl Into<Cow<'a, str>>) -> Item<'a, Message> {
|
||||
Item {
|
||||
title: title.into(),
|
||||
description: None,
|
||||
icon: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A builder for a settings item.
|
||||
#[derive(Setters)]
|
||||
pub struct Item<'a, Message> {
|
||||
/// Describes the item being controlled.
|
||||
title: Cow<'a, str>,
|
||||
|
||||
/// A description to display beneath the title.
|
||||
#[setters(strip_option, into)]
|
||||
description: Option<Cow<'a, str>>,
|
||||
|
||||
/// A custom icon to display before the text.
|
||||
#[setters(strip_option, into)]
|
||||
icon: Option<Element<'a, Message>>,
|
||||
}
|
||||
|
||||
impl<'a, Message: 'static> Item<'a, Message> {
|
||||
/// Assigns a control to the item.
|
||||
pub fn control(self, widget: impl Into<Element<'a, Message>>) -> Row<'a, Message, Renderer> {
|
||||
let mut contents = Vec::with_capacity(4);
|
||||
|
||||
if let Some(icon) = self.icon {
|
||||
contents.push(icon);
|
||||
}
|
||||
|
||||
if let Some(description) = self.description {
|
||||
let title = text(self.title).size(18);
|
||||
let desc = text(description).size(12);
|
||||
|
||||
contents.push(column!(title, desc).spacing(2).into());
|
||||
} else {
|
||||
contents.push(text(self.title).into());
|
||||
}
|
||||
|
||||
contents.push(horizontal_space(iced::Length::Fill).into());
|
||||
contents.push(widget.into());
|
||||
|
||||
item_row(contents)
|
||||
}
|
||||
|
||||
pub fn toggler(
|
||||
self,
|
||||
is_checked: bool,
|
||||
message: impl Fn(bool) -> Message + 'static,
|
||||
) -> Row<'a, Message, Renderer> {
|
||||
self.control(crate::widget::toggler(None, is_checked, message))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
// Copyright 2022 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
mod item;
|
||||
pub mod item;
|
||||
mod section;
|
||||
|
||||
pub use self::item::{item, item_row};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue