-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlabel.cpp
More file actions
206 lines (165 loc) · 4.93 KB
/
Copy pathlabel.cpp
File metadata and controls
206 lines (165 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "label.h"
#include "text_renderer.h"
#include "application.h"
#include "helper_types.h"
Label::Label(Widget* parent) : Widget(parent) {
id = icu::UnicodeString::fromUTF8("Label");
fulltext = icu::UnicodeString::fromUTF8("");
drawlines = {};
}
void Label::setFullText(icu::UnicodeString text, std::vector<MarkdownSpan> spans) {
std::lock_guard<std::mutex> lock(positioning);
fulltext = text;
old_width = -1;
handlingColor = (spans.size() != 0);
colorSpans = spans;
}
icu::UnicodeString Label::getFullText() {
return fulltext;
}
bool Label::on_mouse_button_event(int button, int action, int mods) {
if (action != GLFW_PRESS || button != GLFW_MOUSE_BUTTON_LEFT) {
return Widget::on_mouse_button_event(button, action, mods);
}
if (t_x < App::mouseX && App::mouseX < t_x+t_w && t_y < App::mouseY && App::mouseY < t_y+t_h) {
if (fulltext.length() != 0) {
std::string text;
fulltext.toUTF8String(text);
SetClipboardText(text);
App::displayToast(icu::UnicodeString::fromUTF8("Coppied to clipboard."));
}else{
App::displayToast(icu::UnicodeString::fromUTF8("Nothing to copy."));
}
return true;
}
return Widget::on_mouse_button_event(button, action, mods);
}
void Label::render() {
if (rect) {
App::DrawRect(t_x, t_y, t_w, t_h, background_color);
}
if (border) {
App::DrawBorder(t_x, t_y, t_w, t_h, App::theme.border);
}
int ypos = t_y+App::text_padding;
for (int i = 0; i < drawlines.size(); i++) {
if (handlingColor) {
TextRenderer::draw_text(t_x+App::text_padding, ypos, drawlines[i], drawColors[i]);
}else{
TextRenderer::draw_text(t_x+App::text_padding, ypos, drawlines[i], App::theme.main_text_color);
}
ypos += TextRenderer::get_text_height();
}
Widget::render();
}
void Label::position(int x, int y, int w, int h) {
std::lock_guard<std::mutex> lock(positioning);
t_x = x;
t_y = y;
t_w = w;
t_h = h;
if (POSITIONER) {
POSITIONER(this);
}
const int mx = App::mouseX;
const int my = App::mouseY;
if (cursor_in_this) {
App::expectedCursorType = 3;
}
if (old_width == t_w) { return; }
old_width = t_w;
drawlines.clear();
drawColors.clear();
icu::UnicodeString curline = icu::UnicodeString();
should_be_h = App::text_padding*2;
int linewidth = 0;
int most_allowed = t_w-App::text_padding*2;
int curspan = 0;
std::vector<Color*> curlineColor = {};
Color* thisColor;
int byteIndex = 0;
for (auto ci = 0; ci < fulltext.length(); ci++) {
char16_t c = fulltext.charAt(ci);
int bytes = 0;
int emojiUtf16Count = get_emoji_sequence_length(fulltext, ci);
if (emojiUtf16Count == 0) {
bytes = U8_LENGTH(c);
}else{
bytes = emojiUtf16Count*2;
}
if (handlingColor) {
thisColor = App::theme.main_text_color;
if (curspan < colorSpans.size()) { // spans are always ordered so that we can only look at one at a time (mucho faster)
if (byteIndex >= colorSpans[curspan].start && byteIndex < colorSpans[curspan].end) {
auto t = colorSpans[curspan].type;
if (t == MarkdownElem::Header) {
thisColor = App::theme.equal_diff;
}else if (t == MarkdownElem::Bold) {
thisColor = App::theme.add_diff;
}else if (t == MarkdownElem::Italic) {
thisColor = App::theme.warning_color;
}else if (t == MarkdownElem::Link) {
thisColor = App::theme.equal_diff;
}else if (t == MarkdownElem::Code) {
thisColor = App::theme.warning_color;
}
}
if (byteIndex == colorSpans[curspan].end) {
curspan ++;
}
}
}
byteIndex += bytes;
int num_chr = 1;
if (c == U'\t') {
num_chr = App::settings->getValue("tab_width", 4);
c = ' ';
}
int newlen = linewidth + TextRenderer::get_text_width(num_chr);
if (emojiUtf16Count != 0) {
newlen += TextRenderer::get_text_width(1);
}
if (c == U'\n' || newlen > most_allowed) {
drawlines.push_back(curline);
curline = "";
if (handlingColor) {
drawColors.push_back(curlineColor);
curlineColor = {};
}
should_be_h += TextRenderer::get_text_height();
linewidth = 0;
newlen = linewidth + TextRenderer::get_text_width(num_chr);
}
if (c != U'\n') {
if (emojiUtf16Count != 0) {
for (int i = 0; i < emojiUtf16Count; i++) {
curline.append(fulltext.charAt(ci+i));
if (handlingColor) {
curlineColor.push_back(thisColor);
}
}
}else{
for (int rep = 0; rep < num_chr; rep++) {
curline += c;
if (handlingColor) {
curlineColor.push_back(thisColor);
}
}
}
}
linewidth = newlen;
if (emojiUtf16Count != 0) {
ci += emojiUtf16Count-1;
}
}
drawlines.push_back(curline);
if (handlingColor) {
drawColors.push_back(curlineColor);
}
should_be_h += TextRenderer::get_text_height();
if (t_x < App::mouseX && App::mouseX < t_x+t_w && t_y < App::mouseY && App::mouseY < t_y+t_h) {
if (App::expectedCursorType == -1 && cursor_in_this) { // only set cursor if expected to be arrow right now
App::expectedCursorType = 3; // hand cursor
}
}
}