Skip to content

Commit

Permalink
Correcting the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Ultraxime committed Dec 3, 2024
1 parent 794ce06 commit a1b8133
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/widget_id_return/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> {
Expand Down
18 changes: 7 additions & 11 deletions examples/widget_id_return/numberinput.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -14,17 +14,16 @@ pub struct NumInput<V, M> {

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NumInputMessage<V> {
Change(Result<V, String>),
Change(V),
}

impl<V> NumInputMessage<V>
where
V: Num + NumAssignOps + PartialOrd + Display + FromStr + Copy + Bounded,
{
pub fn get_data(&self) -> Result<V, String> {
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,
}
}
}
Expand All @@ -33,10 +32,9 @@ impl<V> NumInputMessage<V>
where
V: Eq + Copy,
{
pub fn get_enum(&self) -> Result<V, String> {
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,
}
}
}
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions examples/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ struct StrButton {
#[derive(Debug, Clone)]
enum Message {
ChangeAlign(WrapAlign),
ChangeSpacing(Result<f32, String>),
ChangeLineSpacing(Result<f32, String>),
ChangeMinimalLength(Result<f32, String>),
ChangeSpacing(f32),
ChangeLineSpacing(f32),
ChangeMinimalLength(f32),
}

impl RandStrings {
Expand All @@ -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;
}
_ => {}
}
}

Expand Down Expand Up @@ -148,23 +147,23 @@ impl RandStrings {
let spacing_input = Column::new()
.push(Text::new("spacing"))
.push(NumberInput::new(
self.spacing,
&self.spacing,
0.0..500.0,
Message::ChangeSpacing,
));
let line_spacing_input =
Column::new()
.push(Text::new("line spacing"))
.push(NumberInput::new(
self.line_spacing,
&self.line_spacing,
0.0..500.0,
Message::ChangeLineSpacing,
));
let line_minimal_length_input =
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,
));
Expand Down
2 changes: 1 addition & 1 deletion src/widget/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>) -> Self {
Expand Down

0 comments on commit a1b8133

Please sign in to comment.