From 99af88a840bef534540a4b273d24a8a17e7fc9b9 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Jan 2022 13:12:45 +0100 Subject: Split app into modules --- src/app/app.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/app/app.rs (limited to 'src/app/app.rs') diff --git a/src/app/app.rs b/src/app/app.rs new file mode 100644 index 0000000..45904a9 --- /dev/null +++ b/src/app/app.rs @@ -0,0 +1,57 @@ +use gtk4 as gtk; + +use async_channel::Sender; +use chrono::{Datelike, NaiveDate, Weekday}; +use gtk::glib::signal::Inhibit; +use gtk::prelude::*; +use rusqlite::Connection; +use std::rc::Rc; + +use crate::app::calendar; +use crate::app::update::Msg; +use crate::{db, model::event::Event}; + +pub struct App { + pub window: Rc, + pub grid: gtk::Grid, + pub events: Vec, + pub today: NaiveDate, + pub start_date: NaiveDate, +} + +impl App { + pub fn new(conn: Rc, app: >k::Application, tx: Sender) -> Self { + let window = Rc::new( + gtk::ApplicationWindow::builder() + .application(app) + .title("Calendar") + .default_width(800) + .default_height(600) + .visible(true) + .build(), + ); + + let today = chrono::offset::Local::today().naive_utc(); + let start_date = + NaiveDate::from_isoywd(today.year(), today.iso_week().week(), Weekday::Mon); + let events = db::list(&conn).unwrap_or(vec![]); + let grid = calendar::grid(tx, &today, &start_date, &events); + + window.set_child(Some(&grid)); + + window.connect_close_request(move |window| { + if let Some(application) = window.application() { + application.remove_window(window); + } + Inhibit(false) + }); + + Self { + window, + grid, + events, + today, + start_date, + } + } +} -- cgit v1.2.3