JFLib

ui/history.rs#

Source: lib/ui/history.rs

history -- undo / redo for an app's own state

  History::new(limit)                  empty; at most `limit` steps are kept (the oldest go)
  h.apply(&mut state, edit)            run an Edit and record it (the redo steps go)
  h.undo(&mut state) / h.redo(&mut state) -> bool
  h.can_undo() / can_redo()            undo_label() / redo_label() -> the step's label;
                                       undo_value() / redo_value() -> the step as data
  h.group_begin(label) / group_end()   the edits between them are one step (undo / redo
                                       while one is open close it first: its edits so far
                                       are the newest step, a later group_end does nothing)
  h.mark_saved() / dirty()             whether the state differs from the saved one
  h.mark_unsaved()                     the state counts as unsaved (a recovered copy)
  h.clear()
  Snapshot::new(label, before, after)  an Edit for small states: the whole value (Clone)
  ui.undo_keys() -> Option<bool>       Ctrl+Z (Some(true)) / Ctrl+Y or Ctrl+Shift+Z
                                       (Some(false)) when no text field has the keyboard;
                                       the key is consumed

An Edit changes the state forward (apply) and back (revert); merge lets a step absorb the
next one of the same kind (typing in one field, dragging a slider) so one undo takes it
all back.  Text fields keep their own undo; the edits here are the app's.
  • trait Edit<S> one undoable change of a state S

    • fn apply(&mut self, s: &mut S) do it (again)
    • fn revert(&mut self, s: &mut S) take it back
    • fn label(&self) -> &str shown in "Rückgängig: …"
    • fn merge(&mut self, _next: &mut dyn Edit<S>) -> bool absorb the following edit into this one (true: it is part of this step now)
    • fn value(&self) -> Option<&dyn core::any::Any> (merge) the edit as Any-like data: the merge partner's own value
  • struct Snapshot<T: Clone + 'static> an Edit of a whole small value (Clone): before and after

impl<T: Clone + 'static> Snapshot<T>

  • fn new(label: &str, before: T, after: T) -> Self

  • fn merging(mut self, key: u32) -> Self edits with the same key merge into one step

  • struct History<S> the undo and redo steps of a state S

impl<S: 'static> History<S>

  • fn new(limit: usize) -> Self
  • fn apply(&mut self, s: &mut S, mut edit: Box<dyn Edit<S>>) run the edit and record it as the newest step (redo steps are gone)
  • fn record(&mut self, mut edit: Box<dyn Edit<S>>) record an edit that was already applied
  • fn undo(&mut self, s: &mut S) -> bool take the newest step back -> false when there is none (an open group is closed first)
  • fn redo(&mut self, s: &mut S) -> bool do the last undone step again -> false when there is none (an open group is closed first: with edits in it there is nothing to redo any more)
  • fn can_undo(&self) -> bool
  • fn can_redo(&self) -> bool
  • fn undo_label(&self) -> Option<&str> the label of the step undo would take back (an open group with edits: the outermost group's)
  • fn redo_label(&self) -> Option<&str> the label of the step redo would do again
  • fn undo_value(&self) -> Option<&dyn core::any::Any> the step undo would take back as data (Edit::value; None for a group): the app can see what kind of edit it is before it runs
  • fn redo_value(&self) -> Option<&dyn core::any::Any> the step redo would do again as data (Edit::value)
  • fn group_begin(&mut self, label: &str) the following edits are one step (nested groups join the outer one)
  • fn group_end(&mut self) close the group of group_begin (an empty group records nothing)
  • fn mark_saved(&mut self) the state is saved now (dirty() false until the next change)
  • fn mark_unsaved(&mut self) the state counts as unsaved until the next mark_saved (a copy recovered after a crash)
  • fn dirty(&self) -> bool the state differs from the saved one
  • fn clear(&mut self) forget every step (the current state counts as saved when it was)
  • fn len(&self) -> usize the number of undo steps
  • fn is_empty(&self) -> bool

impl Ui

  • fn undo_keys(&mut self) -> Option<bool> undo_keys() -> Some(true) for Ctrl+Z, Some(false) for Ctrl+Y / Ctrl+Shift+Z, when no text field (they keep their own undo) or widget with HB_KEYS has the keyboard; the key is consumed

Source: docs/API.md in the JFLib repository.