aboutsummaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/controls/TextBox.cpp2
-rw-r--r--src/ui/controls/TextControlService.hpp64
-rw-r--r--src/ui/render/TextRenderObject.cpp4
3 files changed, 69 insertions, 1 deletions
diff --git a/src/ui/controls/TextBox.cpp b/src/ui/controls/TextBox.cpp
index b25e3bcb..2bc4cf95 100644
--- a/src/ui/controls/TextBox.cpp
+++ b/src/ui/controls/TextBox.cpp
@@ -32,10 +32,12 @@ TextBox::TextBox()
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);
diff --git a/src/ui/controls/TextControlService.hpp b/src/ui/controls/TextControlService.hpp
index 41959f9d..0d05f2c3 100644
--- a/src/ui/controls/TextControlService.hpp
+++ b/src/ui/controls/TextControlService.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "../Helper.hpp"
#include "cru/common/Logger.hpp"
+#include "cru/common/StringUtil.hpp"
#include "cru/platform/graph/Font.hpp"
#include "cru/platform/graph/Painter.hpp"
#include "cru/platform/native/UiApplication.hpp"
@@ -40,6 +41,7 @@ class TextControlService : public Object {
void SetEnabled(bool enable) {
if (enable == this->enable_) return;
+ this->enable_ = enable;
if (enable) {
this->SetupHandlers();
if (this->caret_visible_) {
@@ -52,6 +54,10 @@ class TextControlService : public Object {
}
}
+ bool IsEditable() { return this->editable_; }
+
+ void SetEditable(bool editable) { this->editable_ = editable; }
+
bool IsCaretVisible() { return caret_visible_; }
void SetCaretVisible(bool visible) {
@@ -218,7 +224,62 @@ class TextControlService : public Object {
}
}
- void KeyDownHandler(event::KeyEventArgs& args) { CRU_UNUSED(args); }
+ void KeyDownHandler(event::KeyEventArgs& args) {
+ const auto key_code = args.GetKeyCode();
+ using cru::platform::native::KeyCode;
+ using cru::platform::native::KeyModifiers;
+
+ switch (key_code) {
+ case KeyCode::Left: {
+ const auto key_modifier = args.GetKeyModifier();
+ const bool shift = key_modifier & KeyModifiers::shift;
+ auto text = this->GetTextRenderObject()->GetTextView();
+ if (shift) {
+ auto selection = this->GetSelection();
+ if (selection) {
+ gsl::index new_position;
+ Utf16PreviousCodePoint(text, selection->GetEnd(), &new_position);
+ selection->SetEnd(new_position);
+ this->SetSelection(selection);
+ } else {
+ const auto caret = this->GetCaretPosition();
+ gsl::index new_position;
+ Utf16PreviousCodePoint(text, caret, &new_position);
+ this->SetSelection(TextRange::FromTwoSides(caret, new_position));
+ }
+ } else {
+ const auto caret = this->GetCaretPosition();
+ gsl::index new_position;
+ Utf16PreviousCodePoint(text, caret, &new_position);
+ this->SetCaretPosition(new_position);
+ }
+ } break;
+ case KeyCode::Right: {
+ const auto key_modifier = args.GetKeyModifier();
+ const bool shift = key_modifier & KeyModifiers::shift;
+ auto text = this->GetTextRenderObject()->GetTextView();
+ if (shift) {
+ auto selection = this->GetSelection();
+ if (selection) {
+ gsl::index new_position;
+ Utf16NextCodePoint(text, selection->GetEnd(), &new_position);
+ selection->SetEnd(new_position);
+ this->SetSelection(selection);
+ } else {
+ const auto caret = this->GetCaretPosition();
+ gsl::index new_position;
+ Utf16PreviousCodePoint(text, caret, &new_position);
+ this->SetSelection(TextRange::FromTwoSides(caret, new_position));
+ }
+ } else {
+ const auto caret = this->GetCaretPosition();
+ gsl::index new_position;
+ Utf16NextCodePoint(text, caret, &new_position);
+ this->SetCaretPosition(new_position);
+ }
+ }
+ }
+ }
void KeyUpHandler(event::KeyEventArgs& args) { CRU_UNUSED(args); }
@@ -231,6 +292,7 @@ class TextControlService : public Object {
std::vector<EventRevokerGuard> event_revoker_guards_;
bool enable_ = false;
+ bool editable_ = false;
bool caret_visible_ = false;
long long caret_timer_id_ = -1;
diff --git a/src/ui/render/TextRenderObject.cpp b/src/ui/render/TextRenderObject.cpp
index cd248cea..cecbe1f3 100644
--- a/src/ui/render/TextRenderObject.cpp
+++ b/src/ui/render/TextRenderObject.cpp
@@ -37,6 +37,10 @@ std::u16string TextRenderObject::GetText() const {
return text_layout_->GetText();
}
+std::u16string_view TextRenderObject::GetTextView() const {
+ return text_layout_->GetTextView();
+}
+
void TextRenderObject::SetText(std::u16string new_text) {
text_layout_->SetText(std::move(new_text));
}