diff options
Diffstat (limited to 'src/gui/state.rs')
| -rw-r--r-- | src/gui/state.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/gui/state.rs b/src/gui/state.rs new file mode 100644 index 0000000..7b9542a --- /dev/null +++ b/src/gui/state.rs @@ -0,0 +1,51 @@ +use gtk::prelude::*; +use std::collections::HashMap; +use std::rc::Rc; +use std::sync::Mutex; + +pub trait View { + fn name(&self) -> &str; + fn set_vm(&mut self, vm : Rc<ViewManager>); + fn make_current(&self) -> gtk::Box; +} + +#[derive(Default)] +pub struct ViewManager { + views : HashMap<String, Rc<Mutex<dyn View>>>, + pub current : String, + pub window : Option<gtk::ApplicationWindow> +} + +impl ViewManager { + pub fn new(window : gtk::ApplicationWindow) -> Self { + Self { + views : HashMap::new(), + current : String::new(), + window : Some(window), + } + } + + pub fn empty() -> Self { + Self { + .. Default::default() + } + } + + pub fn add_view(&mut self, view : Rc<Mutex<dyn View>>) { + self.views.insert(view.lock().unwrap().name().to_string(), view.clone()); + } + + pub fn set_current_view(&self, name : &str) { + let b = self.views.get(name).expect("unkown view").lock().unwrap().make_current(); + let window = self.window.as_ref().unwrap(); + for child in window.children() { + window.remove(&child) + } + window.add(&b); + window.show_all() + } + + pub fn get_window(&self) -> >k::ApplicationWindow { + self.window.as_ref().unwrap() + } +} |