drag_details.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2014 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. #include "ash/wm/drag_details.h"
  5. #include "ash/public/cpp/window_properties.h"
  6. #include "ash/shell.h"
  7. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  8. #include "ash/wm/window_resizer.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/base/hit_test.h"
  11. #include "ui/wm/core/coordinate_conversion.h"
  12. namespace ash {
  13. namespace {
  14. int GetSizeChangeDirectionForWindowComponent(int window_component) {
  15. int size_change_direction = WindowResizer::kBoundsChangeDirection_None;
  16. switch (window_component) {
  17. case HTTOPLEFT:
  18. case HTTOPRIGHT:
  19. case HTBOTTOMLEFT:
  20. case HTBOTTOMRIGHT:
  21. case HTGROWBOX:
  22. case HTCAPTION:
  23. size_change_direction |=
  24. WindowResizer::kBoundsChangeDirection_Horizontal |
  25. WindowResizer::kBoundsChangeDirection_Vertical;
  26. break;
  27. case HTTOP:
  28. case HTBOTTOM:
  29. size_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical;
  30. break;
  31. case HTRIGHT:
  32. case HTLEFT:
  33. size_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal;
  34. break;
  35. default:
  36. break;
  37. }
  38. return size_change_direction;
  39. }
  40. gfx::Rect GetWindowInitialBoundsInParent(aura::Window* window) {
  41. // Floated windows should use their current bounds as a starting point, even
  42. // in tablet mode.
  43. if (WindowState::Get(window)->IsFloated())
  44. return window->bounds();
  45. if (Shell::Get()->tablet_mode_controller()->InTabletMode()) {
  46. gfx::Rect* override_bounds = window->GetProperty(kRestoreBoundsOverrideKey);
  47. if (override_bounds && !override_bounds->IsEmpty()) {
  48. wm::ConvertRectFromScreen(window->GetRootWindow(), override_bounds);
  49. return *override_bounds;
  50. }
  51. }
  52. return window->bounds();
  53. }
  54. gfx::Rect GetRestoreBoundsInParent(aura::Window* window, int window_component) {
  55. if (window_component != HTCAPTION)
  56. return gfx::Rect();
  57. // Ignore the restore bounds of a floated window as we don't want its size to
  58. // change during dragging.
  59. WindowState* window_state = WindowState::Get(window);
  60. if (window_state->IsFloated())
  61. return gfx::Rect();
  62. // TODO(xdai): Move these logic to WindowState::GetRestoreBoundsInScreen()
  63. // and let it return the right value.
  64. gfx::Rect restore_bounds;
  65. if (Shell::Get()->tablet_mode_controller()->InTabletMode()) {
  66. gfx::Rect* override_bounds = window->GetProperty(kRestoreBoundsOverrideKey);
  67. if (override_bounds && !override_bounds->IsEmpty()) {
  68. restore_bounds = *override_bounds;
  69. wm::ConvertRectFromScreen(window->parent(), &restore_bounds);
  70. }
  71. } else if (window_state->IsSnapped() || window_state->IsMaximized()) {
  72. DCHECK(window_state->HasRestoreBounds());
  73. restore_bounds = window_state->GetRestoreBoundsInParent();
  74. } else if (window_state->IsNormalStateType() &&
  75. window_state->HasRestoreBounds()) {
  76. restore_bounds = window_state->GetRestoreBoundsInParent();
  77. }
  78. return restore_bounds;
  79. }
  80. } // namespace
  81. DragDetails::DragDetails(aura::Window* window,
  82. const gfx::PointF& location,
  83. int window_component,
  84. wm::WindowMoveSource source)
  85. : initial_state_type(WindowState::Get(window)->GetStateType()),
  86. initial_bounds_in_parent(GetWindowInitialBoundsInParent(window)),
  87. restore_bounds_in_parent(
  88. GetRestoreBoundsInParent(window, window_component)),
  89. initial_location_in_parent(location),
  90. window_component(window_component),
  91. bounds_change(
  92. WindowResizer::GetBoundsChangeForWindowComponent(window_component)),
  93. size_change_direction(
  94. GetSizeChangeDirectionForWindowComponent(window_component)),
  95. is_resizable(bounds_change != WindowResizer::kBoundsChangeDirection_None),
  96. source(source) {}
  97. DragDetails::~DragDetails() = default;
  98. } // namespace ash