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

Add opt-in serde feature to enable serialization of Tree #30

Merged
merged 2 commits into from
Sep 5, 2022
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
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