diff options
author | crupest <crupest@outlook.com> | 2020-05-23 23:50:00 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-05-23 23:50:00 +0800 |
commit | f3a8fd608a9776ef0a5f547da918a32cf6074060 (patch) | |
tree | 85b320479296ae12339ee1e28bab66ab001cb44b /src/ui/controls | |
parent | 75ff8a6a05afd02aaadf7e3049b0a0e305241182 (diff) | |
download | cru-f3a8fd608a9776ef0a5f547da918a32cf6074060.tar.gz cru-f3a8fd608a9776ef0a5f547da918a32cf6074060.tar.bz2 cru-f3a8fd608a9776ef0a5f547da918a32cf6074060.zip |
...
Diffstat (limited to 'src/ui/controls')
-rw-r--r-- | src/ui/controls/text_box.cpp | 5 | ||||
-rw-r--r-- | src/ui/controls/text_control_service.hpp | 34 |
2 files changed, 26 insertions, 13 deletions
diff --git a/src/ui/controls/text_box.cpp b/src/ui/controls/text_box.cpp index 7b63eea1..8b7dc692 100644 --- a/src/ui/controls/text_box.cpp +++ b/src/ui/controls/text_box.cpp @@ -29,6 +29,7 @@ TextBox::TextBox() : border_render_object_(new BorderRenderObject()) { service_ = std::make_unique<TextControlService<TextBox>>(this); service_->SetEnabled(true); + service_->SetCaretVisible(true); GainFocusEvent()->Direct()->AddHandler([this](event::FocusChangeEventArgs&) { this->service_->SetEnabled(true); @@ -43,6 +44,10 @@ TextBox::TextBox() : border_render_object_(new BorderRenderObject()) { TextBox::~TextBox() {} +render::RenderObject* TextBox::GetRenderObject() const { + return border_render_object_.get(); +} + render::TextRenderObject* TextBox::GetTextRenderObject() { return text_render_object_.get(); } diff --git a/src/ui/controls/text_control_service.hpp b/src/ui/controls/text_control_service.hpp index 57a6efa7..626423bf 100644 --- a/src/ui/controls/text_control_service.hpp +++ b/src/ui/controls/text_control_service.hpp @@ -10,8 +10,7 @@ #include "cru/ui/ui_event.hpp" namespace cru::ui::controls { -constexpr float caret_width = 2; -constexpr long long caret_blink_duration = 500; +constexpr int k_default_caret_blink_duration = 500; // TControl should inherits `Control` and has following methods: // ``` @@ -31,14 +30,11 @@ class TextControlService : public Object { 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); + int GetCaretBlinkDuration() { return caret_blink_duration_; } + void SetCaretBlinkDuration(int milliseconds); private: void AbortSelection(); @@ -60,8 +56,8 @@ class TextControlService : public Object { bool enable_ = false; bool caret_visible_ = false; - int caret_position_ = 0; long long caret_timer_id_ = -1; + int caret_blink_duration_ = k_default_caret_blink_duration; // nullopt means not selecting std::optional<MouseButton> select_down_button_; @@ -105,12 +101,22 @@ void TextControlService<TControl>::SetCaretVisible(bool visible) { if (this->enable_) { if (visible) { - this->SetupCaretTimer(); + this->SetupCaret(); } else { - this->TearDownCaretTimer(); + this->TearDownCaret(); } } -} // namespace cru::ui::controls +} + +template <typename TControl> +void TextControlService<TControl>::SetCaretBlinkDuration(int milliseconds) { + if (this->caret_blink_duration_ == milliseconds) return; + + if (this->enable_ && this->caret_visible_) { + this->TearDownCaret(); + this->SetupCaret(); + } +} template <typename TControl> void TextControlService<TControl>::AbortSelection() { @@ -130,7 +136,7 @@ void TextControlService<TControl>::SetupCaret() { this->control_->GetTextRenderObject()->SetDrawCaret(true); this->caret_timer_id_ = application->SetInterval( - std::chrono::milliseconds(caret_blink_duration), + std::chrono::milliseconds(this->caret_blink_duration_), [this] { this->control_->GetTextRenderObject()->ToggleDrawCaret(); }); } @@ -169,7 +175,6 @@ void TextControlService<TControl>::MouseMoveHandler( 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: {}, " "{}.", @@ -178,6 +183,7 @@ void TextControlService<TControl>::MouseMoveHandler( TextRange::FromTwoSides( static_cast<unsigned>(position), static_cast<unsigned>(this->select_start_position_))); + text_render_object->SetCaretPosition(position); } } @@ -194,6 +200,8 @@ void TextControlService<TControl>::MouseDownHandler( const auto result = text_render_object->TextHitTest( text_render_object->FromRootToContent(args.GetPoint())); const auto position = result.position + (result.trailing ? 1 : 0); + text_render_object->SetSelectionRange(std::nullopt); + text_render_object->SetCaretPosition(position); this->select_start_position_ = position; log::Debug("TextControlService: Begin to select text, start position: {}.", position); |