feat(list_column): button list items
This commit is contained in:
parent
9cac422c24
commit
9b465a8b5c
6 changed files with 298 additions and 163 deletions
|
|
@ -5,7 +5,7 @@ use std::borrow::Cow;
|
|||
|
||||
use crate::{
|
||||
Element, Theme, theme,
|
||||
widget::{FlexRow, Row, column, container, flex_row, row, text},
|
||||
widget::{FlexRow, Row, column, container, flex_row, list, row, text},
|
||||
};
|
||||
use derive_setters::Setters;
|
||||
use iced_core::{Length, text::Wrapping};
|
||||
|
|
@ -114,39 +114,95 @@ impl<'a, Message: 'static> Item<'a, Message> {
|
|||
flex_item_row(self.control_(widget.into()))
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
fn control_(self, widget: Element<'a, Message>) -> Vec<Element<'a, Message>> {
|
||||
let mut contents = Vec::with_capacity(4);
|
||||
|
||||
if let Some(icon) = self.icon {
|
||||
contents.push(icon);
|
||||
}
|
||||
|
||||
fn label(self) -> Element<'a, Message> {
|
||||
if let Some(description) = self.description {
|
||||
let column = column::with_capacity(2)
|
||||
column::with_capacity(2)
|
||||
.spacing(2)
|
||||
.push(text::body(self.title).wrapping(Wrapping::Word))
|
||||
.push(text::caption(description).wrapping(Wrapping::Word))
|
||||
.width(Length::Fill);
|
||||
|
||||
contents.push(column.into());
|
||||
.width(Length::Fill)
|
||||
.into()
|
||||
} else {
|
||||
contents.push(text(self.title).width(Length::Fill).into());
|
||||
text(self.title).width(Length::Fill).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)]
|
||||
fn control_(mut self, widget: Element<'a, Message>) -> Vec<Element<'a, Message>> {
|
||||
let mut contents = Vec::with_capacity(3);
|
||||
if let Some(icon) = self.icon.take() {
|
||||
contents.push(icon);
|
||||
}
|
||||
contents.push(self.label());
|
||||
contents.push(widget);
|
||||
contents
|
||||
}
|
||||
|
||||
fn control_start(self, widget: impl Into<Element<'a, Message>>) -> Row<'a, Message, Theme> {
|
||||
item_row(vec![widget.into(), self.label()])
|
||||
}
|
||||
|
||||
pub fn toggler(
|
||||
self,
|
||||
is_checked: bool,
|
||||
message: impl Fn(bool) -> Message + 'static,
|
||||
) -> Row<'a, Message, Theme> {
|
||||
self.control(
|
||||
crate::widget::toggler(is_checked)
|
||||
.width(Length::Shrink)
|
||||
.on_toggle(message),
|
||||
) -> list::ListButton<'a, Message> {
|
||||
let on_press = message(!is_checked);
|
||||
list::button(
|
||||
self.control(
|
||||
crate::widget::toggler(is_checked)
|
||||
.width(Length::Shrink)
|
||||
.on_toggle(message),
|
||||
),
|
||||
)
|
||||
.on_press(on_press)
|
||||
}
|
||||
|
||||
pub fn toggler_maybe(
|
||||
self,
|
||||
is_checked: bool,
|
||||
message: Option<impl Fn(bool) -> Message + 'static>,
|
||||
) -> list::ListButton<'a, Message> {
|
||||
let on_press = message.as_ref().map(|f| f(!is_checked));
|
||||
list::button(
|
||||
self.control(
|
||||
crate::widget::toggler(is_checked)
|
||||
.width(Length::Shrink)
|
||||
.on_toggle_maybe(message),
|
||||
),
|
||||
)
|
||||
.on_press_maybe(on_press)
|
||||
}
|
||||
|
||||
pub fn checkbox(
|
||||
self,
|
||||
is_checked: bool,
|
||||
message: impl Fn(bool) -> Message + 'static,
|
||||
) -> list::ListButton<'a, Message> {
|
||||
let on_press = message(!is_checked);
|
||||
list::button(
|
||||
self.control_start(
|
||||
crate::widget::checkbox(is_checked)
|
||||
.width(Length::Shrink)
|
||||
.on_toggle(message),
|
||||
),
|
||||
)
|
||||
.on_press(on_press)
|
||||
}
|
||||
|
||||
pub fn checkbox_maybe(
|
||||
self,
|
||||
is_checked: bool,
|
||||
message: Option<impl Fn(bool) -> Message + 'static>,
|
||||
) -> list::ListButton<'a, Message> {
|
||||
let on_press = message.as_ref().map(|f| f(!is_checked));
|
||||
list::button(
|
||||
self.control_start(
|
||||
crate::widget::checkbox(is_checked)
|
||||
.width(Length::Shrink)
|
||||
.on_toggle_maybe(message),
|
||||
),
|
||||
)
|
||||
.on_press_maybe(on_press)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,16 +2,19 @@
|
|||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use crate::Element;
|
||||
use crate::widget::list_column::IntoListItem;
|
||||
use crate::widget::{ListColumn, column, text};
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// A section within a settings view column.
|
||||
pub fn section<'a, Message: 'static>() -> Section<'a, Message> {
|
||||
pub fn section<'a, Message: Clone + 'static>() -> Section<'a, Message> {
|
||||
with_column(ListColumn::default())
|
||||
}
|
||||
|
||||
/// A section with a pre-defined list column.
|
||||
pub fn with_column<Message: 'static>(children: ListColumn<'_, Message>) -> Section<'_, Message> {
|
||||
pub fn with_column<Message: Clone + 'static>(
|
||||
children: ListColumn<'_, Message>,
|
||||
) -> Section<'_, Message> {
|
||||
Section {
|
||||
header: None,
|
||||
children,
|
||||
|
|
@ -24,9 +27,9 @@ pub struct Section<'a, Message> {
|
|||
children: ListColumn<'a, Message>,
|
||||
}
|
||||
|
||||
impl<'a, Message: 'static> Section<'a, Message> {
|
||||
impl<'a, Message: Clone + 'static> Section<'a, Message> {
|
||||
/// Define an optional title for the section.
|
||||
pub fn title(mut self, title: impl Into<Cow<'a, str>>) -> Self {
|
||||
pub fn title(self, title: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.header(text::heading(title.into()))
|
||||
}
|
||||
|
||||
|
|
@ -38,8 +41,8 @@ impl<'a, Message: 'static> Section<'a, Message> {
|
|||
|
||||
/// Add a child element to the section's list column.
|
||||
#[allow(clippy::should_implement_trait)]
|
||||
pub fn add(mut self, item: impl Into<Element<'a, Message>>) -> Self {
|
||||
self.children = self.children.add(item.into());
|
||||
pub fn add(mut self, item: impl IntoListItem<'a, Message>) -> Self {
|
||||
self.children = self.children.add(item);
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +64,7 @@ impl<'a, Message: 'static> Section<'a, Message> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, Message: 'static> From<Section<'a, Message>> for Element<'a, Message> {
|
||||
impl<'a, Message: Clone + 'static> From<Section<'a, Message>> for Element<'a, Message> {
|
||||
fn from(data: Section<'a, Message>) -> Self {
|
||||
column::with_capacity(2)
|
||||
.spacing(8)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue