aboutsummaryrefslogtreecommitdiff
path: root/src/gui/state.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-07-26 09:42:17 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2023-07-26 09:42:17 +0200
commit7679fcc3a0c4fadea00a1a320938851b1518028d (patch)
treeede3c597dbef79f282e7465aeb8652f20bc0515a /src/gui/state.rs
parente1cb45be6575c805e3b0ab1e2d03d4047acf88d1 (diff)
add viewmanager
Diffstat (limited to 'src/gui/state.rs')
-rw-r--r--src/gui/state.rs51
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) -> &gtk::ApplicationWindow {
+ self.window.as_ref().unwrap()
+ }
+}