| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/wm/window_resizer.h" | |
| 6 | |
| 7 #include "ash/shell_window_ids.h" | |
| 8 #include "ash/wm/common/root_window_finder.h" | |
| 9 #include "ash/wm/common/window_positioning_utils.h" | |
| 10 #include "ash/wm/common/wm_window.h" | |
| 11 #include "ash/wm/dock/docked_window_layout_manager.h" | |
| 12 #include "ash/wm/window_state.h" | |
| 13 #include "ui/base/hit_test.h" | |
| 14 #include "ui/base/ui_base_types.h" | |
| 15 #include "ui/gfx/display.h" | |
| 16 #include "ui/gfx/geometry/rect.h" | |
| 17 #include "ui/gfx/screen.h" | |
| 18 | |
| 19 namespace ash { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Returns true for resize components along the right edge, where a drag in | |
| 24 // positive x will make the window larger. | |
| 25 bool IsRightEdge(int window_component) { | |
| 26 return window_component == HTTOPRIGHT || | |
| 27 window_component == HTRIGHT || | |
| 28 window_component == HTBOTTOMRIGHT || | |
| 29 window_component == HTGROWBOX; | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 // static | |
| 35 const int WindowResizer::kBoundsChange_None = 0; | |
| 36 // static | |
| 37 const int WindowResizer::kBoundsChange_Repositions = 1; | |
| 38 // static | |
| 39 const int WindowResizer::kBoundsChange_Resizes = 2; | |
| 40 | |
| 41 // static | |
| 42 const int WindowResizer::kBoundsChangeDirection_None = 0; | |
| 43 // static | |
| 44 const int WindowResizer::kBoundsChangeDirection_Horizontal = 1; | |
| 45 // static | |
| 46 const int WindowResizer::kBoundsChangeDirection_Vertical = 2; | |
| 47 | |
| 48 WindowResizer::WindowResizer(wm::WindowState* window_state) | |
| 49 : window_state_(window_state) { | |
| 50 DCHECK(window_state_->drag_details()); | |
| 51 } | |
| 52 | |
| 53 WindowResizer::~WindowResizer() { | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 int WindowResizer::GetBoundsChangeForWindowComponent(int component) { | |
| 58 int bounds_change = WindowResizer::kBoundsChange_None; | |
| 59 switch (component) { | |
| 60 case HTTOPLEFT: | |
| 61 case HTTOP: | |
| 62 case HTTOPRIGHT: | |
| 63 case HTLEFT: | |
| 64 case HTBOTTOMLEFT: | |
| 65 bounds_change |= WindowResizer::kBoundsChange_Repositions | | |
| 66 WindowResizer::kBoundsChange_Resizes; | |
| 67 break; | |
| 68 case HTCAPTION: | |
| 69 bounds_change |= WindowResizer::kBoundsChange_Repositions; | |
| 70 break; | |
| 71 case HTRIGHT: | |
| 72 case HTBOTTOMRIGHT: | |
| 73 case HTBOTTOM: | |
| 74 case HTGROWBOX: | |
| 75 bounds_change |= WindowResizer::kBoundsChange_Resizes; | |
| 76 break; | |
| 77 default: | |
| 78 break; | |
| 79 } | |
| 80 return bounds_change; | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 int WindowResizer::GetPositionChangeDirectionForWindowComponent( | |
| 85 int window_component) { | |
| 86 int pos_change_direction = WindowResizer::kBoundsChangeDirection_None; | |
| 87 switch (window_component) { | |
| 88 case HTTOPLEFT: | |
| 89 case HTBOTTOMRIGHT: | |
| 90 case HTGROWBOX: | |
| 91 case HTCAPTION: | |
| 92 pos_change_direction |= | |
| 93 WindowResizer::kBoundsChangeDirection_Horizontal | | |
| 94 WindowResizer::kBoundsChangeDirection_Vertical; | |
| 95 break; | |
| 96 case HTTOP: | |
| 97 case HTTOPRIGHT: | |
| 98 case HTBOTTOM: | |
| 99 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical; | |
| 100 break; | |
| 101 case HTBOTTOMLEFT: | |
| 102 case HTRIGHT: | |
| 103 case HTLEFT: | |
| 104 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal; | |
| 105 break; | |
| 106 default: | |
| 107 break; | |
| 108 } | |
| 109 return pos_change_direction; | |
| 110 } | |
| 111 | |
| 112 gfx::Rect WindowResizer::CalculateBoundsForDrag( | |
| 113 const gfx::Point& passed_location) { | |
| 114 if (!details().is_resizable) | |
| 115 return details().initial_bounds_in_parent; | |
| 116 | |
| 117 gfx::Point location = passed_location; | |
| 118 int delta_x = location.x() - details().initial_location_in_parent.x(); | |
| 119 int delta_y = location.y() - details().initial_location_in_parent.y(); | |
| 120 | |
| 121 AdjustDeltaForTouchResize(&delta_x, &delta_y); | |
| 122 | |
| 123 // The minimize size constraint may limit how much we change the window | |
| 124 // position. For example, dragging the left edge to the right should stop | |
| 125 // repositioning the window when the minimize size is reached. | |
| 126 gfx::Size size = GetSizeForDrag(&delta_x, &delta_y); | |
| 127 gfx::Point origin = GetOriginForDrag(delta_x, delta_y); | |
| 128 gfx::Rect new_bounds(origin, size); | |
| 129 | |
| 130 // Sizing has to keep the result on the screen. Note that this correction | |
| 131 // has to come first since it might have an impact on the origin as well as | |
| 132 // on the size. | |
| 133 if (details().bounds_change & kBoundsChange_Resizes) { | |
| 134 gfx::Rect work_area = GetTarget()->GetDisplayNearestWindow().work_area(); | |
| 135 DockedWindowLayoutManager* dock_layout = | |
| 136 DockedWindowLayoutManager::Get(GetTarget()); | |
| 137 | |
| 138 work_area.Union(dock_layout->docked_bounds()); | |
| 139 work_area = GetTarget()->GetParent()->ConvertRectFromScreen(work_area); | |
| 140 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) { | |
| 141 if (IsRightEdge(details().window_component) && | |
| 142 new_bounds.right() < work_area.x() + wm::kMinimumOnScreenArea) { | |
| 143 int delta = | |
| 144 work_area.x() + wm::kMinimumOnScreenArea - new_bounds.right(); | |
| 145 new_bounds.set_width(new_bounds.width() + delta); | |
| 146 } else if (new_bounds.x() > | |
| 147 work_area.right() - wm::kMinimumOnScreenArea) { | |
| 148 int width = | |
| 149 new_bounds.right() - work_area.right() + wm::kMinimumOnScreenArea; | |
| 150 new_bounds.set_x(work_area.right() - wm::kMinimumOnScreenArea); | |
| 151 new_bounds.set_width(width); | |
| 152 } | |
| 153 } | |
| 154 if (details().size_change_direction & kBoundsChangeDirection_Vertical) { | |
| 155 if (!IsBottomEdge(details().window_component) && | |
| 156 new_bounds.y() > work_area.bottom() - wm::kMinimumOnScreenArea) { | |
| 157 int height = | |
| 158 new_bounds.bottom() - work_area.bottom() + wm::kMinimumOnScreenArea; | |
| 159 new_bounds.set_y(work_area.bottom() - wm::kMinimumOnScreenArea); | |
| 160 new_bounds.set_height(height); | |
| 161 } else if (details().window_component == HTBOTTOM || | |
| 162 details().window_component == HTBOTTOMRIGHT || | |
| 163 details().window_component == HTBOTTOMLEFT) { | |
| 164 // Update bottom edge to stay in the work area when we are resizing | |
| 165 // by dragging the bottom edge or corners. | |
| 166 if (new_bounds.bottom() > work_area.bottom()) | |
| 167 new_bounds.Inset(0, 0, 0, | |
| 168 new_bounds.bottom() - work_area.bottom()); | |
| 169 } | |
| 170 } | |
| 171 if (details().bounds_change & kBoundsChange_Repositions && | |
| 172 new_bounds.y() < 0) { | |
| 173 int delta = new_bounds.y(); | |
| 174 new_bounds.set_y(0); | |
| 175 new_bounds.set_height(new_bounds.height() + delta); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 if (details().bounds_change & kBoundsChange_Repositions) { | |
| 180 // When we might want to reposition a window which is also restored to its | |
| 181 // previous size, to keep the cursor within the dragged window. | |
| 182 if (!details().restore_bounds.IsEmpty()) { | |
| 183 // However - it is not desirable to change the origin if the window would | |
| 184 // be still hit by the cursor. | |
| 185 if (details().initial_location_in_parent.x() > | |
| 186 details().initial_bounds_in_parent.x() + | |
| 187 details().restore_bounds.width()) | |
| 188 new_bounds.set_x(location.x() - details().restore_bounds.width() / 2); | |
| 189 } | |
| 190 | |
| 191 // Make sure that |new_bounds| doesn't leave any of the displays. Note that | |
| 192 // the |work_area| above isn't good for this check since it is the work area | |
| 193 // for the current display but the window can move to a different one. | |
| 194 wm::WmWindow* parent = GetTarget()->GetParent(); | |
| 195 gfx::Point passed_location_in_screen( | |
| 196 parent->ConvertPointToScreen(passed_location)); | |
| 197 gfx::Rect near_passed_location(passed_location_in_screen, gfx::Size()); | |
| 198 // Use a pointer location (matching the logic in DragWindowResizer) to | |
| 199 // calculate the target display after the drag. | |
| 200 const gfx::Display& display = | |
| 201 gfx::Screen::GetScreen()->GetDisplayMatching(near_passed_location); | |
| 202 DockedWindowLayoutManager* dock_layout = DockedWindowLayoutManager::Get( | |
| 203 wm::GetRootWindowMatching(near_passed_location)); | |
| 204 | |
| 205 gfx::Rect screen_work_area = display.work_area(); | |
| 206 screen_work_area.Union(dock_layout->docked_bounds()); | |
| 207 screen_work_area.Inset(wm::kMinimumOnScreenArea, 0); | |
| 208 gfx::Rect new_bounds_in_screen = parent->ConvertRectToScreen(new_bounds); | |
| 209 if (!screen_work_area.Intersects(new_bounds_in_screen)) { | |
| 210 // Make sure that the x origin does not leave the current display. | |
| 211 new_bounds_in_screen.set_x( | |
| 212 std::max(screen_work_area.x() - new_bounds.width(), | |
| 213 std::min(screen_work_area.right(), | |
| 214 new_bounds_in_screen.x()))); | |
| 215 new_bounds = parent->ConvertRectFromScreen(new_bounds_in_screen); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 return new_bounds; | |
| 220 } | |
| 221 | |
| 222 // static | |
| 223 bool WindowResizer::IsBottomEdge(int window_component) { | |
| 224 return window_component == HTBOTTOMLEFT || | |
| 225 window_component == HTBOTTOM || | |
| 226 window_component == HTBOTTOMRIGHT || | |
| 227 window_component == HTGROWBOX; | |
| 228 } | |
| 229 | |
| 230 void WindowResizer::AdjustDeltaForTouchResize(int* delta_x, int* delta_y) { | |
| 231 if (details().source != aura::client::WINDOW_MOVE_SOURCE_TOUCH || | |
| 232 !(details().bounds_change & kBoundsChange_Resizes)) | |
| 233 return; | |
| 234 | |
| 235 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) { | |
| 236 if (IsRightEdge(details().window_component)) { | |
| 237 *delta_x += details().initial_location_in_parent.x() - | |
| 238 details().initial_bounds_in_parent.right(); | |
| 239 } else { | |
| 240 *delta_x += details().initial_location_in_parent.x() - | |
| 241 details().initial_bounds_in_parent.x(); | |
| 242 } | |
| 243 } | |
| 244 if (details().size_change_direction & kBoundsChangeDirection_Vertical) { | |
| 245 if (IsBottomEdge(details().window_component)) { | |
| 246 *delta_y += details().initial_location_in_parent.y() - | |
| 247 details().initial_bounds_in_parent.bottom(); | |
| 248 } else { | |
| 249 *delta_y += details().initial_location_in_parent.y() - | |
| 250 details().initial_bounds_in_parent.y(); | |
| 251 } | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 gfx::Point WindowResizer::GetOriginForDrag(int delta_x, int delta_y) { | |
| 256 gfx::Point origin = details().initial_bounds_in_parent.origin(); | |
| 257 if (details().bounds_change & kBoundsChange_Repositions) { | |
| 258 int pos_change_direction = GetPositionChangeDirectionForWindowComponent( | |
| 259 details().window_component); | |
| 260 if (pos_change_direction & kBoundsChangeDirection_Horizontal) | |
| 261 origin.Offset(delta_x, 0); | |
| 262 if (pos_change_direction & kBoundsChangeDirection_Vertical) | |
| 263 origin.Offset(0, delta_y); | |
| 264 } | |
| 265 return origin; | |
| 266 } | |
| 267 | |
| 268 gfx::Size WindowResizer::GetSizeForDrag(int* delta_x, int* delta_y) { | |
| 269 gfx::Size size = details().initial_bounds_in_parent.size(); | |
| 270 if (details().bounds_change & kBoundsChange_Resizes) { | |
| 271 gfx::Size min_size = GetTarget()->GetMinimumSize(); | |
| 272 size.SetSize(GetWidthForDrag(min_size.width(), delta_x), | |
| 273 GetHeightForDrag(min_size.height(), delta_y)); | |
| 274 } else if (!details().restore_bounds.IsEmpty()) { | |
| 275 size = details().restore_bounds.size(); | |
| 276 } | |
| 277 return size; | |
| 278 } | |
| 279 | |
| 280 int WindowResizer::GetWidthForDrag(int min_width, int* delta_x) { | |
| 281 int width = details().initial_bounds_in_parent.width(); | |
| 282 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) { | |
| 283 // Along the right edge, positive delta_x increases the window size. | |
| 284 int x_multiplier = IsRightEdge(details().window_component) ? 1 : -1; | |
| 285 width += x_multiplier * (*delta_x); | |
| 286 | |
| 287 // Ensure we don't shrink past the minimum width and clamp delta_x | |
| 288 // for the window origin computation. | |
| 289 if (width < min_width) { | |
| 290 width = min_width; | |
| 291 *delta_x = -x_multiplier * (details().initial_bounds_in_parent.width() - | |
| 292 min_width); | |
| 293 } | |
| 294 | |
| 295 // And don't let the window go bigger than the display. | |
| 296 int max_width = GetTarget()->GetDisplayNearestWindow().bounds().width(); | |
| 297 gfx::Size max_size = GetTarget()->GetMaximumSize(); | |
| 298 if (max_size.width() != 0) | |
| 299 max_width = std::min(max_width, max_size.width()); | |
| 300 if (width > max_width) { | |
| 301 width = max_width; | |
| 302 *delta_x = -x_multiplier * (details().initial_bounds_in_parent.width() - | |
| 303 max_width); | |
| 304 } | |
| 305 } | |
| 306 return width; | |
| 307 } | |
| 308 | |
| 309 int WindowResizer::GetHeightForDrag(int min_height, int* delta_y) { | |
| 310 int height = details().initial_bounds_in_parent.height(); | |
| 311 if (details().size_change_direction & kBoundsChangeDirection_Vertical) { | |
| 312 // Along the bottom edge, positive delta_y increases the window size. | |
| 313 int y_multiplier = IsBottomEdge(details().window_component) ? 1 : -1; | |
| 314 height += y_multiplier * (*delta_y); | |
| 315 | |
| 316 // Ensure we don't shrink past the minimum height and clamp delta_y | |
| 317 // for the window origin computation. | |
| 318 if (height < min_height) { | |
| 319 height = min_height; | |
| 320 *delta_y = -y_multiplier * (details().initial_bounds_in_parent.height() - | |
| 321 min_height); | |
| 322 } | |
| 323 | |
| 324 // And don't let the window go bigger than the display. | |
| 325 int max_height = GetTarget()->GetDisplayNearestWindow().bounds().height(); | |
| 326 gfx::Size max_size = GetTarget()->GetMaximumSize(); | |
| 327 if (max_size.height() != 0) | |
| 328 max_height = std::min(max_height, max_size.height()); | |
| 329 if (height > max_height) { | |
| 330 height = max_height; | |
| 331 *delta_y = -y_multiplier * (details().initial_bounds_in_parent.height() - | |
| 332 max_height); | |
| 333 } | |
| 334 } | |
| 335 return height; | |
| 336 } | |
| 337 | |
| 338 } // namespace ash | |
| OLD | NEW |