From a1b8133ff002b4de21938f6671e1f6185b26c4cd Mon Sep 17 00:00:00 2001 From: Ultraxime Date: Tue, 3 Dec 2024 01:43:29 +0100 Subject: [PATCH] Correcting the examples --- examples/widget_id_return/main.rs | 2 +- examples/widget_id_return/numberinput.rs | 18 +++++++----------- examples/wrap.rs | 19 +++++++++---------- src/widget/number_input.rs | 2 +- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/examples/widget_id_return/main.rs b/examples/widget_id_return/main.rs index 7e5b99cc..8d230c92 100644 --- a/examples/widget_id_return/main.rs +++ b/examples/widget_id_return/main.rs @@ -41,7 +41,7 @@ use numberinput::*; impl NumberInputDemo { fn update(&mut self, message: self::Message) { let Message::GenericF32Input((id, val)) = message; - self.value[id].value = val.get_data().unwrap_or_default(); + self.value[id].value = val.get_data(); } fn view(&self) -> Element { diff --git a/examples/widget_id_return/numberinput.rs b/examples/widget_id_return/numberinput.rs index 28891aed..35490282 100644 --- a/examples/widget_id_return/numberinput.rs +++ b/examples/widget_id_return/numberinput.rs @@ -1,4 +1,4 @@ -use iced::{Element, Length}; +use iced::Element; use iced_aw::style::number_input::Style; use iced_aw::NumberInput; use num_traits::{bounds::Bounded, Num, NumAssignOps}; @@ -14,17 +14,16 @@ pub struct NumInput { #[derive(Debug, Clone, PartialEq, Eq)] pub enum NumInputMessage { - Change(Result), + Change(V), } impl NumInputMessage where V: Num + NumAssignOps + PartialOrd + Display + FromStr + Copy + Bounded, { - pub fn get_data(&self) -> Result { + pub fn get_data(&self) -> V { match self { - NumInputMessage::Change(Ok(data)) => Ok(*data), - NumInputMessage::Change(Err(data)) => Err(data.clone()), + NumInputMessage::Change(data) => *data, } } } @@ -33,10 +32,9 @@ impl NumInputMessage where V: Eq + Copy, { - pub fn get_enum(&self) -> Result { + pub fn get_enum(&self) -> V { match self { - NumInputMessage::Change(Ok(data)) => Ok(*data), - NumInputMessage::Change(Err(data)) => Err(data.clone()), + NumInputMessage::Change(data) => *data, } } } @@ -70,9 +68,7 @@ where V: 'static, M: 'static + Clone, { - let mut input = NumberInput::new(self.value, min..max, NumInputMessage::Change) - .step(step) - .width(Length::Shrink); + let mut input = NumberInput::new(&self.value, min..max, NumInputMessage::Change).step(step); if let Some(style) = style { input = input.style(move |_theme, _status| style); diff --git a/examples/wrap.rs b/examples/wrap.rs index 357b6927..7a9933cc 100644 --- a/examples/wrap.rs +++ b/examples/wrap.rs @@ -89,9 +89,9 @@ struct StrButton { #[derive(Debug, Clone)] enum Message { ChangeAlign(WrapAlign), - ChangeSpacing(Result), - ChangeLineSpacing(Result), - ChangeMinimalLength(Result), + ChangeSpacing(f32), + ChangeLineSpacing(f32), + ChangeMinimalLength(f32), } impl RandStrings { @@ -100,16 +100,15 @@ impl RandStrings { Message::ChangeAlign(align) => { self.align = align.into(); } - Message::ChangeSpacing(Ok(num)) => { + Message::ChangeSpacing(num) => { self.spacing = num; } - Message::ChangeLineSpacing(Ok(num)) => { + Message::ChangeLineSpacing(num) => { self.line_spacing = num; } - Message::ChangeMinimalLength(Ok(num)) => { + Message::ChangeMinimalLength(num) => { self.line_minimal_length = num; } - _ => {} } } @@ -148,7 +147,7 @@ impl RandStrings { let spacing_input = Column::new() .push(Text::new("spacing")) .push(NumberInput::new( - self.spacing, + &self.spacing, 0.0..500.0, Message::ChangeSpacing, )); @@ -156,7 +155,7 @@ impl RandStrings { Column::new() .push(Text::new("line spacing")) .push(NumberInput::new( - self.line_spacing, + &self.line_spacing, 0.0..500.0, Message::ChangeLineSpacing, )); @@ -164,7 +163,7 @@ impl RandStrings { Column::new() .push(Text::new("line minimal length")) .push(NumberInput::new( - self.line_minimal_length, + &self.line_minimal_length, 0.0..999.9, Message::ChangeMinimalLength, )); diff --git a/src/widget/number_input.rs b/src/widget/number_input.rs index e396c35a..629e78cc 100644 --- a/src/widget/number_input.rs +++ b/src/widget/number_input.rs @@ -159,7 +159,7 @@ where /// ``` /// use iced_aw::widget::number_input; /// // Creates a range from -5 till 5. - /// let input: iced_aw::NumberInput<'_, _, _, iced::Theme, iced::Renderer> = number_input(4 /* my_value */, 0..=4, |_| () /* my_message */).bounds(-5..=5); + /// let input: iced_aw::NumberInput<'_, _, _, iced::Theme, iced::Renderer> = number_input(&4 /* my_value */, 0..=4, |_| () /* my_message */).bounds(-5..=5); /// ``` #[must_use] pub fn bounds(mut self, bounds: impl RangeBounds) -> Self {