Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes #225

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# egui_dock changelog

## 0.11.2 - 2024-02-16

### Fixed
From [#225](https:/Adanos020/egui_dock/pull/225):
- Tabs now always appear at the pointer position while being dragged.
- Retaining tabs no longer breaks the binary tree leading to a panic.
- Filtering tabs no longer leaves some leaves empty and now correctly rearranges the tree.

## 0.11.1 - 2024-02-09

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "egui_dock"
description = "Docking system for egui - an immediate-mode GUI library for Rust"
authors = ["lain-dono", "Adam Gąsior (Adanos020)"]
version = "0.11.1"
version = "0.11.2"
edition = "2021"
rust-version = "1.72"
license = "MIT"
Expand Down
46 changes: 37 additions & 9 deletions src/dock_state/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use node_index::NodeIndex;
pub use tab_index::TabIndex;
pub use tab_iter::TabIter;

use egui::ahash::HashSet;
use egui::Rect;
use std::{
fmt,
Expand Down Expand Up @@ -744,20 +745,24 @@ impl<Tab> Tree<Tab> {
focused_node,
nodes,
} = self;
let mut emptied_nodes = HashSet::default();
let nodes = nodes
.iter()
.filter_map(|node| {
.enumerate()
.map(|(index, node)| {
let node = node.filter_map_tabs(function.clone());
match node {
Node::Leaf { ref tabs, .. } => (!tabs.is_empty()).then_some(node),
_ => Some(node),
if node.is_empty() {
emptied_nodes.insert(NodeIndex(index));
}
node
})
.collect();
Tree {
let mut new_tree = Tree {
nodes,
focused_node: *focused_node,
}
};
new_tree.balance(emptied_nodes);
new_tree
}

/// Returns a new [`Tree`] while mapping the tab type.
Expand All @@ -784,10 +789,33 @@ impl<Tab> Tree<Tab> {
where
F: Clone + FnMut(&mut Tab) -> bool,
{
self.nodes.retain_mut(|node| {
let mut emptied_nodes = HashSet::default();
for (index, node) in self.nodes.iter_mut().enumerate() {
node.retain_tabs(predicate.clone());
!node.is_empty()
});
if node.is_empty() {
emptied_nodes.insert(NodeIndex(index));
}
}
self.balance(emptied_nodes);
}

fn balance(&mut self, emptied_nodes: HashSet<NodeIndex>) {
let mut emptied_parents = HashSet::default();
for parent_index in emptied_nodes.into_iter().filter_map(|ni| ni.parent()) {
if self[parent_index.left()].is_empty() && self[parent_index.right()].is_empty() {
self[parent_index] = Node::Empty;
emptied_parents.insert(parent_index);
} else if self[parent_index.left()].is_empty() {
self.nodes.swap(parent_index.0, parent_index.right().0);
self[parent_index.right()] = Node::Empty;
} else if self[parent_index.right()].is_empty() {
self.nodes.swap(parent_index.0, parent_index.left().0);
self[parent_index.left()] = Node::Empty;
}
}
if !emptied_parents.is_empty() {
self.balance(emptied_parents);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/widgets/dock_area/show/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
.with((tab_index, "tab"));
let tab_index = TabIndex(tab_index);
let is_being_dragged = tabs_ui.memory(|mem| mem.is_being_dragged(id))
&& tabs_ui.input(|i| i.pointer.primary_down() || i.pointer.primary_released())
&& tabs_ui.input(|i| i.pointer.is_decidedly_dragging())
&& self.draggable_tabs;

if is_being_dragged {
Expand Down Expand Up @@ -255,8 +255,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
.response;
let title_id = response.id;

let sense = Sense::click_and_drag();
let response = tabs_ui.interact(response.rect, id, sense);
let response = tabs_ui.interact(response.rect, id, Sense::click_and_drag());

if let Some(pointer_pos) = tabs_ui.ctx().pointer_interact_pos() {
let start = *state.drag_start.get_or_insert(pointer_pos);
Expand Down Expand Up @@ -369,6 +368,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
// the underlying tab
if state.drag_start.is_some() && response.rect.contains(pos) {
self.tab_hover_rect = Some((response.rect, tab_index));
state.drag_start = None;
}
}

Expand Down
Loading