From 68d56a11e6fb88ad85b1fec933a357e34957df5b Mon Sep 17 00:00:00 2001
From: Ygg01 <y.laughing.man.y@gmail.com>
Date: Sun, 27 Mar 2022 17:46:25 +0200
Subject: [PATCH] Refactor on_change to shared function

---
 src/widgets/mod.rs       |  2 ++
 src/widgets/on_change.rs | 27 +++++++++++++++++++++++++++
 src/widgets/text_box.rs  | 33 +++------------------------------
 3 files changed, 32 insertions(+), 30 deletions(-)
 create mode 100644 src/widgets/on_change.rs

diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs
index 4073a78..8f5d900 100644
--- a/src/widgets/mod.rs
+++ b/src/widgets/mod.rs
@@ -8,6 +8,7 @@ mod if_element;
 mod image;
 mod inspector;
 mod nine_patch;
+mod on_change;
 mod scroll;
 mod text;
 mod text_box;
@@ -24,6 +25,7 @@ pub use if_element::*;
 pub use image::*;
 pub use inspector::*;
 pub use nine_patch::*;
+pub use on_change::*;
 pub use scroll::*;
 pub use text::*;
 pub use text_box::*;
diff --git a/src/widgets/on_change.rs b/src/widgets/on_change.rs
new file mode 100644
index 0000000..1aa632f
--- /dev/null
+++ b/src/widgets/on_change.rs
@@ -0,0 +1,27 @@
+use std::sync::{Arc, RwLock};
+
+#[derive(Debug, Clone, PartialEq)]
+pub struct ChangeEvent {
+    pub value: String,
+}
+
+#[derive(Clone)]
+pub struct OnChange(pub Arc<RwLock<dyn FnMut(ChangeEvent) + Send + Sync + 'static>>);
+
+impl OnChange {
+    pub fn new<F: FnMut(ChangeEvent) + Send + Sync + 'static>(f: F) -> OnChange {
+        OnChange(Arc::new(RwLock::new(f)))
+    }
+}
+
+impl PartialEq for OnChange {
+    fn eq(&self, _other: &Self) -> bool {
+        true
+    }
+}
+
+impl std::fmt::Debug for OnChange {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.debug_tuple("OnChange").finish()
+    }
+}
\ No newline at end of file
diff --git a/src/widgets/text_box.rs b/src/widgets/text_box.rs
index f872d2e..2113edc 100644
--- a/src/widgets/text_box.rs
+++ b/src/widgets/text_box.rs
@@ -1,13 +1,12 @@
-use crate::core::{
+use crate::{core::{
     render_command::RenderCommand,
     rsx,
     styles::{Corner, Style, Units},
     widget, Bound, Children, Color, EventType, MutableBound, OnEvent, WidgetProps,
-};
+}, widgets::ChangeEvent};
 use kayak_core::{CursorIcon, OnLayout};
-use std::sync::{Arc, RwLock};
 
-use crate::widgets::{Background, Clip, Text};
+use crate::widgets::{Background, Clip, Text, OnChange};
 
 /// Props used by the [`TextBox`] widget
 #[derive(Default, Debug, PartialEq, Clone)]
@@ -56,32 +55,6 @@ impl WidgetProps for TextBoxProps {
     }
 }
 
-#[derive(Debug, Clone, PartialEq)]
-pub struct ChangeEvent {
-    pub value: String,
-}
-
-#[derive(Clone)]
-pub struct OnChange(pub Arc<RwLock<dyn FnMut(ChangeEvent) + Send + Sync + 'static>>);
-
-impl OnChange {
-    pub fn new<F: FnMut(ChangeEvent) + Send + Sync + 'static>(f: F) -> OnChange {
-        OnChange(Arc::new(RwLock::new(f)))
-    }
-}
-
-impl PartialEq for OnChange {
-    fn eq(&self, _other: &Self) -> bool {
-        true
-    }
-}
-
-impl std::fmt::Debug for OnChange {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        f.debug_tuple("OnChange").finish()
-    }
-}
-
 #[derive(Debug, Clone, Copy, PartialEq)]
 pub struct Focus(pub bool);
 
-- 
GitLab