aboutsummaryrefslogtreecommitdiff
path: root/karlgui/src/main.rs
blob: 60444969987c282fcb9f52b9a898143d893ae6d6 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
pub mod client;
pub mod globals;
pub mod helper;
pub mod views;

use crate::{client::Client, globals::Globals};
use eframe::CreationContext;
use egui::CentralPanel;
use karlcommon::{interfaces::unix_path, ServerboundPacket};
use log::{error, info};
use std::{os::unix::net::UnixStream, process::exit};
use views::{calendar::Calendar, edit::ShowAndEdit};

fn main() {
    env_logger::init();
    eframe::run_native(
        "karlender",
        eframe::NativeOptions::default(),
        Box::new(move |cc| Box::new(App::new(cc))),
    )
}

struct App {
    g: Globals,

    current_tab: Tab,

    show_and_edit: ShowAndEdit,
    calendar: Calendar,
}

#[derive(PartialEq)]
enum Tab {
    ShowAndEdit,
    CalendarWeek,
}

impl App {
    pub fn new(cc: &CreationContext) -> Self {
        cc.egui_ctx.set_visuals(egui::Visuals::dark());

        let socket = match UnixStream::connect(unix_path()) {
            Ok(s) => s,
            Err(e) => {
                error!("failed to connect to socket: {}", e);
                exit(1)
            }
        };

        let mut client = Client::new(socket);
        info!("connected");

        client.send(ServerboundPacket::ListTasks);
        App {
            current_tab: Tab::CalendarWeek,
            g: Globals::new(client),
            show_and_edit: Default::default(),
            calendar: Default::default(),
        }
    }
}

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        self.g.update_network();
        CentralPanel::default().show(ctx, |ui| {
            ui.add_enabled_ui(!self.g.client.busy, |ui| {
                ui.horizontal(|ui| {
                    ui.selectable_value(&mut self.current_tab, Tab::ShowAndEdit, "Tasks");
                    ui.selectable_value(&mut self.current_tab, Tab::CalendarWeek, "Calendar: Week");
                });
                ui.separator();
                match self.current_tab {
                    Tab::ShowAndEdit => self.show_and_edit.ui(ui, &mut self.g),
                    Tab::CalendarWeek => self.calendar.ui(ui, &mut self.g),
                }
            });
        });
    }
}