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,
bool InputMethodEngine::SetSuggestion(int context_id,
const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab,
std::string* error) {
if (!IsActive()) {
......@@ -238,7 +239,7 @@ bool InputMethodEngine::SetSuggestion(int context_id,
IMESuggestionWindowHandlerInterface* sw_handler =
ui::IMEBridge::Get()->GetSuggestionWindowHandler();
if (sw_handler)
sw_handler->Show(text, show_tab);
sw_handler->Show(text, confirmed_text, show_tab);
return true;
}
......
......@@ -119,9 +119,13 @@ class InputMethodEngine : public ::input_method::InputMethodEngineBase {
// Dismiss suggestion window.
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,
const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab,
std::string* error);
......
......@@ -119,7 +119,8 @@ bool PersonalInfoSuggester::Suggest(const base::string16& text) {
void PersonalInfoSuggester::ShowSuggestion(const base::string16& text) {
std::string error;
engine_->SetSuggestion(context_id_, text, true, &error);
engine_->SetSuggestion(context_id_, text, base::EmptyString16(), true,
&error);
if (!error.empty()) {
LOG(ERROR) << "Fail to show suggestion. " << error;
}
......
......@@ -74,11 +74,12 @@ void SuggestionWindowControllerImpl::FocusStateChanged() {
}
void SuggestionWindowControllerImpl::Show(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab) {
if (!suggestion_window_view_)
Init();
suggestion_text_ = text;
suggestion_window_view_->Show(text, show_tab);
suggestion_window_view_->Show(text, confirmed_text, show_tab);
}
base::string16 SuggestionWindowControllerImpl::GetText() const {
......
......@@ -31,7 +31,9 @@ class SuggestionWindowControllerImpl
// IMESuggestionWindowHandlerInterface implementation.
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;
base::string16 GetText() const override;
void FocusStateChanged() override;
......
......@@ -22,7 +22,9 @@ class COMPONENT_EXPORT(UI_BASE_IME) IMESuggestionWindowHandlerInterface {
virtual ~IMESuggestionWindowHandlerInterface() {}
// 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() {}
// Called to get the current suggestion text.
......
......@@ -19,15 +19,14 @@ namespace {
// Creates the suggestion label, and returns it (never returns nullptr).
// The label text is not set in this function.
std::unique_ptr<views::Label> CreateSuggestionLabel() {
std::unique_ptr<views::Label> suggestion_label =
std::make_unique<views::Label>();
suggestion_label->SetFontList(kSuggestionFont);
suggestion_label->SetEnabledColor(kSuggestionLabelColor);
std::unique_ptr<views::StyledLabel> CreateSuggestionLabel() {
std::unique_ptr<views::StyledLabel> suggestion_label =
std::make_unique<views::StyledLabel>(base::EmptyString16(),
/*listener=*/nullptr);
suggestion_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
suggestion_label->SetBorder(
views::CreateEmptyBorder(gfx::Insets(kPadding / 2, 0)));
suggestion_label->SetAutoColorReadabilityEnabled(false);
return suggestion_label;
}
......@@ -37,14 +36,14 @@ std::unique_ptr<views::Label> CreateAnnotationLabel() {
std::unique_ptr<views::Label> annotation_label =
std::make_unique<views::Label>();
annotation_label->SetFontList(kAnnotationFont);
annotation_label->SetEnabledColor(kSuggestionLabelColor);
annotation_label->SetEnabledColor(kSuggestionColor);
annotation_label->SetHorizontalAlignment(gfx::ALIGN_CENTER);
// Set insets.
const gfx::Insets insets(0, 0, 0, kPadding / 2);
annotation_label->SetBorder(views::CreateRoundedRectBorder(
kAnnotationBorderThickness, kAnnotationCornerRadius, insets,
kSuggestionLabelColor));
kSuggestionColor));
// Set text.
annotation_label->SetText(base::UTF8ToUTF16(kTabKey));
......@@ -61,12 +60,33 @@ SuggestionView::SuggestionView() {
SuggestionView::~SuggestionView() = default;
void SuggestionView::SetView(const base::string16& text, const bool show_tab) {
suggestion_label_->SetText(text);
void SuggestionView::SetView(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab) {
SetSuggestionText(text, confirmed_text);
suggestion_width_ = suggestion_label_->GetPreferredSize().width();
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 {
return "SuggestionView";
}
......
......@@ -9,6 +9,7 @@
#include "base/macros.h"
#include "ui/chromeos/ui_chromeos_export.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/view.h"
namespace ui {
......@@ -32,7 +33,8 @@ constexpr int kAnnotationCornerRadius = 4;
constexpr int kPadding = 10;
constexpr int kAnnotationPaddingHeight = 6;
constexpr char kTabKey[] = "tab";
constexpr SkColor kSuggestionLabelColor =
constexpr SkColor kConfirmedTextColor = gfx::kGoogleGrey900;
constexpr SkColor kSuggestionColor =
SkColorSetA(gfx::kGoogleGrey900, gfx::kGoogleGreyAlpha500);
// SuggestionView renders a suggestion.
......@@ -41,7 +43,9 @@ class UI_CHROMEOS_EXPORT SuggestionView : public views::View {
SuggestionView();
~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:
friend class SuggestionWindowViewTest;
......@@ -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
// 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.
views::Label* suggestion_label_ = nullptr;
views::StyledLabel* suggestion_label_ = nullptr;
// The annotation label renders annotations.
views::Label* annotation_label_ = nullptr;
......
......@@ -88,15 +88,18 @@ void SuggestionWindowView::Hide() {
}
void SuggestionWindowView::Show(const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab) {
UpdateSuggestion(text, show_tab);
UpdateSuggestion(text, confirmed_text, show_tab);
suggestion_view_->SetVisible(true);
SizeToContents();
}
void SuggestionWindowView::UpdateSuggestion(const base::string16& text,
const bool show_tab) {
suggestion_view_->SetView(text, show_tab);
void SuggestionWindowView::UpdateSuggestion(
const base::string16& text,
const base::string16& confirmed_text,
const bool show_tab) {
suggestion_view_->SetView(text, confirmed_text, show_tab);
std::unique_ptr<SuggestionWindowBorder> border =
std::make_unique<SuggestionWindowBorder>();
......
......@@ -31,14 +31,18 @@ class UI_CHROMEOS_EXPORT SuggestionWindowView
void Hide();
// 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);
private:
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:
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