aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-09-11 11:17:56 +0800
committercrupest <crupest@outlook.com>2020-09-11 11:17:56 +0800
commit93b8a62d28f9427f0979d62b71f12fb2858064ee (patch)
tree8f77e88a2a48db2a4e9fe72e5c85088f8d659a7f /src
parent65ca046989cd641da65b754bfa1c99f5e54b219a (diff)
downloadcru-93b8a62d28f9427f0979d62b71f12fb2858064ee.tar.gz
cru-93b8a62d28f9427f0979d62b71f12fb2858064ee.tar.bz2
cru-93b8a62d28f9427f0979d62b71f12fb2858064ee.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/ui/controls/TextBox.cpp2
-rw-r--r--src/ui/controls/TextControlService.hpp64
-rw-r--r--src/ui/render/TextRenderObject.cpp4
-rw-r--r--src/win/graph/direct/TextLayout.cpp2
-rw-r--r--src/win/native/Keyboard.cpp6
5 files changed, 74 insertions, 4 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));
}
diff --git a/src/win/graph/direct/TextLayout.cpp b/src/win/graph/direct/TextLayout.cpp
index 7b5b230f..0d4a6392 100644
--- a/src/win/graph/direct/TextLayout.cpp
+++ b/src/win/graph/direct/TextLayout.cpp
@@ -26,6 +26,8 @@ DWriteTextLayout::~DWriteTextLayout() = default;
std::u16string DWriteTextLayout::GetText() { return text_; }
+std::u16string_view DWriteTextLayout::GetTextView() { return text_; }
+
void DWriteTextLayout::SetText(std::u16string new_text) {
text_.swap(new_text);
ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout(
diff --git a/src/win/native/Keyboard.cpp b/src/win/native/Keyboard.cpp
index aa22e4a4..929ca737 100644
--- a/src/win/native/Keyboard.cpp
+++ b/src/win/native/Keyboard.cpp
@@ -66,9 +66,9 @@ KeyCode VirtualKeyToKeyCode(int virtual_key) {
KeyModifier RetrieveKeyMofifier() {
KeyModifier result{0};
- if (::GetKeyState(VK_SHIFT) < 0) result |= key_modifiers::shift;
- if (::GetKeyState(VK_CONTROL) < 0) result |= key_modifiers::ctrl;
- if (::GetKeyState(VK_MENU) < 0) result |= key_modifiers::alt;
+ if (::GetKeyState(VK_SHIFT) < 0) result |= KeyModifiers::shift;
+ if (::GetKeyState(VK_CONTROL) < 0) result |= KeyModifiers::ctrl;
+ if (::GetKeyState(VK_MENU) < 0) result |= KeyModifiers::alt;
return result;
}
} // namespace cru::platform::native::win