Skip to content
Snippets Groups Projects
Commit 5a610baa authored by sam edelsten's avatar sam edelsten
Browse files

further clean up `lib.rs`

parent ac95acd8
No related branches found
No related tags found
No related merge requests found
......@@ -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),
}
}
}
// 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));
......@@ -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::*;
......
......@@ -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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment