From 5a610baa2fa722c1ed9d757d4a678261d24bc5b0 Mon Sep 17 00:00:00 2001
From: sam edelsten <samedelsten1@gmail.com>
Date: Mon, 29 Apr 2024 19:57:55 +0100
Subject: [PATCH] further clean up `lib.rs`

---
 src/cosmic_edit.rs | 21 +++++++++++++++++++++
 src/events.rs      | 14 ++++++++++++++
 src/lib.rs         | 44 +++++---------------------------------------
 src/util.rs        | 13 +++++++++++++
 4 files changed, 53 insertions(+), 39 deletions(-)
 create mode 100644 src/events.rs

diff --git a/src/cosmic_edit.rs b/src/cosmic_edit.rs
index e36a24f..ab13543 100644
--- a/src/cosmic_edit.rs
+++ b/src/cosmic_edit.rs
@@ -110,3 +110,24 @@ impl Default for CosmicEditBundle {
         }
     }
 }
+
+#[derive(Resource, Deref, DerefMut)]
+pub struct CosmicFontSystem(pub FontSystem);
+
+#[derive(Component, Deref, DerefMut)]
+pub struct CosmicEditor {
+    #[deref]
+    pub editor: Editor<'static>,
+    pub cursor_visible: bool,
+    pub cursor_timer: Timer,
+}
+
+impl CosmicEditor {
+    pub fn new(editor: Editor<'static>) -> Self {
+        Self {
+            editor,
+            cursor_visible: true,
+            cursor_timer: Timer::new(Duration::from_millis(530), TimerMode::Repeating),
+        }
+    }
+}
diff --git a/src/events.rs b/src/events.rs
new file mode 100644
index 0000000..57aa200
--- /dev/null
+++ b/src/events.rs
@@ -0,0 +1,14 @@
+// File for all events, meant for easy documentation
+
+use bevy::prelude::*;
+
+pub struct EventsPlugin;
+
+impl Plugin for EventsPlugin {
+    fn build(&self, app: &mut App) {
+        app.add_event::<CosmicTextChanged>();
+    }
+}
+
+#[derive(Event, Debug)]
+pub struct CosmicTextChanged(pub (Entity, String));
diff --git a/src/lib.rs b/src/lib.rs
index 4f9a2ea..49348ce 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,6 +3,7 @@
 mod buffer;
 mod cosmic_edit;
 mod cursor;
+mod events;
 mod focus;
 mod input;
 mod password;
@@ -22,6 +23,7 @@ pub use cosmic_text::{
     FontSystem, Metrics, Shaping, Style as FontStyle, Weight as FontWeight,
 };
 pub use cursor::*;
+pub use events::*;
 pub use focus::*;
 pub use input::*;
 pub use password::*;
@@ -29,6 +31,7 @@ pub use placeholder::*;
 pub use render::*;
 pub use util::*;
 pub use widget::*;
+
 /// Plugin struct that adds systems and initializes resources related to cosmic edit functionality.
 #[derive(Default)]
 pub struct CosmicEditPlugin {
@@ -51,9 +54,9 @@ impl Plugin for CosmicEditPlugin {
             },
             PlaceholderPlugin,
             PasswordPlugin,
+            EventsPlugin,
         ))
-        .insert_resource(CosmicFontSystem(font_system))
-        .add_event::<CosmicTextChanged>();
+        .insert_resource(CosmicFontSystem(font_system));
 
         #[cfg(target_arch = "wasm32")]
         {
@@ -76,30 +79,6 @@ pub enum CursorConfig {
     None,
 }
 
-#[derive(Event, Debug)]
-pub struct CosmicTextChanged(pub (Entity, String));
-
-#[derive(Resource, Deref, DerefMut)]
-pub struct CosmicFontSystem(pub FontSystem);
-
-#[derive(Component, Deref, DerefMut)]
-pub struct CosmicEditor {
-    #[deref]
-    pub editor: Editor<'static>,
-    pub cursor_visible: bool,
-    pub cursor_timer: Timer,
-}
-
-impl CosmicEditor {
-    fn new(editor: Editor<'static>) -> Self {
-        Self {
-            editor,
-            cursor_visible: true,
-            cursor_timer: Timer::new(Duration::from_millis(530), TimerMode::Repeating),
-        }
-    }
-}
-
 /// Resource struct that holds configuration options for cosmic fonts.
 #[derive(Resource, Clone)]
 pub struct CosmicFontConfig {
@@ -136,19 +115,6 @@ fn create_cosmic_font_system(cosmic_font_config: CosmicFontConfig) -> FontSystem
     cosmic_text::FontSystem::new_with_locale_and_db(locale, db)
 }
 
-#[cfg(target_arch = "wasm32")]
-pub fn get_timestamp() -> f64 {
-    js_sys::Date::now()
-}
-
-#[cfg(not(target_arch = "wasm32"))]
-pub fn get_timestamp() -> f64 {
-    use std::time::SystemTime;
-    use std::time::UNIX_EPOCH;
-    let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
-    duration.as_millis() as f64
-}
-
 #[cfg(test)]
 mod tests {
     use crate::*;
diff --git a/src/util.rs b/src/util.rs
index cb50afc..6175e52 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -118,3 +118,16 @@ pub fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor {
         (color.a() * 255.) as u8,
     )
 }
+
+#[cfg(target_arch = "wasm32")]
+pub fn get_timestamp() -> f64 {
+    js_sys::Date::now()
+}
+
+#[cfg(not(target_arch = "wasm32"))]
+pub fn get_timestamp() -> f64 {
+    use std::time::SystemTime;
+    use std::time::UNIX_EPOCH;
+    let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
+    duration.as_millis() as f64
+}
-- 
GitLab