From 25415458292a42a52910f1728334d212ce34906e Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 5 Sep 2022 18:26:18 +0200 Subject: [PATCH 1/2] Add opt-in serde feature to enable serialization of Tree Allows users to persist the `Tree` layout --- CHANGELOG.md | 4 ++++ Cargo.toml | 8 ++++++++ src/tree.rs | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f123d9..09111e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Added +- Added opt-in `serde` feature to enable serialization of `Tree`. + + ## 0.2.0 - 2022-09-04 ### Added diff --git a/Cargo.toml b/Cargo.toml index fb6de6f..c09a83c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/tree.rs b/src/tree.rs index b5508e7..a937558 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -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 for TabIndex { @@ -14,6 +15,7 @@ impl From for TabIndex { // ---------------------------------------------------------------------------- /// Represents an abstract node of a `Tree`. +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub enum Node { /// Empty node Empty, @@ -154,6 +156,7 @@ impl Node { /// 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 for NodeIndex { @@ -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 { tree: Vec>, focused_node: Option, From e9044607d84de013a562bc8c5ef376e7246c0205 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 5 Sep 2022 18:31:19 +0200 Subject: [PATCH 2/2] Fix: Tree::push_to_first_leaf no longer panics when used on empty Tree --- CHANGELOG.md | 3 +++ src/tree.rs | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09111e4..f63c514 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### 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 diff --git a/src/tree.rs b/src/tree.rs index a937558..73362b9 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -574,7 +574,9 @@ impl Tree { _ => {} } } - panic!(); + assert!(self.tree.is_empty()); + self.tree.push(Node::leaf_with(vec![tab])); + self.focused_node = Some(NodeIndex(0)); } /// Currently focused leaf.