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 @@
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/optional.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/ime/composition_text.h"
......@@ -28,8 +30,16 @@
namespace ui {
namespace {
size_t OffsetFromUTF8Offset(const base::StringPiece& text, uint32_t offset) {
return base::UTF8ToUTF16(text.substr(0, offset)).size();
base::Optional<size_t> OffsetFromUTF8Offset(const base::StringPiece& text,
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
......@@ -132,18 +142,67 @@ void WaylandInputMethodContext::SetSurroundingText(
text_input_->SetSurroundingText(text, selection_range);
}
void WaylandInputMethodContext::OnPreeditString(const std::string& text,
void WaylandInputMethodContext::OnPreeditString(
base::StringPiece text,
const std::vector<SpanStyle>& spans,
int32_t preedit_cursor) {
ui::CompositionText composition_text;
composition_text.text = base::UTF8ToUTF16(text);
composition_text.selection =
(preedit_cursor >= 0) ? gfx::Range(OffsetFromUTF8Offset(
text, static_cast<uint32_t>(preedit_cursor)))
: gfx::Range::InvalidRange();
for (const auto& span : spans) {
ImeTextSpan text_span;
auto start_offset = OffsetFromUTF8Offset(text, span.index);
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);
}
void WaylandInputMethodContext::OnCommitString(const std::string& text) {
void WaylandInputMethodContext::OnCommitString(base::StringPiece text) {
ime_delegate_->OnCommit(base::UTF8ToUTF16(text));
}
......
......@@ -6,9 +6,10 @@
#define UI_OZONE_PLATFORM_WAYLAND_HOST_WAYLAND_INPUT_METHOD_CONTEXT_H_
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/strings/string_piece.h"
#include "ui/base/ime/character_composer.h"
#include "ui/base/ime/linux/linux_input_method_context.h"
#include "ui/ozone/platform/wayland/host/wayland_keyboard.h"
......@@ -42,9 +43,10 @@ class WaylandInputMethodContext : public LinuxInputMethodContext,
void Blur() override;
// ui::ZWPTextInputWrapperClient
void OnPreeditString(const std::string& text,
void OnPreeditString(base::StringPiece text,
const std::vector<SpanStyle>& spans,
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 OnKeysym(uint32_t keysym, uint32_t state, uint32_t modifiers) override;
......
......@@ -5,9 +5,13 @@
#ifndef 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/string_piece.h"
#include "ui/ozone/platform/wayland/common/wayland_object.h"
namespace gfx {
class Rect;
......@@ -22,19 +26,26 @@ class WaylandWindow;
// Client interface which handles wayland text input callbacks
class ZWPTextInputWrapperClient {
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
// current cursor position. Any previously set composing text should
// be removed.
// 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;
// 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
// 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
// the cursor
......@@ -52,7 +63,7 @@ class ZWPTextInputWrapperClient {
// IME. This interface collects the functionality behind one wrapper API.
class ZWPTextInputWrapper {
public:
virtual ~ZWPTextInputWrapper() {}
virtual ~ZWPTextInputWrapper() = default;
virtual void Initialize(WaylandConnection* connection,
ZWPTextInputWrapperClient* client) = 0;
......
......@@ -4,6 +4,9 @@
#include "ui/ozone/platform/wayland/host/zwp_text_input_wrapper_v1.h"
#include <string>
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
......@@ -33,7 +36,6 @@ ZWPTextInputWrapperV1::ZWPTextInputWrapperV1(
&ZWPTextInputWrapperV1::OnLanguage, // text_input_language,
&ZWPTextInputWrapperV1::OnTextDirection, // text_input_text_direction
};
ResetInputEventState();
zwp_text_input_v1* text_input =
zwp_text_input_manager_v1_create_text_input(text_input_manager);
......@@ -87,6 +89,7 @@ void ZWPTextInputWrapperV1::SetSurroundingText(
}
void ZWPTextInputWrapperV1::ResetInputEventState() {
spans_.clear();
preedit_cursor_ = -1;
}
......@@ -121,9 +124,10 @@ void ZWPTextInputWrapperV1::OnPreeditString(
const char* text,
const char* commit) {
ZWPTextInputWrapperV1* wti = static_cast<ZWPTextInputWrapperV1*>(data);
auto spans = std::move(wti->spans_);
int32_t preedit_cursor = wti->preedit_cursor_;
wti->ResetInputEventState();
wti->client_->OnPreeditString(std::string(text), preedit_cursor);
wti->client_->OnPreeditString(text, spans, preedit_cursor);
}
void ZWPTextInputWrapperV1::OnPreeditStyling(
......@@ -132,7 +136,9 @@ void ZWPTextInputWrapperV1::OnPreeditStyling(
uint32_t index,
uint32_t length,
uint32_t style) {
NOTIMPLEMENTED_LOG_ONCE();
ZWPTextInputWrapperV1* wti = static_cast<ZWPTextInputWrapperV1*>(data);
wti->spans_.push_back(
ZWPTextInputWrapperClient::SpanStyle{index, length, style});
}
void ZWPTextInputWrapperV1::OnPreeditCursor(
......@@ -149,7 +155,7 @@ void ZWPTextInputWrapperV1::OnCommitString(void* data,
const char* text) {
ZWPTextInputWrapperV1* wti = static_cast<ZWPTextInputWrapperV1*>(data);
wti->ResetInputEventState();
wti->client_->OnCommitString(std::string(text));
wti->client_->OnCommitString(text);
}
void ZWPTextInputWrapperV1::OnCursorPosition(
......
......@@ -5,8 +5,11 @@
#ifndef 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 <string>
#include "ui/ozone/platform/wayland/host/zwp_text_input_wrapper.h"
......@@ -98,7 +101,8 @@ class ZWPTextInputWrapperV1 : public ZWPTextInputWrapper {
wl::Object<zwp_text_input_v1> obj_;
ZWPTextInputWrapperClient* client_;
int32_t preedit_cursor_;
std::vector<ZWPTextInputWrapperClient::SpanStyle> spans_;
int32_t preedit_cursor_ = -1;
};
} // 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