-
Notifications
You must be signed in to change notification settings - Fork 77
/
hello.rs
614 lines (541 loc) · 22.7 KB
/
hello.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use std::collections::HashSet;
use eframe::NativeOptions;
use egui::{
color_picker::{color_edit_button_srgba, Alpha},
vec2, CentralPanel, ComboBox, Frame, Rounding, Slider, TopBottomPanel, Ui, ViewportBuilder,
WidgetText,
};
use egui_dock::{
AllowedSplits, DockArea, DockState, NodeIndex, OverlayType, Style, SurfaceIndex,
TabInteractionStyle, TabViewer,
};
/// Adds a widget with a label next to it, can be given an extra parameter in order to show a hover text
macro_rules! labeled_widget {
($ui:expr, $x:expr, $l:expr) => {
$ui.horizontal(|ui| {
ui.add($x);
ui.label($l);
});
};
($ui:expr, $x:expr, $l:expr, $d:expr) => {
$ui.horizontal(|ui| {
ui.add($x).on_hover_text($d);
ui.label($l).on_hover_text($d);
});
};
}
// Creates a slider which has a unit attached to it
// When given an extra parameter it will be used as a multiplier (e.g 100.0 when working with percentages)
macro_rules! unit_slider {
($val:expr, $range:expr) => {
egui::Slider::new($val, $range)
};
($val:expr, $range:expr, $unit:expr) => {
egui::Slider::new($val, $range).custom_formatter(|value, decimal_range| {
egui::emath::format_with_decimals_in_range(value, decimal_range) + $unit
})
};
($val:expr, $range:expr, $unit:expr, $mul:expr) => {
egui::Slider::new($val, $range)
.custom_formatter(|value, decimal_range| {
egui::emath::format_with_decimals_in_range(value * $mul, decimal_range) + $unit
})
.custom_parser(|string| string.parse::<f64>().ok().map(|valid| valid / $mul))
};
}
fn main() -> eframe::Result<()> {
std::env::set_var("RUST_BACKTRACE", "1");
let options = NativeOptions {
viewport: ViewportBuilder::default().with_inner_size(vec2(1024.0, 1024.0)),
..Default::default()
};
eframe::run_native(
"My egui App",
options,
Box::new(|_cc| Ok(Box::<MyApp>::default())),
)
}
struct MyContext {
pub title: String,
pub age: u32,
pub style: Option<Style>,
open_tabs: HashSet<String>,
show_close_buttons: bool,
show_add_buttons: bool,
draggable_tabs: bool,
show_tab_name_on_hover: bool,
allowed_splits: AllowedSplits,
show_window_close: bool,
show_window_collapse: bool,
}
struct MyApp {
context: MyContext,
tree: DockState<String>,
}
impl TabViewer for MyContext {
type Tab = String;
fn title(&mut self, tab: &mut Self::Tab) -> WidgetText {
tab.as_str().into()
}
fn ui(&mut self, ui: &mut Ui, tab: &mut Self::Tab) {
match tab.as_str() {
"Simple Demo" => self.simple_demo(ui),
"Style Editor" => self.style_editor(ui),
_ => {
ui.label(tab.as_str());
}
}
}
fn context_menu(
&mut self,
ui: &mut Ui,
tab: &mut Self::Tab,
_surface: SurfaceIndex,
_node: NodeIndex,
) {
match tab.as_str() {
"Simple Demo" => self.simple_demo_menu(ui),
_ => {
ui.label(tab.to_string());
ui.label("This is a context menu");
}
}
}
fn closeable(&mut self, tab: &mut Self::Tab) -> bool {
["Inspector", "Style Editor"].contains(&tab.as_str())
}
fn on_close(&mut self, tab: &mut Self::Tab) -> bool {
self.open_tabs.remove(tab);
true
}
}
impl MyContext {
fn simple_demo_menu(&mut self, ui: &mut Ui) {
ui.label("Egui widget example");
ui.menu_button("Sub menu", |ui| {
ui.label("hello :)");
});
}
fn simple_demo(&mut self, ui: &mut Ui) {
ui.heading("My egui Application");
ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit_singleline(&mut self.title);
});
ui.add(Slider::new(&mut self.age, 0..=120).text("age"));
if ui.button("Click each year").clicked() {
self.age += 1;
}
ui.label(format!("Hello '{}', age {}", &self.title, &self.age));
}
fn style_editor(&mut self, ui: &mut Ui) {
ui.heading("Style Editor");
ui.collapsing("DockArea Options", |ui| {
ui.checkbox(&mut self.show_close_buttons, "Show close buttons");
ui.checkbox(&mut self.show_add_buttons, "Show add buttons");
ui.checkbox(&mut self.draggable_tabs, "Draggable tabs");
ui.checkbox(&mut self.show_tab_name_on_hover, "Show tab name on hover");
ui.checkbox(&mut self.show_window_close, "Show close button on windows");
ui.checkbox(
&mut self.show_window_collapse,
"Show collaspse button on windows",
);
ComboBox::new("cbox:allowed_splits", "Split direction(s)")
.selected_text(format!("{:?}", self.allowed_splits))
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.allowed_splits, AllowedSplits::All, "All");
ui.selectable_value(
&mut self.allowed_splits,
AllowedSplits::LeftRightOnly,
"LeftRightOnly",
);
ui.selectable_value(
&mut self.allowed_splits,
AllowedSplits::TopBottomOnly,
"TopBottomOnly",
);
ui.selectable_value(&mut self.allowed_splits, AllowedSplits::None, "None");
});
});
let style = self.style.as_mut().unwrap();
ui.collapsing("Border", |ui| {
egui::Grid::new("border").show(ui, |ui| {
ui.label("Width:");
ui.add(Slider::new(
&mut style.main_surface_border_stroke.width,
1.0..=50.0,
));
ui.end_row();
ui.label("Color:");
color_edit_button_srgba(
ui,
&mut style.main_surface_border_stroke.color,
Alpha::OnlyBlend,
);
ui.end_row();
ui.label("Rounding:");
rounding_ui(ui, &mut style.main_surface_border_rounding);
ui.end_row();
});
});
ui.collapsing("Separator", |ui| {
egui::Grid::new("separator").show(ui, |ui| {
ui.label("Width:");
ui.add(Slider::new(&mut style.separator.width, 1.0..=50.0));
ui.end_row();
ui.label("Extra Interact Width:");
ui.add(Slider::new(
&mut style.separator.extra_interact_width,
0.0..=50.0,
));
ui.end_row();
ui.label("Offset limit:");
ui.add(Slider::new(&mut style.separator.extra, 1.0..=300.0));
ui.end_row();
ui.label("Idle color:");
color_edit_button_srgba(ui, &mut style.separator.color_idle, Alpha::OnlyBlend);
ui.end_row();
ui.label("Hovered color:");
color_edit_button_srgba(ui, &mut style.separator.color_hovered, Alpha::OnlyBlend);
ui.end_row();
ui.label("Dragged color:");
color_edit_button_srgba(ui, &mut style.separator.color_dragged, Alpha::OnlyBlend);
ui.end_row();
});
});
ui.collapsing("Tabs", |ui| {
ui.separator();
ui.checkbox(&mut style.tab_bar.fill_tab_bar, "Expand tabs");
ui.checkbox(
&mut style.tab_bar.show_scroll_bar_on_overflow,
"Show scroll bar on tab overflow",
);
ui.checkbox(
&mut style.tab.hline_below_active_tab_name,
"Show a line below the active tab name",
);
ui.horizontal(|ui| {
ui.add(Slider::new(&mut style.tab_bar.height, 20.0..=50.0));
ui.label("Tab bar height");
});
ComboBox::new("add_button_align", "Add button align")
.selected_text(format!("{:?}", style.buttons.add_tab_align))
.show_ui(ui, |ui| {
for align in [egui_dock::TabAddAlign::Left, egui_dock::TabAddAlign::Right] {
ui.selectable_value(
&mut style.buttons.add_tab_align,
align,
format!("{:?}", align),
);
}
});
ui.separator();
fn tab_style_editor_ui(ui: &mut Ui, tab_style: &mut TabInteractionStyle) {
ui.separator();
ui.label("Rounding");
labeled_widget!(
ui,
Slider::new(&mut tab_style.rounding.nw, 0.0..=15.0),
"North-West"
);
labeled_widget!(
ui,
Slider::new(&mut tab_style.rounding.ne, 0.0..=15.0),
"North-East"
);
labeled_widget!(
ui,
Slider::new(&mut tab_style.rounding.sw, 0.0..=15.0),
"South-West"
);
labeled_widget!(
ui,
Slider::new(&mut tab_style.rounding.se, 0.0..=15.0),
"South-East"
);
ui.separator();
egui::Grid::new("tabs_colors").show(ui, |ui| {
ui.label("Title text color:");
color_edit_button_srgba(ui, &mut tab_style.text_color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Outline color:")
.on_hover_text("The outline around the active tab name.");
color_edit_button_srgba(ui, &mut tab_style.outline_color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Background color:");
color_edit_button_srgba(ui, &mut tab_style.bg_fill, Alpha::OnlyBlend);
ui.end_row();
});
}
ui.collapsing("Active", |ui| {
tab_style_editor_ui(ui, &mut style.tab.active);
});
ui.collapsing("Inactive", |ui| {
tab_style_editor_ui(ui, &mut style.tab.inactive);
});
ui.collapsing("Focused", |ui| {
tab_style_editor_ui(ui, &mut style.tab.focused);
});
ui.collapsing("Hovered", |ui| {
tab_style_editor_ui(ui, &mut style.tab.hovered);
});
ui.separator();
egui::Grid::new("tabs_colors").show(ui, |ui| {
ui.label("Close button color unfocused:");
color_edit_button_srgba(ui, &mut style.buttons.close_tab_color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Close button color focused:");
color_edit_button_srgba(
ui,
&mut style.buttons.close_tab_active_color,
Alpha::OnlyBlend,
);
ui.end_row();
ui.label("Close button background color:");
color_edit_button_srgba(ui, &mut style.buttons.close_tab_bg_fill, Alpha::OnlyBlend);
ui.end_row();
ui.label("Bar background color:");
color_edit_button_srgba(ui, &mut style.tab_bar.bg_fill, Alpha::OnlyBlend);
ui.end_row();
ui.label("Horizontal line color:").on_hover_text(
"The line separating the tab name area from the tab content area",
);
color_edit_button_srgba(ui, &mut style.tab_bar.hline_color, Alpha::OnlyBlend);
ui.end_row();
});
});
ui.collapsing("Tab body", |ui| {
ui.separator();
ui.label("Rounding");
rounding_ui(ui, &mut style.tab.tab_body.rounding);
ui.label("Stroke width:");
ui.add(Slider::new(
&mut style.tab.tab_body.stroke.width,
0.0..=10.0,
));
ui.end_row();
egui::Grid::new("tab_body_colors").show(ui, |ui| {
ui.label("Stroke color:");
color_edit_button_srgba(ui, &mut style.tab.tab_body.stroke.color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Background color:");
color_edit_button_srgba(ui, &mut style.tab.tab_body.bg_fill, Alpha::OnlyBlend);
ui.end_row();
});
});
ui.collapsing("Overlay", |ui| {
let selected_text = match style.overlay.overlay_type {
OverlayType::HighlightedAreas => "Highlighted Areas",
OverlayType::Widgets => "Widgets",
};
ui.label("Overlay Style:");
ComboBox::new("overlay styles", "")
.selected_text(selected_text)
.show_ui(ui, |ui| {
ui.selectable_value(
&mut style.overlay.overlay_type,
OverlayType::HighlightedAreas,
"Highlighted Areas",
);
ui.selectable_value(
&mut style.overlay.overlay_type,
OverlayType::Widgets,
"Widgets",
);
});
ui.collapsing("Feel", |ui|{
labeled_widget!(
ui,
unit_slider!(&mut style.overlay.feel.center_drop_coverage, 0.0..=1.0, "%", 100.0),
"Center drop coverage",
"how big the area where dropping a tab into the center of another should be."
);
labeled_widget!(
ui,
unit_slider!(&mut style.overlay.feel.fade_hold_time, 0.0..=4.0, "s"),
"Fade hold time",
"How long faded windows should hold their fade before unfading, in seconds."
);
labeled_widget!(
ui,
unit_slider!(&mut style.overlay.feel.max_preference_time, 0.0..=4.0, "s"),
"Max preference time",
"How long the overlay may prefer to stick to a surface despite hovering over another, in seconds."
);
labeled_widget!(
ui,
unit_slider!(&mut style.overlay.feel.window_drop_coverage, 0.0..=1.0, "%", 100.0),
"Window drop coverage",
"How big the area for undocking a window should be. [is overshadowed by center drop coverage]"
);
labeled_widget!(
ui,
unit_slider!(&mut style.overlay.feel.interact_expansion, 1.0..=100.0, "ps"),
"Interact expansion",
"How much extra interaction area should be allocated for buttons on the overlay"
);
});
ui.collapsing("Visuals", |ui|{
labeled_widget!(
ui,
unit_slider!(&mut style.overlay.max_button_size, 10.0..=500.0, "ps"),
"Max button size",
"The max length of a side on a overlay button in egui points"
);
labeled_widget!(
ui,
unit_slider!(&mut style.overlay.button_spacing, 0.0..=50.0, "ps"),
"Button spacing",
"Spacing between buttons on the overlay, in egui units."
);
labeled_widget!(
ui,
unit_slider!(&mut style.overlay.surface_fade_opacity, 0.0..=1.0, "%", 100.0),
"Window fade opacity",
"how visible windows are when dragging a tab behind them."
);
labeled_widget!(
ui,
egui::Slider::new(&mut style.overlay.selection_stroke_width, 0.0..=50.0),
"Selection stroke width",
"width of a selection which uses a outline stroke instead of filled rect."
);
egui::Grid::new("overlay style preferences").show(ui, |ui| {
ui.label("Button color:");
color_edit_button_srgba(ui, &mut style.overlay.button_color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Button border color:");
color_edit_button_srgba(ui, &mut style.overlay.button_border_stroke.color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Selection color:");
color_edit_button_srgba(ui, &mut style.overlay.selection_color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Button stroke color:");
color_edit_button_srgba(ui, &mut style.overlay.button_border_stroke.color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Button stroke width:");
ui.add(Slider::new(&mut style.overlay.button_border_stroke.width, 0.0..=50.0));
ui.end_row();
});
});
ui.collapsing("Hover highlight", |ui|{
egui::Grid::new("leaf highlighting prefs").show(ui, |ui|{
ui.label("Fill color:");
color_edit_button_srgba(ui, &mut style.overlay.hovered_leaf_highlight.color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Stroke color:");
color_edit_button_srgba(ui, &mut style.overlay.hovered_leaf_highlight.stroke.color, Alpha::OnlyBlend);
ui.end_row();
ui.label("Stroke width:");
ui.add(Slider::new(&mut style.overlay.hovered_leaf_highlight.stroke.width, 0.0..=50.0));
ui.end_row();
ui.label("Expansion:");
ui.add(Slider::new(&mut style.overlay.hovered_leaf_highlight.expansion, -50.0..=50.0));
ui.end_row();
});
ui.label("Rounding:");
rounding_ui(ui, &mut style.overlay.hovered_leaf_highlight.rounding);
})
});
}
}
impl Default for MyApp {
fn default() -> Self {
let mut dock_state =
DockState::new(vec!["Simple Demo".to_owned(), "Style Editor".to_owned()]);
"Undock".clone_into(&mut dock_state.translations.tab_context_menu.eject_button);
let [a, b] = dock_state.main_surface_mut().split_left(
NodeIndex::root(),
0.3,
vec!["Inspector".to_owned()],
);
let [_, _] = dock_state.main_surface_mut().split_below(
a,
0.7,
vec!["File Browser".to_owned(), "Asset Manager".to_owned()],
);
let [_, _] =
dock_state
.main_surface_mut()
.split_below(b, 0.5, vec!["Hierarchy".to_owned()]);
let mut open_tabs = HashSet::new();
for node in dock_state[SurfaceIndex::main()].iter() {
if let Some(tabs) = node.tabs() {
for tab in tabs {
open_tabs.insert(tab.clone());
}
}
}
let context = MyContext {
title: "Hello".to_string(),
age: 24,
style: None,
open_tabs,
show_window_close: true,
show_window_collapse: true,
show_close_buttons: true,
show_add_buttons: false,
draggable_tabs: true,
show_tab_name_on_hover: false,
allowed_splits: AllowedSplits::default(),
};
Self {
context,
tree: dock_state,
}
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
TopBottomPanel::top("egui_dock::MenuBar").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
ui.menu_button("View", |ui| {
// allow certain tabs to be toggled
for tab in &["File Browser", "Asset Manager"] {
if ui
.selectable_label(self.context.open_tabs.contains(*tab), *tab)
.clicked()
{
if let Some(index) = self.tree.find_tab(&tab.to_string()) {
self.tree.remove_tab(index);
self.context.open_tabs.remove(*tab);
} else {
self.tree[SurfaceIndex::main()]
.push_to_focused_leaf(tab.to_string());
}
ui.close_menu();
}
}
});
})
});
CentralPanel::default()
// When displaying a DockArea in another UI, it looks better
// to set inner margins to 0.
.frame(Frame::central_panel(&ctx.style()).inner_margin(0.))
.show(ctx, |ui| {
let style = self
.context
.style
.get_or_insert(Style::from_egui(ui.style()))
.clone();
DockArea::new(&mut self.tree)
.style(style)
.show_close_buttons(self.context.show_close_buttons)
.show_add_buttons(self.context.show_add_buttons)
.draggable_tabs(self.context.draggable_tabs)
.show_tab_name_on_hover(self.context.show_tab_name_on_hover)
.allowed_splits(self.context.allowed_splits)
.show_window_close_buttons(self.context.show_window_close)
.show_window_collapse_buttons(self.context.show_window_collapse)
.show_inside(ui, &mut self.context);
});
}
}
fn rounding_ui(ui: &mut Ui, rounding: &mut Rounding) {
labeled_widget!(ui, Slider::new(&mut rounding.nw, 0.0..=15.0), "North-West");
labeled_widget!(ui, Slider::new(&mut rounding.ne, 0.0..=15.0), "North-East");
labeled_widget!(ui, Slider::new(&mut rounding.sw, 0.0..=15.0), "South-West");
labeled_widget!(ui, Slider::new(&mut rounding.se, 0.0..=15.0), "South-East");
}