forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelf_app_button.cc
753 lines (638 loc) · 24.9 KB
/
shelf_app_button.cc
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/shelf/shelf_app_button.h"
#include <algorithm>
#include <memory>
#include "ash/public/cpp/ash_constants.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_constants.h"
#include "ash/shelf/shelf_view.h"
#include "base/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/stl_util.h"
#include "base/time/time.h"
#include "chromeos/constants/chromeos_switches.h"
#include "skia/ext/image_operations.h"
#include "ui/base/ui_base_features.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/animation/animation_delegate.h"
#include "ui/gfx/animation/throb_animation.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/skbitmap_operations.h"
#include "ui/gfx/transform_util.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/animation/ink_drop_mask.h"
#include "ui/views/animation/square_ink_drop_ripple.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/painter.h"
namespace {
constexpr int kStatusIndicatorAttentionThrobDurationMS = 800;
constexpr int kStatusIndicatorMaxAnimationSeconds = 10;
constexpr int kStatusIndicatorRadiusDip = 2;
constexpr int kStatusIndicatorMaxSize = 10;
constexpr int kStatusIndicatorActiveSize = 8;
constexpr int kStatusIndicatorRunningSize = 4;
constexpr int kStatusIndicatorThickness = 2;
constexpr int kNotificationIndicatorRadiusDip = 7;
constexpr SkColor kIndicatorColor = SK_ColorWHITE;
// Slightly different colors and alpha in the new UI.
constexpr SkColor kIndicatorColorActive = kIndicatorColor;
constexpr SkColor kIndicatorColorRunning = SkColorSetA(SK_ColorWHITE, 0x7F);
// Shelf item ripple size.
constexpr int kInkDropLargeSize = 60;
// The time threshold before an item can be dragged.
constexpr int kDragTimeThresholdMs = 300;
// The time threshold before the ink drop should activate on a long press.
constexpr int kInkDropRippleActivationTimeMs = 650;
// The drag and drop app icon should get scaled by this factor.
constexpr float kAppIconScale = 1.2f;
// The drag and drop app icon scaling up or down animation transition duration.
constexpr int kDragDropAppIconScaleTransitionMs = 200;
// Simple AnimationDelegate that owns a single ThrobAnimation instance to
// keep all Draw Attention animations in sync.
class ShelfAppButtonAnimation : public gfx::AnimationDelegate {
public:
class Observer {
public:
virtual void AnimationProgressed() = 0;
protected:
virtual ~Observer() = default;
};
static ShelfAppButtonAnimation* GetInstance() {
static ShelfAppButtonAnimation* s_instance = new ShelfAppButtonAnimation();
return s_instance;
}
void AddObserver(Observer* observer) { observers_.AddObserver(observer); }
void RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
if (!observers_.might_have_observers())
animation_.Stop();
}
bool HasObserver(Observer* observer) const {
return observers_.HasObserver(observer);
}
SkAlpha GetAlpha() {
return GetThrobAnimation().CurrentValueBetween(SK_AlphaTRANSPARENT,
SK_AlphaOPAQUE);
}
double GetAnimation() { return GetThrobAnimation().GetCurrentValue(); }
private:
ShelfAppButtonAnimation() : animation_(this) {
animation_.SetThrobDuration(kStatusIndicatorAttentionThrobDurationMS);
animation_.SetTweenType(gfx::Tween::SMOOTH_IN_OUT);
}
~ShelfAppButtonAnimation() override = default;
gfx::ThrobAnimation& GetThrobAnimation() {
if (!animation_.is_animating()) {
animation_.Reset();
animation_.StartThrobbing(-1 /*throb indefinitely*/);
}
return animation_;
}
// gfx::AnimationDelegate
void AnimationProgressed(const gfx::Animation* animation) override {
if (animation != &animation_)
return;
if (!animation_.is_animating())
return;
for (auto& observer : observers_)
observer.AnimationProgressed();
}
gfx::ThrobAnimation animation_;
base::ObserverList<Observer>::Unchecked observers_;
DISALLOW_COPY_AND_ASSIGN(ShelfAppButtonAnimation);
};
} // namespace
namespace ash {
////////////////////////////////////////////////////////////////////////////////
// ShelfAppButton::AppNotificationIndicatorView
// The indicator which is activated when the app corresponding with this
// ShelfAppButton receives a notification.
class ShelfAppButton::AppNotificationIndicatorView : public views::View {
public:
explicit AppNotificationIndicatorView(SkColor indicator_color)
: indicator_color_(indicator_color) {}
~AppNotificationIndicatorView() override {}
void OnPaint(gfx::Canvas* canvas) override {
gfx::ScopedCanvas scoped(canvas);
canvas->SaveLayerAlpha(SK_AlphaOPAQUE);
DCHECK_EQ(width(), height());
DCHECK_EQ(kNotificationIndicatorRadiusDip, width() / 2);
const float dsf = canvas->UndoDeviceScaleFactor();
const int kStrokeWidthPx = 1;
gfx::PointF center = gfx::RectF(GetLocalBounds()).CenterPoint();
center.Scale(dsf);
// Fill the center.
cc::PaintFlags flags;
flags.setColor(indicator_color_);
flags.setAntiAlias(true);
canvas->DrawCircle(
center, dsf * kNotificationIndicatorRadiusDip - kStrokeWidthPx, flags);
// Stroke the border.
flags.setColor(SkColorSetA(SK_ColorBLACK, 0x4D));
flags.setStyle(cc::PaintFlags::kStroke_Style);
canvas->DrawCircle(
center, dsf * kNotificationIndicatorRadiusDip - kStrokeWidthPx / 2.0f,
flags);
}
private:
const SkColor indicator_color_;
DISALLOW_COPY_AND_ASSIGN(AppNotificationIndicatorView);
};
////////////////////////////////////////////////////////////////////////////////
// ShelfAppButton::AppStatusIndicatorView
class ShelfAppButton::AppStatusIndicatorView
: public views::View,
public ShelfAppButtonAnimation::Observer {
public:
AppStatusIndicatorView() : show_attention_(false), active_(false) {
// Make sure the events reach the parent view for handling.
set_can_process_events_within_subtree(false);
}
~AppStatusIndicatorView() override {
ShelfAppButtonAnimation::GetInstance()->RemoveObserver(this);
}
// views::View:
void OnPaint(gfx::Canvas* canvas) override {
gfx::ScopedCanvas scoped(canvas);
if (show_attention_) {
const SkAlpha alpha =
ShelfAppButtonAnimation::GetInstance()->HasObserver(this)
? ShelfAppButtonAnimation::GetInstance()->GetAlpha()
: SK_AlphaOPAQUE;
canvas->SaveLayerAlpha(alpha);
}
const float dsf = canvas->UndoDeviceScaleFactor();
gfx::PointF center = gfx::RectF(GetLocalBounds()).CenterPoint();
cc::PaintFlags flags;
// Active and running indicators look a little different in the new UI.
flags.setColor(active_ ? kIndicatorColorActive : kIndicatorColorRunning);
flags.setAntiAlias(true);
flags.setStrokeCap(cc::PaintFlags::Cap::kRound_Cap);
flags.setStrokeJoin(cc::PaintFlags::Join::kRound_Join);
flags.setStrokeWidth(kStatusIndicatorThickness);
flags.setStyle(cc::PaintFlags::kStroke_Style);
float stroke_length =
active_ ? kStatusIndicatorActiveSize : kStatusIndicatorRunningSize;
gfx::PointF start;
gfx::PointF end;
if (horizontal_shelf_) {
start = gfx::PointF(center.x() - stroke_length / 2, center.y());
end = start;
end.Offset(stroke_length, 0);
} else {
start = gfx::PointF(center.x(), center.y() - stroke_length / 2);
end = start;
end.Offset(0, stroke_length);
}
SkPath path;
path.moveTo(start.x() * dsf, start.y() * dsf);
path.lineTo(end.x() * dsf, end.y() * dsf);
canvas->DrawPath(path, flags);
}
// ShelfAppButtonAnimation::Observer
void AnimationProgressed() override {
UpdateAnimating();
SchedulePaint();
}
void ShowAttention(bool show) {
if (show_attention_ == show)
return;
show_attention_ = show;
if (show_attention_) {
animation_end_time_ =
base::TimeTicks::Now() +
base::TimeDelta::FromSeconds(kStatusIndicatorMaxAnimationSeconds);
ShelfAppButtonAnimation::GetInstance()->AddObserver(this);
} else {
ShelfAppButtonAnimation::GetInstance()->RemoveObserver(this);
}
}
void ShowActiveStatus(bool active) {
if (active_ == active)
return;
active_ = active;
SchedulePaint();
}
void SetHorizontalShelf(bool horizontal_shelf) {
if (horizontal_shelf_ == horizontal_shelf)
return;
horizontal_shelf_ = horizontal_shelf;
SchedulePaint();
}
private:
void UpdateAnimating() {
if (base::TimeTicks::Now() > animation_end_time_)
ShelfAppButtonAnimation::GetInstance()->RemoveObserver(this);
}
bool show_attention_ = false;
bool active_ = false;
bool horizontal_shelf_ = true;
base::TimeTicks animation_end_time_; // For attention throbbing underline.
DISALLOW_COPY_AND_ASSIGN(AppStatusIndicatorView);
};
////////////////////////////////////////////////////////////////////////////////
// ShelfAppButton
// static
const char ShelfAppButton::kViewClassName[] = "ash/ShelfAppButton";
ShelfAppButton::ShelfAppButton(ShelfView* shelf_view,
const base::string16& title)
: ShelfButton(shelf_view),
icon_view_(new views::ImageView()),
indicator_(new AppStatusIndicatorView()),
notification_indicator_(nullptr),
state_(STATE_NORMAL),
destroyed_flag_(nullptr),
is_notification_indicator_enabled_(
features::IsNotificationIndicatorEnabled()) {
SetTitle(title);
const gfx::ShadowValue kShadows[] = {
gfx::ShadowValue(gfx::Vector2d(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
gfx::ShadowValue(gfx::Vector2d(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
gfx::ShadowValue(gfx::Vector2d(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)),
};
icon_shadows_.assign(kShadows, kShadows + base::size(kShadows));
// TODO: refactor the layers so each button doesn't require 3.
// |icon_view_| needs its own layer so it can be scaled up independently of
// the ink drop ripple.
icon_view_->SetPaintToLayer();
icon_view_->layer()->SetFillsBoundsOpaquely(false);
icon_view_->SetHorizontalAlignment(views::ImageView::CENTER);
icon_view_->SetVerticalAlignment(views::ImageView::LEADING);
// Do not make this interactive, so that events are sent to ShelfView.
icon_view_->set_can_process_events_within_subtree(false);
AddChildView(indicator_);
AddChildView(icon_view_);
if (is_notification_indicator_enabled_) {
notification_indicator_ = new AppNotificationIndicatorView(kIndicatorColor);
notification_indicator_->SetPaintToLayer();
notification_indicator_->layer()->SetFillsBoundsOpaquely(false);
notification_indicator_->SetVisible(false);
AddChildView(notification_indicator_);
}
}
ShelfAppButton::~ShelfAppButton() {
if (destroyed_flag_)
*destroyed_flag_ = true;
}
void ShelfAppButton::SetShadowedImage(const gfx::ImageSkia& image) {
icon_view_->SetImage(gfx::ImageSkiaOperations::CreateImageWithDropShadow(
image, icon_shadows_));
}
void ShelfAppButton::SetTitle(const base::string16 title) {
SetAccessibleName(title);
}
void ShelfAppButton::SetImage(const gfx::ImageSkia& image) {
if (image.isNull()) {
// TODO: need an empty image.
icon_view_->SetImage(image);
return;
}
const int icon_size = ShelfConstants::button_icon_size();
// Resize the image maintaining our aspect ratio.
float aspect_ratio =
static_cast<float>(image.width()) / static_cast<float>(image.height());
int height = icon_size;
int width = static_cast<int>(aspect_ratio * height);
if (width > icon_size) {
width = icon_size;
height = static_cast<int>(width / aspect_ratio);
}
if (width == image.width() && height == image.height()) {
SetShadowedImage(image);
return;
}
SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage(
image, skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height)));
}
const gfx::ImageSkia& ShelfAppButton::GetImage() const {
return icon_view_->GetImage();
}
void ShelfAppButton::AddState(State state) {
if (!(state_ & state)) {
state_ |= state;
Layout();
if (state & STATE_ATTENTION)
indicator_->ShowAttention(true);
if (state & STATE_ACTIVE)
indicator_->ShowActiveStatus(true);
if (is_notification_indicator_enabled_ && (state & STATE_NOTIFICATION))
notification_indicator_->SetVisible(true);
if (state & STATE_DRAGGING)
ScaleAppIcon(true);
}
}
void ShelfAppButton::ClearState(State state) {
if (state_ & state) {
state_ &= ~state;
Layout();
if (state & STATE_ATTENTION)
indicator_->ShowAttention(false);
if (state & STATE_ACTIVE)
indicator_->ShowActiveStatus(false);
if (is_notification_indicator_enabled_ && (state & STATE_NOTIFICATION))
notification_indicator_->SetVisible(false);
if (state & STATE_DRAGGING)
ScaleAppIcon(false);
}
}
gfx::Rect ShelfAppButton::GetIconBounds() const {
return icon_view_->bounds();
}
views::InkDrop* ShelfAppButton::GetInkDropForTesting() {
return GetInkDrop();
}
void ShelfAppButton::OnDragStarted(const ui::LocatedEvent* event) {
AnimateInkDrop(views::InkDropState::HIDDEN, event);
}
void ShelfAppButton::OnMenuClosed() {
DCHECK_EQ(views::InkDropState::ACTIVATED,
GetInkDrop()->GetTargetInkDropState());
GetInkDrop()->AnimateToState(views::InkDropState::DEACTIVATED);
}
void ShelfAppButton::ShowContextMenu(const gfx::Point& p,
ui::MenuSourceType source_type) {
if (!context_menu_controller())
return;
bool destroyed = false;
destroyed_flag_ = &destroyed;
if (source_type == ui::MenuSourceType::MENU_SOURCE_MOUSE ||
source_type == ui::MenuSourceType::MENU_SOURCE_KEYBOARD) {
GetInkDrop()->AnimateToState(views::InkDropState::ACTIVATED);
}
ShelfButton::ShowContextMenu(p, source_type);
if (!destroyed) {
destroyed_flag_ = nullptr;
// The menu will not propagate mouse events while it's shown. To address,
// the hover state gets cleared once the menu was shown (and this was not
// destroyed). In case context menu is shown target view does not receive
// OnMouseReleased events and we need to cancel capture manually.
if (shelf_view()->drag_view() == this)
OnMouseCaptureLost();
else
ClearState(STATE_HOVERED);
}
}
void ShelfAppButton::ReflectItemStatus(const ShelfItem& item) {
ShelfID active_id = shelf_view()->model()->active_shelf_id();
if (!active_id.IsNull() && item.id == active_id) {
// The active status trumps all other statuses.
AddState(ShelfAppButton::STATE_ACTIVE);
ClearState(ShelfAppButton::STATE_RUNNING);
ClearState(ShelfAppButton::STATE_ATTENTION);
return;
}
ClearState(ShelfAppButton::STATE_ACTIVE);
switch (item.status) {
case STATUS_CLOSED:
ClearState(ShelfAppButton::STATE_RUNNING);
ClearState(ShelfAppButton::STATE_ATTENTION);
break;
case STATUS_RUNNING:
AddState(ShelfAppButton::STATE_RUNNING);
ClearState(ShelfAppButton::STATE_ATTENTION);
break;
case STATUS_ATTENTION:
ClearState(ShelfAppButton::STATE_RUNNING);
AddState(ShelfAppButton::STATE_ATTENTION);
break;
}
if (features::IsNotificationIndicatorEnabled()) {
if (item.has_notification)
AddState(ShelfAppButton::STATE_NOTIFICATION);
else
ClearState(ShelfAppButton::STATE_NOTIFICATION);
}
}
const char* ShelfAppButton::GetClassName() const {
return kViewClassName;
}
bool ShelfAppButton::OnMousePressed(const ui::MouseEvent& event) {
// TODO: This call should probably live somewhere else (such as inside
// |ShelfView.PointerPressedOnButton|.
// No need to scale up the app for mouse right click since the app can't be
// dragged through right button.
if (!(event.flags() & ui::EF_LEFT_MOUSE_BUTTON)) {
Button::OnMousePressed(event);
return true;
}
ShelfButton::OnMousePressed(event);
if (shelf_view()->IsDraggedView(this)) {
drag_timer_.Start(
FROM_HERE, base::TimeDelta::FromMilliseconds(kDragTimeThresholdMs),
base::Bind(&ShelfAppButton::OnTouchDragTimer, base::Unretained(this)));
}
return true;
}
void ShelfAppButton::OnMouseReleased(const ui::MouseEvent& event) {
drag_timer_.Stop();
ClearState(STATE_DRAGGING);
ShelfButton::OnMouseReleased(event);
// WARNING: we may have been deleted.
}
void ShelfAppButton::OnMouseCaptureLost() {
ClearState(STATE_HOVERED);
ShelfButton::OnMouseCaptureLost();
}
void ShelfAppButton::Layout() {
// TODO: Find out why there is an extra pixel of padding between each item
// and the inner side of the shelf.
int icon_padding =
(ShelfConstants::shelf_size() - ShelfConstants::button_icon_size()) / 2 -
1;
const int icon_size = ShelfConstants::button_icon_size();
const int status_indicator_offet_from_shelf_edge =
ShelfConstants::status_indicator_offset_from_edge();
const gfx::Rect button_bounds(GetContentsBounds());
Shelf* shelf = shelf_view()->shelf();
const bool is_horizontal_shelf = shelf->IsHorizontalAlignment();
int x_offset = is_horizontal_shelf ? 0 : icon_padding;
int y_offset = is_horizontal_shelf ? icon_padding : 0;
int icon_width = std::min(icon_size, button_bounds.width() - x_offset);
int icon_height = std::min(icon_size, button_bounds.height() - y_offset);
// If on the left or top 'invert' the inset so the constant gap is on
// the interior (towards the center of display) edge of the shelf.
if (SHELF_ALIGNMENT_LEFT == shelf->alignment())
x_offset = button_bounds.width() - (icon_size + icon_padding);
// Center icon with respect to the secondary axis.
if (is_horizontal_shelf)
x_offset = std::max(0, button_bounds.width() - icon_width) / 2;
else
y_offset = std::max(0, button_bounds.height() - icon_height) / 2;
// Expand bounds to include shadows.
gfx::Insets insets_shadows = gfx::ShadowValue::GetMargin(icon_shadows_);
// Adjust offsets to center icon, not icon + shadow.
x_offset += (insets_shadows.left() - insets_shadows.right()) / 2;
y_offset += (insets_shadows.top() - insets_shadows.bottom()) / 2;
gfx::Rect icon_view_bounds =
gfx::Rect(button_bounds.x() + x_offset, button_bounds.y() + y_offset,
icon_width, icon_height);
// The indicators should be aligned with the icon, not the icon + shadow.
gfx::Point indicator_midpoint = icon_view_bounds.CenterPoint();
if (is_notification_indicator_enabled_) {
notification_indicator_->SetBoundsRect(
gfx::Rect(icon_view_bounds.right() - kNotificationIndicatorRadiusDip,
icon_view_bounds.y(), kNotificationIndicatorRadiusDip * 2,
kNotificationIndicatorRadiusDip * 2));
}
icon_view_bounds.Inset(insets_shadows);
icon_view_bounds.AdjustToFit(gfx::Rect(size()));
icon_view_->SetBoundsRect(icon_view_bounds);
// Icon size has been incorrect when running
// PanelLayoutManagerTest.PanelAlignmentSecondDisplay on valgrind bot, see
// http://crbug.com/234854.
DCHECK_LE(icon_width, icon_size);
DCHECK_LE(icon_height, icon_size);
switch (shelf->alignment()) {
case SHELF_ALIGNMENT_BOTTOM:
case SHELF_ALIGNMENT_BOTTOM_LOCKED:
indicator_midpoint.set_y(button_bounds.bottom() -
kStatusIndicatorRadiusDip -
status_indicator_offet_from_shelf_edge);
break;
case SHELF_ALIGNMENT_LEFT:
indicator_midpoint.set_x(button_bounds.x() + kStatusIndicatorRadiusDip +
status_indicator_offet_from_shelf_edge);
break;
case SHELF_ALIGNMENT_RIGHT:
indicator_midpoint.set_x(button_bounds.right() -
kStatusIndicatorRadiusDip -
status_indicator_offet_from_shelf_edge);
break;
}
gfx::Rect indicator_bounds(indicator_midpoint, gfx::Size());
indicator_bounds.Inset(gfx::Insets(-kStatusIndicatorMaxSize));
indicator_->SetBoundsRect(indicator_bounds);
UpdateState();
}
void ShelfAppButton::ChildPreferredSizeChanged(views::View* child) {
Layout();
}
void ShelfAppButton::OnGestureEvent(ui::GestureEvent* event) {
switch (event->type()) {
case ui::ET_GESTURE_TAP_DOWN:
if (shelf_view()->shelf()->IsVisible()) {
AddState(STATE_HOVERED);
drag_timer_.Start(
FROM_HERE, base::TimeDelta::FromMilliseconds(kDragTimeThresholdMs),
base::BindRepeating(&ShelfAppButton::OnTouchDragTimer,
base::Unretained(this)));
ripple_activation_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kInkDropRippleActivationTimeMs),
base::BindRepeating(&ShelfAppButton::OnRippleTimer,
base::Unretained(this)));
GetInkDrop()->AnimateToState(views::InkDropState::ACTION_PENDING);
event->SetHandled();
}
break;
case ui::ET_GESTURE_END:
drag_timer_.Stop();
// If the button is being dragged, or there is an active context menu,
// for this ShelfAppButton, don't deactivate the ink drop.
if (!(state_ & STATE_DRAGGING) &&
!shelf_view()->IsShowingMenuForView(this) &&
(GetInkDrop()->GetTargetInkDropState() ==
views::InkDropState::ACTIVATED)) {
GetInkDrop()->AnimateToState(views::InkDropState::DEACTIVATED);
}
ClearState(STATE_HOVERED);
ClearState(STATE_DRAGGING);
break;
case ui::ET_GESTURE_SCROLL_BEGIN:
if (state_ & STATE_DRAGGING) {
shelf_view()->PointerPressedOnButton(this, ShelfView::TOUCH, *event);
event->SetHandled();
} else {
// The drag went to the bezel and is about to be passed to
// ShelfLayoutManager.
drag_timer_.Stop();
GetInkDrop()->AnimateToState(views::InkDropState::HIDDEN);
}
break;
case ui::ET_GESTURE_SCROLL_UPDATE:
if ((state_ & STATE_DRAGGING) && shelf_view()->IsDraggedView(this)) {
shelf_view()->PointerDraggedOnButton(this, ShelfView::TOUCH, *event);
event->SetHandled();
}
break;
case ui::ET_GESTURE_SCROLL_END:
case ui::ET_SCROLL_FLING_START:
if (state_ & STATE_DRAGGING) {
ClearState(STATE_DRAGGING);
shelf_view()->PointerReleasedOnButton(this, ShelfView::TOUCH, false);
event->SetHandled();
}
break;
case ui::ET_GESTURE_LONG_TAP:
GetInkDrop()->AnimateToState(views::InkDropState::ACTIVATED);
// Handle LONG_TAP to avoid opening the context menu twice.
event->SetHandled();
break;
case ui::ET_GESTURE_TWO_FINGER_TAP:
GetInkDrop()->AnimateToState(views::InkDropState::ACTIVATED);
break;
default:
break;
}
if (!event->handled())
return Button::OnGestureEvent(event);
}
std::unique_ptr<views::InkDropRipple> ShelfAppButton::CreateInkDropRipple()
const {
const int ink_drop_small_size = ash::ShelfConstants::shelf_size();
return std::make_unique<views::SquareInkDropRipple>(
gfx::Size(kInkDropLargeSize, kInkDropLargeSize),
ink_drop_large_corner_radius(),
gfx::Size(ink_drop_small_size, ink_drop_small_size),
ink_drop_small_corner_radius(), GetLocalBounds().CenterPoint(),
GetInkDropBaseColor(), ink_drop_visible_opacity());
}
std::unique_ptr<views::InkDropMask> ShelfAppButton::CreateInkDropMask() const {
// Do not set a mask, allow the ink drop to burst out.
return nullptr;
}
void ShelfAppButton::UpdateState() {
const bool is_horizontal_shelf =
shelf_view()->shelf()->IsHorizontalAlignment();
indicator_->SetVisible(!(state_ & STATE_HIDDEN) &&
(state_ & STATE_ATTENTION || state_ & STATE_RUNNING ||
state_ & STATE_ACTIVE));
indicator_->SetHorizontalShelf(is_horizontal_shelf);
icon_view_->SetHorizontalAlignment(is_horizontal_shelf
? views::ImageView::CENTER
: views::ImageView::LEADING);
icon_view_->SetVerticalAlignment(is_horizontal_shelf
? views::ImageView::LEADING
: views::ImageView::CENTER);
SchedulePaint();
}
void ShelfAppButton::OnTouchDragTimer() {
AddState(STATE_DRAGGING);
}
void ShelfAppButton::OnRippleTimer() {
if (GetInkDrop()->GetTargetInkDropState() !=
views::InkDropState::ACTION_PENDING) {
return;
}
GetInkDrop()->AnimateToState(views::InkDropState::ACTIVATED);
}
void ShelfAppButton::ScaleAppIcon(bool scale_up) {
ui::ScopedLayerAnimationSettings settings(icon_view_->layer()->GetAnimator());
settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kDragDropAppIconScaleTransitionMs));
if (scale_up) {
icon_view_->layer()->SetTransform(gfx::GetScaleTransform(
gfx::Rect(icon_view_->layer()->bounds().size()).CenterPoint(),
kAppIconScale));
} else {
icon_view_->layer()->SetTransform(gfx::Transform());
}
}
} // namespace ash