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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
// Copyright 2023 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/ash/arc/input_overlay/ui/touch_point.h"
#include <cmath>
#include "base/notreached.h"
#include "cc/paint/paint_flags.h"
#include "chrome/browser/ash/arc/input_overlay/actions/action.h"
#include "chrome/browser/ash/arc/input_overlay/constants.h"
#include "chrome/browser/ash/arc/input_overlay/db/proto/app_data.pb.h"
#include "chrome/browser/ash/arc/input_overlay/ui/action_view.h"
#include "chrome/browser/ash/arc/input_overlay/ui/ui_utils.h"
#include "chrome/browser/ash/arc/input_overlay/util.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/point.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
namespace arc::input_overlay {
namespace {
// `TouchPoint` consists of outside stroke, inside stroke and center.
constexpr size_t kTouchPointComponentSize = 3u;
constexpr int kDotCenterDiameter = 14;
constexpr int kDotInsideStrokeThickness = 1;
constexpr int kDotOutsideStrokeThickness = 3;
constexpr int kCrossCenterLength = 78;
constexpr int kCrossCenterMiddleLength = 30;
constexpr int kCrossInsideStrokeThickness = 1;
constexpr int kCrossOutsideStrokeThickness = 4;
constexpr int kCrossCornerRadius = 6;
// Gap between focus ring outer edge to label.
constexpr float kHaloInset = -6;
// Thickness of focus ring.
constexpr float kHaloThickness = 3;
// Draw the cross shape path with round corner. It starts from bottom to up on
// line #0 and draws clock-wisely.
// `overall_length` is the total length of one side excluding the stroke
// thickness. `mid_length` is the length of the middle part which is close to
// the one third of `overall_length`.
// __
// _0^ |__
// |__ __|
// |__|
//
SkPath DrawCrossPath(SkScalar overall_length,
SkScalar mid_length,
SkScalar corner_radius,
SkScalar out_stroke_thickness,
SkPoint center) {
SkPath path;
SkScalar short_length = (overall_length - mid_length) / 2;
path.moveTo(center.x() - mid_length / 2, center.y() - mid_length / 2);
// #0
path.rLineTo(0, -(short_length - corner_radius));
path.rArcTo(corner_radius, corner_radius, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, corner_radius, -corner_radius);
// #1
path.rLineTo(mid_length - 2 * corner_radius, 0);
path.rArcTo(corner_radius, corner_radius, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, corner_radius, corner_radius);
// #2
path.rLineTo(0, short_length - corner_radius);
// #3
path.rLineTo(short_length - corner_radius, 0);
path.rArcTo(corner_radius, corner_radius, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, corner_radius, corner_radius);
// #4
path.rLineTo(0, mid_length - 2 * corner_radius);
path.rArcTo(corner_radius, corner_radius, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, -corner_radius, +corner_radius);
// #5
path.rLineTo(-(short_length - corner_radius), 0);
// #6
path.rLineTo(0, short_length - corner_radius);
path.rArcTo(corner_radius, corner_radius, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, -corner_radius, corner_radius);
// #7
path.rLineTo(-(mid_length - 2 * corner_radius), 0);
path.rArcTo(corner_radius, corner_radius, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, -corner_radius, -corner_radius);
// #8
path.rLineTo(0, -(short_length - corner_radius));
// #9
path.rLineTo(-(short_length - corner_radius), 0);
path.rArcTo(corner_radius, corner_radius, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, -corner_radius, -corner_radius);
// #10
path.rLineTo(0, -(mid_length - 2 * corner_radius));
path.rArcTo(corner_radius, corner_radius, 0, SkPath::kSmall_ArcSize,
SkPathDirection::kCW, corner_radius, -corner_radius);
// #11
path.close();
return path;
}
SkPath DrawCrossCenter(const gfx::Point& center) {
return DrawCrossPath(
/*overall_length=*/SkIntToScalar(kCrossCenterLength),
/*mid_length=*/SkIntToScalar(kCrossCenterMiddleLength),
/*corner_radius=*/SkIntToScalar(kCrossCornerRadius),
/*out_stroke_thickness=*/
SkIntToScalar(kCrossOutsideStrokeThickness + kCrossInsideStrokeThickness),
/*center=*/SkPoint::Make(center.x(), center.y()));
}
SkPath DrawCrossOutsideStroke(const gfx::Point& center) {
return DrawCrossPath(
/*overall_length=*/SkIntToScalar(kCrossCenterLength +
2 * kCrossInsideStrokeThickness),
/*mid_length=*/
SkIntToScalar(kCrossCenterMiddleLength + 2 * kCrossInsideStrokeThickness),
/*corner_radius=*/SkIntToScalar(kCrossCornerRadius),
/*out_stroke_thickness=*/SkIntToScalar(kCrossOutsideStrokeThickness),
/*center=*/SkPoint::Make(center.x(), center.y()));
}
SkColor GetOutsideStrokeColor(const ui::ColorProvider* color_provider,
UIState ui_state) {
switch (ui_state) {
case UIState::kDefault:
case UIState::kHover:
return SkColorSetA(SK_ColorWHITE, GetAlpha(/*percent=*/0.8f));
case UIState::kDrag:
return color_provider->GetColor(
cros_tokens::kCrosSysGamingControlButtonBorderHover);
default:
NOTREACHED();
}
}
SkColor GetInsideStrokeColor(const ui::ColorProvider* color_provider,
UIState ui_state) {
switch (ui_state) {
case UIState::kDefault:
return SkColorSetA(SK_ColorBLACK, GetAlpha(/*percent=*/0.2f));
case UIState::kHover:
return SkColorSetA(SK_ColorBLACK, GetAlpha(/*percent=*/0.2f));
case UIState::kDrag:
return SkColorSetA(SK_ColorBLACK, GetAlpha(/*percent=*/0.4f));
default:
NOTREACHED();
}
}
SkColor GetCenterColor(const ui::ColorProvider* color_provider,
UIState ui_state) {
switch (ui_state) {
case UIState::kDefault:
return color_provider->GetColor(
cros_tokens::kCrosSysGamingControlButtonDefault);
case UIState::kHover:
case UIState::kDrag:
return color_provider->GetColor(
cros_tokens::kCrosSysGamingControlButtonHover);
default:
NOTREACHED();
}
}
std::array<SkColor, kTouchPointComponentSize> GetColors(
const ui::ColorProvider* color_provider,
UIState ui_state) {
return {GetOutsideStrokeColor(color_provider, ui_state),
GetInsideStrokeColor(color_provider, ui_state),
GetCenterColor(color_provider, ui_state)};
}
class CrossTouchPoint : public TouchPoint {
public:
explicit CrossTouchPoint(const gfx::Point& center_pos)
: TouchPoint(center_pos) {}
~CrossTouchPoint() override = default;
// TouchPoint:
void Init() override {
GetViewAccessibility().SetRole(ax::mojom::Role::kGroup);
GetViewAccessibility().SetName(l10n_util::GetStringUTF16(
IDS_INPUT_OVERLAY_KEYMAPPING_TOUCH_POINT_CROSS));
TouchPoint::Init();
}
// views::View:
gfx::Size CalculatePreferredSize(
const views::SizeBounds& available_size) const override {
return GetSize(ActionType::MOVE);
}
void OnPaintBackground(gfx::Canvas* canvas) override {
PaintBackground(canvas, ActionType::MOVE);
}
};
class DotTouchPoint : public TouchPoint {
public:
explicit DotTouchPoint(const gfx::Point& center_pos)
: TouchPoint(center_pos) {}
~DotTouchPoint() override = default;
// TouchPoint:
void Init() override {
GetViewAccessibility().SetRole(ax::mojom::Role::kGroup);
GetViewAccessibility().SetName(l10n_util::GetStringUTF16(
IDS_INPUT_OVERLAY_KEYMAPPING_TOUCH_POINT_DOT));
TouchPoint::Init();
}
// views::View:
gfx::Size CalculatePreferredSize(
const views::SizeBounds& available_size) const override {
return GetSize(ActionType::TAP);
}
void OnPaintBackground(gfx::Canvas* canvas) override {
PaintBackground(canvas, ActionType::TAP);
}
};
} // namespace
// static
TouchPoint* TouchPoint::Show(views::View* parent,
ActionType action_type,
const gfx::Point& center_pos) {
std::unique_ptr<TouchPoint> touch_point;
switch (action_type) {
case ActionType::TAP:
touch_point = std::make_unique<DotTouchPoint>(center_pos);
break;
case ActionType::MOVE:
touch_point = std::make_unique<CrossTouchPoint>(center_pos);
break;
default:
NOTREACHED();
}
auto* touch_point_ptr =
parent->AddChildViewAt(std::move(touch_point), /*index=*/0);
touch_point_ptr->Init();
return touch_point_ptr;
}
// static
int TouchPoint::GetEdgeLength(ActionType action_type) {
int length = 0;
switch (action_type) {
case ActionType::TAP:
length = kDotCenterDiameter + kDotInsideStrokeThickness * 2 +
kDotOutsideStrokeThickness * 2;
break;
case ActionType::MOVE:
length = kCrossCenterLength + kCrossInsideStrokeThickness * 2 +
kCrossOutsideStrokeThickness * 2;
break;
default:
NOTREACHED();
}
return length;
}
// static
gfx::Size TouchPoint::GetSize(ActionType action_type) {
const int edge_length = GetEdgeLength(action_type);
return gfx::Size(edge_length, edge_length);
}
// static
void TouchPoint::DrawTouchPoint(gfx::Canvas* canvas,
const ui::ColorProvider* color_provider,
ActionType action_type,
UIState ui_state,
const gfx::Point& center) {
DCHECK(canvas);
DCHECK(color_provider);
cc::PaintFlags flags;
flags.setAntiAlias(true);
const auto colors = GetColors(color_provider, ui_state);
DCHECK_EQ(colors.size(), kTouchPointComponentSize);
switch (action_type) {
case ActionType::TAP: {
const int radius = kDotCenterDiameter / 2;
const int center_x = center.x();
const int center_y = center.y();
// Draw outside stroke.
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(kDotOutsideStrokeThickness);
flags.setColor(colors[0]);
canvas->DrawCircle(gfx::Point(center_x, center_y),
radius + kDotInsideStrokeThickness, flags);
// Draw center.
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(colors[2]);
canvas->DrawCircle(gfx::Point(center_x, center_y), radius, flags);
// Draw inside stroke.
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(kDotInsideStrokeThickness);
flags.setColor(colors[1]);
canvas->DrawCircle(gfx::Point(center_x, center_y), radius, flags);
} break;
case ActionType::MOVE:
// Draw outside stroke.
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(kCrossOutsideStrokeThickness);
flags.setColor(colors[0]);
canvas->DrawPath(DrawCrossOutsideStroke(center), flags);
// Draw center.
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(colors[2]);
canvas->DrawPath(DrawCrossCenter(center), flags);
// Draw inside stroke.
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(kCrossInsideStrokeThickness);
flags.setColor(colors[1]);
canvas->DrawPath(DrawCrossCenter(center), flags);
break;
default:
NOTREACHED();
}
}
TouchPoint::TouchPoint(const gfx::Point& center_pos)
: center_pos_(center_pos) {}
TouchPoint::~TouchPoint() = default;
void TouchPoint::Init() {
SizeToPreferredSize();
SetPosition(gfx::Point(std::max(0, center_pos_.x() - size().width() / 2),
std::max(0, center_pos_.y() - size().height() / 2)));
SetFocusBehavior(FocusBehavior::ALWAYS);
views::FocusRing::Install(this);
auto* focus_ring = views::FocusRing::Get(this);
focus_ring->SetColorId(ui::kColorAshInputOverlayFocusRing);
focus_ring->SetHaloInset(kHaloInset);
focus_ring->SetHaloThickness(kHaloThickness);
}
void TouchPoint::OnCenterPositionChanged(const gfx::Point& point) {
center_pos_ = point;
SetPosition(gfx::Point(std::max(0, center_pos_.x() - size().width() / 2),
std::max(0, center_pos_.y() - size().height() / 2)));
}
ui::Cursor TouchPoint::GetCursor(const ui::MouseEvent& event) {
return ui::mojom::CursorType::kHand;
}
void TouchPoint::OnMouseEntered(const ui::MouseEvent& event) {
SetToHover();
}
void TouchPoint::OnMouseExited(const ui::MouseEvent& event) {
SetToDefault();
}
bool TouchPoint::OnMousePressed(const ui::MouseEvent& event) {
if (auto* parent_view = views::AsViewClass<ActionView>(parent())) {
parent_view->ApplyMousePressed(event);
}
return true;
}
bool TouchPoint::OnMouseDragged(const ui::MouseEvent& event) {
// widget is null for test.
if (auto* widget = GetWidget()) {
widget->SetCursor(ui::mojom::CursorType::kGrabbing);
}
SetToDrag();
views::AsViewClass<ActionView>(parent())->ApplyMouseDragged(event);
return true;
}
void TouchPoint::OnMouseReleased(const ui::MouseEvent& event) {
// widget is null for test.
if (auto* widget = GetWidget()) {
widget->SetCursor(ui::mojom::CursorType::kGrab);
}
SetToHover();
if (auto* parent_view = views::AsViewClass<ActionView>(parent())) {
parent_view->ApplyMouseReleased(event);
}
}
void TouchPoint::OnGestureEvent(ui::GestureEvent* event) {
switch (event->type()) {
case ui::EventType::kGestureScrollBegin:
SetToDrag();
event->SetHandled();
break;
case ui::EventType::kGestureScrollEnd:
case ui::EventType::kScrollFlingStart:
SetToDefault();
event->SetHandled();
break;
default:
break;
}
if (auto* parent_view = views::AsViewClass<ActionView>(parent())) {
parent_view->ApplyGestureEvent(event);
}
}
bool TouchPoint::OnKeyPressed(const ui::KeyEvent& event) {
if (auto* parent_view = views::AsViewClass<ActionView>(parent())) {
return parent_view->ApplyKeyPressed(event);
}
return false;
}
bool TouchPoint::OnKeyReleased(const ui::KeyEvent& event) {
if (auto* parent_view = views::AsViewClass<ActionView>(parent())) {
return parent_view->ApplyKeyReleased(event);
}
return false;
}
void TouchPoint::OnFocus() {
if (auto* parent_view = views::AsViewClass<ActionView>(parent())) {
parent_view->ShowFocusInfoMsg(
l10n_util::GetStringUTF8(
IDS_INPUT_OVERLAY_EDIT_INSTRUCTIONS_TOUCH_POINT_FOCUS),
this);
}
}
void TouchPoint::PaintBackground(gfx::Canvas* canvas, ActionType action_type) {
const auto size = GetSize(action_type);
DrawTouchPoint(canvas, GetColorProvider(), action_type, ui_state_,
/*center=*/gfx::Point(size.width() / 2, size.height() / 2));
}
void TouchPoint::SetToDefault() {
if (ui_state_ == UIState::kDefault) {
return;
}
ui_state_ = UIState::kDefault;
SchedulePaint();
}
void TouchPoint::SetToHover() {
if (ui_state_ == UIState::kHover) {
return;
}
ui_state_ = UIState::kHover;
SchedulePaint();
}
void TouchPoint::SetToDrag() {
if (ui_state_ == UIState::kDrag) {
return;
}
ui_state_ = UIState::kDrag;
SchedulePaint();
}
BEGIN_METADATA(TouchPoint)
END_METADATA
} // namespace arc::input_overlay
|