Commit 8ca53768 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Wayland IME: Fix caret position on preedit string.

Currently, there are two issues.
- Delegate method is called after resetting preedit cursor
  so the passed value is always -1.
- According to the protocol definition, it is the byte-offset,
  but we need to convert it to the string16's offset.

Bug: 1140536
Test: Ran manually with Lacros.
Change-Id: I06b018b7e945208f9f1f166ecf0dcf36d882a2c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2486010Reviewed-by: default avatarRobert Kroeger <rjkroege@chromium.org>
Reviewed-by: default avatarJun Mukai <mukai@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821180}
parent a6ea10e5
...@@ -26,6 +26,13 @@ ...@@ -26,6 +26,13 @@
#endif #endif
namespace ui { namespace ui {
namespace {
size_t OffsetFromUTF8Offset(const base::StringPiece& text, uint32_t offset) {
return base::UTF8ToUTF16(text.substr(0, offset)).size();
}
} // namespace
WaylandInputMethodContext::WaylandInputMethodContext( WaylandInputMethodContext::WaylandInputMethodContext(
WaylandConnection* connection, WaylandConnection* connection,
...@@ -126,18 +133,13 @@ void WaylandInputMethodContext::SetSurroundingText( ...@@ -126,18 +133,13 @@ void WaylandInputMethodContext::SetSurroundingText(
} }
void WaylandInputMethodContext::OnPreeditString(const std::string& text, void WaylandInputMethodContext::OnPreeditString(const std::string& text,
int preedit_cursor) { int32_t preedit_cursor) {
gfx::Range selection_range = gfx::Range::InvalidRange();
if (!selection_range.IsValid()) {
int cursor_pos = (preedit_cursor) ? text.length() : preedit_cursor;
selection_range.set_start(cursor_pos);
selection_range.set_end(cursor_pos);
}
ui::CompositionText composition_text; ui::CompositionText composition_text;
composition_text.text = base::UTF8ToUTF16(text); composition_text.text = base::UTF8ToUTF16(text);
composition_text.selection = selection_range; composition_text.selection =
(preedit_cursor >= 0) ? gfx::Range(OffsetFromUTF8Offset(
text, static_cast<uint32_t>(preedit_cursor)))
: gfx::Range::InvalidRange();
ime_delegate_->OnPreeditChanged(composition_text); ime_delegate_->OnPreeditChanged(composition_text);
} }
......
...@@ -42,7 +42,8 @@ class WaylandInputMethodContext : public LinuxInputMethodContext, ...@@ -42,7 +42,8 @@ class WaylandInputMethodContext : public LinuxInputMethodContext,
void Blur() override; void Blur() override;
// ui::ZWPTextInputWrapperClient // ui::ZWPTextInputWrapperClient
void OnPreeditString(const std::string& text, int preedit_cursor) override; void OnPreeditString(const std::string& text,
int32_t preedit_cursor) override;
void OnCommitString(const std::string& text) override; void OnCommitString(const std::string& text) override;
void OnDeleteSurroundingText(int32_t index, uint32_t length) override; void OnDeleteSurroundingText(int32_t index, uint32_t length) override;
void OnKeysym(uint32_t keysym, uint32_t state, uint32_t modifiers) override; void OnKeysym(uint32_t keysym, uint32_t state, uint32_t modifiers) override;
......
...@@ -27,6 +27,7 @@ class ZWPTextInputWrapperClient { ...@@ -27,6 +27,7 @@ class ZWPTextInputWrapperClient {
// Called when a new composing text (pre-edit) should be set around the // Called when a new composing text (pre-edit) should be set around the
// current cursor position. Any previously set composing text should // current cursor position. Any previously set composing text should
// be removed. // be removed.
// Note that the preedit_cursor is byte-offset.
virtual void OnPreeditString(const std::string& text, virtual void OnPreeditString(const std::string& text,
int32_t preedit_cursor) = 0; int32_t preedit_cursor) = 0;
......
...@@ -121,8 +121,9 @@ void ZWPTextInputWrapperV1::OnPreeditString( ...@@ -121,8 +121,9 @@ void ZWPTextInputWrapperV1::OnPreeditString(
const char* text, const char* text,
const char* commit) { const char* commit) {
ZWPTextInputWrapperV1* wti = static_cast<ZWPTextInputWrapperV1*>(data); ZWPTextInputWrapperV1* wti = static_cast<ZWPTextInputWrapperV1*>(data);
int32_t preedit_cursor = wti->preedit_cursor_;
wti->ResetInputEventState(); wti->ResetInputEventState();
wti->client_->OnPreeditString(std::string(text), wti->preedit_cursor_); wti->client_->OnPreeditString(std::string(text), preedit_cursor);
} }
void ZWPTextInputWrapperV1::OnPreeditStyling( void ZWPTextInputWrapperV1::OnPreeditStyling(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment