File: indexed_suggestion_candidate_button.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (125 lines) | stat: -rw-r--r-- 4,735 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 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/indexed_suggestion_candidate_button.h"

#include "chrome/browser/ui/ash/input_method/colors.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_styles.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/flex_layout.h"

namespace ui::ime {
const int kTopPadding = 4;
const int kBottomPadding = 0;
const int kLeftRightPadding = 2;
const int kBetweenSpacing = 1;
const int kBorderRadius = 2;
const int kCandidateSquareSide = 24;
const views::Label::CustomFont kCandidateTextFont = {
    .font_list = gfx::FontList(gfx::FontList({"Roboto"},
                                             gfx::Font::NORMAL,
                                             16,
                                             gfx::Font::Weight::MEDIUM))};
const views::Label::CustomFont kIndexFont = {
    .font_list = gfx::FontList(gfx::FontList({"Roboto"},
                                             gfx::Font::NORMAL,
                                             10,
                                             gfx::Font::Weight::MEDIUM))};

IndexedSuggestionCandidateButton::IndexedSuggestionCandidateButton(
    PressedCallback callback,
    const std::u16string& candidate_text,
    const std::u16string& index_text,
    bool create_legacy_candidate)
    : views::Button(std::move(callback)) {
  GetViewAccessibility().SetName(candidate_text);
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical,
      gfx::Insets()
          .set_left_right(kLeftRightPadding, kLeftRightPadding)
          .set_top_bottom(kTopPadding, kBottomPadding),
      /* between_child_spacing=*/kBetweenSpacing));

  if (create_legacy_candidate) {
    // TODO(b/240357416): Remove when emoji suggestions uses horizontal layout.
    BuildLegacyCandidate(candidate_text);
  } else {
    BuildCandidate(candidate_text, index_text);
  }
}

void IndexedSuggestionCandidateButton::SetHighlight(bool highlighted) {
  if (highlighted && !background()) {
    // Legacy option does not have a rounded border.
    // TODO(b/240357416): Remove legacy option when emoji suggestions uses
    // horizontal layout.
    int border_radius = is_legacy_candidate_ ? 0 : kBorderRadius;
    SetBackground(views::CreateRoundedRectBackground(
        ResolveSemanticColor(cros_styles::ColorName::kRippleColor),
        border_radius));
  }
  if (!highlighted && background()) {
    SetBackground(nullptr);
  }
}

void IndexedSuggestionCandidateButton::BuildLegacyCandidate(
    const std::u16string& candidate_text) {
  is_legacy_candidate_ = true;
  AddChildView(
      std::make_unique<views::Label>(candidate_text, kCandidateTextFont));
}

void IndexedSuggestionCandidateButton::BuildCandidate(
    const std::u16string& candidate_text,
    const std::u16string& index_text) {
  is_legacy_candidate_ = false;

  // Displaying the candidate text (i.e. the "A" in the diagram below).
  //   +---+
  //   | A | <-- label being created
  //   |   |
  //   | 1 |
  //   +---+

  // Wrapper uses views::Label since it doesn't override the hover state of the
  // button.
  auto* candidate_wrapper = AddChildView(std::make_unique<views::Label>());
  views::FlexLayout* candidate_wrapper_layout =
      candidate_wrapper->SetLayoutManager(
          std::make_unique<views::FlexLayout>());
  candidate_wrapper_layout->SetCrossAxisAlignment(
      views::LayoutAlignment::kCenter);
  candidate_wrapper_layout->SetMainAxisAlignment(
      views::LayoutAlignment::kCenter);
  candidate_wrapper->SetProperty(views::kBoxLayoutFlexKey,
                                 views::BoxLayoutFlexSpecification());
  candidate_wrapper->SetPreferredSize(
      gfx::Size(kCandidateSquareSide, kCandidateSquareSide));
  candidate_wrapper->AddChildView(
      std::make_unique<views::Label>(candidate_text, kCandidateTextFont));

  // Displaying the index (i.e. the "1" in the diagram below).
  //   +---+
  //   | A |
  //   |   |
  //   | 1 | <-- label being created
  //   +---+
  auto* candidate_text_label =
      AddChildView(std::make_unique<views::Label>(index_text, kIndexFont));
  candidate_text_label->SetEnabledColor(
      ResolveSemanticColor(cros_styles::ColorName::kTextColorSecondary));
}

IndexedSuggestionCandidateButton::~IndexedSuggestionCandidateButton() = default;

BEGIN_METADATA(IndexedSuggestionCandidateButton)
END_METADATA

}  // namespace ui::ime