Skip to content

Commit

Permalink
Add opt-in serde feature to enable serialization of Tree (#30)
Browse files Browse the repository at this point in the history
* Add opt-in serde feature to enable serialization of Tree

Allows users to persist the `Tree` layout

* Fix: Tree::push_to_first_leaf no longer panics when used on empty Tree
  • Loading branch information
emilk authored Sep 5, 2022
1 parent 4965371 commit 24d8731
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

### Added
- Added opt-in `serde` feature to enable serialization of `Tree`.

### Fixed
* `Tree::push_to_first_leaf` no longer panics when used on an empty `Tree`


## 0.2.0 - 2022-09-04

### Added
Expand Down
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ readme = "README.md"
repository = "https://github.com/Adanos020/egui_dock"
categories = ["gui", "game-development"]

[features]
default = []

# Enable serialization of `Tree`.
serde = ["dep:serde", "egui/serde"]


[dependencies]
egui = "0.19"
serde = { version = "1", optional = true, features = ["derive"] }

[dev-dependencies]
eframe = "0.19"
8 changes: 7 additions & 1 deletion src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use egui::*;

/// Identifies a tab within a [`Node`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct TabIndex(pub usize);

impl From<usize> for TabIndex {
Expand All @@ -14,6 +15,7 @@ impl From<usize> for TabIndex {
// ----------------------------------------------------------------------------

/// Represents an abstract node of a `Tree`.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Node<Tab> {
/// Empty node
Empty,
Expand Down Expand Up @@ -154,6 +156,7 @@ impl<Tab> Node<Tab> {

/// Wrapper around indices to the collection of nodes inside a `Tree`.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct NodeIndex(pub usize);

impl From<usize> for NodeIndex {
Expand Down Expand Up @@ -239,6 +242,7 @@ pub enum Split {
// ----------------------------------------------------------------------------

/// Binary tree representing the relationships between `Node`s.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Tree<Tab> {
tree: Vec<Node<Tab>>,
focused_node: Option<NodeIndex>,
Expand Down Expand Up @@ -570,7 +574,9 @@ impl<Tab> Tree<Tab> {
_ => {}
}
}
panic!();
assert!(self.tree.is_empty());
self.tree.push(Node::leaf_with(vec![tab]));
self.focused_node = Some(NodeIndex(0));
}

/// Currently focused leaf.
Expand Down

0 comments on commit 24d8731

Please sign in to comment.