Skip to content

Commit

Permalink
ui progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-pro committed Dec 30, 2023
1 parent beab98b commit a4b33cc
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 36 deletions.
1 change: 1 addition & 0 deletions app/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl SsbConfig {
temp_file.write_all(serialised.as_bytes())?;
temp_file.flush()?;
temp_file.persist(&path)?;
log::debug!("Successfully saved config to {}", path.display());
Ok(())
}
}
Expand Down
133 changes: 113 additions & 20 deletions app/src/gui/app.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
use std::sync::{Arc, RwLock};
use crate::apply::ApplyResults;
use crate::common::UserEvent;
use crate::config::SsbConfig;
use crate::controller::Message;
use egui::{Align, Color32, Layout, ScrollArea};
use egui_winit::winit::event_loop::EventLoopProxy;
use enum_iterator::Sequence;
use crate::apply::ApplyResults;
use std::sync::mpsc::Sender;
use std::sync::{Arc, RwLock};

pub struct SsbEguiApp {
main_loop: EventLoopProxy<UserEvent>,
selected_page: Page,
overlay: Option<OverlayWindow>,
results: Arc<RwLock<Option<ApplyResults>>>,
brightness_settings_page: BrightnessSettings,
config: Arc<RwLock<SsbConfig>>,
controller: Sender<Message>,
}

struct BrightnessSettings {
brightness_day: u32,
brightness_night: u32,
transition_mins: u32,
}

impl BrightnessSettings {
fn copy_to_config(&self, config: &mut SsbConfig) {
config.brightness_night = self.brightness_night;
config.brightness_day = self.brightness_day;
config.transition_mins = self.transition_mins;
}
}

#[derive(Copy, Clone, Debug, PartialEq, Sequence)]
Expand All @@ -21,28 +41,41 @@ enum Page {
}

impl Page {

fn title(self) -> &'static str {
match self {
Page::Status => "Status",
Page::BrightnessSettings => "Brightness Settings",
Page::LocationSettings => "Location Settings",
Page::Monitors => "Monitors"
Page::Monitors => "Monitors",
}
}
}

enum OverlayWindow {
ExitModal,
ErrorMessage(String),
}

impl SsbEguiApp {
pub fn new(main_loop: EventLoopProxy<UserEvent>, results: Arc<RwLock<Option<ApplyResults>>>) -> Self {
pub fn new(
main_loop: EventLoopProxy<UserEvent>,
results: Arc<RwLock<Option<ApplyResults>>>,
config: Arc<RwLock<SsbConfig>>,
controller: Sender<Message>,
) -> Self {
let config_read = config.read().unwrap();
SsbEguiApp {
main_loop,
selected_page: Page::Status,
overlay: None,
results,
brightness_settings_page: BrightnessSettings {
brightness_day: config_read.brightness_day,
brightness_night: config_read.brightness_night,
transition_mins: config_read.transition_mins,
},
config: config.clone(),
controller,
}
}

Expand All @@ -62,7 +95,9 @@ impl SsbEguiApp {
}

fn render_overlay(&mut self, ctx: &egui::Context) {
let mut clear_overlay = false;
match &self.overlay {
None => {}
Some(OverlayWindow::ExitModal) => {
egui::Window::new("Are you sure?")
.collapsible(false)
Expand All @@ -79,31 +114,47 @@ impl SsbEguiApp {
});
});
}
None => {}
Some(OverlayWindow::ErrorMessage(msg)) => {
egui::Window::new("Error")
.collapsible(false)
.resizable(true)
.show(ctx, |ui| {
ui.label(msg);
if ui.button("Ok").clicked() {
clear_overlay = true;
};
});
}
}
if clear_overlay {
self.overlay = None;
}
}

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()));
ScrollArea::both().show(ui, |ui| {
ui.heading(self.selected_page.title());
ui.add_space(10.0);
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 => {
self.render_brightness_settings(ui);
}
Page::LocationSettings => {}
Page::Monitors => {}
}
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.with_layout(Layout::top_down_justified(Align::LEFT), |ui| {
enum_iterator::all::<Page>().for_each(|page| {
ui.selectable_value(&mut self.selected_page, page, page.title());
});
Expand All @@ -126,4 +177,46 @@ impl SsbEguiApp {
});
});
}

