TachyonUI 0.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 <functional>
5#include <string>
6#include <string_view>
7#include <utility>
8#include <vector>
9
10namespace tachyon
11{
14
18 {
19 public:
20 struct Step
21 {
22 std::string description;
23 std::function<void()> undo;
24 std::function<void()> redo;
27 uint64_t mergeKey = 0;
28 };
29
32 void Push(Step step)
33 {
34 m_steps.resize(m_current);
35 if (m_mergeOpen && step.mergeKey != 0 && !m_steps.empty() && m_steps.back().mergeKey == step.mergeKey)
36 {
37 m_steps.back().redo = std::move(step.redo);
38 return;
39 }
40 m_steps.push_back(std::move(step));
41 m_current = m_steps.size();
42 m_mergeOpen = true;
43 }
44
46 void EndMerge() { m_mergeOpen = false; }
47
48 bool CanUndo() const { return m_current > 0; }
49 bool CanRedo() const { return m_current < m_steps.size(); }
50
51 void Undo()
52 {
53 if (!CanUndo()) return;
54 m_mergeOpen = false;
55 if (const auto& undo = m_steps[--m_current].undo) undo();
56 }
57
58 void Redo()
59 {
60 if (!CanRedo()) return;
61 m_mergeOpen = false;
62 if (const auto& redo = m_steps[m_current++].redo) redo();
63 }
64
65 size_t UndoCount() const { return m_current; }
66 size_t RedoCount() const { return m_steps.size() - m_current; }
67 std::string_view UndoDescription() const { return CanUndo() ? std::string_view(m_steps[m_current - 1].description) : std::string_view{}; }
68 std::string_view RedoDescription() const { return CanRedo() ? std::string_view(m_steps[m_current].description) : std::string_view{}; }
69
70 void Clear()
71 {
72 m_steps.clear();
73 m_current = 0;
74 m_mergeOpen = false;
75 }
76
77 private:
78 std::vector<Step> m_steps;
79 size_t m_current = 0;
80 bool m_mergeOpen = false;
81 };
82
83}
The app's undo history: steps that each undo and redo one change.
Definition gui_undo_history.h:18
bool CanUndo() const
Definition gui_undo_history.h:48
void EndMerge()
The next push starts a step of its own (e.g. when a field loses the focus).
Definition gui_undo_history.h:46
bool CanRedo() const
Definition gui_undo_history.h:49
std::string_view RedoDescription() const
Definition gui_undo_history.h:68
size_t UndoCount() const
Definition gui_undo_history.h:65
void Undo()
Definition gui_undo_history.h:51
size_t RedoCount() const
Definition gui_undo_history.h:66
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:32
void Clear()
Definition gui_undo_history.h:70
void Redo()
Definition gui_undo_history.h:58
std::string_view UndoDescription() const
Definition gui_undo_history.h:67
Definition gui_logging.h:24
Definition gui_undo_history.h:21
std::function< void()> redo
Definition gui_undo_history.h:24
std::function< void()> undo
Definition gui_undo_history.h:23
std::string description
"Edit text", "Move node"
Definition gui_undo_history.h:22
uint64_t mergeKey
Steps with the same non-zero key merge while merging is open (e.g.
Definition gui_undo_history.h:27