From 7679fcc3a0c4fadea00a1a320938851b1518028d Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 26 Jul 2023 09:42:17 +0200 Subject: add viewmanager --- src/gui/state.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/gui/state.rs (limited to 'src/gui/state.rs') 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); + fn make_current(&self) -> gtk::Box; +} + +#[derive(Default)] +pub struct ViewManager { + views : HashMap>>, + pub current : String, + pub window : Option +} + +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>) { + 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() + } +} -- cgit v1.2.3-70-g09d2