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
|
// Copyright 2019 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/system/accessibility/autoclick_scroll_view.h"
#include "ash/accessibility/autoclick/autoclick_controller.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/system/accessibility/autoclick_menu_bubble_controller.h"
#include "ash/system/accessibility/floating_menu_button.h"
#include "ash/system/unified/custom_shape_button.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ref.h"
#include "base/metrics/user_metrics.h"
#include "base/timer/timer.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/masked_targeter_delegate.h"
#include "ui/views/view.h"
namespace ash {
namespace {
// Constants for size and position.
constexpr int kScrollButtonCloseSizeDips = 48;
constexpr int kScrollpadStrokeWidthDips = 2;
constexpr int kScrollPadButtonHypotenuseDips = 192;
constexpr int kScrollPadIconPadding = 30;
} // namespace
// The close button for the automatic clicks scroll bubble.
class AutoclickScrollCloseButton : public FloatingMenuButton {
METADATA_HEADER(AutoclickScrollCloseButton, FloatingMenuButton)
public:
AutoclickScrollCloseButton()
: FloatingMenuButton(
base::BindRepeating(&AutoclickScrollCloseButton::OnButtonPressed,
base::Unretained(this)),
kAutoclickCloseIcon,
IDS_ASH_AUTOCLICK_SCROLL_CLOSE,
/*flip_for_rtl=*/false,
kScrollButtonCloseSizeDips,
/*draw_highlight=*/false,
/*is_a11y_togglable=*/false) {
views::View::SetID(
static_cast<int>(AutoclickScrollView::ButtonId::kCloseScroll));
}
AutoclickScrollCloseButton(const AutoclickScrollCloseButton&) = delete;
AutoclickScrollCloseButton& operator=(const AutoclickScrollCloseButton&) =
delete;
~AutoclickScrollCloseButton() override = default;
void OnButtonPressed() {
Shell::Get()->autoclick_controller()->DoScrollAction(
AutoclickController::ScrollPadAction::kScrollClose);
base::RecordAction(base::UserMetricsAction(
"Accessibility.Autoclick.ScrollMenu.CloseButton"));
}
// views::View:
void OnMouseEntered(const ui::MouseEvent& event) override {
hovered_ = true;
SchedulePaint();
}
void OnMouseExited(const ui::MouseEvent& event) override {
hovered_ = false;
SchedulePaint();
}
// FloatingMenuButton:
void PaintButtonContents(gfx::Canvas* canvas) override {
if (hovered_) {
gfx::Rect rect(GetContentsBounds());
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(GetColorProvider()->GetColor(kColorAshInkDrop));
canvas->DrawCircle(gfx::PointF(rect.CenterPoint()),
kScrollButtonCloseSizeDips / 2, flags);
}
views::ImageButton::PaintButtonContents(canvas);
}
private:
bool hovered_ = false;
};
BEGIN_METADATA(AutoclickScrollCloseButton)
END_METADATA
// A single scroll button (up/down/left/right) for automatic clicks scroll
// bubble. Subclasses a MaskedTargeterDelegate in order to only get events over
// the button's custom shape, rather than over the whole rectangle which
// encloses the button.
class AutoclickScrollButton : public CustomShapeButton,
public views::MaskedTargeterDelegate {
METADATA_HEADER(AutoclickScrollButton, CustomShapeButton)
public:
AutoclickScrollButton(AutoclickController::ScrollPadAction action,
const gfx::VectorIcon& icon,
int accessible_name_id,
AutoclickScrollView::ButtonId id)
: CustomShapeButton(
base::BindRepeating(&AutoclickScrollButton::OnButtonPressed,
base::Unretained(this))),
action_(action),
icon_(icon) {
views::View::SetID(static_cast<int>(id));
SetTooltipText(l10n_util::GetStringUTF16(accessible_name_id));
// Disable canvas flipping, as scroll left should always be left no matter
// the language orientation.
SetFlipCanvasOnPaintForRTLUI(false);
scroll_hover_timer_ = std::make_unique<base::RetainingOneShotTimer>(
FROM_HERE,
base::Milliseconds(
int64_t{AutoclickScrollView::kAutoclickScrollDelayMs}),
base::BindRepeating(&AutoclickScrollButton::DoScrollAction,
base::Unretained(this)));
if (action_ == AutoclickController::ScrollPadAction::kScrollLeft ||
action_ == AutoclickController::ScrollPadAction::kScrollRight) {
size_ = gfx::Size(kScrollPadButtonHypotenuseDips / 2,
kScrollPadButtonHypotenuseDips);
} else if (action_ == AutoclickController::ScrollPadAction::kScrollUp ||
action_ == AutoclickController::ScrollPadAction::kScrollDown) {
size_ = gfx::Size(kScrollPadButtonHypotenuseDips,
kScrollPadButtonHypotenuseDips / 2);
}
SetPreferredSize(size_);
SetImageModel(
views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(*icon_, kColorAshIconColorPrimary));
SetClipPath(CreateCustomShapePath(gfx::Rect(GetPreferredSize())));
SetEventTargeter(std::make_unique<views::ViewTargeter>(this));
views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(), 0.f);
}
AutoclickScrollButton(const AutoclickScrollButton&) = delete;
AutoclickScrollButton& operator=(const AutoclickScrollButton&) = delete;
~AutoclickScrollButton() override {
Shell::Get()->autoclick_controller()->OnExitedScrollButton();
}
void ProcessAction(AutoclickController::ScrollPadAction action) {
Shell::Get()->autoclick_controller()->DoScrollAction(action);
switch (action) {
case AutoclickController::ScrollPadAction::kScrollUp:
base::RecordAction(
base::UserMetricsAction("Accessibility.Autoclick.ScrollUp"));
return;
case AutoclickController::ScrollPadAction::kScrollDown:
base::RecordAction(
base::UserMetricsAction("Accessibility.Autoclick.ScrollDown"));
return;
case AutoclickController::ScrollPadAction::kScrollLeft:
base::RecordAction(
base::UserMetricsAction("Accessibility.Autoclick.ScrollLeft"));
return;
case AutoclickController::ScrollPadAction::kScrollRight:
base::RecordAction(
base::UserMetricsAction("Accessibility.Autoclick.ScrollRight"));
return;
default:
return;
}
}
void DoScrollAction() {
ProcessAction(action_);
// Reset the timer to continue to do the action as long as we are hovering.
scroll_hover_timer_->Reset();
}
void OnButtonPressed() { ProcessAction(action_); }
// CustomShapeButton:
SkPath CreateCustomShapePath(const gfx::Rect& bounds) const override {
return ComputePath(true /* all_edges */);
}
// Computes the path which is the outline of this button. If |all_edges|,
// returns a path which fully encloses the shape, otherwise just returns a
// path that can be used for drawing the edges but avoids overlap with
// neighboring buttons.
SkPath ComputePath(bool all_edges) const {
int height = kScrollPadButtonHypotenuseDips;
int width = height / 2;
int half_width = width / 2;
SkPath path;
if (all_edges) {
path.moveTo(0, 0);
path.lineTo(0, height);
} else {
path.moveTo(0, height);
}
// Move to the edge of the close button.
float offset = (kScrollButtonCloseSizeDips / 2.) / sqrt(2.);
path.lineTo(SkIntToScalar(width - int(offset)),
SkIntToScalar(width + int(offset)));
gfx::Rect oval =
gfx::Rect(width - kScrollButtonCloseSizeDips / 2,
width - kScrollButtonCloseSizeDips / 2,
kScrollButtonCloseSizeDips, kScrollButtonCloseSizeDips);
path.arcTo(gfx::RectToSkRect(oval), 135, 90, false);
if (all_edges) {
path.lineTo(0, 0);
}
if (action_ == AutoclickController::ScrollPadAction::kScrollLeft)
return path;
SkMatrix matrix;
if (action_ == AutoclickController::ScrollPadAction::kScrollUp) {
matrix.setRotate(90, half_width, width);
matrix.postTranslate(half_width, -half_width);
} else if (action_ == AutoclickController::ScrollPadAction::kScrollRight) {
matrix.setRotate(180, half_width, width);
} else if (action_ == AutoclickController::ScrollPadAction::kScrollDown) {
matrix.setRotate(270, half_width, width);
matrix.postTranslate(half_width, -half_width);
}
path.transform(matrix);
return path;
}
void PaintButtonContents(gfx::Canvas* canvas) override {
gfx::Rect rect(GetContentsBounds());
cc::PaintFlags flags;
flags.setAntiAlias(true);
if (active_) {
flags.setColor(GetColorProvider()->GetColor(kColorAshInkDrop));
flags.setStyle(cc::PaintFlags::kFill_Style);
canvas->DrawPath(CreateCustomShapePath(rect), flags);
}
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(kScrollpadStrokeWidthDips);
flags.setColor(GetColorProvider()->GetColor(kColorAshSeparatorColor));
canvas->DrawPath(ComputePath(false /* only drawn edges */), flags);
gfx::ImageSkia img = GetImageToPaint();
int halfImageSize = img.width() / 2;
gfx::Point position;
if (action_ == AutoclickController::ScrollPadAction::kScrollLeft) {
position = gfx::Point(kScrollPadIconPadding,
kScrollPadButtonHypotenuseDips / 2 - halfImageSize);
} else if (action_ == AutoclickController::ScrollPadAction::kScrollRight) {
position = gfx::Point(kScrollPadButtonHypotenuseDips / 2 - img.width() -
kScrollPadIconPadding,
kScrollPadButtonHypotenuseDips / 2 - halfImageSize);
} else if (action_ == AutoclickController::ScrollPadAction::kScrollUp) {
position = gfx::Point(kScrollPadButtonHypotenuseDips / 2 - halfImageSize,
kScrollPadIconPadding);
} else if (action_ == AutoclickController::ScrollPadAction::kScrollDown) {
position = gfx::Point(kScrollPadButtonHypotenuseDips / 2 - halfImageSize,
kScrollPadButtonHypotenuseDips / 2 - img.width() -
kScrollPadIconPadding);
}
canvas->DrawImageInt(img, position.x(), position.y());
}
// views::MaskedTargeterDelegate:
bool GetHitTestMask(SkPath* mask) const override {
DCHECK(mask);
gfx::Rect rect(GetContentsBounds());
mask->addPath(CreateCustomShapePath(rect));
return true;
}
// views::View:
void OnMouseEntered(const ui::MouseEvent& event) override {
// For the four scroll buttons, set the button to a hovered/active state
// when this happens. Also tell something (autoclick controller? autoclick
// scroll controller?) to start the timer that will cause a lot of scrolls
// to occur, "clicking" on itself multiple times or otherwise.
active_ = true;
scroll_hover_timer_->Reset();
Shell::Get()->autoclick_controller()->OnEnteredScrollButton();
SchedulePaint();
}
// TODO(katie): Determine if this is reliable enough, or if it might not fire
// in some cases.
void OnMouseExited(const ui::MouseEvent& event) override {
// For the four scroll buttons, unset the hover state when this happens.
active_ = false;
if (scroll_hover_timer_->IsRunning())
scroll_hover_timer_->Stop();
// Allow the Autoclick timer and widget to restart.
Shell::Get()->autoclick_controller()->OnExitedScrollButton();
SchedulePaint();
}
private:
const AutoclickController::ScrollPadAction action_;
gfx::Size size_;
std::unique_ptr<base::RetainingOneShotTimer> scroll_hover_timer_;
bool active_ = false;
const raw_ref<const gfx::VectorIcon> icon_;
};
BEGIN_METADATA(AutoclickScrollButton)
END_METADATA
// ------ AutoclickScrollBubbleView ------ //
AutoclickScrollBubbleView::AutoclickScrollBubbleView(
TrayBubbleView::InitParams init_params)
: TrayBubbleView(init_params) {}
AutoclickScrollBubbleView::~AutoclickScrollBubbleView() {}
void AutoclickScrollBubbleView::UpdateAnchorRect(
const gfx::Rect& rect,
views::BubbleBorder::Arrow arrow) {
ui::ScopedLayerAnimationSettings settings(
GetWidget()->GetLayer()->GetAnimator());
settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
settings.SetTransitionDuration(
base::Milliseconds(AutoclickMenuBubbleController::kAnimationDurationMs));
settings.SetTweenType(gfx::Tween::EASE_OUT);
// SetAnchorRect will resize, so set the arrow without reizing to avoid a
// double animation.
SetArrowWithoutResizing(arrow);
SetAnchorRect(rect);
}
void AutoclickScrollBubbleView::UpdateInsets(gfx::Insets insets) {
SetBubbleBorderInsets(insets);
}
bool AutoclickScrollBubbleView::IsAnchoredToStatusArea() const {
return false;
}
BEGIN_METADATA(AutoclickScrollBubbleView)
END_METADATA
// ------ AutoclickScrollView ------ //
AutoclickScrollView::AutoclickScrollView() {
SetPreferredSize(gfx::Size(kScrollPadButtonHypotenuseDips,
kScrollPadButtonHypotenuseDips));
close_scroll_button_ =
AddChildView(std::make_unique<AutoclickScrollCloseButton>());
scroll_up_button_ = AddChildView(std::make_unique<AutoclickScrollButton>(
AutoclickController::ScrollPadAction::kScrollUp, kAutoclickScrollUpIcon,
IDS_ASH_AUTOCLICK_SCROLL_UP, ButtonId::kScrollUp));
scroll_down_button_ = AddChildView(std::make_unique<AutoclickScrollButton>(
AutoclickController::ScrollPadAction::kScrollDown,
kAutoclickScrollDownIcon, IDS_ASH_AUTOCLICK_SCROLL_DOWN,
ButtonId::kScrollDown));
scroll_left_button_ = AddChildView(std::make_unique<AutoclickScrollButton>(
AutoclickController::ScrollPadAction::kScrollLeft,
kAutoclickScrollLeftIcon, IDS_ASH_AUTOCLICK_SCROLL_LEFT,
ButtonId::kScrollLeft));
scroll_right_button_ = AddChildView(std::make_unique<AutoclickScrollButton>(
AutoclickController::ScrollPadAction::kScrollRight,
kAutoclickScrollRightIcon, IDS_ASH_AUTOCLICK_SCROLL_RIGHT,
ButtonId::kScrollRight));
}
void AutoclickScrollView::Layout(PassKey) {
scroll_up_button_->SetBounds(0, 0, kScrollPadButtonHypotenuseDips,
kScrollPadButtonHypotenuseDips / 2);
scroll_down_button_->SetBounds(0, kScrollPadButtonHypotenuseDips / 2,
kScrollPadButtonHypotenuseDips,
kScrollPadButtonHypotenuseDips / 2);
// In RTL languages, the left and right buttons bounds should be inverted
// so that they still draw on the correct side of the screen.
gfx::Rect left_bounds(0, 0, kScrollPadButtonHypotenuseDips / 2,
kScrollPadButtonHypotenuseDips);
gfx::Rect right_bounds(kScrollPadButtonHypotenuseDips / 2, 0,
kScrollPadButtonHypotenuseDips / 2,
kScrollPadButtonHypotenuseDips);
scroll_left_button_->SetBoundsRect(base::i18n::IsRTL() ? right_bounds
: left_bounds);
scroll_right_button_->SetBoundsRect(base::i18n::IsRTL() ? left_bounds
: right_bounds);
close_scroll_button_->SetBounds(
kScrollPadButtonHypotenuseDips / 2 - kScrollButtonCloseSizeDips / 2,
kScrollPadButtonHypotenuseDips / 2 - kScrollButtonCloseSizeDips / 2,
kScrollButtonCloseSizeDips, kScrollButtonCloseSizeDips);
}
BEGIN_METADATA(AutoclickScrollView)
END_METADATA
} // namespace ash
|