Totally rewrite dashboard as a GTK application #54

Merged
savanni merged 14 commits from dashboard-gtk into main 2023-08-09 18:14:01 +00:00
2 changed files with 84 additions and 10 deletions
Showing only changes of commit 96fc16d0f0 - Show all commits

View File

@ -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(&gtk::Label::new(Some(
format!(
"{:?}, {:?} {}, {}",
@ -201,6 +203,67 @@ impl Date {
}
}
#[derive(Default)]
pub struct DayPrivate {
info: Rc<RefCell<Option<SunMoon>>>,
}
#[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<DayPrivate>) @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));
},
);

View File

@ -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";