forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollable_shelf_view.cc
2215 lines (1828 loc) · 82.1 KB
/
scrollable_shelf_view.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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 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/scrollable_shelf_view.h"
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/drag_drop/drag_image_view.h"
#include "ash/public/cpp/presentation_time_recorder.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/screen_util.h"
#include "ash/shelf/shelf_app_button.h"
#include "ash/shelf/shelf_focus_cycler.h"
#include "ash/shelf/shelf_navigation_widget.h"
#include "ash/shelf/shelf_tooltip_manager.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/system/status_area_widget.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "base/metrics/histogram_functions.h"
#include "base/numerics/ranges.h"
#include "chromeos/constants/chromeos_switches.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
#include "ui/gfx/skia_paint_util.h"
#include "ui/gfx/transform_util.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/focus/focus_search.h"
#include "ui/views/view_targeter_delegate.h"
namespace ash {
namespace {
// Padding between the arrow button and the end of ScrollableShelf. It is
// applied when the arrow button shows.
constexpr int kArrowButtonEndPadding = 6;
// Padding between the the shelf container view and the arrow button (if any).
constexpr int kDistanceToArrowButton = 2;
// Size of the arrow button.
constexpr int kArrowButtonSize = 20;
// The distance between ShelfContainerView and the end of ScrollableShelf when
// the arrow button shows.
constexpr int kArrowButtonGroupWidth =
kArrowButtonSize + kArrowButtonEndPadding + kDistanceToArrowButton;
// The gesture fling event with the velocity smaller than the threshold will be
// ignored.
constexpr int kGestureFlingVelocityThreshold = 1000;
// The mouse wheel event (including touchpad scrolling) with the main axis
// offset smaller than the threshold will be ignored.
constexpr int KScrollOffsetThreshold = 20;
// Horizontal size of the tap areafor the overflow arrow button.
constexpr int kArrowButtonTapAreaHorizontal = 32;
// Length of the fade in/out zone.
constexpr int kGradientZoneLength = 26;
// Delay to show a new page of shelf icons.
constexpr base::TimeDelta kShelfPageFlipDelay =
base::TimeDelta::FromMilliseconds(500);
// Histogram names for the scrollable shelf dragging metrics.
constexpr char kScrollDraggingTabletLauncherVisibleHistogram[] =
"Apps.ScrollableShelf.Drag.PresentationTime.TabletMode.LauncherVisible";
constexpr char kScrollDraggingTabletLauncherVisibleMaxLatencyHistogram[] =
"Apps.ScrollableShelf.Drag.PresentationTime.MaxLatency.TabletMode."
"LauncherVisible";
constexpr char kScrollDraggingTabletLauncherHiddenHistogram[] =
"Apps.ScrollableShelf.Drag.PresentationTime.TabletMode.LauncherHidden";
constexpr char kScrollDraggingTabletLauncherHiddenMaxLatencyHistogram[] =
"Apps.ScrollableShelf.Drag.PresentationTime.MaxLatency.TabletMode."
"LauncherHidden";
constexpr char kScrollDraggingClamshellLauncherVisibleHistogram[] =
"Apps.ScrollableShelf.Drag.PresentationTime.ClamshellMode.LauncherVisible";
constexpr char kScrollDraggingClamshellLauncherVisibleMaxLatencyHistogram[] =
"Apps.ScrollableShelf.Drag.PresentationTime.MaxLatency.ClamshellMode."
"LauncherVisible";
constexpr char kScrollDraggingClamshellLauncherHiddenHistogram[] =
"Apps.ScrollableShelf.Drag.PresentationTime.ClamshellMode.LauncherHidden";
constexpr char kScrollDraggingClamshellLauncherHiddenMaxLatencyHistogram[] =
"Apps.ScrollableShelf.Drag.PresentationTime.MaxLatency.ClamshellMode."
"LauncherHidden";
// Histogram names for the scrollable shelf animation smoothness metrics.
constexpr char kAnimationSmoothnessHistogram[] =
"Apps.ScrollableShelf.AnimationSmoothness";
constexpr char kAnimationSmoothnessTabletLauncherVisibleHistogram[] =
"Apps.ScrollableShelf.AnimationSmoothness.TabletMode.LauncherVisible";
constexpr char kAnimationSmoothnessTabletLauncherHiddenHistogram[] =
"Apps.ScrollableShelf.AnimationSmoothness.TabletMode.LauncherHidden";
constexpr char kAnimationSmoothnessClamshellLauncherVisibleHistogram[] =
"Apps.ScrollableShelf.AnimationSmoothness.ClamshellMode.LauncherVisible";
constexpr char kAnimationSmoothnessClamshellLauncherHiddenHistogram[] =
"Apps.ScrollableShelf.AnimationSmoothness.ClamshellMode.LauncherHidden";
// Sum of the shelf button size and the gap between shelf buttons.
int GetUnit() {
return ShelfConfig::Get()->button_size() +
ShelfConfig::Get()->button_spacing();
}
// Decides whether the current first visible shelf icon of the scrollable shelf
// should be hidden or fully shown when gesture scroll ends.
int GetGestureDragThreshold() {
return ShelfConfig::Get()->button_size() / 2;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// DragIconDropAnimationDelegate
class ScrollableShelfView::DragIconDropAnimationDelegate
: public ui::ImplicitAnimationObserver {
public:
DragIconDropAnimationDelegate(views::View* original_view,
const gfx::Rect& src_bounds_in_screen,
const gfx::Rect& dst_bounds_in_screen,
std::unique_ptr<DragImageView> proxy_view)
: original_view_(original_view),
src_bounds_in_screen_(src_bounds_in_screen),
dst_bounds_in_screen_(dst_bounds_in_screen),
proxy_view_(std::move(proxy_view)) {}
~DragIconDropAnimationDelegate() override = default;
DragIconDropAnimationDelegate(const DragIconDropAnimationDelegate&) = delete;
DragIconDropAnimationDelegate& operator=(
const DragIconDropAnimationDelegate&) = delete;
void StartAnimation() {
ui::ScopedLayerAnimationSettings animation_settings(
proxy_view_->layer()->GetAnimator());
animation_settings.SetTweenType(gfx::Tween::FAST_OUT_LINEAR_IN);
animation_settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
animation_settings.AddObserver(this);
proxy_view_->layer()->SetTransform(gfx::TransformBetweenRects(
gfx::RectF(src_bounds_in_screen_), gfx::RectF(dst_bounds_in_screen_)));
}
// ui::ImplicitAnimationObserver:
void OnImplicitAnimationsCompleted() override {
StopObserving();
// Destructs the proxy image view and shows the original drag view at the
// end of animation.
original_view_->layer()->SetOpacity(1.0f);
proxy_view_.reset();
}
private:
// Original app icon being dragged in ShelfView.
views::View* const original_view_ = nullptr;
// Bounds of the DragImageView, aka the DragIconProxy created by the user of
// ApplicationDragAndDropHost.
gfx::Rect const src_bounds_in_screen_;
// Bounds of the ShelfAppButton which DragImageView is imitating.
gfx::Rect const dst_bounds_in_screen_;
// Placeholder icon representing |original_view_| that moves with the pointer
// while being dragged.
std::unique_ptr<DragImageView> proxy_view_;
};
////////////////////////////////////////////////////////////////////////////////
// GradientLayerDelegate
class ScrollableShelfView::GradientLayerDelegate : public ui::LayerDelegate {
public:
GradientLayerDelegate() : layer_(ui::LAYER_TEXTURED) {
layer_.set_delegate(this);
layer_.SetFillsBoundsOpaquely(false);
}
~GradientLayerDelegate() override { layer_.set_delegate(nullptr); }
bool IsStartFadeZoneVisible() const {
return !start_fade_zone_.zone_rect.IsEmpty();
}
bool IsEndFadeZoneVisible() const {
return !end_fade_zone_.zone_rect.IsEmpty();
}
void set_start_fade_zone(const FadeZone& fade_zone) {
start_fade_zone_ = fade_zone;
}
void set_end_fade_zone(const FadeZone& fade_zone) {
end_fade_zone_ = fade_zone;
}
gfx::Rect start_fade_zone_bounds() const {
return start_fade_zone_.zone_rect;
}
gfx::Rect end_fade_zone_bounds() const { return end_fade_zone_.zone_rect; }
ui::Layer* layer() { return &layer_; }
private:
// ui::LayerDelegate:
void OnPaintLayer(const ui::PaintContext& context) override {
const gfx::Size size = layer()->size();
views::PaintInfo paint_info =
views::PaintInfo::CreateRootPaintInfo(context, size);
const auto& paint_recording_size = paint_info.paint_recording_size();
// Pass the scale factor when constructing PaintRecorder so the MaskLayer
// size is not incorrectly rounded (see https://crbug.com/921274).
ui::PaintRecorder recorder(
context, paint_info.paint_recording_size(),
static_cast<float>(paint_recording_size.width()) / size.width(),
static_cast<float>(paint_recording_size.height()) / size.height(),
nullptr);
recorder.canvas()->DrawColor(SK_ColorBLACK, SkBlendMode::kSrc);
if (!start_fade_zone_.zone_rect.IsEmpty())
DrawFadeZone(start_fade_zone_, recorder.canvas());
if (!end_fade_zone_.zone_rect.IsEmpty())
DrawFadeZone(end_fade_zone_, recorder.canvas());
}
void OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) override {}
void DrawFadeZone(const FadeZone& fade_zone, gfx::Canvas* canvas) {
gfx::Point start_point;
gfx::Point end_point;
if (fade_zone.is_horizontal) {
start_point = gfx::Point(fade_zone.zone_rect.x(), 0);
end_point = gfx::Point(fade_zone.zone_rect.right(), 0);
} else {
start_point = gfx::Point(0, fade_zone.zone_rect.y());
end_point = gfx::Point(0, fade_zone.zone_rect.bottom());
}
cc::PaintFlags flags;
flags.setBlendMode(SkBlendMode::kSrc);
flags.setAntiAlias(false);
flags.setShader(gfx::CreateGradientShader(
start_point, end_point,
fade_zone.fade_in ? SK_ColorTRANSPARENT : SK_ColorBLACK,
fade_zone.fade_in ? SK_ColorBLACK : SK_ColorTRANSPARENT));
canvas->DrawRect(fade_zone.zone_rect, flags);
}
ui::Layer layer_;
FadeZone start_fade_zone_;
FadeZone end_fade_zone_;
DISALLOW_COPY_AND_ASSIGN(GradientLayerDelegate);
};
////////////////////////////////////////////////////////////////////////////////
// ScrollableShelfArrowView
class ScrollableShelfView::ScrollableShelfArrowView
: public ScrollArrowView,
public views::ViewTargeterDelegate {
public:
explicit ScrollableShelfArrowView(ArrowType arrow_type,
bool is_horizontal_alignment,
Shelf* shelf,
ShelfButtonDelegate* shelf_button_delegate)
: ScrollArrowView(arrow_type,
is_horizontal_alignment,
shelf,
shelf_button_delegate) {
SetInkDropMode(InkDropMode::OFF);
SetEventTargeter(std::make_unique<views::ViewTargeter>(this));
}
~ScrollableShelfArrowView() override = default;
// views::ViewTargeterDelegate:
bool DoesIntersectRect(const views::View* target,
const gfx::Rect& rect) const override {
DCHECK_EQ(target, this);
gfx::Rect bounds = gfx::Rect(size());
// Calculate padding for the tap area. (Should be 32 x shelfButtonSize)
constexpr int horizontalPadding =
(kArrowButtonTapAreaHorizontal - kArrowButtonSize) / 2;
const int verticalPadding =
(ShelfConfig::Get()->button_size() - kArrowButtonSize) / 2;
bounds.Inset(gfx::Insets(-verticalPadding, -horizontalPadding));
return bounds.Intersects(rect);
}
// Make ScrollRectToVisible a no-op because ScrollableShelfArrowView is
// always visible/invisible depending on the layout strategy at fixed
// locations. So it does not need to be scrolled to show.
// TODO (andrewxu): Moves all of functions related with scrolling into
// ScrollableShelfContainerView. Then erase this empty function.
void ScrollRectToVisible(const gfx::Rect& rect) override {}
const char* GetClassName() const override {
return "ScrollableShelfArrowView";
}
};
////////////////////////////////////////////////////////////////////////////////
// ScrollableShelfAnimationMetricsReporter
class ScrollableShelfAnimationMetricsReporter
: public ui::AnimationMetricsReporter {
public:
ScrollableShelfAnimationMetricsReporter() {}
~ScrollableShelfAnimationMetricsReporter() override = default;
// ui::AnimationMetricsReporter:
void Report(int value) override {
base::UmaHistogramPercentage(kAnimationSmoothnessHistogram, value);
if (Shell::Get()->IsInTabletMode()) {
if (Shell::Get()->app_list_controller()->IsVisible()) {
base::UmaHistogramPercentage(
kAnimationSmoothnessTabletLauncherVisibleHistogram, value);
} else {
base::UmaHistogramPercentage(
kAnimationSmoothnessTabletLauncherHiddenHistogram, value);
}
} else {
if (Shell::Get()->app_list_controller()->IsVisible()) {
base::UmaHistogramPercentage(
kAnimationSmoothnessClamshellLauncherVisibleHistogram, value);
} else {
base::UmaHistogramPercentage(
kAnimationSmoothnessClamshellLauncherHiddenHistogram, value);
}
}
}
DISALLOW_COPY_AND_ASSIGN(ScrollableShelfAnimationMetricsReporter);
};
////////////////////////////////////////////////////////////////////////////////
// ScrollableShelfContainerView
class ScrollableShelfContainerView : public ShelfContainerView,
public views::ViewTargeterDelegate {
public:
explicit ScrollableShelfContainerView(
ScrollableShelfView* scrollable_shelf_view)
: ShelfContainerView(scrollable_shelf_view->shelf_view()),
scrollable_shelf_view_(scrollable_shelf_view) {
SetEventTargeter(std::make_unique<views::ViewTargeter>(this));
}
~ScrollableShelfContainerView() override = default;
// ShelfContainerView:
void TranslateShelfView(const gfx::Vector2dF& offset) override;
private:
// views::View:
void Layout() override;
// views::ViewTargeterDelegate:
bool DoesIntersectRect(const views::View* target,
const gfx::Rect& rect) const override;
ScrollableShelfView* scrollable_shelf_view_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(ScrollableShelfContainerView);
};
void ScrollableShelfContainerView::TranslateShelfView(
const gfx::Vector2dF& offset) {
ShelfContainerView::TranslateShelfView(
scrollable_shelf_view_->ShouldAdaptToRTL() ? -offset : offset);
}
void ScrollableShelfContainerView::Layout() {
// Should not use ShelfView::GetPreferredSize in replace of
// CalculateIdealSize. Because ShelfView::CalculatePreferredSize relies on the
// bounds of app icon. Meanwhile, the icon's bounds may be updated by
// animation.
const gfx::Rect ideal_bounds = gfx::Rect(CalculateIdealSize());
const gfx::Rect local_bounds = GetLocalBounds();
gfx::Rect shelf_view_bounds =
local_bounds.Contains(ideal_bounds) ? local_bounds : ideal_bounds;
if (shelf_view_->shelf()->IsHorizontalAlignment())
shelf_view_bounds.set_x(ShelfConfig::Get()->GetAppIconEndPadding());
else
shelf_view_bounds.set_y(ShelfConfig::Get()->GetAppIconEndPadding());
shelf_view_->SetBoundsRect(shelf_view_bounds);
}
bool ScrollableShelfContainerView::DoesIntersectRect(
const views::View* target,
const gfx::Rect& rect) const {
// This view's layer is clipped. So the view should only handle the events
// within the area after cilp.
gfx::RectF bounds = gfx::RectF(scrollable_shelf_view_->visible_space());
views::View::ConvertRectToTarget(scrollable_shelf_view_, this, &bounds);
return ToEnclosedRect(bounds).Contains(rect);
}
////////////////////////////////////////////////////////////////////////////////
// ScrollableShelfFocusSearch
class ScrollableShelfFocusSearch : public views::FocusSearch {
public:
explicit ScrollableShelfFocusSearch(
ScrollableShelfView* scrollable_shelf_view)
: FocusSearch(/*root=*/nullptr,
/*cycle=*/true,
/*accessibility_mode=*/true),
scrollable_shelf_view_(scrollable_shelf_view) {}
~ScrollableShelfFocusSearch() override = default;
// views::FocusSearch
views::View* FindNextFocusableView(
views::View* starting_view,
FocusSearch::SearchDirection search_direction,
FocusSearch::TraversalDirection traversal_direction,
FocusSearch::StartingViewPolicy check_starting_view,
FocusSearch::AnchoredDialogPolicy can_go_into_anchored_dialog,
views::FocusTraversable** focus_traversable,
views::View** focus_traversable_view) override {
std::vector<views::View*> focusable_views;
ShelfView* shelf_view = scrollable_shelf_view_->shelf_view();
for (int i = shelf_view->first_visible_index();
i <= shelf_view->last_visible_index(); ++i) {
focusable_views.push_back(shelf_view->view_model()->view_at(i));
}
int start_index = 0;
for (size_t i = 0; i < focusable_views.size(); ++i) {
if (focusable_views[i] == starting_view) {
start_index = i;
break;
}
}
int new_index =
start_index +
(search_direction == FocusSearch::SearchDirection::kBackwards ? -1 : 1);
// Scrolls to the new page if the focused shelf item is not tappable
// on the current page.
if (new_index < 0)
new_index = focusable_views.size() - 1;
else if (new_index >= static_cast<int>(focusable_views.size()))
new_index = 0;
else if (new_index < scrollable_shelf_view_->first_tappable_app_index())
scrollable_shelf_view_->ScrollToNewPage(/*forward=*/false);
else if (new_index > scrollable_shelf_view_->last_tappable_app_index())
scrollable_shelf_view_->ScrollToNewPage(/*forward=*/true);
return focusable_views[new_index];
}
private:
ScrollableShelfView* scrollable_shelf_view_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(ScrollableShelfFocusSearch);
};
////////////////////////////////////////////////////////////////////////////////
// ScrollableShelfView
ScrollableShelfView::ScrollableShelfView(ShelfModel* model, Shelf* shelf)
: shelf_view_(new ShelfView(model,
shelf,
/*drag_and_drop_host=*/this,
/*shelf_button_delegate=*/this)),
page_flip_time_threshold_(kShelfPageFlipDelay),
animation_metrics_reporter_(
std::make_unique<ScrollableShelfAnimationMetricsReporter>()) {
Shell::Get()->AddShellObserver(this);
ShelfConfig::Get()->AddObserver(this);
set_allow_deactivate_on_esc(true);
}
ScrollableShelfView::~ScrollableShelfView() {
ShelfConfig::Get()->RemoveObserver(this);
Shell::Get()->RemoveShellObserver(this);
GetShelf()->tooltip()->set_shelf_tooltip_delegate(nullptr);
}
void ScrollableShelfView::Init() {
// Although there is no animation for ScrollableShelfView, a layer is still
// needed. Otherwise, the child view without its own layer will be painted on
// RootView which is beneath |translucent_background_| in ShelfWidget.
// As a result, the child view will not show.
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
// Initialize the shelf container view.
// Note that |shelf_container_view_| should be under the arrow buttons. It
// ensures that the arrow button receives the tapping events which happen
// within the overlapping zone between the arrow button's tapping area and
// the bounds of |shelf_container_view_|.
shelf_container_view_ =
AddChildView(std::make_unique<ScrollableShelfContainerView>(this));
shelf_container_view_->Initialize();
// Initialize the left arrow button.
left_arrow_ = AddChildView(std::make_unique<ScrollableShelfArrowView>(
ScrollArrowView::kLeft, GetShelf()->IsHorizontalAlignment(), GetShelf(),
this));
// Initialize the right arrow button.
right_arrow_ = AddChildView(std::make_unique<ScrollableShelfArrowView>(
ScrollArrowView::kRight, GetShelf()->IsHorizontalAlignment(), GetShelf(),
this));
gradient_layer_delegate_ = std::make_unique<GradientLayerDelegate>();
layer()->SetMaskLayer(gradient_layer_delegate_->layer());
focus_search_ = std::make_unique<ScrollableShelfFocusSearch>(this);
GetShelf()->tooltip()->set_shelf_tooltip_delegate(this);
set_context_menu_controller(this);
// Initializes |shelf_view_| after scrollable shelf view's children are
// initialized.
shelf_view_->Init();
}
void ScrollableShelfView::OnFocusRingActivationChanged(bool activated) {
if (activated) {
focus_ring_activated_ = true;
SetPaneFocusAndFocusDefault();
if (Shell::Get()->IsInTabletMode() &&
chromeos::switches::ShouldShowShelfHotseat())
GetShelf()->shelf_widget()->ForceToShowHotseat();
} else {
// Shows the gradient shader when the focus ring is disabled.
focus_ring_activated_ = false;
if (Shell::Get()->IsInTabletMode() &&
chromeos::switches::ShouldShowShelfHotseat())
GetShelf()->shelf_widget()->ForceToHideHotseat();
}
MaybeUpdateGradientZone();
}
void ScrollableShelfView::ScrollToNewPage(bool forward) {
const float offset = CalculatePageScrollingOffset(forward, layout_strategy_);
if (GetShelf()->IsHorizontalAlignment())
ScrollByXOffset(offset, /*animating=*/true);
else
ScrollByYOffset(offset, /*animating=*/true);
}
views::FocusSearch* ScrollableShelfView::GetFocusSearch() {
return focus_search_.get();
}
views::FocusTraversable* ScrollableShelfView::GetFocusTraversableParent() {
return parent()->GetFocusTraversable();
}
views::View* ScrollableShelfView::GetFocusTraversableParentView() {
return this;
}
views::View* ScrollableShelfView::GetDefaultFocusableChild() {
// Adapts |scroll_offset_| to show the view properly right after the focus
// ring is enabled.
if (default_last_focusable_child_) {
ScrollToMainOffset(CalculateScrollUpperBound(GetSpaceForIcons()),
/*animating=*/true);
return FindLastFocusableChild();
} else {
ScrollToMainOffset(/*target_offset=*/0.f, /*animating=*/true);
return FindFirstFocusableChild();
}
}
gfx::Rect ScrollableShelfView::GetHotseatBackgroundBounds() const {
return available_space_;
}
bool ScrollableShelfView::ShouldAdaptToRTL() const {
return base::i18n::IsRTL() && GetShelf()->IsHorizontalAlignment();
}
gfx::Rect ScrollableShelfView::GetTargetScreenBoundsOfItemIcon(
const ShelfID& id) const {
DCHECK(GetShelf()->IsHorizontalAlignment());
// Calculates the available space for child views based on the target bounds.
const gfx::Insets edge_padding =
CalculateEdgePadding(/*use_target_bounds=*/true);
gfx::Rect target_space = GetAvailableLocalBounds(/*use_target_bounds=*/true);
target_space.Inset(edge_padding);
const int target_scroll_offset =
CalculateScrollOffsetForTargetAvailableSpace(target_space);
gfx::Rect icon_bounds = shelf_view_->view_model()->ideal_bounds(
shelf_view_->model()->ItemIndexByID(id));
icon_bounds.Offset(edge_padding.left() - padding_insets_.left(), 0);
// Transforms |icon_bounds| from shelf view's coordinates to scrollable shelf
// view's coordinates manually.
icon_bounds.Offset(
-target_scroll_offset + ShelfConfig::Get()->GetAppIconEndPadding(), 0);
// If the icon is invisible under the target view bounds, replaces the actual
// icon's bounds with the rectangle centering on the edge of |target_space|.
const gfx::Point icon_bounds_center = icon_bounds.CenterPoint();
if (icon_bounds_center.x() > target_space.right()) {
icon_bounds.Offset(target_space.right_center().OffsetFromOrigin() -
icon_bounds_center.OffsetFromOrigin());
} else if (icon_bounds_center.x() < target_space.x()) {
icon_bounds.Offset(target_space.left_center().OffsetFromOrigin() -
icon_bounds_center.OffsetFromOrigin());
}
// Hotseat's target bounds may differ from the actual bounds. So it has to
// transform the bounds manually from view's local coordinates to screen.
// Notes that the target bounds stored in shelf layout manager are adapted to
// RTL already while |icon_bounds| are not adjusted to RTL yet.
gfx::Rect hotseat_bounds_in_screen =
GetShelf()->hotseat_widget()->GetTargetBounds();
if (ShouldAdaptToRTL()) {
// One simple way for transformation under RTL is: (1) Transforms hotseat
// target bounds from RTL to LTR. (2) Calculates the icon's bounds in screen
// under LTR. (3) Transforms the icon's bounds to RTL.
gfx::Rect display_bounds =
display::Screen::GetScreen()
->GetDisplayNearestWindow(GetWidget()->GetNativeView())
.bounds();
hotseat_bounds_in_screen.set_x(display_bounds.right() -
hotseat_bounds_in_screen.right());
icon_bounds.Offset(hotseat_bounds_in_screen.OffsetFromOrigin());
icon_bounds.set_x(display_bounds.right() - icon_bounds.right());
} else {
icon_bounds.Offset(hotseat_bounds_in_screen.OffsetFromOrigin());
}
return icon_bounds;
}
views::View* ScrollableShelfView::GetShelfContainerViewForTest() {
return shelf_container_view_;
}
void ScrollableShelfView::SetRoundedCornersForShelf(
bool show,
views::View* ink_drop_host) {
shelf_container_view_->layer()->SetRoundedCornerRadius(
show && InkDropNeedsClipping(ink_drop_host)
? CalculateShelfContainerRoundedCorners()
: gfx::RoundedCornersF());
shelf_container_view_->layer()->SetIsFastRoundedCorner(true);
}
bool ScrollableShelfView::InkDropNeedsClipping(views::View* ink_drop_host) {
// The ink drop needs to be clipped only if the host is the app at one of the
// corners of the shelf. This happens if it is either the first or the last
// tappable app and no arrow is showing on its side.
if (shelf_view_->view_model()->view_at(first_tappable_app_index_) ==
ink_drop_host) {
return !(layout_strategy_ == kShowButtons ||
layout_strategy_ == kShowLeftArrowButton);
}
if (shelf_view_->view_model()->view_at(last_tappable_app_index_) ==
ink_drop_host) {
return !(layout_strategy_ == kShowButtons ||
layout_strategy_ == kShowRightArrowButton);
}
return false;
}
bool ScrollableShelfView::ShouldAdjustForTest() const {
return CalculateAdjustmentOffset(CalculateMainAxisScrollDistance(),
layout_strategy_, GetSpaceForIcons());
}
void ScrollableShelfView::SetTestObserver(TestObserver* test_observer) {
DCHECK(!(test_observer && test_observer_));
test_observer_ = test_observer;
}
float ScrollableShelfView::CalculateScrollUpperBound(
int available_space_for_icons) const {
if (layout_strategy_ == kNotShowArrowButtons)
return 0.f;
// Calculate the length of the available space.
int available_length = available_space_for_icons -
2 * ShelfConfig::Get()->GetAppIconEndPadding();
// Calculate the length of the preferred size.
const gfx::Size shelf_preferred_size(
shelf_container_view_->GetPreferredSize());
const int preferred_length =
(GetShelf()->IsHorizontalAlignment() ? shelf_preferred_size.width()
: shelf_preferred_size.height());
return std::max(0, preferred_length - available_length);
}
float ScrollableShelfView::CalculateClampedScrollOffset(
float scroll,
int available_space_for_icons) const {
const float scroll_upper_bound =
CalculateScrollUpperBound(available_space_for_icons);
scroll = base::ClampToRange(scroll, 0.0f, scroll_upper_bound);
return scroll;
}
void ScrollableShelfView::StartShelfScrollAnimation(float scroll_distance) {
const gfx::Vector2dF scroll_offset_before_update = scroll_offset_;
UpdateScrollOffset(scroll_distance);
if (scroll_offset_before_update == scroll_offset_)
return;
StopObservingImplicitAnimations();
during_scroll_animation_ = true;
MaybeUpdateGradientZone();
ui::ScopedLayerAnimationSettings animation_settings(
shelf_view_->layer()->GetAnimator());
animation_settings.SetTweenType(gfx::Tween::EASE_OUT);
animation_settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
animation_settings.SetAnimationMetricsReporter(
animation_metrics_reporter_.get());
animation_settings.AddObserver(this);
shelf_container_view_->TranslateShelfView(scroll_offset_);
}
ScrollableShelfView::LayoutStrategy
ScrollableShelfView::CalculateLayoutStrategy(float scroll_distance_on_main_axis,
int space_for_icons,
bool use_target_bounds) const {
if (CanFitAllAppsWithoutScrolling(use_target_bounds))
return kNotShowArrowButtons;
if (scroll_distance_on_main_axis == 0.f) {
// No invisible shelf buttons at the left side. So hide the left button.
return kShowRightArrowButton;
}
if (scroll_distance_on_main_axis ==
CalculateScrollUpperBound(space_for_icons)) {
// If there is no invisible shelf button at the right side, hide the right
// button.
return kShowLeftArrowButton;
}
// There are invisible shelf buttons at both sides. So show two buttons.
return kShowButtons;
}
bool ScrollableShelfView::ShouldApplyDisplayCentering(
bool use_target_bounds) const {
if (!CanFitAllAppsWithoutScrolling(use_target_bounds))
return false;
const gfx::Rect display_bounds =
screen_util::GetDisplayBoundsWithShelf(GetWidget()->GetNativeWindow());
const int display_size_primary = GetShelf()->PrimaryAxisValue(
display_bounds.width(), display_bounds.height());
const int status_widget_size =
GetStatusWidgetSizeOnPrimaryAxis(use_target_bounds);
// An easy way to check whether the apps fit at the exact center of the
// screen is to imagine that we have another status widget on the other
// side (the status widget is always bigger than the home button plus
// the back button if applicable) and see if the apps can fit in the middle
int available_space_for_screen_centering =
display_size_primary -
2 * (status_widget_size + ShelfConfig::Get()->app_icon_group_margin());
return available_space_for_screen_centering >
shelf_view_->GetSizeOfAppIcons(shelf_view_->number_of_visible_apps(),
false);
}
Shelf* ScrollableShelfView::GetShelf() {
return const_cast<Shelf*>(
const_cast<const ScrollableShelfView*>(this)->GetShelf());
}
const Shelf* ScrollableShelfView::GetShelf() const {
return shelf_view_->shelf();
}
gfx::Size ScrollableShelfView::CalculatePreferredSize() const {
return shelf_container_view_->GetPreferredSize();
}
void ScrollableShelfView::Layout() {
gfx::Rect shelf_container_bounds = gfx::Rect(size());
gfx::Size arrow_button_size(kArrowButtonSize,
shelf_container_bounds.height());
// Transpose and layout as if it is horizontal.
const bool is_horizontal = GetShelf()->IsHorizontalAlignment();
if (!is_horizontal)
shelf_container_bounds.Transpose();
gfx::Size arrow_button_group_size(kArrowButtonGroupWidth,
shelf_container_bounds.height());
// The bounds of |left_arrow_| and |right_arrow_| are in the
// ScrollableShelfView's local coordinates.
gfx::Rect left_arrow_bounds;
gfx::Rect right_arrow_bounds;
const int before_padding =
is_horizontal ? padding_insets_.left() : padding_insets_.top();
const int after_padding =
is_horizontal ? padding_insets_.right() : padding_insets_.bottom();
// Calculates the bounds of the left arrow button. If the left arrow button
// should not show, |left_arrow_bounds| should be empty.
if (layout_strategy_ == kShowLeftArrowButton ||
layout_strategy_ == kShowButtons) {
left_arrow_bounds = gfx::Rect(arrow_button_group_size);
left_arrow_bounds.Offset(before_padding, 0);
left_arrow_bounds.Inset(kArrowButtonEndPadding, 0, kDistanceToArrowButton,
0);
left_arrow_bounds.ClampToCenteredSize(arrow_button_size);
}
if (layout_strategy_ == kShowRightArrowButton ||
layout_strategy_ == kShowButtons) {
gfx::Point right_arrow_start_point(
shelf_container_bounds.right() - after_padding - kArrowButtonGroupWidth,
0);
right_arrow_bounds =
gfx::Rect(right_arrow_start_point, arrow_button_group_size);
right_arrow_bounds.Inset(kDistanceToArrowButton, 0, kArrowButtonEndPadding,
0);
right_arrow_bounds.ClampToCenteredSize(arrow_button_size);
}
// Adjust the bounds when not showing in the horizontal
// alignment.tShelf()->IsHorizontalAlignment()) {
if (!is_horizontal) {
left_arrow_bounds.Transpose();
right_arrow_bounds.Transpose();
shelf_container_bounds.Transpose();
}
// Layout |left_arrow_| if it should show.
left_arrow_->SetVisible(!left_arrow_bounds.IsEmpty());
if (left_arrow_->GetVisible())
left_arrow_->SetBoundsRect(left_arrow_bounds);
// Layout |right_arrow_| if it should show.
right_arrow_->SetVisible(!right_arrow_bounds.IsEmpty());
if (right_arrow_->GetVisible())
right_arrow_->SetBoundsRect(right_arrow_bounds);
// Layer::Clone(), which may be triggered by screen rotation, does not copy
// the mask layer. So we may need to reset the mask layer.
if (ShouldApplyMaskLayerGradientZone() && !layer()->layer_mask_layer()) {
DCHECK(!gradient_layer_delegate_->layer()->layer_mask_back_link());
layer()->SetMaskLayer(gradient_layer_delegate_->layer());
}
MaybeUpdateGradientZone();
// Layout |shelf_container_view_|.
shelf_container_view_->SetBoundsRect(shelf_container_bounds);
}
void ScrollableShelfView::ChildPreferredSizeChanged(views::View* child) {
// Add/remove a shelf icon may change the layout strategy.
Layout();
}
void ScrollableShelfView::OnScrollEvent(ui::ScrollEvent* event) {
if (event->finger_count() != 2)
return;
if (ShouldDelegateScrollToShelf(*event)) {
ui::MouseWheelEvent wheel(*event);
GetShelf()->ProcessMouseWheelEvent(&wheel, /*from_touchpad=*/true);
event->StopPropagation();
}
}
void ScrollableShelfView::OnMouseEvent(ui::MouseEvent* event) {
if (event->IsMouseWheelEvent()) {
HandleMouseWheelEvent(event->AsMouseWheelEvent());
return;
}
// The mouse event's location may be outside of ShelfView but within the
// bounds of the ScrollableShelfView. Meanwhile, ScrollableShelfView should
// handle the mouse event consistently with ShelfView. To achieve this,
// we simply redirect |event| to ShelfView.
gfx::Point location_in_shelf_view = event->location();
View::ConvertPointToTarget(this, shelf_view_, &location_in_shelf_view);
event->set_location(location_in_shelf_view);
shelf_view_->OnMouseEvent(event);
}
void ScrollableShelfView::OnGestureEvent(ui::GestureEvent* event) {
if (ShouldHandleGestures(*event) && ProcessGestureEvent(*event)) {
// |event| is consumed by ScrollableShelfView.
event->SetHandled();
} else if (shelf_view_->HandleGestureEvent(event)) {
// |event| is consumed by ShelfView.
event->StopPropagation();
} else if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) {
// |event| is consumed by neither ScrollableShelfView nor ShelfView. So the
// gesture end event will not be propagated to this view. Then we need to
// reset the class members related with scroll status explicitly.
ResetScrollStatus();
}
}
void ScrollableShelfView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
GetViewAccessibility().OverrideNextFocus(GetShelf()->GetStatusAreaWidget());
GetViewAccessibility().OverridePreviousFocus(
GetShelf()->shelf_widget()->navigation_widget());
}
void ScrollableShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
if (ShouldApplyMaskLayerGradientZone() &&
gradient_layer_delegate_->layer()->bounds() != layer()->bounds()) {
gradient_layer_delegate_->layer()->SetBounds(layer()->bounds());
}
const gfx::Insets old_padding_insets = padding_insets_;
const gfx::Vector2dF old_scroll_offset = scroll_offset_;
// The changed view bounds may lead to update on the available space.
UpdateAvailableSpaceAndScroll();
// Relayout shelf items if the preferred padding changed.
if (old_padding_insets != padding_insets_)
shelf_view_->OnBoundsChanged(shelf_view_->GetBoundsInScreen());
// Avoids calling AdjustOffset() when the scrollable shelf view is
// under scroll along the main axis. Otherwise, animation will conflict with
// scroll gesture. Meanwhile, translates the shelf view
// if AdjustOffset() returns false since when AdjustOffset() returns true,
// shelf view is scrolled by animation.
const bool should_translate_shelf_view =
scroll_status_ == kAlongMainAxisScroll || !AdjustOffset();
if (should_translate_shelf_view && old_scroll_offset != scroll_offset_)
shelf_container_view_->TranslateShelfView(scroll_offset_);
}
void ScrollableShelfView::ViewHierarchyChanged(
const views::ViewHierarchyChangedDetails& details) {
if (details.parent != shelf_view_)
return;
shelf_view_->UpdateVisibleIndices();
const gfx::Vector2dF old_scroll_offset = scroll_offset_;
// Adding/removing an icon may change the padding then affect the available
// space.
UpdateAvailableSpaceAndScroll();
if (old_scroll_offset != scroll_offset_)
shelf_container_view_->TranslateShelfView(scroll_offset_);
}
void ScrollableShelfView::ScrollRectToVisible(const gfx::Rect& rect) {
// Transform |rect| to local view coordinates taking |scroll_offset_| into
// consideration.
const bool is_horizontal_alignment = GetShelf()->IsHorizontalAlignment();
gfx::Rect rect_after_adjustment = rect;
if (is_horizontal_alignment)
rect_after_adjustment.Offset(-scroll_offset_.x(), 0);
else
rect_after_adjustment.Offset(0, -scroll_offset_.y());
// |rect_after_adjustment| is already shown completely. So scroll is not
// needed.
if (visible_space_.Contains(rect_after_adjustment)) {
AdjustOffset();
return;