blob: 2bc4cf95f4c4037047c8d742872e44482241137d (
plain)
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
|
#include "cru/ui/controls/TextBox.hpp"
#include "TextControlService.hpp"
#include "cru/ui/UiManager.hpp"
#include "cru/ui/render/BorderRenderObject.hpp"
#include "cru/ui/render/CanvasRenderObject.hpp"
#include "cru/ui/render/ScrollRenderObject.hpp"
#include "cru/ui/render/StackLayoutRenderObject.hpp"
#include "cru/ui/render/TextRenderObject.hpp"
namespace cru::ui::controls {
using render::BorderRenderObject;
using render::CanvasRenderObject;
using render::ScrollRenderObject;
using render::StackLayoutRenderObject;
using render::TextRenderObject;
TextBox::TextBox()
: border_render_object_(new BorderRenderObject()),
scroll_render_object_(new ScrollRenderObject()) {
const auto theme_resources = UiManager::GetInstance()->GetThemeResources();
border_style_ = theme_resources->text_box_border_style;
text_render_object_ = std::make_unique<TextRenderObject>(
theme_resources->text_brush, theme_resources->default_font,
theme_resources->text_selection_brush, theme_resources->caret_brush);
border_render_object_->AddChild(scroll_render_object_.get(), 0);
scroll_render_object_->AddChild(text_render_object_.get(), 0);
border_render_object_->SetAttachedControl(this);
scroll_render_object_->SetAttachedControl(this);
text_render_object_->SetAttachedControl(this);
text_render_object_->SetMinSize(Size{50, 20});
service_ = std::make_unique<TextControlService<TextBox>>(this);
service_->SetEnabled(true);
service_->SetCaretVisible(true);
service_->SetEditable(true);
GainFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) {
this->service_->SetEnabled(true);
this->UpdateBorderStyle();
});
LoseFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) {
this->service_->SetEnabled(false);
this->UpdateBorderStyle();
});
}
TextBox::~TextBox() {}
render::RenderObject* TextBox::GetRenderObject() const {
return border_render_object_.get();
}
gsl::not_null<render::TextRenderObject*> TextBox::GetTextRenderObject() {
return text_render_object_.get();
}
render::ScrollRenderObject* TextBox::GetScrollRenderObject() {
return scroll_render_object_.get();
}
const TextBoxBorderStyle& TextBox::GetBorderStyle() { return border_style_; }
void TextBox::SetBorderStyle(TextBoxBorderStyle border_style) {
border_style_ = std::move(border_style);
}
void TextBox::OnMouseHoverChange(bool) { UpdateBorderStyle(); }
void TextBox::UpdateBorderStyle() {
const auto focus = HasFocus();
const auto hover = IsMouseOver();
border_render_object_->SetBorderStyle(
focus ? (hover ? border_style_.focus_hover : border_style_.focus)
: (hover ? border_style_.hover : border_style_.normal));
}
} // namespace cru::ui::controls
|