Compare commits

..

6 Commits

8 changed files with 271 additions and 24 deletions

34
Cargo.lock generated
View File

@ -341,6 +341,7 @@ dependencies = [
"gtk4", "gtk4",
"ifc", "ifc",
"lazy_static", "lazy_static",
"libadwaita",
"memorycache", "memorycache",
"reqwest", "reqwest",
"serde", "serde",
@ -1263,6 +1264,39 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8"
[[package]]
name = "libadwaita"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ab9c0843f9f23ff25634df2743690c3a1faffe0a190e60c490878517eb81abf"
dependencies = [
"bitflags 1.3.2",
"gdk-pixbuf",
"gdk4",
"gio",
"glib",
"gtk4",
"libadwaita-sys",
"libc",
"pango",
]
[[package]]
name = "libadwaita-sys"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4231cb2499a9f0c4cdfa4885414b33e39901ddcac61150bc0bb4ff8a57ede404"
dependencies = [
"gdk4-sys",
"gio-sys",
"glib-sys",
"gobject-sys",
"gtk4-sys",
"libc",
"pango-sys",
"system-deps",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.147" version = "0.2.147"

View File

@ -6,6 +6,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
adw = { package = "libadwaita", version = "0.4", features = ["v1_3"] }
cairo-rs = { version = "0.17" } cairo-rs = { version = "0.17" }
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
fluent-ergonomics = { path = "../fluent-ergonomics/" } fluent-ergonomics = { path = "../fluent-ergonomics/" }

View File

@ -0,0 +1,2 @@
mod pie_chart;
pub use pie_chart::{Color, PieChart, Wedge};

View File

@ -0,0 +1,100 @@
use cairo::{Context, Format, ImageSurface};
use std::f64::consts::PI;
#[derive(Clone, Debug)]
pub struct Color {
pub r: f64,
pub g: f64,
pub b: f64,
}
#[derive(Clone, Debug)]
pub struct Wedge {
pub start_angle: f64,
pub end_angle: f64,
pub color: Color,
}
pub struct PieChart {
rotation: f64,
wedges: Vec<Wedge>,
width: i32,
height: i32,
}
impl PieChart {
pub fn new() -> Self {
Self {
rotation: 0.,
wedges: vec![],
width: 0,
height: 0,
}
}
pub fn rotation(mut self, rotation: f64) -> Self {
self.rotation = rotation;
self
}
pub fn wedges(mut self, wedge: impl Iterator<Item = Wedge>) -> Self {
let mut wedges: Vec<Wedge> = wedge.collect();
self.wedges.append(&mut wedges);
self
}
pub fn width(mut self, width: i32) -> Self {
self.width = width;
self
}
pub fn height(mut self, height: i32) -> Self {
self.height = height;
self
}
pub fn draw(self, context: &Context) {
println!("drawing: {} {}", self.width, self.height);
let radius = self.width.min(self.height) as f64 / 2.;
let center_x = (self.width / 2) as f64;
let center_y = (self.height / 2) as f64;
context.set_source_rgba(0., 0., 0., 0.);
let _ = context.paint();
context.set_line_width(2.);
self.wedges.iter().for_each(
|Wedge {
start_angle,
end_angle,
color,
}| {
println!(
"wedge: {:.02?} {:.02?} {:.02?}",
start_angle, end_angle, color
);
println!(
"wedge: {:.02?} {:.02?} [{:.02?}]",
start_angle + self.rotation,
end_angle + self.rotation,
self.rotation
);
context.move_to(center_x, center_y);
context.set_source_rgb(color.r, color.g, color.b);
context.arc(
center_x,
center_y,
radius,
start_angle + self.rotation,
end_angle + self.rotation,
);
let _ = context.fill();
},
);
context.set_source_rgb(1., 1., 1.);
context.arc(center_x, center_y, radius, 0., 2. * PI);
let _ = context.stroke();
}
}

View File

@ -5,18 +5,23 @@ extern crate serde_derive;
extern crate lazy_static; extern crate lazy_static;
*/ */
use chrono::Datelike; use chrono::{Datelike, Duration, NaiveTime};
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*, Orientation};
use ifc::IFC;
use std::{cell::RefCell, env, f64::consts::PI, ops::Deref, rc::Rc};
mod drawing;
use drawing::{Color, PieChart, Wedge};
/* /*
use geo_types::{Latitude, Longitude}; use geo_types::{Latitude, Longitude};
use ifc; */
use std::sync::Arc;
mod soluna_client; mod soluna_client;
use soluna_client::SolunaClient; use soluna_client::{LunarPhase, SunMoon};
/*
mod solstices; mod solstices;
use solstices::EVENTS; use solstices::EVENTS;
*/ */
@ -185,8 +190,14 @@ glib::wrapper! {
impl Date { impl Date {
fn new() -> Self { fn new() -> Self {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.add_css_class("card");
s.add_css_class("activatable");
s.set_margin_bottom(8);
s.set_margin_top(8);
s.set_margin_start(8);
s.set_margin_end(8);
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( s.append(&gtk::Label::new(Some(
format!( format!(
"{:?}, {:?} {}, {}", "{:?}, {:?} {}, {}",
@ -201,17 +212,91 @@ impl Date {
} }
} }
pub struct TransitClockPrivate {
info: Rc<RefCell<Option<SunMoon>>>,
}
impl Default for TransitClockPrivate {
fn default() -> Self {
Self {
info: Rc::new(RefCell::new(None)),
}
}
}
#[glib::object_subclass]
impl ObjectSubclass for TransitClockPrivate {
const NAME: &'static str = "TransitClock";
type Type = TransitClock;
type ParentType = gtk::DrawingArea;
}
impl ObjectImpl for TransitClockPrivate {}
impl WidgetImpl for TransitClockPrivate {}
impl DrawingAreaImpl for TransitClockPrivate {}
glib::wrapper! {
pub struct TransitClock(ObjectSubclass<TransitClockPrivate>) @extends gtk::DrawingArea, gtk::Widget;
}
impl TransitClock {
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 center_x = width as f64 / 2.;
let center_y = height as f64 / 2.;
let radius = width.min(height) as f64 / 2. * 0.9;
if let Some(ref info) = *s.imp().info.borrow() {
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();
PieChart::new()
.width(width)
.height(height)
.rotation(-PI / 2.)
.wedges(
vec![Wedge {
start_angle: (PI * 2.) * sunset.num_seconds() as f64 / full_day,
end_angle: (PI * 2.) * sunrise.num_seconds() as f64 / full_day,
color: Color {
r: 0.1,
g: 0.1,
b: 0.8,
},
}]
.into_iter(),
)
.draw(context);
(0..24).for_each(|tick| {
context.set_source_rgb(0., 0., 0.);
context.translate(center_x, center_y);
context.rotate(tick as f64 * (PI / 12.));
context.move_to(radius, 0.);
context.line_to(radius - 10., 0.);
let _ = context.stroke();
context.identity_matrix();
});
}
}
});
s
}
}
pub fn main() { pub fn main() {
/*
let runtime = Arc::new(
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap(),
);
*/
let app = gtk::Application::builder() let app = gtk::Application::builder()
.application_id("com.luminescent-dreams.dashboard") .application_id("com.luminescent-dreams.dashboard")
.resource_base_path("/com/luminescent-dreams/dashboard")
.build(); .build();
app.connect_activate( app.connect_activate(
@ -220,12 +305,38 @@ pub fn main() {
let window = gtk::ApplicationWindow::new(app); let window = gtk::ApplicationWindow::new(app);
window.present(); window.present();
let layout = gtk::Box::builder()
.orientation(Orientation::Vertical)
.hexpand(true)
.vexpand(true)
.build();
let date = Date::new(); let date = Date::new();
window.set_child(Some(&date)); layout.append(&date);
let button = gtk::Button::builder()
.label("Text")
.margin_bottom(8)
.margin_top(8)
.margin_start(8)
.margin_end(8)
.build();
layout.append(&button);
let day = TransitClock::new(SunMoon {
sunrise: NaiveTime::from_hms_opt(7, 0, 0).unwrap(),
sunset: NaiveTime::from_hms_opt(22, 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(&day);
window.set_child(Some(&layout));
}, },
); );
app.run(); let args: Vec<String> = env::args().collect();
ApplicationExtManual::run_with_args(&app, &args);
/* /*
let now = Local::now(); let now = Local::now();

View File

@ -1,12 +1,11 @@
// 41.78, -71.41 // 41.78, -71.41
// https://api.solunar.org/solunar/41.78,-71.41,20211029,-4 // https://api.solunar.org/solunar/41.78,-71.41,20211029,-4
use chrono::{ use chrono::{DateTime, Duration, NaiveTime, Offset, TimeZone, Utc};
DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike, Utc,
};
use geo_types::{Latitude, Longitude}; use geo_types::{Latitude, Longitude};
use memorycache::MemoryCache; use memorycache::MemoryCache;
use reqwest; use reqwest;
use serde::Deserialize;
const ENDPOINT: &str = "https://api.solunar.org/solunar"; const ENDPOINT: &str = "https://api.solunar.org/solunar";

View File

@ -51,16 +51,16 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1688392541, "lastModified": 1691421349,
"narHash": "sha256-lHrKvEkCPTUO+7tPfjIcb7Trk6k31rz18vkyqmkeJfY=", "narHash": "sha256-RRJyX0CUrs4uW4gMhd/X4rcDG8PTgaaCQM5rXEJOx6g=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "ea4c80b39be4c09702b0cb3b42eab59e2ba4f24b", "rev": "011567f35433879aae5024fc6ec53f2a0568a6c4",
"type": "github" "type": "github"
}, },
"original": { "original": {
"id": "nixpkgs", "id": "nixpkgs",
"ref": "nixos-22.11", "ref": "nixos-23.05",
"type": "indirect" "type": "indirect"
} }
}, },

View File

@ -2,7 +2,7 @@
description = "Lumenescent Dreams Tools"; description = "Lumenescent Dreams Tools";
inputs = { inputs = {
nixpkgs.url = "nixpkgs/nixos-22.11"; nixpkgs.url = "nixpkgs/nixos-23.05";
unstable.url = "nixpkgs/nixos-unstable"; unstable.url = "nixpkgs/nixos-unstable";
pkgs-cargo2nix.url = "github:cargo2nix/cargo2nix"; pkgs-cargo2nix.url = "github:cargo2nix/cargo2nix";
typeshare.url = "github:1Password/typeshare"; typeshare.url = "github:1Password/typeshare";