aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/text_control_service.hpp
blob: 0da8abed985abc64eb7656945652a555756bfbad (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
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
#pragma once
#include "cru/common/logger.hpp"
#include "cru/platform/graph/font.hpp"
#include "cru/platform/graph/painter.hpp"
#include "cru/platform/native/ui_application.hpp"
#include "cru/ui/control.hpp"
#include "cru/ui/render/canvas_render_object.hpp"
#include "cru/ui/render/text_render_object.hpp"
#include "cru/ui/ui_event.hpp"

namespace cru::ui::controls {
constexpr float caret_width = 2;
constexpr long long caret_blink_duration = 500;

// TControl should inherits `Control` and has following methods:
// ```
// render::TextRenderObject* GetTextRenderObject();
// render::CanvasRenderObject* GetCaretRenderObject();
// std::shared_ptr<platform::graph::IBrush> GetCaretBrush();
// ```
template <typename TControl>
class TextControlService : public Object {
 public:
  TextControlService(TControl* control);

  CRU_DELETE_COPY(TextControlService)
  CRU_DELETE_MOVE(TextControlService)

  ~TextControlService();

 public:
  bool IsEnabled() { return enable_; }
  void SetEnabled(bool enable);

  int GetCaretPosition() { return caret_position_; }
  void SetCaretPosition(int position) { caret_position_ = position; }

  bool IsCaretVisible() { return caret_visible_; }
  void SetCaretVisible(bool visible);

  // please set correct offset before calling this
  void DrawCaret(platform::graph::IPainter* painter);

 private:
  void AbortSelection();

  void SetupCaretTimer();
  void TearDownCaretTimer();

  void SetupHandlers();

  void MouseMoveHandler(event::MouseEventArgs& args);
  void MouseDownHandler(event::MouseButtonEventArgs& args);
  void MouseUpHandler(event::MouseButtonEventArgs& args);
  void LoseFocusHandler(event::FocusChangeEventArgs& args);

 private:
  TControl* control_;
  std::vector<EventRevokerGuard> event_revoker_guards_;

  bool enable_ = false;

  bool caret_visible_ = false;
  int caret_position_ = 0;
#ifdef CRU_DEBUG
  bool caret_timer_set_ = false;
#endif
  unsigned long caret_timer_tag_;
  // this is used for blinking of caret
  bool caret_show_ = true;

  // nullopt means not selecting
  std::optional<MouseButton> select_down_button_;

  // before the char
  int select_start_position_;
};

template <typename TControl>
TextControlService<TControl>::TextControlService(TControl* control)
    : control_(control) {}

template <typename TControl>
TextControlService<TControl>::~TextControlService() {
  if (enable_ && caret_visible_) TearDownCaretTimer();
}

template <typename TControl>
void TextControlService<TControl>::SetEnabled(bool enable) {
  if (enable == enable_) return;
  if (enable) {
    AbortSelection();
    SetupHandlers();
    if (caret_visible_) {
      SetupCaretTimer();
    }
  } else {
    event_revoker_guards_.clear();
    if (caret_visible_) {
      TearDownCaretTimer();
    }
  }
}

template <typename TControl>
void TextControlService<TControl>::SetCaretVisible(bool visible) {
  if (visible == caret_visible_) return;

  if (enable_) {
    if (visible) {
      SetupCaretTimer();
    }
  } else {
    TearDownCaretTimer();
  }
}  // namespace cru::ui::controls

template <typename TControl>
void TextControlService<TControl>::DrawCaret(
    platform::graph::IPainter* painter) {
  if (caret_show_) {
    const auto text_render_object = control_->GetTextRenderObject();
    const auto point = text_render_object->TextSingleRect(
        caret_position_, false);  // Maybe cache the result???
    painter->FillRectangle(
        Rect{point,
             Size{caret_width, text_render_object->GetFont()->GetFontSize()}},
        control_->GetCaretBrush().get());
  }
}

template <typename TControl>
void TextControlService<TControl>::AbortSelection() {
  if (select_down_button_.has_value()) {
    control_->ReleaseMouse();
    select_down_button_ = std::nullopt;
  }
  control_->GetTextRenderObject()->SetSelectionRange(std::nullopt);
}

template <typename TControl>
void TextControlService<TControl>::SetupCaretTimer() {
#ifdef CRU_DEBUG
  Expects(!caret_timer_set_);
  caret_timer_set_ = true;
#endif
  caret_timer_tag_ =
      platform::native::IUiApplication::GetInstance()->SetInterval(
          std::chrono::milliseconds(caret_blink_duration), [this] {
            this->caret_show_ = !this->caret_show_;
            this->control_->GetCaretRenderObject()->InvalidatePaint();
          });
}

template <typename TControl>
void TextControlService<TControl>::TearDownCaretTimer() {
#ifdef CRU_DEBUG
  Expects(!caret_timer_set_);
  caret_timer_set_ = false;
#endif
  platform::native::IUiApplication::GetInstance()->CancelTimer(
      caret_timer_tag_);
}

template <typename TControl>
void TextControlService<TControl>::SetupHandlers() {
  Expects(event_revoker_guards_.empty());
  event_revoker_guards_.push_back(
      EventRevokerGuard{control_->MouseMoveEvent()->Direct()->AddHandler(
          std::bind(&TextControlService::MouseMoveHandler, this,
                    std::placeholders::_1))});
  event_revoker_guards_.push_back(
      EventRevokerGuard{control_->MouseDownEvent()->Direct()->AddHandler(
          std::bind(&TextControlService::MouseDownHandler, this,
                    std::placeholders::_1))});
  event_revoker_guards_.push_back(EventRevokerGuard{
      control_->MouseUpEvent()->Direct()->AddHandler(std::bind(
          &TextControlService::MouseUpHandler, this, std::placeholders::_1))});
  event_revoker_guards_.push_back(
      EventRevokerGuard{control_->LoseFocusEvent()->Direct()->AddHandler(
          std::bind(&TextControlService::LoseFocusHandler, this,
                    std::placeholders::_1))});
}

template <typename TControl>
void TextControlService<TControl>::MouseMoveHandler(
    event::MouseEventArgs& args) {
  if (this->select_down_button_.has_value()) {
    const auto text_render_object = this->control_->GetTextRenderObject();
    const auto result = text_render_object->TextHitTest(
        text_render_object->FromRootToContent(args.GetPoint()));
    const auto position = result.position + (result.trailing ? 1 : 0);
    this->caret_position_ = position;
    log::Debug(
        "TextControlService: Text selection changed on mouse move, range: {}, "
        "{}.",
        position, this->select_start_position_);
    this->control_->GetTextRenderObject()->SetSelectionRange(
        TextRange::FromTwoSides(
            static_cast<unsigned>(position),
            static_cast<unsigned>(this->select_start_position_)));
    this->control_->GetTextRenderObject()->InvalidatePaint();
    this->control_->GetCaretRenderObject()->InvalidatePaint();
  }
}

template <typename TControl>
void TextControlService<TControl>::MouseDownHandler(
    event::MouseButtonEventArgs& args) {
  if (this->select_down_button_.has_value()) {
    return;
  } else {
    if (!this->control_->CaptureMouse()) return;
    if (!this->control_->RequestFocus()) return;
    const auto text_render_object = this->control_->GetTextRenderObject();
    this->select_down_button_ = args.GetButton();
    const auto result = text_render_object->TextHitTest(
        text_render_object->FromRootToContent(args.GetPoint()));
    const auto position = result.position + (result.trailing ? 1 : 0);
    this->select_start_position_ = position;
    log::Debug("TextControlService: Begin to select text, start position: {}.",
               position);
  }
}

template <typename TControl>
void TextControlService<TControl>::MouseUpHandler(
    event::MouseButtonEventArgs& args) {
  if (this->select_down_button_.has_value() &&
      this->select_down_button_.value() == args.GetButton()) {
    this->control_->ReleaseMouse();
    this->select_down_button_ = std::nullopt;
    log::Debug("TextControlService: End selecting text.");
  }
}

template <typename TControl>
void TextControlService<TControl>::LoseFocusHandler(
    event::FocusChangeEventArgs& args) {
  if (!args.IsWindow()) this->AbortSelection();
}
}  // namespace cru::ui::controls