forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelf_layout_manager.cc
2885 lines (2488 loc) · 107 KB
/
shelf_layout_manager.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_layout_manager.h"
#include <algorithm>
#include <cmath>
#include <vector>
#include "ash/accessibility/accessibility_controller_impl.h"
#include "ash/animation/animation_change_type.h"
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/app_list/app_list_metrics.h"
#include "ash/app_list/views/app_list_view.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/display/screen_orientation_controller.h"
#include "ash/drag_drop/scoped_drag_drop_observer.h"
#include "ash/public/cpp/app_list/app_list_types.h"
#include "ash/public/cpp/presentation_time_recorder.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/root_window_controller.h"
#include "ash/screen_util.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shelf/contextual_tooltip.h"
#include "ash/shelf/drag_handle.h"
#include "ash/shelf/home_to_overview_nudge_controller.h"
#include "ash/shelf/hotseat_widget.h"
#include "ash/shelf/in_app_to_home_nudge_controller.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_layout_manager_observer.h"
#include "ash/shelf/shelf_metrics.h"
#include "ash/shelf/shelf_navigation_widget.h"
#include "ash/shelf/shelf_view.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shelf/swipe_home_to_overview_controller.h"
#include "ash/shell.h"
#include "ash/system/locale/locale_update_controller_impl.h"
#include "ash/system/status_area_widget.h"
#include "ash/wallpaper/wallpaper_controller_impl.h"
#include "ash/wm/fullscreen_window_finder.h"
#include "ash/wm/lock_state_controller.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/screen_pinning_controller.h"
#include "ash/wm/splitview/split_view_controller.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "ash/wm/work_area_insets.h"
#include "ash/wm/workspace/workspace_types.h"
#include "ash/wm/workspace_controller.h"
#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "components/prefs/pref_service.h"
#include "ui/aura/client/drag_drop_client.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/base/hit_test.h"
#include "ui/base/ui_base_switches.h"
#include "ui/compositor/animation_throughput_reporter.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/android/gesture_event_type.h"
#include "ui/events/event.h"
#include "ui/events/event_handler.h"
#include "ui/events/gesture_event_details.h"
#include "ui/events/types/event_type.h"
#include "ui/views/border.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/public/activation_client.h"
namespace ash {
namespace {
// Default Target Dim Opacity for floating shelf.
constexpr float kFloatingShelfDimOpacity = 0.74f;
// Target Dim Opacity for shelf when shelf is in the maximized state.
constexpr float kMaximizedShelfDimOpacity = 0.6f;
// Default opacity for shelf without dimming.
constexpr float kDefaultShelfOpacity = 1.0f;
// Delay before showing the shelf. This is after the mouse stops moving.
constexpr int kAutoHideDelayMS = 200;
// To avoid hiding the shelf when the mouse transitions from a message bubble
// into the shelf, the hit test area is enlarged by this amount of pixels to
// keep the shelf from hiding.
constexpr int kNotificationBubbleGapHeight = 6;
// The maximum size of the region on the display opposing the shelf managed by
// this ShelfLayoutManager which can trigger showing the shelf.
// For instance:
// - Primary display is left of secondary display.
// - Shelf is left aligned
// - This ShelfLayoutManager manages the shelf for the secondary display.
// |kMaxAutoHideShowShelfRegionSize| refers to the maximum size of the region
// from the right edge of the primary display which can trigger showing the
// auto-hidden shelf. The region is used to make it easier to trigger showing
// the auto-hidden shelf when the shelf is on the boundary between displays.
constexpr int kMaxAutoHideShowShelfRegionSize = 10;
aura::Window* GetDragHandleNudgeWindow(ShelfWidget* shelf_widget) {
if (!shelf_widget->GetDragHandle())
return nullptr;
if (!shelf_widget->GetDragHandle()->drag_handle_nudge())
return nullptr;
return shelf_widget->GetDragHandle()
->drag_handle_nudge()
->GetWidget()
->GetNativeWindow();
}
ui::Layer* GetLayer(views::Widget* widget) {
return widget->GetNativeView()->layer();
}
void SetupAnimator(ui::ScopedLayerAnimationSettings* animation_setter,
base::TimeDelta animation_duration,
gfx::Tween::Type type) {
animation_setter->SetTransitionDuration(animation_duration);
if (!animation_duration.is_zero()) {
animation_setter->SetTweenType(type);
animation_setter->SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
}
}
void AnimateOpacity(ui::Layer* layer,
float target_opacity,
base::TimeDelta animation_duration,
gfx::Tween::Type type) {
ui::ScopedLayerAnimationSettings animation_setter(layer->GetAnimator());
SetupAnimator(&animation_setter, animation_duration, type);
layer->SetOpacity(target_opacity);
}
// Returns true if the window is in the app list window container.
bool IsAppListWindow(const aura::Window* window) {
const aura::Window* parent = window->parent();
return parent && parent->GetId() == kShellWindowId_AppListContainer;
}
// Returns the |WorkspaceWindowState| of the currently active desk on the root
// window of |shelf_window|.
WorkspaceWindowState GetShelfWorkspaceWindowState(aura::Window* shelf_window) {
DCHECK(shelf_window);
// Shelf window does not belong to any desk, use the root to get the active
// desk's workspace state.
auto* controller =
GetActiveWorkspaceController(shelf_window->GetRootWindow());
DCHECK(controller);
return controller->GetWindowState();
}
// Returns shelf's work area inset for given |visibility_state| and |size|.
int GetShelfInset(ShelfVisibilityState visibility_state, int size) {
return visibility_state == SHELF_VISIBLE ? size : 0;
}
int GetOffset(int offset, const char* pref_name) {
PrefService* prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
return prefs->GetBoolean(pref_name) ? -offset : offset;
}
// Returns the window that can be dragged from shelf into home screen or
// overview at |location_in_screen|. Returns nullptr if there is no such
// window.
aura::Window* GetWindowForDragToHomeOrOverview(
const gfx::Point& location_in_screen) {
if (!Shell::Get()->IsInTabletMode())
return nullptr;
auto mru_windows =
Shell::Get()->mru_window_tracker()->BuildWindowForCycleList(kActiveDesk);
if (mru_windows.empty())
return nullptr;
aura::Window* window = nullptr;
SplitViewController* split_view_controller =
SplitViewController::Get(Shell::GetPrimaryRootWindow());
const bool is_in_splitview = split_view_controller->InSplitViewMode();
const bool is_in_overview =
Shell::Get()->overview_controller()->InOverviewSession();
if (!is_in_splitview && !is_in_overview) {
// If split view mode is not active, use the first MRU window.
window = mru_windows[0];
} else if (is_in_splitview) {
// If split view mode is active, use the event location to decide which
// window should be the dragged window.
aura::Window* left_window = split_view_controller->left_window();
aura::Window* right_window = split_view_controller->right_window();
const int divider_position = split_view_controller->divider_position();
const bool is_landscape = IsCurrentScreenOrientationLandscape();
const bool is_primary = IsCurrentScreenOrientationPrimary();
const gfx::Rect work_area =
screen_util::GetDisplayWorkAreaBoundsInScreenForActiveDeskContainer(
split_view_controller->GetDefaultSnappedWindow());
if (is_landscape) {
if (location_in_screen.x() < work_area.x() + divider_position)
window = is_primary ? left_window : right_window;
else
window = is_primary ? right_window : left_window;
} else {
window = is_primary ? right_window : left_window;
}
}
return window && window->IsVisible() ? window : nullptr;
}
// Calculates the type of hotseat gesture which should be recorded in histogram.
// Returns the null value if no gesture should be recorded.
absl::optional<InAppShelfGestures> CalculateHotseatGestureToRecord(
absl::optional<ShelfWindowDragResult> window_drag_result,
bool transitioned_from_overview_to_home,
HotseatState old_state,
HotseatState current_state) {
if (window_drag_result.has_value() &&
(window_drag_result == ShelfWindowDragResult::kGoToOverviewMode ||
window_drag_result == ShelfWindowDragResult::kGoToSplitviewMode) &&
old_state == HotseatState::kHidden) {
return InAppShelfGestures::kSwipeUpToShow;
}
if (window_drag_result.has_value() &&
window_drag_result == ShelfWindowDragResult::kGoToHomeScreen) {
return InAppShelfGestures::kFlingUpToShowHomeScreen;
}
if (transitioned_from_overview_to_home)
return InAppShelfGestures::kFlingUpToShowHomeScreen;
if (old_state == current_state)
return absl::nullopt;
if (current_state == HotseatState::kHidden)
return InAppShelfGestures::kSwipeDownToHide;
if (current_state == HotseatState::kExtended)
return InAppShelfGestures::kSwipeUpToShow;
return absl::nullopt;
}
// Forwards gesture events to ShelfLayoutManager to hide the hotseat
// when it is kExtended.
class HotseatEventHandler : public ui::EventHandler,
public ShelfLayoutManagerObserver {
public:
explicit HotseatEventHandler(ShelfLayoutManager* shelf_layout_manager)
: shelf_layout_manager_(shelf_layout_manager) {
shelf_layout_manager_->AddObserver(this);
Shell::Get()->AddPreTargetHandler(this);
}
HotseatEventHandler(const HotseatEventHandler&) = delete;
HotseatEventHandler& operator=(const HotseatEventHandler&) = delete;
~HotseatEventHandler() override {
shelf_layout_manager_->RemoveObserver(this);
Shell::Get()->RemovePreTargetHandler(this);
}
// ShelfLayoutManagerObserver:
void OnHotseatStateChanged(HotseatState old_state,
HotseatState new_state) override {
should_forward_event_ = new_state == HotseatState::kExtended;
}
// ui::EventHandler:
void OnGestureEvent(ui::GestureEvent* event) override {
if (!should_forward_event_)
return;
shelf_layout_manager_->ProcessGestureEventOfInAppHotseat(
event, static_cast<aura::Window*>(event->target()));
}
private:
// Whether events should get forwarded to ShelfLayoutManager.
bool should_forward_event_ = false;
ShelfLayoutManager* const shelf_layout_manager_; // unowned.
};
} // namespace
ShelfLayoutManager::State::State()
: visibility_state(SHELF_VISIBLE),
auto_hide_state(SHELF_AUTO_HIDE_HIDDEN),
window_state(WorkspaceWindowState::kDefault),
pre_lock_screen_animation_active(false),
session_state(session_manager::SessionState::UNKNOWN) {}
bool ShelfLayoutManager::State::IsAddingSecondaryUser() const {
return session_state == session_manager::SessionState::LOGIN_SECONDARY;
}
bool ShelfLayoutManager::State::IsScreenLocked() const {
return session_state == session_manager::SessionState::LOCKED;
}
bool ShelfLayoutManager::State::IsActiveSessionState() const {
return session_state == session_manager::SessionState::ACTIVE;
}
bool ShelfLayoutManager::State::IsShelfAutoHidden() const {
return visibility_state == SHELF_AUTO_HIDE &&
auto_hide_state == SHELF_AUTO_HIDE_HIDDEN;
}
bool ShelfLayoutManager::State::IsShelfVisible() const {
return visibility_state == SHELF_VISIBLE ||
(visibility_state == SHELF_AUTO_HIDE &&
auto_hide_state == SHELF_AUTO_HIDE_SHOWN);
}
bool ShelfLayoutManager::State::Equals(const State& other) const {
return other.visibility_state == visibility_state &&
(visibility_state != SHELF_AUTO_HIDE ||
other.auto_hide_state == auto_hide_state) &&
other.window_state == window_state &&
other.pre_lock_screen_animation_active ==
pre_lock_screen_animation_active &&
other.session_state == session_state;
}
// ShelfLayoutManager::ScopedSuspendVisibilityUpdate ---------------------------
ShelfLayoutManager::ScopedSuspendWorkAreaUpdate::ScopedSuspendWorkAreaUpdate(
ShelfLayoutManager* manager)
: manager_(manager) {
manager_->SuspendWorkAreaUpdate();
}
ShelfLayoutManager::ScopedSuspendWorkAreaUpdate::
~ScopedSuspendWorkAreaUpdate() {
manager_->ResumeWorkAreaUpdate();
}
// ShelfLayoutManager::ScopedVisibilityLock ------------------------------------
ShelfLayoutManager::ScopedVisibilityLock::ScopedVisibilityLock(
ShelfLayoutManager* shelf)
: shelf_(shelf->weak_factory_.GetWeakPtr()) {
++shelf_->suspend_visibility_update_;
}
ShelfLayoutManager::ScopedVisibilityLock::~ScopedVisibilityLock() {
if (!shelf_)
return;
--shelf_->suspend_visibility_update_;
DCHECK_GE(shelf_->suspend_visibility_update_, 0);
if (shelf_->suspend_visibility_update_ == 0)
shelf_->UpdateVisibilityState();
}
// ShelfLayoutManager ----------------------------------------------------------
ShelfLayoutManager::ShelfLayoutManager(ShelfWidget* shelf_widget, Shelf* shelf)
: shelf_widget_(shelf_widget),
shelf_(shelf),
is_background_blur_enabled_(features::IsBackgroundBlurEnabled()),
is_productivity_launcher_enabled_(
features::IsProductivityLauncherEnabled()) {
DCHECK(shelf_widget_);
DCHECK(shelf_);
}
ShelfLayoutManager::~ShelfLayoutManager() {
// |hotseat_event_handler_| needs to be released before ShelfLayoutManager.
hotseat_event_handler_.reset();
// Ensures that |overview_suspend_work_area_update_| is released before
// ShelfLayoutManager.
overview_suspend_work_area_update_.reset();
for (auto& observer : observers_)
observer.WillDeleteShelfLayoutManager();
auto* shell = Shell::Get();
shell->locale_update_controller()->RemoveObserver(this);
shell->RemoveShellObserver(this);
shell->lock_state_controller()->RemoveObserver(this);
if (shell->app_list_controller())
shell->app_list_controller()->RemoveObserver(this);
if (shell->overview_controller())
shell->overview_controller()->RemoveObserver(this);
}
void ShelfLayoutManager::InitObservers() {
auto* shell = Shell::Get();
shell->AddShellObserver(this);
SplitViewController::Get(shelf_widget_->GetNativeWindow())->AddObserver(this);
ShelfConfig::Get()->AddObserver(this);
shell->overview_controller()->AddObserver(this);
shell->app_list_controller()->AddObserver(this);
shell->lock_state_controller()->AddObserver(this);
shell->activation_client()->AddObserver(this);
shell->locale_update_controller()->AddObserver(this);
state_.session_state = shell->session_controller()->GetSessionState();
shelf_background_type_ = GetShelfBackgroundType();
wallpaper_controller_observation_.Observe(shell->wallpaper_controller());
// DesksController could be null when virtual desks feature is not enabled.
if (DesksController::Get())
DesksController::Get()->AddObserver(this);
}
void ShelfLayoutManager::PrepareForShutdown() {
// This might get called twice during shell shutdown - once at the beginning,
// and once as part of root window controller shutdown.
if (in_shutdown_)
return;
in_shutdown_ = true;
// Stop observing changes to avoid updating a partially destructed shelf.
Shell::Get()->activation_client()->RemoveObserver(this);
ShelfConfig::Get()->RemoveObserver(this);
// DesksController could be null when virtual desks feature is not enabled.
if (DesksController::Get())
DesksController::Get()->RemoveObserver(this);
SplitViewController::Get(shelf_widget_->GetNativeWindow())
->RemoveObserver(this);
}
bool ShelfLayoutManager::IsVisible() const {
// status_area_widget() may be nullptr during the shutdown.
return shelf_widget_->status_area_widget() &&
shelf_widget_->status_area_widget()->IsVisible() &&
state_.IsShelfVisible();
}
gfx::Rect ShelfLayoutManager::GetIdealBounds() const {
const int shelf_size = ShelfConfig::Get()->shelf_size();
aura::Window* shelf_window = shelf_widget_->GetNativeWindow();
gfx::Rect rect(screen_util::GetDisplayBoundsInParent(shelf_window));
return shelf_->SelectValueForShelfAlignment(
gfx::Rect(rect.x(), rect.bottom() - shelf_size, rect.width(), shelf_size),
gfx::Rect(rect.x(), rect.y(), shelf_size, rect.height()),
gfx::Rect(rect.right() - shelf_size, rect.y(), shelf_size,
rect.height()));
}
gfx::Rect ShelfLayoutManager::GetIdealBoundsForWorkAreaCalculation() const {
if (!Shell::Get()->IsInTabletMode() ||
state_.session_state != session_manager::SessionState::ACTIVE) {
return GetIdealBounds();
}
// For the work-area calculation in tablet mode, always use in-app shelf
// bounds, because when the shelf is not in-app the UI is either showing
// AppList or Overview, and updating the WorkArea with the new Shelf size
// would cause unnecessary work.
aura::Window* shelf_window = shelf_widget_->GetNativeWindow();
gfx::Rect rect(screen_util::GetDisplayBoundsInParent(shelf_window));
const int in_app_shelf_size = ShelfConfig::Get()->in_app_shelf_size();
rect.set_y(rect.bottom() - in_app_shelf_size);
rect.set_height(in_app_shelf_size);
return rect;
}
void ShelfLayoutManager::LayoutShelf(bool animate) {
// Do not animate if the shelf container is animating.
animate &= !IsShelfContainerAnimating();
// The ShelfWidget may be partially closed (no native widget) during shutdown
// or before it's been fully initialized so skip layout.
if (in_shutdown_ || !shelf_widget_->native_widget())
return;
CalculateTargetBoundsAndUpdateWorkArea();
UpdateBoundsAndOpacity(animate);
// Update insets in ShelfWindowTargeter when shelf bounds change.
for (auto& observer : observers_)
observer.WillChangeVisibilityState(visibility_state());
}
void ShelfLayoutManager::UpdateVisibilityState() {
// Bail out early after shelf is destroyed or visibility update is suspended.
aura::Window* shelf_window = shelf_widget_->GetNativeWindow();
if (in_shutdown_ || !shelf_window || suspend_visibility_update_)
return;
const WorkspaceWindowState window_state =
GetShelfWorkspaceWindowState(shelf_window);
if (shelf_->ShouldHideOnSecondaryDisplay(state_.session_state)) {
// Needed to hide system tray on secondary display.
SetState(SHELF_HIDDEN);
} else if (!state_.IsActiveSessionState()) {
// Needed to show system tray in non active session state.
SetState(SHELF_VISIBLE);
} else if (Shell::Get()->screen_pinning_controller()->IsPinned()) {
SetState(SHELF_HIDDEN);
} else if (Shell::Get()->session_controller()->IsRunningInAppMode()) {
SetState(SHELF_HIDDEN);
} else {
// TODO(zelidrag): Verify shelf drag animation still shows on the device
// when we are in ShelfAutoHideBehavior::kAlwaysHidden.
switch (window_state) {
case WorkspaceWindowState::kFullscreen:
if (IsShelfAutoHideForFullscreenMaximized()) {
SetState(SHELF_AUTO_HIDE);
} else if (IsShelfHiddenForFullscreen()) {
SetState(SHELF_HIDDEN);
} else {
// The shelf is sometimes not hidden when in immersive fullscreen.
// Force the shelf to be auto hidden in this case.
SetState(SHELF_AUTO_HIDE);
}
break;
case WorkspaceWindowState::kMaximized:
SetState(IsShelfAutoHideForFullscreenMaximized()
? SHELF_AUTO_HIDE
: CalculateShelfVisibility());
break;
case WorkspaceWindowState::kDefault:
SetState(CalculateShelfVisibility());
break;
}
}
UpdateWorkspaceMask(window_state);
SendA11yAlertForFullscreenWorkspaceState(window_state);
}
void ShelfLayoutManager::UpdateVisibilityStateForBackGesture() {
base::AutoReset<bool> back_gesture(&state_forced_by_back_gesture_, true);
SetState(SHELF_VISIBLE);
LayoutShelf(/*animate=*/true);
}
void ShelfLayoutManager::UpdateAutoHideState() {
ShelfAutoHideState auto_hide_state =
CalculateAutoHideState(state_.visibility_state);
if (auto_hide_state != state_.auto_hide_state) {
if (auto_hide_state == SHELF_AUTO_HIDE_HIDDEN) {
// Hides happen immediately.
SetState(state_.visibility_state);
} else {
if (!auto_hide_timer_.IsRunning()) {
mouse_over_shelf_when_auto_hide_timer_started_ =
shelf_widget_->GetWindowBoundsInScreen().Contains(
display::Screen::GetScreen()->GetCursorScreenPoint());
drag_over_shelf_when_auto_hide_timer_started_ =
in_drag_drop_ && shelf_widget_->GetWindowBoundsInScreen().Contains(
last_drag_drop_position_in_screen_);
}
StartAutoHideTimer();
}
} else {
StopAutoHideTimer();
}
}
void ShelfLayoutManager::UpdateAutoHideForMouseEvent(ui::MouseEvent* event,
aura::Window* target) {
// This also checks IsShelfWindow() and IsStatusAreaWindow() to make sure we
// don't attempt to hide the shelf if the mouse down occurs on the shelf.
in_mouse_drag_ = (event->type() == ui::ET_MOUSE_DRAGGED ||
(in_mouse_drag_ && event->type() != ui::ET_MOUSE_RELEASED &&
event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)) &&
!IsShelfWindow(target) && !IsStatusAreaWindow(target);
// Don't update during shutdown because synthetic mouse events (e.g. mouse
// exit) may be generated during status area widget teardown.
if (visibility_state() != SHELF_AUTO_HIDE || in_shutdown_)
return;
if (event->type() == ui::ET_MOUSE_PRESSED ||
event->type() == ui::ET_MOUSE_MOVED) {
if (shelf_->shelf_widget()->GetVisibleShelfBounds().Contains(
display::Screen::GetScreen()->GetCursorScreenPoint())) {
UpdateAutoHideState();
last_seen_mouse_position_was_over_shelf_ = true;
} else {
// The event happened outside the shelf's bounds. If it's a click, hide
// the shelf immediately. If it's a mouse-out, hide after a delay (but
// only if it really is a mouse-out, meaning the mouse actually exited the
// shelf bounds as opposed to having been outside all along).
if (event->type() == ui::ET_MOUSE_PRESSED) {
UpdateAutoHideState();
} else if (last_seen_mouse_position_was_over_shelf_) {
StartAutoHideTimer();
}
last_seen_mouse_position_was_over_shelf_ = false;
}
}
}
void ShelfLayoutManager::UpdateContextualNudges() {
if (!ash::features::AreContextualNudgesEnabled())
return;
// Do not allow nudges outside of an active session.
if (Shell::Get()->session_controller()->GetSessionState() !=
session_manager::SessionState::ACTIVE) {
return;
}
const bool in_app_shelf = ShelfConfig::Get()->is_in_app();
const bool in_tablet_mode = ShelfConfig::Get()->in_tablet_mode();
const bool in_overview_mode = ShelfConfig::Get()->in_overview_mode();
contextual_tooltip::SetDragHandleNudgeDisabledForHiddenShelf(!IsVisible());
if (in_app_shelf && in_tablet_mode && !in_app_to_home_nudge_controller_) {
in_app_to_home_nudge_controller_ =
std::make_unique<InAppToHomeNudgeController>(shelf_widget_);
}
if (in_app_to_home_nudge_controller_) {
in_app_to_home_nudge_controller_->SetNudgeAllowedForCurrentShelf(
in_tablet_mode, in_app_shelf,
ShelfConfig::Get()->shelf_controls_shown());
}
// Create home to overview nudge controller if home to overview nudge is
// allowed by the current shelf state.
const bool allow_home_to_overview_nudge =
in_tablet_mode && !in_app_shelf &&
!ShelfConfig::Get()->shelf_controls_shown() && !in_overview_mode;
if (allow_home_to_overview_nudge && !home_to_overview_nudge_controller_) {
home_to_overview_nudge_controller_ =
std::make_unique<HomeToOverviewNudgeController>(
shelf_->hotseat_widget());
}
if (home_to_overview_nudge_controller_) {
home_to_overview_nudge_controller_->SetNudgeAllowedForCurrentShelf(
allow_home_to_overview_nudge);
}
}
void ShelfLayoutManager::HideContextualNudges() {
if (!ash::features::AreContextualNudgesEnabled())
return;
shelf_widget_->HideDragHandleNudge(
contextual_tooltip::DismissNudgeReason::kOther);
if (home_to_overview_nudge_controller_)
home_to_overview_nudge_controller_->SetNudgeAllowedForCurrentShelf(false);
}
void ShelfLayoutManager::ProcessGestureEventOfAutoHideShelf(
ui::GestureEvent* event,
aura::Window* target) {
const bool is_shelf_window = IsShelfWindow(target);
// Skip event processing if shelf widget is fully visible to let the default
// event dispatching do its work.
if (IsVisible() || in_shutdown_) {
// Tap outside of the AUTO_HIDE_SHOWN shelf should hide it.
if (!is_shelf_window && !IsStatusAreaWindow(target) &&
visibility_state() == SHELF_AUTO_HIDE &&
state_.auto_hide_state == SHELF_AUTO_HIDE_SHOWN &&
event->type() == ui::ET_GESTURE_TAP) {
UpdateAutoHideState();
}
// When hotseat is hidden and in app shelf is shown, then a tap on the
// shelf should hide it.
if (is_shelf_window && !IsStatusAreaWindow(target) &&
hotseat_state() == HotseatState::kHidden &&
ShelfConfig::Get()->is_in_app()) {
UpdateAutoHideState();
}
// Complete gesture drag when Shelf is visible in auto-hide mode. It is
// called when swiping Shelf up to show.
if (is_shelf_window && !IsStatusAreaWindow(target) &&
visibility_state() == SHELF_AUTO_HIDE &&
state_.auto_hide_state == SHELF_AUTO_HIDE_SHOWN &&
event->type() == ui::ET_GESTURE_END && drag_status_ != kDragNone) {
CompleteDrag(*event);
}
return;
}
if (is_shelf_window) {
ui::GestureEvent event_in_screen(*event);
gfx::Point location_in_screen(event->location());
::wm::ConvertPointToScreen(target, &location_in_screen);
event_in_screen.set_location(location_in_screen);
if (ProcessGestureEvent(event_in_screen))
event->StopPropagation();
}
}
void ShelfLayoutManager::ProcessGestureEventOfInAppHotseat(
ui::GestureEvent* event,
aura::Window* target) {
if (!Shell::Get()->IsInTabletMode())
return;
DCHECK_EQ(hotseat_state(), HotseatState::kExtended);
if (IsShelfWindow(target) || drag_status_ != DragStatus::kDragNone)
return;
base::AutoReset<bool> hide_hotseat(&should_hide_hotseat_, true);
// In overview mode, only the gesture tap event is able to make the hotseat
// exit the extended mode.
const bool in_overview =
Shell::Get()->overview_controller() &&
Shell::Get()->overview_controller()->InOverviewSession();
ui::EventType interesting_type =
in_overview ? ui::ET_GESTURE_TAP : ui::ET_GESTURE_BEGIN;
// Record gesture metrics only for `interesting_type` to avoid over counting.
if (event->type() == interesting_type) {
UMA_HISTOGRAM_ENUMERATION(
kHotseatGestureHistogramName,
InAppShelfGestures::kHotseatHiddenDueToInteractionOutsideOfShelf);
}
UpdateVisibilityState();
}
void ShelfLayoutManager::AddObserver(ShelfLayoutManagerObserver* observer) {
observers_.AddObserver(observer);
}
void ShelfLayoutManager::RemoveObserver(ShelfLayoutManagerObserver* observer) {
observers_.RemoveObserver(observer);
}
bool ShelfLayoutManager::ProcessGestureEvent(
const ui::GestureEvent& event_in_screen) {
if (shelf_widget_->HandleLoginShelfGestureEvent(event_in_screen))
return true;
if (!IsDragAllowed())
return false;
if (event_in_screen.type() == ui::ET_GESTURE_SCROLL_BEGIN)
return StartGestureDrag(event_in_screen);
if (drag_status_ != kDragInProgress &&
drag_status_ != kDragAppListInProgress &&
drag_status_ != kDragHomeToOverviewInProgress) {
return false;
}
if (event_in_screen.type() == ui::ET_GESTURE_SCROLL_UPDATE) {
UpdateGestureDrag(event_in_screen);
return true;
}
if (event_in_screen.type() == ui::ET_GESTURE_SCROLL_END ||
event_in_screen.type() == ui::ET_SCROLL_FLING_START) {
if (event_in_screen.type() == ui::ET_SCROLL_FLING_START) {
last_drag_velocity_ =
event_in_screen.AsGestureEvent()->details().velocity_y();
}
if (drag_status_ == kDragAppListInProgress) {
CompleteAppListDrag(event_in_screen);
} else if (drag_status_ == kDragHomeToOverviewInProgress) {
CompleteDragHomeToOverview(event_in_screen);
} else {
CompleteDrag(event_in_screen);
}
return true;
}
// Unexpected event. Reset the state and let the event fall through.
CancelDrag(absl::nullopt);
return false;
}
void ShelfLayoutManager::ProcessMouseEventFromShelf(
const ui::MouseEvent& event_in_screen) {
ui::EventType event_type = event_in_screen.type();
DCHECK(event_type == ui::ET_MOUSE_PRESSED ||
event_type == ui::ET_MOUSE_DRAGGED ||
event_type == ui::ET_MOUSE_RELEASED);
if (!IsDragAllowed())
return;
switch (event_type) {
case ui::ET_MOUSE_PRESSED:
AttemptToDragByMouse(event_in_screen);
break;
case ui::ET_MOUSE_DRAGGED:
UpdateMouseDrag(event_in_screen);
return;
case ui::ET_MOUSE_RELEASED:
ReleaseMouseDrag(event_in_screen);
return;
default:
NOTREACHED();
return;
}
}
void ShelfLayoutManager::ProcessGestureEventFromShelfWidget(
ui::GestureEvent* event_in_screen) {
if (ProcessGestureEvent(*event_in_screen))
event_in_screen->StopPropagation();
}
void ShelfLayoutManager::ProcessScrollOffset(int offset,
base::TimeTicks time_stamp) {
if (offset <= ShelfConfig::Get()->mousewheel_scroll_offset_threshold())
return;
Shell::Get()->app_list_controller()->ToggleAppList(
display::Screen::GetScreen()
->GetDisplayNearestWindow(shelf_widget_->GetNativeWindow())
.id(),
kScrollFromShelf, time_stamp);
}
void ShelfLayoutManager::ProcessScrollEventFromShelf(ui::ScrollEvent* event) {
if (shelf_->IsHorizontalAlignment()) {
ProcessScrollOffset(GetOffset(event->y_offset(), prefs::kNaturalScroll),
event->time_stamp());
} else {
int offset = shelf_->alignment() == ShelfAlignment::kLeft
? -event->x_offset()
: event->x_offset();
offset = GetOffset(offset, prefs::kNaturalScroll),
ProcessScrollOffset(offset, event->time_stamp());
}
}
void ShelfLayoutManager::ProcessMouseWheelEventFromShelf(
ui::MouseWheelEvent* event) {
const int y_offset =
GetOffset(event->offset().y(), prefs::kMouseReverseScroll);
ProcessScrollOffset(y_offset, event->time_stamp());
}
ShelfBackgroundType ShelfLayoutManager::GetShelfBackgroundType() const {
if (state_.pre_lock_screen_animation_active)
return ShelfBackgroundType::kDefaultBg;
// Handle all other non active screen states, including OOBE and pre-login.
if (state_.session_state == session_manager::SessionState::OOBE)
return ShelfBackgroundType::kOobe;
if (state_.session_state != session_manager::SessionState::ACTIVE) {
if (Shell::Get()->wallpaper_controller()->HasShownAnyWallpaper() &&
!Shell::Get()
->wallpaper_controller()
->IsWallpaperBlurredForLockState()) {
return ShelfBackgroundType::kLoginNonBlurredWallpaper;
}
return ShelfBackgroundType::kLogin;
}
const bool in_split_view_mode =
SplitViewController::Get(shelf_widget_->GetNativeWindow())
->InSplitViewMode();
const bool maximized =
in_split_view_mode ||
state_.window_state == WorkspaceWindowState::kFullscreen ||
state_.window_state == WorkspaceWindowState::kMaximized;
const bool in_overview =
Shell::Get()->overview_controller() &&
Shell::Get()->overview_controller()->InOverviewSession();
if (Shell::Get()->IsInTabletMode()) {
const bool app_list_target_visibility =
Shell::Get()->app_list_controller() &&
Shell::Get()->app_list_controller()->GetTargetVisibility(display_.id());
if (app_list_target_visibility) {
// TODO(https://crbug.com/1058205): Test this behavior.
// If the IME virtual keyboard is showing, the shelf should appear in-app.
// The workspace area in tablet mode is always the in-app workspace area,
// and the virtual keyboard places itself on screen based on workspace
// area.
if (ShelfConfig::Get()->is_virtual_keyboard_shown())
return ShelfBackgroundType::kInApp;
return ShelfBackgroundType::kHomeLauncher;
}
return in_overview ? ShelfBackgroundType::kOverview
: ShelfBackgroundType::kInApp;
}
const bool app_list_is_visible =
Shell::Get()->app_list_controller() &&
Shell::Get()->app_list_controller()->IsVisible(display_.id());
// If ProductivityLauncher is enabled, the AppList is just another bubble, the
// shelf does not need to react to it in clamshell mode.
if (!is_productivity_launcher_enabled_ && app_list_is_visible) {
// When auto-hide shelf is enabled, shelf cannot be considered maximized.
if (maximized && state_.visibility_state != SHELF_AUTO_HIDE)
return ShelfBackgroundType::kMaximizedWithAppList;
return ShelfBackgroundType::kAppList;
}
if (maximized) {
// When a window is maximized, if the auto-hide shelf is enabled and we are
// in clamshell mode, the shelf will keep the default transparent
// background.
if (state_.visibility_state == SHELF_AUTO_HIDE)
return ShelfBackgroundType::kDefaultBg;
return ShelfBackgroundType::kMaximized;
}
if (in_overview)
return ShelfBackgroundType::kOverview;
return ShelfBackgroundType::kDefaultBg;
}
void ShelfLayoutManager::MaybeUpdateShelfBackground(AnimationChangeType type) {
const ShelfBackgroundType new_background_type(GetShelfBackgroundType());
if (new_background_type == shelf_background_type_)
return;
shelf_background_type_ = new_background_type;
for (auto& observer : observers_)
observer.OnBackgroundUpdated(shelf_background_type_, type);
}
bool ShelfLayoutManager::ShouldBlurShelfBackground() {
if (!is_background_blur_enabled_)
return false;
return shelf_background_type_ == ShelfBackgroundType::kDefaultBg &&
state_.session_state == session_manager::SessionState::ACTIVE;
}
bool ShelfLayoutManager::HasVisibleWindow() const {
aura::Window* root = shelf_widget_->GetNativeWindow()->GetRootWindow();
const aura::Window::Windows windows =
Shell::Get()->mru_window_tracker()->BuildWindowListIgnoreModal(
kActiveDesk);
// Process the window list and check if there are any visible windows.
// Ignore app list windows that may be animating to hide after dismissal.
for (auto* window : windows) {
if (window->IsVisible() && !IsAppListWindow(window) &&
root->Contains(window)) {
return true;
}
}
return false;
}
void ShelfLayoutManager::CancelDragOnShelfIfInProgress() {
if (drag_status_ == kDragInProgress ||
drag_status_ == kDragAppListInProgress ||
drag_status_ == kDragHomeToOverviewInProgress) {
CancelDrag(absl::nullopt);
}
}
void ShelfLayoutManager::SuspendVisibilityUpdateForShutdown() {
++suspend_visibility_update_;
}
void ShelfLayoutManager::OnShelfItemSelected(ShelfAction action) {
switch (action) {
case SHELF_ACTION_NONE:
case SHELF_ACTION_APP_LIST_SHOWN:
case SHELF_ACTION_APP_LIST_DISMISSED:
case SHELF_ACTION_APP_LIST_BACK:
case SHELF_ACTION_WINDOW_MINIMIZED:
break;
case SHELF_ACTION_NEW_WINDOW_CREATED:
case SHELF_ACTION_WINDOW_ACTIVATED: {
base::AutoReset<bool> reset(&should_hide_hotseat_, true);
UpdateVisibilityState();
} break;
}
}
void ShelfLayoutManager::OnWindowResized() {
LayoutShelf();
}
void ShelfLayoutManager::SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) {
WmDefaultLayoutManager::SetChildBounds(child, requested_bounds);
// We may contain other widgets (such as frame maximize bubble) but they don't
// affect the layout in any way.
if (phase_ != ShelfLayoutPhase::kMoving &&
((shelf_widget_->GetNativeWindow() == child) ||
(shelf_widget_->status_area_widget()->GetNativeWindow() == child))) {
LayoutShelf(/*animate=*/true);
}
}