Commit b258213d authored by My Nguyen's avatar My Nguyen Committed by Commit Bot

Add ability to show suggestion with confirmed text

Screenshot view when there is confirmed_text:
https://screenshot.googleplex.com/Q0sjaEKi2Qa.

There is still a need to take care of cursor position of the window, it
should always be at the start of the confirmed_text.

Bug: 1068044
Change-Id: I12ca6553f6cf57eac82127433340046aaff804f3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2137181
Commit-Queue: My Nguyen <myy@chromium.org>
Reviewed-by: default avatarKeith Lee <keithlee@chromium.org>
Reviewed-by: default avatarJing Wang <jiwan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756932}
parent f32af4ae
...@@ -224,6 +224,7 @@ bool InputMethodEngine::SetCursorPosition(int context_id, ...@@ -224,6 +224,7 @@ bool InputMethodEngine::SetCursorPosition(int context_id,
bool InputMethodEngine::SetSuggestion(int context_id, bool InputMethodEngine::SetSuggestion(int context_id,
const base::string16& text, const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab, const bool show_tab,
std::string* error) { std::string* error) {
if (!IsActive()) { if (!IsActive()) {
...@@ -238,7 +239,7 @@ bool InputMethodEngine::SetSuggestion(int context_id, ...@@ -238,7 +239,7 @@ bool InputMethodEngine::SetSuggestion(int context_id,
IMESuggestionWindowHandlerInterface* sw_handler = IMESuggestionWindowHandlerInterface* sw_handler =
ui::IMEBridge::Get()->GetSuggestionWindowHandler(); ui::IMEBridge::Get()->GetSuggestionWindowHandler();
if (sw_handler) if (sw_handler)
sw_handler->Show(text, show_tab); sw_handler->Show(text, confirmed_text, show_tab);
return true; return true;
} }
......
...@@ -119,9 +119,13 @@ class InputMethodEngine : public ::input_method::InputMethodEngineBase { ...@@ -119,9 +119,13 @@ class InputMethodEngine : public ::input_method::InputMethodEngineBase {
// Dismiss suggestion window. // Dismiss suggestion window.
bool DismissSuggestion(int context_id, std::string* error); bool DismissSuggestion(int context_id, std::string* error);
// Set and show suggestion window. // Sets text and show suggestion window.
// text - the full suggestion text.
// confirmed_text - the confirmed text that the user has typed so far.
// show_tab - whether to show "tab" in the suggestion window.
bool SetSuggestion(int context_id, bool SetSuggestion(int context_id,
const base::string16& text, const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab, const bool show_tab,
std::string* error); std::string* error);
......
...@@ -119,7 +119,8 @@ bool PersonalInfoSuggester::Suggest(const base::string16& text) { ...@@ -119,7 +119,8 @@ bool PersonalInfoSuggester::Suggest(const base::string16& text) {
void PersonalInfoSuggester::ShowSuggestion(const base::string16& text) { void PersonalInfoSuggester::ShowSuggestion(const base::string16& text) {
std::string error; std::string error;
engine_->SetSuggestion(context_id_, text, true, &error); engine_->SetSuggestion(context_id_, text, base::EmptyString16(), true,
&error);
if (!error.empty()) { if (!error.empty()) {
LOG(ERROR) << "Fail to show suggestion. " << error; LOG(ERROR) << "Fail to show suggestion. " << error;
} }
......
...@@ -74,11 +74,12 @@ void SuggestionWindowControllerImpl::FocusStateChanged() { ...@@ -74,11 +74,12 @@ void SuggestionWindowControllerImpl::FocusStateChanged() {
} }
void SuggestionWindowControllerImpl::Show(const base::string16& text, void SuggestionWindowControllerImpl::Show(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab) { const bool show_tab) {
if (!suggestion_window_view_) if (!suggestion_window_view_)
Init(); Init();
suggestion_text_ = text; suggestion_text_ = text;
suggestion_window_view_->Show(text, show_tab); suggestion_window_view_->Show(text, confirmed_text, show_tab);
} }
base::string16 SuggestionWindowControllerImpl::GetText() const { base::string16 SuggestionWindowControllerImpl::GetText() const {
......
...@@ -31,7 +31,9 @@ class SuggestionWindowControllerImpl ...@@ -31,7 +31,9 @@ class SuggestionWindowControllerImpl
// IMESuggestionWindowHandlerInterface implementation. // IMESuggestionWindowHandlerInterface implementation.
void SetBounds(const gfx::Rect& cursor_bounds) override; void SetBounds(const gfx::Rect& cursor_bounds) override;
void Show(const base::string16& text, const bool show_tab) override; void Show(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab) override;
void Hide() override; void Hide() override;
base::string16 GetText() const override; base::string16 GetText() const override;
void FocusStateChanged() override; void FocusStateChanged() override;
......
...@@ -22,7 +22,9 @@ class COMPONENT_EXPORT(UI_BASE_IME) IMESuggestionWindowHandlerInterface { ...@@ -22,7 +22,9 @@ class COMPONENT_EXPORT(UI_BASE_IME) IMESuggestionWindowHandlerInterface {
virtual ~IMESuggestionWindowHandlerInterface() {} virtual ~IMESuggestionWindowHandlerInterface() {}
// Called when showing/hiding suggestion window. // Called when showing/hiding suggestion window.
virtual void Show(const base::string16& text, const bool show_tab) {} virtual void Show(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab) {}
virtual void Hide() {} virtual void Hide() {}
// Called to get the current suggestion text. // Called to get the current suggestion text.
......
...@@ -19,15 +19,14 @@ namespace { ...@@ -19,15 +19,14 @@ namespace {
// Creates the suggestion label, and returns it (never returns nullptr). // Creates the suggestion label, and returns it (never returns nullptr).
// The label text is not set in this function. // The label text is not set in this function.
std::unique_ptr<views::Label> CreateSuggestionLabel() { std::unique_ptr<views::StyledLabel> CreateSuggestionLabel() {
std::unique_ptr<views::Label> suggestion_label = std::unique_ptr<views::StyledLabel> suggestion_label =
std::make_unique<views::Label>(); std::make_unique<views::StyledLabel>(base::EmptyString16(),
/*listener=*/nullptr);
suggestion_label->SetFontList(kSuggestionFont);
suggestion_label->SetEnabledColor(kSuggestionLabelColor);
suggestion_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); suggestion_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
suggestion_label->SetBorder( suggestion_label->SetBorder(
views::CreateEmptyBorder(gfx::Insets(kPadding / 2, 0))); views::CreateEmptyBorder(gfx::Insets(kPadding / 2, 0)));
suggestion_label->SetAutoColorReadabilityEnabled(false);
return suggestion_label; return suggestion_label;
} }
...@@ -37,14 +36,14 @@ std::unique_ptr<views::Label> CreateAnnotationLabel() { ...@@ -37,14 +36,14 @@ std::unique_ptr<views::Label> CreateAnnotationLabel() {
std::unique_ptr<views::Label> annotation_label = std::unique_ptr<views::Label> annotation_label =
std::make_unique<views::Label>(); std::make_unique<views::Label>();
annotation_label->SetFontList(kAnnotationFont); annotation_label->SetFontList(kAnnotationFont);
annotation_label->SetEnabledColor(kSuggestionLabelColor); annotation_label->SetEnabledColor(kSuggestionColor);
annotation_label->SetHorizontalAlignment(gfx::ALIGN_CENTER); annotation_label->SetHorizontalAlignment(gfx::ALIGN_CENTER);
// Set insets. // Set insets.
const gfx::Insets insets(0, 0, 0, kPadding / 2); const gfx::Insets insets(0, 0, 0, kPadding / 2);
annotation_label->SetBorder(views::CreateRoundedRectBorder( annotation_label->SetBorder(views::CreateRoundedRectBorder(
kAnnotationBorderThickness, kAnnotationCornerRadius, insets, kAnnotationBorderThickness, kAnnotationCornerRadius, insets,
kSuggestionLabelColor)); kSuggestionColor));
// Set text. // Set text.
annotation_label->SetText(base::UTF8ToUTF16(kTabKey)); annotation_label->SetText(base::UTF8ToUTF16(kTabKey));
...@@ -61,12 +60,33 @@ SuggestionView::SuggestionView() { ...@@ -61,12 +60,33 @@ SuggestionView::SuggestionView() {
SuggestionView::~SuggestionView() = default; SuggestionView::~SuggestionView() = default;
void SuggestionView::SetView(const base::string16& text, const bool show_tab) { void SuggestionView::SetView(const base::string16& text,
suggestion_label_->SetText(text); const base::string16& confirmed_text,
const bool show_tab) {
SetSuggestionText(text, confirmed_text);
suggestion_width_ = suggestion_label_->GetPreferredSize().width(); suggestion_width_ = suggestion_label_->GetPreferredSize().width();
annotation_label_->SetVisible(show_tab); annotation_label_->SetVisible(show_tab);
} }
void SuggestionView::SetSuggestionText(const base::string16& text,
const base::string16& confirmed_text) {
// SetText clears the existing style.
suggestion_label_->SetText(text);
size_t offset = confirmed_text.length();
if (offset != 0) {
views::StyledLabel::RangeStyleInfo confirmed_style;
confirmed_style.custom_font = kSuggestionFont;
confirmed_style.override_color = kConfirmedTextColor;
suggestion_label_->AddStyleRange(gfx::Range(0, offset), confirmed_style);
}
views::StyledLabel::RangeStyleInfo suggestion_style;
suggestion_style.custom_font = kSuggestionFont;
suggestion_style.override_color = kSuggestionColor;
suggestion_label_->AddStyleRange(gfx::Range(offset, text.length()),
suggestion_style);
}
const char* SuggestionView::GetClassName() const { const char* SuggestionView::GetClassName() const {
return "SuggestionView"; return "SuggestionView";
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "ui/chromeos/ui_chromeos_export.h" #include "ui/chromeos/ui_chromeos_export.h"
#include "ui/views/controls/label.h" #include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace ui { namespace ui {
...@@ -32,7 +33,8 @@ constexpr int kAnnotationCornerRadius = 4; ...@@ -32,7 +33,8 @@ constexpr int kAnnotationCornerRadius = 4;
constexpr int kPadding = 10; constexpr int kPadding = 10;
constexpr int kAnnotationPaddingHeight = 6; constexpr int kAnnotationPaddingHeight = 6;
constexpr char kTabKey[] = "tab"; constexpr char kTabKey[] = "tab";
constexpr SkColor kSuggestionLabelColor = constexpr SkColor kConfirmedTextColor = gfx::kGoogleGrey900;
constexpr SkColor kSuggestionColor =
SkColorSetA(gfx::kGoogleGrey900, gfx::kGoogleGreyAlpha500); SkColorSetA(gfx::kGoogleGrey900, gfx::kGoogleGreyAlpha500);
// SuggestionView renders a suggestion. // SuggestionView renders a suggestion.
...@@ -41,7 +43,9 @@ class UI_CHROMEOS_EXPORT SuggestionView : public views::View { ...@@ -41,7 +43,9 @@ class UI_CHROMEOS_EXPORT SuggestionView : public views::View {
SuggestionView(); SuggestionView();
~SuggestionView() override; ~SuggestionView() override;
void SetView(const base::string16& text, const bool show_tab); void SetView(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab);
private: private:
friend class SuggestionWindowViewTest; friend class SuggestionWindowViewTest;
...@@ -55,8 +59,11 @@ class UI_CHROMEOS_EXPORT SuggestionView : public views::View { ...@@ -55,8 +59,11 @@ class UI_CHROMEOS_EXPORT SuggestionView : public views::View {
// Views created in the class will be part of tree of |this|, so these // Views created in the class will be part of tree of |this|, so these
// child views will be deleted when |this| is deleted. // child views will be deleted when |this| is deleted.
void SetSuggestionText(const base::string16& text,
const base::string16& confirmed_text);
// The suggestion label renders suggestions. // The suggestion label renders suggestions.
views::Label* suggestion_label_ = nullptr; views::StyledLabel* suggestion_label_ = nullptr;
// The annotation label renders annotations. // The annotation label renders annotations.
views::Label* annotation_label_ = nullptr; views::Label* annotation_label_ = nullptr;
......
...@@ -88,15 +88,18 @@ void SuggestionWindowView::Hide() { ...@@ -88,15 +88,18 @@ void SuggestionWindowView::Hide() {
} }
void SuggestionWindowView::Show(const base::string16& text, void SuggestionWindowView::Show(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab) { const bool show_tab) {
UpdateSuggestion(text, show_tab); UpdateSuggestion(text, confirmed_text, show_tab);
suggestion_view_->SetVisible(true); suggestion_view_->SetVisible(true);
SizeToContents(); SizeToContents();
} }
void SuggestionWindowView::UpdateSuggestion(const base::string16& text, void SuggestionWindowView::UpdateSuggestion(
const bool show_tab) { const base::string16& text,
suggestion_view_->SetView(text, show_tab); const base::string16& confirmed_text,
const bool show_tab) {
suggestion_view_->SetView(text, confirmed_text, show_tab);
std::unique_ptr<SuggestionWindowBorder> border = std::unique_ptr<SuggestionWindowBorder> border =
std::make_unique<SuggestionWindowBorder>(); std::make_unique<SuggestionWindowBorder>();
......
...@@ -31,14 +31,18 @@ class UI_CHROMEOS_EXPORT SuggestionWindowView ...@@ -31,14 +31,18 @@ class UI_CHROMEOS_EXPORT SuggestionWindowView
void Hide(); void Hide();
// Shows suggestion text. // Shows suggestion text.
void Show(const base::string16& text, const bool show_tab); void Show(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab);
void SetBounds(const gfx::Rect& cursor_bounds); void SetBounds(const gfx::Rect& cursor_bounds);
private: private:
friend class SuggestionWindowViewTest; friend class SuggestionWindowViewTest;
void UpdateSuggestion(const base::string16& text, const bool show_tab); void UpdateSuggestion(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab);
// views::BubbleDialogDelegateView: // views::BubbleDialogDelegateView:
const char* GetClassName() const override; const char* GetClassName() const override;
......
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