Commit b5981c12 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Lacros IME: apply preedit styles.

Bug: 1140537
Test: Ran Lacros with enabling IME locally with JP IME.
Change-Id: Ie4c4533f3352fdfbfb57047bde7176047c311ba9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2487740
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarJun Mukai <mukai@chromium.org>
Reviewed-by: default avatarRobert Kroeger <rjkroege@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821624}
parent f3c96432
...@@ -6,8 +6,10 @@ ...@@ -6,8 +6,10 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/notreached.h" #include "base/notreached.h"
#include "base/optional.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "ui/base/ime/composition_text.h" #include "ui/base/ime/composition_text.h"
...@@ -28,8 +30,16 @@ ...@@ -28,8 +30,16 @@
namespace ui { namespace ui {
namespace { namespace {
size_t OffsetFromUTF8Offset(const base::StringPiece& text, uint32_t offset) { base::Optional<size_t> OffsetFromUTF8Offset(const base::StringPiece& text,
return base::UTF8ToUTF16(text.substr(0, offset)).size(); uint32_t offset) {
if (offset > text.length())
return base::nullopt;
base::string16 converted;
if (!base::UTF8ToUTF16(text.data(), offset, &converted))
return base::nullopt;
return converted.size();
} }
} // namespace } // namespace
...@@ -132,18 +142,67 @@ void WaylandInputMethodContext::SetSurroundingText( ...@@ -132,18 +142,67 @@ void WaylandInputMethodContext::SetSurroundingText(
text_input_->SetSurroundingText(text, selection_range); text_input_->SetSurroundingText(text, selection_range);
} }
void WaylandInputMethodContext::OnPreeditString(const std::string& text, void WaylandInputMethodContext::OnPreeditString(
int32_t preedit_cursor) { base::StringPiece text,
const std::vector<SpanStyle>& spans,
int32_t preedit_cursor) {
ui::CompositionText composition_text; ui::CompositionText composition_text;
composition_text.text = base::UTF8ToUTF16(text); composition_text.text = base::UTF8ToUTF16(text);
composition_text.selection = for (const auto& span : spans) {
(preedit_cursor >= 0) ? gfx::Range(OffsetFromUTF8Offset( ImeTextSpan text_span;
text, static_cast<uint32_t>(preedit_cursor))) auto start_offset = OffsetFromUTF8Offset(text, span.index);
: gfx::Range::InvalidRange(); if (!start_offset)
continue;
text_span.start_offset = *start_offset;
auto end_offset = OffsetFromUTF8Offset(text, span.index + span.length);
if (!end_offset)
continue;
text_span.end_offset = *end_offset;
bool supported = true;
switch (span.style) {
case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_DEFAULT:
break;
case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_HIGHLIGHT:
text_span.thickness = ImeTextSpan::Thickness::kThick;
break;
case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_UNDERLINE:
text_span.thickness = ImeTextSpan::Thickness::kThin;
break;
case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_SELECTION:
text_span.type = ImeTextSpan::Type::kSuggestion;
break;
case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_INCORRECT:
text_span.type = ImeTextSpan::Type::kMisspellingSuggestion;
break;
case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_NONE:
case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_ACTIVE:
case ZWP_TEXT_INPUT_V1_PREEDIT_STYLE_INACTIVE:
default:
VLOG(1) << "Unsupported style. Skipped: " << span.style;
supported = false;
break;
}
if (!supported)
continue;
composition_text.ime_text_spans.push_back(std::move(text_span));
}
if (preedit_cursor < 0) {
composition_text.selection = gfx::Range::InvalidRange();
} else {
auto cursor =
OffsetFromUTF8Offset(text, static_cast<uint32_t>(preedit_cursor));
if (!cursor) {
// Invalid cursor position. Do nothing.
return;
}
composition_text.selection = gfx::Range(*cursor);
}
ime_delegate_->OnPreeditChanged(composition_text); ime_delegate_->OnPreeditChanged(composition_text);
} }
void WaylandInputMethodContext::OnCommitString(const std::string& text) { void WaylandInputMethodContext::OnCommitString(base::StringPiece text) {
ime_delegate_->OnCommit(base::UTF8ToUTF16(text)); ime_delegate_->OnCommit(base::UTF8ToUTF16(text));
} }
......
...@@ -6,9 +6,10 @@ ...@@ -6,9 +6,10 @@
#define UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_INPUT_METHOD_CONTEXT_H_ #define UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_INPUT_METHOD_CONTEXT_H_
#include <memory> #include <memory>
#include <string> #include <vector>
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string_piece.h"
#include "ui/base/ime/character_composer.h" #include "ui/base/ime/character_composer.h"
#include "ui/base/ime/linux/linux_input_method_context.h" #include "ui/base/ime/linux/linux_input_method_context.h"
#include "ui/ozone/platform/wayland/host/wayland_keyboard.h" #include "ui/ozone/platform/wayland/host/wayland_keyboard.h"
...@@ -42,9 +43,10 @@ class WaylandInputMethodContext : public LinuxInputMethodContext, ...@@ -42,9 +43,10 @@ class WaylandInputMethodContext : public LinuxInputMethodContext,
void Blur() override; void Blur() override;
// ui::ZWPTextInputWrapperClient // ui::ZWPTextInputWrapperClient
void OnPreeditString(const std::string& text, void OnPreeditString(base::StringPiece text,
const std::vector<SpanStyle>& spans,
int32_t preedit_cursor) override; int32_t preedit_cursor) override;
void OnCommitString(const std::string& text) override; void OnCommitString(base::StringPiece 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;
......
...@@ -5,9 +5,13 @@ ...@@ -5,9 +5,13 @@
#ifndef UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_WRAPPER_H_ #ifndef UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_WRAPPER_H_
#define UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_WRAPPER_H_ #define UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_WRAPPER_H_
#include "ui/ozone/platform/wayland/common/wayland_object.h" #include <stdint.h>
#include <vector>
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/string_piece.h"
#include "ui/ozone/platform/wayland/common/wayland_object.h"
namespace gfx { namespace gfx {
class Rect; class Rect;
...@@ -22,19 +26,26 @@ class WaylandWindow; ...@@ -22,19 +26,26 @@ class WaylandWindow;
// Client interface which handles wayland text input callbacks // Client interface which handles wayland text input callbacks
class ZWPTextInputWrapperClient { class ZWPTextInputWrapperClient {
public: public:
virtual ~ZWPTextInputWrapperClient() {} struct SpanStyle {
uint32_t index; // Byte offset.
uint32_t length; // Length in bytes.
uint32_t style; // One of preedit_style.
};
virtual ~ZWPTextInputWrapperClient() = default;
// 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. // Note that the preedit_cursor is byte-offset.
virtual void OnPreeditString(const std::string& text, virtual void OnPreeditString(base::StringPiece text,
const std::vector<SpanStyle>& spans,
int32_t preedit_cursor) = 0; int32_t preedit_cursor) = 0;
// Called when a complete input sequence has been entered. The text to // Called when a complete input sequence has been entered. The text to
// commit could be either just a single character after a key press or the // commit could be either just a single character after a key press or the
// result of some composing (pre-edit). // result of some composing (pre-edit).
virtual void OnCommitString(const std::string& text) = 0; virtual void OnCommitString(base::StringPiece text) = 0;
// Called when client needs to delete all or part of the text surrounding // Called when client needs to delete all or part of the text surrounding
// the cursor // the cursor
...@@ -52,7 +63,7 @@ class ZWPTextInputWrapperClient { ...@@ -52,7 +63,7 @@ class ZWPTextInputWrapperClient {
// IME. This interface collects the functionality behind one wrapper API. // IME. This interface collects the functionality behind one wrapper API.
class ZWPTextInputWrapper { class ZWPTextInputWrapper {
public: public:
virtual ~ZWPTextInputWrapper() {} virtual ~ZWPTextInputWrapper() = default;
virtual void Initialize(WaylandConnection* connection, virtual void Initialize(WaylandConnection* connection,
ZWPTextInputWrapperClient* client) = 0; ZWPTextInputWrapperClient* client) = 0;
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include "ui/ozone/platform/wayland/host/zwp_text_input_wrapper_v1.h" #include "ui/ozone/platform/wayland/host/zwp_text_input_wrapper_v1.h"
#include <string>
#include <utility>
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
...@@ -33,7 +36,6 @@ ZWPTextInputWrapperV1::ZWPTextInputWrapperV1( ...@@ -33,7 +36,6 @@ ZWPTextInputWrapperV1::ZWPTextInputWrapperV1(
&ZWPTextInputWrapperV1::OnLanguage, // text_input_language, &ZWPTextInputWrapperV1::OnLanguage, // text_input_language,
&ZWPTextInputWrapperV1::OnTextDirection, // text_input_text_direction &ZWPTextInputWrapperV1::OnTextDirection, // text_input_text_direction
}; };
ResetInputEventState();
zwp_text_input_v1* text_input = zwp_text_input_v1* text_input =
zwp_text_input_manager_v1_create_text_input(text_input_manager); zwp_text_input_manager_v1_create_text_input(text_input_manager);
...@@ -87,6 +89,7 @@ void ZWPTextInputWrapperV1::SetSurroundingText( ...@@ -87,6 +89,7 @@ void ZWPTextInputWrapperV1::SetSurroundingText(
} }
void ZWPTextInputWrapperV1::ResetInputEventState() { void ZWPTextInputWrapperV1::ResetInputEventState() {
spans_.clear();
preedit_cursor_ = -1; preedit_cursor_ = -1;
} }
...@@ -121,9 +124,10 @@ void ZWPTextInputWrapperV1::OnPreeditString( ...@@ -121,9 +124,10 @@ 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);
auto spans = std::move(wti->spans_);
int32_t preedit_cursor = wti->preedit_cursor_; int32_t preedit_cursor = wti->preedit_cursor_;
wti->ResetInputEventState(); wti->ResetInputEventState();
wti->client_->OnPreeditString(std::string(text), preedit_cursor); wti->client_->OnPreeditString(text, spans, preedit_cursor);
} }
void ZWPTextInputWrapperV1::OnPreeditStyling( void ZWPTextInputWrapperV1::OnPreeditStyling(
...@@ -132,7 +136,9 @@ void ZWPTextInputWrapperV1::OnPreeditStyling( ...@@ -132,7 +136,9 @@ void ZWPTextInputWrapperV1::OnPreeditStyling(
uint32_t index, uint32_t index,
uint32_t length, uint32_t length,
uint32_t style) { uint32_t style) {
NOTIMPLEMENTED_LOG_ONCE(); ZWPTextInputWrapperV1* wti = static_cast<ZWPTextInputWrapperV1*>(data);
wti->spans_.push_back(
ZWPTextInputWrapperClient::SpanStyle{index, length, style});
} }
void ZWPTextInputWrapperV1::OnPreeditCursor( void ZWPTextInputWrapperV1::OnPreeditCursor(
...@@ -149,7 +155,7 @@ void ZWPTextInputWrapperV1::OnCommitString(void* data, ...@@ -149,7 +155,7 @@ void ZWPTextInputWrapperV1::OnCommitString(void* data,
const char* text) { const char* text) {
ZWPTextInputWrapperV1* wti = static_cast<ZWPTextInputWrapperV1*>(data); ZWPTextInputWrapperV1* wti = static_cast<ZWPTextInputWrapperV1*>(data);
wti->ResetInputEventState(); wti->ResetInputEventState();
wti->client_->OnCommitString(std::string(text)); wti->client_->OnCommitString(text);
} }
void ZWPTextInputWrapperV1::OnCursorPosition( void ZWPTextInputWrapperV1::OnCursorPosition(
......
...@@ -5,8 +5,11 @@ ...@@ -5,8 +5,11 @@
#ifndef UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_WRAPPER_V1_H_ #ifndef UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_WRAPPER_V1_H_
#define UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_WRAPPER_V1_H_ #define UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_WRAPPER_V1_H_
#include <stdint.h>
#include <vector>
#include <text-input-unstable-v1-client-protocol.h> #include <text-input-unstable-v1-client-protocol.h>
#include <string>
#include "ui/ozone/platform/wayland/host/zwp_text_input_wrapper.h" #include "ui/ozone/platform/wayland/host/zwp_text_input_wrapper.h"
...@@ -98,7 +101,8 @@ class ZWPTextInputWrapperV1 : public ZWPTextInputWrapper { ...@@ -98,7 +101,8 @@ class ZWPTextInputWrapperV1 : public ZWPTextInputWrapper {
wl::Object<zwp_text_input_v1> obj_; wl::Object<zwp_text_input_v1> obj_;
ZWPTextInputWrapperClient* client_; ZWPTextInputWrapperClient* client_;
int32_t preedit_cursor_; std::vector<ZWPTextInputWrapperClient::SpanStyle> spans_;
int32_t preedit_cursor_ = -1;
}; };
} // namespace ui } // namespace ui
......
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