aboutsummaryrefslogtreecommitdiff
path: root/karlgui/src/main.rs
blob: a85459e8781ad95c296ab6a6e5303ff7d8c61e30 (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
pub mod client;
pub mod edit;
pub mod helper;

use crate::client::Client;
use edit::ShowOrEdit;
use eframe::CreationContext;
use egui::CentralPanel;
use karlcommon::{socket_path, ClientboundPacket, ServerboundPacket, Task};
use log::{error, info};
use std::{os::unix::net::UnixStream, process::exit};

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

struct App {
    client: Client,
    tasks: Vec<ShowOrEdit<Task>>,
}

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

        let socket = match UnixStream::connect(socket_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 {
            client,
            tasks: vec![],
        }
    }

    pub fn update_network(&mut self) {
        for p in self.client.receiver.try_iter() {
            match p {
                ClientboundPacket::TaskList(t) => {
                    self.tasks = t.into_iter().map(|t| ShowOrEdit::new(t, false)).collect()
                }
                ClientboundPacket::Sync => {
                    self.client.busy = false;
                }
                _ => {}
            }
        }
    }
}

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        self.update_network();
        CentralPanel::default().show(ctx, |ui| {
            ui.add_enabled_ui(!self.client.busy, |ui| {
                for t in &mut self.tasks {
                    if t.changed(ui) {
                        self.client
                            .send_sync(ServerboundPacket::UpdateTask(t.inner.clone()))
                    }
                    ui.separator();
                }
            });
        });
    }
}