TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_undo_history.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstddef>
5#include <functional>
6#include <iterator>
7#include <string>
8#include <string_view>
9#include <utility>
10#include <vector>
11
12namespace tachyon
13{
16
20 {
21 public:
22 struct Step
23 {
24 std::string description;
25 std::function<void()> undo;
26 std::function<void()> redo;
29 uint64_t mergeKey = 0;
34 std::function<void(bool undone)> drop;
35 };
36
39 void Push(Step step)
40 {
41 std::vector<Step> dropped = TakeUndone();
42 if (m_mergeOpen && step.mergeKey != 0 && !step.drop && !m_steps.empty()
43 && m_steps.back().mergeKey == step.mergeKey && !m_steps.back().drop)
44 {
45 m_steps.back().redo = std::move(step.redo);
46 }
47 else
48 {
49 m_steps.push_back(std::move(step));
50 m_current = m_steps.size();
51 m_mergeOpen = true;
52 }
53 Drop(dropped, 0);
54 }
55
57 void EndMerge() { m_mergeOpen = false; }
58
59 bool CanUndo() const { return m_current > 0; }
60 bool CanRedo() const { return m_current < m_steps.size(); }
61
64 void Undo()
65 {
66 if (!CanUndo()) return;
67 m_mergeOpen = false;
68 if (const std::function<void()> undo = m_steps[--m_current].undo) undo();
69 }
70
72 void Redo()
73 {
74 if (!CanRedo()) return;
75 m_mergeOpen = false;
76 if (const std::function<void()> redo = m_steps[m_current++].redo) redo();
77 }
78
79 size_t UndoCount() const { return m_current; }
80 size_t RedoCount() const { return m_steps.size() - m_current; }
81 std::string_view UndoDescription() const { return CanUndo() ? std::string_view(m_steps[m_current - 1].description) : std::string_view{}; }
82 std::string_view RedoDescription() const { return CanRedo() ? std::string_view(m_steps[m_current].description) : std::string_view{}; }
83
86 void Clear()
87 {
88 std::vector<Step> dropped = std::exchange(m_steps, {});
89 const size_t done = std::exchange(m_current, 0);
90 m_mergeOpen = false;
91 Drop(dropped, done);
92 }
93
94 private:
95 std::vector<Step> TakeUndone()
96 {
97 std::vector<Step> undone;
98 if (m_current < m_steps.size())
99 {
100 undone.assign(std::make_move_iterator(m_steps.begin() + ptrdiff_t(m_current)), std::make_move_iterator(m_steps.end()));
101 m_steps.resize(m_current);
102 }
103 return undone;
104 }
105
108 static void Drop(std::vector<Step>& steps, size_t done)
109 {
110 for (size_t i = 0; i < steps.size(); ++i)
111 if (steps[i].drop) steps[i].drop(i >= done);
112 }
113
114 std::vector<Step> m_steps;
115 size_t m_current = 0;
116 bool m_mergeOpen = false;
117 };
118
119}
The app's undo history: steps that each undo and redo one change.
Definition gui_undo_history.h:20
bool CanUndo() const
Definition gui_undo_history.h:59
void EndMerge()
The next push starts a step of its own (e.g. when a field loses the focus).
Definition gui_undo_history.h:57
bool CanRedo() const
Definition gui_undo_history.h:60
std::string_view RedoDescription() const
Definition gui_undo_history.h:82
size_t UndoCount() const
Definition gui_undo_history.h:79
void Undo()
The step's callback runs from a copy, after the history has moved: it may push, clear or undo / redo ...
Definition gui_undo_history.h:64
size_t RedoCount() const
Definition gui_undo_history.h:80
void Push(Step step)
Adds a step after the current one, dropping any undone steps (or merges it into the last step,...
Definition gui_undo_history.h:39
void Clear()
Drops every step (see Step::drop).
Definition gui_undo_history.h:86
void Redo()
As Undo, the other way.
Definition gui_undo_history.h:72
std::string_view UndoDescription() const
Definition gui_undo_history.h:81
Definition gui_logging.h:24
Definition gui_undo_history.h:23
std::function< void()> redo
Definition gui_undo_history.h:26
std::function< void(bool undone)> drop
Runs once as the step leaves the history for good: a push replaces it while it's undone,...
Definition gui_undo_history.h:34
std::function< void()> undo
Definition gui_undo_history.h:25
std::string description
"Edit text", "Move node"
Definition gui_undo_history.h:24
uint64_t mergeKey
Steps with the same non-zero key merge while merging is open (e.g.
Definition gui_undo_history.h:29