From de465d6e98c6bac06af837393b5175f27720e7b2 Mon Sep 17 00:00:00 2001 From: valadaptive Date: Sat, 13 Apr 2024 07:51:45 -0400 Subject: [PATCH 1/2] Add Undoer::new() and Undoer::with_settings() Undoer::new() is needed in order to construct an Undoer whose State does not implement Default. Undoer::with_settings() is a new method that allows the caller to specify the settings, which could previously not be done using a public API. --- crates/egui/src/util/undoer.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/egui/src/util/undoer.rs b/crates/egui/src/util/undoer.rs index de6d2716171..784a82a204a 100644 --- a/crates/egui/src/util/undoer.rs +++ b/crates/egui/src/util/undoer.rs @@ -89,6 +89,23 @@ impl Undoer where State: Clone + PartialEq, { + pub fn new() -> Self { + Self { + settings: Settings::default(), + undos: VecDeque::new(), + redos: Vec::new(), + flux: None, + } + } + + /// Create a new [`Undoer`] with the given [`Settings`]. + pub fn with_settings(settings: Settings) -> Self { + Self { + settings, + ..Self::new() + } + } + /// Do we have an undo point different from the given state? pub fn has_undo(&self, current_state: &State) -> bool { match self.undos.len() { From e6403687bc968a3252aa71fb4668eda5a23ceb77 Mon Sep 17 00:00:00 2001 From: valadaptive Date: Sun, 21 Apr 2024 05:39:40 -0400 Subject: [PATCH 2/2] Implement Default for Undoer instead --- crates/egui/src/util/undoer.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/crates/egui/src/util/undoer.rs b/crates/egui/src/util/undoer.rs index 784a82a204a..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,20 +104,11 @@ impl Undoer where State: Clone + PartialEq, { - pub fn new() -> Self { - Self { - settings: Settings::default(), - undos: VecDeque::new(), - redos: Vec::new(), - flux: None, - } - } - /// Create a new [`Undoer`] with the given [`Settings`]. pub fn with_settings(settings: Settings) -> Self { Self { settings, - ..Self::new() + ..Default::default() } }