OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/common/shelf/wm_shelf.h" | |
6 | |
7 #include "ash/common/shelf/shelf_controller.h" | |
8 #include "ash/common/shelf/shelf_delegate.h" | |
9 #include "ash/common/shelf/shelf_item_delegate.h" | |
10 #include "ash/common/shelf/shelf_layout_manager.h" | |
11 #include "ash/common/shelf/shelf_locking_manager.h" | |
12 #include "ash/common/shelf/shelf_model.h" | |
13 #include "ash/common/shelf/shelf_widget.h" | |
14 #include "ash/common/shelf/wm_shelf_observer.h" | |
15 #include "ash/common/wm_shell.h" | |
16 #include "ash/common/wm_window.h" | |
17 #include "ash/public/cpp/shell_window_ids.h" | |
18 #include "ash/root_window_controller.h" | |
19 #include "ash/shelf/shelf_bezel_event_handler.h" | |
20 #include "ash/shell.h" | |
21 #include "ash/system/tray/system_tray_delegate.h" | |
22 #include "base/logging.h" | |
23 #include "base/memory/ptr_util.h" | |
24 #include "ui/aura/env.h" | |
25 #include "ui/display/types/display_constants.h" | |
26 #include "ui/gfx/geometry/rect.h" | |
27 | |
28 namespace ash { | |
29 | |
30 // WmShelf::AutoHideEventHandler ----------------------------------------------- | |
31 | |
32 // Forwards mouse and gesture events to ShelfLayoutManager for auto-hide. | |
33 // TODO(mash): Add similar event handling support for mash. | |
34 class WmShelf::AutoHideEventHandler : public ui::EventHandler { | |
35 public: | |
36 explicit AutoHideEventHandler(ShelfLayoutManager* shelf_layout_manager) | |
37 : shelf_layout_manager_(shelf_layout_manager) { | |
38 Shell::GetInstance()->AddPreTargetHandler(this); | |
39 } | |
40 ~AutoHideEventHandler() override { | |
41 Shell::GetInstance()->RemovePreTargetHandler(this); | |
42 } | |
43 | |
44 // Overridden from ui::EventHandler: | |
45 void OnMouseEvent(ui::MouseEvent* event) override { | |
46 shelf_layout_manager_->UpdateAutoHideForMouseEvent( | |
47 event, WmWindow::Get(static_cast<aura::Window*>(event->target()))); | |
48 } | |
49 void OnGestureEvent(ui::GestureEvent* event) override { | |
50 shelf_layout_manager_->UpdateAutoHideForGestureEvent( | |
51 event, WmWindow::Get(static_cast<aura::Window*>(event->target()))); | |
52 } | |
53 | |
54 private: | |
55 ShelfLayoutManager* shelf_layout_manager_; | |
56 DISALLOW_COPY_AND_ASSIGN(AutoHideEventHandler); | |
57 }; | |
58 | |
59 // WmShelf --------------------------------------------------------------------- | |
60 | |
61 WmShelf::WmShelf() {} | |
62 | |
63 WmShelf::~WmShelf() {} | |
64 | |
65 // static | |
66 WmShelf* WmShelf::ForWindow(WmWindow* window) { | |
67 return window->GetRootWindowController()->GetShelf(); | |
68 } | |
69 | |
70 // static | |
71 bool WmShelf::CanChangeShelfAlignment() { | |
72 if (WmShell::Get()->system_tray_delegate()->IsUserSupervised()) | |
73 return false; | |
74 | |
75 LoginStatus login_status = | |
76 WmShell::Get()->system_tray_delegate()->GetUserLoginStatus(); | |
77 | |
78 switch (login_status) { | |
79 case LoginStatus::LOCKED: | |
80 // Shelf alignment changes can be requested while being locked, but will | |
81 // be applied upon unlock. | |
82 case LoginStatus::USER: | |
83 case LoginStatus::OWNER: | |
84 return true; | |
85 case LoginStatus::PUBLIC: | |
86 case LoginStatus::SUPERVISED: | |
87 case LoginStatus::GUEST: | |
88 case LoginStatus::KIOSK_APP: | |
89 case LoginStatus::ARC_KIOSK_APP: | |
90 case LoginStatus::NOT_LOGGED_IN: | |
91 return false; | |
92 } | |
93 | |
94 NOTREACHED(); | |
95 return false; | |
96 } | |
97 | |
98 void WmShelf::CreateShelfWidget(WmWindow* root) { | |
99 DCHECK(!shelf_widget_); | |
100 WmWindow* shelf_container = | |
101 root->GetChildByShellWindowId(kShellWindowId_ShelfContainer); | |
102 shelf_widget_.reset(new ShelfWidget(shelf_container, this)); | |
103 | |
104 DCHECK(!shelf_layout_manager_); | |
105 shelf_layout_manager_ = shelf_widget_->shelf_layout_manager(); | |
106 shelf_layout_manager_->AddObserver(this); | |
107 | |
108 // Must occur after |shelf_widget_| is constructed because the system tray | |
109 // constructors call back into WmShelf::shelf_widget(). | |
110 DCHECK(!shelf_widget_->status_area_widget()); | |
111 WmWindow* status_container = | |
112 root->GetChildByShellWindowId(kShellWindowId_StatusContainer); | |
113 shelf_widget_->CreateStatusAreaWidget(status_container); | |
114 | |
115 // TODO: ShelfBezelEventHandler needs to work with mus too. | |
116 // http://crbug.com/636647 | |
117 if (aura::Env::GetInstance()->mode() == aura::Env::Mode::LOCAL) | |
118 bezel_event_handler_ = base::MakeUnique<ShelfBezelEventHandler>(this); | |
119 } | |
120 | |
121 void WmShelf::ShutdownShelfWidget() { | |
122 if (shelf_widget_) | |
123 shelf_widget_->Shutdown(); | |
124 } | |
125 | |
126 void WmShelf::DestroyShelfWidget() { | |
127 shelf_widget_.reset(); | |
128 } | |
129 | |
130 void WmShelf::CreateShelfView() { | |
131 DCHECK(shelf_layout_manager_); | |
132 DCHECK(shelf_widget_); | |
133 DCHECK(!shelf_view_); | |
134 shelf_view_ = shelf_widget_->CreateShelfView(); | |
135 shelf_locking_manager_.reset(new ShelfLockingManager(this)); | |
136 WmShell::Get()->shelf_controller()->NotifyShelfCreated(this); | |
137 } | |
138 | |
139 void WmShelf::ShutdownShelf() { | |
140 DCHECK(shelf_view_); | |
141 shelf_locking_manager_.reset(); | |
142 shelf_view_ = nullptr; | |
143 } | |
144 | |
145 bool WmShelf::IsShelfInitialized() const { | |
146 return !!shelf_view_; | |
147 } | |
148 | |
149 WmWindow* WmShelf::GetWindow() { | |
150 return WmWindow::Get(shelf_widget_->GetNativeWindow()); | |
151 } | |
152 | |
153 void WmShelf::SetAlignment(ShelfAlignment alignment) { | |
154 DCHECK(shelf_layout_manager_); | |
155 DCHECK(shelf_locking_manager_); | |
156 | |
157 if (alignment_ == alignment) | |
158 return; | |
159 | |
160 if (shelf_locking_manager_->is_locked() && | |
161 alignment != SHELF_ALIGNMENT_BOTTOM_LOCKED) { | |
162 shelf_locking_manager_->set_stored_alignment(alignment); | |
163 return; | |
164 } | |
165 | |
166 alignment_ = alignment; | |
167 // The ShelfWidget notifies the ShelfView of the alignment change. | |
168 shelf_widget_->OnShelfAlignmentChanged(); | |
169 shelf_layout_manager_->LayoutShelf(); | |
170 WmShell::Get()->shelf_controller()->NotifyShelfAlignmentChanged(this); | |
171 WmShell::Get()->NotifyShelfAlignmentChanged(GetWindow()->GetRootWindow()); | |
172 } | |
173 | |
174 bool WmShelf::IsHorizontalAlignment() const { | |
175 switch (alignment_) { | |
176 case SHELF_ALIGNMENT_BOTTOM: | |
177 case SHELF_ALIGNMENT_BOTTOM_LOCKED: | |
178 return true; | |
179 case SHELF_ALIGNMENT_LEFT: | |
180 case SHELF_ALIGNMENT_RIGHT: | |
181 return false; | |
182 } | |
183 NOTREACHED(); | |
184 return true; | |
185 } | |
186 | |
187 int WmShelf::SelectValueForShelfAlignment(int bottom, | |
188 int left, | |
189 int right) const { | |
190 switch (alignment_) { | |
191 case SHELF_ALIGNMENT_BOTTOM: | |
192 case SHELF_ALIGNMENT_BOTTOM_LOCKED: | |
193 return bottom; | |
194 case SHELF_ALIGNMENT_LEFT: | |
195 return left; | |
196 case SHELF_ALIGNMENT_RIGHT: | |
197 return right; | |
198 } | |
199 NOTREACHED(); | |
200 return bottom; | |
201 } | |
202 | |
203 int WmShelf::PrimaryAxisValue(int horizontal, int vertical) const { | |
204 return IsHorizontalAlignment() ? horizontal : vertical; | |
205 } | |
206 | |
207 void WmShelf::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide_behavior) { | |
208 DCHECK(shelf_layout_manager_); | |
209 | |
210 if (auto_hide_behavior_ == auto_hide_behavior) | |
211 return; | |
212 | |
213 auto_hide_behavior_ = auto_hide_behavior; | |
214 WmShell::Get()->shelf_controller()->NotifyShelfAutoHideBehaviorChanged(this); | |
215 WmShell::Get()->NotifyShelfAutoHideBehaviorChanged( | |
216 GetWindow()->GetRootWindow()); | |
217 } | |
218 | |
219 ShelfAutoHideState WmShelf::GetAutoHideState() const { | |
220 return shelf_layout_manager_->auto_hide_state(); | |
221 } | |
222 | |
223 void WmShelf::UpdateAutoHideState() { | |
224 shelf_layout_manager_->UpdateAutoHideState(); | |
225 } | |
226 | |
227 ShelfBackgroundType WmShelf::GetBackgroundType() const { | |
228 return shelf_widget_->GetBackgroundType(); | |
229 } | |
230 | |
231 bool WmShelf::IsVisible() const { | |
232 return shelf_widget_->IsShelfVisible(); | |
233 } | |
234 | |
235 void WmShelf::UpdateVisibilityState() { | |
236 if (shelf_layout_manager_) | |
237 shelf_layout_manager_->UpdateVisibilityState(); | |
238 } | |
239 | |
240 ShelfVisibilityState WmShelf::GetVisibilityState() const { | |
241 return shelf_layout_manager_ ? shelf_layout_manager_->visibility_state() | |
242 : SHELF_HIDDEN; | |
243 } | |
244 | |
245 gfx::Rect WmShelf::GetIdealBounds() { | |
246 return shelf_layout_manager_->GetIdealBounds(); | |
247 } | |
248 | |
249 gfx::Rect WmShelf::GetUserWorkAreaBounds() const { | |
250 return shelf_layout_manager_ ? shelf_layout_manager_->user_work_area_bounds() | |
251 : gfx::Rect(); | |
252 } | |
253 | |
254 void WmShelf::UpdateIconPositionForPanel(WmWindow* panel) { | |
255 shelf_widget_->UpdateIconPositionForPanel(panel); | |
256 } | |
257 | |
258 gfx::Rect WmShelf::GetScreenBoundsOfItemIconForWindow(WmWindow* window) { | |
259 if (!shelf_widget_) | |
260 return gfx::Rect(); | |
261 return shelf_widget_->GetScreenBoundsOfItemIconForWindow(window); | |
262 } | |
263 | |
264 // static | |
265 void WmShelf::LaunchShelfItem(int item_index) { | |
266 ShelfModel* shelf_model = WmShell::Get()->shelf_model(); | |
267 const ShelfItems& items = shelf_model->items(); | |
268 int item_count = shelf_model->item_count(); | |
269 int indexes_left = item_index >= 0 ? item_index : item_count; | |
270 int found_index = -1; | |
271 | |
272 // Iterating until we have hit the index we are interested in which | |
273 // is true once indexes_left becomes negative. | |
274 for (int i = 0; i < item_count && indexes_left >= 0; i++) { | |
275 if (items[i].type != TYPE_APP_LIST) { | |
276 found_index = i; | |
277 indexes_left--; | |
278 } | |
279 } | |
280 | |
281 // There are two ways how found_index can be valid: a.) the nth item was | |
282 // found (which is true when indexes_left is -1) or b.) the last item was | |
283 // requested (which is true when index was passed in as a negative number). | |
284 if (found_index >= 0 && (indexes_left == -1 || item_index < 0)) { | |
285 // Then set this one as active (or advance to the next item of its kind). | |
286 ActivateShelfItem(found_index); | |
287 } | |
288 } | |
289 | |
290 // static | |
291 void WmShelf::ActivateShelfItem(int item_index) { | |
292 ShelfModel* shelf_model = WmShell::Get()->shelf_model(); | |
293 const ShelfItem& item = shelf_model->items()[item_index]; | |
294 ShelfItemDelegate* item_delegate = shelf_model->GetShelfItemDelegate(item.id); | |
295 item_delegate->ItemSelected(ui::ET_KEY_RELEASED, ui::EF_NONE, | |
296 display::kInvalidDisplayId, LAUNCH_FROM_UNKNOWN); | |
297 } | |
298 | |
299 bool WmShelf::ProcessGestureEvent(const ui::GestureEvent& event) { | |
300 // Can be called at login screen. | |
301 if (!shelf_layout_manager_) | |
302 return false; | |
303 return shelf_layout_manager_->ProcessGestureEvent(event); | |
304 } | |
305 | |
306 void WmShelf::AddObserver(WmShelfObserver* observer) { | |
307 observers_.AddObserver(observer); | |
308 } | |
309 | |
310 void WmShelf::RemoveObserver(WmShelfObserver* observer) { | |
311 observers_.RemoveObserver(observer); | |
312 } | |
313 | |
314 void WmShelf::NotifyShelfIconPositionsChanged() { | |
315 for (auto& observer : observers_) | |
316 observer.OnShelfIconPositionsChanged(); | |
317 } | |
318 | |
319 StatusAreaWidget* WmShelf::GetStatusAreaWidget() const { | |
320 return shelf_widget_->status_area_widget(); | |
321 } | |
322 | |
323 void WmShelf::SetVirtualKeyboardBoundsForTesting(const gfx::Rect& bounds) { | |
324 shelf_layout_manager_->OnKeyboardBoundsChanging(bounds); | |
325 } | |
326 | |
327 ShelfLockingManager* WmShelf::GetShelfLockingManagerForTesting() { | |
328 return shelf_locking_manager_.get(); | |
329 } | |
330 | |
331 ShelfView* WmShelf::GetShelfViewForTesting() { | |
332 return shelf_view_; | |
333 } | |
334 | |
335 void WmShelf::WillDeleteShelfLayoutManager() { | |
336 if (aura::Env::GetInstance()->mode() == aura::Env::Mode::MUS) { | |
337 // TODO(sky): this should be removed once Shell is used everywhere. | |
338 ShutdownShelfWidget(); | |
339 } | |
340 | |
341 // Clear event handlers that might forward events to the destroyed instance. | |
342 auto_hide_event_handler_.reset(); | |
343 bezel_event_handler_.reset(); | |
344 | |
345 DCHECK(shelf_layout_manager_); | |
346 shelf_layout_manager_->RemoveObserver(this); | |
347 shelf_layout_manager_ = nullptr; | |
348 } | |
349 | |
350 void WmShelf::WillChangeVisibilityState(ShelfVisibilityState new_state) { | |
351 for (auto& observer : observers_) | |
352 observer.WillChangeVisibilityState(new_state); | |
353 if (new_state != SHELF_AUTO_HIDE) { | |
354 auto_hide_event_handler_.reset(); | |
355 } else if (!auto_hide_event_handler_ && | |
356 aura::Env::GetInstance()->mode() == aura::Env::Mode::LOCAL) { | |
357 auto_hide_event_handler_ = | |
358 base::MakeUnique<AutoHideEventHandler>(shelf_layout_manager()); | |
359 } | |
360 } | |
361 | |
362 void WmShelf::OnAutoHideStateChanged(ShelfAutoHideState new_state) { | |
363 for (auto& observer : observers_) | |
364 observer.OnAutoHideStateChanged(new_state); | |
365 } | |
366 | |
367 void WmShelf::OnBackgroundUpdated(ShelfBackgroundType background_type, | |
368 AnimationChangeType change_type) { | |
369 if (background_type == GetBackgroundType()) | |
370 return; | |
371 for (auto& observer : observers_) | |
372 observer.OnBackgroundTypeChanged(background_type, change_type); | |
373 } | |
374 | |
375 } // namespace ash | |
OLD | NEW |