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

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

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

Powered by Google App Engine
This is Rietveld 408576698