From 55d23d889a04c7d22edc70da58ebd0895068b96c Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Wed, 27 Nov 2024 08:31:00 -0500 Subject: [PATCH] Fixes #274, Wasm time issues --- Cargo.lock | 1 + Cargo.toml | 4 ++- examples/number_input.rs | 9 +++--- examples/typed_input.rs | 9 +++--- examples/widget_id_return/main.rs | 2 +- examples/widget_id_return/numberinput.rs | 18 ++++++----- examples/wrap.rs | 13 ++++---- src/widget/helpers.rs | 4 +-- src/widget/number_input.rs | 8 ++--- src/widget/spinner.rs | 3 +- src/widget/typed_input.rs | 39 +++++++++++++++--------- 11 files changed, 65 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd706702..b7579b45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1437,6 +1437,7 @@ dependencies = [ "num-traits", "rand", "time", + "web-time", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 674c38b0..a27e30d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,13 +61,15 @@ default = [ [dependencies] cfg-if = "1.0" -chrono = { version = "0.4.38", optional = true } +chrono = { version = "0.4.38", optional = true, features = ["wasmbind"]} itertools = { version = "0.13.0", optional = true } num-format = { version = "0.4.4", optional = true } num-traits = { version = "0.2.19", optional = true } time = { version = "0.3.36", features = ["local-offset"], optional = true } iced_fonts = "0.1.1" +web-time = "1.1.0" + [dependencies.iced] #git = "https://github.com/iced-rs/iced.git" #rev = "b474a2b7a763dcde6a377cb409001a7b5285ee8d" diff --git a/examples/number_input.rs b/examples/number_input.rs index 11cce1c9..2ad181fa 100644 --- a/examples/number_input.rs +++ b/examples/number_input.rs @@ -15,7 +15,7 @@ pub struct NumberInputDemo { #[derive(Debug, Clone)] pub enum Message { - NumInpChanged(f32), + NumInpChanged(Result), } fn main() -> iced::Result { @@ -34,9 +34,10 @@ fn main() -> iced::Result { impl NumberInputDemo { fn update(&mut self, message: self::Message) { - let Message::NumInpChanged(val) = message; - println!("Value changed to {:?}", val); - self.value = val; + if let Message::NumInpChanged(Ok(val)) = message { + println!("Value changed to {:?}", val); + self.value = val; + } } fn view(&self) -> Element { diff --git a/examples/typed_input.rs b/examples/typed_input.rs index e8f4c767..521ec29a 100644 --- a/examples/typed_input.rs +++ b/examples/typed_input.rs @@ -15,7 +15,7 @@ pub struct TypedInputDemo { #[derive(Debug, Clone)] pub enum Message { - TypedInpChanged(f32), + TypedInpChanged(Result), } fn main() -> iced::Result { @@ -34,9 +34,10 @@ fn main() -> iced::Result { impl TypedInputDemo { fn update(&mut self, message: self::Message) { - let Message::TypedInpChanged(val) = message; - println!("Value changed to {:?}", val); - self.value = val; + if let Message::TypedInpChanged(Ok(val)) = message { + println!("Value changed to {:?}", val); + self.value = val; + } } fn view(&self) -> Element { diff --git a/examples/widget_id_return/main.rs b/examples/widget_id_return/main.rs index 8d230c92..7e5b99cc 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(); + self.value[id].value = val.get_data().unwrap_or_default(); } fn view(&self) -> Element { diff --git a/examples/widget_id_return/numberinput.rs b/examples/widget_id_return/numberinput.rs index 6308a681..28891aed 100644 --- a/examples/widget_id_return/numberinput.rs +++ b/examples/widget_id_return/numberinput.rs @@ -14,16 +14,18 @@ pub struct NumInput { #[derive(Debug, Clone, PartialEq, Eq)] pub enum NumInputMessage { - Change(V), + Change(Result), } impl NumInputMessage where V: Num + NumAssignOps + PartialOrd + Display + FromStr + Copy + Bounded, { - pub fn get_data(&self) -> V { - let NumInputMessage::Change(data) = self; - *data + pub fn get_data(&self) -> Result { + match self { + NumInputMessage::Change(Ok(data)) => Ok(*data), + NumInputMessage::Change(Err(data)) => Err(data.clone()), + } } } @@ -31,9 +33,11 @@ impl NumInputMessage where V: Eq + Copy, { - pub fn get_enum(&self) -> V { - let NumInputMessage::Change(data) = self; - *data + pub fn get_enum(&self) -> Result { + match self { + NumInputMessage::Change(Ok(data)) => Ok(*data), + NumInputMessage::Change(Err(data)) => Err(data.clone()), + } } } diff --git a/examples/wrap.rs b/examples/wrap.rs index dfeb0637..357b6927 100644 --- a/examples/wrap.rs +++ b/examples/wrap.rs @@ -89,9 +89,9 @@ struct StrButton { #[derive(Debug, Clone)] enum Message { ChangeAlign(WrapAlign), - ChangeSpacing(f32), - ChangeLineSpacing(f32), - ChangeMinimalLength(f32), + ChangeSpacing(Result), + ChangeLineSpacing(Result), + ChangeMinimalLength(Result), } impl RandStrings { @@ -100,15 +100,16 @@ impl RandStrings { Message::ChangeAlign(align) => { self.align = align.into(); } - Message::ChangeSpacing(num) => { + Message::ChangeSpacing(Ok(num)) => { self.spacing = num; } - Message::ChangeLineSpacing(num) => { + Message::ChangeLineSpacing(Ok(num)) => { self.line_spacing = num; } - Message::ChangeMinimalLength(num) => { + Message::ChangeMinimalLength(Ok(num)) => { self.line_minimal_length = num; } + _ => {} } } diff --git a/src/widget/helpers.rs b/src/widget/helpers.rs index 48655016..a1e328a0 100644 --- a/src/widget/helpers.rs +++ b/src/widget/helpers.rs @@ -316,7 +316,7 @@ where Message: Clone + 'a, Renderer: iced::advanced::text::Renderer, Theme: crate::style::number_input::ExtendedCatalog, - F: 'static + Fn(T) -> Message + Copy, + F: 'static + Fn(Result) -> Message + Copy, T: 'static + num_traits::Num + num_traits::NumAssignOps @@ -342,7 +342,7 @@ where Message: Clone, Renderer: iced::advanced::text::Renderer, Theme: iced::widget::text_input::Catalog, - F: 'static + Fn(T) -> Message + Copy, + F: 'static + Fn(Result) -> Message + Copy, T: 'static + std::fmt::Display + std::str::FromStr + Clone, { crate::TypedInput::new("", value).on_input(on_change) diff --git a/src/widget/number_input.rs b/src/widget/number_input.rs index 39600a5f..74e7ed1e 100644 --- a/src/widget/number_input.rs +++ b/src/widget/number_input.rs @@ -85,7 +85,7 @@ where /// The underlying element of the [`NumberInput`]. content: TypedInput<'a, T, Message, Theme, Renderer>, /// The ``on_change`` event of the [`NumberInput`]. - on_change: Box Message>, + on_change: Box) -> Message>, /// The style of the [`NumberInput`]. class: ::Class<'a>, /// The font text of the [`NumberInput`]. @@ -114,7 +114,7 @@ where /// - a function that produces a message when the [`NumberInput`] changes pub fn new(value: T, bounds: impl RangeBounds, on_change: F) -> Self where - F: 'static + Fn(T) -> Message + Copy, + F: 'static + Fn(Result) -> Message + Copy, T: 'static, { let padding = DEFAULT_PADDING; @@ -248,7 +248,7 @@ where self.value -= self.step; } - shell.publish((self.on_change)(self.value)); + shell.publish((self.on_change)(Ok(self.value))); } /// Increase current value by step of the [`NumberInput`]. @@ -258,7 +258,7 @@ where } else { self.value += self.step; } - shell.publish((self.on_change)(self.value)); + shell.publish((self.on_change)(Ok(self.value))); } fn set_min(min: Bound<&T>) -> T { diff --git a/src/widget/spinner.rs b/src/widget/spinner.rs index 1beb8e55..3c18cd70 100644 --- a/src/widget/spinner.rs +++ b/src/widget/spinner.rs @@ -11,10 +11,9 @@ use iced::{ }, event::Status, mouse::Cursor, - time::Instant, + time::{Duration, Instant}, window, Border, Color, Element, Event, Length, Rectangle, Shadow, Size, Vector, }; -use std::time::Duration; /// A spinner widget, a circle spinning around the center of the widget. #[allow(missing_debug_implementations)] diff --git a/src/widget/typed_input.rs b/src/widget/typed_input.rs index 3741fba4..bf8d714e 100644 --- a/src/widget/typed_input.rs +++ b/src/widget/typed_input.rs @@ -50,12 +50,12 @@ where text_input: text_input::TextInput<'a, InternalMessage, Theme, Renderer>, text: String, /// The ``on_change`` event of the [`TypedInput`]. - on_change: Option Message>>, + on_change: Option) -> Message>>, /// The ``on_submit`` event of the [`TypedInput`]. #[allow(clippy::type_complexity)] on_submit: Option) -> Message>>, /// The ``on_paste`` event of the [`TypedInput`] - on_paste: Option Message>>, + on_paste: Option) -> Message>>, } #[derive(Debug, Clone, PartialEq)] @@ -118,7 +118,7 @@ where #[must_use] pub fn on_input(mut self, callback: F) -> Self where - F: 'a + Fn(T) -> Message, + F: 'a + Fn(Result) -> Message, { self.text_input = self.text_input.on_input(InternalMessage::OnChange); self.on_change = Some(Box::new(callback)); @@ -146,7 +146,7 @@ where #[must_use] pub fn on_paste(mut self, callback: F) -> Self where - F: 'a + Fn(T) -> Message, + F: 'a + Fn(Result) -> Message, { self.text_input = self.text_input.on_paste(InternalMessage::OnPaste); self.on_paste = Some(Box::new(callback)); @@ -347,14 +347,19 @@ where match message { InternalMessage::OnChange(value) => { self.text = value; - if let Ok(val) = T::from_str(&self.text) { - if self.value != val { + let value = match T::from_str(&self.text) { + Ok(val) if self.value != val => { self.value = val.clone(); - if let Some(on_change) = &self.on_change { - shell.publish(on_change(val)); - } + Ok(val) } + Ok(val) => Ok(val), + Err(_) => Err(self.text.clone()), + }; + + if let Some(on_change) = &self.on_change { + shell.publish(on_change(value)); } + shell.invalidate_layout(); } InternalMessage::OnSubmit => { @@ -368,14 +373,20 @@ where } InternalMessage::OnPaste(value) => { self.text = value; - if let Ok(val) = T::from_str(&self.text) { - if self.value != val { + + let value = match T::from_str(&self.text) { + Ok(val) if self.value != val => { self.value = val.clone(); - if let Some(on_paste) = &self.on_paste { - shell.publish(on_paste(val)); - } + Ok(val) } + Ok(val) => Ok(val), + Err(_) => Err(self.text.clone()), + }; + + if let Some(on_paste) = &self.on_paste { + shell.publish(on_paste(value)); } + shell.invalidate_layout(); } }