pub struct Tree<Tab> { /* private fields */ }
Expand description
Binary tree representing the relationships between Node
s.
Implementations§
source§impl<Tab> Tree<Tab>
impl<Tab> Tree<Tab>
sourcepub fn find_active(&mut self) -> Option<(Rect, &mut Tab)>
pub fn find_active(&mut self) -> Option<(Rect, &mut Tab)>
Returns the viewport Rect
and the Tab
inside the first leaf node, or None
of no leaf exists in the Tree
.
sourcepub fn find_active_focused(&mut self) -> Option<(Rect, &mut Tab)>
pub fn find_active_focused(&mut self) -> Option<(Rect, &mut Tab)>
Returns the viewport Rect
and the Tab
inside the focused leaf node or None
if it does not exist.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of nodes in the Tree
.
Examples found in repository?
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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
pub fn show_inside(self, ui: &mut Ui, tab_viewer: &mut impl TabViewer<Tab = Tab>) {
let style = self
.style
.unwrap_or_else(|| Style::from_egui(ui.style().as_ref()));
let mut state = State::load(ui.ctx(), self.id);
let mut rect = ui.max_rect();
if let Some(margin) = style.dock_area_padding {
rect.min += margin.left_top();
rect.max -= margin.right_bottom();
ui.painter().rect(
rect,
margin.top,
style.separator_color_idle,
Stroke::new(margin.top, style.border_color),
);
}
if self.tree.is_empty() {
ui.allocate_rect(rect, Sense::hover());
return;
}
self.tree[NodeIndex::root()].set_rect(rect);
let mut drag_data = None;
let mut hover_data = None;
let pixels_per_point = ui.ctx().pixels_per_point();
let px = pixels_per_point.recip();
let focused = self.tree.focused_leaf();
let mut to_remove = Vec::new();
let mut new_focused = None;
// Deal with Horizontal and Vertical nodes first
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
let is_horizontal = self.tree[node_index].is_horizontal();
if let Node::Horizontal { fraction, rect } | Node::Vertical { fraction, rect } =
&mut self.tree[node_index]
{
let rect = expand_to_pixel(*rect, pixels_per_point);
let (response, left, separator, right) = if is_horizontal {
style.hsplit(ui, fraction, rect)
} else {
style.vsplit(ui, fraction, rect)
};
let color = if response.dragged() {
style.separator_color_dragged
} else if response.hovered() {
style.separator_color_hovered
} else {
style.separator_color_idle
};
ui.painter().rect_filled(separator, Rounding::none(), color);
self.tree[node_index.left()].set_rect(left);
self.tree[node_index.right()].set_rect(right);
}
}
// Then process Leaf nodes
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
if let Node::Leaf {
rect,
tabs,
active,
viewport,
} = &mut self.tree[node_index]
{
let rect = *rect;
ui.set_clip_rect(rect);
let height_topbar = style.tab_bar_height;
let bottom_y = rect.min.y + height_topbar;
let tabbar = rect.intersect(Rect::everything_above(bottom_y));
let full_response = ui.allocate_rect(rect, Sense::hover());
let tabs_response = ui.allocate_rect(tabbar, Sense::hover());
let mut tab_hover_rect = None;
// tabs
ui.scope(|ui| {
ui.painter().rect_filled(
tabbar,
style.tab_rounding,
style.tab_bar_background_color,
);
let a = pos2(tabbar.min.x, tabbar.max.y - px);
let b = pos2(tabbar.max.x, tabbar.max.y - px);
ui.painter()
.line_segment([a, b], (px, style.tab_outline_color));
let mut available_width = tabbar.max.x - tabbar.min.x;
if style.show_add_buttons {
available_width -= Style::TAB_PLUS_SIZE;
}
let expanded_width = available_width / (tabs.len() as f32);
let mut ui = ui.child_ui(tabbar, Default::default());
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
ui.horizontal(|ui| {
for (tab_index, tab) in tabs.iter_mut().enumerate() {
let id = self.id.with((node_index, tab_index, "tab"));
let tab_index = TabIndex(tab_index);
let is_being_dragged =
ui.memory().is_being_dragged(id) && style.tabs_are_draggable;
if is_being_dragged {
ui.output().cursor_icon = CursorIcon::Grabbing;
}
let is_active = *active == tab_index || is_being_dragged;
let label = tab_viewer.title(tab);
let response = if is_being_dragged {
let layer_id = LayerId::new(Order::Tooltip, id);
let response = ui
.with_layer_id(layer_id, |ui| {
style.tab_title(
ui,
label,
is_active,
is_active && Some(node_index) == focused,
is_being_dragged,
id,
expanded_width,
)
})
.response;
let sense = Sense::click_and_drag();
let response = ui.interact(response.rect, id, sense);
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let center = response.rect.center();
let start = state.drag_start.unwrap_or(center);
let delta = pointer_pos - start;
if delta.x.abs() > 30.0 || delta.y.abs() > 6.0 {
ui.ctx().translate_layer(layer_id, delta);
drag_data = Some((node_index, tab_index));
}
}
if response.clicked() {
*active = tab_index;
new_focused = Some(node_index);
}
if response.middle_clicked() && style.show_close_buttons {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
response
} else {
let response = style.tab_title(
ui,
label,
is_active && Some(node_index) == focused,
is_active,
is_being_dragged,
id,
expanded_width,
);
let sense = if response.1 {
Sense::click()
} else {
Sense::click_and_drag()
};
if style.tab_hover_name {
response.0.clone().on_hover_ui(|ui| {
ui.label(tab_viewer.title(tab));
});
}
if style.show_context_menu {
response.0.clone().context_menu(|ui| {
tab_viewer.context_menu(ui, tab);
if style.show_close_buttons && ui.button("Close").clicked()
{
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
});
}
if response.2 {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
let response = ui.interact(response.0.rect, id, sense);
if response.drag_started() {
state.drag_start = response.hover_pos();
}
response
};
if state.drag_start.is_some() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if response.rect.contains(pos) {
tab_hover_rect = Some((response.rect, tab_index));
}
}
}
}
// Add button at the end of the tab bar
if style.show_add_buttons {
let id = self.id.with((node_index, "tab_add"));
let response = style.tab_plus(ui);
let response = ui.interact(response.rect, id, Sense::click());
let popup_id = id.with("tab_add_popup");
popup::popup_under_widget(ui, popup_id, &response, |ui| {
tab_viewer.add_popup(ui, node_index);
});
if response.clicked() {
if style.show_add_popup {
ui.memory().toggle_popup(popup_id);
}
tab_viewer.on_add(node_index);
}
};
});
});
// tab body
if let Some(tab) = tabs.get_mut(active.0) {
let top_y = rect.min.y + height_topbar;
let rect = rect.intersect(Rect::everything_below(top_y));
let rect = expand_to_pixel(rect, pixels_per_point);
*viewport = rect;
if ui.input().pointer.any_click() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if rect.contains(pos) {
new_focused = Some(node_index);
}
}
}
if tab_viewer.clear_background(tab) {
ui.painter()
.rect_filled(rect, 0.0, style.tab_background_color);
}
let mut ui = ui.child_ui(rect, Default::default());
ui.push_id(node_index, |ui| {
if style.tab_include_scrollarea {
ScrollArea::both()
.id_source(
self.id
.with((tab_viewer.title(tab).text(), "egui_dock::Tab")),
)
.show(ui, |ui| {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
let available_rect = ui.available_rect_before_wrap();
ui.expand_to_include_rect(available_rect);
tab_viewer.ui(ui, tab);
});
});
} else {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
tab_viewer.ui(ui, tab);
});
}
});
}
let is_being_dragged = ui.memory().is_anything_being_dragged();
if is_being_dragged && full_response.hovered() {
hover_data = ui.input().pointer.hover_pos().map(|pointer| HoverData {
rect,
dst: node_index,
tabs: tabs_response.hovered().then_some(tabs_response.rect),
tab: tab_hover_rect,
pointer,
});
}
for (tab_index, tab) in tabs.iter_mut().enumerate() {
if tab_viewer.force_close(tab) {
to_remove.push((node_index, TabIndex(tab_index)));
}
}
}
}
let mut emptied = 0;
let mut last = (NodeIndex(usize::MAX), TabIndex(usize::MAX));
for remove in to_remove.iter().rev() {
if let Node::Leaf { tabs, active, .. } = &mut self.tree[remove.0] {
tabs.remove(remove.1 .0);
if remove.1 <= *active {
active.0 = active.0.saturating_sub(1);
}
if tabs.is_empty() {
emptied += 1;
}
if last.0 == remove.0 {
assert!(last.1 > remove.1)
}
last = *remove;
} else {
panic!();
}
}
for _ in 0..emptied {
self.tree.remove_empty_leaf()
}
if let Some(focused) = new_focused {
self.tree.set_focused_node(focused);
}
if let (Some((src, tab_index)), Some(hover)) = (drag_data, hover_data) {
let dst = hover.dst;
if self.tree[src].is_leaf() && self.tree[dst].is_leaf() {
let (target, helper, tap_pos) = hover.resolve();
let id = Id::new("helper");
let layer_id = LayerId::new(Order::Foreground, id);
let painter = ui.ctx().layer_painter(layer_id);
if src != dst || self.tree[dst].tabs_count() > 1 {
painter.rect_filled(helper, 0.0, style.selection_color);
}
if ui.input().pointer.any_released() {
if let Node::Leaf { active, .. } = &mut self.tree[src] {
if *active >= tab_index {
active.0 = active.0.saturating_sub(1);
}
}
let tab = self.tree[src].remove_tab(tab_index).unwrap();
if let Some(target) = target {
self.tree.split(dst, target, 0.5, Node::leaf(tab));
} else {
if let Some(index) = tap_pos {
self.tree[dst].insert_tab(index, tab);
} else {
self.tree[dst].append_tab(tab);
}
self.tree.set_focused_node(dst);
}
self.tree.remove_empty_leaf();
for node in self.tree.iter_mut() {
if let Node::Leaf { tabs, active, .. } = node {
if active.0 >= tabs.len() {
active.0 = 0;
}
}
}
}
}
}
state.store(ui.ctx(), self.id);
}
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the number of nodes in the tree is 0, false
otherwise.
Examples found in repository?
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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
pub fn show_inside(self, ui: &mut Ui, tab_viewer: &mut impl TabViewer<Tab = Tab>) {
let style = self
.style
.unwrap_or_else(|| Style::from_egui(ui.style().as_ref()));
let mut state = State::load(ui.ctx(), self.id);
let mut rect = ui.max_rect();
if let Some(margin) = style.dock_area_padding {
rect.min += margin.left_top();
rect.max -= margin.right_bottom();
ui.painter().rect(
rect,
margin.top,
style.separator_color_idle,
Stroke::new(margin.top, style.border_color),
);
}
if self.tree.is_empty() {
ui.allocate_rect(rect, Sense::hover());
return;
}
self.tree[NodeIndex::root()].set_rect(rect);
let mut drag_data = None;
let mut hover_data = None;
let pixels_per_point = ui.ctx().pixels_per_point();
let px = pixels_per_point.recip();
let focused = self.tree.focused_leaf();
let mut to_remove = Vec::new();
let mut new_focused = None;
// Deal with Horizontal and Vertical nodes first
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
let is_horizontal = self.tree[node_index].is_horizontal();
if let Node::Horizontal { fraction, rect } | Node::Vertical { fraction, rect } =
&mut self.tree[node_index]
{
let rect = expand_to_pixel(*rect, pixels_per_point);
let (response, left, separator, right) = if is_horizontal {
style.hsplit(ui, fraction, rect)
} else {
style.vsplit(ui, fraction, rect)
};
let color = if response.dragged() {
style.separator_color_dragged
} else if response.hovered() {
style.separator_color_hovered
} else {
style.separator_color_idle
};
ui.painter().rect_filled(separator, Rounding::none(), color);
self.tree[node_index.left()].set_rect(left);
self.tree[node_index.right()].set_rect(right);
}
}
// Then process Leaf nodes
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
if let Node::Leaf {
rect,
tabs,
active,
viewport,
} = &mut self.tree[node_index]
{
let rect = *rect;
ui.set_clip_rect(rect);
let height_topbar = style.tab_bar_height;
let bottom_y = rect.min.y + height_topbar;
let tabbar = rect.intersect(Rect::everything_above(bottom_y));
let full_response = ui.allocate_rect(rect, Sense::hover());
let tabs_response = ui.allocate_rect(tabbar, Sense::hover());
let mut tab_hover_rect = None;
// tabs
ui.scope(|ui| {
ui.painter().rect_filled(
tabbar,
style.tab_rounding,
style.tab_bar_background_color,
);
let a = pos2(tabbar.min.x, tabbar.max.y - px);
let b = pos2(tabbar.max.x, tabbar.max.y - px);
ui.painter()
.line_segment([a, b], (px, style.tab_outline_color));
let mut available_width = tabbar.max.x - tabbar.min.x;
if style.show_add_buttons {
available_width -= Style::TAB_PLUS_SIZE;
}
let expanded_width = available_width / (tabs.len() as f32);
let mut ui = ui.child_ui(tabbar, Default::default());
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
ui.horizontal(|ui| {
for (tab_index, tab) in tabs.iter_mut().enumerate() {
let id = self.id.with((node_index, tab_index, "tab"));
let tab_index = TabIndex(tab_index);
let is_being_dragged =
ui.memory().is_being_dragged(id) && style.tabs_are_draggable;
if is_being_dragged {
ui.output().cursor_icon = CursorIcon::Grabbing;
}
let is_active = *active == tab_index || is_being_dragged;
let label = tab_viewer.title(tab);
let response = if is_being_dragged {
let layer_id = LayerId::new(Order::Tooltip, id);
let response = ui
.with_layer_id(layer_id, |ui| {
style.tab_title(
ui,
label,
is_active,
is_active && Some(node_index) == focused,
is_being_dragged,
id,
expanded_width,
)
})
.response;
let sense = Sense::click_and_drag();
let response = ui.interact(response.rect, id, sense);
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let center = response.rect.center();
let start = state.drag_start.unwrap_or(center);
let delta = pointer_pos - start;
if delta.x.abs() > 30.0 || delta.y.abs() > 6.0 {
ui.ctx().translate_layer(layer_id, delta);
drag_data = Some((node_index, tab_index));
}
}
if response.clicked() {
*active = tab_index;
new_focused = Some(node_index);
}
if response.middle_clicked() && style.show_close_buttons {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
response
} else {
let response = style.tab_title(
ui,
label,
is_active && Some(node_index) == focused,
is_active,
is_being_dragged,
id,
expanded_width,
);
let sense = if response.1 {
Sense::click()
} else {
Sense::click_and_drag()
};
if style.tab_hover_name {
response.0.clone().on_hover_ui(|ui| {
ui.label(tab_viewer.title(tab));
});
}
if style.show_context_menu {
response.0.clone().context_menu(|ui| {
tab_viewer.context_menu(ui, tab);
if style.show_close_buttons && ui.button("Close").clicked()
{
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
});
}
if response.2 {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
let response = ui.interact(response.0.rect, id, sense);
if response.drag_started() {
state.drag_start = response.hover_pos();
}
response
};
if state.drag_start.is_some() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if response.rect.contains(pos) {
tab_hover_rect = Some((response.rect, tab_index));
}
}
}
}
// Add button at the end of the tab bar
if style.show_add_buttons {
let id = self.id.with((node_index, "tab_add"));
let response = style.tab_plus(ui);
let response = ui.interact(response.rect, id, Sense::click());
let popup_id = id.with("tab_add_popup");
popup::popup_under_widget(ui, popup_id, &response, |ui| {
tab_viewer.add_popup(ui, node_index);
});
if response.clicked() {
if style.show_add_popup {
ui.memory().toggle_popup(popup_id);
}
tab_viewer.on_add(node_index);
}
};
});
});
// tab body
if let Some(tab) = tabs.get_mut(active.0) {
let top_y = rect.min.y + height_topbar;
let rect = rect.intersect(Rect::everything_below(top_y));
let rect = expand_to_pixel(rect, pixels_per_point);
*viewport = rect;
if ui.input().pointer.any_click() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if rect.contains(pos) {
new_focused = Some(node_index);
}
}
}
if tab_viewer.clear_background(tab) {
ui.painter()
.rect_filled(rect, 0.0, style.tab_background_color);
}
let mut ui = ui.child_ui(rect, Default::default());
ui.push_id(node_index, |ui| {
if style.tab_include_scrollarea {
ScrollArea::both()
.id_source(
self.id
.with((tab_viewer.title(tab).text(), "egui_dock::Tab")),
)
.show(ui, |ui| {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
let available_rect = ui.available_rect_before_wrap();
ui.expand_to_include_rect(available_rect);
tab_viewer.ui(ui, tab);
});
});
} else {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
tab_viewer.ui(ui, tab);
});
}
});
}
let is_being_dragged = ui.memory().is_anything_being_dragged();
if is_being_dragged && full_response.hovered() {
hover_data = ui.input().pointer.hover_pos().map(|pointer| HoverData {
rect,
dst: node_index,
tabs: tabs_response.hovered().then_some(tabs_response.rect),
tab: tab_hover_rect,
pointer,
});
}
for (tab_index, tab) in tabs.iter_mut().enumerate() {
if tab_viewer.force_close(tab) {
to_remove.push((node_index, TabIndex(tab_index)));
}
}
}
}
let mut emptied = 0;
let mut last = (NodeIndex(usize::MAX), TabIndex(usize::MAX));
for remove in to_remove.iter().rev() {
if let Node::Leaf { tabs, active, .. } = &mut self.tree[remove.0] {
tabs.remove(remove.1 .0);
if remove.1 <= *active {
active.0 = active.0.saturating_sub(1);
}
if tabs.is_empty() {
emptied += 1;
}
if last.0 == remove.0 {
assert!(last.1 > remove.1)
}
last = *remove;
} else {
panic!();
}
}
for _ in 0..emptied {
self.tree.remove_empty_leaf()
}
if let Some(focused) = new_focused {
self.tree.set_focused_node(focused);
}
if let (Some((src, tab_index)), Some(hover)) = (drag_data, hover_data) {
let dst = hover.dst;
if self.tree[src].is_leaf() && self.tree[dst].is_leaf() {
let (target, helper, tap_pos) = hover.resolve();
let id = Id::new("helper");
let layer_id = LayerId::new(Order::Foreground, id);
let painter = ui.ctx().layer_painter(layer_id);
if src != dst || self.tree[dst].tabs_count() > 1 {
painter.rect_filled(helper, 0.0, style.selection_color);
}
if ui.input().pointer.any_released() {
if let Node::Leaf { active, .. } = &mut self.tree[src] {
if *active >= tab_index {
active.0 = active.0.saturating_sub(1);
}
}
let tab = self.tree[src].remove_tab(tab_index).unwrap();
if let Some(target) = target {
self.tree.split(dst, target, 0.5, Node::leaf(tab));
} else {
if let Some(index) = tap_pos {
self.tree[dst].insert_tab(index, tab);
} else {
self.tree[dst].append_tab(tab);
}
self.tree.set_focused_node(dst);
}
self.tree.remove_empty_leaf();
for node in self.tree.iter_mut() {
if let Node::Leaf { tabs, active, .. } = node {
if active.0 >= tabs.len() {
active.0 = 0;
}
}
}
}
}
}
state.store(ui.ctx(), self.id);
}
sourcepub fn iter_mut(&mut self) -> IterMut<'_, Node<Tab>>
pub fn iter_mut(&mut self) -> IterMut<'_, Node<Tab>>
Returns IterMut
of the underlying collection of nodes.
Examples found in repository?
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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
pub fn show_inside(self, ui: &mut Ui, tab_viewer: &mut impl TabViewer<Tab = Tab>) {
let style = self
.style
.unwrap_or_else(|| Style::from_egui(ui.style().as_ref()));
let mut state = State::load(ui.ctx(), self.id);
let mut rect = ui.max_rect();
if let Some(margin) = style.dock_area_padding {
rect.min += margin.left_top();
rect.max -= margin.right_bottom();
ui.painter().rect(
rect,
margin.top,
style.separator_color_idle,
Stroke::new(margin.top, style.border_color),
);
}
if self.tree.is_empty() {
ui.allocate_rect(rect, Sense::hover());
return;
}
self.tree[NodeIndex::root()].set_rect(rect);
let mut drag_data = None;
let mut hover_data = None;
let pixels_per_point = ui.ctx().pixels_per_point();
let px = pixels_per_point.recip();
let focused = self.tree.focused_leaf();
let mut to_remove = Vec::new();
let mut new_focused = None;
// Deal with Horizontal and Vertical nodes first
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
let is_horizontal = self.tree[node_index].is_horizontal();
if let Node::Horizontal { fraction, rect } | Node::Vertical { fraction, rect } =
&mut self.tree[node_index]
{
let rect = expand_to_pixel(*rect, pixels_per_point);
let (response, left, separator, right) = if is_horizontal {
style.hsplit(ui, fraction, rect)
} else {
style.vsplit(ui, fraction, rect)
};
let color = if response.dragged() {
style.separator_color_dragged
} else if response.hovered() {
style.separator_color_hovered
} else {
style.separator_color_idle
};
ui.painter().rect_filled(separator, Rounding::none(), color);
self.tree[node_index.left()].set_rect(left);
self.tree[node_index.right()].set_rect(right);
}
}
// Then process Leaf nodes
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
if let Node::Leaf {
rect,
tabs,
active,
viewport,
} = &mut self.tree[node_index]
{
let rect = *rect;
ui.set_clip_rect(rect);
let height_topbar = style.tab_bar_height;
let bottom_y = rect.min.y + height_topbar;
let tabbar = rect.intersect(Rect::everything_above(bottom_y));
let full_response = ui.allocate_rect(rect, Sense::hover());
let tabs_response = ui.allocate_rect(tabbar, Sense::hover());
let mut tab_hover_rect = None;
// tabs
ui.scope(|ui| {
ui.painter().rect_filled(
tabbar,
style.tab_rounding,
style.tab_bar_background_color,
);
let a = pos2(tabbar.min.x, tabbar.max.y - px);
let b = pos2(tabbar.max.x, tabbar.max.y - px);
ui.painter()
.line_segment([a, b], (px, style.tab_outline_color));
let mut available_width = tabbar.max.x - tabbar.min.x;
if style.show_add_buttons {
available_width -= Style::TAB_PLUS_SIZE;
}
let expanded_width = available_width / (tabs.len() as f32);
let mut ui = ui.child_ui(tabbar, Default::default());
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
ui.horizontal(|ui| {
for (tab_index, tab) in tabs.iter_mut().enumerate() {
let id = self.id.with((node_index, tab_index, "tab"));
let tab_index = TabIndex(tab_index);
let is_being_dragged =
ui.memory().is_being_dragged(id) && style.tabs_are_draggable;
if is_being_dragged {
ui.output().cursor_icon = CursorIcon::Grabbing;
}
let is_active = *active == tab_index || is_being_dragged;
let label = tab_viewer.title(tab);
let response = if is_being_dragged {
let layer_id = LayerId::new(Order::Tooltip, id);
let response = ui
.with_layer_id(layer_id, |ui| {
style.tab_title(
ui,
label,
is_active,
is_active && Some(node_index) == focused,
is_being_dragged,
id,
expanded_width,
)
})
.response;
let sense = Sense::click_and_drag();
let response = ui.interact(response.rect, id, sense);
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let center = response.rect.center();
let start = state.drag_start.unwrap_or(center);
let delta = pointer_pos - start;
if delta.x.abs() > 30.0 || delta.y.abs() > 6.0 {
ui.ctx().translate_layer(layer_id, delta);
drag_data = Some((node_index, tab_index));
}
}
if response.clicked() {
*active = tab_index;
new_focused = Some(node_index);
}
if response.middle_clicked() && style.show_close_buttons {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
response
} else {
let response = style.tab_title(
ui,
label,
is_active && Some(node_index) == focused,
is_active,
is_being_dragged,
id,
expanded_width,
);
let sense = if response.1 {
Sense::click()
} else {
Sense::click_and_drag()
};
if style.tab_hover_name {
response.0.clone().on_hover_ui(|ui| {
ui.label(tab_viewer.title(tab));
});
}
if style.show_context_menu {
response.0.clone().context_menu(|ui| {
tab_viewer.context_menu(ui, tab);
if style.show_close_buttons && ui.button("Close").clicked()
{
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
});
}
if response.2 {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
let response = ui.interact(response.0.rect, id, sense);
if response.drag_started() {
state.drag_start = response.hover_pos();
}
response
};
if state.drag_start.is_some() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if response.rect.contains(pos) {
tab_hover_rect = Some((response.rect, tab_index));
}
}
}
}
// Add button at the end of the tab bar
if style.show_add_buttons {
let id = self.id.with((node_index, "tab_add"));
let response = style.tab_plus(ui);
let response = ui.interact(response.rect, id, Sense::click());
let popup_id = id.with("tab_add_popup");
popup::popup_under_widget(ui, popup_id, &response, |ui| {
tab_viewer.add_popup(ui, node_index);
});
if response.clicked() {
if style.show_add_popup {
ui.memory().toggle_popup(popup_id);
}
tab_viewer.on_add(node_index);
}
};
});
});
// tab body
if let Some(tab) = tabs.get_mut(active.0) {
let top_y = rect.min.y + height_topbar;
let rect = rect.intersect(Rect::everything_below(top_y));
let rect = expand_to_pixel(rect, pixels_per_point);
*viewport = rect;
if ui.input().pointer.any_click() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if rect.contains(pos) {
new_focused = Some(node_index);
}
}
}
if tab_viewer.clear_background(tab) {
ui.painter()
.rect_filled(rect, 0.0, style.tab_background_color);
}
let mut ui = ui.child_ui(rect, Default::default());
ui.push_id(node_index, |ui| {
if style.tab_include_scrollarea {
ScrollArea::both()
.id_source(
self.id
.with((tab_viewer.title(tab).text(), "egui_dock::Tab")),
)
.show(ui, |ui| {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
let available_rect = ui.available_rect_before_wrap();
ui.expand_to_include_rect(available_rect);
tab_viewer.ui(ui, tab);
});
});
} else {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
tab_viewer.ui(ui, tab);
});
}
});
}
let is_being_dragged = ui.memory().is_anything_being_dragged();
if is_being_dragged && full_response.hovered() {
hover_data = ui.input().pointer.hover_pos().map(|pointer| HoverData {
rect,
dst: node_index,
tabs: tabs_response.hovered().then_some(tabs_response.rect),
tab: tab_hover_rect,
pointer,
});
}
for (tab_index, tab) in tabs.iter_mut().enumerate() {
if tab_viewer.force_close(tab) {
to_remove.push((node_index, TabIndex(tab_index)));
}
}
}
}
let mut emptied = 0;
let mut last = (NodeIndex(usize::MAX), TabIndex(usize::MAX));
for remove in to_remove.iter().rev() {
if let Node::Leaf { tabs, active, .. } = &mut self.tree[remove.0] {
tabs.remove(remove.1 .0);
if remove.1 <= *active {
active.0 = active.0.saturating_sub(1);
}
if tabs.is_empty() {
emptied += 1;
}
if last.0 == remove.0 {
assert!(last.1 > remove.1)
}
last = *remove;
} else {
panic!();
}
}
for _ in 0..emptied {
self.tree.remove_empty_leaf()
}
if let Some(focused) = new_focused {
self.tree.set_focused_node(focused);
}
if let (Some((src, tab_index)), Some(hover)) = (drag_data, hover_data) {
let dst = hover.dst;
if self.tree[src].is_leaf() && self.tree[dst].is_leaf() {
let (target, helper, tap_pos) = hover.resolve();
let id = Id::new("helper");
let layer_id = LayerId::new(Order::Foreground, id);
let painter = ui.ctx().layer_painter(layer_id);
if src != dst || self.tree[dst].tabs_count() > 1 {
painter.rect_filled(helper, 0.0, style.selection_color);
}
if ui.input().pointer.any_released() {
if let Node::Leaf { active, .. } = &mut self.tree[src] {
if *active >= tab_index {
active.0 = active.0.saturating_sub(1);
}
}
let tab = self.tree[src].remove_tab(tab_index).unwrap();
if let Some(target) = target {
self.tree.split(dst, target, 0.5, Node::leaf(tab));
} else {
if let Some(index) = tap_pos {
self.tree[dst].insert_tab(index, tab);
} else {
self.tree[dst].append_tab(tab);
}
self.tree.set_focused_node(dst);
}
self.tree.remove_empty_leaf();
for node in self.tree.iter_mut() {
if let Node::Leaf { tabs, active, .. } = node {
if active.0 >= tabs.len() {
active.0 = 0;
}
}
}
}
}
}
state.store(ui.ctx(), self.id);
}
sourcepub fn split_tabs(
&mut self,
parent: NodeIndex,
split: Split,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
pub fn split_tabs(
&mut self,
parent: NodeIndex,
split: Split,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent
node and assigns them as its children. The first (old) node
inherits content of the parent
from before the split, and the second (new) has tabs
.
fraction
(in range 0..=1) specifies how much of the parent
node’s area the old node is will occupy after the
split.
The new node is placed relatively to the old node, in the direction specified by split
.
Returns the indices of the old node and the new node.
sourcepub fn split_above(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
pub fn split_above(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent
node and assigns them as its children. The first (old) node
inherits content of the parent
from before the split, and the second (new) has tabs
.
fraction
(in range 0..=1) specifies how much of the parent
node’s area the old node is will occupy after the
split.
The new node is placed above the old node.
Returns the indices of the old node and the new node.
sourcepub fn split_below(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
pub fn split_below(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent
node and assigns them as its children. The first (old) node
inherits content of the parent
from before the split, and the second (new) has tabs
.
fraction
(in range 0..=1) specifies how much of the parent
node’s area the old node is will occupy after the
split.
The new node is placed below the old node.
Returns the indices of the old node and the new node.
sourcepub fn split_left(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
pub fn split_left(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent
node and assigns them as its children. The first (old) node
inherits content of the parent
from before the split, and the second (new) has tabs
.
fraction
(in range 0..=1) specifies how much of the parent
node’s area the old node is will occupy after the
split.
The new node is placed to the left of the old node.
Returns the indices of the old node and the new node.
sourcepub fn split_right(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
pub fn split_right(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>
) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent
node and assigns them as its children. The first (old) node
inherits content of the parent
from before the split, and the second (new) has tabs
.
fraction
(in range 0..=1) specifies how much of the parent
node’s area the old node is will occupy after the
split.
The new node is placed to the right of the old node.
Returns the indices of the old node and the new node.
sourcepub fn split(
&mut self,
parent: NodeIndex,
split: Split,
fraction: f32,
new: Node<Tab>
) -> [NodeIndex; 2]
pub fn split(
&mut self,
parent: NodeIndex,
split: Split,
fraction: f32,
new: Node<Tab>
) -> [NodeIndex; 2]
Creates two new nodes by splitting a given parent
node and assigns them as its children. The first (old) node
inherits content of the parent
from before the split, and the second (new) has tabs
.
fraction
(in range 0..=1) specifies how much of the parent
node’s area the old node is will occupy after the
split.
The new node is placed relatively to the old node, in the direction specified by split
.
Returns the indices of the old node and the new node.
Examples found in repository?
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
pub fn split_tabs(
&mut self,
parent: NodeIndex,
split: Split,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2] {
self.split(parent, split, fraction, Node::leaf_with(tabs))
}
/// Creates two new nodes by splitting a given `parent` node and assigns them as its children. The first (old) node
/// inherits content of the `parent` from before the split, and the second (new) has `tabs`.
///
/// `fraction` (in range 0..=1) specifies how much of the `parent` node's area the old node is will occupy after the
/// split.
///
/// The new node is placed above the old node.
///
/// Returns the indices of the old node and the new node.
#[inline(always)]
pub fn split_above(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2] {
self.split(parent, Split::Above, fraction, Node::leaf_with(tabs))
}
/// Creates two new nodes by splitting a given `parent` node and assigns them as its children. The first (old) node
/// inherits content of the `parent` from before the split, and the second (new) has `tabs`.
///
/// `fraction` (in range 0..=1) specifies how much of the `parent` node's area the old node is will occupy after the
/// split.
///
/// The new node is placed below the old node.
///
/// Returns the indices of the old node and the new node.
#[inline(always)]
pub fn split_below(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2] {
self.split(parent, Split::Below, fraction, Node::leaf_with(tabs))
}
/// Creates two new nodes by splitting a given `parent` node and assigns them as its children. The first (old) node
/// inherits content of the `parent` from before the split, and the second (new) has `tabs`.
///
/// `fraction` (in range 0..=1) specifies how much of the `parent` node's area the old node is will occupy after the
/// split.
///
/// The new node is placed to the left of the old node.
///
/// Returns the indices of the old node and the new node.
#[inline(always)]
pub fn split_left(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2] {
self.split(parent, Split::Left, fraction, Node::leaf_with(tabs))
}
/// Creates two new nodes by splitting a given `parent` node and assigns them as its children. The first (old) node
/// inherits content of the `parent` from before the split, and the second (new) has `tabs`.
///
/// `fraction` (in range 0..=1) specifies how much of the `parent` node's area the old node is will occupy after the
/// split.
///
/// The new node is placed to the right of the old node.
///
/// Returns the indices of the old node and the new node.
#[inline(always)]
pub fn split_right(
&mut self,
parent: NodeIndex,
fraction: f32,
tabs: Vec<Tab>,
) -> [NodeIndex; 2] {
self.split(parent, Split::Right, fraction, Node::leaf_with(tabs))
}
More examples
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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
pub fn show_inside(self, ui: &mut Ui, tab_viewer: &mut impl TabViewer<Tab = Tab>) {
let style = self
.style
.unwrap_or_else(|| Style::from_egui(ui.style().as_ref()));
let mut state = State::load(ui.ctx(), self.id);
let mut rect = ui.max_rect();
if let Some(margin) = style.dock_area_padding {
rect.min += margin.left_top();
rect.max -= margin.right_bottom();
ui.painter().rect(
rect,
margin.top,
style.separator_color_idle,
Stroke::new(margin.top, style.border_color),
);
}
if self.tree.is_empty() {
ui.allocate_rect(rect, Sense::hover());
return;
}
self.tree[NodeIndex::root()].set_rect(rect);
let mut drag_data = None;
let mut hover_data = None;
let pixels_per_point = ui.ctx().pixels_per_point();
let px = pixels_per_point.recip();
let focused = self.tree.focused_leaf();
let mut to_remove = Vec::new();
let mut new_focused = None;
// Deal with Horizontal and Vertical nodes first
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
let is_horizontal = self.tree[node_index].is_horizontal();
if let Node::Horizontal { fraction, rect } | Node::Vertical { fraction, rect } =
&mut self.tree[node_index]
{
let rect = expand_to_pixel(*rect, pixels_per_point);
let (response, left, separator, right) = if is_horizontal {
style.hsplit(ui, fraction, rect)
} else {
style.vsplit(ui, fraction, rect)
};
let color = if response.dragged() {
style.separator_color_dragged
} else if response.hovered() {
style.separator_color_hovered
} else {
style.separator_color_idle
};
ui.painter().rect_filled(separator, Rounding::none(), color);
self.tree[node_index.left()].set_rect(left);
self.tree[node_index.right()].set_rect(right);
}
}
// Then process Leaf nodes
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
if let Node::Leaf {
rect,
tabs,
active,
viewport,
} = &mut self.tree[node_index]
{
let rect = *rect;
ui.set_clip_rect(rect);
let height_topbar = style.tab_bar_height;
let bottom_y = rect.min.y + height_topbar;
let tabbar = rect.intersect(Rect::everything_above(bottom_y));
let full_response = ui.allocate_rect(rect, Sense::hover());
let tabs_response = ui.allocate_rect(tabbar, Sense::hover());
let mut tab_hover_rect = None;
// tabs
ui.scope(|ui| {
ui.painter().rect_filled(
tabbar,
style.tab_rounding,
style.tab_bar_background_color,
);
let a = pos2(tabbar.min.x, tabbar.max.y - px);
let b = pos2(tabbar.max.x, tabbar.max.y - px);
ui.painter()
.line_segment([a, b], (px, style.tab_outline_color));
let mut available_width = tabbar.max.x - tabbar.min.x;
if style.show_add_buttons {
available_width -= Style::TAB_PLUS_SIZE;
}
let expanded_width = available_width / (tabs.len() as f32);
let mut ui = ui.child_ui(tabbar, Default::default());
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
ui.horizontal(|ui| {
for (tab_index, tab) in tabs.iter_mut().enumerate() {
let id = self.id.with((node_index, tab_index, "tab"));
let tab_index = TabIndex(tab_index);
let is_being_dragged =
ui.memory().is_being_dragged(id) && style.tabs_are_draggable;
if is_being_dragged {
ui.output().cursor_icon = CursorIcon::Grabbing;
}
let is_active = *active == tab_index || is_being_dragged;
let label = tab_viewer.title(tab);
let response = if is_being_dragged {
let layer_id = LayerId::new(Order::Tooltip, id);
let response = ui
.with_layer_id(layer_id, |ui| {
style.tab_title(
ui,
label,
is_active,
is_active && Some(node_index) == focused,
is_being_dragged,
id,
expanded_width,
)
})
.response;
let sense = Sense::click_and_drag();
let response = ui.interact(response.rect, id, sense);
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let center = response.rect.center();
let start = state.drag_start.unwrap_or(center);
let delta = pointer_pos - start;
if delta.x.abs() > 30.0 || delta.y.abs() > 6.0 {
ui.ctx().translate_layer(layer_id, delta);
drag_data = Some((node_index, tab_index));
}
}
if response.clicked() {
*active = tab_index;
new_focused = Some(node_index);
}
if response.middle_clicked() && style.show_close_buttons {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
response
} else {
let response = style.tab_title(
ui,
label,
is_active && Some(node_index) == focused,
is_active,
is_being_dragged,
id,
expanded_width,
);
let sense = if response.1 {
Sense::click()
} else {
Sense::click_and_drag()
};
if style.tab_hover_name {
response.0.clone().on_hover_ui(|ui| {
ui.label(tab_viewer.title(tab));
});
}
if style.show_context_menu {
response.0.clone().context_menu(|ui| {
tab_viewer.context_menu(ui, tab);
if style.show_close_buttons && ui.button("Close").clicked()
{
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
});
}
if response.2 {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
let response = ui.interact(response.0.rect, id, sense);
if response.drag_started() {
state.drag_start = response.hover_pos();
}
response
};
if state.drag_start.is_some() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if response.rect.contains(pos) {
tab_hover_rect = Some((response.rect, tab_index));
}
}
}
}
// Add button at the end of the tab bar
if style.show_add_buttons {
let id = self.id.with((node_index, "tab_add"));
let response = style.tab_plus(ui);
let response = ui.interact(response.rect, id, Sense::click());
let popup_id = id.with("tab_add_popup");
popup::popup_under_widget(ui, popup_id, &response, |ui| {
tab_viewer.add_popup(ui, node_index);
});
if response.clicked() {
if style.show_add_popup {
ui.memory().toggle_popup(popup_id);
}
tab_viewer.on_add(node_index);
}
};
});
});
// tab body
if let Some(tab) = tabs.get_mut(active.0) {
let top_y = rect.min.y + height_topbar;
let rect = rect.intersect(Rect::everything_below(top_y));
let rect = expand_to_pixel(rect, pixels_per_point);
*viewport = rect;
if ui.input().pointer.any_click() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if rect.contains(pos) {
new_focused = Some(node_index);
}
}
}
if tab_viewer.clear_background(tab) {
ui.painter()
.rect_filled(rect, 0.0, style.tab_background_color);
}
let mut ui = ui.child_ui(rect, Default::default());
ui.push_id(node_index, |ui| {
if style.tab_include_scrollarea {
ScrollArea::both()
.id_source(
self.id
.with((tab_viewer.title(tab).text(), "egui_dock::Tab")),
)
.show(ui, |ui| {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
let available_rect = ui.available_rect_before_wrap();
ui.expand_to_include_rect(available_rect);
tab_viewer.ui(ui, tab);
});
});
} else {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
tab_viewer.ui(ui, tab);
});
}
});
}
let is_being_dragged = ui.memory().is_anything_being_dragged();
if is_being_dragged && full_response.hovered() {
hover_data = ui.input().pointer.hover_pos().map(|pointer| HoverData {
rect,
dst: node_index,
tabs: tabs_response.hovered().then_some(tabs_response.rect),
tab: tab_hover_rect,
pointer,
});
}
for (tab_index, tab) in tabs.iter_mut().enumerate() {
if tab_viewer.force_close(tab) {
to_remove.push((node_index, TabIndex(tab_index)));
}
}
}
}
let mut emptied = 0;
let mut last = (NodeIndex(usize::MAX), TabIndex(usize::MAX));
for remove in to_remove.iter().rev() {
if let Node::Leaf { tabs, active, .. } = &mut self.tree[remove.0] {
tabs.remove(remove.1 .0);
if remove.1 <= *active {
active.0 = active.0.saturating_sub(1);
}
if tabs.is_empty() {
emptied += 1;
}
if last.0 == remove.0 {
assert!(last.1 > remove.1)
}
last = *remove;
} else {
panic!();
}
}
for _ in 0..emptied {
self.tree.remove_empty_leaf()
}
if let Some(focused) = new_focused {
self.tree.set_focused_node(focused);
}
if let (Some((src, tab_index)), Some(hover)) = (drag_data, hover_data) {
let dst = hover.dst;
if self.tree[src].is_leaf() && self.tree[dst].is_leaf() {
let (target, helper, tap_pos) = hover.resolve();
let id = Id::new("helper");
let layer_id = LayerId::new(Order::Foreground, id);
let painter = ui.ctx().layer_painter(layer_id);
if src != dst || self.tree[dst].tabs_count() > 1 {
painter.rect_filled(helper, 0.0, style.selection_color);
}
if ui.input().pointer.any_released() {
if let Node::Leaf { active, .. } = &mut self.tree[src] {
if *active >= tab_index {
active.0 = active.0.saturating_sub(1);
}
}
let tab = self.tree[src].remove_tab(tab_index).unwrap();
if let Some(target) = target {
self.tree.split(dst, target, 0.5, Node::leaf(tab));
} else {
if let Some(index) = tap_pos {
self.tree[dst].insert_tab(index, tab);
} else {
self.tree[dst].append_tab(tab);
}
self.tree.set_focused_node(dst);
}
self.tree.remove_empty_leaf();
for node in self.tree.iter_mut() {
if let Node::Leaf { tabs, active, .. } = node {
if active.0 >= tabs.len() {
active.0 = 0;
}
}
}
}
}
}
state.store(ui.ctx(), self.id);
}
sourcepub fn remove_empty_leaf(&mut self)
pub fn remove_empty_leaf(&mut self)
Removes the first node containing 0 tabs.
Examples found in repository?
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
pub fn remove_tab(&mut self, remove: (NodeIndex, TabIndex)) -> Option<Tab> {
match &mut self[remove.0] {
Node::Leaf { tabs, active, .. } => {
let tab = tabs.remove(remove.1 .0);
if remove.1 <= *active {
active.0 = active.0.saturating_sub(1);
}
if tabs.is_empty() {
self.remove_empty_leaf();
}
Some(tab)
}
_ => None,
}
}
More examples
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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
pub fn show_inside(self, ui: &mut Ui, tab_viewer: &mut impl TabViewer<Tab = Tab>) {
let style = self
.style
.unwrap_or_else(|| Style::from_egui(ui.style().as_ref()));
let mut state = State::load(ui.ctx(), self.id);
let mut rect = ui.max_rect();
if let Some(margin) = style.dock_area_padding {
rect.min += margin.left_top();
rect.max -= margin.right_bottom();
ui.painter().rect(
rect,
margin.top,
style.separator_color_idle,
Stroke::new(margin.top, style.border_color),
);
}
if self.tree.is_empty() {
ui.allocate_rect(rect, Sense::hover());
return;
}
self.tree[NodeIndex::root()].set_rect(rect);
let mut drag_data = None;
let mut hover_data = None;
let pixels_per_point = ui.ctx().pixels_per_point();
let px = pixels_per_point.recip();
let focused = self.tree.focused_leaf();
let mut to_remove = Vec::new();
let mut new_focused = None;
// Deal with Horizontal and Vertical nodes first
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
let is_horizontal = self.tree[node_index].is_horizontal();
if let Node::Horizontal { fraction, rect } | Node::Vertical { fraction, rect } =
&mut self.tree[node_index]
{
let rect = expand_to_pixel(*rect, pixels_per_point);
let (response, left, separator, right) = if is_horizontal {
style.hsplit(ui, fraction, rect)
} else {
style.vsplit(ui, fraction, rect)
};
let color = if response.dragged() {
style.separator_color_dragged
} else if response.hovered() {
style.separator_color_hovered
} else {
style.separator_color_idle
};
ui.painter().rect_filled(separator, Rounding::none(), color);
self.tree[node_index.left()].set_rect(left);
self.tree[node_index.right()].set_rect(right);
}
}
// Then process Leaf nodes
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
if let Node::Leaf {
rect,
tabs,
active,
viewport,
} = &mut self.tree[node_index]
{
let rect = *rect;
ui.set_clip_rect(rect);
let height_topbar = style.tab_bar_height;
let bottom_y = rect.min.y + height_topbar;
let tabbar = rect.intersect(Rect::everything_above(bottom_y));
let full_response = ui.allocate_rect(rect, Sense::hover());
let tabs_response = ui.allocate_rect(tabbar, Sense::hover());
let mut tab_hover_rect = None;
// tabs
ui.scope(|ui| {
ui.painter().rect_filled(
tabbar,
style.tab_rounding,
style.tab_bar_background_color,
);
let a = pos2(tabbar.min.x, tabbar.max.y - px);
let b = pos2(tabbar.max.x, tabbar.max.y - px);
ui.painter()
.line_segment([a, b], (px, style.tab_outline_color));
let mut available_width = tabbar.max.x - tabbar.min.x;
if style.show_add_buttons {
available_width -= Style::TAB_PLUS_SIZE;
}
let expanded_width = available_width / (tabs.len() as f32);
let mut ui = ui.child_ui(tabbar, Default::default());
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
ui.horizontal(|ui| {
for (tab_index, tab) in tabs.iter_mut().enumerate() {
let id = self.id.with((node_index, tab_index, "tab"));
let tab_index = TabIndex(tab_index);
let is_being_dragged =
ui.memory().is_being_dragged(id) && style.tabs_are_draggable;
if is_being_dragged {
ui.output().cursor_icon = CursorIcon::Grabbing;
}
let is_active = *active == tab_index || is_being_dragged;
let label = tab_viewer.title(tab);
let response = if is_being_dragged {
let layer_id = LayerId::new(Order::Tooltip, id);
let response = ui
.with_layer_id(layer_id, |ui| {
style.tab_title(
ui,
label,
is_active,
is_active && Some(node_index) == focused,
is_being_dragged,
id,
expanded_width,
)
})
.response;
let sense = Sense::click_and_drag();
let response = ui.interact(response.rect, id, sense);
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let center = response.rect.center();
let start = state.drag_start.unwrap_or(center);
let delta = pointer_pos - start;
if delta.x.abs() > 30.0 || delta.y.abs() > 6.0 {
ui.ctx().translate_layer(layer_id, delta);
drag_data = Some((node_index, tab_index));
}
}
if response.clicked() {
*active = tab_index;
new_focused = Some(node_index);
}
if response.middle_clicked() && style.show_close_buttons {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
response
} else {
let response = style.tab_title(
ui,
label,
is_active && Some(node_index) == focused,
is_active,
is_being_dragged,
id,
expanded_width,
);
let sense = if response.1 {
Sense::click()
} else {
Sense::click_and_drag()
};
if style.tab_hover_name {
response.0.clone().on_hover_ui(|ui| {
ui.label(tab_viewer.title(tab));
});
}
if style.show_context_menu {
response.0.clone().context_menu(|ui| {
tab_viewer.context_menu(ui, tab);
if style.show_close_buttons && ui.button("Close").clicked()
{
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
});
}
if response.2 {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
let response = ui.interact(response.0.rect, id, sense);
if response.drag_started() {
state.drag_start = response.hover_pos();
}
response
};
if state.drag_start.is_some() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if response.rect.contains(pos) {
tab_hover_rect = Some((response.rect, tab_index));
}
}
}
}
// Add button at the end of the tab bar
if style.show_add_buttons {
let id = self.id.with((node_index, "tab_add"));
let response = style.tab_plus(ui);
let response = ui.interact(response.rect, id, Sense::click());
let popup_id = id.with("tab_add_popup");
popup::popup_under_widget(ui, popup_id, &response, |ui| {
tab_viewer.add_popup(ui, node_index);
});
if response.clicked() {
if style.show_add_popup {
ui.memory().toggle_popup(popup_id);
}
tab_viewer.on_add(node_index);
}
};
});
});
// tab body
if let Some(tab) = tabs.get_mut(active.0) {
let top_y = rect.min.y + height_topbar;
let rect = rect.intersect(Rect::everything_below(top_y));
let rect = expand_to_pixel(rect, pixels_per_point);
*viewport = rect;
if ui.input().pointer.any_click() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if rect.contains(pos) {
new_focused = Some(node_index);
}
}
}
if tab_viewer.clear_background(tab) {
ui.painter()
.rect_filled(rect, 0.0, style.tab_background_color);
}
let mut ui = ui.child_ui(rect, Default::default());
ui.push_id(node_index, |ui| {
if style.tab_include_scrollarea {
ScrollArea::both()
.id_source(
self.id
.with((tab_viewer.title(tab).text(), "egui_dock::Tab")),
)
.show(ui, |ui| {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
let available_rect = ui.available_rect_before_wrap();
ui.expand_to_include_rect(available_rect);
tab_viewer.ui(ui, tab);
});
});
} else {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
tab_viewer.ui(ui, tab);
});
}
});
}
let is_being_dragged = ui.memory().is_anything_being_dragged();
if is_being_dragged && full_response.hovered() {
hover_data = ui.input().pointer.hover_pos().map(|pointer| HoverData {
rect,
dst: node_index,
tabs: tabs_response.hovered().then_some(tabs_response.rect),
tab: tab_hover_rect,
pointer,
});
}
for (tab_index, tab) in tabs.iter_mut().enumerate() {
if tab_viewer.force_close(tab) {
to_remove.push((node_index, TabIndex(tab_index)));
}
}
}
}
let mut emptied = 0;
let mut last = (NodeIndex(usize::MAX), TabIndex(usize::MAX));
for remove in to_remove.iter().rev() {
if let Node::Leaf { tabs, active, .. } = &mut self.tree[remove.0] {
tabs.remove(remove.1 .0);
if remove.1 <= *active {
active.0 = active.0.saturating_sub(1);
}
if tabs.is_empty() {
emptied += 1;
}
if last.0 == remove.0 {
assert!(last.1 > remove.1)
}
last = *remove;
} else {
panic!();
}
}
for _ in 0..emptied {
self.tree.remove_empty_leaf()
}
if let Some(focused) = new_focused {
self.tree.set_focused_node(focused);
}
if let (Some((src, tab_index)), Some(hover)) = (drag_data, hover_data) {
let dst = hover.dst;
if self.tree[src].is_leaf() && self.tree[dst].is_leaf() {
let (target, helper, tap_pos) = hover.resolve();
let id = Id::new("helper");
let layer_id = LayerId::new(Order::Foreground, id);
let painter = ui.ctx().layer_painter(layer_id);
if src != dst || self.tree[dst].tabs_count() > 1 {
painter.rect_filled(helper, 0.0, style.selection_color);
}
if ui.input().pointer.any_released() {
if let Node::Leaf { active, .. } = &mut self.tree[src] {
if *active >= tab_index {
active.0 = active.0.saturating_sub(1);
}
}
let tab = self.tree[src].remove_tab(tab_index).unwrap();
if let Some(target) = target {
self.tree.split(dst, target, 0.5, Node::leaf(tab));
} else {
if let Some(index) = tap_pos {
self.tree[dst].insert_tab(index, tab);
} else {
self.tree[dst].append_tab(tab);
}
self.tree.set_focused_node(dst);
}
self.tree.remove_empty_leaf();
for node in self.tree.iter_mut() {
if let Node::Leaf { tabs, active, .. } = node {
if active.0 >= tabs.len() {
active.0 = 0;
}
}
}
}
}
}
state.store(ui.ctx(), self.id);
}
sourcepub fn push_to_first_leaf(&mut self, tab: Tab)
pub fn push_to_first_leaf(&mut self, tab: Tab)
Push a tab to the first leaf it finds or creates a leaf if an empty spot is encountered.
Examples found in repository?
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
pub fn push_to_focused_leaf(&mut self, tab: Tab) {
match self.focused_node {
Some(node) => {
if self.tree.is_empty() {
self.tree.push(Node::leaf(tab));
self.focused_node = Some(NodeIndex::root());
} else {
match &mut self[node] {
Node::Empty => {
self[node] = Node::leaf(tab);
self.focused_node = Some(node);
}
Node::Leaf { tabs, active, .. } => {
*active = TabIndex(tabs.len());
tabs.push(tab);
self.focused_node = Some(node);
}
_ => {
self.push_to_first_leaf(tab);
}
}
}
}
None => {
if self.tree.is_empty() {
self.tree.push(Node::leaf(tab));
self.focused_node = Some(NodeIndex::root());
} else {
self.push_to_first_leaf(tab);
}
}
}
}
sourcepub fn focused_leaf(&self) -> Option<NodeIndex>
pub fn focused_leaf(&self) -> Option<NodeIndex>
Currently focused leaf.
Examples found in repository?
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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
pub fn show_inside(self, ui: &mut Ui, tab_viewer: &mut impl TabViewer<Tab = Tab>) {
let style = self
.style
.unwrap_or_else(|| Style::from_egui(ui.style().as_ref()));
let mut state = State::load(ui.ctx(), self.id);
let mut rect = ui.max_rect();
if let Some(margin) = style.dock_area_padding {
rect.min += margin.left_top();
rect.max -= margin.right_bottom();
ui.painter().rect(
rect,
margin.top,
style.separator_color_idle,
Stroke::new(margin.top, style.border_color),
);
}
if self.tree.is_empty() {
ui.allocate_rect(rect, Sense::hover());
return;
}
self.tree[NodeIndex::root()].set_rect(rect);
let mut drag_data = None;
let mut hover_data = None;
let pixels_per_point = ui.ctx().pixels_per_point();
let px = pixels_per_point.recip();
let focused = self.tree.focused_leaf();
let mut to_remove = Vec::new();
let mut new_focused = None;
// Deal with Horizontal and Vertical nodes first
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
let is_horizontal = self.tree[node_index].is_horizontal();
if let Node::Horizontal { fraction, rect } | Node::Vertical { fraction, rect } =
&mut self.tree[node_index]
{
let rect = expand_to_pixel(*rect, pixels_per_point);
let (response, left, separator, right) = if is_horizontal {
style.hsplit(ui, fraction, rect)
} else {
style.vsplit(ui, fraction, rect)
};
let color = if response.dragged() {
style.separator_color_dragged
} else if response.hovered() {
style.separator_color_hovered
} else {
style.separator_color_idle
};
ui.painter().rect_filled(separator, Rounding::none(), color);
self.tree[node_index.left()].set_rect(left);
self.tree[node_index.right()].set_rect(right);
}
}
// Then process Leaf nodes
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
if let Node::Leaf {
rect,
tabs,
active,
viewport,
} = &mut self.tree[node_index]
{
let rect = *rect;
ui.set_clip_rect(rect);
let height_topbar = style.tab_bar_height;
let bottom_y = rect.min.y + height_topbar;
let tabbar = rect.intersect(Rect::everything_above(bottom_y));
let full_response = ui.allocate_rect(rect, Sense::hover());
let tabs_response = ui.allocate_rect(tabbar, Sense::hover());
let mut tab_hover_rect = None;
// tabs
ui.scope(|ui| {
ui.painter().rect_filled(
tabbar,
style.tab_rounding,
style.tab_bar_background_color,
);
let a = pos2(tabbar.min.x, tabbar.max.y - px);
let b = pos2(tabbar.max.x, tabbar.max.y - px);
ui.painter()
.line_segment([a, b], (px, style.tab_outline_color));
let mut available_width = tabbar.max.x - tabbar.min.x;
if style.show_add_buttons {
available_width -= Style::TAB_PLUS_SIZE;
}
let expanded_width = available_width / (tabs.len() as f32);
let mut ui = ui.child_ui(tabbar, Default::default());
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
ui.horizontal(|ui| {
for (tab_index, tab) in tabs.iter_mut().enumerate() {
let id = self.id.with((node_index, tab_index, "tab"));
let tab_index = TabIndex(tab_index);
let is_being_dragged =
ui.memory().is_being_dragged(id) && style.tabs_are_draggable;
if is_being_dragged {
ui.output().cursor_icon = CursorIcon::Grabbing;
}
let is_active = *active == tab_index || is_being_dragged;
let label = tab_viewer.title(tab);
let response = if is_being_dragged {
let layer_id = LayerId::new(Order::Tooltip, id);
let response = ui
.with_layer_id(layer_id, |ui| {
style.tab_title(
ui,
label,
is_active,
is_active && Some(node_index) == focused,
is_being_dragged,
id,
expanded_width,
)
})
.response;
let sense = Sense::click_and_drag();
let response = ui.interact(response.rect, id, sense);
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let center = response.rect.center();
let start = state.drag_start.unwrap_or(center);
let delta = pointer_pos - start;
if delta.x.abs() > 30.0 || delta.y.abs() > 6.0 {
ui.ctx().translate_layer(layer_id, delta);
drag_data = Some((node_index, tab_index));
}
}
if response.clicked() {
*active = tab_index;
new_focused = Some(node_index);
}
if response.middle_clicked() && style.show_close_buttons {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
response
} else {
let response = style.tab_title(
ui,
label,
is_active && Some(node_index) == focused,
is_active,
is_being_dragged,
id,
expanded_width,
);
let sense = if response.1 {
Sense::click()
} else {
Sense::click_and_drag()
};
if style.tab_hover_name {
response.0.clone().on_hover_ui(|ui| {
ui.label(tab_viewer.title(tab));
});
}
if style.show_context_menu {
response.0.clone().context_menu(|ui| {
tab_viewer.context_menu(ui, tab);
if style.show_close_buttons && ui.button("Close").clicked()
{
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
});
}
if response.2 {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
let response = ui.interact(response.0.rect, id, sense);
if response.drag_started() {
state.drag_start = response.hover_pos();
}
response
};
if state.drag_start.is_some() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if response.rect.contains(pos) {
tab_hover_rect = Some((response.rect, tab_index));
}
}
}
}
// Add button at the end of the tab bar
if style.show_add_buttons {
let id = self.id.with((node_index, "tab_add"));
let response = style.tab_plus(ui);
let response = ui.interact(response.rect, id, Sense::click());
let popup_id = id.with("tab_add_popup");
popup::popup_under_widget(ui, popup_id, &response, |ui| {
tab_viewer.add_popup(ui, node_index);
});
if response.clicked() {
if style.show_add_popup {
ui.memory().toggle_popup(popup_id);
}
tab_viewer.on_add(node_index);
}
};
});
});
// tab body
if let Some(tab) = tabs.get_mut(active.0) {
let top_y = rect.min.y + height_topbar;
let rect = rect.intersect(Rect::everything_below(top_y));
let rect = expand_to_pixel(rect, pixels_per_point);
*viewport = rect;
if ui.input().pointer.any_click() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if rect.contains(pos) {
new_focused = Some(node_index);
}
}
}
if tab_viewer.clear_background(tab) {
ui.painter()
.rect_filled(rect, 0.0, style.tab_background_color);
}
let mut ui = ui.child_ui(rect, Default::default());
ui.push_id(node_index, |ui| {
if style.tab_include_scrollarea {
ScrollArea::both()
.id_source(
self.id
.with((tab_viewer.title(tab).text(), "egui_dock::Tab")),
)
.show(ui, |ui| {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
let available_rect = ui.available_rect_before_wrap();
ui.expand_to_include_rect(available_rect);
tab_viewer.ui(ui, tab);
});
});
} else {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
tab_viewer.ui(ui, tab);
});
}
});
}
let is_being_dragged = ui.memory().is_anything_being_dragged();
if is_being_dragged && full_response.hovered() {
hover_data = ui.input().pointer.hover_pos().map(|pointer| HoverData {
rect,
dst: node_index,
tabs: tabs_response.hovered().then_some(tabs_response.rect),
tab: tab_hover_rect,
pointer,
});
}
for (tab_index, tab) in tabs.iter_mut().enumerate() {
if tab_viewer.force_close(tab) {
to_remove.push((node_index, TabIndex(tab_index)));
}
}
}
}
let mut emptied = 0;
let mut last = (NodeIndex(usize::MAX), TabIndex(usize::MAX));
for remove in to_remove.iter().rev() {
if let Node::Leaf { tabs, active, .. } = &mut self.tree[remove.0] {
tabs.remove(remove.1 .0);
if remove.1 <= *active {
active.0 = active.0.saturating_sub(1);
}
if tabs.is_empty() {
emptied += 1;
}
if last.0 == remove.0 {
assert!(last.1 > remove.1)
}
last = *remove;
} else {
panic!();
}
}
for _ in 0..emptied {
self.tree.remove_empty_leaf()
}
if let Some(focused) = new_focused {
self.tree.set_focused_node(focused);
}
if let (Some((src, tab_index)), Some(hover)) = (drag_data, hover_data) {
let dst = hover.dst;
if self.tree[src].is_leaf() && self.tree[dst].is_leaf() {
let (target, helper, tap_pos) = hover.resolve();
let id = Id::new("helper");
let layer_id = LayerId::new(Order::Foreground, id);
let painter = ui.ctx().layer_painter(layer_id);
if src != dst || self.tree[dst].tabs_count() > 1 {
painter.rect_filled(helper, 0.0, style.selection_color);
}
if ui.input().pointer.any_released() {
if let Node::Leaf { active, .. } = &mut self.tree[src] {
if *active >= tab_index {
active.0 = active.0.saturating_sub(1);
}
}
let tab = self.tree[src].remove_tab(tab_index).unwrap();
if let Some(target) = target {
self.tree.split(dst, target, 0.5, Node::leaf(tab));
} else {
if let Some(index) = tap_pos {
self.tree[dst].insert_tab(index, tab);
} else {
self.tree[dst].append_tab(tab);
}
self.tree.set_focused_node(dst);
}
self.tree.remove_empty_leaf();
for node in self.tree.iter_mut() {
if let Node::Leaf { tabs, active, .. } = node {
if active.0 >= tabs.len() {
active.0 = 0;
}
}
}
}
}
}
state.store(ui.ctx(), self.id);
}
sourcepub fn set_focused_node(&mut self, node_index: NodeIndex)
pub fn set_focused_node(&mut self, node_index: NodeIndex)
Sets the currently focused leaf to node_index
if the node at node_index
is a leaf.
Examples found in repository?
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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
pub fn show_inside(self, ui: &mut Ui, tab_viewer: &mut impl TabViewer<Tab = Tab>) {
let style = self
.style
.unwrap_or_else(|| Style::from_egui(ui.style().as_ref()));
let mut state = State::load(ui.ctx(), self.id);
let mut rect = ui.max_rect();
if let Some(margin) = style.dock_area_padding {
rect.min += margin.left_top();
rect.max -= margin.right_bottom();
ui.painter().rect(
rect,
margin.top,
style.separator_color_idle,
Stroke::new(margin.top, style.border_color),
);
}
if self.tree.is_empty() {
ui.allocate_rect(rect, Sense::hover());
return;
}
self.tree[NodeIndex::root()].set_rect(rect);
let mut drag_data = None;
let mut hover_data = None;
let pixels_per_point = ui.ctx().pixels_per_point();
let px = pixels_per_point.recip();
let focused = self.tree.focused_leaf();
let mut to_remove = Vec::new();
let mut new_focused = None;
// Deal with Horizontal and Vertical nodes first
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
let is_horizontal = self.tree[node_index].is_horizontal();
if let Node::Horizontal { fraction, rect } | Node::Vertical { fraction, rect } =
&mut self.tree[node_index]
{
let rect = expand_to_pixel(*rect, pixels_per_point);
let (response, left, separator, right) = if is_horizontal {
style.hsplit(ui, fraction, rect)
} else {
style.vsplit(ui, fraction, rect)
};
let color = if response.dragged() {
style.separator_color_dragged
} else if response.hovered() {
style.separator_color_hovered
} else {
style.separator_color_idle
};
ui.painter().rect_filled(separator, Rounding::none(), color);
self.tree[node_index.left()].set_rect(left);
self.tree[node_index.right()].set_rect(right);
}
}
// Then process Leaf nodes
for node_index in 0..self.tree.len() {
let node_index = NodeIndex(node_index);
if let Node::Leaf {
rect,
tabs,
active,
viewport,
} = &mut self.tree[node_index]
{
let rect = *rect;
ui.set_clip_rect(rect);
let height_topbar = style.tab_bar_height;
let bottom_y = rect.min.y + height_topbar;
let tabbar = rect.intersect(Rect::everything_above(bottom_y));
let full_response = ui.allocate_rect(rect, Sense::hover());
let tabs_response = ui.allocate_rect(tabbar, Sense::hover());
let mut tab_hover_rect = None;
// tabs
ui.scope(|ui| {
ui.painter().rect_filled(
tabbar,
style.tab_rounding,
style.tab_bar_background_color,
);
let a = pos2(tabbar.min.x, tabbar.max.y - px);
let b = pos2(tabbar.max.x, tabbar.max.y - px);
ui.painter()
.line_segment([a, b], (px, style.tab_outline_color));
let mut available_width = tabbar.max.x - tabbar.min.x;
if style.show_add_buttons {
available_width -= Style::TAB_PLUS_SIZE;
}
let expanded_width = available_width / (tabs.len() as f32);
let mut ui = ui.child_ui(tabbar, Default::default());
ui.spacing_mut().item_spacing = vec2(0.0, 0.0);
ui.horizontal(|ui| {
for (tab_index, tab) in tabs.iter_mut().enumerate() {
let id = self.id.with((node_index, tab_index, "tab"));
let tab_index = TabIndex(tab_index);
let is_being_dragged =
ui.memory().is_being_dragged(id) && style.tabs_are_draggable;
if is_being_dragged {
ui.output().cursor_icon = CursorIcon::Grabbing;
}
let is_active = *active == tab_index || is_being_dragged;
let label = tab_viewer.title(tab);
let response = if is_being_dragged {
let layer_id = LayerId::new(Order::Tooltip, id);
let response = ui
.with_layer_id(layer_id, |ui| {
style.tab_title(
ui,
label,
is_active,
is_active && Some(node_index) == focused,
is_being_dragged,
id,
expanded_width,
)
})
.response;
let sense = Sense::click_and_drag();
let response = ui.interact(response.rect, id, sense);
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let center = response.rect.center();
let start = state.drag_start.unwrap_or(center);
let delta = pointer_pos - start;
if delta.x.abs() > 30.0 || delta.y.abs() > 6.0 {
ui.ctx().translate_layer(layer_id, delta);
drag_data = Some((node_index, tab_index));
}
}
if response.clicked() {
*active = tab_index;
new_focused = Some(node_index);
}
if response.middle_clicked() && style.show_close_buttons {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
response
} else {
let response = style.tab_title(
ui,
label,
is_active && Some(node_index) == focused,
is_active,
is_being_dragged,
id,
expanded_width,
);
let sense = if response.1 {
Sense::click()
} else {
Sense::click_and_drag()
};
if style.tab_hover_name {
response.0.clone().on_hover_ui(|ui| {
ui.label(tab_viewer.title(tab));
});
}
if style.show_context_menu {
response.0.clone().context_menu(|ui| {
tab_viewer.context_menu(ui, tab);
if style.show_close_buttons && ui.button("Close").clicked()
{
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
});
}
if response.2 {
if tab_viewer.on_close(tab) {
to_remove.push((node_index, tab_index));
} else {
*active = tab_index;
new_focused = Some(node_index);
}
}
let response = ui.interact(response.0.rect, id, sense);
if response.drag_started() {
state.drag_start = response.hover_pos();
}
response
};
if state.drag_start.is_some() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if response.rect.contains(pos) {
tab_hover_rect = Some((response.rect, tab_index));
}
}
}
}
// Add button at the end of the tab bar
if style.show_add_buttons {
let id = self.id.with((node_index, "tab_add"));
let response = style.tab_plus(ui);
let response = ui.interact(response.rect, id, Sense::click());
let popup_id = id.with("tab_add_popup");
popup::popup_under_widget(ui, popup_id, &response, |ui| {
tab_viewer.add_popup(ui, node_index);
});
if response.clicked() {
if style.show_add_popup {
ui.memory().toggle_popup(popup_id);
}
tab_viewer.on_add(node_index);
}
};
});
});
// tab body
if let Some(tab) = tabs.get_mut(active.0) {
let top_y = rect.min.y + height_topbar;
let rect = rect.intersect(Rect::everything_below(top_y));
let rect = expand_to_pixel(rect, pixels_per_point);
*viewport = rect;
if ui.input().pointer.any_click() {
if let Some(pos) = ui.input().pointer.hover_pos() {
if rect.contains(pos) {
new_focused = Some(node_index);
}
}
}
if tab_viewer.clear_background(tab) {
ui.painter()
.rect_filled(rect, 0.0, style.tab_background_color);
}
let mut ui = ui.child_ui(rect, Default::default());
ui.push_id(node_index, |ui| {
if style.tab_include_scrollarea {
ScrollArea::both()
.id_source(
self.id
.with((tab_viewer.title(tab).text(), "egui_dock::Tab")),
)
.show(ui, |ui| {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
let available_rect = ui.available_rect_before_wrap();
ui.expand_to_include_rect(available_rect);
tab_viewer.ui(ui, tab);
});
});
} else {
Frame::none()
.inner_margin(tab_viewer.inner_margin_override(&style))
.show(ui, |ui| {
tab_viewer.ui(ui, tab);
});
}
});
}
let is_being_dragged = ui.memory().is_anything_being_dragged();
if is_being_dragged && full_response.hovered() {
hover_data = ui.input().pointer.hover_pos().map(|pointer| HoverData {
rect,
dst: node_index,
tabs: tabs_response.hovered().then_some(tabs_response.rect),
tab: tab_hover_rect,
pointer,
});
}
for (tab_index, tab) in tabs.iter_mut().enumerate() {
if tab_viewer.force_close(tab) {
to_remove.push((node_index, TabIndex(tab_index)));
}
}
}
}
let mut emptied = 0;
let mut last = (NodeIndex(usize::MAX), TabIndex(usize::MAX));
for remove in to_remove.iter().rev() {
if let Node::Leaf { tabs, active, .. } = &mut self.tree[remove.0] {
tabs.remove(remove.1 .0);
if remove.1 <= *active {
active.0 = active.0.saturating_sub(1);
}
if tabs.is_empty() {
emptied += 1;
}
if last.0 == remove.0 {
assert!(last.1 > remove.1)
}
last = *remove;
} else {
panic!();
}
}
for _ in 0..emptied {
self.tree.remove_empty_leaf()
}
if let Some(focused) = new_focused {
self.tree.set_focused_node(focused);
}
if let (Some((src, tab_index)), Some(hover)) = (drag_data, hover_data) {
let dst = hover.dst;
if self.tree[src].is_leaf() && self.tree[dst].is_leaf() {
let (target, helper, tap_pos) = hover.resolve();
let id = Id::new("helper");
let layer_id = LayerId::new(Order::Foreground, id);
let painter = ui.ctx().layer_painter(layer_id);
if src != dst || self.tree[dst].tabs_count() > 1 {
painter.rect_filled(helper, 0.0, style.selection_color);
}
if ui.input().pointer.any_released() {
if let Node::Leaf { active, .. } = &mut self.tree[src] {
if *active >= tab_index {
active.0 = active.0.saturating_sub(1);
}
}
let tab = self.tree[src].remove_tab(tab_index).unwrap();
if let Some(target) = target {
self.tree.split(dst, target, 0.5, Node::leaf(tab));
} else {
if let Some(index) = tap_pos {
self.tree[dst].insert_tab(index, tab);
} else {
self.tree[dst].append_tab(tab);
}
self.tree.set_focused_node(dst);
}
self.tree.remove_empty_leaf();
for node in self.tree.iter_mut() {
if let Node::Leaf { tabs, active, .. } = node {
if active.0 >= tabs.len() {
active.0 = 0;
}
}
}
}
}
}
state.store(ui.ctx(), self.id);
}
sourcepub fn set_active_tab(&mut self, node_index: NodeIndex, tab_index: TabIndex)
pub fn set_active_tab(&mut self, node_index: NodeIndex, tab_index: TabIndex)
Sets which is the active tab within a specific node.
sourcepub fn push_to_focused_leaf(&mut self, tab: Tab)
pub fn push_to_focused_leaf(&mut self, tab: Tab)
Pushes tab
to the currently focused leaf.
If no leaf is focused it will be pushed to the first available leaf.
If no leaf is available then a new leaf will be created.