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); fn make_current(&self) -> Option; } #[derive(Default)] pub struct ViewManager { views : HashMap>>, pub current : Mutex, pub window : Option, index : Arc> } 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>) { 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) -> >k::ApplicationWindow { self.window.as_ref().unwrap() } pub fn get_index(&self) -> Arc> { Arc::clone(&self.index) } pub fn set_index(&self, index : Index) { let mut idx = self.index.lock().unwrap(); *idx = index; } }