diff --git a/src/clock.rs b/src/clock.rs new file mode 100644 index 0000000..ab0f1a5 --- /dev/null +++ b/src/clock.rs @@ -0,0 +1,9 @@ +use chrono::prelude::Local; + +pub fn get_moon_phase() -> f32 { + let local_timestamp = Local::now(); + let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis()); + let moon_illumination = suncalc::moon_illumination(unixtime); + + moon_illumination.phase as f32 * 28.0 +} diff --git a/src/main.rs b/src/main.rs index bff40d2..78d87d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use sctk::shm::AutoMemPool; use sctk::window::{Event as WEvent, FallbackFrame}; use svg::node::element::path::Data as PathData; +mod clock; mod config; mod svg_clock; diff --git a/src/svg_clock.rs b/src/svg_clock.rs index 7d308e5..b5dd6ce 100644 --- a/src/svg_clock.rs +++ b/src/svg_clock.rs @@ -17,6 +17,7 @@ use svg::{ }; use usvg::Tree; +use crate::clock::get_moon_phase; use crate::config::Config; const HOUR_NAMES: [&str; 24] = [ @@ -296,16 +297,16 @@ fn get_range_path(radius: f32, range_name: &str, start_time: i32, end_time: i32) Path::new().set("class", range_name).set("d", path_data) } -fn get_moon_path(radius: f32, moon_phase: f64) -> Path { - let handle_x_pos = radius as f64 * 1.34; +fn get_moon_path(radius: f32, moon_phase: f32) -> Path { + let handle_x_pos = radius * 1.34; let handle_y_pos = radius * 0.88; - let min_x = IMAGE_WIDTH as f64 / 2.0 - handle_x_pos; + let min_x = IMAGE_WIDTH as f32 / 2.0 - handle_x_pos; let max_x = min_x + 2.0 * handle_x_pos; let top_y = IMAGE_WIDTH as f32 / 2.0 - handle_y_pos; let bottom_y = IMAGE_WIDTH as f32 / 2.0 + handle_y_pos; - let h1_x: f64; - let h2_x: f64; + let h1_x: f32; + let h2_x: f32; if moon_phase < 14.0 { h1_x = min_x + 2.0 * handle_x_pos * (1.0 - moon_phase / 14.0); @@ -345,16 +346,15 @@ pub fn gen_svg( let local_hour = local_timestamp.time().hour(); let local_minute = local_timestamp.time().minute(); let local_second = local_timestamp.time().second(); + let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis()); let utc_offset = local_timestamp.offset().local_minus_utc(); let local_time = local_timestamp.time().num_seconds_from_midnight() as i32; let utc_rotation = seconds_to_degrees(utc_offset); + let moon_radius = IMAGE_WIDTH as f32 * 0.071428571; // Calculate the Moon phase - let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis()); - let moon_illumination = suncalc::moon_illumination(unixtime); - let moon_radius = IMAGE_WIDTH as f32 * 0.071428571; - let moon_phase = moon_illumination.phase * 28.0; + let moon_phase = get_moon_phase(); let local_hour_font_size = IMAGE_WIDTH as f32 * 0.02357; let sun_radius = IMAGE_WIDTH as f32 * 0.0142871;