Skip to content

Commit

Permalink
Merge pull request #242 from Redhawk18/main
Browse files Browse the repository at this point in the history
changed number input's bounds to an inclusive range
  • Loading branch information
genusistimelord authored May 17, 2024
2 parents 67de1be + 70c3fe0 commit 8cc9128
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 77 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tab_bar = []
tabs = ["tab_bar"]
time_picker = ["chrono", "icons", "iced/canvas"]
wrap = []
number_input = ["num-traits"]
number_input = ["num-format", "num-traits"]
selection_list = []
split = []
menu = []
Expand Down Expand Up @@ -67,11 +67,12 @@ default = [

[dependencies]
cfg-if = "1.0"
chrono = { version = "0.4.34", optional = true }
itertools = { version = "0.12.1", optional = true }
num-format = { version = "0.4.4", optional = true }
num-traits = { version = "0.2.18", optional = true }
time = { version = "0.3.34", features = ["local-offset"], optional = true }
chrono = { version = "0.4.34", optional = true }
once_cell = { version = "1.19.0", optional = true }
itertools = { version = "0.12.1", optional = true }

[dependencies.iced]
#git = "https://github.com/iced-rs/iced.git"
Expand Down
9 changes: 4 additions & 5 deletions examples/WidgetIDReturn/src/numberinput.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced::{Element, Length};
use iced_aw::{NumberInput, NumberInputStyles};
use num_traits::{Num, NumAssignOps};
use num_traits::{bounds::Bounded, Num, NumAssignOps};
use std::fmt::Display;
use std::marker::PhantomData;
use std::str::FromStr;
Expand All @@ -18,7 +18,7 @@ pub enum NumInputMessage<V> {

impl<V> NumInputMessage<V>
where
V: Num + NumAssignOps + PartialOrd + Display + FromStr + Copy,
V: Num + NumAssignOps + PartialOrd + Display + FromStr + Copy + Bounded,
{
pub fn get_data(&self) -> V {
let NumInputMessage::Change(data) = self;
Expand All @@ -38,7 +38,7 @@ where

impl<V, M> NumInput<V, M>
where
V: Num + NumAssignOps + PartialOrd + Display + FromStr + Copy,
V: Num + NumAssignOps + PartialOrd + Display + FromStr + Copy + Bounded,
M: Clone,
{
pub fn new(value: V) -> NumInput<V, M>
Expand All @@ -65,9 +65,8 @@ where
V: 'static,
M: 'static,
{
let mut input = NumberInput::new(self.value, max, NumInputMessage::Change)
let mut input = NumberInput::new(self.value, min..max, NumInputMessage::Change)
.step(step)
.min(min)
.width(Length::Shrink);

if let Some(style) = style {
Expand Down
2 changes: 1 addition & 1 deletion examples/number_input/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ 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, 0.0..250.0, Message::NumInpChanged)
.style(NumberInputStyles::Default)
.step(0.5);

Expand Down
6 changes: 3 additions & 3 deletions examples/wrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,22 @@ impl Application for RandStrings {
.push(Text::new("spacing"))
.push(NumberInput::new(
state.spacing,
500.0,
0.0..500.0,
Message::ChangeSpacing,
));
let line_spacing_input =
Column::new()
.push(Text::new("line spacing"))
.push(NumberInput::new(
state.line_spacing,
500.0,
0.0..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,
0.0..999.9,
Message::ChangeMinimalLength,
));
let ctrls = Column::new()
Expand Down
12 changes: 7 additions & 5 deletions src/widgets/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

#[allow(unused_imports)]
use iced::{self, advanced::renderer, Color, Element};
use num_traits::bounds::Bounded;
#[allow(unused_imports)]
use std::{borrow::Cow, fmt::Display, hash::Hash};
use std::{borrow::Cow, fmt::Display, hash::Hash, ops::RangeBounds};

/// Creates a [`Grid`] with the given [`GridRow`]s.
///
Expand Down Expand Up @@ -337,7 +338,7 @@ where
#[must_use]
pub fn number_input<'a, T, Message, Theme, Renderer, F>(
value: T,
max: T,
bounds: impl RangeBounds<T>,
on_changed: F,
) -> crate::NumberInput<'a, T, Message, Theme, Renderer>
where
Expand All @@ -354,9 +355,10 @@ where
+ PartialOrd
+ std::fmt::Display
+ std::str::FromStr
+ Copy,
+ Copy
+ Bounded,
{
crate::NumberInput::new(value, max, on_changed)
crate::NumberInput::new(value, bounds, on_changed)
}

#[cfg(feature = "selection_list")]
Expand Down Expand Up @@ -410,7 +412,7 @@ where
+ crate::style::selection_list::StyleSheet
+ iced::widget::container::StyleSheet
+ iced::widget::scrollable::StyleSheet,
T: Clone + Display + Eq + Hash,
T: Clone + Display + Eq + Hash + Bounded,
[T]: ToOwned<Owned = Vec<T>>,
{
crate::SelectionList::new(options, on_selected)
Expand Down
Loading

0 comments on commit 8cc9128

Please sign in to comment.