diff options
Diffstat (limited to 'src/ui/controls/text_box.cpp')
-rw-r--r-- | src/ui/controls/text_box.cpp | 220 |
1 files changed, 108 insertions, 112 deletions
diff --git a/src/ui/controls/text_box.cpp b/src/ui/controls/text_box.cpp index 83311548..28e1053d 100644 --- a/src/ui/controls/text_box.cpp +++ b/src/ui/controls/text_box.cpp @@ -20,149 +20,145 @@ namespace cru::ui::controls GetBorderProperty() = UiManager::GetInstance()->GetPredefineResources()->text_box_border; SetBordered(true); - } - - TextBox::~TextBox() = default; - StringView TextBox::GetControlType() const - { - return control_type; - } - - void TextBox::OnDrawContent(ID2D1DeviceContext* device_context) - { - TextControl::OnDrawContent(device_context); - if (is_caret_show_) + draw_content_event.AddHandler([this](events::DrawEventArgs& args) { - const auto caret_half_width = UiManager::GetInstance()->GetCaretInfo().half_caret_width; - FLOAT x, y; - DWRITE_HIT_TEST_METRICS metrics{}; - ThrowIfFailed(text_layout_->HitTestTextPosition(caret_position_, FALSE, &x, &y, &metrics)); - device_context->FillRectangle(D2D1::RectF(metrics.left - caret_half_width, metrics.top, metrics.left + caret_half_width, metrics.top + metrics.height), caret_brush_.Get()); - } - } + const auto device_context = args.GetDeviceContext(); + if (is_caret_show_) + { + const auto caret_half_width = UiManager::GetInstance()->GetCaretInfo().half_caret_width; + FLOAT x, y; + DWRITE_HIT_TEST_METRICS metrics{}; + ThrowIfFailed(text_layout_->HitTestTextPosition(caret_position_, FALSE, &x, &y, &metrics)); + device_context->FillRectangle(D2D1::RectF(metrics.left - caret_half_width, metrics.top, metrics.left + caret_half_width, metrics.top + metrics.height), caret_brush_.Get()); + } + }); - void TextBox::OnGetFocusCore(events::FocusChangeEventArgs& args) - { - TextControl::OnGetFocusCore(args); - assert(!caret_timer_.has_value()); - is_caret_show_ = true; - caret_timer_ = SetInterval(UiManager::GetInstance()->GetCaretInfo().caret_blink_duration, [this] + get_focus_event.bubble.AddHandler([this](events::FocusChangeEventArgs& args) { - is_caret_show_ = !is_caret_show_; - InvalidateDraw(); + assert(!caret_timer_.has_value()); + is_caret_show_ = true; + caret_timer_ = SetInterval(UiManager::GetInstance()->GetCaretInfo().caret_blink_duration, [this] + { + is_caret_show_ = !is_caret_show_; + InvalidateDraw(); + }); }); - } - void TextBox::OnLoseFocusCore(events::FocusChangeEventArgs& args) - { - Control::OnLoseFocusCore(args); - assert(caret_timer_.has_value()); - caret_timer_->Cancel(); - caret_timer_ = std::nullopt; - is_caret_show_ = false; - } + lose_focus_event.bubble.AddHandler([this](events::FocusChangeEventArgs& args) + { + assert(caret_timer_.has_value()); + caret_timer_->Cancel(); + caret_timer_ = std::nullopt; + is_caret_show_ = false; + }); - void TextBox::OnKeyDownCore(events::KeyEventArgs& args) - { - Control::OnKeyDownCore(args); - if (args.GetVirtualCode() == VK_LEFT && caret_position_ > 0) + key_down_event.bubble.AddHandler([this](events::KeyEventArgs& args) { - if (IsKeyDown(VK_SHIFT)) + if (args.GetVirtualCode() == VK_LEFT && caret_position_ > 0) { - if (GetCaretSelectionSide()) - ShiftLeftSelectionRange(-1); - else - ShiftRightSelectionRange(-1); - } - else - { - const auto selection = GetSelectedRange(); - if (selection.has_value()) + if (IsKeyDown(VK_SHIFT)) { - ClearSelection(); - caret_position_ = selection.value().position; + if (GetCaretSelectionSide()) + ShiftLeftSelectionRange(-1); + else + ShiftRightSelectionRange(-1); } else - caret_position_--; + { + const auto selection = GetSelectedRange(); + if (selection.has_value()) + { + ClearSelection(); + caret_position_ = selection.value().position; + } + else + caret_position_--; + } + InvalidateDraw(); } - InvalidateDraw(); - } - if (args.GetVirtualCode() == VK_RIGHT && caret_position_ < GetText().size()) - { - if (IsKeyDown(VK_SHIFT)) + if (args.GetVirtualCode() == VK_RIGHT && caret_position_ < GetText().size()) { - if (GetCaretSelectionSide()) - ShiftLeftSelectionRange(1); - else - ShiftRightSelectionRange(1); - } - else - { - const auto selection = GetSelectedRange(); - if (selection.has_value()) + if (IsKeyDown(VK_SHIFT)) { - ClearSelection(); - caret_position_ = selection.value().position + selection.value().count; + if (GetCaretSelectionSide()) + ShiftLeftSelectionRange(1); + else + ShiftRightSelectionRange(1); } else - caret_position_++; + { + const auto selection = GetSelectedRange(); + if (selection.has_value()) + { + ClearSelection(); + caret_position_ = selection.value().position + selection.value().count; + } + else + caret_position_++; + } } - } - } + }); - void TextBox::OnCharCore(events::CharEventArgs& args) - { - Control::OnCharCore(args); - if (args.GetChar() == L'\b') + char_event.bubble.AddHandler([this](events::CharEventArgs& args) { - if (GetSelectedRange().has_value()) - { - const auto selection_range = GetSelectedRange().value(); - auto text = GetText(); - text.erase(text.cbegin() + selection_range.position, text.cbegin() + selection_range.position + selection_range.count); - SetText(text); - caret_position_ = selection_range.position; - ClearSelection(); - } - else + if (args.GetChar() == L'\b') { - if (caret_position_ > 0) + if (GetSelectedRange().has_value()) { + const auto selection_range = GetSelectedRange().value(); auto text = GetText(); - if (!text.empty()) + text.erase(text.cbegin() + selection_range.position, text.cbegin() + selection_range.position + selection_range.count); + SetText(text); + caret_position_ = selection_range.position; + ClearSelection(); + } + else + { + if (caret_position_ > 0) { - const auto position = --caret_position_; - text.erase(text.cbegin() + position); - SetText(text); + auto text = GetText(); + if (!text.empty()) + { + const auto position = --caret_position_; + text.erase(text.cbegin() + position); + SetText(text); + } } } + return; } - return; - } - if (std::iswprint(args.GetChar())) - { - if (GetSelectedRange().has_value()) - { - const auto selection_range = GetSelectedRange().value(); - auto text = GetText(); - text.erase(selection_range.position, selection_range.count); - text.insert(text.cbegin() + selection_range.position, args.GetChar()); - SetText(text); - caret_position_ = selection_range.position + 1; - ClearSelection(); - } - else + if (std::iswprint(args.GetChar())) { - ClearSelection(); - const auto position = caret_position_++; - auto text = GetText(); - text.insert(text.cbegin() + position, { args.GetChar() }); - SetText(text); + if (GetSelectedRange().has_value()) + { + const auto selection_range = GetSelectedRange().value(); + auto text = GetText(); + text.erase(selection_range.position, selection_range.count); + text.insert(text.cbegin() + selection_range.position, args.GetChar()); + SetText(text); + caret_position_ = selection_range.position + 1; + ClearSelection(); + } + else + { + ClearSelection(); + const auto position = caret_position_++; + auto text = GetText(); + text.insert(text.cbegin() + position, { args.GetChar() }); + SetText(text); + } } - } + }); + } + + TextBox::~TextBox() = default; + + StringView TextBox::GetControlType() const + { + return control_type; } void TextBox::RequestChangeCaretPosition(const unsigned position) |