-
Notifications
You must be signed in to change notification settings - Fork 77
/
tab_add.rs
79 lines (65 loc) · 2.22 KB
/
tab_add.rs
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
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use eframe::{egui, NativeOptions};
use egui_dock::{DockArea, DockState, NodeIndex, Style, SurfaceIndex};
fn main() -> eframe::Result<()> {
let options = NativeOptions::default();
eframe::run_native(
"My egui App",
options,
Box::new(|_cc| Ok(Box::<MyApp>::default())),
)
}
struct TabViewer<'a> {
added_nodes: &'a mut Vec<(SurfaceIndex, NodeIndex)>,
}
impl egui_dock::TabViewer for TabViewer<'_> {
type Tab = usize;
fn title(&mut self, tab: &mut Self::Tab) -> egui::WidgetText {
format!("Tab {tab}").into()
}
fn ui(&mut self, ui: &mut egui::Ui, tab: &mut Self::Tab) {
ui.label(format!("Content of tab {tab}"));
}
fn on_add(&mut self, surface: SurfaceIndex, node: NodeIndex) {
self.added_nodes.push((surface, node));
}
}
struct MyApp {
tree: DockState<usize>,
counter: usize,
}
impl Default for MyApp {
fn default() -> Self {
let mut tree = DockState::new(vec![1, 2]);
// You can modify the tree before constructing the dock
let [a, b] = tree
.main_surface_mut()
.split_left(NodeIndex::root(), 0.3, vec![3]);
let [_, _] = tree.main_surface_mut().split_below(a, 0.7, vec![4]);
let [_, _] = tree.main_surface_mut().split_below(b, 0.5, vec![5]);
Self { tree, counter: 6 }
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let mut added_nodes = Vec::new();
DockArea::new(&mut self.tree)
.show_add_buttons(true)
.style({
let mut style = Style::from_egui(ctx.style().as_ref());
style.tab_bar.fill_tab_bar = true;
style
})
.show(
ctx,
&mut TabViewer {
added_nodes: &mut added_nodes,
},
);
added_nodes.drain(..).for_each(|(surface, node)| {
self.tree.set_focused_node_and_surface((surface, node));
self.tree.push_to_focused_leaf(self.counter);
self.counter += 1;
});
}
}