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

🐍 Let serde be an optional dependency #13

Merged
merged 3 commits into from
Nov 2, 2023
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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ahash = { version = "0.8.1", default-features = false, features = [
egui = { version = "0.23", default-features = false }
itertools = "0.11"
log = { version = "0.4", features = ["std"] }
serde = { version = "1", features = ["derive"] }
serde = { version = "1", features = ["derive"], optional = true }


# For the example:
Expand All @@ -39,3 +39,7 @@ eframe = { version = "0.23", default-features = false, features = [
"persistence",
] }
env_logger = "0.10"

[features]
default = ["serde"]
serde = ["dep:serde", "egui/serde"]
19 changes: 11 additions & 8 deletions examples/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ fn main() -> Result<(), eframe::Error> {
eframe::run_native(
"egui_tiles example",
options,
Box::new(|cc| {
Box::new(|_cc| {
#[cfg_attr(not(feature = "serde"), allow(unused_mut))]
let mut app = MyApp::default();
if let Some(storage) = cc.storage {
#[cfg(feature = "serde")]
if let Some(storage) = _cc.storage {
if let Some(state) = eframe::get_value(storage, eframe::APP_KEY) {
app = state;
}
Expand All @@ -23,7 +25,7 @@ fn main() -> Result<(), eframe::Error> {
)
}

#[derive(serde::Deserialize, serde::Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Pane {
nr: usize,
}
Expand Down Expand Up @@ -156,14 +158,14 @@ impl egui_tiles::Behavior<Pane> for TreeBehavior {
}
}

#[derive(serde::Deserialize, serde::Serialize)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
struct MyApp {
tree: egui_tiles::Tree<Pane>,

#[serde(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
behavior: TreeBehavior,

#[serde(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
last_tree_debug: String,
}

Expand Down Expand Up @@ -247,8 +249,9 @@ impl eframe::App for MyApp {
});
}

fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, &self);
fn save(&mut self, _storage: &mut dyn eframe::Storage) {
#[cfg(feature = "serde")]
eframe::set_value(_storage, eframe::APP_KEY, &self);
}
}

Expand Down
22 changes: 6 additions & 16 deletions src/container/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@ use crate::{
};

/// How to lay out the children of a grid.
#[derive(
Clone,
Copy,
Debug,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Serialize,
serde::Deserialize,
)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum GridLayout {
/// Place children in a grid, with a dynamic number of columns and rows.
/// Resizing the window may change the number of columns and rows.
Expand All @@ -32,7 +21,8 @@ pub enum GridLayout {
}

/// A grid of tiles.
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Grid {
/// The order of the children, row-major.
///
Expand All @@ -50,11 +40,11 @@ pub struct Grid {
pub row_shares: Vec<f32>,

/// ui point x ranges for each column, recomputed during layout
#[serde(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
col_ranges: Vec<Rangef>,

/// ui point y ranges for each row, recomputed during layout
#[serde(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
row_ranges: Vec<Rangef>,
}

Expand Down
11 changes: 6 additions & 5 deletions src/container/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use crate::{
/// Used for [`Linear`] containers (horizontal and vertical).
///
/// Also contains the shares for currently invisible tiles.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Shares {
/// How large of a share each child has.
///
Expand Down Expand Up @@ -82,17 +83,17 @@ impl std::ops::IndexMut<TileId> for Shares {
// ----------------------------------------------------------------------------

/// The direction of a [`Linear`] container. Either horizontal or vertical.
#[derive(
Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum LinearDir {
#[default]
Horizontal,
Vertical,
}

/// Horizontal or vertical container.
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Linear {
pub children: Vec<TileId>,
pub dir: LinearDir,
Expand Down
6 changes: 4 additions & 2 deletions src/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub use tabs::Tabs;
/// The layout type of a [`Container`].
///
/// This is used to describe a [`Container`], and to change it to a different layout type.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ContainerKind {
/// Each child in an individual tab.
#[default]
Expand All @@ -40,7 +41,8 @@ impl ContainerKind {
// ----------------------------------------------------------------------------

/// A container of several [`super::Tile`]s.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Container {
Tabs(Tabs),
Linear(Linear),
Expand Down
3 changes: 2 additions & 1 deletion src/container/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
};

/// A container with tabs. Only one tab is open (active) at a time.
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Tabs {
/// The tabs, in order.
pub children: Vec<TileId>,
Expand Down
6 changes: 4 additions & 2 deletions src/tile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{Container, ContainerKind};

/// An identifier for a [`Tile`] in the tree, be it a [`Container`] or a pane.
#[derive(Clone, Copy, Hash, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct TileId(u64);

impl TileId {
Expand All @@ -24,7 +25,8 @@ impl std::fmt::Debug for TileId {
// ----------------------------------------------------------------------------

/// A tile in the tree. Either a pane (leaf) or a [`Container`] of more tiles.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Tile<Pane> {
/// A leaf. This is where the user puts their UI, using the [`crate::Behavior`] trait.
Pane(Pane),
Expand Down
5 changes: 3 additions & 2 deletions src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use super::{
///
/// let tree = Tree::new(root, tiles);
/// ```
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Tiles<Pane> {
next_tile_id: u64,

Expand All @@ -28,7 +29,7 @@ pub struct Tiles<Pane> {
invisible: ahash::HashSet<TileId>,

/// Filled in by the layout step at the start of each frame.
#[serde(default, skip)]
#[cfg_attr(feature = "serde", serde(default, skip))]
pub(super) rects: ahash::HashMap<TileId, Rect>,
}

Expand Down
3 changes: 2 additions & 1 deletion src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use super::{
///
/// let tree = Tree::new(root, tiles);
/// ```
#[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Tree<Pane> {
/// None = empty tree
pub root: Option<TileId>,
Expand Down
Loading