| 1 | // Aseprite |
| 2 | // Copyright (C) 2019-2020 Igara Studio S.A. |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifndef APP_CLOSED_DOCS_H_INCLUDED |
| 8 | #define APP_CLOSED_DOCS_H_INCLUDED |
| 9 | #pragma once |
| 10 | |
| 11 | #include "base/time.h" |
| 12 | |
| 13 | #include <atomic> |
| 14 | #include <condition_variable> |
| 15 | #include <mutex> |
| 16 | #include <thread> |
| 17 | #include <vector> |
| 18 | |
| 19 | namespace app { |
| 20 | |
| 21 | class Doc; |
| 22 | class Preferences; |
| 23 | |
| 24 | // Handle the list of closed docs: |
| 25 | // * When a document is closed, we keep it for some time so the user |
| 26 | // can undo the close command without losing the undo history. |
| 27 | // * For the first closed document, a thread is launched to wait |
| 28 | // until we can definitely delete the doc after X minutes (like a |
| 29 | // garbage collector). |
| 30 | // * If the document was not restore, we delete it from memory, if |
| 31 | // the document was restore, we remove it from the m_docs. |
| 32 | class ClosedDocs { |
| 33 | public: |
| 34 | ClosedDocs(const Preferences& pref); |
| 35 | ~ClosedDocs(); |
| 36 | |
| 37 | bool hasClosedDocs(); |
| 38 | void addClosedDoc(Doc* doc); |
| 39 | Doc* reopenLastClosedDoc(); |
| 40 | |
| 41 | // Called at the very end to get all closed docs, remove them from |
| 42 | // the list of closed docs, and stop the thread. |
| 43 | std::vector<Doc*> getAndRemoveAllClosedDocs(); |
| 44 | |
| 45 | private: |
| 46 | void backgroundThread(); |
| 47 | |
| 48 | struct ClosedDoc { |
| 49 | Doc* doc; |
| 50 | base::tick_t timestamp; |
| 51 | }; |
| 52 | |
| 53 | std::atomic<bool> m_done; |
| 54 | base::tick_t m_dataRecoveryPeriodMSecs; |
| 55 | base::tick_t m_keepClosedDocAliveForMSecs; |
| 56 | std::vector<ClosedDoc> m_docs; |
| 57 | std::mutex m_mutex; |
| 58 | std::condition_variable m_cv; |
| 59 | std::thread m_thread; |
| 60 | }; |
| 61 | |
| 62 | } // namespace app |
| 63 | |
| 64 | #endif |
| 65 | |