feat!(spin-button): pass label rather than model to widget

This commit is contained in:
Michael Aaron Murphy 2023-01-19 19:44:40 +01:00 committed by Michael Murphy
parent 4269fad768
commit ccc9b60955
2 changed files with 96 additions and 114 deletions

View file

@ -2,7 +2,9 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
mod model; mod model;
pub use self::model::SpinButtonModel; use std::borrow::Cow;
pub use self::model::{Message, Model};
use crate::widget::icon; use crate::widget::icon;
use crate::{theme, Element}; use crate::{theme, Element};
@ -12,95 +14,83 @@ use iced::{
widget::{button, container, row, text}, widget::{button, container, row, text},
Alignment, Background, Length, Alignment, Background, Length,
}; };
use std::hash::Hash;
pub struct SpinButton<T, Message> { pub struct SpinButton<'a, Message> {
value: T, label: Cow<'a, str>,
on_change: Box<dyn Fn(SpinMessage) -> Message + 'static>, on_change: Box<dyn Fn(model::Message) -> Message + 'static>,
} }
/// A message emitted by the [`SpinButton`] widget. pub fn spin_button<'a, Message: 'static>(
#[derive(Clone, Copy, Debug, Hash)] label: impl Into<Cow<'a, str>>,
pub enum SpinMessage { on_change: impl Fn(model::Message) -> Message + 'static,
Increment, ) -> SpinButton<'a, Message> {
Decrement, SpinButton::new(label, on_change)
} }
pub fn spin_button<T: 'static + Copy + Hash + ToString, Message: 'static>( impl<'a, Message: 'static> SpinButton<'a, Message> {
model: &SpinButtonModel<T>, pub fn new(
on_change: impl Fn(SpinMessage) -> Message + 'static, label: impl Into<Cow<'a, str>>,
) -> SpinButton<T, Message> { on_change: impl Fn(model::Message) -> Message + 'static,
SpinButton::new(model.value, on_change) ) -> Self {
}
impl<T: 'static + Copy + Hash + ToString, Message: 'static> SpinButton<T, Message> {
pub fn new(value: T, on_change: impl Fn(SpinMessage) -> Message + 'static) -> Self {
Self { Self {
on_change: Box::from(on_change), on_change: Box::from(on_change),
value, label: label.into(),
} }
} }
pub fn into_element(self) -> Element<'static, Message> { #[must_use]
let Self { on_change, value } = self; pub fn into_element(self) -> Element<'a, Message> {
let Self { on_change, label } = self;
Element::from(iced_lazy::lazy( container(
(value, crate::settings::default_icon_theme()), row![
move || -> Element<'static, SpinMessage> { icon("list-remove-symbolic", 24)
container( .style(theme::Svg::Symbolic)
row![ .apply(container)
icon("list-remove-symbolic", 24)
.style(theme::Svg::Symbolic)
.apply(container)
.width(Length::Fill)
.height(Length::Fill)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.apply(button)
.width(Length::Fill)
.height(Length::Fill)
.style(theme::Button::Text)
.on_press(SpinMessage::Decrement),
text(value)
.vertical_alignment(Vertical::Center)
.apply(container)
.width(Length::Fill)
.height(Length::Fill)
.align_x(Horizontal::Center)
.align_y(Vertical::Center),
icon("list-add-symbolic", 24)
.style(theme::Svg::Symbolic)
.apply(container)
.width(Length::Fill)
.height(Length::Fill)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.apply(button)
.width(Length::Fill)
.height(Length::Fill)
.style(theme::Button::Text)
.on_press(SpinMessage::Increment),
]
.width(Length::Fill) .width(Length::Fill)
.height(Length::Units(32)) .height(Length::Fill)
.align_items(Alignment::Center), .align_x(Horizontal::Center)
) .align_y(Vertical::Center)
.padding([4, 4]) .apply(button)
.align_y(Vertical::Center) .width(Length::Fill)
.width(Length::Units(95)) .height(Length::Fill)
.height(Length::Units(32)) .style(theme::Button::Text)
.style(theme::Container::Custom(container_style)) .on_press(model::Message::Decrement),
.into() text(label)
}, .vertical_alignment(Vertical::Center)
)) .apply(container)
.width(Length::Fill)
.height(Length::Fill)
.align_x(Horizontal::Center)
.align_y(Vertical::Center),
icon("list-add-symbolic", 24)
.style(theme::Svg::Symbolic)
.apply(container)
.width(Length::Fill)
.height(Length::Fill)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.apply(button)
.width(Length::Fill)
.height(Length::Fill)
.style(theme::Button::Text)
.on_press(model::Message::Increment),
]
.width(Length::Fill)
.height(Length::Units(32))
.align_items(Alignment::Center),
)
.padding([4, 4])
.align_y(Vertical::Center)
.width(Length::Units(95))
.height(Length::Units(32))
.style(theme::Container::Custom(container_style))
.apply(Element::from)
.map(on_change) .map(on_change)
} }
} }
impl<'a, T: 'static + Copy + Hash + ToString, Message: 'static> From<SpinButton<T, Message>> impl<'a, Message: 'static> From<SpinButton<'a, Message>> for Element<'a, Message> {
for Element<'a, Message> fn from(spin_button: SpinButton<'a, Message>) -> Self {
{
fn from(spin_button: SpinButton<T, Message>) -> Self {
spin_button.into_element() spin_button.into_element()
} }
} }

