Early gui impl, basic window management
This commit is contained in:
75
xmpd-gui/src/windows/mod.rs
Normal file
75
xmpd-gui/src/windows/mod.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use std::{collections::{HashMap, HashSet}, ops::Deref, sync::{Arc, Mutex}};
|
||||
use egui::{ViewportBuilder, ViewportId};
|
||||
use crate::GuiState;
|
||||
|
||||
mod error;
|
||||
|
||||
lazy_static::lazy_static!(
|
||||
static ref WINDOWS: Arc<Mutex<HashMap<WindowId, Box<dyn Window>>>> = Arc::new(Mutex::new(HashMap::new()));
|
||||
static ref OPEN_WINDOWS: Arc<Mutex<HashSet<WindowId>>> = Arc::new(Mutex::new(HashSet::new()));
|
||||
|
||||
);
|
||||
|
||||
pub trait Window: std::fmt::Debug + Send {
|
||||
fn draw(&self, ui: &mut egui::Ui, state: &mut GuiState) -> crate::Result<()>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Ord, Eq)]
|
||||
pub enum WindowId {
|
||||
Error
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Windows {
|
||||
windows: HashMap<WindowId, (ViewportId, ViewportBuilder)>,
|
||||
}
|
||||
|
||||
impl Windows {
|
||||
pub fn new() -> Self {
|
||||
let mut s = Self {
|
||||
windows: HashMap::new(),
|
||||
};
|
||||
s.add_all_windows();
|
||||
s
|
||||
}
|
||||
|
||||
pub fn add_all_windows(&mut self) {
|
||||
self.add_new_window(WindowId::Error, "Error!", Box::<error::ErrorW>::default())
|
||||
}
|
||||
|
||||
pub fn add_new_window(&mut self, id: WindowId, title: &str, cb: Box<dyn Window>) {
|
||||
let builder = ViewportBuilder::default()
|
||||
.with_title(title);
|
||||
self.windows.insert(id.clone(), (ViewportId::from_hash_of(id.clone()), builder));
|
||||
WINDOWS.lock().unwrap().insert(id, cb);
|
||||
}
|
||||
|
||||
pub fn draw_all(&mut self, ctx: &egui::Context, state: &mut GuiState) {
|
||||
for (win_id, (vp_id, builder)) in &self.windows {
|
||||
if self.is_open(&win_id) {
|
||||
ctx.show_viewport_immediate(vp_id.clone(), builder.clone(), |ctx, _vp_class| {
|
||||
ctx.input(|inp| {
|
||||
// println!("CLose requested: {}",inp.viewport().close_requested() );
|
||||
self.toggle(win_id, !inp.viewport().close_requested());
|
||||
});
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
WINDOWS.lock().unwrap().get(&win_id).unwrap().draw(ui, state)
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle(&self, id: &WindowId, status: bool) {
|
||||
if status {
|
||||
OPEN_WINDOWS.lock().unwrap().insert(id.clone());
|
||||
} else {
|
||||
log::debug!("tried to kill");
|
||||
OPEN_WINDOWS.lock().unwrap().remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_open(&self, id: &WindowId) -> bool {
|
||||
OPEN_WINDOWS.lock().unwrap().contains(&id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user