Extract an application window and encapsulate the entire UI

This commit is contained in:
Savanni D'Gerinel 2023-08-09 13:54:56 -04:00
parent e2d3875587
commit 5e2b7fbc99
4 changed files with 70 additions and 32 deletions

View File

@ -0,0 +1,44 @@
use crate::{
components::{Date, TransitClock},
types::State,
};
use gtk::prelude::*;
#[derive(Clone)]
pub struct ApplicationWindow {
pub window: gtk::ApplicationWindow,
pub date_label: Date,
pub transit_clock: TransitClock,
}
impl ApplicationWindow {
pub fn new(app: &gtk::Application) -> Self {
let window = gtk::ApplicationWindow::new(app);
let date_label = Date::new();
let transit_clock = TransitClock::new();
let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.hexpand(true)
.vexpand(true)
.build();
layout.append(&date_label);
layout.append(&transit_clock);
window.set_child(Some(&layout));
Self {
window,
date_label,
transit_clock,
}
}
pub fn update_state(&self, state: State) {
self.date_label.update_date(state.date);
if let Some(transit) = state.transit {
self.transit_clock.update_transit(transit);
}
}
}

View File

@ -8,6 +8,9 @@ use std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
mod app_window;
use app_window::ApplicationWindow;
mod components; mod components;
use components::{Date, TransitClock}; use components::{Date, TransitClock};
@ -16,11 +19,13 @@ mod drawing;
mod soluna_client; mod soluna_client;
use soluna_client::{SolunaClient, SunMoon}; use soluna_client::{SolunaClient, SunMoon};
/*
mod solstices; mod solstices;
use solstices::EVENTS; use solstices::EVENTS;
*/
mod types;
use types::State;
/*
const EO_TEXT: &'static str = " const EO_TEXT: &'static str = "
day = {$day -> day = {$day ->
*[Sunday] Dimanĉo *[Sunday] Dimanĉo
@ -53,18 +58,13 @@ summer_solstice = Somera Solstico
autumn_equinox = Aŭtuna Ekvinokso autumn_equinox = Aŭtuna Ekvinokso
winter_solstice = Vintra Solstico winter_solstice = Vintra Solstico
"; ";
*/
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Message { pub enum Message {
Refresh(State), Refresh(State),
} }
#[derive(Clone, Debug)]
pub struct State {
date: IFC,
transit: Option<SunMoon>,
}
#[derive(Clone)] #[derive(Clone)]
pub struct Core { pub struct Core {
tx: Arc<RwLock<Option<Sender<Message>>>>, tx: Arc<RwLock<Option<Sender<Message>>>>,
@ -118,31 +118,17 @@ pub fn main() {
*core.tx.write().unwrap() = Some(gtk_tx); *core.tx.write().unwrap() = Some(gtk_tx);
let window = gtk::ApplicationWindow::new(app); let window = ApplicationWindow::new(app);
window.present(); window.window.present();
let layout = gtk::Box::builder() gtk_rx.attach(None, {
.orientation(Orientation::Vertical) let window = window.clone();
.hexpand(true) move |msg| {
.vexpand(true) let Message::Refresh(state) = msg;
.build(); ApplicationWindow::update_state(&window, state);
let date_label = Date::new();
layout.append(&date_label);
let transit_clock = TransitClock::new(); Continue(true)
layout.append(&transit_clock);
window.set_child(Some(&layout));
gtk_rx.attach(None, move |msg| {
let Message::Refresh(state) = msg;
println!("new state: {:?}", state);
date_label.update_date(state.date);
if let Some(transit) = state.transit {
transit_clock.update_transit(transit);
} }
Continue(true)
}); });
std::thread::spawn(move || {}); std::thread::spawn(move || {});

View File

@ -1,7 +1,7 @@
use chrono; use chrono;
use chrono::prelude::*; use chrono::prelude::*;
use lazy_static::lazy_static;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use serde_json;
use std::collections::HashMap; use std::collections::HashMap;
// http://astropixels.com/ephemeris/soleq2001.html // http://astropixels.com/ephemeris/soleq2001.html
@ -133,7 +133,7 @@ fn parse_events() -> Vec<Option<YearlyEvents>> {
pub struct Solstices(HashMap<i32, YearlyEvents>); pub struct Solstices(HashMap<i32, YearlyEvents>);
impl Solstices { impl Solstices {
pub fn akiru(&self, year: i32) -> Option<YearlyEvents> { pub fn acquire(&self, year: i32) -> Option<YearlyEvents> {
self.0.get(&year).map(|c| c.clone()) self.0.get(&year).map(|c| c.clone())
} }

8
dashboard/src/types.rs Normal file
View File

@ -0,0 +1,8 @@
use crate::soluna_client::SunMoon;
use ifc::IFC;
#[derive(Clone, Debug)]
pub struct State {
pub date: IFC,
pub transit: Option<SunMoon>,
}