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