1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/input_method/candidate_view.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/ash/input_method/candidate_window_constants.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/ime/candidate_window.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/label.h"
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/widget/widget.h"
namespace ui::ime {
namespace {
// VerticalCandidateLabel is used for rendering candidate text in
// the vertical candidate window.
class VerticalCandidateLabel : public views::Label {
METADATA_HEADER(VerticalCandidateLabel, views::Label)
public:
VerticalCandidateLabel() = default;
VerticalCandidateLabel(const VerticalCandidateLabel&) = delete;
VerticalCandidateLabel& operator=(const VerticalCandidateLabel&) = delete;
~VerticalCandidateLabel() override = default;
private:
// views::Label:
// Returns the preferred size, but guarantees that the width has at
// least kMinCandidateLabelWidth pixels.
gfx::Size CalculatePreferredSize(
const views::SizeBounds& available_size) const override {
gfx::Size size = Label::CalculatePreferredSize(available_size);
size.SetToMax(gfx::Size(kMinCandidateLabelWidth, 0));
size.SetToMin(gfx::Size(kMaxCandidateLabelWidth, size.height()));
return size;
}
};
BEGIN_METADATA(VerticalCandidateLabel)
END_METADATA
// The label text is not set in this class.
class ShortcutLabel : public views::Label {
METADATA_HEADER(ShortcutLabel, views::Label)
public:
explicit ShortcutLabel(ui::CandidateWindow::Orientation orientation)
: orientation_(orientation) {
// TODO(tapted): Get this FontList from views::style.
if (orientation == ui::CandidateWindow::VERTICAL) {
SetFontList(font_list().Derive(kFontSizeDelta, gfx::Font::NORMAL,
gfx::Font::Weight::BOLD));
} else {
SetFontList(font_list().DeriveWithSizeDelta(kFontSizeDelta));
}
// TODO(satorux): Maybe we need to use language specific fonts for
// candidate_label, like Chinese font for Chinese input method?
// Setup paddings.
const auto kVerticalShortcutLabelInsets = gfx::Insets::TLBR(1, 6, 1, 6);
const auto kHorizontalShortcutLabelInsets = gfx::Insets::TLBR(1, 3, 1, 0);
const gfx::Insets insets = (orientation == ui::CandidateWindow::VERTICAL
? kVerticalShortcutLabelInsets
: kHorizontalShortcutLabelInsets);
SetBorder(views::CreateEmptyBorder(insets));
SetElideBehavior(gfx::NO_ELIDE);
}
ShortcutLabel(const ShortcutLabel&) = delete;
ShortcutLabel& operator=(const ShortcutLabel&) = delete;
~ShortcutLabel() override = default;
// views::Label:
void OnThemeChanged() override {
Label::OnThemeChanged();
// Add decoration based on the orientation.
if (orientation_ == ui::CandidateWindow::VERTICAL) {
// Set the background color.
SkColor blackish = color_utils::AlphaBlend(
SK_ColorBLACK,
GetColorProvider()->GetColor(ui::kColorWindowBackground), 0.25f);
SetBackground(views::CreateSolidBackground(SkColorSetA(blackish, 0xE0)));
}
}
private:
const ui::CandidateWindow::Orientation orientation_;
};
BEGIN_METADATA(ShortcutLabel)
END_METADATA
// Creates an annotation label. Sets no text by default.
std::unique_ptr<views::Label> CreateAnnotationLabel() {
auto label = views::Builder<views::Label>()
.SetHorizontalAlignment(gfx::ALIGN_LEFT)
.SetElideBehavior(gfx::NO_ELIDE)
.SetEnabledColor(ui::kColorLabelForegroundSecondary)
.Build();
label->SetFontList(label->font_list().DeriveWithSizeDelta(kFontSizeDelta));
return label;
}
// Creates the candidate label, and returns it (never returns nullptr).
// The label text is not set in this function.
std::unique_ptr<views::Label> CreateCandidateLabel(
ui::CandidateWindow::Orientation orientation) {
std::unique_ptr<views::Label> candidate_label;
if (orientation == ui::CandidateWindow::VERTICAL) {
candidate_label = std::make_unique<VerticalCandidateLabel>();
} else {
candidate_label = std::make_unique<views::Label>();
}
// Change the font size.
candidate_label->SetFontList(
candidate_label->font_list().DeriveWithSizeDelta(kFontSizeDelta));
candidate_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
candidate_label->SetElideBehavior(gfx::NO_ELIDE);
return candidate_label;
}
} // namespace
CandidateView::CandidateView(PressedCallback callback,
ui::CandidateWindow::Orientation orientation)
: views::Button(std::move(callback)), orientation_(orientation) {
SetBorder(views::CreateEmptyBorder(1));
shortcut_label_ = AddChildView(std::make_unique<ShortcutLabel>(orientation));
candidate_label_ = AddChildView(CreateCandidateLabel(orientation));
annotation_label_ = AddChildView(CreateAnnotationLabel());
if (orientation == ui::CandidateWindow::VERTICAL) {
infolist_icon_ =
AddChildView(views::Builder<views::View>()
.SetBackground(views::CreateSolidBackground(
ui::kColorFocusableBorderFocused))
.Build());
}
SetFocusBehavior(views::View::FocusBehavior::ACCESSIBLE_ONLY);
GetViewAccessibility().SetRole(ax::mojom::Role::kImeCandidate);
}
void CandidateView::GetPreferredWidths(int* shortcut_width,
int* candidate_width) {
*shortcut_width =
shortcut_label_
->GetPreferredSize(views::SizeBounds(shortcut_label_->width(), {}))
.width();
*candidate_width =
candidate_label_
->GetPreferredSize(views::SizeBounds(candidate_label_->width(), {}))
.width();
}
void CandidateView::SetWidths(int shortcut_width, int candidate_width) {
shortcut_width_ = shortcut_width;
shortcut_label_->SetVisible(shortcut_width_ != 0);
candidate_width_ = candidate_width;
}
void CandidateView::SetEntry(const ui::CandidateWindow::Entry& entry) {
std::u16string label = entry.label;
if (!label.empty() && orientation_ != ui::CandidateWindow::VERTICAL) {
label += u".";
}
shortcut_label_->SetText(label);
candidate_label_->SetText(entry.value);
annotation_label_->SetText(entry.annotation);
GetViewAccessibility().SetName(entry.value);
}
void CandidateView::SetInfolistIcon(bool enable) {
if (infolist_icon_) {
infolist_icon_->SetVisible(enable);
}
SchedulePaint();
}
void CandidateView::SetHighlighted(bool highlighted) {
if (highlighted_ == highlighted) {
return;
}
highlighted_ = highlighted;
if (highlighted) {
SetBackground(
views::CreateSolidBackground(ui::kColorTextfieldSelectionBackground));
SetBorder(views::CreateSolidBorder(1, ui::kColorFocusableBorderFocused));
// Cancel currently focused one.
for (View* view : parent()->children()) {
if (view != this) {
static_cast<CandidateView*>(view)->SetHighlighted(false);
}
}
} else {
SetBackground(nullptr);
SetBorder(views::CreateEmptyBorder(1));
}
SchedulePaint();
}
void CandidateView::StateChanged(ButtonState old_state) {
Button::StateChanged(old_state);
int text_style = GetState() == STATE_DISABLED ? views::style::STYLE_DISABLED
: views::style::STYLE_PRIMARY;
shortcut_label_->SetEnabledColor(views::TypographyProvider::Get().GetColorId(
views::style::CONTEXT_LABEL, text_style));
if (GetState() == STATE_PRESSED) {
SetHighlighted(true);
}
}
bool CandidateView::OnMouseDragged(const ui::MouseEvent& event) {
if (!HitTestPoint(event.location())) {
// Moves the drag target to the sibling view.
gfx::Point location_in_widget(event.location());
ConvertPointToWidget(this, &location_in_widget);
for (View* view : parent()->children()) {
if (view == this) {
continue;
}
gfx::Point location_in_sibling(location_in_widget);
ConvertPointFromWidget(view, &location_in_sibling);
if (view->HitTestPoint(location_in_sibling)) {
GetWidget()->GetRootView()->SetMouseAndGestureHandler(view);
auto* sibling = static_cast<CandidateView*>(view);
sibling->SetHighlighted(true);
return view->OnMouseDragged(ui::MouseEvent(event, this, sibling));
}
}
return false;
}
return views::Button::OnMouseDragged(event);
}
void CandidateView::Layout(PassKey) {
const int padding_width =
orientation_ == ui::CandidateWindow::VERTICAL ? 4 : 6;
int x = 0;
shortcut_label_->SetBounds(x, 0, shortcut_width_, height());
if (shortcut_width_ > 0) {
x += shortcut_width_ + padding_width;
}
candidate_label_->SetBounds(x, 0, candidate_width_, height());
x += candidate_width_ + padding_width;
int right = bounds().right();
if (infolist_icon_ && infolist_icon_->GetVisible()) {
infolist_icon_->SetBounds(
right - kInfolistIndicatorIconWidth - kInfolistIndicatorIconPadding,
kInfolistIndicatorIconPadding, kInfolistIndicatorIconWidth,
height() - kInfolistIndicatorIconPadding * 2);
right -= kInfolistIndicatorIconWidth + kInfolistIndicatorIconPadding * 2;
}
annotation_label_->SetBounds(x, 0, right - x, height());
}
gfx::Size CandidateView::CalculatePreferredSize(
const views::SizeBounds& available_size) const {
const int padding_width =
orientation_ == ui::CandidateWindow::VERTICAL ? 4 : 6;
gfx::Size size;
if (shortcut_label_->GetVisible()) {
size = shortcut_label_->GetPreferredSize(
views::SizeBounds(shortcut_width_, {}));
size.SetToMax(gfx::Size(shortcut_width_, 0));
size.Enlarge(padding_width, 0);
}
gfx::Size candidate_size = candidate_label_->GetPreferredSize(
views::SizeBounds(candidate_width_, {}));
candidate_size.SetToMax(gfx::Size(candidate_width_, 0));
size.Enlarge(candidate_size.width() + padding_width, 0);
size.SetToMax(candidate_size);
int reserve_margin =
kInfolistIndicatorIconWidth + kInfolistIndicatorIconPadding * 2;
if (annotation_label_->GetVisible()) {
views::SizeBound available_width = std::max<views::SizeBound>(
0, available_size.width() - size.width() - reserve_margin);
gfx::Size annotation_size = annotation_label_->GetPreferredSize(
views::SizeBounds(available_width, {}));
size.Enlarge(annotation_size.width() + padding_width, 0);
size.SetToMax(annotation_size);
}
// Reserves the margin for infolist_icon even if it's not visible.
size.Enlarge(reserve_margin, 0);
return size;
}
void CandidateView::SetPositionData(int index, int total) {
candidate_index_ = index;
total_candidates_ = total;
// PosInSet needs to be incremented since |candidate_index_| is 0-based.
GetViewAccessibility().SetPosInSet(candidate_index_ + 1);
GetViewAccessibility().SetSetSize(total_candidates_);
}
BEGIN_METADATA(CandidateView)
END_METADATA
} // namespace ui::ime
|