-
-
Notifications
You must be signed in to change notification settings - Fork 525
/
ui_widget.cpp
2775 lines (2298 loc) · 71.2 KB
/
ui_widget.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2016 Furrtek
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ui_widget.hpp"
#include "ui_painter.hpp"
#include "portapack.hpp"
#include <cstdint>
#include <cstddef>
#include <algorithm>
#include "chprintf.h"
#include "irq_controls.hpp"
#include "string_format.hpp"
#include "usb_serial_device_to_host.h"
#include "rtc_time.hpp"
using namespace portapack;
using namespace rtc_time;
namespace ui {
static bool ui_dirty = true;
void dirty_set() {
ui_dirty = true;
}
void dirty_clear() {
ui_dirty = false;
}
bool is_dirty() {
return ui_dirty;
}
/* Widget ****************************************************************/
const std::vector<Widget*> Widget::no_children{};
Point Widget::screen_pos() {
return screen_rect().location();
}
Size Widget::size() const {
return _parent_rect.size();
}
Rect Widget::screen_rect() const {
return parent() ? (parent_rect() + parent()->screen_pos()) : parent_rect();
}
Rect Widget::parent_rect() const {
return _parent_rect;
}
void Widget::set_parent_rect(const Rect new_parent_rect) {
_parent_rect = new_parent_rect;
set_dirty();
}
Widget* Widget::parent() const {
return parent_;
}
void Widget::set_parent(Widget* const widget) {
if (widget == parent_) {
return;
}
if (parent_ && !widget) {
// We have a parent, but are losing it. Update visible status.
dirty_overlapping_children_in_rect(screen_rect());
visible(false);
}
parent_ = widget;
set_dirty();
}
void Widget::set_dirty() {
flags.dirty = true;
dirty_set();
}
bool Widget::dirty() const {
return flags.dirty;
}
void Widget::set_clean() {
flags.dirty = false;
}
void Widget::hidden(bool hide) {
if (hide != flags.hidden) {
flags.hidden = hide;
// If parent is hidden, either of these is a no-op.
if (hide) {
// TODO: Instead of dirtying parent entirely, dirty only children
// that overlap with this widget.
// parent()->dirty_overlapping_children_in_rect(parent_rect());
/* TODO: Notify self and all non-hidden children that they're
* now effectively hidden?
*/
} else {
set_dirty();
/* TODO: Notify self and all non-hidden children that they're
* now effectively shown?
*/
}
}
}
void Widget::focus() {
context().focus_manager().set_focus_widget(this);
}
void Widget::on_focus() {
}
void Widget::blur() {
context().focus_manager().set_focus_widget(nullptr);
}
void Widget::on_blur() {
}
bool Widget::focusable() const {
return flags.focusable;
}
void Widget::set_focusable(const bool value) {
flags.focusable = value;
}
bool Widget::has_focus() {
return (context().focus_manager().focus_widget() == this);
}
bool Widget::on_key(const KeyEvent event) {
(void)event;
return false;
}
bool Widget::on_encoder(const EncoderEvent event) {
(void)event;
return false;
}
bool Widget::on_touch(const TouchEvent event) {
(void)event;
return false;
}
bool Widget::on_keyboard(const KeyboardEvent event) {
(void)event;
return false;
}
const std::vector<Widget*>& Widget::children() const {
return no_children;
}
Context& Widget::context() const {
chDbgAssert(parent_, "parent_ is null",
"Check that parent isn't null before deref.");
return parent()->context();
}
void Widget::set_style(const Style* new_style) {
if (new_style != style_) {
style_ = new_style;
set_dirty();
}
}
const Style& Widget::style() const {
return style_ ? *style_ : parent()->style();
}
void Widget::visible(bool v) {
if (v != flags.visible) {
flags.visible = v;
/* TODO: This on_show/on_hide implementation seems inelegant.
* But I need *some* way to take/configure resources when
* a widget becomes visible, and reverse the process when the
* widget becomes invisible, whether the widget (or parent) is
* hidden, or the widget (or parent) is removed from the tree.
*/
if (v) {
on_show();
} else {
on_hide();
// Set all children invisible too.
for (const auto child : children()) {
child->visible(false);
}
}
}
}
bool Widget::highlighted() const {
return flags.highlighted;
}
void Widget::set_highlighted(const bool value) {
flags.highlighted = value;
}
void Widget::dirty_overlapping_children_in_rect(const Rect& child_rect) {
for (auto child : children()) {
if (!child_rect.intersect(child->parent_rect()).is_empty()) {
child->set_dirty();
}
}
}
void Widget::getAccessibilityText(std::string& result) {
result = "";
}
void Widget::getWidgetName(std::string& result) {
result = "";
}
/* View ******************************************************************/
void View::paint(Painter& painter) {
painter.fill_rectangle(
screen_rect(),
style().background);
}
void View::add_child(Widget* const widget) {
if (widget) {
if (widget->parent() == nullptr) {
widget->set_parent(this);
children_.push_back(widget);
}
}
}
void View::add_children(const std::initializer_list<Widget*> children) {
children_.insert(std::end(children_), children);
for (auto child : children) {
child->set_parent(this);
}
}
void View::remove_child(Widget* const widget) {
if (widget) {
children_.erase(std::remove(children_.begin(), children_.end(), widget), children_.end());
widget->set_parent(nullptr);
}
}
void View::remove_children(const std::vector<Widget*>& children) {
for (auto child : children) {
remove_child(child);
}
}
const std::vector<Widget*>& View::children() const {
return children_;
}
std::string View::title() const {
return "";
};
/* OptionTabView *********************************************************/
OptionTabView::OptionTabView(Rect parent_rect) {
set_parent_rect(parent_rect);
add_child(&check_enable);
hidden(true);
check_enable.on_select = [this](Checkbox&, bool value) {
enabled = value;
};
}
void OptionTabView::set_enabled(bool value) {
check_enable.set_value(value);
}
bool OptionTabView::is_enabled() {
return check_enable.value();
}
void OptionTabView::set_type(std::string type) {
check_enable.set_text("Transmit " + type);
}
void OptionTabView::focus() {
check_enable.focus();
}
/* Rectangle *************************************************************/
Rectangle::Rectangle(
Color c)
: Widget{},
color{c} {
}
Rectangle::Rectangle(
Rect parent_rect,
Color c)
: Widget{parent_rect},
color{c} {
}
void Rectangle::set_color(const Color c) {
color = c;
set_dirty();
}
void Rectangle::set_outline(const bool outline) {
_outline = outline;
set_dirty();
}
void Rectangle::paint(Painter& painter) {
if (!_outline) {
painter.fill_rectangle(
screen_rect(),
color);
} else {
painter.draw_rectangle(
screen_rect(),
color);
}
}
/* Text ******************************************************************/
Text::Text(
Rect parent_rect,
std::string text)
: Widget{parent_rect},
text{std::move(text)} {
}
Text::Text(
Rect parent_rect)
: Text{parent_rect, {}} {
}
void Text::set(std::string_view value) {
text = std::string{value};
set_dirty();
}
void Text::getAccessibilityText(std::string& result) {
result = text;
}
void Text::getWidgetName(std::string& result) {
result = "Text";
}
void Text::paint(Painter& painter) {
const auto rect = screen_rect();
auto s = has_focus() ? style().invert() : style();
auto max_len = (unsigned)rect.width() / s.font.char_width();
auto text_view = std::string_view{text};
painter.fill_rectangle(rect, s.background);
if (text_view.length() > max_len)
text_view = text_view.substr(0, max_len);
painter.draw_string(
rect.location(),
s,
text_view);
}
/* Labels ****************************************************************/
Labels::Labels(
std::initializer_list<Label> labels)
: labels_{labels} {
}
void Labels::set_labels(std::initializer_list<Label> labels) {
labels_ = labels;
set_dirty();
}
void Labels::paint(Painter& painter) {
for (auto& label : labels_) {
painter.draw_string(
label.pos + screen_pos(),
style().font,
label.color,
style().background,
label.text);
}
}
void Labels::getAccessibilityText(std::string& result) {
result = "";
for (auto& label : labels_) {
result += label.text;
result += ", ";
}
}
void Labels::getWidgetName(std::string& result) {
result = "Labels";
}
/* LiveDateTime **********************************************************/
void LiveDateTime::on_tick_second() {
rtc_time::now(datetime);
text = "";
if (!hide_clock) {
if (date_enabled) {
text = to_string_dec_uint(datetime.year(), 4, '0') + "-" +
to_string_dec_uint(datetime.month(), 2, '0') + "-" +
to_string_dec_uint(datetime.day(), 2, '0') + " ";
} else {
text = " ";
}
text = text + to_string_dec_uint(datetime.hour(), 2, '0') + ":" + to_string_dec_uint(datetime.minute(), 2, '0');
if (seconds_enabled) {
text += ":";
if (init_delay == 0)
text += to_string_dec_uint(datetime.second(), 2, '0');
else {
// Placeholder while the seconds are not updated
text += "XX";
init_delay--;
}
}
}
set_dirty();
}
LiveDateTime::LiveDateTime(
Rect parent_rect)
: Widget{parent_rect} {
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
this->on_tick_second();
};
}
LiveDateTime::~LiveDateTime() {
rtc_time::signal_tick_second -= signal_token_tick_second;
}
void LiveDateTime::paint(Painter& painter) {
const auto rect = screen_rect();
const auto s = style();
on_tick_second();
painter.fill_rectangle(rect, s.background);
painter.draw_string(
rect.location(),
s,
text);
}
void LiveDateTime::set_hide_clock(bool new_value) {
this->hide_clock = new_value;
}
void LiveDateTime::set_date_enabled(bool new_value) {
this->date_enabled = new_value;
}
void LiveDateTime::set_seconds_enabled(bool new_value) {
this->seconds_enabled = new_value;
}
/* BigFrequency **********************************************************/
BigFrequency::BigFrequency(
Rect parent_rect,
rf::Frequency frequency)
: Widget{parent_rect},
_frequency{frequency} {
}
void BigFrequency::set(const rf::Frequency frequency) {
_frequency = frequency;
set_dirty();
}
void BigFrequency::paint(Painter& painter) {
uint32_t i, digit_def;
std::array<char, 7> digits;
char digit;
Point digit_pos;
ui::Color segment_color;
const auto rect = screen_rect();
// Erase
painter.fill_rectangle(
{{0, rect.location().y()}, {screen_width, 52}},
Theme::getInstance()->bg_darkest->background);
// Prepare digits
if (!_frequency) {
digits.fill(10); // ----.---
digit_pos = {0, rect.location().y()};
} else {
_frequency /= 1000; // GMMM.KKK(uuu)
for (i = 0; i < 7; i++) {
digits[6 - i] = _frequency % 10;
_frequency /= 10;
}
// Remove leading zeros
for (i = 0; i < 3; i++) {
if (!digits[i])
digits[i] = 16; // "Don't draw" code
else
break;
}
digit_pos = {(Coord)(240 - ((7 * digit_width) + 8) - (i * digit_width)) / 2, rect.location().y()};
}
segment_color = style().foreground;
// Draw
for (i = 0; i < 7; i++) {
digit = digits[i];
if (digit < 16) {
digit_def = segment_font[(uint8_t)digit];
for (size_t s = 0; s < 7; s++) {
if (digit_def & 1)
painter.fill_rectangle({digit_pos + segments[s].location(), segments[s].size()}, segment_color);
digit_def >>= 1;
}
}
if (i == 3) {
// Dot
painter.fill_rectangle({digit_pos + Point(34, 48), {4, 4}}, segment_color);
digit_pos += {(digit_width + 8), 0};
} else {
digit_pos += {digit_width, 0};
}
}
}
/* ProgressBar ***********************************************************/
ProgressBar::ProgressBar(
Rect parent_rect)
: Widget{parent_rect} {
}
void ProgressBar::set_max(const uint32_t max) {
if (max == _max) return;
if (_value > _max)
_value = _max;
_max = max;
set_dirty();
}
void ProgressBar::set_value(const uint32_t value) {
if (value == _value) return;
if (value > _max)
_value = _max;
else
_value = value;
set_dirty();
}
void ProgressBar::getAccessibilityText(std::string& result) {
result = to_string_dec_uint(_value) + " / " + to_string_dec_uint(_max);
}
void ProgressBar::getWidgetName(std::string& result) {
result = "ProgressBar";
}
void ProgressBar::paint(Painter& painter) {
int v_scaled;
const auto sr = screen_rect();
const auto s = style();
v_scaled = (sr.size().width() * (uint64_t)_value) / _max;
painter.fill_rectangle({sr.location(), {v_scaled, sr.size().height()}}, style().foreground);
painter.fill_rectangle({{sr.location().x() + v_scaled, sr.location().y()}, {sr.size().width() - v_scaled, sr.size().height()}}, s.background);
painter.draw_rectangle(sr, s.foreground);
}
/* ActivityDot ***********************************************************/
ActivityDot::ActivityDot(
Rect parent_rect,
Color color)
: Widget{parent_rect},
_color{color} {}
void ActivityDot::paint(Painter& painter) {
painter.fill_rectangle(screen_rect(), _on ? _color : Theme::getInstance()->bg_medium->background);
}
void ActivityDot::toggle() {
_on = !_on;
set_dirty();
}
void ActivityDot::reset() {
_on = false;
set_dirty();
}
/* Console ***************************************************************/
Console::Console(
Rect parent_rect)
: Widget{parent_rect} {
}
void Console::clear(bool clear_buffer = false) {
if (clear_buffer)
buffer.clear();
if (!hidden() && visible()) {
display.fill_rectangle(
screen_rect(),
Theme::getInstance()->bg_darkest->background);
}
pos = {0, 0};
}
void Console::write(std::string message) {
bool escape = false;
if (!hidden() && visible()) {
const Style& s = style();
const Font& font = s.font;
auto rect = screen_rect();
ui::Color pen_color = s.foreground;
for (auto c : message) {
if (escape) {
if (c < std::size(term_colors))
pen_color = term_colors[(uint8_t)c];
else
pen_color = s.foreground;
escape = false;
} else {
if (c == '\n') {
crlf();
} else if (c == '\r') {
pos = {0, pos.y()};
} else if (c == '\x1B') {
escape = true;
} else {
auto glyph = font.glyph(c);
auto advance = glyph.advance();
// Would drawing next character be off the end? Newline.
if ((pos.x() + advance.x()) > rect.width())
crlf();
Point pos_glyph{
rect.left() + pos.x(),
display.scroll_area_y(pos.y())};
display.draw_glyph(pos_glyph, glyph, pen_color, s.background);
pos += {advance.x(), 0};
}
}
}
buffer = message;
} else {
if (buffer.size() < 256) buffer += message;
}
}
void Console::getAccessibilityText(std::string& result) {
result = "{" + buffer + "}";
}
void Console::getWidgetName(std::string& result) {
result = "Console";
}
void Console::writeln(std::string message) {
write(message + "\n");
}
void Console::paint(Painter&) {
write(buffer);
}
void Console::on_show() {
enable_scrolling(true);
clear();
}
bool Console::scrolling_enabled = false;
void Console::enable_scrolling(bool enable) {
if (enable) {
auto sr = screen_rect();
auto line_height = style().font.line_height();
// Count full lines that can fit in console's rectangle.
auto max_lines = sr.height() / line_height; // NB: int division to floor.
// The scroll area must be a multiple of the line_height
// or some lines will end up vertically truncated.
scroll_height = max_lines * line_height;
display.scroll_set_area(sr.top(), sr.top() + scroll_height);
display.scroll_set_position(0);
scrolling_enabled = true;
} else {
display.scroll_disable();
scrolling_enabled = false;
}
}
void Console::on_hide() {
/* TODO: Clear region to eliminate brief flash of content at un-shifted
* position? */
enable_scrolling(false);
}
void Console::crlf() {
if (hidden() || !visible()) return;
const auto& s = style();
auto sr = screen_rect();
auto line_height = s.font.line_height();
// Advance to the next line (\n) position and "carriage return" x to 0.
pos = {0, pos.y() + line_height};
if (pos.y() >= scroll_height) {
// Line is past off the "bottom", need to scroll.
if (!scrolling_enabled)
enable_scrolling(true);
// See the notes in lcd_ili9341.hpp about how scrolling works.
// The gist is that VSA will be moved to scroll the "top" off the
// screen. The drawing code uses 'scroll_area_y' to get the actual
// screen coordinate based on VSA. The "bottom" line is *always*
// at 'VSA + ((max_lines - 1) * line_height)' and so is constant.
pos = {0, scroll_height - line_height};
// Scroll off the "top" line.
display.scroll(-line_height);
// Clear the new line at the "bottom".
Rect dirty{sr.left(), display.scroll_area_y(pos.y()), sr.width(), line_height};
display.fill_rectangle(dirty, s.background);
}
}
/* Checkbox **************************************************************/
Checkbox::Checkbox(
Point parent_pos,
size_t length,
std::string text,
bool small)
: Widget{},
text_{text},
small_{small} {
if (!small_)
set_parent_rect({parent_pos, {static_cast<ui::Dim>((8 * length) + 24), 24}});
else
set_parent_rect({parent_pos, {static_cast<ui::Dim>((8 * length) + 16), 16}});
set_focusable(true);
}
void Checkbox::set_text(const std::string value) {
text_ = value;
set_dirty();
}
bool Checkbox::set_value(const bool value) {
value_ = value;
set_dirty();
if (on_select) {
on_select(*this, value_);
return true;
}
return false;
}
void Checkbox::getAccessibilityText(std::string& result) {
result = text_ + ((value_) ? " checked" : " unchecked");
}
void Checkbox::getWidgetName(std::string& result) {
result = "Checkbox";
}
bool Checkbox::value() const {
return value_;
}
void Checkbox::paint(Painter& painter) {
const auto r = screen_rect();
const auto paint_style = (has_focus() || highlighted()) ? style().invert() : style();
const auto x = r.location().x();
const auto y = r.location().y();
const auto label_r = paint_style.font.size_of(text_);
if (!small_) {
painter.draw_rectangle({{r.location()}, {24, 24}}, style().foreground);
painter.fill_rectangle({x + 1, y + 1, 24 - 2, 24 - 2}, style().background);
// Highlight
painter.draw_rectangle({x + 2, y + 2, 24 - 4, 24 - 4}, paint_style.background);
if (value_ == true) {
// Check
portapack::display.draw_line({x + 2, y + 14}, {x + 6, y + 18}, Theme::getInstance()->fg_green->foreground);
portapack::display.draw_line({x + 6, y + 18}, {x + 20, y + 4}, Theme::getInstance()->fg_green->foreground);
} else {
// Cross
portapack::display.draw_line({x + 1, y + 1}, {x + 24 - 2, y + 24 - 2}, Theme::getInstance()->fg_red->foreground);
portapack::display.draw_line({x + 24 - 2, y + 1}, {x + 1, y + 24 - 2}, Theme::getInstance()->fg_red->foreground);
}
painter.draw_string(
{static_cast<Coord>(x + 24 + 4),
static_cast<Coord>(y + (24 - label_r.height()) / 2)},
paint_style,
text_);
} else {
painter.draw_rectangle({{r.location()}, {16, 16}}, style().foreground);
painter.fill_rectangle({x + 1, y + 1, 16 - 2, 16 - 2}, style().background);
// Highlight
painter.draw_rectangle({x + 1, y + 1, 16 - 2, 16 - 2}, paint_style.background);
if (value_ == true) {
// Check
portapack::display.draw_line({x + 2, y + 8}, {x + 6, y + 12}, Theme::getInstance()->fg_green->foreground);
portapack::display.draw_line({x + 6, y + 12}, {x + 13, y + 5}, Theme::getInstance()->fg_green->foreground);
} else {
// Cross
portapack::display.draw_line({x + 1, y + 1}, {x + 16 - 2, y + 16 - 2}, Theme::getInstance()->fg_red->foreground);
portapack::display.draw_line({x + 16 - 2, y + 1}, {x + 1, y + 16 - 2}, Theme::getInstance()->fg_red->foreground);
}
painter.draw_string(
{static_cast<Coord>(x + 16 + 2),
static_cast<Coord>(y + (16 - label_r.height()) / 2)},
paint_style,
text_);
}
}
bool Checkbox::on_key(const KeyEvent key) {
if (key == KeyEvent::Select)
return set_value(not value_);
return false;
}
bool Checkbox::on_keyboard(const KeyboardEvent event) {
if (event == 10 || event == 32) return set_value(not value_);
return false;
}
bool Checkbox::on_touch(const TouchEvent event) {
switch (event.type) {
case TouchEvent::Type::Start:
set_highlighted(true);
set_dirty();
return true;
case TouchEvent::Type::End:
set_highlighted(false);
value_ = not value_;
set_dirty();
if (on_select) {
on_select(*this, value_);
}
return true;
default:
return false;
}
}
/* Button ****************************************************************/
Button::Button(
Rect parent_rect,
std::string text,
bool instant_exec)
: Widget{parent_rect},
text_{text},
instant_exec_{instant_exec} {
set_focusable(true);
}
void Button::set_text(const std::string value) {
text_ = value;
set_dirty();
}
std::string Button::text() const {
return text_;
}
void Button::getAccessibilityText(std::string& result) {
result = text_;
}
void Button::getWidgetName(std::string& result) {
result = "Button";
}
void Button::paint(Painter& painter) {
Color bg, fg;
const auto r = screen_rect();
if (has_focus() || highlighted()) {
bg = style().foreground;
fg = Theme::getInstance()->bg_darkest->background;
} else {
bg = Theme::getInstance()->bg_medium->background;
fg = style().foreground;
}
const Style paint_style = {style().font, bg, fg};
painter.draw_rectangle({r.location(), {r.size().width(), 1}}, Theme::getInstance()->fg_light->foreground);
painter.draw_rectangle({r.location().x(), r.location().y() + r.size().height() - 1, r.size().width(), 1}, Theme::getInstance()->bg_dark->background);
painter.draw_rectangle({r.location().x() + r.size().width() - 1, r.location().y(), 1, r.size().height()}, Theme::getInstance()->bg_dark->background);
painter.fill_rectangle(
{r.location().x(), r.location().y() + 1, r.size().width() - 1, r.size().height() - 2},
paint_style.background);
const auto label_r = paint_style.font.size_of(text_);
painter.draw_string(
{r.location().x() + (r.size().width() - label_r.width()) / 2, r.location().y() + (r.size().height() - label_r.height()) / 2},
paint_style,
text_);
}
void Button::on_focus() {
if (on_highlight)
on_highlight(*this);
}
bool Button::on_key(const KeyEvent key) {
if (key == KeyEvent::Select) {
if (on_select) {
on_select(*this);
return true;
}
} else {