forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelf_view.cc
2032 lines (1793 loc) · 72.9 KB
/
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 (c) 2012 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_view.h"
#include <algorithm>
#include "ash/ash_constants.h"
#include "ash/ash_switches.h"
#include "ash/drag_drop/drag_image_view.h"
#include "ash/metrics/user_metrics_recorder.h"
#include "ash/root_window_controller.h"
#include "ash/scoped_target_root_window.h"
#include "ash/shelf/alternate_app_list_button.h"
#include "ash/shelf/app_list_button.h"
#include "ash/shelf/overflow_bubble.h"
#include "ash/shelf/overflow_bubble_view.h"
#include "ash/shelf/overflow_button.h"
#include "ash/shelf/shelf_button.h"
#include "ash/shelf/shelf_constants.h"
#include "ash/shelf/shelf_delegate.h"
#include "ash/shelf/shelf_icon_observer.h"
#include "ash/shelf/shelf_item_delegate.h"
#include "ash/shelf/shelf_item_delegate_manager.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/shelf_menu_model.h"
#include "ash/shelf/shelf_model.h"
#include "ash/shelf/shelf_tooltip_manager.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/wm/coordinate_conversion.h"
#include "base/auto_reset.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
#include "grit/ash_resources.h"
#include "grit/ash_strings.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/point.h"
#include "ui/views/animation/bounds_animator.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/menu/menu_model_adapter.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/focus/focus_search.h"
#include "ui/views/view_model.h"
#include "ui/views/view_model_utils.h"
#include "ui/views/widget/widget.h"
using gfx::Animation;
using views::View;
namespace ash {
namespace internal {
const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM = 0;
const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT = 1;
const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT = 2;
const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT = 3;
// Default amount content is inset on the left edge.
const int kDefaultLeadingInset = 8;
// Minimum distance before drag starts.
const int kMinimumDragDistance = 8;
// Size between the buttons.
const int kButtonSpacing = 4;
const int kAlternateButtonSpacing = 10;
// Size allocated to for each button.
const int kButtonSize = 44;
// Additional spacing for the left and right side of icons.
const int kHorizontalIconSpacing = 2;
// Inset for items which do not have an icon.
const int kHorizontalNoIconInsetSpacing =
kHorizontalIconSpacing + kDefaultLeadingInset;
// The proportion of the shelf space reserved for non-panel icons. Panels
// may flow into this space but will be put into the overflow bubble if there
// is contention for the space.
const float kReservedNonPanelIconProportion = 0.67f;
// This is the command id of the menu item which contains the name of the menu.
const int kCommandIdOfMenuName = 0;
// The background color of the active item in the list.
const SkColor kActiveListItemBackgroundColor = SkColorSetRGB(203 , 219, 241);
// The background color of the active & hovered item in the list.
const SkColor kFocusedActiveListItemBackgroundColor =
SkColorSetRGB(193, 211, 236);
// The text color of the caption item in a list.
const SkColor kCaptionItemForegroundColor = SK_ColorBLACK;
// The maximum allowable length of a menu line of an application menu in pixels.
const int kMaximumAppMenuItemLength = 350;
// The distance of the cursor from the outer rim of the shelf before it
// separates.
const int kRipOffDistance = 48;
// The rip off drag and drop proxy image should get scaled by this factor.
const float kDragAndDropProxyScale = 1.5f;
// The opacity represents that this partially disappeared item will get removed.
const float kDraggedImageOpacity = 0.5f;
namespace {
// The MenuModelAdapter gets slightly changed to adapt the menu appearance to
// our requirements.
class ShelfMenuModelAdapter : public views::MenuModelAdapter {
public:
explicit ShelfMenuModelAdapter(ShelfMenuModel* menu_model);
// views::MenuModelAdapter:
virtual const gfx::FontList* GetLabelFontList(int command_id) const OVERRIDE;
virtual bool IsCommandEnabled(int id) const OVERRIDE;
virtual void GetHorizontalIconMargins(int id,
int icon_size,
int* left_margin,
int* right_margin) const OVERRIDE;
virtual bool GetForegroundColor(int command_id,
bool is_hovered,
SkColor* override_color) const OVERRIDE;
virtual bool GetBackgroundColor(int command_id,
bool is_hovered,
SkColor* override_color) const OVERRIDE;
virtual int GetMaxWidthForMenu(views::MenuItemView* menu) OVERRIDE;
virtual bool ShouldReserveSpaceForSubmenuIndicator() const OVERRIDE;
private:
ShelfMenuModel* menu_model_;
DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelAdapter);
};
ShelfMenuModelAdapter::ShelfMenuModelAdapter(ShelfMenuModel* menu_model)
: MenuModelAdapter(menu_model),
menu_model_(menu_model) {
}
const gfx::FontList* ShelfMenuModelAdapter::GetLabelFontList(
int command_id) const {
if (command_id != kCommandIdOfMenuName)
return MenuModelAdapter::GetLabelFontList(command_id);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
return &rb.GetFontList(ui::ResourceBundle::BoldFont);
}
bool ShelfMenuModelAdapter::IsCommandEnabled(int id) const {
return id != kCommandIdOfMenuName;
}
bool ShelfMenuModelAdapter::GetForegroundColor(int command_id,
bool is_hovered,
SkColor* override_color) const {
if (command_id != kCommandIdOfMenuName)
return false;
*override_color = kCaptionItemForegroundColor;
return true;
}
bool ShelfMenuModelAdapter::GetBackgroundColor(int command_id,
bool is_hovered,
SkColor* override_color) const {
if (!menu_model_->IsCommandActive(command_id))
return false;
*override_color = is_hovered ? kFocusedActiveListItemBackgroundColor :
kActiveListItemBackgroundColor;
return true;
}
void ShelfMenuModelAdapter::GetHorizontalIconMargins(int command_id,
int icon_size,
int* left_margin,
int* right_margin) const {
*left_margin = kHorizontalIconSpacing;
*right_margin = (command_id != kCommandIdOfMenuName) ?
kHorizontalIconSpacing : -(icon_size + kHorizontalNoIconInsetSpacing);
}
int ShelfMenuModelAdapter::GetMaxWidthForMenu(views::MenuItemView* menu) {
return kMaximumAppMenuItemLength;
}
bool ShelfMenuModelAdapter::ShouldReserveSpaceForSubmenuIndicator() const {
return false;
}
// Custom FocusSearch used to navigate the shelf in the order items are in
// the ViewModel.
class ShelfFocusSearch : public views::FocusSearch {
public:
explicit ShelfFocusSearch(views::ViewModel* view_model)
: FocusSearch(NULL, true, true),
view_model_(view_model) {}
virtual ~ShelfFocusSearch() {}
// views::FocusSearch overrides:
virtual View* FindNextFocusableView(
View* starting_view,
bool reverse,
Direction direction,
bool check_starting_view,
views::FocusTraversable** focus_traversable,
View** focus_traversable_view) OVERRIDE {
int index = view_model_->GetIndexOfView(starting_view);
if (index == -1)
return view_model_->view_at(0);
if (reverse) {
--index;
if (index < 0)
index = view_model_->view_size() - 1;
} else {
++index;
if (index >= view_model_->view_size())
index = 0;
}
return view_model_->view_at(index);
}
private:
views::ViewModel* view_model_;
DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch);
};
// AnimationDelegate used when inserting a new item. This steadily increases the
// opacity of the layer as the animation progress.
class FadeInAnimationDelegate
: public views::BoundsAnimator::OwnedAnimationDelegate {
public:
explicit FadeInAnimationDelegate(views::View* view) : view_(view) {}
virtual ~FadeInAnimationDelegate() {}
// AnimationDelegate overrides:
virtual void AnimationProgressed(const Animation* animation) OVERRIDE {
view_->layer()->SetOpacity(animation->GetCurrentValue());
view_->layer()->ScheduleDraw();
}
virtual void AnimationEnded(const Animation* animation) OVERRIDE {
view_->layer()->SetOpacity(1.0f);
view_->layer()->ScheduleDraw();
}
virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
view_->layer()->SetOpacity(1.0f);
view_->layer()->ScheduleDraw();
}
private:
views::View* view_;
DISALLOW_COPY_AND_ASSIGN(FadeInAnimationDelegate);
};
void ReflectItemStatus(const LauncherItem& item, ShelfButton* button) {
switch (item.status) {
case STATUS_CLOSED:
button->ClearState(ShelfButton::STATE_ACTIVE);
button->ClearState(ShelfButton::STATE_RUNNING);
button->ClearState(ShelfButton::STATE_ATTENTION);
break;
case STATUS_RUNNING:
button->ClearState(ShelfButton::STATE_ACTIVE);
button->AddState(ShelfButton::STATE_RUNNING);
button->ClearState(ShelfButton::STATE_ATTENTION);
break;
case STATUS_ACTIVE:
button->AddState(ShelfButton::STATE_ACTIVE);
button->ClearState(ShelfButton::STATE_RUNNING);
button->ClearState(ShelfButton::STATE_ATTENTION);
break;
case STATUS_ATTENTION:
button->ClearState(ShelfButton::STATE_ACTIVE);
button->ClearState(ShelfButton::STATE_RUNNING);
button->AddState(ShelfButton::STATE_ATTENTION);
break;
}
}
} // namespace
// AnimationDelegate used when deleting an item. This steadily decreased the
// opacity of the layer as the animation progress.
class ShelfView::FadeOutAnimationDelegate
: public views::BoundsAnimator::OwnedAnimationDelegate {
public:
FadeOutAnimationDelegate(ShelfView* host, views::View* view)
: shelf_view_(host),
view_(view) {}
virtual ~FadeOutAnimationDelegate() {}
// AnimationDelegate overrides:
virtual void AnimationProgressed(const Animation* animation) OVERRIDE {
view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
view_->layer()->ScheduleDraw();
}
virtual void AnimationEnded(const Animation* animation) OVERRIDE {
shelf_view_->OnFadeOutAnimationEnded();
}
virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
}
private:
ShelfView* shelf_view_;
scoped_ptr<views::View> view_;
DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate);
};
// AnimationDelegate used to trigger fading an element in. When an item is
// inserted this delegate is attached to the animation that expands the size of
// the item. When done it kicks off another animation to fade the item in.
class ShelfView::StartFadeAnimationDelegate
: public views::BoundsAnimator::OwnedAnimationDelegate {
public:
StartFadeAnimationDelegate(ShelfView* host,
views::View* view)
: shelf_view_(host),
view_(view) {}
virtual ~StartFadeAnimationDelegate() {}
// AnimationDelegate overrides:
virtual void AnimationEnded(const Animation* animation) OVERRIDE {
shelf_view_->FadeIn(view_);
}
virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
view_->layer()->SetOpacity(1.0f);
}
private:
ShelfView* shelf_view_;
views::View* view_;
DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
};
ShelfView::ShelfView(ShelfModel* model,
ShelfDelegate* delegate,
ShelfLayoutManager* manager)
: model_(model),
delegate_(delegate),
view_model_(new views::ViewModel),
first_visible_index_(0),
last_visible_index_(-1),
overflow_button_(NULL),
owner_overflow_bubble_(NULL),
drag_pointer_(NONE),
drag_view_(NULL),
drag_offset_(0),
start_drag_index_(-1),
context_menu_id_(0),
leading_inset_(kDefaultLeadingInset),
cancelling_drag_model_changed_(false),
last_hidden_index_(0),
closing_event_time_(base::TimeDelta()),
got_deleted_(NULL),
drag_and_drop_item_pinned_(false),
drag_and_drop_launcher_id_(0),
dragged_off_shelf_(false),
snap_back_from_rip_off_view_(NULL),
item_manager_(Shell::GetInstance()->shelf_item_delegate_manager()),
layout_manager_(manager),
overflow_mode_(false),
main_shelf_(NULL),
dragged_off_from_overflow_to_shelf_(false) {
DCHECK(model_);
bounds_animator_.reset(new views::BoundsAnimator(this));
bounds_animator_->AddObserver(this);
set_context_menu_controller(this);
focus_search_.reset(new ShelfFocusSearch(view_model_.get()));
tooltip_.reset(new ShelfTooltipManager(manager, this));
}
ShelfView::~ShelfView() {
bounds_animator_->RemoveObserver(this);
model_->RemoveObserver(this);
// If we are inside the MenuRunner, we need to know if we were getting
// deleted while it was running.
if (got_deleted_)
*got_deleted_ = true;
}
void ShelfView::Init() {
model_->AddObserver(this);
const LauncherItems& items(model_->items());
for (LauncherItems::const_iterator i = items.begin(); i != items.end(); ++i) {
views::View* child = CreateViewForItem(*i);
child->SetPaintToLayer(true);
view_model_->Add(child, static_cast<int>(i - items.begin()));
AddChildView(child);
}
ShelfStatusChanged();
overflow_button_ = new OverflowButton(this);
overflow_button_->set_context_menu_controller(this);
ConfigureChildView(overflow_button_);
AddChildView(overflow_button_);
UpdateFirstButtonPadding();
// We'll layout when our bounds change.
}
void ShelfView::OnShelfAlignmentChanged() {
UpdateFirstButtonPadding();
overflow_button_->OnShelfAlignmentChanged();
LayoutToIdealBounds();
for (int i=0; i < view_model_->view_size(); ++i) {
// TODO: remove when AppIcon is a Shelf Button.
if (TYPE_APP_LIST == model_->items()[i].type &&
!ash::switches::UseAlternateShelfLayout()) {
static_cast<AppListButton*>(view_model_->view_at(i))->SetImageAlignment(
layout_manager_->SelectValueForShelfAlignment(
views::ImageButton::ALIGN_CENTER,
views::ImageButton::ALIGN_LEFT,
views::ImageButton::ALIGN_RIGHT,
views::ImageButton::ALIGN_CENTER),
layout_manager_->SelectValueForShelfAlignment(
views::ImageButton::ALIGN_TOP,
views::ImageButton::ALIGN_MIDDLE,
views::ImageButton::ALIGN_MIDDLE,
views::ImageButton::ALIGN_BOTTOM));
}
if (i >= first_visible_index_ && i <= last_visible_index_)
view_model_->view_at(i)->Layout();
}
tooltip_->Close();
if (overflow_bubble_)
overflow_bubble_->Hide();
}
void ShelfView::SchedulePaintForAllButtons() {
for (int i = 0; i < view_model_->view_size(); ++i) {
if (i >= first_visible_index_ && i <= last_visible_index_)
view_model_->view_at(i)->SchedulePaint();
}
if (overflow_button_ && overflow_button_->visible())
overflow_button_->SchedulePaint();
}
gfx::Rect ShelfView::GetIdealBoundsOfItemIcon(LauncherID id) {
int index = model_->ItemIndexByID(id);
if (index == -1 || (index > last_visible_index_ &&
index < model_->FirstPanelIndex()))
return gfx::Rect();
const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index));
DCHECK_NE(TYPE_APP_LIST, model_->items()[index].type);
ShelfButton* button = static_cast<ShelfButton*>(view_model_->view_at(index));
gfx::Rect icon_bounds = button->GetIconBounds();
return gfx::Rect(GetMirroredXWithWidthInView(
ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()),
ideal_bounds.y() + icon_bounds.y(),
icon_bounds.width(),
icon_bounds.height());
}
void ShelfView::UpdatePanelIconPosition(LauncherID id,
const gfx::Point& midpoint) {
int current_index = model_->ItemIndexByID(id);
int first_panel_index = model_->FirstPanelIndex();
if (current_index < first_panel_index)
return;
gfx::Point midpoint_in_view(GetMirroredXInView(midpoint.x()),
midpoint.y());
int target_index = current_index;
while (target_index > first_panel_index &&
layout_manager_->PrimaryAxisValue(
view_model_->ideal_bounds(target_index).x(),
view_model_->ideal_bounds(target_index).y()) >
layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
midpoint_in_view.y())) {
--target_index;
}
while (target_index < view_model_->view_size() - 1 &&
layout_manager_->PrimaryAxisValue(
view_model_->ideal_bounds(target_index).right(),
view_model_->ideal_bounds(target_index).bottom()) <
layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
midpoint_in_view.y())) {
++target_index;
}
if (current_index != target_index)
model_->Move(current_index, target_index);
}
bool ShelfView::IsShowingMenu() const {
return (launcher_menu_runner_.get() &&
launcher_menu_runner_->IsRunning());
}
bool ShelfView::IsShowingOverflowBubble() const {
return overflow_bubble_.get() && overflow_bubble_->IsShowing();
}
views::View* ShelfView::GetAppListButtonView() const {
for (int i = 0; i < model_->item_count(); ++i) {
if (model_->items()[i].type == TYPE_APP_LIST)
return view_model_->view_at(i);
}
NOTREACHED() << "Applist button not found";
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
// ShelfView, FocusTraversable implementation:
views::FocusSearch* ShelfView::GetFocusSearch() {
return focus_search_.get();
}
views::FocusTraversable* ShelfView::GetFocusTraversableParent() {
return parent()->GetFocusTraversable();
}
View* ShelfView::GetFocusTraversableParentView() {
return this;
}
void ShelfView::CreateDragIconProxy(
const gfx::Point& location_in_screen_coordinates,
const gfx::ImageSkia& icon,
views::View* replaced_view,
const gfx::Vector2d& cursor_offset_from_center,
float scale_factor) {
drag_replaced_view_ = replaced_view;
drag_image_.reset(new ash::internal::DragImageView(
drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow(),
ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE));
drag_image_->SetImage(icon);
gfx::Size size = drag_image_->GetPreferredSize();
size.set_width(size.width() * scale_factor);
size.set_height(size.height() * scale_factor);
drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) +
cursor_offset_from_center;
gfx::Rect drag_image_bounds(
location_in_screen_coordinates - drag_image_offset_,
size);
drag_image_->SetBoundsInScreen(drag_image_bounds);
drag_image_->SetWidgetVisible(true);
}
void ShelfView::UpdateDragIconProxy(
const gfx::Point& location_in_screen_coordinates) {
drag_image_->SetScreenPosition(
location_in_screen_coordinates - drag_image_offset_);
}
void ShelfView::DestroyDragIconProxy() {
drag_image_.reset();
drag_image_offset_ = gfx::Vector2d(0, 0);
}
bool ShelfView::StartDrag(const std::string& app_id,
const gfx::Point& location_in_screen_coordinates) {
// Bail if an operation is already going on - or the cursor is not inside.
// This could happen if mouse / touch operations overlap.
if (drag_and_drop_launcher_id_ ||
!GetBoundsInScreen().Contains(location_in_screen_coordinates))
return false;
// If the AppsGridView (which was dispatching this event) was opened by our
// button, ShelfView dragging operations are locked and we have to unlock.
CancelDrag(-1);
drag_and_drop_item_pinned_ = false;
drag_and_drop_app_id_ = app_id;
drag_and_drop_launcher_id_ =
delegate_->GetLauncherIDForAppID(drag_and_drop_app_id_);
// Check if the application is known and pinned - if not, we have to pin it so
// that we can re-arrange the shelf order accordingly. Note that items have
// to be pinned to give them the same (order) possibilities as a shortcut.
// When an item is dragged from overflow to shelf, IsShowingOverflowBubble()
// returns true. At this time, we don't need to pin the item.
if (!IsShowingOverflowBubble() &&
(!drag_and_drop_launcher_id_ ||
!delegate_->IsAppPinned(app_id))) {
delegate_->PinAppWithID(app_id);
drag_and_drop_launcher_id_ =
delegate_->GetLauncherIDForAppID(drag_and_drop_app_id_);
if (!drag_and_drop_launcher_id_)
return false;
drag_and_drop_item_pinned_ = true;
}
views::View* drag_and_drop_view = view_model_->view_at(
model_->ItemIndexByID(drag_and_drop_launcher_id_));
DCHECK(drag_and_drop_view);
// Since there is already an icon presented by the caller, we hide this item
// for now. That has to be done by reducing the size since the visibility will
// change once a regrouping animation is performed.
pre_drag_and_drop_size_ = drag_and_drop_view->size();
drag_and_drop_view->SetSize(gfx::Size());
// First we have to center the mouse cursor over the item.
gfx::Point pt = drag_and_drop_view->GetBoundsInScreen().CenterPoint();
views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
gfx::Point point_in_root = location_in_screen_coordinates;
ash::wm::ConvertPointFromScreen(
ash::wm::GetRootWindowAt(location_in_screen_coordinates),
&point_in_root);
ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root, 0, 0);
PointerPressedOnButton(drag_and_drop_view,
ShelfButtonHost::DRAG_AND_DROP,
event);
// Drag the item where it really belongs.
Drag(location_in_screen_coordinates);
return true;
}
bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) {
if (!drag_and_drop_launcher_id_ ||
!GetBoundsInScreen().Contains(location_in_screen_coordinates))
return false;
gfx::Point pt = location_in_screen_coordinates;
views::View* drag_and_drop_view = view_model_->view_at(
model_->ItemIndexByID(drag_and_drop_launcher_id_));
ConvertPointFromScreen(drag_and_drop_view, &pt);
gfx::Point point_in_root = location_in_screen_coordinates;
ash::wm::ConvertPointFromScreen(
ash::wm::GetRootWindowAt(location_in_screen_coordinates),
&point_in_root);
ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root, 0, 0);
PointerDraggedOnButton(drag_and_drop_view,
ShelfButtonHost::DRAG_AND_DROP,
event);
return true;
}
void ShelfView::EndDrag(bool cancel) {
if (!drag_and_drop_launcher_id_)
return;
views::View* drag_and_drop_view = view_model_->view_at(
model_->ItemIndexByID(drag_and_drop_launcher_id_));
PointerReleasedOnButton(
drag_and_drop_view, ShelfButtonHost::DRAG_AND_DROP, cancel);
// Either destroy the temporarily created item - or - make the item visible.
if (drag_and_drop_item_pinned_ && cancel)
delegate_->UnpinAppWithID(drag_and_drop_app_id_);
else if (drag_and_drop_view) {
if (cancel) {
// When a hosted drag gets canceled, the item can remain in the same slot
// and it might have moved within the bounds. In that case the item need
// to animate back to its correct location.
AnimateToIdealBounds();
} else {
drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
}
}
drag_and_drop_launcher_id_ = 0;
}
void ShelfView::LayoutToIdealBounds() {
if (bounds_animator_->IsAnimating()) {
AnimateToIdealBounds();
return;
}
IdealBounds ideal_bounds;
CalculateIdealBounds(&ideal_bounds);
views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
}
void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() {
// The overflow button is not shown in overflow mode.
overflow_button_->SetVisible(false);
int last_button_index = model_->FirstPanelIndex() - 1;
DCHECK_LT(last_visible_index_, view_model_->view_size());
for (int i = 0; i < view_model_->view_size(); ++i) {
bool visible = i >= first_visible_index_ &&
i <= last_visible_index_;
if (!ash::switches::UseAlternateShelfLayout())
visible &= i != last_button_index;
// To track the dragging of |drag_view_| continuously, its visibility
// should be always true regardless of its position.
if (dragged_off_from_overflow_to_shelf_ &&
view_model_->view_at(i) == drag_view_)
view_model_->view_at(i)->SetVisible(true);
else
view_model_->view_at(i)->SetVisible(visible);
}
}
void ShelfView::CalculateIdealBounds(IdealBounds* bounds) {
int available_size = layout_manager_->PrimaryAxisValue(width(), height());
DCHECK(model_->item_count() == view_model_->view_size());
if (!available_size)
return;
int first_panel_index = model_->FirstPanelIndex();
int last_button_index = first_panel_index - 1;
// Initial x,y values account both leading_inset in primary
// coordinate and secondary coordinate based on the dynamic edge of the
// shelf (eg top edge on bottom-aligned shelf).
int inset = ash::switches::UseAlternateShelfLayout() ? 0 : leading_inset_;
int x = layout_manager_->SelectValueForShelfAlignment(inset, 0, 0, inset);
int y = layout_manager_->SelectValueForShelfAlignment(0, inset, inset, 0);
int button_size = GetButtonSize();
int button_spacing = GetButtonSpacing();
int w = layout_manager_->PrimaryAxisValue(button_size, width());
int h = layout_manager_->PrimaryAxisValue(height(), button_size);
for (int i = 0; i < view_model_->view_size(); ++i) {
if (i < first_visible_index_) {
view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
continue;
}
view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
if (i != last_button_index) {
x = layout_manager_->PrimaryAxisValue(x + w + button_spacing, x);
y = layout_manager_->PrimaryAxisValue(y, y + h + button_spacing);
}
}
if (is_overflow_mode()) {
UpdateAllButtonsVisibilityInOverflowMode();
return;
}
// To address Fitt's law, we make the first shelf button include the
// leading inset (if there is one).
if (!ash::switches::UseAlternateShelfLayout()) {
if (view_model_->view_size() > 0) {
view_model_->set_ideal_bounds(0, gfx::Rect(gfx::Size(
layout_manager_->PrimaryAxisValue(inset + w, w),
layout_manager_->PrimaryAxisValue(h, inset + h))));
}
}
// Right aligned icons.
int end_position = available_size - button_spacing;
x = layout_manager_->PrimaryAxisValue(end_position, 0);
y = layout_manager_->PrimaryAxisValue(0, end_position);
for (int i = view_model_->view_size() - 1;
i >= first_panel_index; --i) {
x = layout_manager_->PrimaryAxisValue(x - w - button_spacing, x);
y = layout_manager_->PrimaryAxisValue(y, y - h - button_spacing);
view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
end_position = layout_manager_->PrimaryAxisValue(x, y);
}
// Icons on the left / top are guaranteed up to kLeftIconProportion of
// the available space.
int last_icon_position = layout_manager_->PrimaryAxisValue(
view_model_->ideal_bounds(last_button_index).right(),
view_model_->ideal_bounds(last_button_index).bottom())
+ button_size + inset;
if (!ash::switches::UseAlternateShelfLayout())
last_icon_position += button_size;
int reserved_icon_space = available_size * kReservedNonPanelIconProportion;
if (last_icon_position < reserved_icon_space)
end_position = last_icon_position;
else
end_position = std::max(end_position, reserved_icon_space);
bounds->overflow_bounds.set_size(
gfx::Size(layout_manager_->PrimaryAxisValue(w, width()),
layout_manager_->PrimaryAxisValue(height(), h)));
if (ash::switches::UseAlternateShelfLayout()) {
last_visible_index_ = DetermineLastVisibleIndex(
end_position - button_size);
} else {
last_visible_index_ = DetermineLastVisibleIndex(
end_position - inset - 2 * button_size);
}
last_hidden_index_ = DetermineFirstVisiblePanelIndex(end_position) - 1;
bool show_overflow =
((ash::switches::UseAlternateShelfLayout() ? 0 : 1) +
last_visible_index_ < last_button_index ||
last_hidden_index_ >= first_panel_index);
// Create Space for the overflow button
if (show_overflow && ash::switches::UseAlternateShelfLayout() &&
last_visible_index_ > 0 && last_visible_index_ < last_button_index)
--last_visible_index_;
for (int i = 0; i < view_model_->view_size(); ++i) {
bool visible = i <= last_visible_index_ || i > last_hidden_index_;
// Always show the app list.
if (!ash::switches::UseAlternateShelfLayout())
visible |= (i == last_button_index);
// To receive drag event continously from |drag_view_| during the dragging
// off from the shelf, don't make |drag_view_| invisible. It will be
// eventually invisible and removed from the |view_model_| by
// FinalizeRipOffDrag().
if (dragged_off_shelf_ && view_model_->view_at(i) == drag_view_)
continue;
view_model_->view_at(i)->SetVisible(visible);
}
overflow_button_->SetVisible(show_overflow);
if (show_overflow) {
DCHECK_NE(0, view_model_->view_size());
if (last_visible_index_ == -1) {
x = layout_manager_->SelectValueForShelfAlignment(inset, 0, 0, inset);
y = layout_manager_->SelectValueForShelfAlignment(0, inset, inset, 0);
} else if (last_visible_index_ == last_button_index
&& !ash::switches::UseAlternateShelfLayout()) {
x = view_model_->ideal_bounds(last_visible_index_).x();
y = view_model_->ideal_bounds(last_visible_index_).y();
} else {
x = layout_manager_->PrimaryAxisValue(
view_model_->ideal_bounds(last_visible_index_).right(),
view_model_->ideal_bounds(last_visible_index_).x());
y = layout_manager_->PrimaryAxisValue(
view_model_->ideal_bounds(last_visible_index_).y(),
view_model_->ideal_bounds(last_visible_index_).bottom());
}
// Set all hidden panel icon positions to be on the overflow button.
for (int i = first_panel_index; i <= last_hidden_index_; ++i)
view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
// Add more space between last visible item and overflow button.
// Without this, two buttons look too close compared with other items.
if (ash::switches::UseAlternateShelfLayout()) {
x = layout_manager_->PrimaryAxisValue(x + button_spacing, x);
y = layout_manager_->PrimaryAxisValue(y, y + button_spacing);
}
bounds->overflow_bounds.set_x(x);
bounds->overflow_bounds.set_y(y);
if (!ash::switches::UseAlternateShelfLayout()) {
// Position app list after overflow button.
gfx::Rect app_list_bounds = view_model_->ideal_bounds(last_button_index);
x = layout_manager_->PrimaryAxisValue(x + w + button_spacing, x);
y = layout_manager_->PrimaryAxisValue(y, y + h + button_spacing);
app_list_bounds.set_x(x);
app_list_bounds.set_y(y);
view_model_->set_ideal_bounds(last_button_index, app_list_bounds);
}
if (overflow_bubble_.get() && overflow_bubble_->IsShowing())
UpdateOverflowRange(overflow_bubble_->shelf_view());
} else {
if (overflow_bubble_)
overflow_bubble_->Hide();
}
}
int ShelfView::DetermineLastVisibleIndex(int max_value) const {
int index = model_->FirstPanelIndex() - 1;
while (index >= 0 &&
layout_manager_->PrimaryAxisValue(
view_model_->ideal_bounds(index).right(),
view_model_->ideal_bounds(index).bottom()) > max_value) {
index--;
}
return index;
}
int ShelfView::DetermineFirstVisiblePanelIndex(int min_value) const {
int index = model_->FirstPanelIndex();
while (index < view_model_->view_size() &&
layout_manager_->PrimaryAxisValue(
view_model_->ideal_bounds(index).right(),
view_model_->ideal_bounds(index).bottom()) < min_value) {
++index;
}
return index;
}
void ShelfView::AddIconObserver(ShelfIconObserver* observer) {
observers_.AddObserver(observer);
}
void ShelfView::RemoveIconObserver(ShelfIconObserver* observer) {
observers_.RemoveObserver(observer);
}
void ShelfView::AnimateToIdealBounds() {
IdealBounds ideal_bounds;
CalculateIdealBounds(&ideal_bounds);
for (int i = 0; i < view_model_->view_size(); ++i) {
View* view = view_model_->view_at(i);
bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
// Now that the item animation starts, we have to make sure that the
// padding of the first gets properly transferred to the new first item.
if (i && view->border())
view->set_border(NULL);
else if (!i && !view->border())
UpdateFirstButtonPadding();
}
overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
}
views::View* ShelfView::CreateViewForItem(const LauncherItem& item) {
views::View* view = NULL;
switch (item.type) {
case TYPE_BROWSER_SHORTCUT:
case TYPE_APP_SHORTCUT:
case TYPE_WINDOWED_APP:
case TYPE_PLATFORM_APP:
case TYPE_DIALOG:
case TYPE_APP_PANEL: {
ShelfButton* button = ShelfButton::Create(this, this, layout_manager_);
button->SetImage(item.image);
ReflectItemStatus(item, button);
view = button;
break;
}
case TYPE_APP_LIST: {
if (ash::switches::UseAlternateShelfLayout()) {
view = new AlternateAppListButton(this,
this,
layout_manager_->shelf_widget());
} else {
// TODO(dave): turn this into a ShelfButton too.
AppListButton* button = new AppListButton(this, this);
button->SetImageAlignment(
layout_manager_->SelectValueForShelfAlignment(
views::ImageButton::ALIGN_CENTER,
views::ImageButton::ALIGN_LEFT,
views::ImageButton::ALIGN_RIGHT,
views::ImageButton::ALIGN_CENTER),
layout_manager_->SelectValueForShelfAlignment(
views::ImageButton::ALIGN_TOP,
views::ImageButton::ALIGN_MIDDLE,
views::ImageButton::ALIGN_MIDDLE,
views::ImageButton::ALIGN_BOTTOM));
view = button;
}
break;
}
default:
break;
}
view->set_context_menu_controller(this);
DCHECK(view);
ConfigureChildView(view);
return view;
}
void ShelfView::FadeIn(views::View* view) {
view->SetVisible(true);
view->layer()->SetOpacity(0);
AnimateToIdealBounds();
bounds_animator_->SetAnimationDelegate(
view, new FadeInAnimationDelegate(view), true);
}
void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
DCHECK(!dragging());
DCHECK(drag_view_);
drag_pointer_ = pointer;
start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
if (start_drag_index_== -1) {
CancelDrag(-1);
return;
}
// If the item is no longer draggable, bail out.
ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
model_->items()[start_drag_index_].id);
if (!item_delegate->IsDraggable()) {
CancelDrag(-1);
return;
}
// Move the view to the front so that it appears on top of other views.
ReorderChildView(drag_view_, -1);
bounds_animator_->StopAnimatingView(drag_view_);
}
void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
// Due to a syncing operation the application might have been removed.
// Bail if it is gone.
int current_index = view_model_->GetIndexOfView(drag_view_);