-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUndoableTextView.h
More file actions
56 lines (43 loc) · 1.32 KB
/
UndoableTextView.h
File metadata and controls
56 lines (43 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright 2025, Johan Wagenheim <johan@dospuntos.no>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef UNDOABLE_TEXT_VIEW_H
#define UNDOABLE_TEXT_VIEW_H
#include <MessageRunner.h>
#include <TextView.h>
#include <stack>
struct TextSnapshot {
BString text;
int32 selectionStart;
int32 selectionEnd;
};
class UndoableTextView : public BTextView {
public:
UndoableTextView(const char* name);
~UndoableTextView();
void MessageReceived(BMessage* msg) override;
void InsertText(const char* text, int32 length, int32 offset,
const text_run_array* runs) override;
void DeleteText(int32 start, int32 finish) override;
void KeyDown(const char* bytes, int32 numBytes) override;
void MouseDown(BPoint point) override;
void AllAttached() override;
void SetColorsFromTheme();
void SetTextWithUndo(const BString& newText);
void Undo();
void Redo();
bool CanUndo() const { return !fUndoStack.empty(); }
bool CanRedo() const { return !fRedoStack.empty(); }
private:
void PushUndoSnapshot();
void StartCoalesceTimer();
void StopCoalesceTimer();
static const bigtime_t kCoalesceDelay = 1500000; // 1.5 seconds
static const size_t kMaxUndoSteps = 100;
std::stack<TextSnapshot> fUndoStack;
std::stack<TextSnapshot> fRedoStack;
bool fCoalescing;
bool fRecording;
};
#endif // UNDOABLE_TEXT_VIEW_H