feat: Add on_change input argument to SpinButton
This commit is contained in:
parent
3f88fb98e2
commit
cb77ca1255
3 changed files with 29 additions and 15 deletions
|
|
@ -300,7 +300,7 @@ impl Application for Window {
|
||||||
]))
|
]))
|
||||||
.add(settings::item(
|
.add(settings::item(
|
||||||
format!("Spin Button (Range {}:{})", self.spin_button.min, self.spin_button.max),
|
format!("Spin Button (Range {}:{})", self.spin_button.min, self.spin_button.max),
|
||||||
self.spin_button.view().map(Message::SpinButton)
|
self.spin_button.view(Message::SpinButton),
|
||||||
))
|
))
|
||||||
.into()
|
.into()
|
||||||
])
|
])
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,9 @@ use iced::{
|
||||||
};
|
};
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
pub struct SpinButton<T> {
|
pub struct SpinButton<T, Message> {
|
||||||
value: T,
|
value: T,
|
||||||
|
on_change: Box<dyn Fn(SpinMessage) -> Message + 'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A message emitted by the [`SpinButton`] widget.
|
/// A message emitted by the [`SpinButton`] widget.
|
||||||
|
|
@ -25,19 +26,26 @@ pub enum SpinMessage {
|
||||||
Decrement,
|
Decrement,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spin_button<T: 'static + Clone + Hash + ToString>(value: T) -> SpinButton<T> {
|
pub fn spin_button<T: 'static + Copy + Hash + ToString, Message: 'static>(
|
||||||
SpinButton::new(value)
|
model: &SpinButtonModel<T>,
|
||||||
|
on_change: impl Fn(SpinMessage) -> Message + 'static,
|
||||||
|
) -> SpinButton<T, Message> {
|
||||||
|
SpinButton::new(model.value, on_change)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static + Clone + Hash + ToString> SpinButton<T> {
|
impl<T: 'static + Copy + Hash + ToString, Message: 'static> SpinButton<T, Message> {
|
||||||
pub fn new(value: T) -> Self {
|
pub fn new(value: T, on_change: impl Fn(SpinMessage) -> Message + 'static) -> Self {
|
||||||
Self { value }
|
Self {
|
||||||
|
on_change: Box::from(on_change),
|
||||||
|
value,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_element(self) -> Element<'static, SpinMessage> {
|
pub fn into_element(self) -> Element<'static, Message> {
|
||||||
let Self { value } = self;
|
let Self { on_change, value } = self;
|
||||||
|
|
||||||
Element::from(iced_lazy::lazy(
|
Element::from(iced_lazy::lazy(
|
||||||
value.clone(),
|
value,
|
||||||
move || -> Element<'static, SpinMessage> {
|
move || -> Element<'static, SpinMessage> {
|
||||||
container(
|
container(
|
||||||
row![
|
row![
|
||||||
|
|
@ -53,7 +61,7 @@ impl<T: 'static + Clone + Hash + ToString> SpinButton<T> {
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.style(theme::Button::Text)
|
.style(theme::Button::Text)
|
||||||
.on_press(SpinMessage::Decrement),
|
.on_press(SpinMessage::Decrement),
|
||||||
text(value.clone())
|
text(value)
|
||||||
.vertical_alignment(Vertical::Center)
|
.vertical_alignment(Vertical::Center)
|
||||||
.apply(container)
|
.apply(container)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
|
|
@ -85,11 +93,14 @@ impl<T: 'static + Clone + Hash + ToString> SpinButton<T> {
|
||||||
.into()
|
.into()
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
.map(on_change)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: 'static + Clone + Hash + ToString> From<SpinButton<T>> for Element<'a, SpinMessage> {
|
impl<'a, T: 'static + Copy + Hash + ToString, Message: 'static> From<SpinButton<T, Message>>
|
||||||
fn from(spin_button: SpinButton<T>) -> Self {
|
for Element<'a, Message>
|
||||||
|
{
|
||||||
|
fn from(spin_button: SpinButton<T, Message>) -> Self {
|
||||||
spin_button.into_element()
|
spin_button.into_element()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,11 @@ impl<T: 'static> SpinButtonModel<T>
|
||||||
where
|
where
|
||||||
T: Copy + Hash + ToString + Sub<Output = T> + Add<Output = T> + Ord,
|
T: Copy + Hash + ToString + Sub<Output = T> + Add<Output = T> + Ord,
|
||||||
{
|
{
|
||||||
pub fn view(&self) -> Element<'static, SpinMessage> {
|
pub fn view<Message: 'static>(
|
||||||
SpinButton::new(self.value).into_element()
|
&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: SpinMessage) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue