feat!(spin-button): pass label rather than model to widget
This commit is contained in:
parent
4269fad768
commit
ccc9b60955
2 changed files with 96 additions and 114 deletions
|
|
@ -2,7 +2,9 @@
|
|||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
mod model;
|
||||
pub use self::model::SpinButtonModel;
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub use self::model::{Message, Model};
|
||||
|
||||
use crate::widget::icon;
|
||||
use crate::{theme, Element};
|
||||
|
|
@ -12,95 +14,83 @@ use iced::{
|
|||
widget::{button, container, row, text},
|
||||
Alignment, Background, Length,
|
||||
};
|
||||
use std::hash::Hash;
|
||||
|
||||
pub struct SpinButton<T, Message> {
|
||||
value: T,
|
||||
on_change: Box<dyn Fn(SpinMessage) -> Message + 'static>,
|
||||
pub struct SpinButton<'a, Message> {
|
||||
label: Cow<'a, str>,
|
||||
on_change: Box<dyn Fn(model::Message) -> Message + 'static>,
|
||||
}
|
||||
|
||||
/// A message emitted by the [`SpinButton`] widget.
|
||||
#[derive(Clone, Copy, Debug, Hash)]
|
||||
pub enum SpinMessage {
|
||||
Increment,
|
||||
Decrement,
|
||||
pub fn spin_button<'a, Message: 'static>(
|
||||
label: impl Into<Cow<'a, str>>,
|
||||
on_change: impl Fn(model::Message) -> Message + 'static,
|
||||
) -> SpinButton<'a, Message> {
|
||||
SpinButton::new(label, on_change)
|
||||
}
|
||||
|
||||
pub fn spin_button<T: 'static + Copy + Hash + ToString, Message: 'static>(
|
||||
model: &SpinButtonModel<T>,
|
||||
on_change: impl Fn(SpinMessage) -> Message + 'static,
|
||||
) -> SpinButton<T, Message> {
|
||||
SpinButton::new(model.value, on_change)
|
||||
}
|
||||
|
||||
impl<T: 'static + Copy + Hash + ToString, Message: 'static> SpinButton<T, Message> {
|
||||
pub fn new(value: T, on_change: impl Fn(SpinMessage) -> Message + 'static) -> Self {
|
||||
impl<'a, Message: 'static> SpinButton<'a, Message> {
|
||||
pub fn new(
|
||||
label: impl Into<Cow<'a, str>>,
|
||||
on_change: impl Fn(model::Message) -> Message + 'static,
|
||||
) -> Self {
|
||||
Self {
|
||||
on_change: Box::from(on_change),
|
||||
value,
|
||||
label: label.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_element(self) -> Element<'static, Message> {
|
||||
let Self { on_change, value } = self;
|
||||
|
||||
Element::from(iced_lazy::lazy(
|
||||
(value, crate::settings::default_icon_theme()),
|
||||
move || -> Element<'static, SpinMessage> {
|
||||
container(
|
||||
row![
|
||||
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),
|
||||
]
|
||||
#[must_use]
|
||||
pub fn into_element(self) -> Element<'a, Message> {
|
||||
let Self { on_change, label } = self;
|
||||
container(
|
||||
row![
|
||||
icon("list-remove-symbolic", 24)
|
||||
.style(theme::Svg::Symbolic)
|
||||
.apply(container)
|
||||
.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))
|
||||
.into()
|
||||
},
|
||||
))
|
||||
.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::Decrement),
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'static + Copy + Hash + ToString, Message: 'static> From<SpinButton<T, Message>>
|
||||
for Element<'a, Message>
|
||||
{
|
||||
fn from(spin_button: SpinButton<T, Message>) -> Self {
|
||||
impl<'a, Message: 'static> From<SpinButton<'a, Message>> for Element<'a, Message> {
|
||||
fn from(spin_button: SpinButton<'a, Message>) -> Self {
|
||||
spin_button.into_element()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,48 +1,51 @@
|
|||
// Copyright 2022 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use super::{SpinButton, SpinMessage};
|
||||
use crate::Element;
|
||||
use derive_setters::Setters;
|
||||
use fraction::{Bounded, Decimal};
|
||||
use std::hash::Hash;
|
||||
use std::ops::{Add, Sub};
|
||||
|
||||
/// A message emitted by the [`SpinButton`] widget.
|
||||
#[derive(Clone, Copy, Debug, Hash)]
|
||||
pub enum Message {
|
||||
Increment,
|
||||
Decrement,
|
||||
}
|
||||
|
||||
#[derive(Setters)]
|
||||
pub struct SpinButtonModel<T> {
|
||||
pub struct Model<T> {
|
||||
/// The current value of the spin button.
|
||||
#[setters(into)]
|
||||
pub value: T,
|
||||
/// The amount to increment the value.
|
||||
#[setters(into)]
|
||||
pub step: T,
|
||||
/// The minimum value permitted.
|
||||
#[setters(into)]
|
||||
pub min: T,
|
||||
/// The maximum value permitted.
|
||||
#[setters(into)]
|
||||
pub max: T,
|
||||
}
|
||||
|
||||
impl<T: 'static> SpinButtonModel<T>
|
||||
impl<T: 'static> Model<T>
|
||||
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>(
|
||||
&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) {
|
||||
pub fn update(&mut self, message: Message) {
|
||||
self.value = match message {
|
||||
SpinMessage::Increment => {
|
||||
Message::Increment => {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<i8> {
|
||||
impl Default for Model<i8> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
|
|
@ -53,7 +56,7 @@ impl Default for SpinButtonModel<i8> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<i16> {
|
||||
impl Default for Model<i16> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
|
|
@ -64,7 +67,7 @@ impl Default for SpinButtonModel<i16> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<i32> {
|
||||
impl Default for Model<i32> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
|
|
@ -75,7 +78,7 @@ impl Default for SpinButtonModel<i32> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<isize> {
|
||||
impl Default for Model<isize> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
|
|
@ -86,7 +89,7 @@ impl Default for SpinButtonModel<isize> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<u8> {
|
||||
impl Default for Model<u8> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
|
|
@ -97,7 +100,7 @@ impl Default for SpinButtonModel<u8> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<u16> {
|
||||
impl Default for Model<u16> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
|
|
@ -108,7 +111,7 @@ impl Default for SpinButtonModel<u16> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<u32> {
|
||||
impl Default for Model<u32> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
|
|
@ -119,7 +122,7 @@ impl Default for SpinButtonModel<u32> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<usize> {
|
||||
impl Default for Model<usize> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0,
|
||||
|
|
@ -130,24 +133,13 @@ impl Default for SpinButtonModel<usize> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<f32> {
|
||||
impl Default for Model<Decimal> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0.0,
|
||||
step: 1.0,
|
||||
min: f32::MIN,
|
||||
max: f32::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SpinButtonModel<f64> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: 0.0,
|
||||
step: 1.0,
|
||||
min: f64::MIN,
|
||||
max: f64::MAX,
|
||||
value: Decimal::from(0.0),
|
||||
step: Decimal::from(0.0),
|
||||
min: Decimal::min_positive_value(),
|
||||
max: Decimal::max_value(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue