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 a way to specify Undoer settings and construct Undoers more easily #4357

Merged
merged 2 commits into from
Apr 21, 2024
Merged
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
25 changes: 24 additions & 1 deletion crates/egui/src/util/undoer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<State> {
settings: Settings,
Expand Down Expand Up @@ -77,6 +77,21 @@ impl<State> std::fmt::Debug for Undoer<State> {
}
}

impl<State> Default for Undoer<State>
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<State> {
Expand All @@ -89,6 +104,14 @@ impl<State> Undoer<State>
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() {
Expand Down
Loading