| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <functional> |
| 17 | #include <string> |
| 18 | |
| 19 | #include "../TextEditor.h" |
| 20 | #include "../TextDiff.h" |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | class Editor { |
| 28 | public: |
| 29 | |
| 30 | Editor(); |
| 31 | |
| 32 | |
| 33 | void newFile(); |
| 34 | void openFile(); |
| 35 | void openFile(const std::string& path); |
| 36 | void saveFile(); |
| 37 | |
| 38 | |
| 39 | void tryToQuit(); |
| 40 | inline bool isDone() const { return done; } |
| 41 | |
| 42 | |
| 43 | void render(); |
| 44 | |
| 45 | private: |
| 46 | |
| 47 | void renderMenuBar(); |
| 48 | void renderStatusBar(); |
| 49 | |
| 50 | void showDiff(); |
| 51 | void showFileOpen(); |
| 52 | void showSaveFileAs(); |
| 53 | void showConfirmClose(std::function<void()> callback); |
| 54 | void showConfirmQuit(); |
| 55 | void showError(const std::string& message); |
| 56 | |
| 57 | void renderDiff(); |
| 58 | void renderFileOpen(); |
| 59 | void renderSaveAs(); |
| 60 | void renderConfirmClose(); |
| 61 | void renderConfirmQuit(); |
| 62 | void renderConfirmError(); |
| 63 | |
| 64 | void setAutocompleteMode(bool flag); |
| 65 | void buildAutocompleteTrie(); |
| 66 | |
| 67 | inline bool isDirty() const { return editor.GetUndoIndex() != version; } |
| 68 | inline bool isSavable() const { return isDirty() && filename != "untitled"; } |
| 69 | |
| 70 | |
| 71 | std::string originalText; |
| 72 | TextEditor editor; |
| 73 | TextDiff diff; |
| 74 | std::string filename; |
| 75 | size_t version; |
| 76 | bool done = false; |
| 77 | std::string errorMessage; |
| 78 | std::function<void()> onConfirmClose; |
| 79 | |
| 80 | float fontSize = 17.0f; |
| 81 | inline void increaseFontSIze() { fontSize = std::clamp(fontSize + 1.0f, 8.0f, 24.0f); } |
| 82 | inline void decreaseFontSIze() { fontSize = std::clamp(fontSize - 1.0f, 8.0f, 24.0f); } |
| 83 | |
| 84 | bool autocomplete = false; |
| 85 | TextEditor::Trie trie; |
| 86 | |
| 87 | |
| 88 | enum class State { |
| 89 | edit, |
| 90 | diff, |
| 91 | newFile, |
| 92 | openFile, |
| 93 | saveFileAs, |
| 94 | confirmClose, |
| 95 | confirmQuit, |
| 96 | confirmError |
| 97 | } state = State::edit; |
| 98 | }; |