fn render_brightness_settings(&mut self, ui: &mut egui::Ui) {
egui::Grid::new("brightness_settings")
.num_columns(2)
.spacing([40.0, 4.0])
.striped(true)
.show(ui, |ui| {

ui.label("Day Brightness");
ui.add(egui::Slider::new(&mut self.brightness_settings_page.brightness_day, 0u32..=100u32).suffix("%"));
ui.end_row();

ui.label("Night Brightness");
ui.add(egui::Slider::new(&mut self.brightness_settings_page.brightness_night, 0u32..=100u32).suffix("%"));
ui.end_row();

ui.label("Transition Minutes").on_hover_text("How long it takes to transition between day and night brightness at sunset/sunrise");
ui.add(egui::Slider::new(&mut self.brightness_settings_page.transition_mins, 0u32..=360u32).suffix("min"));
ui.end_row();

});
ui.add_space(10.0);
ui.with_layout(Layout::left_to_right(Align::LEFT), |ui| {
if ui.button("Apply").clicked() {
let mut config = self.config.write().unwrap();
self.brightness_settings_page.copy_to_config(&mut config);
self.controller.send(Message::Refresh).unwrap();
}
if ui.button("Save").clicked() {
let mut config = self.config.write().unwrap();
self.brightness_settings_page.copy_to_config(&mut config);
self.controller.send(Message::Refresh).unwrap();
if let Err(e) = config.save() {
self.overlay = Some(OverlayWindow::ErrorMessage(format!(
"Unable to save config: {}",
e
)));
log::error!("Unable to save config: {:#}", e)
}
};
});
}
}
26 changes: 12 additions & 14 deletions app/src/gui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
mod app;
pub mod app;

use crate::common::UserEvent;
use crate::common::APP_NAME;
use crate::gui::app::SsbEguiApp;
use crate::tray::read_icon;
use egui_winit::winit;
use egui_winit::winit::event::{Event, WindowEvent};
use egui_winit::winit::event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget};
use egui_winit::winit::event_loop::{EventLoopProxy, EventLoopWindowTarget};
use egui_winit::winit::window::Icon;
use std::sync::{Arc, Mutex, RwLock};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use crate::apply::ApplyResults;
use crate::controller::BrightnessController;

#[derive(Debug)]
pub enum NextPaint {
Expand Down Expand Up @@ -42,22 +40,22 @@ impl Drop for WgpuWinitRunning {
}
}

pub struct WgpuWinitApp {
pub struct WgpuWinitApp<F> {
repaint_proxy: EventLoopProxy<UserEvent>,
running: Option<WgpuWinitRunning>,
icon: Icon,
results: Arc<RwLock<Option<ApplyResults>>>
app_factory: F,
}

impl WgpuWinitApp {
pub fn new(event_loop: &EventLoop<UserEvent>, controller: &BrightnessController) -> Self {
impl<F: Fn() -> SsbEguiApp> WgpuWinitApp<F> {
pub fn new(event_loop: EventLoopProxy<UserEvent>, app_factory: F) -> Self {
let (buf, info) = read_icon();
let icon = Icon::from_rgba(buf, info.width, info.height).unwrap();
Self {
repaint_proxy: event_loop.create_proxy(),
repaint_proxy: event_loop,
running: None,
icon,
results: controller.last_result.clone(),
app_factory,
}
}

Expand All @@ -71,8 +69,8 @@ impl WgpuWinitApp {
.with_transparent(false)
.with_title(APP_NAME)
.with_inner_size(winit::dpi::PhysicalSize {
width: 800,
height: 600,
width: 1024,
height: 768,
})
.with_window_icon(Some(self.icon.clone()))
.build(&event_loop)?;
Expand Down Expand Up @@ -114,7 +112,7 @@ impl WgpuWinitApp {

self.running = Some(WgpuWinitRunning {
painter,
app: SsbEguiApp::new(self.repaint_proxy.clone(), self.results.clone()),
app: (self.app_factory)(),
window,
follow_system_theme: system_theme.is_some(),
egui_ctx,
Expand Down
13 changes: 11 additions & 2 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::common::{install_logger, APP_NAME};
use crate::config::SsbConfig;
use crate::controller::BrightnessController;
use crate::event_watcher::EventWatcher;
use crate::gui::app::SsbEguiApp;
use crate::gui::{NextPaint, WgpuWinitApp};
use crate::unique::SsbUniqueInstance;
use common::UserEvent;
Expand All @@ -38,14 +39,22 @@ fn run() -> anyhow::Result<()> {
SsbConfig::default()
}
}));
let controller = BrightnessController::start(config);
let controller = BrightnessController::start(config.clone());

let mut event_loop = EventLoopBuilder::<UserEvent>::with_user_event().build();

let _event_watcher = EventWatcher::start(&controller, Some(&event_loop));
let _tray = tray::create(&event_loop)?;

let mut framework = WgpuWinitApp::new(&event_loop, &controller);
let proxy2 = event_loop.create_proxy();
let mut framework = WgpuWinitApp::new(event_loop.create_proxy(), move || {
SsbEguiApp::new(
proxy2.clone(),
controller.last_result.clone(),
config.clone(),
controller.sender.clone(),
)
});

let mut returned_result = Ok(());
let mut next_repaint_time = Some(Instant::now());
Expand Down

0 comments on commit a4b33cc

Please sign in to comment.