Skip to main content

iced_webview/engines/
view_manager.rs

1use std::collections::HashMap;
2
3use super::ViewId;
4
5pub struct ViewManager<V> {
6    views: HashMap<ViewId, V>,
7}
8
9impl<V> Default for ViewManager<V> {
10    fn default() -> Self {
11        Self {
12            views: HashMap::new(),
13        }
14    }
15}
16
17impl<V> ViewManager<V> {
18    pub fn get(&self, id: ViewId) -> Option<&V> {
19        self.views.get(&id)
20    }
21
22    pub fn get_mut(&mut self, id: ViewId) -> Option<&mut V> {
23        self.views.get_mut(&id)
24    }
25
26    pub fn insert(&mut self, view: V) -> ViewId {
27        let id = rand::random::<ViewId>();
28        self.views.insert(id, view);
29        id
30    }
31
32    pub fn remove(&mut self, id: ViewId) -> Option<V> {
33        self.views.remove(&id)
34    }
35
36    pub fn contains(&self, id: ViewId) -> bool {
37        self.views.contains_key(&id)
38    }
39
40    pub fn keys(&self) -> Vec<ViewId> {
41        self.views.keys().copied().collect()
42    }
43
44    pub fn values(&self) -> impl Iterator<Item = &V> {
45        self.views.values()
46    }
47
48    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
49        self.views.values_mut()
50    }
51
52    pub fn iter(&self) -> impl Iterator<Item = (ViewId, &V)> {
53        self.views.iter().map(|(&id, v)| (id, v))
54    }
55
56    pub fn iter_mut(&mut self) -> impl Iterator<Item = (ViewId, &mut V)> {
57        self.views.iter_mut().map(|(&id, v)| (id, v))
58    }
59
60    pub fn len(&self) -> usize {
61        self.views.len()
62    }
63}