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