diff options
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/ui/controls/text_box.cpp | 5 | ||||
-rw-r--r-- | src/ui/controls/text_control_service.hpp | 34 | ||||
-rw-r--r-- | src/ui/render/scroll_render_object.cpp | 1 |
4 files changed, 29 insertions, 13 deletions
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index a8fd46a5..777e4fbc 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -25,6 +25,7 @@ add_library(cru_ui STATIC render/flex_layout_render_object.cpp render/layout_utility.cpp render/render_object.cpp + render/scroll_render_object.cpp render/stack_layout_render_object.cpp render/text_render_object.cpp render/window_render_object.cpp @@ -54,6 +55,7 @@ target_sources(cru_ui PUBLIC ${CRU_UI_INCLUDE_DIR}/render/layout_render_object.hpp ${CRU_UI_INCLUDE_DIR}/render/layout_utility.hpp ${CRU_UI_INCLUDE_DIR}/render/render_object.hpp + ${CRU_UI_INCLUDE_DIR}/render/scroll_render_object.hpp ${CRU_UI_INCLUDE_DIR}/render/stack_layout_render_object.hpp ${CRU_UI_INCLUDE_DIR}/render/text_render_object.hpp ${CRU_UI_INCLUDE_DIR}/render/window_render_object.hpp 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); diff --git a/src/ui/render/scroll_render_object.cpp b/src/ui/render/scroll_render_object.cpp new file mode 100644 index 00000000..f77ee636 --- /dev/null +++ b/src/ui/render/scroll_render_object.cpp @@ -0,0 +1 @@ +#include "cru/ui/render/scroll_render_object.hpp" |