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