Commit 3ef6ff3b authored by Yuichiro Hanada's avatar Yuichiro Hanada Committed by Commit Bot

Implement ImeInstance.SetComposingRegion mojo API.

The mojo call is connected to InputConnection.setComposingRegion() in
Android side.

Bug: b:163013722
Test: components_unittests
Change-Id: I4dd9d76f8788fb76e649fd8418c84ef56c8cc6f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2352453
Commit-Queue: Yuichiro Hanada <yhanada@chromium.org>
Reviewed-by: default avatarJorge Lucangeli Obes <jorgelo@chromium.org>
Reviewed-by: default avatarTetsui Ohkubo <tetsui@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804125}
parent 59516795
......@@ -60,6 +60,7 @@ class ArcImeBridge {
virtual void SendExtendSelectionAndDelete(size_t before, size_t after) = 0;
virtual void SendOnKeyboardAppearanceChanging(const gfx::Rect& new_bounds,
bool is_available) = 0;
virtual void SendSetComposingRegion(const gfx::Range& composing_range) = 0;
protected:
ArcImeBridge() {}
......
......@@ -118,6 +118,16 @@ void ArcImeBridgeImpl::SendOnKeyboardAppearanceChanging(
ime_instance->OnKeyboardAppearanceChanging(new_bounds, is_available);
}
void ArcImeBridgeImpl::SendSetComposingRegion(
const gfx::Range& composing_range) {
auto* ime_instance =
ARC_GET_INSTANCE_FOR_METHOD(bridge_service_->ime(), SetComposingRegion);
if (!ime_instance)
return;
ime_instance->SetComposingRegion(composing_range);
}
void ArcImeBridgeImpl::OnTextInputTypeChanged(
ui::TextInputType type,
bool is_personalized_learning_allowed,
......
......@@ -37,6 +37,7 @@ class ArcImeBridgeImpl : public ArcImeBridge, public mojom::ImeHost {
void SendOnKeyboardAppearanceChanging(const gfx::Rect& new_bounds,
bool is_available) override;
void SendSelectionRange(const gfx::Range& selection_range) override;
void SendSetComposingRegion(const gfx::Range& composing_range) override;
// mojom::ImeHost overrides:
void OnTextInputTypeChanged(ui::TextInputType type,
......
......@@ -616,41 +616,16 @@ bool ArcImeService::ShouldDoLearning() {
bool ArcImeService::SetCompositionFromExistingText(
const gfx::Range& range,
const std::vector<ui::ImeTextSpan>& ui_ime_text_spans) {
// TODO(https://crbug.com/952757): Implement this method using
// Android API's |SetComposingRegion|.
if (!selection_range_.is_empty() || !selection_range_.IsBoundedBy(range)) {
return true;
}
if (!range.IsBoundedBy(text_range_))
return false;
// |text_in_range_| might be only a subset of the full text, so make sure that
// the composition range is within bounds.
const gfx::Range range_relative_to_text(
range.GetMin() - text_range_.GetMin(),
range.GetMax() - text_range_.GetMin());
if (range_relative_to_text.GetMin() < 0 ||
range_relative_to_text.length() > text_in_range_.length()) {
return true;
}
InvalidateSurroundingTextAndSelectionRange();
has_composition_text_ = !range.is_empty();
// Save the text to be composed since we need to recompose it later.
base::string16 text = text_in_range_.substr(range_relative_to_text.GetMin(),
range_relative_to_text.length());
const int cursor_relative_to_text =
selection_range_.start() - text_range_.GetMin();
// Confirm the existing composition, delete the text to be composed, and
// recompose it.
ConfirmCompositionText(/*keep_selection=*/true);
ExtendSelectionAndDelete(
/*before=*/cursor_relative_to_text - range_relative_to_text.GetMin(),
/*after=*/range_relative_to_text.GetMax() - cursor_relative_to_text);
ui::CompositionText composition;
composition.text = std::move(text);
composition.selection =
gfx::Range(cursor_relative_to_text, cursor_relative_to_text);
composition.ime_text_spans = ui_ime_text_spans;
SetCompositionText(std::move(composition));
// The sent |range| might be already invalid if the textfield state in Android
// side is changed simultaneously. It's okay because InputConnection's
// setComposingRegion handles invalid region correctly.
ime_bridge_->SendSetComposingRegion(range);
return true;
}
......
......@@ -34,9 +34,7 @@ namespace {
class FakeArcImeBridge : public ArcImeBridge {
public:
FakeArcImeBridge()
: count_send_insert_text_(0),
last_keyboard_availability_(false),
selection_range_(gfx::Range()) {}
: count_send_insert_text_(0), last_keyboard_availability_(false) {}
void SendSetCompositionText(const ui::CompositionText& composition) override {
}
......@@ -55,6 +53,9 @@ class FakeArcImeBridge : public ArcImeBridge {
last_keyboard_bounds_ = new_bounds;
last_keyboard_availability_ = is_available;
}
void SendSetComposingRegion(const gfx::Range& composing_range) override {
composing_range_ = composing_range;
}
int count_send_insert_text() const { return count_send_insert_text_; }
const gfx::Rect& last_keyboard_bounds() const {
......@@ -64,12 +65,14 @@ class FakeArcImeBridge : public ArcImeBridge {
return last_keyboard_availability_;
}
gfx::Range selection_range() { return selection_range_; }
gfx::Range composing_range() { return composing_range_; }
private:
int count_send_insert_text_;
gfx::Rect last_keyboard_bounds_;
bool last_keyboard_availability_;
gfx::Range selection_range_;
gfx::Range composing_range_;
};
class FakeInputMethod : public ui::DummyInputMethod {
......@@ -512,4 +515,27 @@ TEST_F(ArcImeServiceTest, DoNothingIfArcWindowIsNotFocused) {
EXPECT_EQ(0, fake_input_method_->count_cancel_composition());
}
TEST_F(ArcImeServiceTest, SetComposingRegion) {
instance_->OnWindowFocused(arc_win_.get(), nullptr);
const gfx::Range composing_range(1, 3);
// Ignore it if the range is outside of text range.
instance_->SetCompositionFromExistingText(composing_range, {});
EXPECT_EQ(gfx::Range(), fake_arc_ime_bridge_->composing_range());
instance_->OnCursorRectChangedWithSurroundingText(
gfx::Rect(), gfx::Range(0, 100), base::string16(100, 'a'),
gfx::Range(0, 0), false);
instance_->SetCompositionFromExistingText(composing_range, {});
EXPECT_EQ(composing_range, fake_arc_ime_bridge_->composing_range());
// Ignore it if the range is outside of text range.
instance_->OnCursorRectChangedWithSurroundingText(
gfx::Rect(), gfx::Range(0, 100), base::string16(100, 'a'),
gfx::Range(0, 0), false);
instance_->SetCompositionFromExistingText(gfx::Range(50, 101), {});
EXPECT_EQ(composing_range, fake_arc_ime_bridge_->composing_range());
}
} // namespace arc
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Next MinVersion: 15
// Next MinVersion: 16
module arc.mojom;
......@@ -114,7 +114,7 @@ interface ImeHost {
=> (bool is_consumed);
};
// Next method ID: 8
// Next method ID: 9
interface ImeInstance {
// DEPRECATED: Please use Init@6 instead.
InitDeprecated@0(pending_remote<ImeHost> host_remote);
......@@ -145,4 +145,7 @@ interface ImeInstance {
// Deletes current selection plus the specified number of char16 values
// before and after selection or caret.
[MinVersion=4] ExtendSelectionAndDelete@5(uint64 before, uint64 after);
// Sets composing region requested by the host IME.
[MinVersion=15] SetComposingRegion@8(Range range);
};
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