aboutsummaryrefslogtreecommitdiff
path: root/src/gui/state.rs
blob: be41f862ff47d4fab6f20671692795e786ae1bfe (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use gtk::prelude::*;
use std::collections::HashMap;
use std::sync::Mutex;
use crate::index::Index;
use std::rc::Rc;
use std::sync::Arc;

pub trait View {
    fn name(&self) -> &str;
    fn set_vm(&mut self, vm : Rc<ViewManager>);
    fn make_current(&self) -> Option<gtk::Box>;
}

#[derive(Default)]
pub struct ViewManager {
    views : HashMap<String, Rc<Mutex<dyn View>>>,
    pub current : Mutex<String>,
    pub window : Option<gtk::ApplicationWindow>,
    index : Arc<Mutex<Index>>
}

impl ViewManager {
    pub fn new(window : gtk::ApplicationWindow) -> Self {
        Self {
            views : HashMap::new(),
            current : Mutex::new(String::new()),
            window : Some(window),
            index : Arc::new(Mutex::new(Index::default()))
        }
    }

    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)
        }
        {
            *(self.current.lock().unwrap()) = name.to_string();
        }
        if let Some(b) = b {
            window.add(&b);
            window.show_all();
        } else {
            window.hide();
        }
    }

    pub fn get_current_view(&self) -> String {
        self.current.lock().unwrap().to_string()
    }

    pub fn get_window(&self) -> &gtk::ApplicationWindow {
        self.window.as_ref().unwrap()
    }

    pub fn get_index(&self) -> Arc<Mutex<Index>> {
        Arc::clone(&self.index)
    }

    pub fn set_index(&self, index : Index) {
        let mut idx = self.index.lock().unwrap();
        *idx = index;
    }
}