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
/// Wrapper around indices to the collection of surfaces inside a [`DockState`].
pub mod surface_index;
pub mod tree;
/// Represents an area in which a dock tree is rendered.
pub mod surface;
/// Specifies text displayed in different elements of the [`DockArea`](crate::DockArea).
pub mod translations;
/// Window states which tells floating tabs how to be displayed inside their window,
pub mod window_state;
pub use surface::Surface;
pub use surface_index::SurfaceIndex;
pub use window_state::WindowState;
use egui::Rect;
use crate::{Node, NodeIndex, Split, TabDestination, TabIndex, TabInsert, Translations, Tree};
/// The heart of `egui_dock`.
///
/// This structure holds a collection of surfaces, each of which stores a tree in which tabs are arranged.
///
/// Indexing it with a [`SurfaceIndex`] will yield a [`Tree`] which then contains nodes and tabs.
///
/// [`DockState`] is generic, so you can use any type of data to represent a tab.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct DockState<Tab> {
surfaces: Vec<Surface<Tab>>,
focused_surface: Option<SurfaceIndex>, // Part of the tree which is in focus.
/// Contains translations of text shown in [`DockArea`](crate::DockArea).
pub translations: Translations,
}
impl<Tab> std::ops::Index<SurfaceIndex> for DockState<Tab> {
type Output = Tree<Tab>;
#[inline(always)]
fn index(&self, index: SurfaceIndex) -> &Self::Output {
match self.surfaces[index.0].node_tree() {
Some(tree) => tree,
None => {
panic!("There did not exist a tree at surface index {}", index.0);
}
}
}
}
impl<Tab> std::ops::IndexMut<SurfaceIndex> for DockState<Tab> {
#[inline(always)]
fn index_mut(&mut self, index: SurfaceIndex) -> &mut Self::Output {
match self.surfaces[index.0].node_tree_mut() {
Some(tree) => tree,
None => {
panic!("There did not exist a tree at surface index {}", index.0);
}
}
}
}
impl<Tab> DockState<Tab> {
/// Create a new tree with given tabs at the main surface's root node.
pub fn new(tabs: Vec<Tab>) -> Self {
Self {
surfaces: vec![Surface::Main(Tree::new(tabs))],
focused_surface: None,
translations: Translations::english(),
}
}
/// Sets translations of text later displayed in [`DockArea`](crate::DockArea).
pub fn with_translations(mut self, translations: Translations) -> Self {
self.translations = translations;
self
}
/// Get an immutable borrow to the tree at the main surface.
pub fn main_surface(&self) -> &Tree<Tab> {
&self[SurfaceIndex::main()]
}
/// Get a mutable borrow to the tree at the main surface.
pub fn main_surface_mut(&mut self) -> &mut Tree<Tab> {
&mut self[SurfaceIndex::main()]
}
/// Get the [`WindowState`] which corresponds to a [`SurfaceIndex`].
///
/// Returns `None` if the surface is [`Empty`](Surface::Empty), [`Main`](Surface::Main), or doesn't exist.
///
/// This can be used to modify properties of a window, e.g. size and position.
///
/// # Examples
///
/// ```rust
/// # use egui_dock::DockState;
/// # use egui::{Vec2, Pos2};
/// let mut dock_state = DockState::new(vec![]);
/// let mut surface_index = dock_state.add_window(vec!["Window Tab".to_string()]);
/// let window_state = dock_state.get_window_state_mut(surface_index).unwrap();
///
/// window_state.set_position(Pos2::ZERO);
/// window_state.set_size(Vec2::splat(100.0));
/// ```
pub fn get_window_state_mut(&mut self, surface: SurfaceIndex) -> Option<&mut WindowState> {
match &mut self.surfaces[surface.0] {
Surface::Window(_, state) => Some(state),
_ => None,
}
}
/// Get the [`WindowState`] which corresponds to a [`SurfaceIndex`].
///
/// Returns `None` if the surface is an [`Empty`](Surface::Empty), [`Main`](Surface::Main), or doesn't exist.
pub fn get_window_state(&mut self, surface: SurfaceIndex) -> Option<&WindowState> {
match &self.surfaces[surface.0] {
Surface::Window(_, state) => Some(state),
_ => None,
}
}
/// Returns the viewport [`Rect`] and the `Tab` inside the focused leaf node or `None` if no node is in focus.
#[inline]
pub fn find_active_focused(&mut self) -> Option<(Rect, &mut Tab)> {
self.focused_surface
.and_then(|surface| self[surface].find_active_focused())
}
/// Get a mutable borrow to the raw surface from a surface index.
#[inline]
pub fn get_surface_mut(&mut self, surface: SurfaceIndex) -> Option<&mut Surface<Tab>> {
self.surfaces.get_mut(surface.0)
}
/// Get an immutable borrow to the raw surface from a surface index.
#[inline]
pub fn get_surface(&self, surface: SurfaceIndex) -> Option<&Surface<Tab>> {
self.surfaces.get(surface.0)
}
/// Returns true if the specified surface exists and isn't [`Empty`](Surface::Empty).
#[inline]
pub fn is_surface_valid(&self, surface_index: SurfaceIndex) -> bool {
self.surfaces
.get(surface_index.0)
.map_or(false, |surface| !surface.is_empty())
}
/// Returns a list of all valid [`SurfaceIndex`]es.
#[inline]
pub(crate) fn valid_surface_indices(&self) -> Box<[SurfaceIndex]> {
(0..self.surfaces.len())
.filter_map(|index| {
let index = SurfaceIndex(index);
self.is_surface_valid(index).then_some(index)
})
.collect()
}
/// Remove a surface based on its [`SurfaceIndex`]
///
/// Returns the removed surface or `None` if it didn't exist.
///
/// # Panics
///
/// Panics if you try to remove the main surface: `SurfaceIndex::main()`.
pub fn remove_surface(&mut self, surface_index: SurfaceIndex) -> Option<Surface<Tab>> {
assert!(!surface_index.is_main());
(surface_index.0 < self.surfaces.len()).then(|| {
self.focused_surface = Some(SurfaceIndex::main());
if surface_index.0 == self.surfaces.len() - 1 {
self.surfaces.pop().unwrap()
} else {
let dest = &mut self.surfaces[surface_index.0];
std::mem::replace(dest, Surface::Empty)
}
})
}
/// Sets which is the active tab within a specific node on a given surface.
#[inline]
pub fn set_active_tab(
&mut self,
(surface_index, node_index, tab_index): (SurfaceIndex, NodeIndex, TabIndex),
) {
if let Some(Node::Leaf { active, .. }) = self[surface_index].nodes.get_mut(node_index.0) {
*active = tab_index;
}
}
/// Sets the currently focused leaf to `node_index` if the node at `node_index` is a leaf.
#[inline]
pub fn set_focused_node_and_surface(
&mut self,
(surface_index, node_index): (SurfaceIndex, NodeIndex),
) {
if self.is_surface_valid(surface_index) && node_index.0 < self[surface_index].len() {
// I don't want this code to be evaluated until im absolutely sure the surface index is valid.
if self[surface_index][node_index].is_leaf() {
self.focused_surface = Some(surface_index);
self[surface_index].set_focused_node(node_index);
return;
}
}
self.focused_surface = None;
}
/// Moves a tab from a node to another node.
/// You need to specify with [`TabDestination`] how the tab should be moved.
pub fn move_tab(
&mut self,
(src_surface, src_node, src_tab): (SurfaceIndex, NodeIndex, TabIndex),
dst_tab: impl Into<TabDestination>,
) {
match dst_tab.into() {
TabDestination::Window(position) => {
self.detach_tab((src_surface, src_node, src_tab), position);
return;
}
TabDestination::Node(dst_surface, dst_node, dst_tab) => {
// Moving a single tab inside its own node is a no-op
if src_surface == dst_surface
&& src_node == dst_node
&& self[src_surface][src_node].tabs_count() == 1
{
return;
}
// Call `Node::remove_tab` to avoid auto remove of the node by `Tree::remove_tab` from Tree.
let tab = self[src_surface][src_node].remove_tab(src_tab).unwrap();
match dst_tab {
TabInsert::Split(split) => {
self[dst_surface].split(dst_node, split, 0.5, Node::leaf(tab));
}
TabInsert::Insert(index) => self[dst_surface][dst_node].insert_tab(index, tab),
TabInsert::Append => self[dst_surface][dst_node].append_tab(tab),
}
}
TabDestination::EmptySurface(dst_surface) => {
assert!(self[dst_surface].is_empty());
let tab = self[src_surface][src_node].remove_tab(src_tab).unwrap();
self[dst_surface] = Tree::new(vec![tab])
}
}
if self[src_surface][src_node].is_leaf() && self[src_surface][src_node].tabs_count() == 0 {
self[src_surface].remove_leaf(src_node);
}
if self[src_surface].is_empty() && !src_surface.is_main() {
self.remove_surface(src_surface);
}
}
/// Takes a tab out of its current surface and puts it in a new window.
/// Returns the surface index of the new window.
pub fn detach_tab(
&mut self,
(src_surface, src_node, src_tab): (SurfaceIndex, NodeIndex, TabIndex),
window_rect: Rect,
) -> SurfaceIndex {
// Remove the tab from the tree and it add to a new window.
let tab = self[src_surface][src_node].remove_tab(src_tab).unwrap();
let surface_index = self.add_window(vec![tab]);
// Set the window size and position to match `window_rect`.
let state = self.get_window_state_mut(surface_index).unwrap();
state.set_position(window_rect.min);
if src_surface.is_main() {
state.set_size(window_rect.size() * 0.8);
} else {
state.set_size(window_rect.size());
}
// Clean up any empty leaves and surfaces which may be left behind from the detachment.
if self[src_surface][src_node].is_leaf() && self[src_surface][src_node].tabs_count() == 0 {
self[src_surface].remove_leaf(src_node);
}
if self[src_surface].is_empty() && !src_surface.is_main() {
self.remove_surface(src_surface);
}
surface_index
}
/// Currently focused leaf.
#[inline]
pub fn focused_leaf(&self) -> Option<(SurfaceIndex, NodeIndex)> {
let surface = self.focused_surface?;
self[surface].focused_leaf().map(|leaf| (surface, leaf))
}
/// Remove a tab at the specified surface, node, and tab index.
/// This method will yield the removed tab, or `None` if it doesn't exist.
pub fn remove_tab(
&mut self,
(surface_index, node_index, tab_index): (SurfaceIndex, NodeIndex, TabIndex),
) -> Option<Tab> {
let removed_tab = self[surface_index].remove_tab((node_index, tab_index));
if !surface_index.is_main() && self[surface_index].is_empty() {
self.remove_surface(surface_index);
}
removed_tab
}
/// 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 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.
pub fn split(
&mut self,
(surface, parent): (SurfaceIndex, NodeIndex),
split: Split,
fraction: f32,
new: Node<Tab>,
) -> [NodeIndex; 2] {
let index = self[surface].split(parent, split, fraction, new);
self.focused_surface = Some(surface);
index
}
/// Adds a window with its own list of tabs.
///
/// Returns the [`SurfaceIndex`] of the new window, which will remain constant through the windows lifetime.
pub fn add_window(&mut self, tabs: Vec<Tab>) -> SurfaceIndex {
let surface = Surface::Window(Tree::new(tabs), WindowState::new());
let index = self.find_empty_surface_index();
if index.0 < self.surfaces.len() {
self.surfaces[index.0] = surface;
} else {
self.surfaces.push(surface);
}
index
}
/// Finds the first empty surface index which may be used.
///
/// **WARNING**: in cases where one isn't found, `SurfaceIndex(self.surfaces.len())` is used.
/// therefore it's not inherently safe to index the [`DockState`] with this index, as it may panic.
fn find_empty_surface_index(&self) -> SurfaceIndex {
// Find the first possible empty surface to insert our window into.
// Starts at 1 as 0 is always the main surface.
for i in 1..self.surfaces.len() {
if self.surfaces[i].is_empty() {
return SurfaceIndex(i);
}
}
SurfaceIndex(self.surfaces.len())
}
/// 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.
pub fn push_to_focused_leaf(&mut self, tab: Tab) {
if let Some(surface) = self.focused_surface {
self[surface].push_to_focused_leaf(tab)
} else {
self[SurfaceIndex::main()].push_to_focused_leaf(tab)
}
}
/// Push a tab to the first available `Leaf` or create a new leaf if an `Empty` node is encountered.
pub fn push_to_first_leaf(&mut self, tab: Tab) {
self[SurfaceIndex::main()].push_to_first_leaf(tab);
}
/// Returns the current number of surfaces.
pub fn surfaces_count(&self) -> usize {
self.surfaces.len()
}
/// Returns an [`Iterator`] over all surfaces.
pub fn iter_surfaces(&self) -> impl Iterator<Item = &Surface<Tab>> {
self.surfaces.iter()
}
/// Returns a mutable [`Iterator`] over all surfaces.
pub fn iter_surfaces_mut(&mut self) -> impl Iterator<Item = &mut Surface<Tab>> {
self.surfaces.iter_mut()
}
/// Returns an [`Iterator`] of **all** underlying nodes in the dock state,
/// and the indices of containing surfaces.
pub fn iter_all_nodes(&self) -> impl Iterator<Item = (SurfaceIndex, &Node<Tab>)> {
self.iter_surfaces()
.enumerate()
.flat_map(|(surface_index, surface)| {
surface
.iter_nodes()
.map(move |node| (SurfaceIndex(surface_index), node))
})
}
/// Returns a mutable [`Iterator`] of **all** underlying nodes in the dock state,
/// and the indices of containing surfaces.
pub fn iter_all_nodes_mut(&mut self) -> impl Iterator<Item = (SurfaceIndex, &mut Node<Tab>)> {
self.iter_surfaces_mut()
.enumerate()
.flat_map(|(surface_index, surface)| {
surface
.iter_nodes_mut()
.map(move |node| (SurfaceIndex(surface_index), node))
})
}
/// Returns an [`Iterator`] of **all** tabs in the dock state,
/// and the indices of containing surfaces and nodes.
pub fn iter_all_tabs(&self) -> impl Iterator<Item = ((SurfaceIndex, NodeIndex), &Tab)> {
self.iter_surfaces()
.enumerate()
.flat_map(|(surface_index, surface)| {
surface
.iter_all_tabs()
.map(move |(node_index, tab)| ((SurfaceIndex(surface_index), node_index), tab))
})
}
/// Returns a mutable [`Iterator`] of **all** tabs in the dock state,
/// and the indices of containing surfaces and nodes.
pub fn iter_all_tabs_mut(
&mut self,
) -> impl Iterator<Item = ((SurfaceIndex, NodeIndex), &mut Tab)> {
self.iter_surfaces_mut()
.enumerate()
.flat_map(|(surface_index, surface)| {
surface
.iter_all_tabs_mut()
.map(move |(node_index, tab)| ((SurfaceIndex(surface_index), node_index), tab))
})
}
/// Returns an [`Iterator`] of the underlying collection of nodes on the main surface.
#[deprecated = "Use `dock_state.main_surface().iter()` instead"]
pub fn iter_main_surface_nodes(&self) -> impl Iterator<Item = &Node<Tab>> {
self[SurfaceIndex::main()].iter()
}
/// Returns a mutable [`Iterator`] of the underlying collection of nodes on the main surface.
#[deprecated = "Use `dock_state.main_surface_mut().iter_mut()` instead"]
pub fn iter_main_surface_nodes_mut(&mut self) -> impl Iterator<Item = &mut Node<Tab>> {
self[SurfaceIndex::main()].iter_mut()
}
/// Returns an [`Iterator`] of **all** underlying nodes in the dock state and all subsequent trees.
#[deprecated = "Use `iter_all_nodes` instead"]
pub fn iter_nodes(&self) -> impl Iterator<Item = &Node<Tab>> {
self.surfaces
.iter()
.filter_map(|surface| surface.node_tree())
.flat_map(|nodes| nodes.iter())
}
/// Returns a new [`DockState`] while mapping and filtering the tab type.
/// Any remaining empty [`Node`]s and [`Surface`]s are removed.
///
/// ```
/// # use egui_dock::{DockState, Node};
/// let dock_state = DockState::new(vec![1, 2, 3]);
/// let mapped_dock_state = dock_state.filter_map_tabs(|tab| (tab % 2 == 1).then(|| tab.to_string()));
///
/// let tabs: Vec<_> = mapped_dock_state.iter_all_tabs().map(|(_, tab)| tab.to_owned()).collect();
/// assert_eq!(tabs, vec!["1".to_string(), "3".to_string()]);
/// ```
pub fn filter_map_tabs<F, NewTab>(&self, function: F) -> DockState<NewTab>
where
F: Clone + FnMut(&Tab) -> Option<NewTab>,
{
let DockState {
surfaces,
focused_surface,
translations,
} = self;
let surfaces = surfaces
.iter()
.filter_map(|surface| {
let surface = surface.filter_map_tabs(function.clone());
(!surface.is_empty()).then_some(surface)
})
.collect();
DockState {
surfaces,
focused_surface: *focused_surface,
translations: translations.clone(),
}
}
/// Returns a new [`DockState`] while mapping the tab type.
///
/// ```
/// # use egui_dock::{DockState, Node};
/// let dock_state = DockState::new(vec![1, 2, 3]);
/// let mapped_dock_state = dock_state.map_tabs(|tab| tab.to_string());
///
/// let tabs: Vec<_> = mapped_dock_state.iter_all_tabs().map(|(_, tab)| tab.to_owned()).collect();
/// assert_eq!(tabs, vec!["1".to_string(), "2".to_string(), "3".to_string()]);
/// ```
pub fn map_tabs<F, NewTab>(&self, mut function: F) -> DockState<NewTab>
where
F: Clone + FnMut(&Tab) -> NewTab,
{
self.filter_map_tabs(move |tab| Some(function(tab)))
}
/// Returns a new [`DockState`] while filtering the tab type.
/// Any remaining empty [`Node`]s and [`Surface`]s are removed.
///
/// ```
/// # use egui_dock::{DockState, Node};
/// let dock_state = DockState::new(["tab1", "tab2", "outlier"].map(str::to_string).to_vec());
/// let filtered_dock_state = dock_state.filter_tabs(|tab| tab.starts_with("tab"));
///
/// let tabs: Vec<_> = filtered_dock_state.iter_all_tabs().map(|(_, tab)| tab.to_owned()).collect();
/// assert_eq!(tabs, vec!["tab1".to_string(), "tab2".to_string()]);
/// ```
pub fn filter_tabs<F>(&self, mut predicate: F) -> DockState<Tab>
where
F: Clone + FnMut(&Tab) -> bool,
Tab: Clone,
{
self.filter_map_tabs(move |tab| predicate(tab).then(|| tab.clone()))
}
/// Removes all tabs for which `predicate` returns `false`.
/// Any remaining empty [`Node`]s and [`Surface`]s are also removed.
///
/// ```
/// # use egui_dock::{DockState, Node};
/// let mut dock_state = DockState::new(["tab1", "tab2", "outlier"].map(str::to_string).to_vec());
/// dock_state.retain_tabs(|tab| tab.starts_with("tab"));
///
/// let tabs: Vec<_> = dock_state.iter_all_tabs().map(|(_, tab)| tab.to_owned()).collect();
/// assert_eq!(tabs, vec!["tab1".to_string(), "tab2".to_string()]);
/// ```
pub fn retain_tabs<F>(&mut self, predicate: F)
where
F: Clone + FnMut(&mut Tab) -> bool,
{
self.surfaces.retain_mut(|surface| {
surface.retain_tabs(predicate.clone());
!surface.is_empty()
});
}
}
impl<Tab> DockState<Tab>
where
Tab: PartialEq,
{
/// Find the given tab.
///
/// Returns in which node and where in that node the tab is.
///
/// The returned [`NodeIndex`] will always point to a [`Node::Leaf`].
///
/// In case there are several hits, only the first is returned.
///
/// See also: [`find_main_surface_tab`](DockState::find_main_surface_tab)
pub fn find_tab(&self, needle_tab: &Tab) -> Option<(SurfaceIndex, NodeIndex, TabIndex)> {
for &surface_index in self.valid_surface_indices().iter() {
if !self.surfaces[surface_index.0].is_empty() {
if let Some((node_index, tab_index)) = self[surface_index].find_tab(needle_tab) {
return Some((surface_index, node_index, tab_index));
}
}
}
None
}
/// Find the given tab on the main surface.
///
/// Returns which node and where in that node the tab is.
///
/// The returned [`NodeIndex`] will always point to a [`Node::Leaf`].
///
/// In case there are several hits, only the first is returned.
pub fn find_main_surface_tab(&self, needle_tab: &Tab) -> Option<(NodeIndex, TabIndex)> {
self[SurfaceIndex::main()].find_tab(needle_tab)
}
}