OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/app_list/presenter/app_list_presenter_impl.h" | 5 #include "ui/app_list/presenter/app_list_presenter_impl.h" |
6 | 6 |
7 #include "ui/app_list/app_list_constants.h" | 7 #include "ui/app_list/app_list_constants.h" |
8 #include "ui/app_list/app_list_switches.h" | 8 #include "ui/app_list/app_list_switches.h" |
9 #include "ui/app_list/pagination_model.h" | 9 #include "ui/app_list/pagination_model.h" |
10 #include "ui/app_list/presenter/app_list_presenter_delegate_factory.h" | 10 #include "ui/app_list/presenter/app_list_presenter_delegate_factory.h" |
11 #include "ui/app_list/views/app_list_view.h" | 11 #include "ui/app_list/views/app_list_view.h" |
12 #include "ui/aura/client/focus_client.h" | 12 #include "ui/aura/client/focus_client.h" |
13 #include "ui/aura/window.h" | 13 #include "ui/aura/window.h" |
14 #include "ui/compositor/layer.h" | 14 #include "ui/compositor/layer.h" |
15 #include "ui/compositor/scoped_layer_animation_settings.h" | 15 #include "ui/compositor/scoped_layer_animation_settings.h" |
16 #include "ui/display/display.h" | |
17 #include "ui/display/screen.h" | |
16 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
17 | 19 |
18 namespace app_list { | 20 namespace app_list { |
19 namespace { | 21 namespace { |
20 | 22 |
21 // Duration for show/hide animation in milliseconds. | 23 // Duration for show/hide animation in milliseconds. |
22 const int kAnimationDurationMs = 200; | 24 const int kAnimationDurationMs = 200; |
23 | 25 |
24 // The maximum shift in pixels when over-scroll happens. | 26 // The maximum shift in pixels when over-scroll happens. |
25 const int kMaxOverScrollShift = 48; | 27 const int kMaxOverScrollShift = 48; |
(...skipping 16 matching lines...) Expand all Loading... | |
42 // Ensures app list view goes before the controller since pagination model | 44 // Ensures app list view goes before the controller since pagination model |
43 // lives in the controller and app list view would access it on destruction. | 45 // lives in the controller and app list view would access it on destruction. |
44 if (view_) { | 46 if (view_) { |
45 view_->GetAppsPaginationModel()->RemoveObserver(this); | 47 view_->GetAppsPaginationModel()->RemoveObserver(this); |
46 if (view_->GetWidget()) | 48 if (view_->GetWidget()) |
47 view_->GetWidget()->CloseNow(); | 49 view_->GetWidget()->CloseNow(); |
48 } | 50 } |
49 } | 51 } |
50 | 52 |
51 aura::Window* AppListPresenterImpl::GetWindow() { | 53 aura::Window* AppListPresenterImpl::GetWindow() { |
52 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : nullptr; | 54 return (GetTargetVisibility() && view_) |
55 ? view_->GetWidget()->GetNativeWindow() | |
56 : nullptr; | |
53 } | 57 } |
54 | 58 |
55 void AppListPresenterImpl::Show(int64_t display_id) { | 59 void AppListPresenterImpl::Show(int64_t display_id) { |
56 if (is_visible_) | 60 if (GetTargetVisibility()) |
57 return; | 61 return; |
58 | 62 |
59 is_visible_ = true; | 63 target_display_id_ = display_id; |
60 if (view_) { | 64 if (view_) { |
61 ScheduleAnimation(); | 65 ScheduleAnimation(); |
62 } else { | 66 } else { |
63 presenter_delegate_ = factory_->GetDelegate(this); | 67 presenter_delegate_ = factory_->GetDelegate(this); |
64 AppListViewDelegate* view_delegate = presenter_delegate_->GetViewDelegate(); | 68 AppListViewDelegate* view_delegate = presenter_delegate_->GetViewDelegate(); |
65 DCHECK(view_delegate); | 69 DCHECK(view_delegate); |
66 // Note the AppListViewDelegate outlives the AppListView. For Ash, the view | 70 // Note the AppListViewDelegate outlives the AppListView. For Ash, the view |
67 // is destroyed when dismissed. | 71 // is destroyed when dismissed. |
68 AppListView* view = new AppListView(view_delegate); | 72 AppListView* view = new AppListView(view_delegate); |
69 presenter_delegate_->Init(view, display_id, current_apps_page_); | 73 presenter_delegate_->Init(view, display_id, current_apps_page_); |
70 SetView(view); | 74 SetView(view); |
71 } | 75 } |
72 presenter_delegate_->OnShown(display_id); | 76 presenter_delegate_->OnShown(display_id); |
73 } | 77 } |
74 | 78 |
75 void AppListPresenterImpl::Dismiss() { | 79 void AppListPresenterImpl::Dismiss() { |
76 if (!is_visible_) | 80 if (!GetTargetVisibility()) |
77 return; | 81 return; |
78 | 82 |
79 // If the app list is currently visible, there should be an existing view. | 83 // If the app list is currently visible, there should be an existing view. |
80 DCHECK(view_); | 84 DCHECK(view_); |
81 | 85 |
82 is_visible_ = false; | 86 target_display_id_ = display::kInvalidDisplayId; |
83 | 87 |
84 // The dismissal may have occurred in response to the app list losing | 88 // The dismissal may have occurred in response to the app list losing |
85 // activation. Otherwise, our widget is currently active. When the animation | 89 // activation. Otherwise, our widget is currently active. When the animation |
86 // completes we'll hide the widget, changing activation. If a menu is shown | 90 // completes we'll hide the widget, changing activation. If a menu is shown |
87 // before the animation completes then the activation change triggers the menu | 91 // before the animation completes then the activation change triggers the menu |
88 // to close. By deactivating now we ensure there is no activation change when | 92 // to close. By deactivating now we ensure there is no activation change when |
89 // the animation completes and any menus stay open. | 93 // the animation completes and any menus stay open. |
90 if (view_->GetWidget()->IsActive()) | 94 if (view_->GetWidget()->IsActive()) |
91 view_->GetWidget()->Deactivate(); | 95 view_->GetWidget()->Deactivate(); |
92 | 96 |
93 presenter_delegate_->OnDismissed(); | 97 presenter_delegate_->OnDismissed(); |
94 ScheduleAnimation(); | 98 ScheduleAnimation(); |
95 } | 99 } |
96 | 100 |
97 void AppListPresenterImpl::ToggleAppList(int64_t display_id) { | 101 void AppListPresenterImpl::ToggleAppList(int64_t display_id) { |
98 if (IsVisible()) { | 102 if (IsVisible()) { |
99 Dismiss(); | 103 Dismiss(); |
100 return; | 104 return; |
101 } | 105 } |
102 Show(display_id); | 106 Show(display_id); |
103 } | 107 } |
104 | 108 |
105 bool AppListPresenterImpl::IsVisible() const { | 109 bool AppListPresenterImpl::IsVisible(int64_t display_id) const { |
106 return view_ && view_->GetWidget()->IsVisible(); | 110 views::Widget* widget = view_ ? view_->GetWidget() : nullptr; |
111 return widget && widget->IsVisible() && | |
112 (display_id == display::kInvalidDisplayId || | |
113 display_id == | |
114 display::Screen::GetScreen() | |
115 ->GetDisplayNearestWindow(widget->GetNativeWindow()) | |
116 .id()); | |
107 } | 117 } |
James Cook
2016/11/30 23:27:19
this function might be easier to read with some ea
| |
108 | 118 |
109 bool AppListPresenterImpl::GetTargetVisibility() const { | 119 bool AppListPresenterImpl::GetTargetVisibility(int64_t display_id) const { |
110 return is_visible_; | 120 return target_display_id_ != display::kInvalidDisplayId && |
James Cook
2016/11/30 23:27:19
nit: This might be clearer as:
if (target_display
| |
121 (display_id == display::kInvalidDisplayId || | |
122 display_id == target_display_id_); | |
111 } | 123 } |
112 | 124 |
113 //////////////////////////////////////////////////////////////////////////////// | 125 //////////////////////////////////////////////////////////////////////////////// |
114 // AppListPresenterImpl, private: | 126 // AppListPresenterImpl, private: |
115 | 127 |
116 void AppListPresenterImpl::SetView(AppListView* view) { | 128 void AppListPresenterImpl::SetView(AppListView* view) { |
117 DCHECK(view_ == nullptr); | 129 DCHECK_EQ(view_, nullptr); |
118 DCHECK(is_visible_); | 130 DCHECK(GetTargetVisibility()); |
119 | 131 |
120 view_ = view; | 132 view_ = view; |
121 views::Widget* widget = view_->GetWidget(); | 133 views::Widget* widget = view_->GetWidget(); |
122 widget->AddObserver(this); | 134 widget->AddObserver(this); |
123 widget->GetNativeView()->GetRootWindow()->AddObserver(this); | 135 widget->GetNativeView()->GetRootWindow()->AddObserver(this); |
124 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this); | 136 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this); |
125 view_->GetAppsPaginationModel()->AddObserver(this); | 137 view_->GetAppsPaginationModel()->AddObserver(this); |
126 view_->ShowWhenReady(); | 138 view_->ShowWhenReady(); |
127 } | 139 } |
128 | 140 |
(...skipping 17 matching lines...) Expand all Loading... | |
146 // Stop observing previous animation. | 158 // Stop observing previous animation. |
147 StopObservingImplicitAnimations(); | 159 StopObservingImplicitAnimations(); |
148 | 160 |
149 views::Widget* widget = view_->GetWidget(); | 161 views::Widget* widget = view_->GetWidget(); |
150 ui::Layer* layer = GetLayer(widget); | 162 ui::Layer* layer = GetLayer(widget); |
151 layer->GetAnimator()->StopAnimating(); | 163 layer->GetAnimator()->StopAnimating(); |
152 | 164 |
153 gfx::Rect target_bounds; | 165 gfx::Rect target_bounds; |
154 gfx::Vector2d offset = presenter_delegate_->GetVisibilityAnimationOffset( | 166 gfx::Vector2d offset = presenter_delegate_->GetVisibilityAnimationOffset( |
155 widget->GetNativeView()->GetRootWindow()); | 167 widget->GetNativeView()->GetRootWindow()); |
156 if (is_visible_) { | 168 if (GetTargetVisibility()) { |
157 target_bounds = widget->GetWindowBoundsInScreen(); | 169 target_bounds = widget->GetWindowBoundsInScreen(); |
158 gfx::Rect start_bounds = gfx::Rect(target_bounds); | 170 gfx::Rect start_bounds = gfx::Rect(target_bounds); |
159 start_bounds.Offset(offset); | 171 start_bounds.Offset(offset); |
160 widget->SetBounds(start_bounds); | 172 widget->SetBounds(start_bounds); |
161 } else { | 173 } else { |
162 target_bounds = widget->GetWindowBoundsInScreen(); | 174 target_bounds = widget->GetWindowBoundsInScreen(); |
163 target_bounds.Offset(offset); | 175 target_bounds.Offset(offset); |
164 } | 176 } |
165 | 177 |
166 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator()); | 178 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator()); |
167 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds( | 179 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds( |
168 is_visible_ ? 0 : kAnimationDurationMs)); | 180 GetTargetVisibility() ? 0 : kAnimationDurationMs)); |
169 animation.AddObserver(this); | 181 animation.AddObserver(this); |
170 | 182 |
171 layer->SetOpacity(is_visible_ ? 1.0 : 0.0); | 183 layer->SetOpacity(GetTargetVisibility() ? 1.0 : 0.0); |
172 widget->SetBounds(target_bounds); | 184 widget->SetBounds(target_bounds); |
173 } | 185 } |
174 | 186 |
175 //////////////////////////////////////////////////////////////////////////////// | 187 //////////////////////////////////////////////////////////////////////////////// |
176 // AppListPresenterImpl, aura::client::FocusChangeObserver implementation: | 188 // AppListPresenterImpl, aura::client::FocusChangeObserver implementation: |
177 | 189 |
178 void AppListPresenterImpl::OnWindowFocused(aura::Window* gained_focus, | 190 void AppListPresenterImpl::OnWindowFocused(aura::Window* gained_focus, |
179 aura::Window* lost_focus) { | 191 aura::Window* lost_focus) { |
180 if (view_ && is_visible_) { | 192 if (view_ && GetTargetVisibility()) { |
181 aura::Window* applist_window = view_->GetWidget()->GetNativeView(); | 193 aura::Window* applist_window = view_->GetWidget()->GetNativeView(); |
182 aura::Window* applist_container = applist_window->parent(); | 194 aura::Window* applist_container = applist_window->parent(); |
183 if (applist_container->Contains(lost_focus) && | 195 if (applist_container->Contains(lost_focus) && |
184 (!gained_focus || !applist_container->Contains(gained_focus)) && | 196 (!gained_focus || !applist_container->Contains(gained_focus)) && |
185 !switches::ShouldNotDismissOnBlur()) { | 197 !switches::ShouldNotDismissOnBlur()) { |
186 Dismiss(); | 198 Dismiss(); |
187 } | 199 } |
188 } | 200 } |
189 } | 201 } |
190 | 202 |
191 //////////////////////////////////////////////////////////////////////////////// | 203 //////////////////////////////////////////////////////////////////////////////// |
192 // AppListPresenterImpl, aura::WindowObserver implementation: | 204 // AppListPresenterImpl, aura::WindowObserver implementation: |
193 void AppListPresenterImpl::OnWindowBoundsChanged(aura::Window* root, | 205 void AppListPresenterImpl::OnWindowBoundsChanged(aura::Window* root, |
194 const gfx::Rect& old_bounds, | 206 const gfx::Rect& old_bounds, |
195 const gfx::Rect& new_bounds) { | 207 const gfx::Rect& new_bounds) { |
196 if (presenter_delegate_) | 208 if (presenter_delegate_) |
197 presenter_delegate_->UpdateBounds(); | 209 presenter_delegate_->UpdateBounds(); |
198 } | 210 } |
199 | 211 |
200 //////////////////////////////////////////////////////////////////////////////// | 212 //////////////////////////////////////////////////////////////////////////////// |
201 // AppListPresenterImpl, ui::ImplicitAnimationObserver implementation: | 213 // AppListPresenterImpl, ui::ImplicitAnimationObserver implementation: |
202 | 214 |
203 void AppListPresenterImpl::OnImplicitAnimationsCompleted() { | 215 void AppListPresenterImpl::OnImplicitAnimationsCompleted() { |
204 if (is_visible_) | 216 if (GetTargetVisibility()) |
205 view_->GetWidget()->Activate(); | 217 view_->GetWidget()->Activate(); |
206 else | 218 else |
207 view_->GetWidget()->Close(); | 219 view_->GetWidget()->Close(); |
208 } | 220 } |
209 | 221 |
210 //////////////////////////////////////////////////////////////////////////////// | 222 //////////////////////////////////////////////////////////////////////////////// |
211 // AppListPresenterImpl, views::WidgetObserver implementation: | 223 // AppListPresenterImpl, views::WidgetObserver implementation: |
212 | 224 |
213 void AppListPresenterImpl::OnWidgetDestroying(views::Widget* widget) { | 225 void AppListPresenterImpl::OnWidgetDestroying(views::Widget* widget) { |
214 DCHECK(view_->GetWidget() == widget); | 226 DCHECK(view_->GetWidget() == widget); |
215 if (is_visible_) | 227 if (GetTargetVisibility()) |
216 Dismiss(); | 228 Dismiss(); |
217 ResetView(); | 229 ResetView(); |
218 } | 230 } |
219 | 231 |
220 //////////////////////////////////////////////////////////////////////////////// | 232 //////////////////////////////////////////////////////////////////////////////// |
221 // AppListPresenterImpl, PaginationModelObserver implementation: | 233 // AppListPresenterImpl, PaginationModelObserver implementation: |
222 | 234 |
223 void AppListPresenterImpl::TotalPagesChanged() {} | 235 void AppListPresenterImpl::TotalPagesChanged() {} |
224 | 236 |
225 void AppListPresenterImpl::SelectedPageChanged(int old_selected, | 237 void AppListPresenterImpl::SelectedPageChanged(int old_selected, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 } else if (should_snap_back_) { | 277 } else if (should_snap_back_) { |
266 should_snap_back_ = false; | 278 should_snap_back_ = false; |
267 ui::ScopedLayerAnimationSettings animation(widget_animator); | 279 ui::ScopedLayerAnimationSettings animation(widget_animator); |
268 animation.SetTransitionDuration( | 280 animation.SetTransitionDuration( |
269 base::TimeDelta::FromMilliseconds(kOverscrollPageTransitionDurationMs)); | 281 base::TimeDelta::FromMilliseconds(kOverscrollPageTransitionDurationMs)); |
270 widget->SetBounds(view_bounds_); | 282 widget->SetBounds(view_bounds_); |
271 } | 283 } |
272 } | 284 } |
273 | 285 |
274 } // namespace app_list | 286 } // namespace app_list |
OLD | NEW |