File: target_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (283 lines) | stat: -rw-r--r-- 9,094 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
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
// 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/target_view.h"

#include <algorithm>

#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/display_overlay_controller.h"
#include "chrome/browser/ash/arc/input_overlay/touch_injector.h"
#include "chrome/browser/ash/arc/input_overlay/ui/touch_point.h"
#include "chrome/browser/ash/arc/input_overlay/ui/ui_utils.h"
#include "chromeos/strings/grit/chromeos_strings.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_provider.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/widget/widget.h"

namespace arc::input_overlay {

namespace {

constexpr int kDashedLineStrokeWidth = 1;
constexpr int kDashLength = 2;
constexpr int kDashGap = 4;

constexpr int kCircleRingStroke = 3;

constexpr int kActionTapCircleRadius = 37;
constexpr int kActionTapCircleRingRadius = 29;  // 32 - inside stroke 3

constexpr int kActionMoveCircleRadius = 69;
constexpr int kActionMoveCircleRingRadius = 61;  // 64 - inside stroke 3

// Returns true if `val` is inside of the circle with `radius` and `center`.
bool InsideOfCircle(const int radius, const int center, const int val) {
  return val >= center - radius && val < center + radius;
}

// Clamps `start` and `end` oustside of the circle with `radius` and `center`.
void ClampDashLineInCircle(const int radius,
                           const int center,
                           const int limit,
                           int& start,
                           int& end) {
  if (InsideOfCircle(radius, center, start) &&
      InsideOfCircle(radius, center, end)) {
    // Whole dash line segment of `kDashLength` is inside of the circle.
    start = end = 0;
  } else if (InsideOfCircle(radius, center, start)) {
    // `start` is inside of the circle.
    start = center + radius;
  } else if (InsideOfCircle(radius, center, end)) {
    // `end` is inside of the circle.
    end = center - radius;
  }

  start = std::clamp(start, 0, limit);
  end = std::clamp(end, 0, limit);
}

}  // namespace

TargetView::TargetView(DisplayOverlayController* controller,
                       ActionType action_type)
    : controller_(controller), action_type_(action_type) {
  const auto* touch_injector = controller_->touch_injector();
  DCHECK(touch_injector);
  const auto& bounds = touch_injector->content_bounds();
  center_.set_x(bounds.width() / 2);
  center_.set_y(bounds.height() / 2);
  SetFocusBehavior(FocusBehavior::ALWAYS);
  GetViewAccessibility().SetRole(ax::mojom::Role::kPane);
  GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(IDS_INPUT_OVERLAY_BUTTON_PLACEMENT_A11Y_LABEL));
}

TargetView::~TargetView() = default;

void TargetView::UpdateWidgetBounds() {
  auto* widget = GetWidget();
  DCHECK(widget);

  controller_->UpdateWidgetBoundsInRootWindow(
      widget, controller_->touch_injector()->content_bounds());
}

gfx::Rect TargetView::GetTargetCircleBounds() const {
  gfx::Rect bounds = gfx::Rect(center_.x(), center_.y(), 0, 0);
  bounds.Outset(GetCircleRadius());
  return bounds;
}

int TargetView::GetCircleRadius() const {
  switch (action_type_) {
    case ActionType::TAP:
      return kActionTapCircleRadius;
    case ActionType::MOVE:
      return kActionMoveCircleRadius;
    default:
      NOTREACHED();
  }
}

int TargetView::GetCircleRingRadius() const {
  switch (action_type_) {
    case ActionType::TAP:
      return kActionTapCircleRingRadius;
    case ActionType::MOVE:
      return kActionMoveCircleRingRadius;
    default:
      NOTREACHED();
  }
}

int TargetView::GetPadding() const {
  return TouchPoint::GetEdgeLength(action_type_) / 2;
}

void TargetView::ClampCenter() {
  const int padding = GetPadding();
  const auto& view_size = size();
  center_.set_x(std::clamp(center_.x(), /*lo=*/padding,
                           /*hi=*/view_size.width() - padding));
  center_.set_y(std::clamp(center_.y(), /*lo=*/padding,
                           /*hi=*/view_size.height() - padding));
}

void TargetView::OnCenterChanged() {
  ClampCenter();
  SchedulePaint();
  controller_->UpdateButtonPlacementNudgeAnchorRect();
}

void TargetView::MoveCursorToViewCenter() {
  auto* widget = GetWidget();
  DCHECK(widget);
  auto* window = widget->GetNativeWindow();
  DCHECK(window);
  window->MoveCursorTo(bounds().CenterPoint());
}

