diff options
author | crupest <crupest@outlook.com> | 2021-11-19 15:57:49 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-11-19 15:57:49 +0800 |
commit | 2ad748a0fcb66398299886970cc9c40cc50b3cd0 (patch) | |
tree | f89f283c771c66cdf79a7e54bebb05fc5f53ee18 /src/ui/controls | |
parent | 814be0155a567328f95c3e1a2b7becf4f0b5b977 (diff) | |
download | cru-2ad748a0fcb66398299886970cc9c40cc50b3cd0.tar.gz cru-2ad748a0fcb66398299886970cc9c40cc50b3cd0.tar.bz2 cru-2ad748a0fcb66398299886970cc9c40cc50b3cd0.zip |
...
Diffstat (limited to 'src/ui/controls')
-rw-r--r-- | src/ui/controls/TextHostControlService.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index edfabb94..46c02f50 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -6,6 +6,7 @@ #include "cru/common/StringUtil.hpp" #include "cru/platform/graphics/Font.hpp" #include "cru/platform/gui/Base.hpp" +#include "cru/platform/gui/Clipboard.hpp" #include "cru/platform/gui/Cursor.hpp" #include "cru/platform/gui/InputMethod.hpp" #include "cru/platform/gui/Keyboard.hpp" @@ -304,6 +305,11 @@ render::ScrollRenderObject* TextHostControlService::GetScrollRenderObject() { return this->text_host_control_->GetScrollRenderObject(); } +StringView TextHostControlService::GetSelectedText() { + auto selection = this->GetSelection().Normalize(); + return GetTextView().substr(selection.position, selection.count); +} + void TextHostControlService::SetSelection(gsl::index caret_position) { this->SetSelection(TextRange{caret_position, 0}); } @@ -347,6 +353,25 @@ void TextHostControlService::DeleteSelectedText() { SetSelection(GetSelection().Normalize().GetStart()); } +void TextHostControlService::Cut() { + Copy(); + ReplaceSelectedText(StringView{}); +} + +void TextHostControlService::Copy() { + auto selected_text = GetSelectedText(); + if (selected_text.size() == 0) return; + auto clipboard = GetUiApplication()->GetClipboard(); + clipboard->SetText(selected_text.ToString()); +} + +void TextHostControlService::Paste() { + auto clipboard = GetUiApplication()->GetClipboard(); + auto text = clipboard->GetText(); + if (text.empty()) return; + ReplaceSelectedText(text); +} + void TextHostControlService::SetupCaret() { const auto application = GetUiApplication(); this->GetTextRenderObject()->SetDrawCaret(true); @@ -497,6 +522,33 @@ void TextHostControlService::SetUpShortcuts() { return false; }); + shortcut_hub_.RegisterShortcut(u"Cut", {KeyCode::X, kKeyModifierCommand}, + [this] { + if (IsEnabled() && IsEditable()) { + this->Cut(); + return true; + } + return false; + }); + + shortcut_hub_.RegisterShortcut(u"Copy", {KeyCode::C, kKeyModifierCommand}, + [this] { + if (IsEnabled()) { + this->Copy(); + return true; + } + return false; + }); + + shortcut_hub_.RegisterShortcut(u"Paste", {KeyCode::V, kKeyModifierCommand}, + [this] { + if (IsEnabled() && IsEditable()) { + this->Paste(); + return true; + } + return false; + }); + shortcut_hub_.RegisterShortcut(u"Backspace", KeyCode::Backspace, [this] { if (!IsEnabled()) return false; if (!IsEditable()) return false; |