Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed number input's bounds to an inclusive range #242

Merged
merged 7 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/WidgetIDReturn/src/numberinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ where
V: 'static,
M: 'static,
{
let mut input = NumberInput::new(self.value, max, NumInputMessage::Change)
let mut input = NumberInput::new(self.value, NumInputMessage::Change)
.step(step)
.min(min)
.bounds(min..=max)
.width(Length::Shrink);

if let Some(style) = style {
Expand Down
3 changes: 2 additions & 1 deletion examples/number_input/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ impl Application for NumberInputDemo {
.into(),
NumberInputDemo::Loaded(State { value }) => {
let lb_minute = Text::new("Number Input:");
let txt_minute = number_input(*value, 255.0, Message::NumInpChanged)
let txt_minute = number_input(*value, Message::NumInpChanged)
.max(255.0)
.style(NumberInputStyles::Default)
.step(0.5);

Expand Down
34 changes: 11 additions & 23 deletions examples/wrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,29 +189,17 @@ impl Application for RandStrings {
Some(Into::<WrapAlign>::into(state.align)),
Message::ChangeAlign,
);
let spacing_input =
Column::new()
.push(Text::new("spacing"))
.push(NumberInput::new(
state.spacing,
500.0,
Message::ChangeSpacing,
));
let line_spacing_input =
Column::new()
.push(Text::new("line spacing"))
.push(NumberInput::new(
state.line_spacing,
500.0,
Message::ChangeLineSpacing,
));
let line_minimal_length_input = Column::new()
.push(Text::new("line minimal length"))
.push(NumberInput::new(
state.line_minimal_length,
999.0,
Message::ChangeMinimalLength,
));
let spacing_input = Column::new()
.push(Text::new("spacing"))
.push(NumberInput::new(state.spacing, Message::ChangeSpacing).max(500.0));
let line_spacing_input = Column::new().push(Text::new("line spacing")).push(
NumberInput::new(state.line_spacing, Message::ChangeLineSpacing).max(500.0),
);
let line_minimal_length_input =
Column::new().push(Text::new("line minimal length")).push(
NumberInput::new(state.line_minimal_length, Message::ChangeMinimalLength)
.max(999.0),
);
let ctrls = Column::new()
.push(align_picklist)
.push(spacing_input)
Expand Down
3 changes: 1 addition & 2 deletions src/widgets/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ where
#[must_use]
pub fn number_input<'a, T, Message, Theme, Renderer, F>(
value: T,
max: T,
on_changed: F,
) -> crate::NumberInput<'a, T, Message, Theme, Renderer>
where
Expand All @@ -356,7 +355,7 @@ where
+ std::str::FromStr
+ Copy,
{
crate::NumberInput::new(value, max, on_changed)
crate::NumberInput::new(value, on_changed)
}

#[cfg(feature = "selection_list")]
Expand Down
59 changes: 30 additions & 29 deletions src/widgets/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use iced::{
Rectangle, Shadow, Size,
};
use num_traits::{Num, NumAssignOps};
use std::{fmt::Display, str::FromStr};
use std::{fmt::Display, ops::RangeInclusive, str::FromStr};

use crate::style;
pub use crate::{
Expand Down Expand Up @@ -70,7 +70,7 @@ where
/// The step for each modify of the [`NumberInput`].
step: T,
/// The min and max value of the [`NumberInput`].
bounds: (T, T),
bounds: RangeInclusive<T>,
/// The content padding of the [`NumberInput`].
padding: f32,
/// The text size of the [`NumberInput`].
Expand Down Expand Up @@ -106,7 +106,7 @@ where
/// - the current value
/// - the max value
/// - a function that produces a message when the [`NumberInput`] changes
pub fn new<F>(value: T, max: T, on_changed: F) -> Self
pub fn new<F>(value: T, on_changed: F) -> Self
where
F: 'static + Fn(T) -> Message + Copy,
T: 'static,
Expand All @@ -119,7 +119,7 @@ where
Self {
value,
step: T::one(),
bounds: (T::zero(), max),
bounds: RangeInclusive::new(T::zero(), T::one()),
padding,
size: None,
content: TextInput::new("", format!("{value}").as_str())
Expand All @@ -135,11 +135,14 @@ where
}

/// Sets the minimum & maximum value (bound) of the [`NumberInput`].
/// # Example
/// ```
/// // Creates a range from -5 till 5.
/// number_input(my_value, my_message).bounds(-5..=5)
/// ```
#[must_use]
pub fn bounds(mut self, bounds: (T, T)) -> Self {
if bounds.0 <= bounds.1 {
self.bounds = bounds;
}
pub fn bounds(mut self, bounds: RangeInclusive<T>) -> Self {
self.bounds = bounds;
self
}

Expand All @@ -165,17 +168,17 @@ where
/// Sets the minimum value of the [`NumberInput`].
#[must_use]
pub fn min(mut self, min: T) -> Self {
if min <= self.bounds.1 {
self.bounds.0 = min;
if min <= *self.bounds.end() {
self.bounds = RangeInclusive::new(min, *self.bounds.end());
}
self
}

/// Sets the maximum value of the [`NumberInput`].
#[must_use]
pub fn max(mut self, max: T) -> Self {
if max >= self.bounds.0 {
self.bounds.1 = max;
if max >= *self.bounds.start() {
self.bounds = RangeInclusive::new(*self.bounds.start(), max);
}
self
}
Expand Down Expand Up @@ -235,8 +238,8 @@ where

/// Decrease current value by step of the [`NumberInput`].
fn decrease_val(&mut self, shell: &mut Shell<Message>) {
if self.value < self.bounds.0 + self.step {
self.value = self.bounds.0;
if self.value < *self.bounds.start() + self.step {
self.value = *self.bounds.start();
} else {
self.value -= self.step;
}
Expand All @@ -246,8 +249,8 @@ where

/// Increase current value by step of the [`NumberInput`].
fn increase_val(&mut self, shell: &mut Shell<Message>) {
if self.value > self.bounds.1 - self.step {
self.value = self.bounds.1;
if self.value > *self.bounds.end() - self.step {
self.value = *self.bounds.end();
} else {
self.value += self.step;
}
Expand Down Expand Up @@ -404,7 +407,7 @@ where
.expect("fail to get decreate mod layout")
.bounds();

if self.bounds.0 == self.bounds.1 {
if self.bounds.start() == self.bounds.end() {
return event::Status::Ignored;
}

Expand Down Expand Up @@ -472,10 +475,7 @@ where
}

match T::from_str(&new_val) {
Ok(val)
if (self.bounds.0..=self.bounds.1).contains(&val)
&& val != self.value =>
{
Ok(val) if self.bounds.contains(&val) && val != self.value => {
self.value = val;
forward_to_text(event, shell, child, clipboard)
}
Expand Down Expand Up @@ -515,10 +515,7 @@ where
}

match T::from_str(&new_val) {
Ok(val)
if (self.bounds.0..=self.bounds.1).contains(&val)
&& val != self.value =>
{
Ok(val) if self.bounds.contains(&val) && val != self.value => {
self.value = val;
forward_to_text(event, shell, child, clipboard)
}
Expand Down Expand Up @@ -590,8 +587,10 @@ where
.expect("fail to get decreate mod layout")
.bounds();
let is_mouse_over = bounds.contains(cursor.position().unwrap_or_default());
let is_decrease_disabled = self.value <= self.bounds.0 || self.bounds.0 == self.bounds.1;
let is_increase_disabled = self.value >= self.bounds.1 || self.bounds.0 == self.bounds.1;
let is_decrease_disabled =
self.value <= *self.bounds.start() || self.bounds.start() == self.bounds.end();
let is_increase_disabled =
self.value >= *self.bounds.end() || self.bounds.start() == self.bounds.end();
let mouse_over_decrease = dec_bounds.contains(cursor.position().unwrap_or_default());
let mouse_over_increase = inc_bounds.contains(cursor.position().unwrap_or_default());

Expand Down Expand Up @@ -639,8 +638,10 @@ where
None,
viewport,
);
let is_decrease_disabled = self.value <= self.bounds.0 || self.bounds.0 == self.bounds.1;
let is_increase_disabled = self.value >= self.bounds.1 || self.bounds.0 == self.bounds.1;
let is_decrease_disabled =
self.value <= *self.bounds.start() || self.bounds.start() == self.bounds.end();
let is_increase_disabled =
self.value >= *self.bounds.end() || self.bounds.start() == self.bounds.end();

let decrease_btn_style = if is_decrease_disabled {
style::number_input::StyleSheet::disabled(theme, &self.style)
Expand Down
Loading