aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/text_box.cpp
blob: 99164b9b56681a5864cb1d0c85755c3b1bc26b16 (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
#include "cru/ui/controls/text_box.hpp"

#include "cru/ui/render/border_render_object.hpp"
#include "cru/ui/render/canvas_render_object.hpp"
#include "cru/ui/render/stack_layout_render_object.hpp"
#include "cru/ui/render/text_render_object.hpp"
#include "cru/ui/ui_manager.hpp"
#include "text_control_service.hpp"

namespace cru::ui::controls {
using render::BorderRenderObject;
using render::CanvasRenderObject;
using render::StackLayoutRenderObject;
using render::TextRenderObject;

TextBox::TextBox()
    : border_render_object_(new BorderRenderObject()),
      stack_layout_render_object_(new StackLayoutRenderObject()),
      text_render_object_(),
      caret_render_object_(new CanvasRenderObject()),
      service_(new TextControlService<TextBox>(this)) {
  const auto theme_resources = UiManager::GetInstance()->GetThemeResources();

  caret_brush_ = theme_resources->caret_brush;
  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);

  border_render_object_->AddChild(stack_layout_render_object_.get(), 0);
  stack_layout_render_object_->AddChild(text_render_object_.get(), 0);
  stack_layout_render_object_->AddChild(caret_render_object_.get(), 1);

  border_render_object_->SetAttachedControl(this);
  stack_layout_render_object_->SetAttachedControl(this);
  text_render_object_->SetAttachedControl(this);
  caret_render_object_->SetAttachedControl(this);

  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::TextRenderObject* TextBox::GetTextRenderObject() {
  return text_render_object_.get();
}

render::CanvasRenderObject* TextBox::GetCaretRenderObject() {
  return caret_render_object_.get();
}

std::shared_ptr<platform::graph::IBrush> TextBox::GetCaretBrush() {
  return caret_brush_;
}

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