aboutsummaryrefslogtreecommitdiff
path: root/src/gui/state.rs
blob: 7b9542a9e3ecded28a88bc8482d7b69aefdd765c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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()
    }
}