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/workspace/workspace_window_resizer.h" 6 7 #include <algorithm> 8 #include <cmath> 9 #include <utility> 10 #include <vector> 11 12 #include "ash/display/display_controller.h" 13 #include "ash/metrics/user_metrics_recorder.h" 14 #include "ash/root_window_controller.h" 15 #include "ash/screen_util.h" 16 #include "ash/shell.h" 17 #include "ash/shell_window_ids.h" 18 #include "ash/wm/coordinate_conversion.h" 19 #include "ash/wm/default_window_resizer.h" 20 #include "ash/wm/dock/docked_window_layout_manager.h" 21 #include "ash/wm/dock/docked_window_resizer.h" 22 #include "ash/wm/drag_window_resizer.h" 23 #include "ash/wm/panels/panel_window_resizer.h" 24 #include "ash/wm/window_state.h" 25 #include "ash/wm/window_util.h" 26 #include "ash/wm/wm_event.h" 27 #include "ash/wm/workspace/phantom_window_controller.h" 28 #include "ash/wm/workspace/two_step_edge_cycler.h" 29 #include "base/command_line.h" 30 #include "base/memory/weak_ptr.h" 31 #include "ui/aura/client/aura_constants.h" 32 #include "ui/aura/client/screen_position_client.h" 33 #include "ui/aura/window.h" 34 #include "ui/aura/window_delegate.h" 35 #include "ui/aura/window_event_dispatcher.h" 36 #include "ui/base/hit_test.h" 37 #include "ui/compositor/layer.h" 38 #include "ui/gfx/screen.h" 39 #include "ui/gfx/transform.h" 40 #include "ui/wm/core/window_util.h" 41 #include "ui/wm/public/window_types.h" 42 43 namespace ash { 44 45 scoped_ptr<WindowResizer> CreateWindowResizer( 46 aura::Window* window, 47 const gfx::Point& point_in_parent, 48 int window_component, 49 aura::client::WindowMoveSource source) { 50 DCHECK(window); 51 wm::WindowState* window_state = wm::GetWindowState(window); 52 // No need to return a resizer when the window cannot get resized or when a 53 // resizer already exists for this window. 54 if ((!window_state->CanResize() && window_component != HTCAPTION) || 55 window_state->drag_details()) { 56 return scoped_ptr<WindowResizer>(); 57 } 58 59 if (window_component == HTCAPTION && !window_state->can_be_dragged()) 60 return scoped_ptr<WindowResizer>(); 61 62 // TODO(varkha): The chaining of window resizers causes some of the logic 63 // to be repeated and the logic flow difficult to control. With some windows 64 // classes using reparenting during drag operations it becomes challenging to 65 // implement proper transition from one resizer to another during or at the 66 // end of the drag. This also causes http://crbug.com/247085. 67 // It seems the only thing the panel or dock resizer needs to do is notify the 68 // layout manager when a docked window is being dragged. We should have a 69 // better way of doing this, perhaps by having a way of observing drags or 70 // having a generic drag window wrapper which informs a layout manager that a 71 // drag has started or stopped. 72 // It may be possible to refactor and eliminate chaining. 73 WindowResizer* window_resizer = NULL; 74 75 if (!window_state->IsNormalOrSnapped()) 76 return scoped_ptr<WindowResizer>(); 77 78 int bounds_change = WindowResizer::GetBoundsChangeForWindowComponent( 79 window_component); 80 if (bounds_change == WindowResizer::kBoundsChangeDirection_None) 81 return scoped_ptr<WindowResizer>(); 82 83 window_state->CreateDragDetails(window, point_in_parent, window_component, 84 source); 85 if (window->parent() && 86 (window->parent()->id() == kShellWindowId_DefaultContainer || 87 window->parent()->id() == kShellWindowId_DockedContainer || 88 window->parent()->id() == kShellWindowId_PanelContainer)) { 89 window_resizer = WorkspaceWindowResizer::Create( 90 window_state, std::vector<aura::Window*>()); 91 } else { 92 window_resizer = DefaultWindowResizer::Create(window_state); 93 } 94 window_resizer = DragWindowResizer::Create(window_resizer, window_state); 95 if (window->type() == ui::wm::WINDOW_TYPE_PANEL) 96 window_resizer = PanelWindowResizer::Create(window_resizer, window_state); 97 if (window_resizer && window->parent() && 98 !::wm::GetTransientParent(window) && 99 (window->parent()->id() == kShellWindowId_DefaultContainer || 100 window->parent()->id() == kShellWindowId_DockedContainer || 101 window->parent()->id() == kShellWindowId_PanelContainer)) { 102 window_resizer = DockedWindowResizer::Create(window_resizer, window_state); 103 } 104 return make_scoped_ptr<WindowResizer>(window_resizer); 105 } 106 107 namespace { 108 109 // Snapping distance used instead of WorkspaceWindowResizer::kScreenEdgeInset 110 // when resizing a window using touchscreen. 111 const int kScreenEdgeInsetForTouchDrag = 32; 112 113 // Returns true if the window should stick to the edge. 114 bool ShouldStickToEdge(int distance_from_edge, int sticky_size) { 115 return distance_from_edge < sticky_size && 116 distance_from_edge > -sticky_size * 2; 117 } 118 119 // Returns the coordinate along the secondary axis to snap to. 120 int CoordinateAlongSecondaryAxis(SecondaryMagnetismEdge edge, 121 int leading, 122 int trailing, 123 int none) { 124 switch (edge) { 125 case SECONDARY_MAGNETISM_EDGE_LEADING: 126 return leading; 127 case SECONDARY_MAGNETISM_EDGE_TRAILING: 128 return trailing; 129 case SECONDARY_MAGNETISM_EDGE_NONE: 130 return none; 131 } 132 NOTREACHED(); 133 return none; 134 } 135 136 // Returns the origin for |src| when magnetically attaching to |attach_to| along 137 // the edges |edges|. |edges| is a bitmask of the MagnetismEdges. 138 gfx::Point OriginForMagneticAttach(const gfx::Rect& src, 139 const gfx::Rect& attach_to, 140 const MatchedEdge& edge) { 141 int x = 0, y = 0; 142 switch (edge.primary_edge) { 143 case MAGNETISM_EDGE_TOP: 144 y = attach_to.bottom(); 145 break; 146 case MAGNETISM_EDGE_LEFT: 147 x = attach_to.right(); 148 break; 149 case MAGNETISM_EDGE_BOTTOM: 150 y = attach_to.y() - src.height(); 151 break; 152 case MAGNETISM_EDGE_RIGHT: 153 x = attach_to.x() - src.width(); 154 break; 155 } 156 switch (edge.primary_edge) { 157 case MAGNETISM_EDGE_TOP: 158 case MAGNETISM_EDGE_BOTTOM: 159 x = CoordinateAlongSecondaryAxis( 160 edge.secondary_edge, attach_to.x(), attach_to.right() - src.width(), 161 src.x()); 162 break; 163 case MAGNETISM_EDGE_LEFT: 164 case MAGNETISM_EDGE_RIGHT: 165 y = CoordinateAlongSecondaryAxis( 166 edge.secondary_edge, attach_to.y(), attach_to.bottom() - src.height(), 167 src.y()); 168 break; 169 } 170 return gfx::Point(x, y); 171 } 172 173 // Returns the bounds for a magnetic attach when resizing. |src| is the bounds 174 // of window being resized, |attach_to| the bounds of the window to attach to 175 // and |edge| identifies the edge to attach to. 176 gfx::Rect BoundsForMagneticResizeAttach(const gfx::Rect& src, 177 const gfx::Rect& attach_to, 178 const MatchedEdge& edge) { 179 int x = src.x(); 180 int y = src.y(); 181 int w = src.width(); 182 int h = src.height(); 183 gfx::Point attach_origin(OriginForMagneticAttach(src, attach_to, edge)); 184 switch (edge.primary_edge) { 185 case MAGNETISM_EDGE_LEFT: 186 x = attach_origin.x(); 187 w = src.right() - x; 188 break; 189 case MAGNETISM_EDGE_RIGHT: 190 w += attach_origin.x() - src.x(); 191 break; 192 case MAGNETISM_EDGE_TOP: 193 y = attach_origin.y(); 194 h = src.bottom() - y; 195 break; 196 case MAGNETISM_EDGE_BOTTOM: 197 h += attach_origin.y() - src.y(); 198 break; 199 } 200 switch (edge.primary_edge) { 201 case MAGNETISM_EDGE_LEFT: 202 case MAGNETISM_EDGE_RIGHT: 203 if (edge.secondary_edge == SECONDARY_MAGNETISM_EDGE_LEADING) { 204 y = attach_origin.y(); 205 h = src.bottom() - y; 206 } else if (edge.secondary_edge == SECONDARY_MAGNETISM_EDGE_TRAILING) { 207 h += attach_origin.y() - src.y(); 208 } 209 break; 210 case MAGNETISM_EDGE_TOP: 211 case MAGNETISM_EDGE_BOTTOM: 212 if (edge.secondary_edge == SECONDARY_MAGNETISM_EDGE_LEADING) { 213 x = attach_origin.x(); 214 w = src.right() - x; 215 } else if (edge.secondary_edge == SECONDARY_MAGNETISM_EDGE_TRAILING) { 216 w += attach_origin.x() - src.x(); 217 } 218 break; 219 } 220 return gfx::Rect(x, y, w, h); 221 } 222 223 // Converts a window component edge to the magnetic edge to snap to. 224 uint32 WindowComponentToMagneticEdge(int window_component) { 225 switch (window_component) { 226 case HTTOPLEFT: 227 return MAGNETISM_EDGE_LEFT | MAGNETISM_EDGE_TOP; 228 case HTTOPRIGHT: 229 return MAGNETISM_EDGE_TOP | MAGNETISM_EDGE_RIGHT; 230 case HTBOTTOMLEFT: 231 return MAGNETISM_EDGE_LEFT | MAGNETISM_EDGE_BOTTOM; 232 case HTBOTTOMRIGHT: 233 return MAGNETISM_EDGE_RIGHT | MAGNETISM_EDGE_BOTTOM; 234 case HTTOP: 235 return MAGNETISM_EDGE_TOP; 236 case HTBOTTOM: 237 return MAGNETISM_EDGE_BOTTOM; 238 case HTRIGHT: 239 return MAGNETISM_EDGE_RIGHT; 240 case HTLEFT: 241 return MAGNETISM_EDGE_LEFT; 242 default: 243 break; 244 } 245 return 0; 246 } 247 248 } // namespace 249 250 // static 251 const int WorkspaceWindowResizer::kMinOnscreenSize = 20; 252 253 // static 254 const int WorkspaceWindowResizer::kMinOnscreenHeight = 32; 255 256 // static 257 const int WorkspaceWindowResizer::kScreenEdgeInset = 8; 258 259 // static 260 WorkspaceWindowResizer* WorkspaceWindowResizer::instance_ = NULL; 261 262 // Represents the width or height of a window with constraints on its minimum 263 // and maximum size. 0 represents a lack of a constraint. 264 class WindowSize { 265 public: 266 WindowSize(int size, int min, int max) 267 : size_(size), 268 min_(min), 269 max_(max) { 270 // Grow the min/max bounds to include the starting size. 271 if (is_underflowing()) 272 min_ = size_; 273 if (is_overflowing()) 274 max_ = size_; 275 } 276 277 bool is_at_capacity(bool shrinking) { 278 return size_ == (shrinking ? min_ : max_); 279 } 280 281 int size() const { 282 return size_; 283 } 284 285 bool has_min() const { 286 return min_ != 0; 287 } 288 289 bool has_max() const { 290 return max_ != 0; 291 } 292 293 bool is_valid() const { 294 return !is_overflowing() && !is_underflowing(); 295 } 296 297 bool is_overflowing() const { 298 return has_max() && size_ > max_; 299 } 300 301 bool is_underflowing() const { 302 return has_min() && size_ < min_; 303 } 304 305 // Add |amount| to this WindowSize not exceeding min or max size constraints. 306 // Returns by how much |size_| + |amount| exceeds the min/max constraints. 307 int Add(int amount) { 308 DCHECK(is_valid()); 309 int new_value = size_ + amount; 310 311 if (has_min() && new_value < min_) { 312 size_ = min_; 313 return new_value - min_; 314 } 315 316 if (has_max() && new_value > max_) { 317 size_ = max_; 318 return new_value - max_; 319 } 320 321 size_ = new_value; 322 return 0; 323 } 324 325 private: 326 int size_; 327 int min_; 328 int max_; 329 }; 330 331 WorkspaceWindowResizer::~WorkspaceWindowResizer() { 332 if (did_lock_cursor_) { 333 Shell* shell = Shell::GetInstance(); 334 shell->cursor_manager()->UnlockCursor(); 335 } 336 if (instance_ == this) 337 instance_ = NULL; 338 } 339 340 // static 341 WorkspaceWindowResizer* WorkspaceWindowResizer::Create( 342 wm::WindowState* window_state, 343 const std::vector<aura::Window*>& attached_windows) { 344 return new WorkspaceWindowResizer(window_state, attached_windows); 345 } 346 347 void WorkspaceWindowResizer::Drag(const gfx::Point& location_in_parent, 348 int event_flags) { 349 last_mouse_location_ = location_in_parent; 350 351 int sticky_size; 352 if (event_flags & ui::EF_CONTROL_DOWN) { 353 sticky_size = 0; 354 } else if ((details().bounds_change & kBoundsChange_Resizes) && 355 details().source == aura::client::WINDOW_MOVE_SOURCE_TOUCH) { 356 sticky_size = kScreenEdgeInsetForTouchDrag; 357 } else { 358 sticky_size = kScreenEdgeInset; 359 } 360 // |bounds| is in |GetTarget()->parent()|'s coordinates. 361 gfx::Rect bounds = CalculateBoundsForDrag(location_in_parent); 362 AdjustBoundsForMainWindow(sticky_size, &bounds); 363 364 if (bounds != GetTarget()->bounds()) { 365 if (!did_move_or_resize_) { 366 if (!details().restore_bounds.IsEmpty()) 367 window_state()->ClearRestoreBounds(); 368 RestackWindows(); 369 } 370 did_move_or_resize_ = true; 371 } 372 373 gfx::Point location_in_screen = location_in_parent; 374 wm::ConvertPointToScreen(GetTarget()->parent(), &location_in_screen); 375 376 aura::Window* root = NULL; 377 gfx::Display display = 378 ScreenUtil::FindDisplayContainingPoint(location_in_screen); 379 // Track the last screen that the pointer was on to keep the snap phantom 380 // window there. 381 if (display.is_valid()) { 382 root = Shell::GetInstance()->display_controller()-> 383 GetRootWindowForDisplayId(display.id()); 384 } 385 if (!attached_windows_.empty()) 386 LayoutAttachedWindows(&bounds); 387 if (bounds != GetTarget()->bounds()) { 388 // SetBounds needs to be called to update the layout which affects where the 389 // phantom window is drawn. Keep track if the window was destroyed during 390 // the drag and quit early if so. 391 base::WeakPtr<WorkspaceWindowResizer> resizer( 392 weak_ptr_factory_.GetWeakPtr()); 393 GetTarget()->SetBounds(bounds); 394 if (!resizer) 395 return; 396 } 397 const bool in_original_root = !root || root == GetTarget()->GetRootWindow(); 398 // Hide a phantom window for snapping if the cursor is in another root window. 399 if (in_original_root) { 400 UpdateSnapPhantomWindow(location_in_parent, bounds); 401 } else { 402 snap_type_ = SNAP_NONE; 403 snap_phantom_window_controller_.reset(); 404 edge_cycler_.reset(); 405 SetDraggedWindowDocked(false); 406 } 407 } 408 409 void WorkspaceWindowResizer::CompleteDrag() { 410 if (!did_move_or_resize_) 411 return; 412 413 window_state()->set_bounds_changed_by_user(true); 414 snap_phantom_window_controller_.reset(); 415 416 // If the window's state type changed over the course of the drag do not snap 417 // the window. This happens when the user minimizes or maximizes the window 418 // using a keyboard shortcut while dragging it. 419 if (window_state()->GetStateType() != details().initial_state_type) 420 return; 421 422 bool snapped = false; 423 if (snap_type_ == SNAP_LEFT || snap_type_ == SNAP_RIGHT) { 424 if (!window_state()->HasRestoreBounds()) { 425 gfx::Rect initial_bounds = ScreenUtil::ConvertRectToScreen( 426 GetTarget()->parent(), details().initial_bounds_in_parent); 427 window_state()->SetRestoreBoundsInScreen( 428 details().restore_bounds.IsEmpty() ? 429 initial_bounds : 430 details().restore_bounds); 431 } 432 if (!dock_layout_->is_dragged_window_docked()) { 433 UserMetricsRecorder* metrics = Shell::GetInstance()->metrics(); 434 // TODO(oshima): Add event source type to WMEvent and move 435 // metrics recording inside WindowState::OnWMEvent. 436 const wm::WMEvent event(snap_type_ == SNAP_LEFT ? 437 wm::WM_EVENT_SNAP_LEFT : wm::WM_EVENT_SNAP_RIGHT); 438 window_state()->OnWMEvent(&event); 439 metrics->RecordUserMetricsAction( 440 snap_type_ == SNAP_LEFT ? 441 UMA_DRAG_MAXIMIZE_LEFT : UMA_DRAG_MAXIMIZE_RIGHT); 442 snapped = true; 443 } 444 } 445 446 if (!snapped && window_state()->IsSnapped()) { 447 // Keep the window snapped if the user resizes the window such that the 448 // window has valid bounds for a snapped window. Always unsnap the window 449 // if the user dragged the window via the caption area because doing this is 450 // slightly less confusing. 451 if (details().window_component == HTCAPTION || 452 !AreBoundsValidSnappedBounds(window_state()->GetStateType(), 453 GetTarget()->bounds())) { 454 // Set the window to WINDOW_STATE_TYPE_NORMAL but keep the 455 // window at the bounds that the user has moved/resized the 456 // window to. ClearRestoreBounds() is used instead of 457 // SaveCurrentBoundsForRestore() because most of the restore 458 // logic is skipped because we are still in the middle of a 459 // drag. TODO(pkotwicz): Fix this and use 460 // SaveCurrentBoundsForRestore(). 461 window_state()->ClearRestoreBounds(); 462 window_state()->Restore(); 463 } 464 } 465 } 466 467 void WorkspaceWindowResizer::RevertDrag() { 468 window_state()->set_bounds_changed_by_user(initial_bounds_changed_by_user_); 469 snap_phantom_window_controller_.reset(); 470 471 if (!did_move_or_resize_) 472 return; 473 474 GetTarget()->SetBounds(details().initial_bounds_in_parent); 475 if (!details().restore_bounds.IsEmpty()) { 476 window_state()->SetRestoreBoundsInScreen(details().restore_bounds); 477 } 478 479 if (details().window_component == HTRIGHT) { 480 int last_x = details().initial_bounds_in_parent.right(); 481 for (size_t i = 0; i < attached_windows_.size(); ++i) { 482 gfx::Rect bounds(attached_windows_[i]->bounds()); 483 bounds.set_x(last_x); 484 bounds.set_width(initial_size_[i]); 485 attached_windows_[i]->SetBounds(bounds); 486 last_x = attached_windows_[i]->bounds().right(); 487 } 488 } else { 489 int last_y = details().initial_bounds_in_parent.bottom(); 490 for (size_t i = 0; i < attached_windows_.size(); ++i) { 491 gfx::Rect bounds(attached_windows_[i]->bounds()); 492 bounds.set_y(last_y); 493 bounds.set_height(initial_size_[i]); 494 attached_windows_[i]->SetBounds(bounds); 495 last_y = attached_windows_[i]->bounds().bottom(); 496 } 497 } 498 } 499 500 WorkspaceWindowResizer::WorkspaceWindowResizer( 501 wm::WindowState* window_state, 502 const std::vector<aura::Window*>& attached_windows) 503 : WindowResizer(window_state), 504 attached_windows_(attached_windows), 505 did_lock_cursor_(false), 506 did_move_or_resize_(false), 507 initial_bounds_changed_by_user_(window_state_->bounds_changed_by_user()), 508 total_min_(0), 509 total_initial_size_(0), 510 snap_type_(SNAP_NONE), 511 num_mouse_moves_since_bounds_change_(0), 512 magnetism_window_(NULL), 513 weak_ptr_factory_(this) { 514 DCHECK(details().is_resizable); 515 516 // A mousemove should still show the cursor even if the window is 517 // being moved or resized with touch, so do not lock the cursor. 518 if (details().source != aura::client::WINDOW_MOVE_SOURCE_TOUCH) { 519 Shell* shell = Shell::GetInstance(); 520 shell->cursor_manager()->LockCursor(); 521 did_lock_cursor_ = true; 522 } 523 524 aura::Window* dock_container = Shell::GetContainer( 525 GetTarget()->GetRootWindow(), kShellWindowId_DockedContainer); 526 dock_layout_ = static_cast<DockedWindowLayoutManager*>( 527 dock_container->layout_manager()); 528 529 // Only support attaching to the right/bottom. 530 DCHECK(attached_windows_.empty() || 531 (details().window_component == HTRIGHT || 532 details().window_component == HTBOTTOM)); 533 534 // TODO: figure out how to deal with window going off the edge. 535 536 // Calculate sizes so that we can maintain the ratios if we need to resize. 537 int total_available = 0; 538 for (size_t i = 0; i < attached_windows_.size(); ++i) { 539 gfx::Size min(attached_windows_[i]->delegate()->GetMinimumSize()); 540 int initial_size = PrimaryAxisSize(attached_windows_[i]->bounds().size()); 541 initial_size_.push_back(initial_size); 542 // If current size is smaller than the min, use the current size as the min. 543 // This way we don't snap on resize. 544 int min_size = std::min(initial_size, 545 std::max(PrimaryAxisSize(min), kMinOnscreenSize)); 546 total_min_ += min_size; 547 total_initial_size_ += initial_size; 548 total_available += std::max(min_size, initial_size) - min_size; 549 } 550 instance_ = this; 551 } 552 553 void WorkspaceWindowResizer::LayoutAttachedWindows( 554 gfx::Rect* bounds) { 555 gfx::Rect work_area(ScreenUtil::GetDisplayWorkAreaBoundsInParent( 556 GetTarget())); 557 int initial_size = PrimaryAxisSize(details().initial_bounds_in_parent.size()); 558 int current_size = PrimaryAxisSize(bounds->size()); 559 int start = PrimaryAxisCoordinate(bounds->right(), bounds->bottom()); 560 int end = PrimaryAxisCoordinate(work_area.right(), work_area.bottom()); 561 562 int delta = current_size - initial_size; 563 int available_size = end - start; 564 std::vector<int> sizes; 565 int leftovers = CalculateAttachedSizes(delta, available_size, &sizes); 566 567 // leftovers > 0 means that the attached windows can't grow to compensate for 568 // the shrinkage of the main window. This line causes the attached windows to 569 // be moved so they are still flush against the main window, rather than the 570 // main window being prevented from shrinking. 571 leftovers = std::min(0, leftovers); 572 // Reallocate any leftover pixels back into the main window. This is 573 // necessary when, for example, the main window shrinks, but none of the 574 // attached windows can grow without exceeding their max size constraints. 575 // Adding the pixels back to the main window effectively prevents the main 576 // window from resizing too far. 577 if (details().window_component == HTRIGHT) 578 bounds->set_width(bounds->width() + leftovers); 579 else 580 bounds->set_height(bounds->height() + leftovers); 581 582 DCHECK_EQ(attached_windows_.size(), sizes.size()); 583 int last = PrimaryAxisCoordinate(bounds->right(), bounds->bottom()); 584 for (size_t i = 0; i < attached_windows_.size(); ++i) { 585 gfx::Rect attached_bounds(attached_windows_[i]->bounds()); 586 if (details().window_component == HTRIGHT) { 587 attached_bounds.set_x(last); 588 attached_bounds.set_width(sizes[i]); 589 } else { 590 attached_bounds.set_y(last); 591 attached_bounds.set_height(sizes[i]); 592 } 593 attached_windows_[i]->SetBounds(attached_bounds); 594 last += sizes[i]; 595 } 596 } 597 598 int WorkspaceWindowResizer::CalculateAttachedSizes( 599 int delta, 600 int available_size, 601 std::vector<int>* sizes) const { 602 std::vector<WindowSize> window_sizes; 603 CreateBucketsForAttached(&window_sizes); 604 605 // How much we need to grow the attached by (collectively). 606 int grow_attached_by = 0; 607 if (delta > 0) { 608 // If the attached windows don't fit when at their initial size, we will 609 // have to shrink them by how much they overflow. 610 if (total_initial_size_ >= available_size) 611 grow_attached_by = available_size - total_initial_size_; 612 } else { 613 // If we're shrinking, we grow the attached so the total size remains 614 // constant. 615 grow_attached_by = -delta; 616 } 617 618 int leftover_pixels = 0; 619 while (grow_attached_by != 0) { 620 int leftovers = GrowFairly(grow_attached_by, window_sizes); 621 if (leftovers == grow_attached_by) { 622 leftover_pixels = leftovers; 623 break; 624 } 625 grow_attached_by = leftovers; 626 } 627 628 for (size_t i = 0; i < window_sizes.size(); ++i) 629 sizes->push_back(window_sizes[i].size()); 630 631 return leftover_pixels; 632 } 633 634 int WorkspaceWindowResizer::GrowFairly( 635 int pixels, 636 std::vector<WindowSize>& sizes) const { 637 bool shrinking = pixels < 0; 638 std::vector<WindowSize*> nonfull_windows; 639 for (size_t i = 0; i < sizes.size(); ++i) { 640 if (!sizes[i].is_at_capacity(shrinking)) 641 nonfull_windows.push_back(&sizes[i]); 642 } 643 std::vector<float> ratios; 644 CalculateGrowthRatios(nonfull_windows, &ratios); 645 646 int remaining_pixels = pixels; 647 bool add_leftover_pixels_to_last = true; 648 for (size_t i = 0; i < nonfull_windows.size(); ++i) { 649 int grow_by = pixels * ratios[i]; 650 // Put any leftover pixels into the last window. 651 if (i == nonfull_windows.size() - 1 && add_leftover_pixels_to_last) 652 grow_by = remaining_pixels; 653 int remainder = nonfull_windows[i]->Add(grow_by); 654 int consumed = grow_by - remainder; 655 remaining_pixels -= consumed; 656 if (nonfull_windows[i]->is_at_capacity(shrinking) && remainder > 0) { 657 // Because this window overflowed, some of the pixels in 658 // |remaining_pixels| aren't there due to rounding errors. Rather than 659 // unfairly giving all those pixels to the last window, we refrain from 660 // allocating them so that this function can be called again to distribute 661 // the pixels fairly. 662 add_leftover_pixels_to_last = false; 663 } 664 } 665 return remaining_pixels; 666 } 667 668 void WorkspaceWindowResizer::CalculateGrowthRatios( 669 const std::vector<WindowSize*>& sizes, 670 std::vector<float>* out_ratios) const { 671 DCHECK(out_ratios->empty()); 672 int total_value = 0; 673 for (size_t i = 0; i < sizes.size(); ++i) 674 total_value += sizes[i]->size(); 675 676 for (size_t i = 0; i < sizes.size(); ++i) 677 out_ratios->push_back( 678 (static_cast<float>(sizes[i]->size())) / total_value); 679 } 680 681 void WorkspaceWindowResizer::CreateBucketsForAttached( 682 std::vector<WindowSize>* sizes) const { 683 for (size_t i = 0; i < attached_windows_.size(); i++) { 684 int initial_size = initial_size_[i]; 685 aura::WindowDelegate* delegate = attached_windows_[i]->delegate(); 686 int min = PrimaryAxisSize(delegate->GetMinimumSize()); 687 int max = PrimaryAxisSize(delegate->GetMaximumSize()); 688 689 sizes->push_back(WindowSize(initial_size, min, max)); 690 } 691 } 692 693 void WorkspaceWindowResizer::MagneticallySnapToOtherWindows(gfx::Rect* bounds) { 694 if (UpdateMagnetismWindow(*bounds, kAllMagnetismEdges)) { 695 gfx::Point point = OriginForMagneticAttach( 696 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), *bounds), 697 magnetism_window_->GetBoundsInScreen(), 698 magnetism_edge_); 699 aura::client::GetScreenPositionClient(GetTarget()->GetRootWindow())-> 700 ConvertPointFromScreen(GetTarget()->parent(), &point); 701 bounds->set_origin(point); 702 } 703 } 704 705 void WorkspaceWindowResizer::MagneticallySnapResizeToOtherWindows( 706 gfx::Rect* bounds) { 707 const uint32 edges = WindowComponentToMagneticEdge( 708 details().window_component); 709 if (UpdateMagnetismWindow(*bounds, edges)) { 710 *bounds = ScreenUtil::ConvertRectFromScreen( 711 GetTarget()->parent(), 712 BoundsForMagneticResizeAttach( 713 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), *bounds), 714 magnetism_window_->GetBoundsInScreen(), 715 magnetism_edge_)); 716 } 717 } 718 719 bool WorkspaceWindowResizer::UpdateMagnetismWindow(const gfx::Rect& bounds, 720 uint32 edges) { 721 // |bounds| are in coordinates of original window's parent. 722 gfx::Rect bounds_in_screen = 723 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds); 724 MagnetismMatcher matcher(bounds_in_screen, edges); 725 726 // If we snapped to a window then check it first. That way we don't bounce 727 // around when close to multiple edges. 728 if (magnetism_window_) { 729 if (window_tracker_.Contains(magnetism_window_) && 730 matcher.ShouldAttach(magnetism_window_->GetBoundsInScreen(), 731 &magnetism_edge_)) { 732 return true; 733 } 734 window_tracker_.Remove(magnetism_window_); 735 magnetism_window_ = NULL; 736 } 737 738 // Avoid magnetically snapping windows that are not resizable. 739 // TODO(oshima): change this to window.type() == TYPE_NORMAL. 740 if (!window_state()->CanResize()) 741 return false; 742 743 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); 744 for (aura::Window::Windows::iterator iter = root_windows.begin(); 745 iter != root_windows.end(); ++iter) { 746 const aura::Window* root_window = *iter; 747 // Test all children from the desktop in each root window. 748 const aura::Window::Windows& children = Shell::GetContainer( 749 root_window, kShellWindowId_DefaultContainer)->children(); 750 for (aura::Window::Windows::const_reverse_iterator i = children.rbegin(); 751 i != children.rend() && !matcher.AreEdgesObscured(); ++i) { 752 wm::WindowState* other_state = wm::GetWindowState(*i); 753 if (other_state->window() == GetTarget() || 754 !other_state->window()->IsVisible() || 755 !other_state->IsNormalOrSnapped() || 756 !other_state->CanResize()) { 757 continue; 758 } 759 if (matcher.ShouldAttach( 760 other_state->window()->GetBoundsInScreen(), &magnetism_edge_)) { 761 magnetism_window_ = other_state->window(); 762 window_tracker_.Add(magnetism_window_); 763 return true; 764 } 765 } 766 } 767 return false; 768 } 769 770 void WorkspaceWindowResizer::AdjustBoundsForMainWindow( 771 int sticky_size, 772 gfx::Rect* bounds) { 773 gfx::Point last_mouse_location_in_screen = last_mouse_location_; 774 wm::ConvertPointToScreen(GetTarget()->parent(), 775 &last_mouse_location_in_screen); 776 gfx::Display display = Shell::GetScreen()->GetDisplayNearestPoint( 777 last_mouse_location_in_screen); 778 gfx::Rect work_area = 779 ScreenUtil::ConvertRectFromScreen(GetTarget()->parent(), 780 display.work_area()); 781 if (details().window_component == HTCAPTION) { 782 // Adjust the bounds to the work area where the mouse cursor is located. 783 // Always keep kMinOnscreenHeight or the window height (whichever is less) 784 // on the bottom. 785 int max_y = work_area.bottom() - std::min(kMinOnscreenHeight, 786 bounds->height()); 787 if (bounds->y() > max_y) { 788 bounds->set_y(max_y); 789 } else if (bounds->y() <= work_area.y()) { 790 // Don't allow dragging above the top of the display until the mouse 791 // cursor reaches the work area above if any. 792 bounds->set_y(work_area.y()); 793 } 794 795 if (sticky_size > 0) { 796 // Possibly stick to edge except when a mouse pointer is outside the 797 // work area. 798 if (display.work_area().Contains(last_mouse_location_in_screen)) 799 StickToWorkAreaOnMove(work_area, sticky_size, bounds); 800 MagneticallySnapToOtherWindows(bounds); 801 } 802 } else if (sticky_size > 0) { 803 MagneticallySnapResizeToOtherWindows(bounds); 804 if (!magnetism_window_ && sticky_size > 0) 805 StickToWorkAreaOnResize(work_area, sticky_size, bounds); 806 } 807 808 if (attached_windows_.empty()) 809 return; 810 811 if (details().window_component == HTRIGHT) { 812 bounds->set_width(std::min(bounds->width(), 813 work_area.right() - total_min_ - bounds->x())); 814 } else { 815 DCHECK_EQ(HTBOTTOM, details().window_component); 816 bounds->set_height(std::min(bounds->height(), 817 work_area.bottom() - total_min_ - bounds->y())); 818 } 819 } 820 821 bool WorkspaceWindowResizer::StickToWorkAreaOnMove( 822 const gfx::Rect& work_area, 823 int sticky_size, 824 gfx::Rect* bounds) const { 825 const int left_edge = work_area.x(); 826 const int right_edge = work_area.right(); 827 const int top_edge = work_area.y(); 828 const int bottom_edge = work_area.bottom(); 829 bool updated = false; 830 if (ShouldStickToEdge(bounds->x() - left_edge, sticky_size)) { 831 bounds->set_x(left_edge); 832 updated = true; 833 } else if (ShouldStickToEdge(right_edge - bounds->right(), sticky_size)) { 834 bounds->set_x(right_edge - bounds->width()); 835 updated = true; 836 } 837 if (ShouldStickToEdge(bounds->y() - top_edge, sticky_size)) { 838 bounds->set_y(top_edge); 839 updated = true; 840 } else if (ShouldStickToEdge(bottom_edge - bounds->bottom(), sticky_size) && 841 bounds->height() < (bottom_edge - top_edge)) { 842 // Only snap to the bottom if the window is smaller than the work area. 843 // Doing otherwise can lead to window snapping in weird ways as it bounces 844 // between snapping to top then bottom. 845 bounds->set_y(bottom_edge - bounds->height()); 846 updated = true; 847 } 848 return updated; 849 } 850 851 void WorkspaceWindowResizer::StickToWorkAreaOnResize( 852 const gfx::Rect& work_area, 853 int sticky_size, 854 gfx::Rect* bounds) const { 855 const uint32 edges = WindowComponentToMagneticEdge( 856 details().window_component); 857 const int left_edge = work_area.x(); 858 const int right_edge = work_area.right(); 859 const int top_edge = work_area.y(); 860 const int bottom_edge = work_area.bottom(); 861 if (edges & MAGNETISM_EDGE_TOP && 862 ShouldStickToEdge(bounds->y() - top_edge, sticky_size)) { 863 bounds->set_height(bounds->bottom() - top_edge); 864 bounds->set_y(top_edge); 865 } 866 if (edges & MAGNETISM_EDGE_LEFT && 867 ShouldStickToEdge(bounds->x() - left_edge, sticky_size)) { 868 bounds->set_width(bounds->right() - left_edge); 869 bounds->set_x(left_edge); 870 } 871 if (edges & MAGNETISM_EDGE_BOTTOM && 872 ShouldStickToEdge(bottom_edge - bounds->bottom(), sticky_size)) { 873 bounds->set_height(bottom_edge - bounds->y()); 874 } 875 if (edges & MAGNETISM_EDGE_RIGHT && 876 ShouldStickToEdge(right_edge - bounds->right(), sticky_size)) { 877 bounds->set_width(right_edge - bounds->x()); 878 } 879 } 880 881 int WorkspaceWindowResizer::PrimaryAxisSize(const gfx::Size& size) const { 882 return PrimaryAxisCoordinate(size.width(), size.height()); 883 } 884 885 int WorkspaceWindowResizer::PrimaryAxisCoordinate(int x, int y) const { 886 switch (details().window_component) { 887 case HTRIGHT: 888 return x; 889 case HTBOTTOM: 890 return y; 891 default: 892 NOTREACHED(); 893 } 894 return 0; 895 } 896 897 void WorkspaceWindowResizer::UpdateSnapPhantomWindow(const gfx::Point& location, 898 const gfx::Rect& bounds) { 899 if (!did_move_or_resize_ || details().window_component != HTCAPTION) 900 return; 901 902 SnapType last_type = snap_type_; 903 snap_type_ = GetSnapType(location); 904 if (snap_type_ == SNAP_NONE || snap_type_ != last_type) { 905 snap_phantom_window_controller_.reset(); 906 edge_cycler_.reset(); 907 if (snap_type_ == SNAP_NONE) { 908 SetDraggedWindowDocked(false); 909 return; 910 } 911 } 912 913 DCHECK(snap_type_ == SNAP_LEFT || snap_type_ == SNAP_RIGHT); 914 DockedAlignment desired_alignment = (snap_type_ == SNAP_LEFT) ? 915 DOCKED_ALIGNMENT_LEFT : DOCKED_ALIGNMENT_RIGHT; 916 const bool can_dock = 917 dock_layout_->CanDockWindow(GetTarget(), desired_alignment) && 918 dock_layout_->GetAlignmentOfWindow(GetTarget()) != DOCKED_ALIGNMENT_NONE; 919 if (!can_dock) { 920 // If the window cannot be docked, undock the window. This may change the 921 // workspace bounds and hence |snap_type_|. 922 SetDraggedWindowDocked(false); 923 snap_type_ = GetSnapType(location); 924 } 925 const bool can_snap = snap_type_ != SNAP_NONE && window_state()->CanSnap(); 926 if (!can_snap && !can_dock) { 927 snap_type_ = SNAP_NONE; 928 snap_phantom_window_controller_.reset(); 929 edge_cycler_.reset(); 930 return; 931 } 932 if (!edge_cycler_) 933 edge_cycler_.reset(new TwoStepEdgeCycler(location)); 934 else 935 edge_cycler_->OnMove(location); 936 937 // Update phantom window with snapped or docked guide bounds. 938 // Windows that cannot be snapped or are less wide than kMaxDockWidth can get 939 // docked without going through a snapping sequence. 940 gfx::Rect phantom_bounds; 941 const bool should_dock = can_dock && 942 (!can_snap || 943 GetTarget()->bounds().width() <= 944 DockedWindowLayoutManager::kMaxDockWidth || 945 edge_cycler_->use_second_mode() || 946 dock_layout_->is_dragged_window_docked()); 947 if (should_dock) { 948 SetDraggedWindowDocked(true); 949 phantom_bounds = ScreenUtil::ConvertRectFromScreen( 950 GetTarget()->parent(), dock_layout_->dragged_bounds()); 951 } else { 952 phantom_bounds = (snap_type_ == SNAP_LEFT) ? 953 wm::GetDefaultLeftSnappedWindowBoundsInParent(GetTarget()) : 954 wm::GetDefaultRightSnappedWindowBoundsInParent(GetTarget()); 955 } 956 957 if (!snap_phantom_window_controller_) { 958 snap_phantom_window_controller_.reset( 959 new PhantomWindowController(GetTarget())); 960 } 961 snap_phantom_window_controller_->Show(ScreenUtil::ConvertRectToScreen( 962 GetTarget()->parent(), phantom_bounds)); 963 } 964 965 void WorkspaceWindowResizer::RestackWindows() { 966 if (attached_windows_.empty()) 967 return; 968 // Build a map from index in children to window, returning if there is a 969 // window with a different parent. 970 typedef std::map<size_t, aura::Window*> IndexToWindowMap; 971 IndexToWindowMap map; 972 aura::Window* parent = GetTarget()->parent(); 973 const aura::Window::Windows& windows(parent->children()); 974 map[std::find(windows.begin(), windows.end(), GetTarget()) - 975 windows.begin()] = GetTarget(); 976 for (std::vector<aura::Window*>::const_iterator i = 977 attached_windows_.begin(); i != attached_windows_.end(); ++i) { 978 if ((*i)->parent() != parent) 979 return; 980 size_t index = 981 std::find(windows.begin(), windows.end(), *i) - windows.begin(); 982 map[index] = *i; 983 } 984 985 // Reorder the windows starting at the topmost. 986 parent->StackChildAtTop(map.rbegin()->second); 987 for (IndexToWindowMap::const_reverse_iterator i = map.rbegin(); 988 i != map.rend(); ) { 989 aura::Window* window = i->second; 990 ++i; 991 if (i != map.rend()) 992 parent->StackChildBelow(i->second, window); 993 } 994 } 995 996 WorkspaceWindowResizer::SnapType WorkspaceWindowResizer::GetSnapType( 997 const gfx::Point& location) const { 998 // TODO: this likely only wants total display area, not the area of a single 999 // display. 1000 gfx::Rect area(ScreenUtil::GetDisplayWorkAreaBoundsInParent(GetTarget())); 1001 if (details().source == aura::client::WINDOW_MOVE_SOURCE_TOUCH) { 1002 // Increase tolerance for touch-snapping near the screen edges. This is only 1003 // necessary when the work area left or right edge is same as screen edge. 1004 gfx::Rect display_bounds(ScreenUtil::GetDisplayBoundsInParent(GetTarget())); 1005 int inset_left = 0; 1006 if (area.x() == display_bounds.x()) 1007 inset_left = kScreenEdgeInsetForTouchDrag; 1008 int inset_right = 0; 1009 if (area.right() == display_bounds.right()) 1010 inset_right = kScreenEdgeInsetForTouchDrag; 1011 area.Inset(inset_left, 0, inset_right, 0); 1012 } 1013 if (location.x() <= area.x()) 1014 return SNAP_LEFT; 1015 if (location.x() >= area.right() - 1) 1016 return SNAP_RIGHT; 1017 return SNAP_NONE; 1018 } 1019 1020 void WorkspaceWindowResizer::SetDraggedWindowDocked(bool should_dock) { 1021 if (should_dock) { 1022 if (!dock_layout_->is_dragged_window_docked()) { 1023 window_state()->set_bounds_changed_by_user(false); 1024 dock_layout_->DockDraggedWindow(GetTarget()); 1025 } 1026 } else { 1027 if (dock_layout_->is_dragged_window_docked()) { 1028 dock_layout_->UndockDraggedWindow(); 1029 window_state()->set_bounds_changed_by_user(true); 1030 } 1031 } 1032 } 1033 1034 bool WorkspaceWindowResizer::AreBoundsValidSnappedBounds( 1035 wm::WindowStateType snapped_type, 1036 const gfx::Rect& bounds_in_parent) const { 1037 DCHECK(snapped_type == wm::WINDOW_STATE_TYPE_LEFT_SNAPPED || 1038 snapped_type == wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED); 1039 gfx::Rect snapped_bounds = ScreenUtil::GetDisplayWorkAreaBoundsInParent( 1040 GetTarget()); 1041 if (snapped_type == wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED) 1042 snapped_bounds.set_x(snapped_bounds.right() - bounds_in_parent.width()); 1043 snapped_bounds.set_width(bounds_in_parent.width()); 1044 return bounds_in_parent == snapped_bounds; 1045 } 1046 1047 } // namespace ash 1048