OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ash/wm/app_list_controller.h" | 5 #include "ash/wm/app_list_controller.h" |
6 | 6 |
7 #include "ash/launcher/launcher.h" | 7 #include "ash/launcher/launcher.h" |
8 #include "ash/root_window_controller.h" | 8 #include "ash/root_window_controller.h" |
9 #include "ash/shelf/shelf_layout_manager.h" | 9 #include "ash/shelf/shelf_layout_manager.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
11 #include "ash/shell_delegate.h" | 11 #include "ash/shell_delegate.h" |
12 #include "ash/shell_window_ids.h" | 12 #include "ash/shell_window_ids.h" |
| 13 #include "ash/wm/app_list_controller_observer.h" |
13 #include "ash/wm/property_util.h" | 14 #include "ash/wm/property_util.h" |
14 #include "ui/app_list/app_list_constants.h" | 15 #include "ui/app_list/app_list_constants.h" |
15 #include "ui/app_list/pagination_model.h" | 16 #include "ui/app_list/pagination_model.h" |
16 #include "ui/app_list/views/app_list_view.h" | 17 #include "ui/app_list/views/app_list_view.h" |
17 #include "ui/aura/client/focus_client.h" | 18 #include "ui/aura/client/focus_client.h" |
18 #include "ui/aura/root_window.h" | 19 #include "ui/aura/root_window.h" |
19 #include "ui/aura/window.h" | 20 #include "ui/aura/window.h" |
20 #include "ui/base/events/event.h" | 21 #include "ui/base/events/event.h" |
21 #include "ui/compositor/layer.h" | 22 #include "ui/compositor/layer.h" |
22 #include "ui/compositor/scoped_layer_animation_settings.h" | 23 #include "ui/compositor/scoped_layer_animation_settings.h" |
(...skipping 22 matching lines...) Expand all Loading... |
45 views::BubbleBorder::Arrow GetBubbleArrow(aura::Window* window) { | 46 views::BubbleBorder::Arrow GetBubbleArrow(aura::Window* window) { |
46 DCHECK(Shell::HasInstance()); | 47 DCHECK(Shell::HasInstance()); |
47 return ShelfLayoutManager::ForLauncher(window)-> | 48 return ShelfLayoutManager::ForLauncher(window)-> |
48 SelectValueForShelfAlignment( | 49 SelectValueForShelfAlignment( |
49 views::BubbleBorder::BOTTOM_CENTER, | 50 views::BubbleBorder::BOTTOM_CENTER, |
50 views::BubbleBorder::LEFT_CENTER, | 51 views::BubbleBorder::LEFT_CENTER, |
51 views::BubbleBorder::RIGHT_CENTER, | 52 views::BubbleBorder::RIGHT_CENTER, |
52 views::BubbleBorder::TOP_CENTER); | 53 views::BubbleBorder::TOP_CENTER); |
53 } | 54 } |
54 | 55 |
55 // Offset given |rect| towards shelf. | 56 // Offsets the given |rect| towards shelf. |
56 gfx::Rect OffsetTowardsShelf(const gfx::Rect& rect, views::Widget* widget) { | 57 gfx::Rect OffsetTowardsShelf(Shell* shell, |
57 DCHECK(Shell::HasInstance()); | 58 const gfx::Rect& rect, |
58 ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment( | 59 views::Widget* widget) { |
| 60 ShelfAlignment shelf_alignment = shell->GetShelfAlignment( |
59 widget->GetNativeView()->GetRootWindow()); | 61 widget->GetNativeView()->GetRootWindow()); |
60 gfx::Rect offseted(rect); | 62 gfx::Rect offseted(rect); |
61 switch (shelf_alignment) { | 63 switch (shelf_alignment) { |
62 case SHELF_ALIGNMENT_BOTTOM: | 64 case SHELF_ALIGNMENT_BOTTOM: |
63 offseted.Offset(0, kAnimationOffset); | 65 offseted.Offset(0, kAnimationOffset); |
64 break; | 66 break; |
65 case SHELF_ALIGNMENT_LEFT: | 67 case SHELF_ALIGNMENT_LEFT: |
66 offseted.Offset(-kAnimationOffset, 0); | 68 offseted.Offset(-kAnimationOffset, 0); |
67 break; | 69 break; |
68 case SHELF_ALIGNMENT_RIGHT: | 70 case SHELF_ALIGNMENT_RIGHT: |
69 offseted.Offset(kAnimationOffset, 0); | 71 offseted.Offset(kAnimationOffset, 0); |
70 break; | 72 break; |
71 case SHELF_ALIGNMENT_TOP: | 73 case SHELF_ALIGNMENT_TOP: |
72 offseted.Offset(0, -kAnimationOffset); | 74 offseted.Offset(0, -kAnimationOffset); |
73 break; | 75 break; |
74 } | 76 } |
75 | 77 |
76 return offseted; | 78 return offseted; |
77 } | 79 } |
78 | 80 |
79 } // namespace | 81 } // namespace |
80 | 82 |
81 //////////////////////////////////////////////////////////////////////////////// | 83 //////////////////////////////////////////////////////////////////////////////// |
82 // AppListController, public: | 84 // AppListController, public: |
83 | 85 |
84 AppListController::AppListController() | 86 AppListController::AppListController(Shell* shell) |
85 : pagination_model_(new app_list::PaginationModel), | 87 : shell_(shell), |
| 88 pagination_model_(new app_list::PaginationModel), |
86 is_visible_(false), | 89 is_visible_(false), |
87 view_(NULL), | 90 view_(NULL), |
88 should_snap_back_(false) { | 91 should_snap_back_(false) { |
89 Shell::GetInstance()->AddShellObserver(this); | 92 shell_->AddShellObserver(this); |
90 pagination_model_->AddObserver(this); | 93 pagination_model_->AddObserver(this); |
91 } | 94 } |
92 | 95 |
93 AppListController::~AppListController() { | 96 AppListController::~AppListController() { |
| 97 Shutdown(); |
| 98 pagination_model_->RemoveObserver(this); |
| 99 } |
| 100 |
| 101 void AppListController::Shutdown() { |
94 // Ensures app list view goes before the controller since pagination model | 102 // Ensures app list view goes before the controller since pagination model |
95 // lives in the controller and app list view would access it on destruction. | 103 // lives in the controller and app list view would access it on destruction. |
96 if (view_ && view_->GetWidget()) | 104 if (view_ && view_->GetWidget()) |
97 view_->GetWidget()->CloseNow(); | 105 view_->GetWidget()->CloseNow(); |
98 | 106 |
99 Shell::GetInstance()->RemoveShellObserver(this); | 107 if (shell_) { |
100 pagination_model_->RemoveObserver(this); | 108 shell_->RemoveShellObserver(this); |
| 109 shell_ = NULL; |
| 110 } |
101 } | 111 } |
102 | 112 |
103 void AppListController::SetVisible(bool visible, aura::Window* window) { | 113 void AppListController::SetVisible(bool visible, aura::Window* window) { |
104 if (visible == is_visible_) | 114 if (visible == is_visible_) |
105 return; | 115 return; |
106 | 116 |
107 is_visible_ = visible; | 117 is_visible_ = visible; |
108 | 118 |
109 // App list needs to know the new shelf layout in order to calculate its | 119 // App list needs to know the new shelf layout in order to calculate its |
110 // UI layout when AppListView visibility changes. | 120 // UI layout when AppListView visibility changes. |
111 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()-> | 121 Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager()-> |
112 UpdateAutoHideState(); | 122 UpdateAutoHideState(); |
113 | 123 |
| 124 // Determine the root window to show the UI. |
| 125 aura::RootWindow* root_window = |
| 126 window ? window->GetRootWindow() : Shell::GetActiveRootWindow(); |
| 127 |
114 if (view_) { | 128 if (view_) { |
115 ScheduleAnimation(); | 129 ScheduleAnimation(); |
116 } else if (is_visible_) { | 130 } else if (is_visible_) { |
117 // AppListModel and AppListViewDelegate are owned by AppListView. They | 131 // AppListModel and AppListViewDelegate are owned by AppListView. They |
118 // will be released with AppListView on close. | 132 // will be released with AppListView on close. |
119 app_list::AppListView* view = new app_list::AppListView( | 133 app_list::AppListView* view = new app_list::AppListView( |
120 Shell::GetInstance()->delegate()->CreateAppListViewDelegate()); | 134 shell_->delegate()->CreateAppListViewDelegate()); |
121 aura::Window* container = GetRootWindowController(window->GetRootWindow())-> | 135 aura::Window* container = GetRootWindowController(root_window)-> |
122 GetContainer(kShellWindowId_AppListContainer); | 136 GetContainer(kShellWindowId_AppListContainer); |
123 view->InitAsBubble( | 137 view->InitAsBubble( |
124 container, | 138 container, |
125 pagination_model_.get(), | 139 pagination_model_.get(), |
126 Launcher::ForWindow(container)->GetAppListButtonView(), | 140 Launcher::ForWindow(container)->GetAppListButtonView(), |
127 gfx::Point(), | 141 gfx::Point(), |
128 GetBubbleArrow(container), | 142 GetBubbleArrow(container), |
129 true /* border_accepts_events */); | 143 true /* border_accepts_events */); |
130 SetView(view); | 144 SetView(view); |
131 } | 145 } |
| 146 |
| 147 FOR_EACH_OBSERVER(AppListControllerObserver, |
| 148 observers_, |
| 149 OnAppLauncherVisibilityChanged(is_visible_, root_window)); |
132 } | 150 } |
133 | 151 |
134 bool AppListController::IsVisible() const { | 152 bool AppListController::IsVisible() const { |
135 return view_ && view_->GetWidget()->IsVisible(); | 153 return view_ && view_->GetWidget()->IsVisible(); |
136 } | 154 } |
137 | 155 |
| 156 void AppListController::Toggle(aura::Window* window) { |
| 157 SetVisible(!IsVisible(), window); |
| 158 } |
| 159 |
138 aura::Window* AppListController::GetWindow() { | 160 aura::Window* AppListController::GetWindow() { |
139 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL; | 161 return is_visible_ && view_ ? view_->GetWidget()->GetNativeWindow() : NULL; |
140 } | 162 } |
141 | 163 |
| 164 void AppListController::AddObserver(AppListControllerObserver* observer) { |
| 165 observers_.AddObserver(observer); |
| 166 } |
| 167 |
| 168 void AppListController::RemoveObserver(AppListControllerObserver* observer) { |
| 169 observers_.RemoveObserver(observer); |
| 170 } |
| 171 |
142 //////////////////////////////////////////////////////////////////////////////// | 172 //////////////////////////////////////////////////////////////////////////////// |
143 // AppListController, private: | 173 // AppListController, private: |
144 | 174 |
145 void AppListController::SetView(app_list::AppListView* view) { | 175 void AppListController::SetView(app_list::AppListView* view) { |
146 DCHECK(view_ == NULL); | 176 DCHECK(view_ == NULL); |
147 DCHECK(is_visible_); | 177 DCHECK(is_visible_); |
148 | 178 |
149 view_ = view; | 179 view_ = view; |
150 views::Widget* widget = view_->GetWidget(); | 180 views::Widget* widget = view_->GetWidget(); |
151 widget->AddObserver(this); | 181 widget->AddObserver(this); |
152 Shell::GetInstance()->AddPreTargetHandler(this); | 182 shell_->AddPreTargetHandler(this); |
153 Launcher::ForWindow(widget->GetNativeWindow())->AddIconObserver(this); | 183 Launcher::ForWindow(widget->GetNativeWindow())->AddIconObserver(this); |
154 widget->GetNativeView()->GetRootWindow()->AddRootWindowObserver(this); | 184 widget->GetNativeView()->GetRootWindow()->AddRootWindowObserver(this); |
155 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this); | 185 aura::client::GetFocusClient(widget->GetNativeView())->AddObserver(this); |
156 | 186 |
157 view_->ShowWhenReady(); | 187 view_->ShowWhenReady(); |
158 } | 188 } |
159 | 189 |
160 void AppListController::ResetView() { | 190 void AppListController::ResetView() { |
161 if (!view_) | 191 if (!view_) |
162 return; | 192 return; |
163 | 193 |
164 views::Widget* widget = view_->GetWidget(); | 194 views::Widget* widget = view_->GetWidget(); |
165 widget->RemoveObserver(this); | 195 widget->RemoveObserver(this); |
166 GetLayer(widget)->GetAnimator()->RemoveObserver(this); | 196 GetLayer(widget)->GetAnimator()->RemoveObserver(this); |
167 Shell::GetInstance()->RemovePreTargetHandler(this); | 197 shell_->RemovePreTargetHandler(this); |
168 Launcher::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this); | 198 Launcher::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this); |
169 widget->GetNativeView()->GetRootWindow()->RemoveRootWindowObserver(this); | 199 widget->GetNativeView()->GetRootWindow()->RemoveRootWindowObserver(this); |
170 aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this); | 200 aura::client::GetFocusClient(widget->GetNativeView())->RemoveObserver(this); |
171 view_ = NULL; | 201 view_ = NULL; |
172 } | 202 } |
173 | 203 |
174 void AppListController::ScheduleAnimation() { | 204 void AppListController::ScheduleAnimation() { |
175 // Stop observing previous animation. | 205 // Stop observing previous animation. |
176 StopObservingImplicitAnimations(); | 206 StopObservingImplicitAnimations(); |
177 | 207 |
178 views::Widget* widget = view_->GetWidget(); | 208 views::Widget* widget = view_->GetWidget(); |
179 ui::Layer* layer = GetLayer(widget); | 209 ui::Layer* layer = GetLayer(widget); |
180 layer->GetAnimator()->StopAnimating(); | 210 layer->GetAnimator()->StopAnimating(); |
181 | 211 |
182 gfx::Rect target_bounds; | 212 gfx::Rect target_bounds; |
183 if (is_visible_) { | 213 if (is_visible_) { |
184 target_bounds = widget->GetWindowBoundsInScreen(); | 214 target_bounds = widget->GetWindowBoundsInScreen(); |
185 widget->SetBounds(OffsetTowardsShelf(target_bounds, widget)); | 215 widget->SetBounds(OffsetTowardsShelf(shell_, target_bounds, widget)); |
186 } else { | 216 } else { |
187 target_bounds = OffsetTowardsShelf(widget->GetWindowBoundsInScreen(), | 217 target_bounds = OffsetTowardsShelf(shell_, |
| 218 widget->GetWindowBoundsInScreen(), |
188 widget); | 219 widget); |
189 } | 220 } |
190 | 221 |
191 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator()); | 222 ui::ScopedLayerAnimationSettings animation(layer->GetAnimator()); |
192 animation.SetTransitionDuration( | 223 animation.SetTransitionDuration( |
193 base::TimeDelta::FromMilliseconds( | 224 base::TimeDelta::FromMilliseconds( |
194 is_visible_ ? 0 : kAnimationDurationMs)); | 225 is_visible_ ? 0 : kAnimationDurationMs)); |
195 animation.AddObserver(this); | 226 animation.AddObserver(this); |
196 | 227 |
197 layer->SetOpacity(is_visible_ ? 1.0 : 0.0); | 228 layer->SetOpacity(is_visible_ ? 1.0 : 0.0); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 should_snap_back_ = false; | 371 should_snap_back_ = false; |
341 ui::ScopedLayerAnimationSettings animation(widget_animator); | 372 ui::ScopedLayerAnimationSettings animation(widget_animator); |
342 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds( | 373 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds( |
343 app_list::kOverscrollPageTransitionDurationMs)); | 374 app_list::kOverscrollPageTransitionDurationMs)); |
344 widget->SetBounds(view_bounds_); | 375 widget->SetBounds(view_bounds_); |
345 } | 376 } |
346 } | 377 } |
347 | 378 |
348 } // namespace internal | 379 } // namespace internal |
349 } // namespace ash | 380 } // namespace ash |
OLD | NEW |