View file

@ -1,48 +1,51 @@
// Copyright 2022 System76 <info@system76.com> // Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use super::{SpinButton, SpinMessage};
use crate::Element;
use derive_setters::Setters; use derive_setters::Setters;
use fraction::{Bounded, Decimal};
use std::hash::Hash; use std::hash::Hash;
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
/// A message emitted by the [`SpinButton`] widget.
#[derive(Clone, Copy, Debug, Hash)]
pub enum Message {
Increment,
Decrement,
}
#[derive(Setters)] #[derive(Setters)]
pub struct SpinButtonModel<T> { pub struct Model<T> {
/// The current value of the spin button. /// The current value of the spin button.
#[setters(into)]
pub value: T, pub value: T,
/// The amount to increment the value. /// The amount to increment the value.
#[setters(into)]
pub step: T, pub step: T,
/// The minimum value permitted. /// The minimum value permitted.
#[setters(into)]
pub min: T, pub min: T,
/// The maximum value permitted. /// The maximum value permitted.
#[setters(into)]
pub max: T, pub max: T,
} }
impl<T: 'static> SpinButtonModel<T> impl<T: 'static> Model<T>
where where
T: Copy + Hash + ToString + Sub<Output = T> + Add<Output = T> + Ord, T: Copy + Hash + Sub<Output = T> + Add<Output = T> + Ord,
{ {
pub fn view<Message: 'static>( pub fn update(&mut self, message: Message) {
&self,
on_change: impl Fn(SpinMessage) -> Message + 'static,
) -> Element<'static, Message> {
SpinButton::new(self.value, on_change).into_element()
}
pub fn update(&mut self, message: SpinMessage) {
self.value = match message { self.value = match message {
SpinMessage::Increment => { Message::Increment => {
std::cmp::min(std::cmp::max(self.value + self.step, self.min), self.max) std::cmp::min(std::cmp::max(self.value + self.step, self.min), self.max)
} }
SpinMessage::Decrement => { Message::Decrement => {
std::cmp::max(std::cmp::min(self.value - self.step, self.max), self.min) std::cmp::max(std::cmp::min(self.value - self.step, self.max), self.min)
} }
} }
} }
} }
impl Default for SpinButtonModel<i8> { impl Default for Model<i8> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
@ -53,7 +56,7 @@ impl Default for SpinButtonModel<i8> {
} }
} }
impl Default for SpinButtonModel<i16> { impl Default for Model<i16> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
@ -64,7 +67,7 @@ impl Default for SpinButtonModel<i16> {
} }
} }
impl Default for SpinButtonModel<i32> { impl Default for Model<i32> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
@ -75,7 +78,7 @@ impl Default for SpinButtonModel<i32> {
} }
} }
impl Default for SpinButtonModel<isize> { impl Default for Model<isize> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
@ -86,7 +89,7 @@ impl Default for SpinButtonModel<isize> {
} }
} }
impl Default for SpinButtonModel<u8> { impl Default for Model<u8> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
@ -97,7 +100,7 @@ impl Default for SpinButtonModel<u8> {
} }
} }
impl Default for SpinButtonModel<u16> { impl Default for Model<u16> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
@ -108,7 +111,7 @@ impl Default for SpinButtonModel<u16> {
} }
} }
impl Default for SpinButtonModel<u32> { impl Default for Model<u32> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
@ -119,7 +122,7 @@ impl Default for SpinButtonModel<u32> {
} }
} }
impl Default for SpinButtonModel<usize> { impl Default for Model<usize> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
@ -130,24 +133,13 @@ impl Default for SpinButtonModel<usize> {
} }
} }
impl Default for SpinButtonModel<f32> { impl Default for Model<Decimal> {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0.0, value: Decimal::from(0.0),
step: 1.0, step: Decimal::from(0.0),
min: f32::MIN, min: Decimal::min_positive_value(),
max: f32::MAX, max: Decimal::max_value(),
}
}
}
impl Default for SpinButtonModel<f64> {
fn default() -> Self {
Self {
value: 0.0,
step: 1.0,
min: f64::MIN,
max: f64::MAX,
} }
} }
} }