From d68c8d70aa1dd5c44bd1a83a9f6aa8247e7c5c3b Mon Sep 17 00:00:00 2001 From: valadaptive <79560998+valadaptive@users.noreply.github.com> Date: Sun, 21 Apr 2024 06:23:59 -0400 Subject: [PATCH] Add a way to specify Undoer settings and construct Undoers more easily (#4357) * Closes https://github.com/emilk/egui/issues/4356 - Add `Undoer::new()` - This is necessary to construct an `Undoer` whose `State` parameter doesn't implement `Default`. - Add `Undoer::with_settings(...)` - This is necessary to actually pass settings into the `Undoer`. Without this, API consumers could construct their own `Settings` but not actually do anything with it. I've refrained from adding any kind of builder API for `Settings` because there are only three options and I don't want to duplicate or move all the documentation onto the builder methods. --- crates/egui/src/util/undoer.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/util/undoer.rs b/crates/egui/src/util/undoer.rs index de6d2716171..d5f004f86ae 100644 --- a/crates/egui/src/util/undoer.rs +++ b/crates/egui/src/util/undoer.rs @@ -47,7 +47,7 @@ impl Default for Settings { /// /// Rule 1) will make sure an undo point is not created until you _stop_ dragging that slider. /// Rule 2) will make sure that you will get some undo points even if you are constantly changing the state. -#[derive(Clone, Default)] +#[derive(Clone)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Undoer { settings: Settings, @@ -77,6 +77,21 @@ impl std::fmt::Debug for Undoer { } } +impl Default for Undoer +where + State: Clone + PartialEq, +{ + #[inline] + fn default() -> Self { + Self { + settings: Settings::default(), + undos: VecDeque::new(), + redos: Vec::new(), + flux: None, + } + } +} + /// Represents how the current state is changing #[derive(Clone)] struct Flux { @@ -89,6 +104,14 @@ impl Undoer where State: Clone + PartialEq, { + /// Create a new [`Undoer`] with the given [`Settings`]. + pub fn with_settings(settings: Settings) -> Self { + Self { + settings, + ..Default::default() + } + } + /// Do we have an undo point different from the given state? pub fn has_undo(&self, current_state: &State) -> bool { match self.undos.len() {