aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls/TextHostControlService.cpp
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-10-17 20:50:53 +0800
committerYuqian Yang <crupest@crupest.life>2025-10-17 20:50:53 +0800
commit045462a6aed2796976a2f5cf0042f9a0ac1493f7 (patch)
treee1df5a7ad35192772a63902cee65871447976706 /src/ui/controls/TextHostControlService.cpp
parent9f419314b646bf57dfc3fcbb509b3be2c974e3fd (diff)
downloadcru-045462a6aed2796976a2f5cf0042f9a0ac1493f7.tar.gz
cru-045462a6aed2796976a2f5cf0042f9a0ac1493f7.tar.bz2
cru-045462a6aed2796976a2f5cf0042f9a0ac1493f7.zip
Implement Utf8/16 funcs.
Diffstat (limited to 'src/ui/controls/TextHostControlService.cpp')
-rw-r--r--src/ui/controls/TextHostControlService.cpp39
1 files changed, 23 insertions, 16 deletions
diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp
index bb723e3f..3c38c454 100644
--- a/src/ui/controls/TextHostControlService.cpp
+++ b/src/ui/controls/TextHostControlService.cpp
@@ -5,8 +5,6 @@
#include "cru/base/Base.h"
#include "cru/base/StringUtil.h"
#include "cru/base/log/Logger.h"
-#include "cru/platform/graphics/Font.h"
-#include "cru/platform/gui/Base.h"
#include "cru/platform/gui/Clipboard.h"
#include "cru/platform/gui/Cursor.h"
#include "cru/platform/gui/InputMethod.h"
@@ -16,7 +14,6 @@
#include "cru/ui/DebugFlags.h"
#include "cru/ui/DeleteLater.h"
#include "cru/ui/components/Menu.h"
-#include "cru/ui/events/UiEvents.h"
#include "cru/ui/helper/ShortcutHub.h"
#include "cru/ui/host/WindowHost.h"
#include "cru/ui/render/ScrollRenderObject.h"
@@ -25,12 +22,15 @@
#include <memory>
namespace cru::ui::controls {
+using namespace cru::string;
+
TextControlMovePattern TextControlMovePattern::kLeft(
"Left", helper::ShortcutKeyBind(platform::gui::KeyCode::Left),
[](TextHostControlService* service, std::string_view text,
Index current_position) {
CRU_UNUSED(service)
- Utf8PreviousCodePoint(text, current_position, &current_position);
+ Utf8PreviousCodePoint(text.data(), text.size(), current_position,
+ &current_position);
return current_position;
});
TextControlMovePattern TextControlMovePattern::kRight(
@@ -38,7 +38,8 @@ TextControlMovePattern TextControlMovePattern::kRight(
[](TextHostControlService* service, std::string_view text,
Index current_position) {
CRU_UNUSED(service)
- Utf8NextCodePoint(text, current_position, &current_position);
+ Utf8NextCodePoint(text.data(), text.size(), current_position,
+ &current_position);
return current_position;
});
TextControlMovePattern TextControlMovePattern::kCtrlLeft(
@@ -48,7 +49,7 @@ TextControlMovePattern TextControlMovePattern::kCtrlLeft(
[](TextHostControlService* service, std::string_view text,
Index current_position) {
CRU_UNUSED(service)
- return Utf8PreviousWord(text, current_position);
+ return Utf8PreviousWord(text.data(), text.size(), current_position);
});
TextControlMovePattern TextControlMovePattern::kCtrlRight(
"Ctrl+Right(Next Word)",
@@ -57,7 +58,7 @@ TextControlMovePattern TextControlMovePattern::kCtrlRight(
[](TextHostControlService* service, std::string_view text,
Index current_position) {
CRU_UNUSED(service)
- return Utf8NextWord(text, current_position);
+ return Utf8NextWord(text.data(), text.size(), current_position);
});
TextControlMovePattern TextControlMovePattern::kUp(
"Up", helper::ShortcutKeyBind(platform::gui::KeyCode::Up),
@@ -86,7 +87,7 @@ TextControlMovePattern TextControlMovePattern::kHome(
[](TextHostControlService* service, std::string_view text,
Index current_position) {
CRU_UNUSED(service)
- return Utf8BackwardUntil(text, current_position,
+ return Utf8BackwardUntil(text.data(), text.size(), current_position,
[](CodePoint c) { return c == u'\n'; });
});
TextControlMovePattern TextControlMovePattern::kEnd(
@@ -94,7 +95,7 @@ TextControlMovePattern TextControlMovePattern::kEnd(
[](TextHostControlService* service, std::string_view text,
Index current_position) {
CRU_UNUSED(service)
- return Utf8ForwardUntil(text, current_position,
+ return Utf8ForwardUntil(text.data(), text.size(), current_position,
[](CodePoint c) { return c == u'\n'; });
});
TextControlMovePattern TextControlMovePattern::kCtrlHome(
@@ -225,7 +226,8 @@ void TextHostControlService::SetText(std::string text, bool stop_composition) {
void TextHostControlService::InsertText(Index position, std::string_view text,
bool stop_composition) {
- if (!Utf8IsValidInsertPosition(this->text_, position)) {
+ if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(),
+ position)) {
CRU_LOG_TAG_ERROR("Invalid text insert position.");
return;
}
@@ -239,26 +241,29 @@ void TextHostControlService::InsertText(Index position, std::string_view text,
}
void TextHostControlService::DeleteChar(Index position, bool stop_composition) {
- if (!Utf8IsValidInsertPosition(this->text_, position)) {
+ if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(),
+ position)) {
CRU_LOG_TAG_ERROR("Invalid text delete position.");
return;
}
if (position == static_cast<Index>(this->text_.size())) return;
Index next;
- Utf8NextCodePoint(this->text_, position, &next);
+ Utf8NextCodePoint(this->text_.data(), this->text_.size(), position, &next);
this->DeleteText(TextRange::FromTwoSides(position, next), stop_composition);
}
// Return the position of deleted character.
Index TextHostControlService::DeleteCharPrevious(Index position,
bool stop_composition) {
- if (!Utf8IsValidInsertPosition(this->text_, position)) {
+ if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(),
+ position)) {
CRU_LOG_TAG_ERROR("Invalid text delete position.");
return 0;
}
if (position == 0) return 0;
Index previous;
- Utf8PreviousCodePoint(this->text_, position, &previous);
+ Utf8PreviousCodePoint(this->text_.data(), this->text_.size(), position,
+ &previous);
this->DeleteText(TextRange::FromTwoSides(previous, position),
stop_composition);
return previous;
@@ -268,11 +273,13 @@ void TextHostControlService::DeleteText(TextRange range,
bool stop_composition) {
if (range.count == 0) return;
range = range.Normalize();
- if (!Utf8IsValidInsertPosition(this->text_, range.GetStart())) {
+ if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(),
+ range.GetStart())) {
CRU_LOG_TAG_ERROR("Invalid text delete start position.");
return;
}
- if (!Utf8IsValidInsertPosition(this->text_, range.GetStart())) {
+ if (!Utf8IsValidInsertPosition(this->text_.data(), this->text_.size(),
+ range.GetStart())) {
CRU_LOG_TAG_ERROR("Invalid text delete end position.");
return;
}