void TargetView::VisibilityChanged(views::View* starting_from,
                                   bool is_visible) {
  if (is_visible) {
    UpdateWidgetBounds();
    RequestFocus();
    MoveCursorToViewCenter();
  }
}

void TargetView::OnGestureEvent(ui::GestureEvent* event) {
  auto type = event->type();
  center_ = event->location();
  OnCenterChanged();
  event->SetHandled();

  // When the gesture event is released, add a new action.
  if (type == ui::EventType::kGestureScrollEnd ||
      type == ui::EventType::kScrollFlingStart ||
      type == ui::EventType::kGesturePinchEnd ||
      type == ui::EventType::kGestureEnd) {
    controller_->AddNewAction(action_type_, center_);
  }
}

bool TargetView::OnKeyPressed(const ui::KeyEvent& event) {
  const auto key_code = event.key_code();

  // Exit the button placement mode when key `esc` is pressed.
  if (key_code == ui::VKEY_ESCAPE) {
    controller_->ExitButtonPlaceMode(/*is_action_added=*/false);
    return true;
  }

  // Continue to add new action if key `enter` is pressed.
  if (key_code == ui::VKEY_RETURN) {
    controller_->AddNewAction(action_type_, center_);
    return true;
  }

  // Update `center_` and repaint if arrow keys are pressed.
  if (OffsetPositionByArrowKey(key_code, center_)) {
    OnCenterChanged();
    return true;
  }

  return false;
}

void TargetView::OnMouseMoved(const ui::MouseEvent& event) {
  center_ = event.location();
  OnCenterChanged();
}

bool TargetView::OnMousePressed(const ui::MouseEvent& event) {
  // Considered as consumed. If the mouse is clicked, a new action is added when
  // the mouse button is released.
  return true;
}

void TargetView::OnMouseReleased(const ui::MouseEvent& event) {
  controller_->AddNewAction(action_type_, center_);
}

void TargetView::OnPaintBackground(gfx::Canvas* canvas) {
  cc::PaintFlags flags;
  flags.setAntiAlias(true);
  const auto* color_provider = GetColorProvider();

  const int circle_ring_radius = GetCircleRingRadius();
  const int circle_ring_overall_radius = circle_ring_radius + kCircleRingStroke;
  const int center_x = center_.x();
  const int center_y = center_.y();

  // Draw background circle.
  flags.setStyle(cc::PaintFlags::kFill_Style);
  flags.setColor(
      SkColorSetA(color_provider->GetColor(cros_tokens::kCrosSysPrimary),
                  GetAlpha(/*percent=*/0.8f)));
  canvas->DrawCircle(center_, GetCircleRadius(), flags);

  // Draw dashed crossed lines.
  flags.setStyle(cc::PaintFlags::kStroke_Style);
  flags.setStrokeWidth(kDashedLineStrokeWidth);
  flags.setStrokeCap(cc::PaintFlags::Cap::kRound_Cap);
  flags.setColor(color_provider->GetColor(cros_tokens::kCrosSysWhite));
  const auto& view_size = size();
  const int width = view_size.width();
  const int height = view_size.height();
  // Draw the horizontal dashed line.
  for (int x = 0; x < width; x += kDashGap + kDashLength) {
    int start = x;
    int end = x + kDashLength;
    ClampDashLineInCircle(circle_ring_overall_radius, center_x, width, start,
                          end);
    if (start < end) {
      canvas->DrawLine(gfx::Point(start, center_y), gfx::Point(end, center_y),
                       flags);
    }
  }
  // Draw the vertical dashed line.
  for (int y = 0; y < height; y += kDashGap + kDashLength) {
    int start = y;
    int end = y + kDashLength;
    ClampDashLineInCircle(circle_ring_overall_radius, center_y, height, start,
                          end);
    if (start < end) {
      canvas->DrawLine(gfx::Point(center_x, start), gfx::Point(center_x, end),
                       flags);
    }
  }

  // Draw the white circle ring.
  flags.setStyle(cc::PaintFlags::kStroke_Style);
  flags.setColor(color_provider->GetColor(cros_tokens::kCrosSysWhite));
  flags.setStrokeWidth(kCircleRingStroke);
  canvas->DrawCircle(center_, circle_ring_radius, flags);

  // Draw the touch point.
  TouchPoint::DrawTouchPoint(canvas, color_provider, action_type_,
                             UIState::kDefault, center_);
}

void TargetView::OnThemeChanged() {
  views::View::OnThemeChanged();
  // Repaint to update colors.
  SchedulePaint();
}

BEGIN_METADATA(TargetView)
END_METADATA
}  // namespace arc::input_overlay