From 864b031211322dc276b220ec0a6e11483503a0e9 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Oct 2020 17:44:06 +0800 Subject: ... --- src/common/StringUtil.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/common/StringUtil.cpp') diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp index fc6d6349..3c312d49 100644 --- a/src/common/StringUtil.cpp +++ b/src/common/StringUtil.cpp @@ -1,4 +1,5 @@ #include "cru/common/StringUtil.hpp" +#include "gsl/gsl_util" namespace cru { namespace { @@ -191,8 +192,8 @@ void Utf8EncodeCodePointAppend(CodePoint code_point, std::string& str) { } void Utf16EncodeCodePointAppend(CodePoint code_point, std::u16string& str) { - if (code_point >= 0 && code_point <= 0xD7FF || - code_point >= 0xE000 && code_point <= 0xFFFF) { + if ((code_point >= 0 && code_point <= 0xD7FF) || + (code_point >= 0xE000 && code_point <= 0xFFFF)) { str.push_back(static_cast(code_point)); } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { std::uint32_t u = code_point - 0x10000; @@ -220,4 +221,12 @@ std::u16string ToUtf16(std::string_view s) { } return result; } + +bool Utf16IsValidInsertPosition(std::u16string_view s, gsl::index position) { + if (position < 0) return false; + if (position > static_cast(s.size())) return false; + if (position == 0) return true; + if (position == static_cast(s.size())) return true; + return !IsUtf16SurrogatePairTrailing(s[position]); +} } // namespace cru -- cgit v1.2.3 From 6523ba8e0f2830f3cd4434d8331882077be3f82c Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 26 Dec 2020 23:41:02 +0800 Subject: ... --- include/cru/common/StringUtil.hpp | 19 ++++++++++- src/common/StringUtil.cpp | 55 ++++++++++++++++++++++++++++++ src/ui/controls/TextHostControlService.cpp | 16 +++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) (limited to 'src/common/StringUtil.cpp') diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp index 276048f5..62999d53 100644 --- a/include/cru/common/StringUtil.hpp +++ b/include/cru/common/StringUtil.hpp @@ -1,7 +1,10 @@ #pragma once -#include #include "Base.hpp" +#include +#include +#include + namespace cru { using CodePoint = std::int32_t; constexpr CodePoint k_invalid_code_point = -1; @@ -128,4 +131,18 @@ std::u16string ToUtf16(std::string_view s); // If given s is not a valid utf16 string, return value is UD. bool Utf16IsValidInsertPosition(std::u16string_view s, gsl::index position); + +// Return position after the character making predicate returns true or 0 if no +// character doing so. +gsl::index Utf16BackwardUntil(std::u16string_view str, gsl::index position, + const std::function& predicate); +// Return position before the character making predicate returns true or +// str.size() if no character doing so. +gsl::index Utf16ForwardUntil(std::u16string_view str, gsl::index position, + const std::function& predicate); + +gsl::index Utf16PreviousWord(std::u16string_view str, gsl::index position, + bool* is_space = nullptr); +gsl::index Utf16NextWord(std::u16string_view str, gsl::index position, + bool* is_space = nullptr); } // namespace cru diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp index 3c312d49..b13c0193 100644 --- a/src/common/StringUtil.cpp +++ b/src/common/StringUtil.cpp @@ -1,4 +1,5 @@ #include "cru/common/StringUtil.hpp" +#include "cru/common/Base.hpp" #include "gsl/gsl_util" namespace cru { @@ -229,4 +230,58 @@ bool Utf16IsValidInsertPosition(std::u16string_view s, gsl::index position) { if (position == static_cast(s.size())) return true; return !IsUtf16SurrogatePairTrailing(s[position]); } + +gsl::index Utf16BackwardUntil(std::u16string_view str, gsl::index position, + const std::function& predicate) { + if (position <= 0) return position; + while (true) { + gsl::index p = position; + auto c = Utf16PreviousCodePoint(str, p, &position); + if (predicate(c)) return p; + if (c == k_invalid_code_point) return p; + } + UnreachableCode(); +} + +gsl::index Utf16ForwardUntil(std::u16string_view str, gsl::index position, + const std::function& predicate) { + if (position >= static_cast(str.size())) return position; + while (true) { + gsl::index p = position; + auto c = Utf16NextCodePoint(str, p, &position); + if (predicate(c)) return p; + if (c == k_invalid_code_point) return p; + } + UnreachableCode(); +} + +inline bool IsSpace(CodePoint c) { return c == 0x20; } + +gsl::index Utf16PreviousWord(std::u16string_view str, gsl::index position, + bool* is_space) { + if (position <= 0) return position; + auto c = Utf16PreviousCodePoint(str, position, nullptr); + if (IsSpace(c)) { // TODO: Currently only test against 0x20(space). + if (is_space) *is_space = true; + return Utf16BackwardUntil(str, position, + [](CodePoint c) { return !IsSpace(c); }); + } else { + if (is_space) *is_space = false; + return Utf16BackwardUntil(str, position, IsSpace); + } +} + +gsl::index Utf16NextWord(std::u16string_view str, gsl::index position, + bool* is_space) { + if (position >= static_cast(str.size())) return position; + auto c = Utf16NextCodePoint(str, position, nullptr); + if (IsSpace(c)) { // TODO: Currently only test against 0x20(space). + if (is_space) *is_space = true; + return Utf16ForwardUntil(str, position, + [](CodePoint c) { return !IsSpace(c); }); + } else { + if (is_space) *is_space = false; + return Utf16ForwardUntil(str, position, IsSpace); + } +} } // namespace cru diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index 91ec53ff..4bfd6715 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -419,6 +419,14 @@ void TextHostControlService::SetUpShortcuts() { return true; }); + shortcut_hub_.RegisterShortcut( + u"CtrlLeft", {KeyCode::Left, KeyModifiers::ctrl}, [this] { + auto text = this->GetTextView(); + auto caret = this->GetCaretPosition(); + this->SetSelection(Utf16PreviousWord(text, caret)); + return true; + }); + shortcut_hub_.RegisterShortcut(u"Right", KeyCode::Right, [this] { auto text = this->GetTextView(); const auto caret = this->GetCaretPosition(); @@ -438,5 +446,13 @@ void TextHostControlService::SetUpShortcuts() { this->SetSelection(selection); return true; }); + + shortcut_hub_.RegisterShortcut( + u"CtrlRight", {KeyCode::Right, KeyModifiers::ctrl}, [this] { + auto text = this->GetTextView(); + auto caret = this->GetCaretPosition(); + this->SetSelection(Utf16NextWord(text, caret)); + return true; + }); } } // namespace cru::ui::controls -- cgit v1.2.3 From 7b6ca6e8a158868da316351b64e64ab1cdb5872c Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 28 Dec 2020 23:05:06 +0800 Subject: ... --- src/common/StringUtil.cpp | 2 +- src/ui/controls/TextHostControlService.cpp | 1 - src/win/gui/InputMethod.cpp | 10 +++++++--- 3 files changed, 8 insertions(+), 5 deletions(-) (limited to 'src/common/StringUtil.cpp') diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp index b13c0193..0cadc545 100644 --- a/src/common/StringUtil.cpp +++ b/src/common/StringUtil.cpp @@ -255,7 +255,7 @@ gsl::index Utf16ForwardUntil(std::u16string_view str, gsl::index position, UnreachableCode(); } -inline bool IsSpace(CodePoint c) { return c == 0x20; } +inline bool IsSpace(CodePoint c) { return c == 0x20 || c == 0xA; } gsl::index Utf16PreviousWord(std::u16string_view str, gsl::index position, bool* is_space) { diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index 502f63f9..07b4f1e8 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -346,7 +346,6 @@ void TextHostControlService::GainFocusHandler( input_method_context_event_guard_ += input_method_context->TextEvent()->AddHandler( [this](const std::u16string_view& text) { - if (text == u"\b") return; this->ReplaceSelectedText(text); }); diff --git a/src/win/gui/InputMethod.cpp b/src/win/gui/InputMethod.cpp index 6643ea58..cc237e88 100644 --- a/src/win/gui/InputMethod.cpp +++ b/src/win/gui/InputMethod.cpp @@ -226,7 +226,7 @@ void WinInputMethodContext::OnWindowNativeMessage( const auto& message = args.GetWindowMessage(); switch (message.msg) { case WM_CHAR: { - const auto c = static_cast(message.w_param); + auto c = static_cast(message.w_param); if (IsUtf16SurrogatePairCodeUnit(c)) { // I don't think this will happen because normal key strike without ime // should only trigger ascci character. If it is a charater from @@ -235,8 +235,12 @@ void WinInputMethodContext::OnWindowNativeMessage( u"A WM_CHAR message for character from supplementary " u"planes is ignored."); } else { - char16_t s[1] = {c}; - text_event_.Raise({s, 1}); + if (c != '\b') { // ignore backspace + if (c == '\r') c = '\n'; // Change \r to \n + + char16_t s[1] = {c}; + text_event_.Raise({s, 1}); + } } args.HandleWithResult(0); break; -- cgit v1.2.3