feat(widget/settings): add flex items and flex controls
This commit is contained in:
parent
cb6bc86e1e
commit
095c2b5336
2 changed files with 40 additions and 6 deletions
|
|
@ -4,11 +4,13 @@
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
widget::{column, horizontal_space, row, text, Row},
|
widget::{column, container, flex_row, horizontal_space, row, text, FlexRow, Row},
|
||||||
Element,
|
Element,
|
||||||
};
|
};
|
||||||
use derive_setters::Setters;
|
use derive_setters::Setters;
|
||||||
|
use iced::alignment::Horizontal;
|
||||||
use iced_core::Length;
|
use iced_core::Length;
|
||||||
|
use taffy::AlignContent;
|
||||||
|
|
||||||
/// A settings item aligned in a row
|
/// A settings item aligned in a row
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|
@ -29,9 +31,33 @@ pub fn item<'a, Message: 'static>(
|
||||||
#[allow(clippy::module_name_repetitions)]
|
#[allow(clippy::module_name_repetitions)]
|
||||||
pub fn item_row<Message>(children: Vec<Element<Message>>) -> Row<Message> {
|
pub fn item_row<Message>(children: Vec<Element<Message>>) -> Row<Message> {
|
||||||
row::with_children(children)
|
row::with_children(children)
|
||||||
|
.spacing(12)
|
||||||
.align_items(iced::Alignment::Center)
|
.align_items(iced::Alignment::Center)
|
||||||
.padding([0, 18])
|
.padding([0, 18])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A settings item aligned in a flex row
|
||||||
|
#[allow(clippy::module_name_repetitions)]
|
||||||
|
pub fn flex_item<'a, Message: 'static>(
|
||||||
|
title: impl Into<Cow<'a, str>> + 'a,
|
||||||
|
widget: impl Into<Element<'a, Message>> + 'a,
|
||||||
|
) -> FlexRow<'a, Message> {
|
||||||
|
flex_item_row(vec![
|
||||||
|
text(title).width(Length::Fill).into(),
|
||||||
|
container(widget).into(),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A settings item aligned in a flex row
|
||||||
|
#[allow(clippy::module_name_repetitions)]
|
||||||
|
pub fn flex_item_row<Message>(children: Vec<Element<Message>>) -> FlexRow<Message> {
|
||||||
|
flex_row(children)
|
||||||
|
.padding([0, 18])
|
||||||
.spacing(12)
|
.spacing(12)
|
||||||
|
.min_item_width(200.0)
|
||||||
|
.justify_items(iced::Alignment::Center)
|
||||||
|
.justify_content(AlignContent::SpaceBetween)
|
||||||
|
.width(Length::Fill)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a builder for an item, beginning with the title.
|
/// Creates a builder for an item, beginning with the title.
|
||||||
|
|
@ -61,6 +87,15 @@ pub struct Item<'a, Message> {
|
||||||
impl<'a, Message: 'static> Item<'a, Message> {
|
impl<'a, Message: 'static> Item<'a, Message> {
|
||||||
/// Assigns a control to the item.
|
/// Assigns a control to the item.
|
||||||
pub fn control(self, widget: impl Into<Element<'a, Message>>) -> Row<'a, Message> {
|
pub fn control(self, widget: impl Into<Element<'a, Message>>) -> Row<'a, Message> {
|
||||||
|
item_row(self.control_(widget))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Assigns a control which flexes.
|
||||||
|
pub fn flex_control(self, widget: impl Into<Element<'a, Message>>) -> FlexRow<'a, Message> {
|
||||||
|
flex_item_row(self.control_(widget))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn control_(self, widget: impl Into<Element<'a, Message>>) -> Vec<Element<'a, Message>> {
|
||||||
let mut contents = Vec::with_capacity(4);
|
let mut contents = Vec::with_capacity(4);
|
||||||
|
|
||||||
if let Some(icon) = self.icon {
|
if let Some(icon) = self.icon {
|
||||||
|
|
@ -80,15 +115,14 @@ impl<'a, Message: 'static> Item<'a, Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
contents.push(widget.into());
|
contents.push(widget.into());
|
||||||
|
contents
|
||||||
item_row(contents)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggler(
|
pub fn toggler(
|
||||||
self,
|
self,
|
||||||
is_checked: bool,
|
is_checked: bool,
|
||||||
message: impl Fn(bool) -> Message + 'static,
|
message: impl Fn(bool) -> Message + 'static,
|
||||||
) -> Row<'a, Message> {
|
) -> FlexRow<'a, Message> {
|
||||||
self.control(crate::widget::toggler(None, is_checked, message))
|
self.flex_control(crate::widget::toggler(None, is_checked, message))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
pub mod item;
|
pub mod item;
|
||||||
mod section;
|
mod section;
|
||||||
|
|
||||||
pub use self::item::{item, item_row};
|
pub use self::item::{flex_item, flex_item_row, item, item_row};
|
||||||
pub use self::section::{view_section, Section};
|
pub use self::section::{view_section, Section};
|
||||||
|
|
||||||
use crate::widget::{column, Column};
|
use crate::widget::{column, Column};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue