aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-12-26 19:39:24 +0800
committercrupest <crupest@outlook.com>2020-12-26 19:39:24 +0800
commit715be3c81b96fcf87c7650501d71480a8743a984 (patch)
treef56119db325f57280ab625cf1927b846e3eebdcb /src
parent61b835d47ab98279c5f2f43bebcd4fe18f7c408d (diff)
downloadcru-715be3c81b96fcf87c7650501d71480a8743a984.tar.gz
cru-715be3c81b96fcf87c7650501d71480a8743a984.tar.bz2
cru-715be3c81b96fcf87c7650501d71480a8743a984.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/ui/controls/TextHostControlService.cpp242
-rw-r--r--src/ui/helper/ShortcutHub.cpp13
2 files changed, 129 insertions, 126 deletions
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