OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/wm/common/panels/panel_layout_manager.h" | |
6 | |
7 #include <algorithm> | |
8 #include <map> | |
9 #include <utility> | |
10 | |
11 #include "ash/wm/common/shelf/wm_shelf.h" | |
12 #include "ash/wm/common/shelf/wm_shelf_util.h" | |
13 #include "ash/wm/common/window_animation_types.h" | |
14 #include "ash/wm/common/window_parenting_utils.h" | |
15 #include "ash/wm/common/window_state.h" | |
16 #include "ash/wm/common/wm_globals.h" | |
17 #include "ash/wm/common/wm_lookup.h" | |
18 #include "ash/wm/common/wm_root_window_controller.h" | |
19 #include "ash/wm/common/wm_shell_window_ids.h" | |
20 #include "ash/wm/common/wm_window.h" | |
21 #include "ash/wm/common/wm_window_property.h" | |
22 #include "base/auto_reset.h" | |
23 #include "third_party/skia/include/core/SkColor.h" | |
24 #include "third_party/skia/include/core/SkPaint.h" | |
25 #include "third_party/skia/include/core/SkPath.h" | |
26 #include "ui/compositor/scoped_layer_animation_settings.h" | |
27 #include "ui/gfx/canvas.h" | |
28 #include "ui/gfx/geometry/rect.h" | |
29 #include "ui/gfx/geometry/vector2d.h" | |
30 #include "ui/views/background.h" | |
31 #include "ui/views/widget/widget.h" | |
32 | |
33 namespace ash { | |
34 namespace { | |
35 | |
36 const int kPanelIdealSpacing = 4; | |
37 | |
38 const float kMaxHeightFactor = .80f; | |
39 const float kMaxWidthFactor = .50f; | |
40 | |
41 // Duration for panel animations. | |
42 const int kPanelSlideDurationMilliseconds = 50; | |
43 const int kCalloutFadeDurationMilliseconds = 50; | |
44 | |
45 // Offset used when sliding panel in/out of the shelf. Used for minimizing, | |
46 // restoring and the initial showing of a panel. | |
47 const int kPanelSlideInOffset = 20; | |
48 | |
49 // Callout arrow dimensions. | |
50 const int kArrowWidth = 18; | |
51 const int kArrowHeight = 9; | |
52 | |
53 class CalloutWidgetBackground : public views::Background { | |
54 public: | |
55 CalloutWidgetBackground() : alignment_(wm::SHELF_ALIGNMENT_BOTTOM) {} | |
56 | |
57 void Paint(gfx::Canvas* canvas, views::View* view) const override { | |
58 SkPath path; | |
59 switch (alignment_) { | |
60 case wm::SHELF_ALIGNMENT_BOTTOM: | |
61 case wm::SHELF_ALIGNMENT_BOTTOM_LOCKED: | |
62 path.moveTo(SkIntToScalar(0), SkIntToScalar(0)); | |
63 path.lineTo(SkIntToScalar(kArrowWidth / 2), | |
64 SkIntToScalar(kArrowHeight)); | |
65 path.lineTo(SkIntToScalar(kArrowWidth), SkIntToScalar(0)); | |
66 break; | |
67 case wm::SHELF_ALIGNMENT_LEFT: | |
68 path.moveTo(SkIntToScalar(kArrowHeight), SkIntToScalar(kArrowWidth)); | |
69 path.lineTo(SkIntToScalar(0), SkIntToScalar(kArrowWidth / 2)); | |
70 path.lineTo(SkIntToScalar(kArrowHeight), SkIntToScalar(0)); | |
71 break; | |
72 case wm::SHELF_ALIGNMENT_RIGHT: | |
73 path.moveTo(SkIntToScalar(0), SkIntToScalar(0)); | |
74 path.lineTo(SkIntToScalar(kArrowHeight), | |
75 SkIntToScalar(kArrowWidth / 2)); | |
76 path.lineTo(SkIntToScalar(0), SkIntToScalar(kArrowWidth)); | |
77 break; | |
78 } | |
79 // Hard code the arrow color for now. | |
80 SkPaint paint; | |
81 paint.setStyle(SkPaint::kFill_Style); | |
82 paint.setColor(SkColorSetARGB(0xff, 0xe5, 0xe5, 0xe5)); | |
83 canvas->DrawPath(path, paint); | |
84 } | |
85 | |
86 wm::ShelfAlignment alignment() { return alignment_; } | |
87 | |
88 void set_alignment(wm::ShelfAlignment alignment) { alignment_ = alignment; } | |
89 | |
90 private: | |
91 wm::ShelfAlignment alignment_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(CalloutWidgetBackground); | |
94 }; | |
95 | |
96 struct VisiblePanelPositionInfo { | |
97 VisiblePanelPositionInfo() | |
98 : min_major(0), | |
99 max_major(0), | |
100 major_pos(0), | |
101 major_length(0), | |
102 window(NULL), | |
103 slide_in(false) {} | |
104 | |
105 int min_major; | |
106 int max_major; | |
107 int major_pos; | |
108 int major_length; | |
109 wm::WmWindow* window; | |
110 bool slide_in; | |
111 }; | |
112 | |
113 bool CompareWindowMajor(const VisiblePanelPositionInfo& win1, | |
114 const VisiblePanelPositionInfo& win2) { | |
115 return win1.major_pos < win2.major_pos; | |
116 } | |
117 | |
118 void FanOutPanels(std::vector<VisiblePanelPositionInfo>::iterator first, | |
119 std::vector<VisiblePanelPositionInfo>::iterator last) { | |
120 int num_panels = last - first; | |
121 if (num_panels == 1) { | |
122 (*first).major_pos = std::max( | |
123 (*first).min_major, std::min((*first).max_major, (*first).major_pos)); | |
124 } | |
125 if (num_panels <= 1) | |
126 return; | |
127 | |
128 if (num_panels == 2) { | |
129 // If there are two adjacent overlapping windows, separate them by the | |
130 // minimum major_length necessary. | |
131 std::vector<VisiblePanelPositionInfo>::iterator second = first + 1; | |
132 int separation = (*first).major_length / 2 + (*second).major_length / 2 + | |
133 kPanelIdealSpacing; | |
134 int overlap = (*first).major_pos + separation - (*second).major_pos; | |
135 (*first).major_pos = | |
136 std::max((*first).min_major, (*first).major_pos - overlap / 2); | |
137 (*second).major_pos = | |
138 std::min((*second).max_major, (*first).major_pos + separation); | |
139 // Recalculate the first panel position in case the second one was | |
140 // constrained on the right. | |
141 (*first).major_pos = | |
142 std::max((*first).min_major, (*second).major_pos - separation); | |
143 return; | |
144 } | |
145 | |
146 // If there are more than two overlapping windows, fan them out from minimum | |
147 // position to maximum position equally spaced. | |
148 int delta = ((*(last - 1)).max_major - (*first).min_major) / (num_panels - 1); | |
149 int major_pos = (*first).min_major; | |
150 for (std::vector<VisiblePanelPositionInfo>::iterator iter = first; | |
151 iter != last; ++iter) { | |
152 (*iter).major_pos = | |
153 std::max((*iter).min_major, std::min((*iter).max_major, major_pos)); | |
154 major_pos += delta; | |
155 } | |
156 } | |
157 | |
158 bool BoundsAdjacent(const gfx::Rect& bounds1, const gfx::Rect& bounds2) { | |
159 return bounds1.x() == bounds2.right() || bounds1.y() == bounds2.bottom() || | |
160 bounds1.right() == bounds2.x() || bounds1.bottom() == bounds2.y(); | |
161 } | |
162 | |
163 gfx::Vector2d GetSlideInAnimationOffset(wm::ShelfAlignment alignment) { | |
164 gfx::Vector2d offset; | |
165 if (alignment == wm::SHELF_ALIGNMENT_LEFT) | |
166 offset.set_x(-kPanelSlideInOffset); | |
167 else if (alignment == wm::SHELF_ALIGNMENT_RIGHT) | |
168 offset.set_x(kPanelSlideInOffset); | |
169 else | |
170 offset.set_y(kPanelSlideInOffset); | |
171 return offset; | |
172 } | |
173 | |
174 } // namespace | |
175 | |
176 class PanelCalloutWidget : public views::Widget { | |
177 public: | |
178 explicit PanelCalloutWidget(wm::WmWindow* container) : background_(nullptr) { | |
179 InitWidget(container); | |
180 } | |
181 | |
182 void SetAlignment(wm::ShelfAlignment alignment) { | |
183 gfx::Rect callout_bounds = GetWindowBoundsInScreen(); | |
184 if (wm::IsHorizontalAlignment(alignment)) { | |
185 callout_bounds.set_width(kArrowWidth); | |
186 callout_bounds.set_height(kArrowHeight); | |
187 } else { | |
188 callout_bounds.set_width(kArrowHeight); | |
189 callout_bounds.set_height(kArrowWidth); | |
190 } | |
191 SetBounds(callout_bounds); | |
192 if (background_->alignment() != alignment) { | |
193 background_->set_alignment(alignment); | |
194 SchedulePaintInRect(gfx::Rect(callout_bounds.size())); | |
195 } | |
196 } | |
197 | |
198 private: | |
199 void InitWidget(wm::WmWindow* parent) { | |
200 views::Widget::InitParams params; | |
201 params.type = views::Widget::InitParams::TYPE_POPUP; | |
202 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
203 params.keep_on_top = true; | |
204 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
205 params.bounds = parent->ConvertRectToScreen(gfx::Rect()); | |
206 params.bounds.set_width(kArrowWidth); | |
207 params.bounds.set_height(kArrowHeight); | |
208 params.accept_events = false; | |
209 parent->GetRootWindowController()->ConfigureWidgetInitParamsForContainer( | |
210 this, parent->GetShellWindowId(), ¶ms); | |
211 set_focus_on_creation(false); | |
212 Init(params); | |
213 wm::WmWindow* widget_window = wm::WmLookup::Get()->GetWindowForWidget(this); | |
214 DCHECK_EQ(widget_window->GetRootWindow(), parent->GetRootWindow()); | |
215 views::View* content_view = new views::View; | |
216 background_ = new CalloutWidgetBackground; | |
217 content_view->set_background(background_); | |
218 SetContentsView(content_view); | |
219 widget_window->GetLayer()->SetOpacity(0); | |
220 } | |
221 | |
222 // Weak pointer owned by this widget's content view. | |
223 CalloutWidgetBackground* background_; | |
224 | |
225 DISALLOW_COPY_AND_ASSIGN(PanelCalloutWidget); | |
226 }; | |
227 | |
228 views::Widget* PanelLayoutManager::PanelInfo::CalloutWidget() { | |
229 return callout_widget; | |
230 } | |
231 | |
232 //////////////////////////////////////////////////////////////////////////////// | |
233 // PanelLayoutManager public implementation: | |
234 PanelLayoutManager::PanelLayoutManager(wm::WmWindow* panel_container) | |
235 : panel_container_(panel_container), | |
236 root_window_controller_(panel_container->GetRootWindowController()), | |
237 in_add_window_(false), | |
238 in_layout_(false), | |
239 show_callout_widgets_(true), | |
240 dragged_panel_(NULL), | |
241 shelf_(nullptr), | |
242 last_active_panel_(NULL), | |
243 weak_factory_(this) { | |
244 DCHECK(panel_container); | |
245 wm::WmGlobals* globals = panel_container->GetGlobals(); | |
246 globals->AddActivationObserver(this); | |
247 globals->AddDisplayObserver(this); | |
248 globals->AddOverviewModeObserver(this); | |
249 root_window_controller_->AddObserver(this); | |
250 } | |
251 | |
252 PanelLayoutManager::~PanelLayoutManager() { | |
253 Shutdown(); | |
254 } | |
255 | |
256 // static | |
257 PanelLayoutManager* PanelLayoutManager::Get(wm::WmWindow* window) { | |
258 if (!window) | |
259 return nullptr; | |
260 | |
261 return static_cast<PanelLayoutManager*>( | |
262 window->GetRootWindow() | |
263 ->GetChildByShellWindowId(kShellWindowId_PanelContainer) | |
264 ->GetLayoutManager()); | |
265 } | |
266 | |
267 void PanelLayoutManager::Shutdown() { | |
268 if (shelf_) { | |
269 shelf_->RemoveObserver(this); | |
270 shelf_ = nullptr; | |
271 } | |
272 for (PanelList::iterator iter = panel_windows_.begin(); | |
273 iter != panel_windows_.end(); ++iter) { | |
274 delete iter->callout_widget; | |
275 } | |
276 panel_windows_.clear(); | |
277 wm::WmGlobals* globals = panel_container_->GetGlobals(); | |
278 globals->RemoveActivationObserver(this); | |
279 globals->RemoveDisplayObserver(this); | |
280 globals->RemoveOverviewModeObserver(this); | |
281 root_window_controller_->RemoveObserver(this); | |
282 } | |
283 | |
284 void PanelLayoutManager::StartDragging(wm::WmWindow* panel) { | |
285 DCHECK(!dragged_panel_); | |
286 dragged_panel_ = panel; | |
287 Relayout(); | |
288 } | |
289 | |
290 void PanelLayoutManager::FinishDragging() { | |
291 dragged_panel_ = NULL; | |
292 Relayout(); | |
293 } | |
294 | |
295 void PanelLayoutManager::SetShelf(wm::WmShelf* shelf) { | |
296 DCHECK(!shelf_); | |
297 shelf_ = shelf; | |
298 shelf_->AddObserver(this); | |
299 WillChangeVisibilityState(shelf_->GetVisibilityState()); | |
300 } | |
301 | |
302 void PanelLayoutManager::ToggleMinimize(wm::WmWindow* panel) { | |
303 DCHECK(panel->GetParent() == panel_container_); | |
304 wm::WindowState* window_state = panel->GetWindowState(); | |
305 if (window_state->IsMinimized()) | |
306 window_state->Restore(); | |
307 else | |
308 window_state->Minimize(); | |
309 } | |
310 | |
311 void PanelLayoutManager::SetShowCalloutWidgets(bool show) { | |
312 if (show_callout_widgets_ == show) | |
313 return; | |
314 show_callout_widgets_ = show; | |
315 UpdateCallouts(); | |
316 } | |
317 | |
318 views::Widget* PanelLayoutManager::GetCalloutWidgetForPanel( | |
319 wm::WmWindow* panel) { | |
320 DCHECK(panel->GetParent() == panel_container_); | |
321 PanelList::iterator found = | |
322 std::find(panel_windows_.begin(), panel_windows_.end(), panel); | |
323 DCHECK(found != panel_windows_.end()); | |
324 return found->callout_widget; | |
325 } | |
326 | |
327 //////////////////////////////////////////////////////////////////////////////// | |
328 // PanelLayoutManager, wm::WmLayoutManager implementation: | |
329 void PanelLayoutManager::OnWindowResized() { | |
330 Relayout(); | |
331 } | |
332 | |
333 void PanelLayoutManager::OnWindowAddedToLayout(wm::WmWindow* child) { | |
334 if (child->GetType() == ui::wm::WINDOW_TYPE_POPUP) | |
335 return; | |
336 if (in_add_window_) | |
337 return; | |
338 base::AutoReset<bool> auto_reset_in_add_window(&in_add_window_, true); | |
339 if (!child->GetWindowState()->panel_attached()) { | |
340 // This should only happen when a window is added to panel container as a | |
341 // result of bounds change from within the application during a drag. | |
342 // If so we have already stopped the drag and should reparent the panel | |
343 // back to appropriate container and ignore it. | |
344 // TODO(varkha): Updating bounds during a drag can cause problems and a more | |
345 // general solution is needed. See http://crbug.com/251813 . | |
346 wm::WmWindow* old_parent = child->GetParent(); | |
347 child->SetParentUsingContext(child, | |
348 child->GetRootWindow()->GetBoundsInScreen()); | |
349 wm::ReparentTransientChildrenOfChild(child, old_parent, child->GetParent()); | |
350 DCHECK(child->GetParent()->GetShellWindowId() != | |
351 kShellWindowId_PanelContainer); | |
352 return; | |
353 } | |
354 PanelInfo panel_info; | |
355 panel_info.window = child; | |
356 panel_info.callout_widget = new PanelCalloutWidget(panel_container_); | |
357 panel_info.slide_in = child != dragged_panel_; | |
358 panel_windows_.push_back(panel_info); | |
359 child->AddObserver(this); | |
360 child->GetWindowState()->AddObserver(this); | |
361 Relayout(); | |
362 } | |
363 | |
364 void PanelLayoutManager::OnWillRemoveWindowFromLayout(wm::WmWindow* child) {} | |
365 | |
366 void PanelLayoutManager::OnWindowRemovedFromLayout(wm::WmWindow* child) { | |
367 if (child->GetType() == ui::wm::WINDOW_TYPE_POPUP) | |
368 return; | |
369 PanelList::iterator found = | |
370 std::find(panel_windows_.begin(), panel_windows_.end(), child); | |
371 if (found != panel_windows_.end()) { | |
372 delete found->callout_widget; | |
373 panel_windows_.erase(found); | |
374 } | |
375 if (restore_windows_on_shelf_visible_) | |
376 restore_windows_on_shelf_visible_->Remove(child); | |
377 child->RemoveObserver(this); | |
378 child->GetWindowState()->RemoveObserver(this); | |
379 | |
380 if (dragged_panel_ == child) | |
381 dragged_panel_ = NULL; | |
382 | |
383 if (last_active_panel_ == child) | |
384 last_active_panel_ = NULL; | |
385 | |
386 Relayout(); | |
387 } | |
388 | |
389 void PanelLayoutManager::OnChildWindowVisibilityChanged(wm::WmWindow* child, | |
390 bool visible) { | |
391 if (visible) | |
392 child->GetWindowState()->Restore(); | |
393 Relayout(); | |
394 } | |
395 | |
396 void PanelLayoutManager::SetChildBounds(wm::WmWindow* child, | |
397 const gfx::Rect& requested_bounds) { | |
398 gfx::Rect bounds(requested_bounds); | |
399 const gfx::Rect& max_bounds = panel_container_->GetRootWindow()->GetBounds(); | |
400 const int max_width = max_bounds.width() * kMaxWidthFactor; | |
401 const int max_height = max_bounds.height() * kMaxHeightFactor; | |
402 if (bounds.width() > max_width) | |
403 bounds.set_width(max_width); | |
404 if (bounds.height() > max_height) | |
405 bounds.set_height(max_height); | |
406 | |
407 // Reposition dragged panel in the panel order. | |
408 if (dragged_panel_ == child) { | |
409 PanelList::iterator dragged_panel_iter = | |
410 std::find(panel_windows_.begin(), panel_windows_.end(), dragged_panel_); | |
411 DCHECK(dragged_panel_iter != panel_windows_.end()); | |
412 PanelList::iterator new_position; | |
413 for (new_position = panel_windows_.begin(); | |
414 new_position != panel_windows_.end(); ++new_position) { | |
415 const gfx::Rect& bounds = (*new_position).window->GetBounds(); | |
416 if (bounds.x() + bounds.width() / 2 <= requested_bounds.x()) | |
417 break; | |
418 } | |
419 if (new_position != dragged_panel_iter) { | |
420 PanelInfo dragged_panel_info = *dragged_panel_iter; | |
421 panel_windows_.erase(dragged_panel_iter); | |
422 panel_windows_.insert(new_position, dragged_panel_info); | |
423 } | |
424 } | |
425 // Respect the minimum size of the window. | |
426 if (child->HasNonClientArea()) { | |
427 const gfx::Size min_size = child->GetMinimumSize(); | |
428 bounds.set_width(std::max(min_size.width(), bounds.width())); | |
429 bounds.set_height(std::max(min_size.height(), bounds.height())); | |
430 } | |
431 | |
432 child->SetBoundsDirect(bounds); | |
433 Relayout(); | |
434 } | |
435 | |
436 //////////////////////////////////////////////////////////////////////////////// | |
437 // PanelLayoutManager, wm::WmShellObserver implementation: | |
438 | |
439 void PanelLayoutManager::OnOverviewModeEnded() { | |
440 Relayout(); | |
441 } | |
442 | |
443 void PanelLayoutManager::OnShelfAlignmentChanged() { | |
444 Relayout(); | |
445 } | |
446 | |
447 ///////////////////////////////////////////////////////////////////////////// | |
448 // PanelLayoutManager, WindowObserver implementation: | |
449 | |
450 void PanelLayoutManager::OnWindowPropertyChanged( | |
451 wm::WmWindow* window, | |
452 wm::WmWindowProperty property) { | |
453 // Trigger a relayout to position the panels whenever the panel icon is set | |
454 // or changes. | |
455 if (property == wm::WmWindowProperty::SHELF_ID) | |
456 Relayout(); | |
457 } | |
458 | |
459 ///////////////////////////////////////////////////////////////////////////// | |
460 // PanelLayoutManager, WindowStateObserver implementation: | |
461 | |
462 void PanelLayoutManager::OnPostWindowStateTypeChange( | |
463 wm::WindowState* window_state, | |
464 wm::WindowStateType old_type) { | |
465 // If the shelf is currently hidden then windows will not actually be shown | |
466 // but the set to restore when the shelf becomes visible is updated. | |
467 if (restore_windows_on_shelf_visible_) { | |
468 if (window_state->IsMinimized()) { | |
469 MinimizePanel(window_state->window()); | |
470 restore_windows_on_shelf_visible_->Remove(window_state->window()); | |
471 } else { | |
472 restore_windows_on_shelf_visible_->Add(window_state->window()); | |
473 } | |
474 return; | |
475 } | |
476 | |
477 if (window_state->IsMinimized()) | |
478 MinimizePanel(window_state->window()); | |
479 else | |
480 RestorePanel(window_state->window()); | |
481 } | |
482 | |
483 //////////////////////////////////////////////////////////////////////////////// | |
484 // PanelLayoutManager, wm::WmActivationObserver implementation: | |
485 | |
486 void PanelLayoutManager::OnWindowActivated(wm::WmWindow* gained_active, | |
487 wm::WmWindow* lost_active) { | |
488 // Ignore if the panel that is not managed by this was activated. | |
489 if (gained_active && gained_active->GetType() == ui::wm::WINDOW_TYPE_PANEL && | |
490 gained_active->GetParent() == panel_container_) { | |
491 UpdateStacking(gained_active); | |
492 UpdateCallouts(); | |
493 } | |
494 } | |
495 | |
496 //////////////////////////////////////////////////////////////////////////////// | |
497 // PanelLayoutManager, wm::WmDisplayObserver::Observer implementation: | |
498 | |
499 void PanelLayoutManager::OnDisplayConfigurationChanged() { | |
500 Relayout(); | |
501 } | |
502 | |
503 //////////////////////////////////////////////////////////////////////////////// | |
504 // PanelLayoutManager, ShelfLayoutManagerObserver implementation: | |
505 | |
506 void PanelLayoutManager::WillChangeVisibilityState( | |
507 ShelfVisibilityState new_state) { | |
508 // On entering / leaving full screen mode the shelf visibility state is | |
509 // changed to / from SHELF_HIDDEN. In this state, panel windows should hide | |
510 // to allow the full-screen application to use the full screen. | |
511 bool shelf_hidden = new_state == ash::SHELF_HIDDEN; | |
512 if (!shelf_hidden) { | |
513 if (restore_windows_on_shelf_visible_) { | |
514 std::unique_ptr<wm::WmWindowTracker> restore_windows( | |
515 std::move(restore_windows_on_shelf_visible_)); | |
516 for (wm::WmWindow* window : restore_windows->windows()) | |
517 RestorePanel(window); | |
518 } | |
519 return; | |
520 } | |
521 | |
522 if (restore_windows_on_shelf_visible_) | |
523 return; | |
524 std::unique_ptr<wm::WmWindowTracker> minimized_windows( | |
525 new wm::WmWindowTracker); | |
526 for (PanelList::iterator iter = panel_windows_.begin(); | |
527 iter != panel_windows_.end();) { | |
528 wm::WmWindow* window = iter->window; | |
529 // Minimizing a panel window may remove it from the panel_windows_ list. | |
530 // Advance the iterator before minimizing it: http://crbug.com/393047. | |
531 ++iter; | |
532 if (window != dragged_panel_ && window->IsVisible()) { | |
533 minimized_windows->Add(window); | |
534 window->GetWindowState()->Minimize(); | |
535 } | |
536 } | |
537 restore_windows_on_shelf_visible_ = std::move(minimized_windows); | |
538 } | |
539 | |
540 void PanelLayoutManager::OnShelfIconPositionsChanged() { | |
541 // TODO: As this is called for every animation step now. Relayout needs to be | |
542 // updated to use current icon position instead of use the ideal bounds so | |
543 // that the panels slide with their icons instead of jumping. | |
544 Relayout(); | |
545 } | |
546 | |
547 //////////////////////////////////////////////////////////////////////////////// | |
548 // PanelLayoutManager private implementation: | |
549 | |
550 void PanelLayoutManager::MinimizePanel(wm::WmWindow* panel) { | |
551 panel->SetVisibilityAnimationType( | |
552 wm::WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE); | |
553 ui::Layer* layer = panel->GetLayer(); | |
554 ui::ScopedLayerAnimationSettings panel_slide_settings(layer->GetAnimator()); | |
555 panel_slide_settings.SetPreemptionStrategy( | |
556 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
557 panel_slide_settings.SetTransitionDuration( | |
558 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds)); | |
559 gfx::Rect bounds(panel->GetBounds()); | |
560 bounds.Offset(GetSlideInAnimationOffset(shelf_->GetAlignment())); | |
561 panel->SetBoundsDirect(bounds); | |
562 panel->Hide(); | |
563 layer->SetOpacity(0); | |
564 if (panel->IsActive()) | |
565 panel->Deactivate(); | |
566 Relayout(); | |
567 } | |
568 | |
569 void PanelLayoutManager::RestorePanel(wm::WmWindow* panel) { | |
570 PanelList::iterator found = | |
571 std::find(panel_windows_.begin(), panel_windows_.end(), panel); | |
572 DCHECK(found != panel_windows_.end()); | |
573 found->slide_in = true; | |
574 Relayout(); | |
575 } | |
576 | |
577 void PanelLayoutManager::Relayout() { | |
578 if (!shelf_) | |
579 return; | |
580 | |
581 // Suppress layouts during overview mode because changing window bounds | |
582 // interfered with overview mode animations. However, layouts need to be done | |
583 // when the WindowSelectorController is restoring minimized windows so that | |
584 // they actually become visible. | |
585 wm::WmGlobals* globals = panel_container_->GetGlobals(); | |
586 if (in_layout_ || (globals->IsOverviewModeSelecting() && | |
587 !globals->IsOverviewModeRestoringMinimizedWindows())) { | |
588 return; | |
589 } | |
590 | |
591 base::AutoReset<bool> auto_reset_in_layout(&in_layout_, true); | |
592 | |
593 const wm::ShelfAlignment alignment = shelf_->GetAlignment(); | |
594 const bool horizontal = wm::IsHorizontalAlignment(shelf_->GetAlignment()); | |
595 gfx::Rect shelf_bounds = panel_container_->ConvertRectFromScreen( | |
596 shelf_->GetWindow()->GetBoundsInScreen()); | |
597 int panel_start_bounds = kPanelIdealSpacing; | |
598 int panel_end_bounds = | |
599 horizontal ? panel_container_->GetBounds().width() - kPanelIdealSpacing | |
600 : panel_container_->GetBounds().height() - kPanelIdealSpacing; | |
601 wm::WmWindow* active_panel = nullptr; | |
602 std::vector<VisiblePanelPositionInfo> visible_panels; | |
603 for (PanelList::iterator iter = panel_windows_.begin(); | |
604 iter != panel_windows_.end(); ++iter) { | |
605 wm::WmWindow* panel = iter->window; | |
606 iter->callout_widget->SetAlignment(alignment); | |
607 | |
608 // Consider the dragged panel as part of the layout as long as it is | |
609 // touching the shelf. | |
610 if ((!panel->IsVisible() && !iter->slide_in) || | |
611 (panel == dragged_panel_ && | |
612 !BoundsAdjacent(panel->GetBounds(), shelf_bounds))) { | |
613 continue; | |
614 } | |
615 | |
616 // If the shelf is currently hidden (full-screen mode), minimize panel until | |
617 // full-screen mode is exited. When a panel is dragged from another display | |
618 // the shelf state does not update before the panel is added so we exclude | |
619 // the dragged panel. | |
620 if (panel != dragged_panel_ && restore_windows_on_shelf_visible_) { | |
621 panel->GetWindowState()->Minimize(); | |
622 restore_windows_on_shelf_visible_->Add(panel); | |
623 continue; | |
624 } | |
625 | |
626 gfx::Rect icon_bounds = shelf_->GetScreenBoundsOfItemIconForWindow(panel); | |
627 | |
628 // If both the icon width and height are 0 then there is no icon in the | |
629 // shelf. If the shelf is hidden, one of the height or width will be | |
630 // 0 but the position in the shelf and major dimension is still reported | |
631 // correctly and the panel can be aligned above where the hidden icon is. | |
632 if (icon_bounds.width() == 0 && icon_bounds.height() == 0) | |
633 continue; | |
634 | |
635 if (panel->IsFocused() || | |
636 panel->Contains(panel->GetGlobals()->GetFocusedWindow())) { | |
637 DCHECK(!active_panel); | |
638 active_panel = panel; | |
639 } | |
640 icon_bounds = panel_container_->ConvertRectFromScreen(icon_bounds); | |
641 gfx::Point icon_origin = icon_bounds.origin(); | |
642 VisiblePanelPositionInfo position_info; | |
643 int icon_start = horizontal ? icon_origin.x() : icon_origin.y(); | |
644 int icon_end = | |
645 icon_start + (horizontal ? icon_bounds.width() : icon_bounds.height()); | |
646 position_info.major_length = | |
647 horizontal ? panel->GetBounds().width() : panel->GetBounds().height(); | |
648 position_info.min_major = | |
649 std::max(panel_start_bounds + position_info.major_length / 2, | |
650 icon_end - position_info.major_length / 2); | |
651 position_info.max_major = | |
652 std::min(icon_start + position_info.major_length / 2, | |
653 panel_end_bounds - position_info.major_length / 2); | |
654 position_info.major_pos = (icon_start + icon_end) / 2; | |
655 position_info.window = panel; | |
656 position_info.slide_in = iter->slide_in; | |
657 iter->slide_in = false; | |
658 visible_panels.push_back(position_info); | |
659 } | |
660 | |
661 // Sort panels by their X positions and fan out groups of overlapping panels. | |
662 // The fan out method may result in new overlapping panels however given that | |
663 // the panels start at least a full panel width apart this overlap will | |
664 // never completely obscure a panel. | |
665 // TODO(flackr): Rearrange panels if new overlaps are introduced. | |
666 std::sort(visible_panels.begin(), visible_panels.end(), CompareWindowMajor); | |
667 size_t first_overlapping_panel = 0; | |
668 for (size_t i = 1; i < visible_panels.size(); ++i) { | |
669 if (visible_panels[i - 1].major_pos + | |
670 visible_panels[i - 1].major_length / 2 < | |
671 visible_panels[i].major_pos - visible_panels[i].major_length / 2) { | |
672 FanOutPanels(visible_panels.begin() + first_overlapping_panel, | |
673 visible_panels.begin() + i); | |
674 first_overlapping_panel = i; | |
675 } | |
676 } | |
677 FanOutPanels(visible_panels.begin() + first_overlapping_panel, | |
678 visible_panels.end()); | |
679 | |
680 for (size_t i = 0; i < visible_panels.size(); ++i) { | |
681 if (visible_panels[i].window == dragged_panel_) | |
682 continue; | |
683 bool slide_in = visible_panels[i].slide_in; | |
684 gfx::Rect bounds = visible_panels[i].window->GetTargetBounds(); | |
685 if (alignment == wm::SHELF_ALIGNMENT_LEFT) | |
686 bounds.set_x(shelf_bounds.right()); | |
687 else if (alignment == wm::SHELF_ALIGNMENT_RIGHT) | |
688 bounds.set_x(shelf_bounds.x() - bounds.width()); | |
689 else | |
690 bounds.set_y(shelf_bounds.y() - bounds.height()); | |
691 bool on_shelf = visible_panels[i].window->GetTargetBounds() == bounds; | |
692 | |
693 if (horizontal) { | |
694 bounds.set_x(visible_panels[i].major_pos - | |
695 visible_panels[i].major_length / 2); | |
696 } else { | |
697 bounds.set_y(visible_panels[i].major_pos - | |
698 visible_panels[i].major_length / 2); | |
699 } | |
700 | |
701 ui::Layer* layer = visible_panels[i].window->GetLayer(); | |
702 if (slide_in) { | |
703 // New windows shift up from the shelf into position and fade in. | |
704 layer->SetOpacity(0); | |
705 gfx::Rect initial_bounds(bounds); | |
706 initial_bounds.Offset(GetSlideInAnimationOffset(alignment)); | |
707 visible_panels[i].window->SetBoundsDirect(initial_bounds); | |
708 // Set on shelf so that the panel animates into its target position. | |
709 on_shelf = true; | |
710 } | |
711 | |
712 if (on_shelf) { | |
713 ui::ScopedLayerAnimationSettings panel_slide_settings( | |
714 layer->GetAnimator()); | |
715 panel_slide_settings.SetPreemptionStrategy( | |
716 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
717 panel_slide_settings.SetTransitionDuration( | |
718 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds)); | |
719 visible_panels[i].window->SetBoundsDirect(bounds); | |
720 if (slide_in) { | |
721 layer->SetOpacity(1); | |
722 visible_panels[i].window->Show(); | |
723 } | |
724 } else { | |
725 // If the shelf moved don't animate, move immediately to the new | |
726 // target location. | |
727 visible_panels[i].window->SetBoundsDirect(bounds); | |
728 } | |
729 } | |
730 | |
731 UpdateStacking(active_panel); | |
732 UpdateCallouts(); | |
733 } | |
734 | |
735 void PanelLayoutManager::UpdateStacking(wm::WmWindow* active_panel) { | |
736 if (!active_panel) { | |
737 if (!last_active_panel_) | |
738 return; | |
739 active_panel = last_active_panel_; | |
740 } | |
741 | |
742 // We want to to stack the panels like a deck of cards: | |
743 // ,--,--,--,-------.--.--. | |
744 // | | | | | | | | |
745 // | | | | | | | | |
746 // | |
747 // We use the middle of each panel to figure out how to stack the panels. This | |
748 // allows us to update the stacking when a panel is being dragged around by | |
749 // the titlebar--even though it doesn't update the shelf icon positions, we | |
750 // still want the visual effect. | |
751 std::map<int, wm::WmWindow*> window_ordering; | |
752 const bool horizontal = wm::IsHorizontalAlignment(shelf_->GetAlignment()); | |
753 for (PanelList::const_iterator it = panel_windows_.begin(); | |
754 it != panel_windows_.end(); ++it) { | |
755 gfx::Rect bounds = it->window->GetBounds(); | |
756 window_ordering.insert( | |
757 std::make_pair(horizontal ? bounds.x() + bounds.width() / 2 | |
758 : bounds.y() + bounds.height() / 2, | |
759 it->window)); | |
760 } | |
761 | |
762 wm::WmWindow* previous_panel = nullptr; | |
763 for (std::map<int, wm::WmWindow*>::const_iterator it = | |
764 window_ordering.begin(); | |
765 it != window_ordering.end() && it->second != active_panel; ++it) { | |
766 if (previous_panel) | |
767 panel_container_->StackChildAbove(it->second, previous_panel); | |
768 previous_panel = it->second; | |
769 } | |
770 | |
771 previous_panel = NULL; | |
772 for (std::map<int, wm::WmWindow*>::const_reverse_iterator it = | |
773 window_ordering.rbegin(); | |
774 it != window_ordering.rend() && it->second != active_panel; ++it) { | |
775 if (previous_panel) | |
776 panel_container_->StackChildAbove(it->second, previous_panel); | |
777 previous_panel = it->second; | |
778 } | |
779 | |
780 panel_container_->StackChildAtTop(active_panel); | |
781 if (dragged_panel_ && dragged_panel_->GetParent() == panel_container_) | |
782 panel_container_->StackChildAtTop(dragged_panel_); | |
783 last_active_panel_ = active_panel; | |
784 } | |
785 | |
786 void PanelLayoutManager::UpdateCallouts() { | |
787 const bool horizontal = wm::IsHorizontalAlignment(shelf_->GetAlignment()); | |
788 for (PanelList::iterator iter = panel_windows_.begin(); | |
789 iter != panel_windows_.end(); ++iter) { | |
790 wm::WmWindow* panel = iter->window; | |
791 views::Widget* callout_widget = iter->callout_widget; | |
792 wm::WmWindow* callout_widget_window = | |
793 wm::WmLookup::Get()->GetWindowForWidget(callout_widget); | |
794 | |
795 gfx::Rect current_bounds = panel->GetBoundsInScreen(); | |
796 gfx::Rect bounds = | |
797 panel->GetParent()->ConvertRectToScreen(panel->GetTargetBounds()); | |
798 gfx::Rect icon_bounds = shelf_->GetScreenBoundsOfItemIconForWindow(panel); | |
799 if (icon_bounds.IsEmpty() || !panel->GetLayer()->GetTargetVisibility() || | |
800 panel == dragged_panel_ || !show_callout_widgets_) { | |
801 callout_widget->Hide(); | |
802 callout_widget_window->GetLayer()->SetOpacity(0); | |
803 continue; | |
804 } | |
805 | |
806 gfx::Rect callout_bounds = callout_widget->GetWindowBoundsInScreen(); | |
807 gfx::Vector2d slide_vector = bounds.origin() - current_bounds.origin(); | |
808 int slide_distance = horizontal ? slide_vector.x() : slide_vector.y(); | |
809 int distance_until_over_panel = 0; | |
810 if (horizontal) { | |
811 callout_bounds.set_x(icon_bounds.x() + | |
812 (icon_bounds.width() - callout_bounds.width()) / 2); | |
813 distance_until_over_panel = | |
814 std::max(current_bounds.x() - callout_bounds.x(), | |
815 callout_bounds.right() - current_bounds.right()); | |
816 } else { | |
817 callout_bounds.set_y(icon_bounds.y() + | |
818 (icon_bounds.height() - callout_bounds.height()) / | |
819 2); | |
820 distance_until_over_panel = | |
821 std::max(current_bounds.y() - callout_bounds.y(), | |
822 callout_bounds.bottom() - current_bounds.bottom()); | |
823 } | |
824 if (shelf_->GetAlignment() == wm::SHELF_ALIGNMENT_LEFT) | |
825 callout_bounds.set_x(bounds.x() - callout_bounds.width()); | |
826 else if (shelf_->GetAlignment() == wm::SHELF_ALIGNMENT_RIGHT) | |
827 callout_bounds.set_x(bounds.right()); | |
828 else | |
829 callout_bounds.set_y(bounds.bottom()); | |
830 callout_bounds = callout_widget_window->GetParent()->ConvertRectFromScreen( | |
831 callout_bounds); | |
832 | |
833 callout_widget_window->SetBoundsDirect(callout_bounds); | |
834 panel_container_->StackChildAbove(callout_widget_window, panel); | |
835 | |
836 ui::Layer* layer = callout_widget_window->GetLayer(); | |
837 // If the panel is not over the callout position or has just become visible | |
838 // then fade in the callout. | |
839 if ((distance_until_over_panel > 0 || layer->GetTargetOpacity() < 1)) { | |
840 if (distance_until_over_panel > 0 && | |
841 slide_distance >= distance_until_over_panel) { | |
842 // If the panel is not yet over the callout, then delay fading in | |
843 // the callout until after the panel should be over it. | |
844 int delay = kPanelSlideDurationMilliseconds * | |
845 distance_until_over_panel / slide_distance; | |
846 layer->SetOpacity(0); | |
847 layer->GetAnimator()->StopAnimating(); | |
848 layer->GetAnimator()->SchedulePauseForProperties( | |
849 base::TimeDelta::FromMilliseconds(delay), | |
850 ui::LayerAnimationElement::OPACITY); | |
851 } | |
852 ui::ScopedLayerAnimationSettings callout_settings(layer->GetAnimator()); | |
853 callout_settings.SetPreemptionStrategy( | |
854 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | |
855 callout_settings.SetTransitionDuration( | |
856 base::TimeDelta::FromMilliseconds(kCalloutFadeDurationMilliseconds)); | |
857 layer->SetOpacity(1); | |
858 } | |
859 | |
860 // Show after changing the opacity animation. This way we don't have a | |
861 // state where the widget is visible but the opacity is 0. | |
862 callout_widget->Show(); | |
863 } | |
864 } | |
865 | |
866 //////////////////////////////////////////////////////////////////////////////// | |
867 // keyboard::KeyboardControllerObserver implementation: | |
868 | |
869 void PanelLayoutManager::OnKeyboardBoundsChanging( | |
870 const gfx::Rect& keyboard_bounds) { | |
871 gfx::Rect parent_bounds = panel_container_->GetBounds(); | |
872 int available_space = parent_bounds.height() - keyboard_bounds.height(); | |
873 for (PanelList::iterator iter = panel_windows_.begin(); | |
874 iter != panel_windows_.end(); ++iter) { | |
875 wm::WmWindow* panel = iter->window; | |
876 wm::WindowState* panel_state = panel->GetWindowState(); | |
877 if (keyboard_bounds.height() > 0) { | |
878 // Save existing bounds, so that we can restore them when the keyboard | |
879 // hides. | |
880 panel_state->SaveCurrentBoundsForRestore(); | |
881 | |
882 gfx::Rect panel_bounds = | |
883 panel->GetParent()->ConvertRectToScreen(panel->GetTargetBounds()); | |
884 int delta = panel_bounds.height() - available_space; | |
885 // Ensure panels are not pushed above the parent boundaries, shrink any | |
886 // panels that violate this constraint. | |
887 if (delta > 0) { | |
888 panel->SetBoundsDirect( | |
889 gfx::Rect(panel_bounds.x(), panel_bounds.y() + delta, | |
890 panel_bounds.width(), panel_bounds.height() - delta)); | |
891 } | |
892 } else if (panel_state->HasRestoreBounds()) { | |
893 // Keyboard hidden, restore original bounds if they exist. | |
894 panel->SetBoundsDirect(panel_state->GetRestoreBoundsInScreen()); | |
895 } | |
896 } | |
897 // This bounds change will have caused a change to the Shelf which does not | |
898 // propogate automatically to this class, so manually recalculate bounds. | |
899 OnWindowResized(); | |
900 } | |
901 | |
902 } // namespace ash | |
OLD | NEW |