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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
|
// 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/launcher/launcher_view.h"
#include <algorithm>
#include "ash/ash_constants.h"
#include "ash/launcher/app_list_button.h"
#include "ash/launcher/launcher_button.h"
#include "ash/launcher/launcher_delegate.h"
#include "ash/launcher/launcher_icon_observer.h"
#include "ash/launcher/launcher_model.h"
#include "ash/launcher/launcher_tooltip_manager.h"
#include "ash/launcher/overflow_bubble.h"
#include "ash/launcher/overflow_button.h"
#include "ash/launcher/tabbed_launcher_button.h"
#include "ash/shell_delegate.h"
#include "ash/wm/shelf_layout_manager.h"
#include "base/auto_reset.h"
#include "base/memory/scoped_ptr.h"
#include "grit/ash_strings.h"
#include "grit/ash_resources.h"
#include "ui/aura/window.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/gfx/canvas.h"
#include "ui/views/animation/bounds_animator.h"
#include "ui/views/border.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/focus_border.h"
#include "ui/views/view_model.h"
#include "ui/views/view_model_utils.h"
#include "ui/views/widget/widget.h"
using ui::Animation;
using views::View;
namespace ash {
namespace internal {
// 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;
namespace {
// Custom FocusSearch used to navigate the launcher in the order items are in
// the ViewModel.
class LauncherFocusSearch : public views::FocusSearch {
public:
explicit LauncherFocusSearch(views::ViewModel* view_model)
: FocusSearch(NULL, true, true),
view_model_(view_model) {}
virtual ~LauncherFocusSearch() {}
// 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(LauncherFocusSearch);
};
class LauncherButtonFocusBorder : public views::FocusBorder {
public:
LauncherButtonFocusBorder() {}
virtual ~LauncherButtonFocusBorder() {}
private:
// views::FocusBorder overrides:
virtual void Paint(const View& view, gfx::Canvas* canvas) const OVERRIDE {
gfx::Rect rect(view.GetLocalBounds());
rect.Inset(1, 1);
canvas->DrawRect(rect, kFocusBorderColor);
}
DISALLOW_COPY_AND_ASSIGN(LauncherButtonFocusBorder);
};
// ui::SimpleMenuModel::Delegate implementation that remembers the id of the
// menu that was activated.
class MenuDelegateImpl : public ui::SimpleMenuModel::Delegate {
public:
MenuDelegateImpl() : activated_command_id_(-1) {}
int activated_command_id() const { return activated_command_id_; }
// ui::SimpleMenuModel::Delegate overrides:
virtual bool IsCommandIdChecked(int command_id) const OVERRIDE {
return false;
}
virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE {
return true;
}
virtual bool GetAcceleratorForCommandId(
int command_id,
ui::Accelerator* accelerator) OVERRIDE {
return false;
}
virtual void ExecuteCommand(int command_id) OVERRIDE {
activated_command_id_ = command_id;
}
private:
// ID of the command passed to ExecuteCommand.
int activated_command_id_;
DISALLOW_COPY_AND_ASSIGN(MenuDelegateImpl);
};
// AnimationDelegate that deletes a view when done. This is used when a launcher
// item is removed, which triggers a remove animation. When the animation is
// done we delete the view.
class DeleteViewAnimationDelegate
: public views::BoundsAnimator::OwnedAnimationDelegate {
public:
explicit DeleteViewAnimationDelegate(views::View* view) : view_(view) {}
virtual ~DeleteViewAnimationDelegate() {}
private:
scoped_ptr<views::View> view_;
DISALLOW_COPY_AND_ASSIGN(DeleteViewAnimationDelegate);
};
// 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 ash::LauncherItem& item,
LauncherButton* button) {
switch (item.status) {
case STATUS_CLOSED:
button->ClearState(LauncherButton::STATE_ACTIVE);
button->ClearState(LauncherButton::STATE_RUNNING);
button->ClearState(LauncherButton::STATE_ATTENTION);
break;
case STATUS_RUNNING:
button->ClearState(LauncherButton::STATE_ACTIVE);
button->AddState(LauncherButton::STATE_RUNNING);
button->ClearState(LauncherButton::STATE_ATTENTION);
break;
case STATUS_ACTIVE:
button->AddState(LauncherButton::STATE_ACTIVE);
button->ClearState(LauncherButton::STATE_RUNNING);
button->ClearState(LauncherButton::STATE_ATTENTION);
break;
case STATUS_ATTENTION:
button->ClearState(LauncherButton::STATE_ACTIVE);
button->ClearState(LauncherButton::STATE_RUNNING);
button->AddState(LauncherButton::STATE_ATTENTION);
break;
}
}
} // namespace
// AnimationDelegate used when inserting a new item. This steadily decreased the
// opacity of the layer as the animation progress.
class LauncherView::FadeOutAnimationDelegate
: public views::BoundsAnimator::OwnedAnimationDelegate {
public:
FadeOutAnimationDelegate(LauncherView* host, views::View* view)
: launcher_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 {
launcher_view_->AnimateToIdealBounds();
}
virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
}
private:
LauncherView* launcher_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 LauncherView::StartFadeAnimationDelegate
: public views::BoundsAnimator::OwnedAnimationDelegate {
public:
StartFadeAnimationDelegate(LauncherView* host,
views::View* view)
: launcher_view_(host),
view_(view) {}
virtual ~StartFadeAnimationDelegate() {}
// AnimationDelegate overrides:
virtual void AnimationEnded(const Animation* animation) OVERRIDE {
launcher_view_->FadeIn(view_);
}
virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
view_->layer()->SetOpacity(1.0f);
}
private:
LauncherView* launcher_view_;
views::View* view_;
DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
};
LauncherView::LauncherView(LauncherModel* model,
LauncherDelegate* delegate,
ShelfLayoutManager* shelf_layout_manager)
: model_(model),
delegate_(delegate),
view_model_(new views::ViewModel),
first_visible_index_(0),
last_visible_index_(-1),
overflow_button_(NULL),
drag_pointer_(NONE),
drag_view_(NULL),
drag_offset_(0),
start_drag_index_(-1),
context_menu_id_(0),
leading_inset_(kDefaultLeadingInset) {
DCHECK(model_);
bounds_animator_.reset(new views::BoundsAnimator(this));
bounds_animator_->AddObserver(this);
set_context_menu_controller(this);
focus_search_.reset(new LauncherFocusSearch(view_model_.get()));
tooltip_.reset(new LauncherTooltipManager(
shelf_layout_manager, this));
}
LauncherView::~LauncherView() {
bounds_animator_->RemoveObserver(this);
model_->RemoveObserver(this);
}
void LauncherView::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);
}
UpdateFirstButtonPadding();
LauncherStatusChanged();
overflow_button_ = new OverflowButton(this);
overflow_button_->set_context_menu_controller(this);
ConfigureChildView(overflow_button_);
AddChildView(overflow_button_);
// We'll layout when our bounds change.
}
void LauncherView::SetAlignment(ShelfAlignment alignment) {
if (alignment_ == alignment)
return;
alignment_ = alignment;
UpdateFirstButtonPadding();
overflow_button_->OnShelfAlignmentChanged();
LayoutToIdealBounds();
tooltip_->UpdateArrowLocation();
if (overflow_bubble_.get())
overflow_bubble_->Hide();
}
gfx::Rect LauncherView::GetIdealBoundsOfItemIcon(LauncherID id) {
int index = model_->ItemIndexByID(id);
if (index == -1 || index > last_visible_index_)
return gfx::Rect();
const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index));
DCHECK_NE(TYPE_APP_LIST, model_->items()[index].type);
LauncherButton* button =
static_cast<LauncherButton*>(view_model_->view_at(index));
gfx::Rect icon_bounds = button->GetIconBounds();
return gfx::Rect(ideal_bounds.x() + icon_bounds.x(),
ideal_bounds.y() + icon_bounds.y(),
icon_bounds.width(), icon_bounds.height());
}
bool LauncherView::IsShowingMenu() const {
#if !defined(OS_MACOSX)
return (launcher_menu_runner_.get() &&
launcher_menu_runner_->IsRunning());
#endif
return false;
}
bool LauncherView::IsShowingOverflowBubble() const {
return overflow_bubble_.get() && overflow_bubble_->IsShowing();
}
views::View* LauncherView::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;
}
////////////////////////////////////////////////////////////////////////////////
// LauncherView, FocusTraversable implementation:
views::FocusSearch* LauncherView::GetFocusSearch() {
return focus_search_.get();
}
views::FocusTraversable* LauncherView::GetFocusTraversableParent() {
return parent()->GetFocusTraversable();
}
View* LauncherView::GetFocusTraversableParentView() {
return this;
}
void LauncherView::LayoutToIdealBounds() {
IdealBounds ideal_bounds;
CalculateIdealBounds(&ideal_bounds);
if (bounds_animator_->IsAnimating())
AnimateToIdealBounds();
else
views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
}
void LauncherView::CalculateIdealBounds(IdealBounds* bounds) {
ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
int available_size = shelf->PrimaryAxisValue(width(), height());
if (!available_size)
return;
// Initial x,y values account both leading_inset in primary
// coordinate and secondary coordinate based on the dynamic edge of the
// launcher (eg top edge on bottom-aligned launcher).
int x = shelf->SelectValueForShelfAlignment(
leading_inset(),
width() - kLauncherPreferredSize,
std::max(width() - kLauncherPreferredSize,
ShelfLayoutManager::kAutoHideSize + 1));
int y = shelf->PrimaryAxisValue(0, leading_inset());
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;
}
int w = shelf->PrimaryAxisValue(kLauncherPreferredSize, width());
int h = shelf->PrimaryAxisValue(height(), kLauncherPreferredSize);
view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
x = shelf->PrimaryAxisValue(x + w + kButtonSpacing, x);
y = shelf->PrimaryAxisValue(y, y + h + kButtonSpacing);
}
int app_list_index = view_model_->view_size() - 1;
if (is_overflow_mode()) {
last_visible_index_ = app_list_index - 1;
for (int i = 0; i < view_model_->view_size(); ++i) {
view_model_->view_at(i)->SetVisible(
i >= first_visible_index_ && i <= last_visible_index_);
}
return;
}
bounds->overflow_bounds.set_size(gfx::Size(
shelf->PrimaryAxisValue(kLauncherPreferredSize, width()),
shelf->PrimaryAxisValue(height(), kLauncherPreferredSize)));
last_visible_index_ = DetermineLastVisibleIndex(
available_size - leading_inset() - kLauncherPreferredSize -
kButtonSpacing - kLauncherPreferredSize);
bool show_overflow = (last_visible_index_ + 1 < app_list_index);
for (int i = 0; i < view_model_->view_size(); ++i) {
view_model_->view_at(i)->SetVisible(
i == app_list_index || i <= last_visible_index_);
}
overflow_button_->SetVisible(show_overflow);
if (show_overflow) {
DCHECK_NE(0, view_model_->view_size());
if (last_visible_index_ == -1) {
x = shelf->SelectValueForShelfAlignment(
leading_inset(),
width() - kLauncherPreferredSize,
std::max(width() - kLauncherPreferredSize,
ShelfLayoutManager::kAutoHideSize + 1));
y = shelf->PrimaryAxisValue(0, leading_inset());
} else {
x = shelf->PrimaryAxisValue(
view_model_->ideal_bounds(last_visible_index_).right(),
view_model_->ideal_bounds(last_visible_index_).x());
y = shelf->PrimaryAxisValue(
view_model_->ideal_bounds(last_visible_index_).y(),
view_model_->ideal_bounds(last_visible_index_).bottom());
}
gfx::Rect app_list_bounds = view_model_->ideal_bounds(app_list_index);
bounds->overflow_bounds.set_x(x);
bounds->overflow_bounds.set_y(y);
x = shelf->PrimaryAxisValue(x + kLauncherPreferredSize + kButtonSpacing, x);
y = shelf->PrimaryAxisValue(y, y + kLauncherPreferredSize + kButtonSpacing);
app_list_bounds.set_x(x);
app_list_bounds.set_y(y);
view_model_->set_ideal_bounds(app_list_index, app_list_bounds);
} else {
if (overflow_bubble_.get())
overflow_bubble_->Hide();
}
}
int LauncherView::DetermineLastVisibleIndex(int max_value) {
ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
int index = view_model_->view_size() - 1;
while (index >= 0 &&
shelf->PrimaryAxisValue(
view_model_->ideal_bounds(index).right(),
view_model_->ideal_bounds(index).bottom()) > max_value) {
index--;
}
return index;
}
void LauncherView::AddIconObserver(LauncherIconObserver* observer) {
observers_.AddObserver(observer);
}
void LauncherView::RemoveIconObserver(LauncherIconObserver* observer) {
observers_.RemoveObserver(observer);
}
void LauncherView::AnimateToIdealBounds() {
IdealBounds ideal_bounds;
CalculateIdealBounds(&ideal_bounds);
for (int i = 0; i < view_model_->view_size(); ++i) {
bounds_animator_->AnimateViewTo(view_model_->view_at(i),
view_model_->ideal_bounds(i));
}
overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
}
views::View* LauncherView::CreateViewForItem(const LauncherItem& item) {
views::View* view = NULL;
switch (item.type) {
case TYPE_TABBED: {
TabbedLauncherButton* button =
TabbedLauncherButton::Create(
this,
this,
tooltip_->shelf_layout_manager(),
item.is_incognito ?
TabbedLauncherButton::STATE_INCOGNITO :
TabbedLauncherButton::STATE_NOT_INCOGNITO);
button->SetTabImage(item.image);
ReflectItemStatus(item, button);
view = button;
break;
}
case TYPE_APP_SHORTCUT:
case TYPE_PLATFORM_APP:
case TYPE_APP_PANEL: {
LauncherButton* button = LauncherButton::Create(
this, this, tooltip_->shelf_layout_manager());
button->SetImage(item.image);
ReflectItemStatus(item, button);
view = button;
break;
}
case TYPE_APP_LIST: {
// TODO(dave): turn this into a LauncherButton too.
AppListButton* button = new AppListButton(this, this);
view = button;
break;
}
case TYPE_BROWSER_SHORTCUT: {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
LauncherButton* button = LauncherButton::Create(
this, this, tooltip_->shelf_layout_manager());
int image_id = delegate_ ?
delegate_->GetBrowserShortcutResourceId() :
IDR_AURA_LAUNCHER_BROWSER_SHORTCUT;
button->SetImage(*rb.GetImageNamed(image_id).ToImageSkia());
view = button;
break;
}
default:
break;
}
view->set_context_menu_controller(this);
view->set_focus_border(new LauncherButtonFocusBorder);
DCHECK(view);
ConfigureChildView(view);
return view;
}
void LauncherView::FadeIn(views::View* view) {
view->SetVisible(true);
view->layer()->SetOpacity(0);
AnimateToIdealBounds();
bounds_animator_->SetAnimationDelegate(
view, new FadeInAnimationDelegate(view), true);
}
void LauncherView::PrepareForDrag(Pointer pointer,
const ui::LocatedEvent& event) {
DCHECK(!dragging());
DCHECK(drag_view_);
drag_pointer_ = pointer;
start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
// If the item is no longer draggable, bail out.
if (start_drag_index_ == -1 ||
!delegate_->IsDraggable(model_->items()[start_drag_index_])) {
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 LauncherView::ContinueDrag(const ui::LocatedEvent& event) {
ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
// TODO: I don't think this works correctly with RTL.
gfx::Point drag_point(event.location());
views::View::ConvertPointToTarget(drag_view_, this, &drag_point);
int current_index = view_model_->GetIndexOfView(drag_view_);
DCHECK_NE(-1, current_index);
// If the item is no longer draggable, bail out.
if (current_index == -1 ||
!delegate_->IsDraggable(model_->items()[current_index])) {
CancelDrag(-1);
return;
}
// Constrain the location to the range of valid indices for the type.
std::pair<int, int> indices(GetDragRange(current_index));
int last_drag_index = indices.second;
// If the last index isn't valid, we're overflowing. Constrain to the app list
// (which is the last visible item).
if (last_drag_index > last_visible_index_)
last_drag_index = last_visible_index_;
int x = 0, y = 0;
if (shelf->IsHorizontalAlignment()) {
x = std::max(view_model_->ideal_bounds(indices.first).x(),
drag_point.x() - drag_offset_);
x = std::min(view_model_->ideal_bounds(last_drag_index).right() -
view_model_->ideal_bounds(current_index).width(),
x);
if (drag_view_->x() == x)
return;
drag_view_->SetX(x);
} else {
y = std::max(view_model_->ideal_bounds(indices.first).y(),
drag_point.y() - drag_offset_);
y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() -
view_model_->ideal_bounds(current_index).height(),
y);
if (drag_view_->y() == y)
return;
drag_view_->SetY(y);
}
int target_index =
views::ViewModelUtils::DetermineMoveIndex(
*view_model_, drag_view_,
shelf->IsHorizontalAlignment() ?
views::ViewModelUtils::HORIZONTAL :
views::ViewModelUtils::VERTICAL,
x, y);
target_index =
std::min(indices.second, std::max(target_index, indices.first));
if (target_index == current_index)
return;
// Change the model, the LauncherItemMoved() callback will handle the
// |view_model_| update.
model_->Move(current_index, target_index);
bounds_animator_->StopAnimatingView(drag_view_);
}
bool LauncherView::SameDragType(LauncherItemType typea,
LauncherItemType typeb) const {
switch (typea) {
case TYPE_TABBED:
case TYPE_APP_PANEL:
case TYPE_PLATFORM_APP:
return (typeb == TYPE_TABBED ||
typeb == TYPE_APP_PANEL ||
typeb == TYPE_PLATFORM_APP);
case TYPE_APP_SHORTCUT:
case TYPE_APP_LIST:
case TYPE_BROWSER_SHORTCUT:
return typeb == typea;
}
NOTREACHED();
return false;
}
std::pair<int, int> LauncherView::GetDragRange(int index) {
int min_index = -1;
int max_index = -1;
LauncherItemType type = model_->items()[index].type;
for (int i = 0; i < model_->item_count(); ++i) {
if (SameDragType(model_->items()[i].type, type)) {
if (min_index == -1)
min_index = i;
max_index = i;
}
}
return std::pair<int, int>(min_index, max_index);
}
void LauncherView::ConfigureChildView(views::View* view) {
view->SetPaintToLayer(true);
view->layer()->SetFillsBoundsOpaquely(false);
}
void LauncherView::ShowOverflowBubble() {
int first_overflow_index = last_visible_index_ + 1;
DCHECK_LT(first_overflow_index, view_model_->view_size() - 1);
if (!overflow_bubble_.get())
overflow_bubble_.reset(new OverflowBubble());
overflow_bubble_->Show(delegate_,
model_,
overflow_button_,
first_overflow_index);
Shell::GetInstance()->UpdateShelfVisibility();
}
void LauncherView::UpdateFirstButtonPadding() {
ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
// Creates an empty border for first launcher button to make included leading
// inset act as the button's padding. This is only needed on button creation
// and when shelf alignment changes.
if (view_model_->view_size() > 0) {
view_model_->view_at(0)->set_border(views::Border::CreateEmptyBorder(
shelf->PrimaryAxisValue(0, leading_inset()),
shelf->PrimaryAxisValue(leading_inset(), 0),
0,
0));
}
}
bool LauncherView::ShouldHideTooltip(const gfx::Point& cursor_location) {
gfx::Rect active_bounds;
for (int i = 0; i < child_count(); ++i) {
views::View* child = child_at(i);
if (child == overflow_button_)
continue;
// The tooltip shouldn't show over the app-list window.
if (child == GetAppListButtonView() &&
Shell::GetInstance()->GetAppListWindow())
continue;
gfx::Rect child_bounds = child->GetMirroredBounds();
active_bounds.Union(child_bounds);
}
return !active_bounds.Contains(cursor_location);
}
int LauncherView::CancelDrag(int modified_index) {
if (!drag_view_)
return modified_index;
bool was_dragging = dragging();
int drag_view_index = view_model_->GetIndexOfView(drag_view_);
drag_pointer_ = NONE;
drag_view_ = NULL;
if (drag_view_index == modified_index) {
// The view that was being dragged is being modified. Don't do anything.
return modified_index;
}
if (!was_dragging)
return modified_index;
// Restore previous position, tracking the position of the modified view.
views::View* removed_view =
(modified_index >= 0) ? view_model_->view_at(modified_index) : NULL;
model_->Move(drag_view_index, start_drag_index_);
return removed_view ? view_model_->GetIndexOfView(removed_view) : -1;
}
gfx::Size LauncherView::GetPreferredSize() {
IdealBounds ideal_bounds;
CalculateIdealBounds(&ideal_bounds);
const int app_list_index = view_model_->view_size() - 1;
const int last_button_index = is_overflow_mode() ?
last_visible_index_ : app_list_index;
const gfx::Rect last_button_bounds =
last_button_index >= first_visible_index_ ?
view_model_->view_at(last_button_index)->bounds() :
gfx::Rect(gfx::Size(kLauncherPreferredSize,
kLauncherPreferredSize));
ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
if (shelf->IsHorizontalAlignment()) {
return gfx::Size(last_button_bounds.right() + leading_inset(),
kLauncherPreferredSize);
}
return gfx::Size(kLauncherPreferredSize,
last_button_bounds.bottom() + leading_inset());
}
void LauncherView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
LayoutToIdealBounds();
FOR_EACH_OBSERVER(LauncherIconObserver, observers_,
OnLauncherIconPositionsChanged());
if (IsShowingOverflowBubble())
overflow_bubble_->Hide();
}
views::FocusTraversable* LauncherView::GetPaneFocusTraversable() {
return this;
}
void LauncherView::OnGestureEvent(ui::GestureEvent* event) {
if (gesture_handler_.ProcessGestureEvent(*event))
event->StopPropagation();
}
void LauncherView::LauncherItemAdded(int model_index) {
model_index = CancelDrag(model_index);
views::View* view = CreateViewForItem(model_->items()[model_index]);
AddChildView(view);
// Hide the view, it'll be made visible when the animation is done. Using
// opacity 0 here to avoid messing with CalculateIdealBounds which touches
// the view's visibility.
view->layer()->SetOpacity(0);
view_model_->Add(view, model_index);
// Give the button its ideal bounds. That way if we end up animating the
// button before this animation completes it doesn't appear at some random
// spot (because it was in the middle of animating from 0,0 0x0 to its
// target).
IdealBounds ideal_bounds;
CalculateIdealBounds(&ideal_bounds);
view->SetBoundsRect(view_model_->ideal_bounds(model_index));
// The first animation moves all the views to their target position. |view|
// is hidden, so it visually appears as though we are providing space for
// it. When done we'll fade the view in.
AnimateToIdealBounds();
if (model_index <= last_visible_index_) {
bounds_animator_->SetAnimationDelegate(
view, new StartFadeAnimationDelegate(this, view), true);
} else {
// Undo the hiding if animation does not run.
view->layer()->SetOpacity(1.0f);
}
}
void LauncherView::LauncherItemRemoved(int model_index, LauncherID id) {
#if !defined(OS_MACOSX)
if (id == context_menu_id_)
launcher_menu_runner_.reset();
#endif
model_index = CancelDrag(model_index);
views::View* view = view_model_->view_at(model_index);
view_model_->Remove(model_index);
// The first animation fades out the view. When done we'll animate the rest of
// the views to their target location.
bounds_animator_->AnimateViewTo(view, view->bounds());
bounds_animator_->SetAnimationDelegate(
view, new FadeOutAnimationDelegate(this, view), true);
}
void LauncherView::LauncherItemChanged(int model_index,
const ash::LauncherItem& old_item) {
const LauncherItem& item(model_->items()[model_index]);
if (old_item.type != item.type) {
// Type changed, swap the views.
model_index = CancelDrag(model_index);
scoped_ptr<views::View> old_view(view_model_->view_at(model_index));
bounds_animator_->StopAnimatingView(old_view.get());
view_model_->Remove(model_index);
views::View* new_view = CreateViewForItem(item);
AddChildView(new_view);
view_model_->Add(new_view, model_index);
new_view->SetBoundsRect(old_view->bounds());
return;
}
views::View* view = view_model_->view_at(model_index);
switch (item.type) {
case TYPE_TABBED: {
TabbedLauncherButton* button = static_cast<TabbedLauncherButton*>(view);
gfx::Size pref = button->GetPreferredSize();
button->SetTabImage(item.image);
if (pref != button->GetPreferredSize())
AnimateToIdealBounds();
else
button->SchedulePaint();
ReflectItemStatus(item, button);
break;
}
case TYPE_APP_SHORTCUT:
case TYPE_PLATFORM_APP:
case TYPE_APP_PANEL: {
LauncherButton* button = static_cast<LauncherButton*>(view);
ReflectItemStatus(item, button);
button->SetImage(item.image);
button->SchedulePaint();
break;
}
default:
break;
}
}
void LauncherView::LauncherItemMoved(int start_index, int target_index) {
view_model_->Move(start_index, target_index);
AnimateToIdealBounds();
}
void LauncherView::LauncherStatusChanged() {
AppListButton* app_list_button =
static_cast<AppListButton*>(GetAppListButtonView());
if (model_->status() == LauncherModel::STATUS_LOADING)
app_list_button->StartLoadingAnimation();
else
app_list_button->StopLoadingAnimation();
}
void LauncherView::PointerPressedOnButton(views::View* view,
Pointer pointer,
const ui::LocatedEvent& event) {
if (drag_view_)
return;
tooltip_->Close();
int index = view_model_->GetIndexOfView(view);
if (index == -1 ||
view_model_->view_size() <= 1 ||
!delegate_->IsDraggable(model_->items()[index]))
return; // View is being deleted or not draggable, ignore request.
ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
drag_view_ = view;
drag_offset_ = shelf->PrimaryAxisValue(event.x(), event.y());
}
void LauncherView::PointerDraggedOnButton(views::View* view,
Pointer pointer,
const ui::LocatedEvent& event) {
ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
if (!dragging() && drag_view_ &&
shelf->PrimaryAxisValue(abs(event.x() - drag_offset_),
abs(event.y() - drag_offset_)) >=
kMinimumDragDistance) {
PrepareForDrag(pointer, event);
}
if (drag_pointer_ == pointer)
ContinueDrag(event);
}
void LauncherView::PointerReleasedOnButton(views::View* view,
Pointer pointer,
bool canceled) {
if (canceled) {
CancelDrag(-1);
} else if (drag_pointer_ == pointer) {
drag_pointer_ = NONE;
drag_view_ = NULL;
AnimateToIdealBounds();
}
}
void LauncherView::MouseMovedOverButton(views::View* view) {
// Mouse cursor moves doesn't make effects on the app-list button if
// app-list bubble is already visible.
if (view == GetAppListButtonView() &&
Shell::GetInstance()->GetAppListWindow())
return;
if (!tooltip_->IsVisible())
tooltip_->ResetTimer();
}
void LauncherView::MouseEnteredButton(views::View* view) {
// If mouse cursor enters to the app-list button but app-list bubble is
// already visible, we should not show the bubble in that case.
if (view == GetAppListButtonView() &&
Shell::GetInstance()->GetAppListWindow())
return;
if (tooltip_->IsVisible()) {
tooltip_->ShowImmediately(view, GetAccessibleName(view));
} else {
tooltip_->ShowDelayed(view, GetAccessibleName(view));
}
}
void LauncherView::MouseExitedButton(views::View* view) {
if (!tooltip_->IsVisible())
tooltip_->StopTimer();
}
string16 LauncherView::GetAccessibleName(const views::View* view) {
int view_index = view_model_->GetIndexOfView(view);
// May be -1 while in the process of animating closed.
if (view_index == -1)
return string16();
switch (model_->items()[view_index].type) {
case TYPE_TABBED:
case TYPE_APP_PANEL:
case TYPE_APP_SHORTCUT:
case TYPE_PLATFORM_APP:
return delegate_->GetTitle(model_->items()[view_index]);
case TYPE_APP_LIST:
return model_->status() == LauncherModel::STATUS_LOADING ?
l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_SYNCING_TITLE) :
l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE);
case TYPE_BROWSER_SHORTCUT:
return l10n_util::GetStringUTF16(IDS_AURA_NEW_TAB);
}
return string16();
}
void LauncherView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
// Do not handle mouse release during drag.
if (dragging())
return;
if (sender == overflow_button_) {
ShowOverflowBubble();
return;
}
int view_index = view_model_->GetIndexOfView(sender);
// May be -1 while in the process of animating closed.
if (view_index == -1)
return;
if (event.IsShiftDown())
ui::LayerAnimator::set_slow_animation_mode(true);
tooltip_->Close();
switch (model_->items()[view_index].type) {
case TYPE_TABBED:
case TYPE_APP_PANEL:
delegate_->ItemClicked(model_->items()[view_index], event.flags());
break;
case TYPE_APP_SHORTCUT:
case TYPE_PLATFORM_APP:
Shell::GetInstance()->delegate()->RecordUserMetricsAction(
UMA_LAUNCHER_CLICK_ON_APP);
delegate_->ItemClicked(model_->items()[view_index], event.flags());
break;
case TYPE_APP_LIST:
Shell::GetInstance()->delegate()->RecordUserMetricsAction(
UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
Shell::GetInstance()->ToggleAppList();
break;
case TYPE_BROWSER_SHORTCUT:
// Click on browser icon is counted in app clicks.
Shell::GetInstance()->delegate()->RecordUserMetricsAction(
UMA_LAUNCHER_CLICK_ON_APP);
delegate_->OnBrowserShortcutClicked(event.flags());
break;
}
if (event.IsShiftDown())
ui::LayerAnimator::set_slow_animation_mode(false);
}
void LauncherView::ShowContextMenuForView(views::View* source,
const gfx::Point& point) {
int view_index = view_model_->GetIndexOfView(source);
if (view_index != -1 &&
model_->items()[view_index].type == TYPE_APP_LIST) {
view_index = -1;
}
#if !defined(OS_MACOSX)
if (view_index == -1) {
Shell::GetInstance()->ShowContextMenu(point);
return;
}
scoped_ptr<ui::MenuModel> menu_model(delegate_->CreateContextMenu(
model_->items()[view_index],
source->GetWidget()->GetNativeView()->GetRootWindow()));
if (!menu_model.get())
return;
base::AutoReset<LauncherID> reseter(
&context_menu_id_,
view_index == -1 ? 0 : model_->items()[view_index].id);
views::MenuModelAdapter menu_model_adapter(menu_model.get());
launcher_menu_runner_.reset(
new views::MenuRunner(menu_model_adapter.CreateMenu()));
// NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
// code.
if (launcher_menu_runner_->RunMenuAt(
source->GetWidget(), NULL, gfx::Rect(point, gfx::Size()),
views::MenuItemView::TOPLEFT, views::MenuRunner::CONTEXT_MENU) ==
views::MenuRunner::MENU_DELETED)
return;
Shell::GetInstance()->UpdateShelfVisibility();
#endif
}
void LauncherView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
FOR_EACH_OBSERVER(LauncherIconObserver, observers_,
OnLauncherIconPositionsChanged());
PreferredSizeChanged();
}
void LauncherView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
}
} // namespace internal
} // namespace ash
|