diff --git a/app/Cargo.lock b/app/Cargo.lock index d24e9c4..d6db58e 100644 --- a/app/Cargo.lock +++ b/app/Cargo.lock @@ -1106,6 +1106,26 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "enum-iterator" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7add3873b5dd076766ee79c8e406ad1a472c385476b9e38849f8eec24f1be689" +dependencies = [ + "enum-iterator-derive", +] + +[[package]] +name = "enum-iterator-derive" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eecf8589574ce9b895052fa12d69af7a233f99e6107f5cb8dd1044f2a17bfdcb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.28", +] + [[package]] name = "enumflags2" version = "0.7.7" @@ -3597,6 +3617,7 @@ dependencies = [ "egui", "egui-wgpu", "egui-winit", + "enum-iterator", "geocoding", "human-repr", "image", diff --git a/app/Cargo.toml b/app/Cargo.toml index e1a9c4e..9d56250 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -28,6 +28,7 @@ dirs = "5.0.1" egui = { version = "0.22.0" } egui-wgpu = { version = "0.22.0", features = ["winit"] } egui-winit = { version = "0.22.0" } +enum-iterator = "1.4.1" geocoding = "0.2.0" human-repr = "1.1.0" image = "0.24.7" diff --git a/app/src/controller.rs b/app/src/controller.rs index cdfaf4b..0da4650 100644 --- a/app/src/controller.rs +++ b/app/src/controller.rs @@ -13,12 +13,11 @@ pub enum Message { Enable, Disable, Refresh, - RegisterListener(mpsc::Sender<()>), } pub struct BrightnessController { pub sender: mpsc::Sender, - last_result: Arc>>, + pub last_result: Arc>>, join_handle: Option>, } @@ -53,7 +52,6 @@ fn run( ) { log::info!("Starting BrightnessController"); let mut enabled = true; - let mut listeners = Vec::new(); loop { // Apply brightness using latest config @@ -98,9 +96,6 @@ fn run( Ok(Message::Refresh) => { log::info!("Refreshing due to signal"); } - Ok(Message::RegisterListener(listener)) => { - listeners.push(listener); - } Err(RecvTimeoutError::Timeout) => { log::debug!("Refreshing due to timeout") } diff --git a/app/src/gui/app.rs b/app/src/gui/app.rs index 0a45d52..b97abba 100644 --- a/app/src/gui/app.rs +++ b/app/src/gui/app.rs @@ -1,15 +1,18 @@ +use std::sync::{Arc, RwLock}; use crate::common::UserEvent; use egui::{Align, Color32, Layout, ScrollArea}; use egui_winit::winit::event_loop::EventLoopProxy; +use enum_iterator::Sequence; +use crate::apply::ApplyResults; pub struct SsbEguiApp { main_loop: EventLoopProxy, - age: u32, selected_page: Page, overlay: Option, + results: Arc>>, } -#[derive(Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Sequence)] enum Page { Status, BrightnessSettings, @@ -17,17 +20,29 @@ enum Page { Monitors, } +impl Page { + + fn title(self) -> &'static str { + match self { + Page::Status => "Status", + Page::BrightnessSettings => "Brightness Settings", + Page::LocationSettings => "Location Settings", + Page::Monitors => "Monitors" + } + } +} + enum OverlayWindow { ExitModal, } impl SsbEguiApp { - pub fn new(main_loop: EventLoopProxy) -> Self { + pub fn new(main_loop: EventLoopProxy, results: Arc>>) -> Self { SsbEguiApp { main_loop, - age: 0, selected_page: Page::Status, overlay: None, + results, } } @@ -42,11 +57,7 @@ impl SsbEguiApp { }); egui::CentralPanel::default().show(ctx, |ui| { ui.set_enabled(self.overlay.is_none()); - ui.heading("Hello World!"); - if ui.button("Click each year").clicked() { - self.age += 1; - } - ui.label(format!("age {}", self.age)); + self.render_main(ui); }); } @@ -72,21 +83,30 @@ impl SsbEguiApp { } } + fn render_main(&mut self, ui: &mut egui::Ui) { + ui.heading(self.selected_page.title()); + match self.selected_page { + Page::Status => { + let results = self.results.read().unwrap(); + + if let Some(results) = results.as_ref() { + ui.label(format!("{} monitors", results.monitors.len())); + } + + } + Page::BrightnessSettings => {} + Page::LocationSettings => {} + Page::Monitors => {} + } + } + fn render_menu_panel(&mut self, ui: &mut egui::Ui) { ScrollArea::vertical().show(ui, |ui| { ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| { - ui.selectable_value(&mut self.selected_page, Page::Status, "Status"); - ui.selectable_value( - &mut self.selected_page, - Page::BrightnessSettings, - "Brightness Settings", - ); - ui.selectable_value( - &mut self.selected_page, - Page::LocationSettings, - "Location Settings", - ); - ui.selectable_value(&mut self.selected_page, Page::Monitors, "Monitors"); + + enum_iterator::all::().for_each(|page| { + ui.selectable_value(&mut self.selected_page, page, page.title()); + }); ui.separator(); diff --git a/app/src/gui/mod.rs b/app/src/gui/mod.rs index f048079..056bae6 100644 --- a/app/src/gui/mod.rs +++ b/app/src/gui/mod.rs @@ -8,8 +8,10 @@ use egui_winit::winit; use egui_winit::winit::event::{Event, WindowEvent}; use egui_winit::winit::event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget}; use egui_winit::winit::window::Icon; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, RwLock}; use std::time::Instant; +use crate::apply::ApplyResults; +use crate::controller::BrightnessController; #[derive(Debug)] pub enum NextPaint { @@ -44,16 +46,18 @@ pub struct WgpuWinitApp { repaint_proxy: EventLoopProxy, running: Option, icon: Icon, + results: Arc>> } impl WgpuWinitApp { - pub fn new(event_loop: &EventLoop) -> Self { + pub fn new(event_loop: &EventLoop, controller: &BrightnessController) -> Self { let (buf, info) = read_icon(); let icon = Icon::from_rgba(buf, info.width, info.height).unwrap(); Self { repaint_proxy: event_loop.create_proxy(), running: None, icon, + results: controller.last_result.clone(), } } @@ -110,7 +114,7 @@ impl WgpuWinitApp { self.running = Some(WgpuWinitRunning { painter, - app: SsbEguiApp::new(self.repaint_proxy.clone()), + app: SsbEguiApp::new(self.repaint_proxy.clone(), self.results.clone()), window, follow_system_theme: system_theme.is_some(), egui_ctx, diff --git a/app/src/main.rs b/app/src/main.rs index b7ffb57..78af7a0 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -45,7 +45,7 @@ fn run() -> anyhow::Result<()> { let _event_watcher = EventWatcher::start(&controller, Some(&event_loop)); let _tray = tray::create(&event_loop)?; - let mut framework = WgpuWinitApp::new(&event_loop); + let mut framework = WgpuWinitApp::new(&event_loop, &controller); let mut returned_result = Ok(()); let mut next_repaint_time = Some(Instant::now());