Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: ash/common/wm/panels/panel_layout_manager.cc

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

Powered by Google App Engine
This is Rietveld 408576698