From 96fc16d0f036e3da8faaf1b65053f86d25e6e70e Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 7 Aug 2023 22:35:43 -0400 Subject: [PATCH] Start drawing the clock --- dashboard/src/main.rs | 89 +++++++++++++++++++++++++++++++--- dashboard/src/soluna_client.rs | 5 +- 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index e1a9a3e..7447564 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -5,18 +5,20 @@ extern crate serde_derive; extern crate lazy_static; */ -use chrono::Datelike; +use chrono::{Datelike, Duration, NaiveTime}; use glib::Object; -use gtk::{prelude::*, subclass::prelude::*}; +use gtk::{prelude::*, subclass::prelude::*, Orientation}; +use ifc::IFC; +use std::{cell::RefCell, f64::consts::PI, ops::Deref, rc::Rc}; /* use geo_types::{Latitude, Longitude}; -use ifc; -use std::sync::Arc; +*/ mod soluna_client; -use soluna_client::SolunaClient; +use soluna_client::{LunarPhase, SunMoon}; +/* mod solstices; use solstices::EVENTS; */ @@ -186,7 +188,7 @@ impl Date { fn new() -> Self { let s: Self = Object::builder().build(); - let dt = ifc::IFC::from(chrono::Local::now().date_naive().with_year(12023).unwrap()); + let dt = IFC::from(chrono::Local::now().date_naive().with_year(12023).unwrap()); s.append(>k::Label::new(Some( format!( "{:?}, {:?} {}, {}", @@ -201,6 +203,67 @@ impl Date { } } +#[derive(Default)] +pub struct DayPrivate { + info: Rc>>, +} + +#[glib::object_subclass] +impl ObjectSubclass for DayPrivate { + const NAME: &'static str = "Day"; + type Type = Day; + type ParentType = gtk::DrawingArea; +} + +impl ObjectImpl for DayPrivate {} +impl WidgetImpl for DayPrivate {} +impl DrawingAreaImpl for DayPrivate {} + +glib::wrapper! { + pub struct Day(ObjectSubclass) @extends gtk::DrawingArea, gtk::Widget; +} + +impl Day { + pub fn new(sun_moon_info: SunMoon) -> Self { + let s: Self = Object::builder().build(); + s.set_width_request(500); + s.set_height_request(500); + + *s.imp().info.borrow_mut() = Some(sun_moon_info); + s.set_draw_func({ + let s = s.clone(); + move |_, context, width, height| { + let info = s.imp().info.borrow(); + + // context.set_source_rgb(240., 240., 240.); + context.set_source_rgb(0., 0., 0.); + let _ = context.paint(); + + context.set_line_width(5.); + context.set_source_rgb(0.7, 0., 0.9); + context.arc(width as f64 / 2., height as f64 / 2., 200., 0., PI * 2.); + let _ = context.stroke(); + + if let Some(info) = info.deref() { + context.move_to(width as f64 / 2., height as f64 / 2.); + let full_day = Duration::days(1).num_seconds() as f64; + let sunrise = info.sunrise - NaiveTime::from_hms_opt(0, 0, 0).unwrap(); + let sunset = info.sunset - NaiveTime::from_hms_opt(0, 0, 0).unwrap(); + + let start = (PI * 2.) * sunset.num_seconds() as f64 / full_day - (PI / 2.); + let end = (PI * 2.) * sunrise.num_seconds() as f64 / full_day - (PI / 2.); + + context.set_source_rgb(0., 0.7, 0.9); + context.arc(width as f64 / 2., height as f64 / 2., 200., start, end); + let _ = context.fill(); + } + } + }); + + s + } +} + pub fn main() { /* let runtime = Arc::new( @@ -220,8 +283,20 @@ pub fn main() { let window = gtk::ApplicationWindow::new(app); window.present(); + let layout = gtk::Box::builder() + .orientation(Orientation::Vertical) + .build(); let date = Date::new(); - window.set_child(Some(&date)); + let day = Day::new(SunMoon { + sunrise: NaiveTime::from_hms_opt(6, 0, 0).unwrap(), + sunset: NaiveTime::from_hms_opt(0, 0, 0).unwrap(), + moonrise: NaiveTime::from_hms_opt(15, 0, 0), + moonset: NaiveTime::from_hms_opt(5, 0, 0), + moon_phase: LunarPhase::WaningCrescent, + }); + layout.append(&date); + layout.append(&day); + window.set_child(Some(&layout)); }, ); diff --git a/dashboard/src/soluna_client.rs b/dashboard/src/soluna_client.rs index 60e3e69..f705043 100644 --- a/dashboard/src/soluna_client.rs +++ b/dashboard/src/soluna_client.rs @@ -1,12 +1,11 @@ // 41.78, -71.41 // https://api.solunar.org/solunar/41.78,-71.41,20211029,-4 -use chrono::{ - DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike, Utc, -}; +use chrono::{DateTime, Duration, NaiveTime, Offset, TimeZone, Utc}; use geo_types::{Latitude, Longitude}; use memorycache::MemoryCache; use reqwest; +use serde::Deserialize; const ENDPOINT: &str = "https://api.solunar.org/solunar";