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

Correcting several issues with NumberInput #309

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand Down Expand Up @@ -36,7 +36,7 @@ repos:
- id: clippy

- repo: https://github.com/DevinR528/cargo-sort
rev: v1.0.9
rev: v1.1.0
hooks:
- id: cargo-sort
- repo: local
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.10.0] - 2024-09-18

### Added
### Added
- Typed Input @Ultraxime

### Changes
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ default = [

[dependencies]
cfg-if = "1.0"
chrono = { version = "0.4.38", optional = true, features = ["wasmbind"]}
chrono = { version = "0.4.38", optional = true, features = ["wasmbind"] }
getrandom = { version = "0.2", features = ["js"] }
iced_fonts = "0.1.1"
itertools = { version = "0.13.0", optional = true }
num-format = { version = "0.4.4", optional = true }
num-traits = { version = "0.2.19", optional = true }
iced_fonts = "0.1.1"
getrandom = { version = "0.2", features = ["js"] }
web-time = "1.1.0"

[dependencies.iced]
Expand Down Expand Up @@ -162,4 +162,4 @@ name = "wrap"
required-features = ["wrap", "number_input"]

[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib", "rlib"]
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,3 @@ Also included in this feature, are two widgets `sidebar::column::FlushColumn` an
### Color palette

This crate adds a predefined color palette based on the [CSS color palette](https://www.w3schools.com/cssref/css_colors.asp).

20 changes: 12 additions & 8 deletions examples/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub struct NumberInputDemo {

#[derive(Debug, Clone)]
pub enum Message {
NumInpChanged(Result<f32, String>),
NumInpChanged(f32),
NumInpSubmitted,
}

fn main() -> iced::Result {
Expand All @@ -34,19 +35,22 @@ fn main() -> iced::Result {

impl NumberInputDemo {
fn update(&mut self, message: self::Message) {
if let Message::NumInpChanged(Ok(val)) = message {
println!("Value changed to {:?}", val);
self.value = val;
} else if let Message::NumInpChanged(Err(_)) = message {
println!("Error Value reset to 0.0");
self.value = 0.0;
match message {
Message::NumInpChanged(val) => {
println!("Value changed to {:?}", val);
self.value = val;
}
Message::NumInpSubmitted => {
println!("Value submitted");
}
}
}

fn view(&self) -> Element<Message> {
let lb_minute = Text::new("Number Input:");
let txt_minute = number_input(self.value, -10.0..250.0, Message::NumInpChanged)
let txt_minute = number_input(&self.value, -10.0..250.0, Message::NumInpChanged)
.style(number_input::number_input::primary)
.on_submit(Message::NumInpSubmitted)
.step(0.5);

Container::new(
Expand Down
18 changes: 13 additions & 5 deletions examples/typed_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub struct TypedInputDemo {

#[derive(Debug, Clone)]
pub enum Message {
TypedInpChanged(Result<f32, String>),
TypedInpChanged(f32),
TypedInpSubmit(Result<f32, String>),
}

fn main() -> iced::Result {
Expand All @@ -34,16 +35,23 @@ fn main() -> iced::Result {

impl TypedInputDemo {
fn update(&mut self, message: self::Message) {
if let Message::TypedInpChanged(Ok(val)) = message {
println!("Value changed to {:?}", val);
self.value = val;
match message {
Message::TypedInpChanged(value) => {
println!("Value changed to {}", value);
self.value = value;
}
Message::TypedInpSubmit(Ok(value)) => println!("Value submitted: {}", value),
Message::TypedInpSubmit(Err(text)) => {
println!("Value submitted while invalid: {}", text)
}
}
}

fn view(&self) -> Element<Message> {
let lb_minute = Text::new("Typed Input:");
let txt_minute = typed_input::TypedInput::new("Placeholder", &self.value)
.on_input(Message::TypedInpChanged);
.on_input(Message::TypedInpChanged)
.on_submit(Message::TypedInpSubmit);

Container::new(
Row::new()
Expand Down
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
6 changes: 3 additions & 3 deletions src/widget/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,15 @@ where
/// [`NumberInput`]: crate::NumberInput
#[must_use]
pub fn number_input<'a, T, Message, Theme, Renderer, F>(
value: T,
value: &T,
bounds: impl RangeBounds<T>,
on_change: F,
) -> crate::NumberInput<'a, T, Message, Theme, Renderer>
where
Message: Clone + 'a,
Renderer: iced::advanced::text::Renderer<Font = iced::Font>,
Theme: crate::style::number_input::ExtendedCatalog,
F: 'static + Fn(Result<T, String>) -> Message + Copy,
F: 'static + Fn(T) -> Message + Copy,
T: 'static
+ num_traits::Num
+ num_traits::NumAssignOps
Expand All @@ -342,7 +342,7 @@ where
Message: Clone,
Renderer: iced::advanced::text::Renderer<Font = iced::Font>,
Theme: iced::widget::text_input::Catalog,
F: 'static + Fn(Result<T, String>) -> Message + Copy,
F: 'static + Fn(T) -> Message + Copy,
T: 'static + std::fmt::Display + std::str::FromStr + Clone,
{
crate::TypedInput::new("", value).on_input(on_change)
Expand Down
Loading
Loading