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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
ash / style / system_textfield.cc [blame]
// 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 "ash/style/system_textfield.h"
#include <optional>
#include "ash/style/ash_color_id.h"
#include "ash/style/system_textfield_controller.h"
#include "ash/style/typography.h"
#include "ash/wm/work_area_insets.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_provider.h"
#include "ui/events/event_handler.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/canvas.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace ash {
namespace {
// The heights of textfield containers for different font sizes.
constexpr int kSmallContainerHeight = 24;
constexpr int kMediumContainerHeight = 28;
constexpr int kLargeContainerHeight = 28;
// The minimum textfield container width.
constexpr int kMinWidth = 80;
// The gap between the focus ring and textfield container.
constexpr float kFocusRingGap = 2.0f;
// The border insets to add horizontal paddings in container.
constexpr gfx::Insets kBorderInsets = gfx::Insets::VH(0, 8);
// The rounded conner radius of textfield container.
constexpr int kCornerRadius = 4;
// Gets textfield container heights for different types.
int GetContainerHeightFromType(SystemTextfield::Type type) {
int container_height;
switch (type) {
case SystemTextfield::Type::kSmall:
container_height = kSmallContainerHeight;
break;
case SystemTextfield::Type::kMedium:
container_height = kMediumContainerHeight;
break;
case SystemTextfield::Type::kLarge:
container_height = kLargeContainerHeight;
break;
}
return container_height;
}
// Gets font list for different types.
gfx::FontList GetFontListFromType(SystemTextfield::Type type) {
TypographyToken token;
switch (type) {
case SystemTextfield::Type::kSmall:
token = TypographyToken::kCrosAnnotation1;
break;
case SystemTextfield::Type::kMedium:
token = TypographyToken::kCrosBody1;
break;
case SystemTextfield::Type::kLarge:
token = TypographyToken::kCrosBody0;
break;
}
return TypographyProvider::Get()->ResolveTypographyToken(token);
}
} // namespace
//------------------------------------------------------------------------------
// SystemTextfield::EventHandler:
// Used to handle the case when user wants to commit the changes by clicking
// outside the textfield.
// TODO(b/312226702): should fix remaining issues: 1. it does not handle
// the touch event, 2. the changes can only be committed when clicking within
// the widget.
class SystemTextfield::EventHandler : public ui::EventHandler {
public:
explicit EventHandler(SystemTextfield* textfield) : textfield_(textfield) {
aura::Env::GetInstance()->AddPreTargetHandler(this);
}
EventHandler(const EventHandler&) = delete;
EventHandler& operator=(const EventHandler&) = delete;
~EventHandler() override {
aura::Env::GetInstance()->RemovePreTargetHandler(this);
}
// ui::EventHandler:
void OnMouseEvent(ui::MouseEvent* event) override { OnLocatedEvent(event); }
void OnTouchEvent(ui::TouchEvent* event) override { OnLocatedEvent(event); }
private:
void OnLocatedEvent(ui::LocatedEvent* event) {
if (!textfield_->IsActive()) {
return;
}
const ui::EventType event_type = event->type();
if (event_type != ui::EventType::kMousePressed) {
return;
}
// Do not handle the pre-target event if the context menu is showing.
if (textfield_->IsMenuShowing()) {
return;
}
// Get event location in screen.
gfx::Point event_location = event->location();
aura::Window* event_target = static_cast<aura::Window*>(event->target());
if (!aura::client::GetScreenPositionClient(event_target->GetRootWindow())) {
return;
}
wm::ConvertPointToScreen(event_target, &event_location);
const bool event_in_textfield =
textfield_->GetBoundsInScreen().Contains(event_location);
// If a clicking event happens outside the textfield, commit the
// changes and deactivate the textfield.
if (!event_in_textfield) {
textfield_->SetActive(false);
}
}
raw_ptr<SystemTextfield> textfield_;
};
//------------------------------------------------------------------------------
// SystemTextfield::SystemTextfield:
SystemTextfield::SystemTextfield(Type type)
: type_(type),
event_handler_(std::make_unique<EventHandler>(this)),
corner_radius_(kCornerRadius) {
SetFontList(GetFontListFromType(type_));
SetBorder(views::CreateEmptyBorder(kBorderInsets));
// Remove the default hover effect, since the hover effect of system textfield
// appears not only on hover but also on focus.
RemoveHoverEffect();
// Override the very round highlight path set in `views::Textfield`.
views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
corner_radius_);
// Configure focus ring.
auto* focus_ring = views::FocusRing::Get(this);
DCHECK(focus_ring);
focus_ring->SetOutsetFocusRingDisabled(true);
const float halo_thickness = focus_ring->GetHaloThickness();
focus_ring->SetHaloInset(-kFocusRingGap - 0.5f * halo_thickness);
focus_ring->SetColorId(cros_tokens::kCrosSysFocusRing);
focus_ring->SetHasFocusPredicate(base::BindRepeating(
[](const SystemTextfield* textfield, const views::View* view) {
return textfield->show_focus_ring_;
},
base::Unretained(this)));
enabled_changed_subscription_ = AddEnabledChangedCallback(base::BindRepeating(
&SystemTextfield::OnEnabledStateChanged, base::Unretained(this)));
}
SystemTextfield::~SystemTextfield() = default;
void SystemTextfield::SetTextColorId(ui::ColorId color_id) {
UpdateColorId(text_color_id_, color_id, /*is_background_color=*/false);
}
void SystemTextfield::SetSelectedTextColorId(ui::ColorId color_id) {
UpdateColorId(selected_text_color_id_, color_id,
/*is_background_color=*/false);
}
void SystemTextfield::SetSelectionBackgroundColorId(ui::ColorId color_id) {
UpdateColorId(selection_background_color_id_, color_id,
/*is_background_color=*/false);
}
void SystemTextfield::SetBackgroundColorId(ui::ColorId color_id) {
UpdateColorId(background_color_id_, color_id, /*is_background_color=*/true);
}
void SystemTextfield::SetPlaceholderTextColorId(ui::ColorId color_id) {
UpdateColorId(placeholder_text_color_id_, color_id,
/*is_background_color=*/false);
}
void SystemTextfield::SetActiveStateChangedCallback(
base::RepeatingClosure callback) {
active_state_changed_callback_ = std::move(callback);
}
void SystemTextfield::SetCornerRadius(int corner_radius) {
corner_radius_ = corner_radius;
views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
corner_radius_);
UpdateBackground();
}
void SystemTextfield::SetActive(bool active) {
if (IsActive() == active) {
return;
}
if (active) {
// Activate the textfield and record the text content.
views::Textfield::OnFocus();
restored_text_content_ = GetText();
} else {
// Clear selection when the textfield is deactivated.
ClearSelection();
views::Textfield::OnBlur();
}
SetShowFocusRing(active);
UpdateBackground();
if (active_state_changed_callback_) {
active_state_changed_callback_.Run();
}
}
bool SystemTextfield::IsActive() const {
return GetRenderText()->focused();
}
void SystemTextfield::SetShowFocusRing(bool show) {
if (show_focus_ring_ == show) {
return;
}
show_focus_ring_ = show;
// It's possible that derived classes could have removed the focus ring.
if (auto* focus_ring = views::FocusRing::Get(this); focus_ring != nullptr) {
focus_ring->SetOutsetFocusRingDisabled(true);
focus_ring->SchedulePaint();
}
}
void SystemTextfield::SetShowBackground(bool show) {
show_background_ = show;
UpdateBackground();
}
void SystemTextfield::RestoreText() {
SetText(restored_text_content_);
}
void SystemTextfield::UpdateBackground() {
const bool has_background =
GetBackgroundEnabled() &&
(IsMouseHovered() || HasFocus() || show_background_);
if (!has_background) {
SetBackground(nullptr);
return;
}
SetBackground(views::CreateThemedRoundedRectBackground(
background_color_id_.value_or(cros_tokens::kCrosSysHoverOnSubtle),
corner_radius_));
}
gfx::Size SystemTextfield::CalculatePreferredSize(
const views::SizeBounds& available_size) const {
// The width of container equals to the content width with horizontal padding.
// The height of the container dependents on the type.
const std::u16string& text = GetText();
int width = 0;
int height = 0;
gfx::Canvas::SizeStringInt(text.empty() ? GetPlaceholderText() : text,
GetFontListFromType(type_), &width, &height, 0,
gfx::Canvas::NO_ELLIPSIS);
return gfx::Size(
std::max(width + GetCaretBounds().width() + GetInsets().width(),
kMinWidth),
GetContainerHeightFromType(type_));
}
void SystemTextfield::SetBorder(std::unique_ptr<views::Border> b) {
// The base `Textfield` has a preset border. When a new border is set, the
// focus ring will be removed. The `SystemTextfield` needs an empty border for
// horizontal padding and keeps the focus ring.
views::View::SetBorder(std::move(b));
}
void SystemTextfield::OnMouseEntered(const ui::MouseEvent& event) {
UpdateBackground();
}
void SystemTextfield::OnMouseExited(const ui::MouseEvent& event) {
UpdateBackground();
}
void SystemTextfield::OnThemeChanged() {
views::View::OnThemeChanged();
// Only update the text color since the background color will be handled by
// themed background.
UpdateTextColor();
}
void SystemTextfield::OnFocus() {
SetActive(true);
}
void SystemTextfield::OnBlur() {
// TODO(b/323054951): Remove this when we can correctly handle our peculiar
// blur logic.
UpdateCursorVisibility();
// Call SetActive last because some callbacks might delete `this`.
SetActive(false);
}
void SystemTextfield::OnEnabledStateChanged() {
UpdateBackground();
UpdateTextColor();
SchedulePaint();
}
void SystemTextfield::UpdateColorId(std::optional<ui::ColorId>& src,
ui::ColorId dst,
bool is_background_color) {
if (src && *src == dst) {
return;
}
src = dst;
if (is_background_color) {
UpdateBackground();
} else {
UpdateTextColor();
}
}
void SystemTextfield::UpdateTextColor() {
if (!GetWidget()) {
return;
}
// Set text color.
auto* color_provider = GetColorProvider();
gfx::RenderText* render_text = GetRenderText();
if (!GetEnabled()) {
SetColor(color_provider->GetColor(cros_tokens::kCrosSysDisabled));
return;
}
// Set text color and selection text and background (highlight part) colors.
SetColor(color_provider->GetColor(
text_color_id_.value_or(cros_tokens::kCrosSysOnSurface)));
render_text->set_selection_color(color_provider->GetColor(
selected_text_color_id_.value_or(cros_tokens::kCrosSysOnSurface)));
render_text->set_selection_background_focused_color(
color_provider->GetColor(selection_background_color_id_.value_or(
cros_tokens::kCrosSysHighlightText)));
// Set placeholder text color
set_placeholder_text_color(color_provider->GetColor(
placeholder_text_color_id_.value_or(cros_tokens::kCrosSysDisabled)));
}
BEGIN_METADATA(SystemTextfield)
END_METADATA
} // namespace ash