-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdock_state.rs
More file actions
118 lines (96 loc) · 3.37 KB
/
dock_state.rs
File metadata and controls
118 lines (96 loc) · 3.37 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::{
ops::{Deref, DerefMut},
ptr,
};
use egui_dock::{NodeIndex, TabIndex};
use crate::view::ViewId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TabType {
Pipeline,
DataView(ViewId),
}
/// Wrapper around [egui_dock::DockState], adding additional functionality
/// important for this application.
pub struct DockState(egui_dock::DockState<TabType>);
impl DockState {
pub fn new() -> Self {
Self(egui_dock::DockState::new(vec![TabType::Pipeline]))
}
/// Finds a fitting dock node and appends a data view tab.
pub fn add_view_tab(&mut self, view_id: ViewId) -> bool {
if self
.iter_all_tabs()
.any(|(_, tab)| matches!(tab, TabType::DataView(id) if *id == view_id))
{
// Tab for view_id already exists
return false;
}
if let Some((_, TabType::DataView(_))) = self.find_active_focused() {
let (surface_id, node_id) = self.focused_leaf().unwrap();
let node = &mut self[surface_id][node_id];
node.append_tab(TabType::DataView(view_id));
return true;
} else if let Some((surf_idx, node)) = {
let mut iter = self.iter_all_nodes_mut();
iter.find(|(_, node)| {
matches!(
node,
egui_dock::Node::Leaf {
tabs,
active: TabIndex(active),
..
} if matches!(tabs[*active], TabType::DataView(_))
)
})
} {
// There is a node that has a DataView in focus
node.append_tab(TabType::DataView(view_id));
let node_ptr = node as *mut _;
if let Some(node_idx) = self[surf_idx]
.iter()
.enumerate()
.find(|(_, n)| ptr::eq(*n, node_ptr))
.map(|(node_idx, _)| node_idx)
.map(NodeIndex)
{
self.set_focused_node_and_surface((surf_idx, node_idx));
}
return true;
}
let surf = self.main_surface_mut();
surf.split_above(NodeIndex::root(), 0.666, vec![TabType::DataView(view_id)]);
true
}
pub fn focus_view(&mut self, view_id: ViewId) {
if let Some((surf_idx, node_idx)) = {
let mut iter = self.iter_all_tabs();
iter.find_map(|(surf_node, tab)| match tab {
TabType::DataView(id) if *id == view_id => Some(surf_node),
_ => None,
})
} {
self.set_focused_node_and_surface((surf_idx, node_idx));
if let egui_dock::Node::Leaf { tabs, active, .. } = &mut self[surf_idx][node_idx] {
*active = tabs
.iter()
.position(|t| matches!(t, TabType::DataView(id) if *id == view_id))
.map(TabIndex)
.expect("Tab should already be found in previous search");
}
}
}
pub fn close_all_views(&mut self) {
self.retain_tabs(|tab| !matches!(tab, TabType::DataView(_)));
}
}
impl Deref for DockState {
type Target = egui_dock::DockState<TabType>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for DockState {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}