undoStudio
The IDE of the undoRT ecosystem
Loading...
Searching...
No Matches
undoAppOpenDocuments.hpp
Go to the documentation of this file.
1
32#ifndef UNDOSTUDIO_UNDOAPP_OPENDOCUMENTS_HPP
33#define UNDOSTUDIO_UNDOAPP_OPENDOCUMENTS_HPP
34
35#include <cstddef>
36#include <string>
37#include <vector>
38
39namespace undoApp {
40
48enum class DocKind
49{
50 None,
51 ST,
52 JSON,
53 Text,
54 Cpp
55};
56
61{
62 std::string path;
64 bool pinned = false;
65 bool dirty = false;
66};
67
77{
78 bool alreadyOpen = false;
79 bool droppedPreview = false;
80 std::string droppedPath;
81};
82
87{
88public:
94 OpenOutcome open(const std::string& path, DocKind kind)
95 {
96 OpenOutcome outcome;
97 if (path.empty()) {
98 return outcome;
99 }
100 for (OpenDocument& doc : m_documents) {
101 if (doc.path == path) {
102 m_active = path;
103 outcome.alreadyOpen = true;
104 return outcome;
105 }
106 }
107
108 // The outgoing preview. There is only ever one, which is what makes browsing
109 // a tree cheap: it is a slot that the next file takes rather than a tab per
110 // file passed over.
111 //
112 // A dirty preview cannot be reached here. setDirty() pins a document the
113 // moment it gains changes, so anything still in the slot has none to lose.
114 for (size_t i = 0; i < m_documents.size(); ++i) {
115 if (m_documents[i].pinned) {
116 continue;
117 }
118 outcome.droppedPath = m_documents[i].path;
119 m_documents.erase(m_documents.begin() + static_cast<long>(i));
120 outcome.droppedPreview = true;
121 break;
122 }
123
124 OpenDocument doc;
125 doc.path = path;
126 doc.kind = kind;
127 m_documents.push_back(doc);
128 m_active = path;
129 return outcome;
130 }
131
134 bool pin(const std::string& path)
135 {
136 for (OpenDocument& doc : m_documents) {
137 if (doc.path == path) {
138 doc.pinned = true;
139 return true;
140 }
141 }
142 return false;
143 }
144
149 void setDirty(const std::string& path, bool dirty)
150 {
151 for (OpenDocument& doc : m_documents) {
152 if (doc.path != path) {
153 continue;
154 }
155 doc.dirty = dirty;
156 if (dirty) {
157 doc.pinned = true;
158 }
159 return;
160 }
161 }
162
164 void setClean(const std::string& path) { setDirty(path, false); }
165
169 {
170 bool closed = false;
171 std::string nextActive;
172 bool activeWasClosed = false;
173 };
174
175 CloseOutcome close(const std::string& path)
176 {
177 CloseOutcome outcome;
178 for (size_t i = 0; i < m_documents.size(); ++i) {
179 if (m_documents[i].path != path) {
180 continue;
181 }
182 outcome.activeWasClosed = (m_active == path);
183 outcome.closed = true;
184 m_documents.erase(m_documents.begin() + static_cast<long>(i));
185 if (outcome.activeWasClosed) {
186 // The tab to the left, or the first one left, so closing does not jump
187 // to whatever happens to be at the end of the list.
188 m_active = m_documents.empty() ? std::string()
189 : m_documents[(i > 0) ? i - 1 : 0].path;
190 outcome.nextActive = m_active;
191 }
192 return outcome;
193 }
194 return outcome;
195 }
196
208 bool rename(const std::string& oldPath, const std::string& newPath)
209 {
210 if (newPath.empty() || oldPath == newPath || isOpen(newPath)) {
211 return false;
212 }
213 for (OpenDocument& doc : m_documents) {
214 if (doc.path != oldPath) {
215 continue;
216 }
217 doc.path = newPath;
218 if (m_active == oldPath) {
219 m_active = newPath;
220 }
221 return true;
222 }
223 return false;
224 }
225
227 bool activate(const std::string& path)
228 {
229 for (const OpenDocument& doc : m_documents) {
230 if (doc.path == path) {
231 m_active = path;
232 return true;
233 }
234 }
235 return false;
236 }
237
241 {
242 size_t closed = 0;
243 size_t dirty = 0;
244 };
245
247 {
248 DirtySummary summary;
249 for (const OpenDocument& doc : m_documents) {
250 if (doc.dirty) {
251 ++summary.dirty;
252 }
253 }
254 summary.closed = m_documents.size();
255 m_documents.clear();
256 m_active.clear();
257 return summary;
258 }
259
260 const std::vector<OpenDocument>& documents() const { return m_documents; }
261 const std::string& active() const { return m_active; }
262 size_t size() const { return m_documents.size(); }
263
265 bool isOpen(const std::string& path) const
266 {
267 for (const OpenDocument& doc : m_documents) {
268 if (doc.path == path) {
269 return true;
270 }
271 }
272 return false;
273 }
274
276 size_t pinnedCount() const
277 {
278 size_t count = 0;
279 for (const OpenDocument& doc : m_documents) {
280 if (doc.pinned) {
281 ++count;
282 }
283 }
284 return count;
285 }
286
297 const std::string* cycle(int direction)
298 {
299 if (m_documents.empty()) {
300 return nullptr;
301 }
302 int index = -1;
303 for (size_t i = 0; i < m_documents.size(); ++i) {
304 if (m_documents[i].path == m_active) {
305 index = static_cast<int>(i);
306 break;
307 }
308 }
309 const int count = static_cast<int>(m_documents.size());
310 // With nothing active, "next" starts at the beginning and "previous" at the
311 // end, so either key goes somewhere rather than jumping to the first twice.
312 int next = (index < 0) ? (direction >= 0 ? 0 : count - 1) : (index + direction + count) % count;
313 m_active = m_documents[static_cast<size_t>(next)].path;
314 return &m_active;
315 }
316
317private:
318 std::vector<OpenDocument> m_documents;
319 std::string m_active;
320};
321
322} // namespace undoApp
323
324#endif // UNDOSTUDIO_UNDOAPP_OPENDOCUMENTS_HPP
The set of open files, at most one of them a preview.
Definition undoAppOpenDocuments.hpp:87
DirtySummary closeAll()
Definition undoAppOpenDocuments.hpp:246
const std::string * cycle(int direction)
Move to the next or previous document, wrapping round.
Definition undoAppOpenDocuments.hpp:297
void setClean(const std::string &path)
Record that a document's changes have been written.
Definition undoAppOpenDocuments.hpp:164
CloseOutcome close(const std::string &path)
Definition undoAppOpenDocuments.hpp:175
std::vector< OpenDocument > m_documents
Definition undoAppOpenDocuments.hpp:318
void setDirty(const std::string &path, bool dirty)
Record whether a document has changes not written to disk.
Definition undoAppOpenDocuments.hpp:149
OpenOutcome open(const std::string &path, DocKind kind)
Open a file, taking the preview slot.
Definition undoAppOpenDocuments.hpp:94
bool rename(const std::string &oldPath, const std::string &newPath)
Follow a file that was renamed or moved on disk.
Definition undoAppOpenDocuments.hpp:208
const std::string & active() const
Definition undoAppOpenDocuments.hpp:261
size_t size() const
Definition undoAppOpenDocuments.hpp:262
bool activate(const std::string &path)
Bring a document to the front without reopening it.
Definition undoAppOpenDocuments.hpp:227
bool isOpen(const std::string &path) const
Whether a document is open.
Definition undoAppOpenDocuments.hpp:265
size_t pinnedCount() const
How many documents are pinned.
Definition undoAppOpenDocuments.hpp:276
bool pin(const std::string &path)
Make a document permanent.
Definition undoAppOpenDocuments.hpp:134
std::string m_active
Definition undoAppOpenDocuments.hpp:319
const std::vector< OpenDocument > & documents() const
Definition undoAppOpenDocuments.hpp:260
Definition undoAppCpp.hpp:26
DocKind
Which backend a document belongs to.
Definition undoAppOpenDocuments.hpp:49
One file that is open.
Definition undoAppOpenDocuments.hpp:61
DocKind kind
Definition undoAppOpenDocuments.hpp:63
bool dirty
Has changes not written to disk.
Definition undoAppOpenDocuments.hpp:65
std::string path
Absolute path.
Definition undoAppOpenDocuments.hpp:62
bool pinned
Permanent: a preview is not replaced.
Definition undoAppOpenDocuments.hpp:64
Close a document.
Definition undoAppOpenDocuments.hpp:169
std::string nextActive
Definition undoAppOpenDocuments.hpp:171
bool closed
Definition undoAppOpenDocuments.hpp:170
bool activeWasClosed
Definition undoAppOpenDocuments.hpp:172
Close every document that has changes.
Definition undoAppOpenDocuments.hpp:241
size_t dirty
Definition undoAppOpenDocuments.hpp:243
size_t closed
Definition undoAppOpenDocuments.hpp:242
What opening a file did, so the caller knows what to put back.
Definition undoAppOpenDocuments.hpp:77
bool alreadyOpen
The file was already open and was focused.
Definition undoAppOpenDocuments.hpp:78
bool droppedPreview
The preview slot's previous occupant went away.
Definition undoAppOpenDocuments.hpp:79
std::string droppedPath
The file that went away, empty if none did.
Definition undoAppOpenDocuments.hpp:80