2022-12-06 16:12:59 +01:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-01-23 22:48:06 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
use crate::{
|
|
|
|
|
widget::{column, horizontal_space, row, text, Row},
|
2023-09-13 15:40:07 +02:00
|
|
|
Element,
|
2023-09-01 07:29:19 +02:00
|
|
|
};
|
2023-01-25 04:13:29 +01:00
|
|
|
use derive_setters::Setters;
|
2023-10-16 16:19:04 -04:00
|
|
|
use iced_core::Length;
|
|
|
|
|
use iced_widget::container;
|
2022-12-06 16:12:59 +01:00
|
|
|
|
2023-01-25 04:13:29 +01:00
|
|
|
/// A settings item aligned in a row
|
2022-12-06 16:12:59 +01:00
|
|
|
#[must_use]
|
|
|
|
|
#[allow(clippy::module_name_repetitions)]
|
2022-12-07 03:03:41 +01:00
|
|
|
pub fn item<'a, Message: 'static>(
|
2023-09-01 07:29:19 +02:00
|
|
|
title: impl Into<Cow<'a, str>> + 'a,
|
|
|
|
|
widget: impl Into<Element<'a, Message>> + 'a,
|
2023-09-13 15:40:07 +02:00
|
|
|
) -> Row<'a, Message> {
|
2022-12-06 16:12:59 +01:00
|
|
|
item_row(vec![
|
2023-05-30 23:46:49 +02:00
|
|
|
text(title).into(),
|
2023-01-23 22:48:06 +01:00
|
|
|
horizontal_space(iced::Length::Fill).into(),
|
2022-12-07 03:03:41 +01:00
|
|
|
widget.into(),
|
2022-12-06 16:12:59 +01:00
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A settings item aligned in a row
|
|
|
|
|
#[must_use]
|
|
|
|
|
#[allow(clippy::module_name_repetitions)]
|
2023-09-13 15:40:07 +02:00
|
|
|
pub fn item_row<Message>(children: Vec<Element<Message>>) -> Row<Message> {
|
2023-09-01 07:29:19 +02:00
|
|
|
row::with_children(children)
|
2022-12-06 16:12:59 +01:00
|
|
|
.align_items(iced::Alignment::Center)
|
2022-12-21 12:18:54 -07:00
|
|
|
.padding([0, 18])
|
2022-12-06 16:12:59 +01:00
|
|
|
.spacing(12)
|
|
|
|
|
}
|
2023-01-25 04:13:29 +01:00
|
|
|
|
|
|
|
|
/// 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.
|
2023-09-13 15:40:07 +02:00
|
|
|
pub fn control(self, widget: impl Into<Element<'a, Message>>) -> Row<'a, Message> {
|
2023-01-25 04:13:29 +01:00
|
|
|
let mut contents = Vec::with_capacity(4);
|
|
|
|
|
|
|
|
|
|
if let Some(icon) = self.icon {
|
|
|
|
|
contents.push(icon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(description) = self.description {
|
2023-09-01 07:29:19 +02:00
|
|
|
let column = column::with_capacity(2)
|
|
|
|
|
.spacing(2)
|
|
|
|
|
.push(text(self.title))
|
2023-10-16 16:19:04 -04:00
|
|
|
.push(text(description).size(10))
|
|
|
|
|
.width(Length::FillPortion(12));
|
2023-01-25 04:13:29 +01:00
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
contents.push(column.into());
|
2023-01-25 04:13:29 +01:00
|
|
|
} else {
|
2023-05-30 23:46:49 +02:00
|
|
|
contents.push(text(self.title).into());
|
2023-01-25 04:13:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-16 16:19:04 -04:00
|
|
|
contents.push(
|
|
|
|
|
container(widget.into())
|
|
|
|
|
.width(Length::FillPortion(4))
|
|
|
|
|
.align_x(iced_core::alignment::Horizontal::Right)
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
2023-01-25 04:13:29 +01:00
|
|
|
|
|
|
|
|
item_row(contents)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn toggler(
|
|
|
|
|
self,
|
|
|
|
|
is_checked: bool,
|
|
|
|
|
message: impl Fn(bool) -> Message + 'static,
|
2023-09-13 15:40:07 +02:00
|
|
|
) -> Row<'a, Message> {
|
2023-01-25 04:13:29 +01:00
|
|
|
self.control(crate::widget::toggler(None, is_checked, message))
|
|
|
|
|
}
|
|
|
|
|
}
|