controller/UndoManager.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QList> | ||
| 4 | #include <QObject> | ||
| 5 | #include <QString> | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | |||
| 9 | // A single undoable action. Asynchronous entries are retained until their | ||
| 10 | // completion callback confirms success; failure remains retryable. | ||
| 11 | struct UndoEntry { | ||
| 12 | QString description; | ||
| 13 | std::function<void()> undo; | ||
| 14 | qint64 timestamp; // QDateTime::currentMSecsSinceEpoch() | ||
| 15 | quint64 id = 0; | ||
| 16 | qint64 retainedBytes = 0; | ||
| 17 | std::function<void(std::function<void(bool)>)> asynchronousUndo; | ||
| 18 | }; | ||
| 19 | |||
| 20 | // UndoManager maintains a bounded LIFO stack of undoable actions. | ||
| 21 | // Used by MailController to record flag changes, label edits, and moves. | ||
| 22 | // MainWindow wires Ctrl+Z / u shortcuts to undo(). | ||
| 23 | class UndoManager : public QObject { | ||
| 24 | Q_OBJECT | ||
| 25 | |||
| 26 | public: | ||
| 27 | explicit UndoManager(QObject *parent = nullptr); | ||
| 28 | |||
| 29 | // Push a new undo entry onto the stack. | ||
| 30 | // If the stack exceeds kMaxUndoDepth, the oldest entry is discarded. | ||
| 31 | void push(const QString &description, std::function<void()> undoAction); | ||
| 32 | using Completion = std::function<void(bool)>; | ||
| 33 | bool pushAsync(const QString &description, | ||
| 34 | std::function<void(Completion)> undoAction, | ||
| 35 | qint64 retainedBytes = 0); | ||
| 36 | |||
| 37 | // Start the most recent undo. Async entries pop only after confirmed success. | ||
| 38 | // Returns true if execution started, false for an empty/busy stack. | ||
| 39 | bool undo(); | ||
| 40 | |||
| 41 | bool canUndo() const; | ||
| 42 | 676 | bool busy() const { return m_activeId != 0; } | |
| 43 | QString lastDescription() const; | ||
| 44 | int stackSize() const; | ||
| 45 | |||
| 46 | // Clear the entire undo stack (e.g. on disconnect or account switch). | ||
| 47 | void clear(); | ||
| 48 | |||
| 49 | static constexpr int kMaxUndoDepth = 50; | ||
| 50 | static constexpr qint64 kMaxAsyncRetainedBytes = 2 * 1024 * 1024; | ||
| 51 | |||
| 52 | signals: | ||
| 53 | void undoAvailable(bool available); | ||
| 54 | void undoPerformed(const QString &description); | ||
| 55 | void undoFailed(const QString &description); | ||
| 56 | |||
| 57 | private: | ||
| 58 | QList<UndoEntry> m_stack; | ||
| 59 | bool append(UndoEntry entry); | ||
| 60 | void finishAsync(quint64 id, quint64 attempt, bool succeeded); | ||
| 61 | quint64 m_nextId = 0; | ||
| 62 | quint64 m_activeId = 0; | ||
| 63 | quint64 m_nextAttempt = 0; | ||
| 64 | quint64 m_activeAttempt = 0; | ||
| 65 | }; | ||
| 66 |