Skip to content

Commit

Permalink
Fixes #274, Wasm time issues
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed Nov 27, 2024
1 parent 719fe29 commit 55d23d8
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions examples/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct NumberInputDemo {

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

fn main() -> iced::Result {
Expand All @@ -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<Message> {
Expand Down
9 changes: 5 additions & 4 deletions examples/typed_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct TypedInputDemo {

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

fn main() -> iced::Result {
Expand All @@ -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<Message> {
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();
self.value[id].value = val.get_data().unwrap_or_default();
}

fn view(&self) -> Element<Message> {
Expand Down
18 changes: 11 additions & 7 deletions examples/widget_id_return/numberinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,30 @@ pub struct NumInput<V, M> {

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

impl<V> NumInputMessage<V>
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<V, String> {
match self {
NumInputMessage::Change(Ok(data)) => Ok(*data),
NumInputMessage::Change(Err(data)) => Err(data.clone()),
}
}
}

impl<V> NumInputMessage<V>
where
V: Eq + Copy,
{
pub fn get_enum(&self) -> V {
let NumInputMessage::Change(data) = self;
*data
pub fn get_enum(&self) -> Result<V, String> {
match self {
NumInputMessage::Change(Ok(data)) => Ok(*data),
NumInputMessage::Change(Err(data)) => Err(data.clone()),
}
}
}

Expand Down
13 changes: 7 additions & 6 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(f32),
ChangeLineSpacing(f32),
ChangeMinimalLength(f32),
ChangeSpacing(Result<f32, String>),
ChangeLineSpacing(Result<f32, String>),
ChangeMinimalLength(Result<f32, String>),
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/widget/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ where
Message: Clone + 'a,
Renderer: iced::advanced::text::Renderer<Font = iced::Font>,
Theme: crate::style::number_input::ExtendedCatalog,
F: 'static + Fn(T) -> Message + Copy,
F: 'static + Fn(Result<T, String>) -> 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(T) -> Message + Copy,
F: 'static + Fn(Result<T, String>) -> Message + Copy,
T: 'static + std::fmt::Display + std::str::FromStr + Clone,
{
crate::TypedInput::new("", value).on_input(on_change)
Expand Down
8 changes: 4 additions & 4 deletions src/widget/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Fn(T) -> Message>,
on_change: Box<dyn Fn(Result<T, String>) -> Message>,
/// The style of the [`NumberInput`].
class: <Theme as style::number_input::Catalog>::Class<'a>,
/// The font text of the [`NumberInput`].
Expand Down Expand Up @@ -114,7 +114,7 @@ where
/// - a function that produces a message when the [`NumberInput`] changes
pub fn new<F>(value: T, bounds: impl RangeBounds<T>, on_change: F) -> Self
where
F: 'static + Fn(T) -> Message + Copy,
F: 'static + Fn(Result<T, String>) -> Message + Copy,
T: 'static,
{
let padding = DEFAULT_PADDING;
Expand Down Expand Up @@ -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`].
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/widget/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
39 changes: 25 additions & 14 deletions src/widget/typed_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn 'a + Fn(T) -> Message>>,
on_change: Option<Box<dyn 'a + Fn(Result<T, String>) -> Message>>,
/// The ``on_submit`` event of the [`TypedInput`].
#[allow(clippy::type_complexity)]
on_submit: Option<Box<dyn 'a + Fn(Result<T, String>) -> Message>>,
/// The ``on_paste`` event of the [`TypedInput`]
on_paste: Option<Box<dyn 'a + Fn(T) -> Message>>,
on_paste: Option<Box<dyn 'a + Fn(Result<T, String>) -> Message>>,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -118,7 +118,7 @@ where
#[must_use]
pub fn on_input<F>(mut self, callback: F) -> Self
where
F: 'a + Fn(T) -> Message,
F: 'a + Fn(Result<T, String>) -> Message,
{
self.text_input = self.text_input.on_input(InternalMessage::OnChange);
self.on_change = Some(Box::new(callback));
Expand Down Expand Up @@ -146,7 +146,7 @@ where
#[must_use]
pub fn on_paste<F>(mut self, callback: F) -> Self
where
F: 'a + Fn(T) -> Message,
F: 'a + Fn(Result<T, String>) -> Message,
{
self.text_input = self.text_input.on_paste(InternalMessage::OnPaste);
self.on_paste = Some(Box::new(callback));
Expand Down Expand Up @@ -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 => {
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 55d23d8

Please sign in to comment.