diff options
author | crupest <crupest@outlook.com> | 2020-12-26 19:39:24 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-12-26 19:39:24 +0800 |
commit | 715be3c81b96fcf87c7650501d71480a8743a984 (patch) | |
tree | f56119db325f57280ab625cf1927b846e3eebdcb | |
parent | 61b835d47ab98279c5f2f43bebcd4fe18f7c408d (diff) | |
download | cru-715be3c81b96fcf87c7650501d71480a8743a984.tar.gz cru-715be3c81b96fcf87c7650501d71480a8743a984.tar.bz2 cru-715be3c81b96fcf87c7650501d71480a8743a984.zip |
...
-rw-r--r-- | include/cru/platform/gui/Keyboard.hpp | 1 | ||||
-rw-r--r-- | include/cru/ui/controls/TextHostControlService.hpp | 16 | ||||
-rw-r--r-- | include/cru/ui/helper/ShortcutHub.hpp | 9 | ||||
-rw-r--r-- | src/ui/controls/TextHostControlService.cpp | 242 | ||||
-rw-r--r-- | src/ui/helper/ShortcutHub.cpp | 13 |
5 files changed, 143 insertions, 138 deletions
diff --git a/include/cru/platform/gui/Keyboard.hpp b/include/cru/platform/gui/Keyboard.hpp index e12cccda..6c29239b 100644 --- a/include/cru/platform/gui/Keyboard.hpp +++ b/include/cru/platform/gui/Keyboard.hpp @@ -116,6 +116,7 @@ struct TagKeyModifier {}; using KeyModifier = Bitmask<details::TagKeyModifier>; struct KeyModifiers { + static constexpr KeyModifier none{0}; static constexpr KeyModifier shift{0b1}; static constexpr KeyModifier ctrl{0b10}; static constexpr KeyModifier alt{0b100}; diff --git a/include/cru/ui/controls/TextHostControlService.hpp b/include/cru/ui/controls/TextHostControlService.hpp index 0bea52c8..9e6a08bc 100644 --- a/include/cru/ui/controls/TextHostControlService.hpp +++ b/include/cru/ui/controls/TextHostControlService.hpp @@ -70,8 +70,10 @@ class TextHostControlService : public Object { void SetSelection(gsl::index caret_position); void SetSelection(TextRange selection, bool scroll_to_caret = true); - void DeleteSelectedText(); + void ChangeSelectionEnd(gsl::index new_end); + void AbortSelection(); + void DeleteSelectedText(); // If some text is selected, then they are deleted first. Then insert text // into caret position. void ReplaceSelectedText(std::u16string_view text); @@ -92,10 +94,6 @@ class TextHostControlService : public Object { void SyncTextRenderObject(); - void StartSelection(Index start); - void UpdateSelection(Index new_end); - void AbortSelection(); - void UpdateInputMethodPosition(); template <typename TArgs> @@ -106,16 +104,14 @@ class TextHostControlService : public Object { std::bind(handler, this, std::placeholders::_1)); } - void SetUpHandlers(); - void TearDownHandlers(); - void MouseMoveHandler(event::MouseEventArgs& args); void MouseDownHandler(event::MouseButtonEventArgs& args); void MouseUpHandler(event::MouseButtonEventArgs& args); - void KeyDownHandler(event::KeyEventArgs& args); void GainFocusHandler(event::FocusChangeEventArgs& args); void LoseFocusHandler(event::FocusChangeEventArgs& args); + void SetUpShortcuts(); + private: gsl::not_null<Control*> control_; gsl::not_null<ITextHostControl*> text_host_control_; @@ -136,6 +132,6 @@ class TextHostControlService : public Object { helper::ShortcutHub shortcut_hub_; // true if left mouse is down and selecting - bool mouse_move_selecting_; + bool mouse_move_selecting_ = false; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/helper/ShortcutHub.hpp b/include/cru/ui/helper/ShortcutHub.hpp index a4ff2da2..fe3414fe 100644 --- a/include/cru/ui/helper/ShortcutHub.hpp +++ b/include/cru/ui/helper/ShortcutHub.hpp @@ -19,8 +19,9 @@ namespace cru::ui::helper { class ShortcutKeyBind { public: - ShortcutKeyBind(platform::gui::KeyCode key, - platform::gui::KeyModifier modifier) + ShortcutKeyBind( + platform::gui::KeyCode key, + platform::gui::KeyModifier modifier = platform::gui::KeyModifiers::none) : key_(key), modifier_(modifier) {} CRU_DEFAULT_COPY(ShortcutKeyBind) @@ -111,6 +112,8 @@ class ShortcutHub : public Object { const std::vector<ShortcutInfo>& GetShortcutByKeyBind( const ShortcutKeyBind& key_bind) const; + IEvent<event::KeyEventArgs&>* FallbackKeyEvent() { return &fallback_event_; } + void Install(controls::Control* control); void Uninstall(); @@ -124,6 +127,8 @@ class ShortcutHub : public Object { int current_id_ = 1; + Event<event::KeyEventArgs&> fallback_event_; + EventRevokerListGuard event_guard_; }; } // namespace cru::ui::helper diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index 7126f7b7..91ec53ff 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -3,11 +3,15 @@ #include "../Helper.hpp" #include "cru/common/Logger.hpp" #include "cru/common/StringUtil.hpp" +#include "cru/platform/gui/Base.hpp" #include "cru/platform/gui/Cursor.hpp" #include "cru/platform/gui/InputMethod.hpp" +#include "cru/platform/gui/Keyboard.hpp" #include "cru/platform/gui/UiApplication.hpp" #include "cru/platform/gui/Window.hpp" +#include "cru/ui/Base.hpp" #include "cru/ui/DebugFlags.hpp" +#include "cru/ui/events/UiEvent.hpp" #include "cru/ui/helper/ShortcutHub.hpp" #include "cru/ui/host/WindowHost.hpp" #include "cru/ui/render/ScrollRenderObject.hpp" @@ -16,13 +20,27 @@ namespace cru::ui::controls { TextHostControlService::TextHostControlService(gsl::not_null<Control*> control) : control_(control), - text_host_control_(dynamic_cast<ITextHostControl*>(control.get())) {} + text_host_control_(dynamic_cast<ITextHostControl*>(control.get())) { + SetUpShortcuts(); + + SetupOneHandler(&Control::MouseMoveEvent, + &TextHostControlService::MouseMoveHandler); + SetupOneHandler(&Control::MouseDownEvent, + &TextHostControlService::MouseDownHandler); + SetupOneHandler(&Control::MouseUpEvent, + &TextHostControlService::MouseUpHandler); + SetupOneHandler(&Control::GainFocusEvent, + &TextHostControlService::GainFocusHandler); + SetupOneHandler(&Control::LoseFocusEvent, + &TextHostControlService::LoseFocusHandler); + + shortcut_hub_.Install(control_); +} void TextHostControlService::SetEnabled(bool enable) { if (enable == this->enable_) return; this->enable_ = enable; if (enable) { - this->SetUpHandlers(); if (this->caret_visible_) { this->SetupCaret(); } @@ -31,7 +49,6 @@ void TextHostControlService::SetEnabled(bool enable) { platform::gui::SystemCursorType::IBeam)); } else { this->AbortSelection(); - this->TearDownHandlers(); this->TearDownCaret(); this->control_->SetCursor(nullptr); } @@ -194,6 +211,20 @@ void TextHostControlService::SetSelection(TextRange selection, } } +void TextHostControlService::ChangeSelectionEnd(Index new_end) { + auto selection = GetSelection(); + selection.ChangeEnd(new_end); + this->SetSelection(selection); +} + +void TextHostControlService::AbortSelection() { + if (this->mouse_move_selecting_) { + this->control_->ReleaseMouse(); + this->mouse_move_selecting_ = false; + } + SetSelection(GetCaretPosition()); +} + void TextHostControlService::ReplaceSelectedText(std::u16string_view text) { DeleteSelectedText(); InsertText(GetSelection().GetStart(), text); @@ -222,21 +253,6 @@ void TextHostControlService::CoerceSelection() { this->selection_ = this->selection_.CoerceInto(0, text_.size()); } -void TextHostControlService::StartSelection(Index start) { - SetSelection(start); - if constexpr (debug_flags::text_service) - log::TagDebug(log_tag, u"Text selection started, position: {}.", start); -} - -void TextHostControlService::UpdateSelection(Index new_end) { - auto selection = GetSelection(); - selection.ChangeEnd(new_end); - this->SetSelection(selection); - if constexpr (debug_flags::text_service) - log::TagDebug(log_tag, u"Text selection updated, range: {}, {}.", - selection.GetStart(), selection.GetEnd()); -} - void TextHostControlService::SyncTextRenderObject() { const auto text_render_object = this->GetTextRenderObject(); const auto composition_info = this->GetCompositionInfo(); @@ -257,14 +273,6 @@ void TextHostControlService::SyncTextRenderObject() { } } -void TextHostControlService::AbortSelection() { - if (this->mouse_move_selecting_) { - this->control_->ReleaseMouse(); - this->mouse_move_selecting_ = false; - } - this->GetTextRenderObject()->SetSelectionRange(std::nullopt); -} - void TextHostControlService::UpdateInputMethodPosition() { if (auto input_method_context = this->GetInputMethodContext()) { Point right_bottom = @@ -283,47 +291,25 @@ void TextHostControlService::UpdateInputMethodPosition() { } } -void TextHostControlService::TearDownHandlers() { - event_guard_.Clear(); - shortcut_hub_.Uninstall(); -} -void TextHostControlService::SetUpHandlers() { - Expects(event_guard_.IsEmpty()); - - SetupOneHandler(&Control::MouseMoveEvent, - &TextHostControlService::MouseMoveHandler); - SetupOneHandler(&Control::MouseDownEvent, - &TextHostControlService::MouseDownHandler); - SetupOneHandler(&Control::MouseUpEvent, - &TextHostControlService::MouseUpHandler); - SetupOneHandler(&Control::KeyDownEvent, - &TextHostControlService::KeyDownHandler); - SetupOneHandler(&Control::GainFocusEvent, - &TextHostControlService::GainFocusHandler); - SetupOneHandler(&Control::LoseFocusEvent, - &TextHostControlService::LoseFocusHandler); - - shortcut_hub_.Install(control_); -} - void TextHostControlService::MouseDownHandler( event::MouseButtonEventArgs& args) { - if (this->mouse_move_selecting_) { - return; - } else { + if (IsEnabled()) { this->control_->SetFocus(); - if (!this->control_->CaptureMouse()) return; - this->mouse_move_selecting_ = true; - const auto text_render_object = this->GetTextRenderObject(); - const auto result = text_render_object->TextHitTest( - args.GetPointToContent(text_render_object)); - const auto position = result.position + (result.trailing ? 1 : 0); - StartSelection(position); + if (args.GetButton() == mouse_buttons::left && + !this->mouse_move_selecting_) { + if (!this->control_->CaptureMouse()) return; + this->mouse_move_selecting_ = true; + const auto text_render_object = this->GetTextRenderObject(); + const auto result = text_render_object->TextHitTest( + args.GetPointToContent(text_render_object)); + const auto position = result.position + (result.trailing ? 1 : 0); + SetSelection(position); + } } } -void TextHostControlService::MouseUpHandler(event::MouseButtonEventArgs&) { - if (mouse_move_selecting_) { +void TextHostControlService::MouseUpHandler(event::MouseButtonEventArgs& args) { + if (args.GetButton() == mouse_buttons::left && mouse_move_selecting_) { this->control_->ReleaseMouse(); this->mouse_move_selecting_ = false; } @@ -335,70 +321,7 @@ void TextHostControlService::MouseMoveHandler(event::MouseEventArgs& args) { const auto result = text_render_object->TextHitTest( args.GetPointToContent(text_render_object)); const auto position = result.position + (result.trailing ? 1 : 0); - UpdateSelection(position); - } -} - -void TextHostControlService::KeyDownHandler(event::KeyEventArgs& args) { - const auto key_code = args.GetKeyCode(); - using cru::platform::gui::KeyCode; - using cru::platform::gui::KeyModifiers; - - switch (key_code) { - case KeyCode::Backspace: { - if (!IsEditable()) return; - const auto selection = GetSelection(); - if (selection.count == 0) { - SetSelection(DeleteCharPrevious(GetCaretPosition())); - } else { - this->DeleteSelectedText(); - } - } break; - case KeyCode::Delete: { - if (!IsEditable()) return; - const auto selection = GetSelection(); - if (selection.count == 0) { - DeleteChar(GetCaretPosition()); - } else { - this->DeleteSelectedText(); - } - } break; - case KeyCode::Left: { - const auto key_modifier = args.GetKeyModifier(); - const bool shift = key_modifier & KeyModifiers::shift; - auto text = this->GetTextView(); - if (shift) { - auto selection = this->GetSelection(); - gsl::index new_position; - Utf16PreviousCodePoint(text, selection.GetEnd(), &new_position); - selection.ChangeEnd(new_position); - this->SetSelection(selection); - } else { - const auto caret = this->GetCaretPosition(); - gsl::index new_position; - Utf16PreviousCodePoint(text, caret, &new_position); - this->SetSelection(new_position); - } - } break; - case KeyCode::Right: { - const auto key_modifier = args.GetKeyModifier(); - const bool shift = key_modifier & KeyModifiers::shift; - auto text = this->GetTextView(); - if (shift) { - auto selection = this->GetSelection(); - gsl::index new_position; - Utf16NextCodePoint(text, selection.GetEnd(), &new_position); - selection.ChangeEnd(new_position); - this->SetSelection(selection); - } else { - const auto caret = this->GetCaretPosition(); - gsl::index new_position; - Utf16NextCodePoint(text, caret, &new_position); - this->SetSelection(new_position); - } - } break; - default: - break; + ChangeSelectionEnd(position); } } @@ -447,4 +370,73 @@ void TextHostControlService::LoseFocusHandler( SetCaretVisible(false); SyncTextRenderObject(); } + +void TextHostControlService::SetUpShortcuts() { + using platform::gui::KeyCode; + using platform::gui::KeyModifiers; + + shortcut_hub_.RegisterShortcut(u"Backspace", KeyCode::Backspace, [this] { + if (!IsEnabled()) return false; + if (!IsEditable()) return false; + const auto selection = GetSelection(); + if (selection.count == 0) { + SetSelection(DeleteCharPrevious(GetCaretPosition())); + } else { + this->DeleteSelectedText(); + } + return true; + }); + + shortcut_hub_.RegisterShortcut(u"Delete", KeyCode::Delete, [this] { + if (!IsEnabled()) return false; + if (!IsEditable()) return false; + const auto selection = GetSelection(); + if (selection.count == 0) { + DeleteChar(GetCaretPosition()); + } else { + this->DeleteSelectedText(); + } + return true; + }); + + shortcut_hub_.RegisterShortcut(u"Left", KeyCode::Left, [this] { + auto text = this->GetTextView(); + const auto caret = this->GetCaretPosition(); + gsl::index new_position; + Utf16PreviousCodePoint(text, caret, &new_position); + this->SetSelection(new_position); + return true; + }); + + shortcut_hub_.RegisterShortcut( + u"ShiftLeft", {KeyCode::Left, KeyModifiers::shift}, [this] { + auto text = this->GetTextView(); + auto selection = this->GetSelection(); + gsl::index new_position; + Utf16PreviousCodePoint(text, selection.GetEnd(), &new_position); + selection.ChangeEnd(new_position); + this->SetSelection(selection); + return true; + }); + + shortcut_hub_.RegisterShortcut(u"Right", KeyCode::Right, [this] { + auto text = this->GetTextView(); + const auto caret = this->GetCaretPosition(); + gsl::index new_position; + Utf16NextCodePoint(text, caret, &new_position); + this->SetSelection(new_position); + return true; + }); + + shortcut_hub_.RegisterShortcut( + u"ShiftRight", {KeyCode::Right, KeyModifiers::shift}, [this] { + auto text = this->GetTextView(); + auto selection = this->GetSelection(); + gsl::index new_position; + Utf16NextCodePoint(text, selection.GetEnd(), &new_position); + selection.ChangeEnd(new_position); + this->SetSelection(selection); + return true; + }); +} } // namespace cru::ui::controls diff --git a/src/ui/helper/ShortcutHub.cpp b/src/ui/helper/ShortcutHub.cpp index 823072f2..f35ad0ef 100644 --- a/src/ui/helper/ShortcutHub.cpp +++ b/src/ui/helper/ShortcutHub.cpp @@ -85,6 +85,8 @@ void ShortcutHub::OnKeyDown(event::KeyEventArgs& event) { ShortcutKeyBind key_bind(event.GetKeyCode(), event.GetKeyModifier()); const auto& shortcut_list = this->GetShortcutByKeyBind(key_bind); + bool handled = false; + if constexpr (debug_flags::shortcut) { if (shortcut_list.empty()) { log::Debug(u"No shortcut for key bind {}.", key_bind.ToString()); @@ -100,12 +102,13 @@ void ShortcutHub::OnKeyDown(event::KeyEventArgs& event) { log::Debug(u"Handle {} handled it.", shortcut.name); } + handled = true; event.SetHandled(); break; } else { if constexpr (debug_flags::shortcut) { - log::Debug(u"Handle {} disdn't handle it.", shortcut.name); + log::Debug(u"Handle {} didn't handle it.", shortcut.name); } } } @@ -116,5 +119,13 @@ void ShortcutHub::OnKeyDown(event::KeyEventArgs& event) { key_bind.ToString()); } } + + if (!handled) { + if constexpr (debug_flags::shortcut) { + log::Debug(u"Raise fallback event for unhandled shortcut of key bind {}.", + key_bind.ToString()); + } + fallback_event_.Raise(event); + } } } // namespace cru::ui::helper |