From 3e13cbcdaaeb9225128bb5d8e9cc71ba5f13b1ac Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 10 Aug 2023 10:13:57 -0400 Subject: [PATCH 1/3] Add the transit times to a card --- dashboard/src/app_window.rs | 8 ++- dashboard/src/components/mod.rs | 3 + dashboard/src/components/transit_card.rs | 73 ++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 dashboard/src/components/transit_card.rs diff --git a/dashboard/src/app_window.rs b/dashboard/src/app_window.rs index 7051687..0f877ab 100644 --- a/dashboard/src/app_window.rs +++ b/dashboard/src/app_window.rs @@ -1,5 +1,5 @@ use crate::{ - components::{Date, TransitClock}, + components::{Date, TransitCard, TransitClock}, types::State, }; use adw::prelude::AdwApplicationWindowExt; @@ -11,6 +11,7 @@ pub struct ApplicationWindow { pub window: adw::ApplicationWindow, pub date_label: Date, pub next_event: gtk::Label, + pub transit_card: TransitCard, pub transit_clock: TransitClock, } @@ -52,6 +53,9 @@ impl ApplicationWindow { next_event.add_css_class("activatable"); layout.append(&next_event); + let transit_card = TransitCard::new(); + layout.append(&transit_card); + let transit_clock = TransitClock::new(); layout.append(&transit_clock); @@ -61,6 +65,7 @@ impl ApplicationWindow { window, date_label, next_event, + transit_card, transit_clock, } } @@ -69,6 +74,7 @@ impl ApplicationWindow { self.date_label.update_date(state.date); self.next_event.set_text(&format!("{:?}", state.next_event)); if let Some(transit) = state.transit { + self.transit_card.update_transit(&transit); self.transit_clock.update_transit(transit); } } diff --git a/dashboard/src/components/mod.rs b/dashboard/src/components/mod.rs index a011020..2abc253 100644 --- a/dashboard/src/components/mod.rs +++ b/dashboard/src/components/mod.rs @@ -1,5 +1,8 @@ mod date; pub use date::Date; +mod transit_card; +pub use transit_card::TransitCard; + mod transit_clock; pub use transit_clock::TransitClock; diff --git a/dashboard/src/components/transit_card.rs b/dashboard/src/components/transit_card.rs new file mode 100644 index 0000000..565fd4c --- /dev/null +++ b/dashboard/src/components/transit_card.rs @@ -0,0 +1,73 @@ +use crate::soluna_client::SunMoon; +use glib::Object; +use gtk::{prelude::*, subclass::prelude::*}; +use std::{cell::RefCell, rc::Rc}; + +pub struct TransitCardPrivate { + sunrise: gtk::Label, + sunset: gtk::Label, + moonrise: gtk::Label, + moonset: gtk::Label, +} + +impl Default for TransitCardPrivate { + fn default() -> Self { + Self { + sunrise: gtk::Label::new(None), + sunset: gtk::Label::new(None), + moonrise: gtk::Label::new(None), + moonset: gtk::Label::new(None), + } + } +} + +#[glib::object_subclass] +impl ObjectSubclass for TransitCardPrivate { + const NAME: &'static str = "TransitCard"; + type Type = TransitCard; + type ParentType = gtk::Grid; +} + +impl ObjectImpl for TransitCardPrivate {} +impl WidgetImpl for TransitCardPrivate {} +impl GridImpl for TransitCardPrivate {} + +glib::wrapper! { + pub struct TransitCard(ObjectSubclass) @extends gtk::Grid, gtk::Widget; +} + +impl TransitCard { + pub fn new() -> Self { + let s: Self = Object::builder().build(); + s.add_css_class("card"); + s.set_column_homogeneous(true); + + s.attach(&s.imp().sunrise, 0, 0, 1, 1); + s.attach(&s.imp().sunset, 0, 1, 1, 1); + s.attach(&s.imp().moonrise, 1, 0, 1, 1); + s.attach(&s.imp().moonset, 1, 1, 1, 1); + + s + } + + pub fn update_transit(&self, transit_info: &SunMoon) { + self.imp() + .sunrise + .set_text(format!("{}", transit_info.sunrise.format("%H:%M")).as_ref()); + self.imp() + .sunset + .set_text(format!("{}", transit_info.sunset.format("%H:%M")).as_ref()); + self.imp().moonrise.set_text( + &transit_info + .moonrise + .map(|time| format!("{}", time.format("%H:%M"))) + .unwrap_or("".to_owned()), + ); + self.imp().moonset.set_text( + &transit_info + .moonset + .map(|time| format!("{}", time.format("%H:%M"))) + .unwrap_or("".to_owned()), + ); + } +} -- 2.44.1 From 8e36091a198f0a531f38b41f4b1ce33cbc6b3fb3 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 10 Aug 2023 11:09:27 -0400 Subject: [PATCH 2/3] styling --- dashboard/src/app_window.rs | 2 -- dashboard/src/components/date.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/dashboard/src/app_window.rs b/dashboard/src/app_window.rs index 0f877ab..fd15ca2 100644 --- a/dashboard/src/app_window.rs +++ b/dashboard/src/app_window.rs @@ -49,8 +49,6 @@ impl ApplicationWindow { .margin_start(8) .margin_end(8) .build(); - next_event.add_css_class("card"); - next_event.add_css_class("activatable"); layout.append(&next_event); let transit_card = TransitCard::new(); diff --git a/dashboard/src/components/date.rs b/dashboard/src/components/date.rs index 3df0e39..f8698e4 100644 --- a/dashboard/src/components/date.rs +++ b/dashboard/src/components/date.rs @@ -38,8 +38,6 @@ glib::wrapper! { impl Date { pub fn new() -> Self { 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); -- 2.44.1 From 1f2c7b80020e3d935134f8e111e889ce44f4701f Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 10 Aug 2023 11:52:44 -0400 Subject: [PATCH 3/3] Add icons to the rise and set times --- dashboard/src/components/label.rs | 58 ++++++++++++++++++++++++ dashboard/src/components/mod.rs | 3 ++ dashboard/src/components/transit_card.rs | 21 ++++----- 3 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 dashboard/src/components/label.rs diff --git a/dashboard/src/components/label.rs b/dashboard/src/components/label.rs new file mode 100644 index 0000000..b793ccc --- /dev/null +++ b/dashboard/src/components/label.rs @@ -0,0 +1,58 @@ +use crate::soluna_client::SunMoon; +use glib::Object; +use gtk::{prelude::*, subclass::prelude::*, IconLookupFlags}; + +#[derive(Default)] +pub struct LabelPrivate { + label: gtk::Label, + icon: gtk::Image, +} + +#[glib::object_subclass] +impl ObjectSubclass for LabelPrivate { + const NAME: &'static str = "Label"; + type Type = Label; + type ParentType = gtk::Box; +} + +impl ObjectImpl for LabelPrivate {} +impl WidgetImpl for LabelPrivate {} +impl BoxImpl for LabelPrivate {} + +glib::wrapper! { + pub struct Label(ObjectSubclass) @extends gtk::Box, gtk::Widget, + @implements gtk::Orientable; +} + +impl Label { + pub fn new(text: Option<&str>, icon: Option) -> Self { + let s: Self = Object::builder().build(); + s.set_orientation(gtk::Orientation::Horizontal); + s.set_spacing(8); + s.set_margin_bottom(8); + s.set_margin_top(8); + s.set_margin_start(8); + s.set_margin_end(8); + + s.append(&s.imp().icon); + s.append(&s.imp().label); + + if let Some(text) = text { + s.set_text(text); + } + + if let Some(icon) = icon { + s.set_icon(icon); + } + + s + } + + pub fn set_text(&self, text: &str) { + self.imp().label.set_text(text); + } + + pub fn set_icon(&self, icon: gio::ThemedIcon) { + self.imp().icon.set_from_gicon(&icon); + } +} diff --git a/dashboard/src/components/mod.rs b/dashboard/src/components/mod.rs index 2abc253..02a5aa4 100644 --- a/dashboard/src/components/mod.rs +++ b/dashboard/src/components/mod.rs @@ -1,6 +1,9 @@ mod date; pub use date::Date; +mod label; +pub use label::Label; + mod transit_card; pub use transit_card::TransitCard; diff --git a/dashboard/src/components/transit_card.rs b/dashboard/src/components/transit_card.rs index 565fd4c..27334e7 100644 --- a/dashboard/src/components/transit_card.rs +++ b/dashboard/src/components/transit_card.rs @@ -1,22 +1,21 @@ -use crate::soluna_client::SunMoon; +use crate::{components::Label, soluna_client::SunMoon}; use glib::Object; -use gtk::{prelude::*, subclass::prelude::*}; -use std::{cell::RefCell, rc::Rc}; +use gtk::{prelude::*, subclass::prelude::*, IconLookupFlags}; pub struct TransitCardPrivate { - sunrise: gtk::Label, - sunset: gtk::Label, - moonrise: gtk::Label, - moonset: gtk::Label, + sunrise: Label, + sunset: Label, + moonrise: Label, + moonset: Label, } impl Default for TransitCardPrivate { fn default() -> Self { Self { - sunrise: gtk::Label::new(None), - sunset: gtk::Label::new(None), - moonrise: gtk::Label::new(None), - moonset: gtk::Label::new(None), + sunrise: Label::new(None, Some(gio::ThemedIcon::new("daytime-sunrise-symbolic"))), + sunset: Label::new(None, Some(gio::ThemedIcon::new("daytime-sunset-symbolic"))), + moonrise: Label::new(None, Some(gio::ThemedIcon::new("moon-outline-symbolic"))), + moonset: Label::new(None, Some(gio::ThemedIcon::new("moon-outline-symbolic"))), } } } -- 2.44.1