diff --git a/src/ui/device.rs b/src/ui/device.rs index 8021323..60f78cd 100644 --- a/src/ui/device.rs +++ b/src/ui/device.rs @@ -58,6 +58,7 @@ impl Device { self.toasts.show(ui.ctx()); } + /* fn power(&mut self, ui: &mut Ui, ip: &str) { fn handle_power_result(result: &anyhow::Result<()>, toasts: &mut Toasts, power: &str) { match result { @@ -92,6 +93,7 @@ impl Device { .then_some(()) .context("Failed to set power") } + */ fn sleep(&mut self, ui: &mut Ui, ip: &str) { ui.horizontal(|ui| { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 3a390f6..298225a 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,12 +1,10 @@ use eframe::CreationContext; use egui::{ - vec2, CentralPanel, Color32, ColorImage, Context, ImageData, ScrollArea, TextStyle, - TextureOptions, + vec2, CentralPanel, Color32, ColorImage, Context, ImageData, TextStyle, TextureOptions, }; use egui_notify::Toasts; use parking_lot::RwLock; use status::Stat; -use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use crate::config::Config; @@ -75,8 +73,13 @@ impl App { impl eframe::App for App { fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) { CentralPanel::default().show(ctx, |ui| { - self.statusbar - .show(ui, &mut self.current_tab, &mut self.config); + match self + .statusbar + .show(ui, &mut self.current_tab, &mut self.config) + { + Ok(()) => self.toasts.success("Config saved!"), + Err(e) => self.toasts.error(e.to_string()), + }; ui.vertical_centered(|ui| { ui.separator(); }); @@ -95,9 +98,7 @@ impl eframe::App for App { Tab::Settings => self.settings.show(ui, ip), } .unwrap_or_else(|e| { - self.toasts - .error(e.to_string()) - .set_duration(Some(std::time::Duration::from_secs(5))); + self.toasts.error(e.to_string()); }); } }); diff --git a/src/ui/settings.rs b/src/ui/settings.rs index bf40475..52e3ca8 100644 --- a/src/ui/settings.rs +++ b/src/ui/settings.rs @@ -1,9 +1,7 @@ -use std::time::Duration; use anyhow::Context; use egui::{Align, Button, Layout, ScrollArea, TextEdit, TextStyle, Ui}; use egui_extras::syntax_highlighting; -use egui_notify::Toasts; use reqwest::blocking::{get, Client}; pub struct Settings { diff --git a/src/ui/statusbar.rs b/src/ui/statusbar.rs index 80bfce5..a783aa5 100644 --- a/src/ui/statusbar.rs +++ b/src/ui/statusbar.rs @@ -3,48 +3,51 @@ use egui::{ include_image, special_emojis::GITHUB, vec2, Align, Align2, Button, Frame, Image, Layout, TextEdit, Ui, Window, }; -use egui_notify::Toasts; use super::Tab; pub struct StatusBar { show_about: bool, - toasts: Toasts, } impl StatusBar { pub fn new() -> Self { - Self { - show_about: false, - toasts: Toasts::new().with_anchor(egui_notify::Anchor::BottomLeft), - } + Self { show_about: false } } - pub fn show(&mut self, ui: &mut Ui, tab: &mut Tab, config: &mut Config) { - ui.horizontal(|ui| { - ui.add_enabled_ui(!config.ip.is_empty(), |ui| { - ui.selectable_value(tab, Tab::Screen, "Screen"); - ui.selectable_value(tab, Tab::Status, "Status"); - ui.selectable_value(tab, Tab::Settings, "Settings"); - }); - - ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - ui.add(Button::new(" ? ").rounding(40.0)) - .clicked() - .then(|| self.show_about = true); - ui.add(Button::new("Save")).clicked().then(|| { - config.write().unwrap(); - }); - ui.add( - TextEdit::singleline(&mut config.ip) - .hint_text("IP") - .desired_width(150.0), - ); - ui.label("IP:"); - }); - }); + pub fn show(&mut self, ui: &mut Ui, tab: &mut Tab, config: &mut Config) -> anyhow::Result<()> { self.about_window(ui); - self.toasts.show(ui.ctx()); + return ui + .horizontal(|ui| { + ui.add_enabled_ui(!config.ip.is_empty(), |ui| { + ui.selectable_value(tab, Tab::Screen, "Screen"); + ui.selectable_value(tab, Tab::Status, "Status"); + ui.selectable_value(tab, Tab::Settings, "Settings"); + }); + + ui.with_layout(Layout::right_to_left(Align::Center), |ui| { + ui.add(Button::new(" ? ").rounding(40.0)) + .clicked() + .then(|| self.show_about = true); + let ret = ui + .add(Button::new("Save")) + .clicked() + .then(|| match config.write() { + Ok(()) => anyhow::Ok(()), + Err(e) => anyhow::bail!(e), + }) + .unwrap_or(Ok(())); + ui.add( + TextEdit::singleline(&mut config.ip) + .hint_text("IP") + .desired_width(150.0), + ); + ui.label("IP:"); + ret + }) + .inner + }) + .inner; } fn about_window(&mut self, ui: &mut Ui) {