1.9 KB、98行 生 Blame 履歴
1// TextEditor - A syntax highlighting text editor for ImGui
2// Copyright (c) 2024-2026 Johan A. Goossens. All rights reserved.
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7
8#pragma once
9
10
11//
12// Include files
13//
14
15#include <algorithm>
16#include <functional>
17#include <string>
18
19#include "../TextEditor.h"
20#include "../TextDiff.h"
21
22
23//
24// Editor
25//
26
27class Editor {
28public:
29 // constructor
30 Editor();
31
32 // file related functions
33 void newFile();
34 void openFile();
35 void openFile(const std::string& path);
36 void saveFile();
37
38 // manage program exit
39 void tryToQuit();
40 inline bool isDone() const { return done; }
41
42 // render the editor
43 void render();
44
45 private:
46 // private functions
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 // properties
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 // editor state
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};