aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/TextBox.cpp
blob: 4a8d6658ddabeb3bd22d9f88be12d42557b7cfb7 (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
83
84
85
#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{100, 24});

  service_ = std::make_unique<TextControlService<TextBox>>(this);
  service_->SetEnabled(true);
  service_->SetCaretVisible(true);
  service_->SetEditable(true);

  border_render_object_->SetBorderEnabled(true);
  border_render_object_->SetBorderStyle(border_style_.normal);

  GainFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) {
    this->service_->SetCaretVisible(true);
    this->UpdateBorderStyle();
  });

  LoseFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) {
    this->service_->SetCaretVisible(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