aboutsummaryrefslogtreecommitdiff
path: root/src/gui/mod.rs
blob: 078c4b23b46c7fd7660f23bc5d19e4b0c357c506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
mod app;
mod calendar;
mod form;
mod update;
mod utils;

use gtk4 as gtk;

use gtk::gdk::Display;
use gtk4::prelude::{ApplicationExt, ApplicationExtManual};
use rusqlite::Connection;
use std::rc::Rc;

use app::App;

pub fn run(conn: Connection) {
    let conn = Rc::new(conn);
    let app = gtk::Application::new(Some("me.guyonvarch.calendar"), Default::default());
    app.connect_startup(|_| load_style());
    app.connect_activate(move |app| build_ui(conn.clone(), app));
    let args: [&str; 0] = [];
    app.run_with_args(&args);
}

fn build_ui(conn: Rc<Connection>, app: &gtk::Application) {
    let (tx, rx) = async_channel::unbounded();
    match App::new(conn, app, tx) {
        Ok(app) => utils::spawn(update::event_handler(rx, app)),
        Err(err) => {
            eprintln!("{}", err);
            std::process::exit(1)
        }
    }
}

fn load_style() {
    let provider = gtk::CssProvider::new();
    provider.load_from_data(include_str!("style.css"));
    gtk::style_context_add_provider_for_display(
        &Display::default().expect("Error initializing gtk css provider."),
        &provider,
        gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
    );
}