pub mod client; pub mod helper; pub mod views; use crate::client::Client; 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}; 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))), ) } pub struct Globals { client: Client, tasks: Vec, } 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(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 { current_tab: Tab::ShowAndEdit, g: Globals { client, tasks: vec![], }, show_and_edit: Default::default(), calendar: Default::default(), } } pub fn update_network(&mut self) { for p in self.g.client.receiver.try_iter() { match p { ClientboundPacket::TaskList(t) => self.g.tasks = t, ClientboundPacket::Sync => { self.g.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.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), } }); }); } }