From 5cc6bfcbdeb5596a9941b47d23ff20491c159aa3 Mon Sep 17 00:00:00 2001 From: Phillip Tennen Date: Tue, 20 Dec 2022 20:32:37 +0000 Subject: [PATCH] [awm2] Dedicated utils module --- rust_programs/awm2/src/desktop.rs | 30 +++----------------------- rust_programs/awm2/src/main.rs | 1 + rust_programs/awm2/src/utils.rs | 35 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 rust_programs/awm2/src/utils.rs diff --git a/rust_programs/awm2/src/desktop.rs b/rust_programs/awm2/src/desktop.rs index f2118aa1e..a4191bac3 100644 --- a/rust_programs/awm2/src/desktop.rs +++ b/rust_programs/awm2/src/desktop.rs @@ -26,9 +26,7 @@ use mouse_driver_messages::MousePacket; use file_manager_messages::str_from_u8_nul_utf8_unchecked; use kb_driver_messages::{KeyEventType, KeyboardPacket}; use lazy_static::lazy_static; -use rand::rngs::SmallRng; -use rand::RngCore; -use rand::{Rng, SeedableRng}; +use rand::prelude::*; #[cfg(target_os = "axle")] pub extern crate libc; @@ -38,33 +36,12 @@ mod conditional_imports { pub use axle_rt::amc_message_send; } #[cfg(not(target_os = "axle"))] -mod conditional_imports { - pub use std::time::{SystemTime, UNIX_EPOCH}; -} +mod conditional_imports {} use crate::desktop::conditional_imports::*; +use crate::utils::{get_timestamp, random_color, random_color_with_rng}; use crate::window::Window; -fn get_timestamp() -> u64 { - #[cfg(target_os = "axle")] - return unsafe { libc::ms_since_boot() as u64 }; - #[cfg(not(target_os = "axle"))] - return SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_millis() as u64; -} - -fn random_color() -> Color { - let seed = get_timestamp(); - let mut rng = SmallRng::seed_from_u64(seed); - Color::new(rng.gen(), rng.gen(), rng.gen()) -} - -fn random_color_with_rng(rng: &mut SmallRng) -> Color { - Color::new(rng.gen(), rng.gen(), rng.gen()) -} - fn send_left_click_event(window: &Rc, mouse_pos: Point) { let mouse_within_window = window.frame().translate_point(mouse_pos); let mouse_within_content_view = window.content_frame().translate_point(mouse_within_window); @@ -424,7 +401,6 @@ pub struct Desktop { impl Desktop { pub fn new(video_memory_layer: Rc>) -> Self { let desktop_frame = Rect::with_size(video_memory_layer.frame().size); - video_memory_layer.fill_rect(desktop_frame, Color::yellow(), StrokeThickness::Filled); let desktop_background_layer = Box::new(SingleFramebufferLayer::new(desktop_frame.size)); let screen_buffer_layer = Box::new(SingleFramebufferLayer::new(desktop_frame.size)); diff --git a/rust_programs/awm2/src/main.rs b/rust_programs/awm2/src/main.rs index aa9ee378e..440ab098a 100644 --- a/rust_programs/awm2/src/main.rs +++ b/rust_programs/awm2/src/main.rs @@ -7,6 +7,7 @@ mod desktop; mod effects; +mod utils; mod window; extern crate alloc; diff --git a/rust_programs/awm2/src/utils.rs b/rust_programs/awm2/src/utils.rs new file mode 100644 index 000000000..edebf2102 --- /dev/null +++ b/rust_programs/awm2/src/utils.rs @@ -0,0 +1,35 @@ +use agx_definitions::Color; +use rand::rngs::SmallRng; +use rand::RngCore; +use rand::{Rng, SeedableRng}; + +#[cfg(target_os = "axle")] +pub extern crate libc; +#[cfg(target_os = "axle")] +mod conditional_imports {} +#[cfg(not(target_os = "axle"))] +mod conditional_imports { + pub use std::time::{SystemTime, UNIX_EPOCH}; +} + +use crate::utils::conditional_imports::*; + +pub fn get_timestamp() -> u64 { + #[cfg(target_os = "axle")] + return unsafe { libc::ms_since_boot() as u64 }; + #[cfg(not(target_os = "axle"))] + return SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis() as u64; +} + +pub fn random_color() -> Color { + let seed = get_timestamp(); + let mut rng = SmallRng::seed_from_u64(seed); + Color::new(rng.gen(), rng.gen(), rng.gen()) +} + +pub fn random_color_with_rng(rng: &mut SmallRng) -> Color { + Color::new(rng.gen(), rng.gen(), rng.gen()) +}