Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/wm/window_resizer.h" | 5 #include "ash/wm/window_resizer.h" |
| 6 | 6 |
| 7 #include "ash/wm/root_window_finder.h" | 7 #include "ash/wm/root_window_finder.h" |
| 8 #include "ash/wm/window_positioning_utils.h" | 8 #include "ash/wm/window_positioning_utils.h" |
| 9 #include "ash/wm/window_state.h" | 9 #include "ash/wm/window_state.h" |
| 10 #include "ash/wm_window.h" | 10 #include "ash/wm_window.h" |
| 11 #include "ui/aura/window.h" | |
| 12 #include "ui/aura/window_delegate.h" | |
| 11 #include "ui/base/hit_test.h" | 13 #include "ui/base/hit_test.h" |
| 12 #include "ui/base/ui_base_types.h" | 14 #include "ui/base/ui_base_types.h" |
| 13 #include "ui/display/display.h" | 15 #include "ui/display/display.h" |
| 14 #include "ui/display/screen.h" | 16 #include "ui/display/screen.h" |
| 15 #include "ui/gfx/geometry/rect.h" | 17 #include "ui/gfx/geometry/rect.h" |
| 18 #include "ui/wm/core/coordinate_conversion.h" | |
| 16 | 19 |
| 17 namespace ash { | 20 namespace ash { |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 // Returns true for resize components along the right edge, where a drag in | 24 // Returns true for resize components along the right edge, where a drag in |
| 22 // positive x will make the window larger. | 25 // positive x will make the window larger. |
| 23 bool IsRightEdge(int window_component) { | 26 bool IsRightEdge(int window_component) { |
| 24 return window_component == HTTOPRIGHT || window_component == HTRIGHT || | 27 return window_component == HTTOPRIGHT || window_component == HTRIGHT || |
| 25 window_component == HTBOTTOMRIGHT || window_component == HTGROWBOX; | 28 window_component == HTBOTTOMRIGHT || window_component == HTGROWBOX; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 case HTRIGHT: | 99 case HTRIGHT: |
| 97 case HTLEFT: | 100 case HTLEFT: |
| 98 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal; | 101 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal; |
| 99 break; | 102 break; |
| 100 default: | 103 default: |
| 101 break; | 104 break; |
| 102 } | 105 } |
| 103 return pos_change_direction; | 106 return pos_change_direction; |
| 104 } | 107 } |
| 105 | 108 |
| 109 aura::Window* WindowResizer::GetTarget() const { | |
| 110 return window_state_ ? window_state_->window()->aura_window() : nullptr; | |
| 111 } | |
| 112 | |
| 106 gfx::Rect WindowResizer::CalculateBoundsForDrag( | 113 gfx::Rect WindowResizer::CalculateBoundsForDrag( |
| 107 const gfx::Point& passed_location) { | 114 const gfx::Point& passed_location) { |
| 108 if (!details().is_resizable) | 115 if (!details().is_resizable) |
| 109 return details().initial_bounds_in_parent; | 116 return details().initial_bounds_in_parent; |
| 110 | 117 |
| 111 gfx::Point location = passed_location; | 118 gfx::Point location = passed_location; |
| 112 int delta_x = location.x() - details().initial_location_in_parent.x(); | 119 int delta_x = location.x() - details().initial_location_in_parent.x(); |
| 113 int delta_y = location.y() - details().initial_location_in_parent.y(); | 120 int delta_y = location.y() - details().initial_location_in_parent.y(); |
| 114 | 121 |
| 115 AdjustDeltaForTouchResize(&delta_x, &delta_y); | 122 AdjustDeltaForTouchResize(&delta_x, &delta_y); |
| 116 | 123 |
| 117 // The minimize size constraint may limit how much we change the window | 124 // The minimize size constraint may limit how much we change the window |
| 118 // position. For example, dragging the left edge to the right should stop | 125 // position. For example, dragging the left edge to the right should stop |
| 119 // repositioning the window when the minimize size is reached. | 126 // repositioning the window when the minimize size is reached. |
| 120 gfx::Size size = GetSizeForDrag(&delta_x, &delta_y); | 127 gfx::Size size = GetSizeForDrag(&delta_x, &delta_y); |
| 121 gfx::Point origin = GetOriginForDrag(delta_x, delta_y); | 128 gfx::Point origin = GetOriginForDrag(delta_x, delta_y); |
| 122 gfx::Rect new_bounds(origin, size); | 129 gfx::Rect new_bounds(origin, size); |
| 123 | 130 |
| 124 // Sizing has to keep the result on the screen. Note that this correction | 131 // Sizing has to keep the result on the screen. Note that this correction |
| 125 // has to come first since it might have an impact on the origin as well as | 132 // has to come first since it might have an impact on the origin as well as |
| 126 // on the size. | 133 // on the size. |
| 127 if (details().bounds_change & kBoundsChange_Resizes) { | 134 if (details().bounds_change & kBoundsChange_Resizes) { |
| 128 gfx::Rect work_area = GetTarget()->GetDisplayNearestWindow().work_area(); | 135 gfx::Rect work_area = display::Screen::GetScreen() |
| 129 work_area = GetTarget()->GetParent()->ConvertRectFromScreen(work_area); | 136 ->GetDisplayNearestWindow(GetTarget()) |
| 137 .work_area(); | |
| 138 ::wm::ConvertRectFromScreen(GetTarget()->parent(), &work_area); | |
| 130 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) { | 139 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) { |
| 131 if (IsRightEdge(details().window_component) && | 140 if (IsRightEdge(details().window_component) && |
| 132 new_bounds.right() < work_area.x() + wm::kMinimumOnScreenArea) { | 141 new_bounds.right() < work_area.x() + wm::kMinimumOnScreenArea) { |
| 133 int delta = | 142 int delta = |
| 134 work_area.x() + wm::kMinimumOnScreenArea - new_bounds.right(); | 143 work_area.x() + wm::kMinimumOnScreenArea - new_bounds.right(); |
| 135 new_bounds.set_width(new_bounds.width() + delta); | 144 new_bounds.set_width(new_bounds.width() + delta); |
| 136 } else if (new_bounds.x() > | 145 } else if (new_bounds.x() > |
| 137 work_area.right() - wm::kMinimumOnScreenArea) { | 146 work_area.right() - wm::kMinimumOnScreenArea) { |
| 138 int width = | 147 int width = |
| 139 new_bounds.right() - work_area.right() + wm::kMinimumOnScreenArea; | 148 new_bounds.right() - work_area.right() + wm::kMinimumOnScreenArea; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 // be still hit by the cursor. | 182 // be still hit by the cursor. |
| 174 if (details().initial_location_in_parent.x() > | 183 if (details().initial_location_in_parent.x() > |
| 175 details().initial_bounds_in_parent.x() + | 184 details().initial_bounds_in_parent.x() + |
| 176 details().restore_bounds.width()) | 185 details().restore_bounds.width()) |
| 177 new_bounds.set_x(location.x() - details().restore_bounds.width() / 2); | 186 new_bounds.set_x(location.x() - details().restore_bounds.width() / 2); |
| 178 } | 187 } |
| 179 | 188 |
| 180 // Make sure that |new_bounds| doesn't leave any of the displays. Note that | 189 // Make sure that |new_bounds| doesn't leave any of the displays. Note that |
| 181 // the |work_area| above isn't good for this check since it is the work area | 190 // the |work_area| above isn't good for this check since it is the work area |
| 182 // for the current display but the window can move to a different one. | 191 // for the current display but the window can move to a different one. |
| 183 WmWindow* parent = GetTarget()->GetParent(); | 192 aura::Window* parent = GetTarget()->parent(); |
| 184 gfx::Point passed_location_in_screen( | 193 gfx::Point passed_location_in_screen(passed_location); |
| 185 parent->ConvertPointToScreen(passed_location)); | 194 ::wm::ConvertPointToScreen(parent, &passed_location_in_screen); |
| 186 gfx::Rect near_passed_location(passed_location_in_screen, gfx::Size()); | 195 gfx::Rect near_passed_location(passed_location_in_screen, gfx::Size()); |
| 187 // Use a pointer location (matching the logic in DragWindowResizer) to | 196 // Use a pointer location (matching the logic in DragWindowResizer) to |
| 188 // calculate the target display after the drag. | 197 // calculate the target display after the drag. |
| 189 const display::Display& display = | 198 const display::Display& display = |
| 190 display::Screen::GetScreen()->GetDisplayMatching(near_passed_location); | 199 display::Screen::GetScreen()->GetDisplayMatching(near_passed_location); |
| 191 gfx::Rect screen_work_area = display.work_area(); | 200 gfx::Rect screen_work_area = display.work_area(); |
| 192 screen_work_area.Inset(wm::kMinimumOnScreenArea, 0); | 201 screen_work_area.Inset(wm::kMinimumOnScreenArea, 0); |
| 193 gfx::Rect new_bounds_in_screen = parent->ConvertRectToScreen(new_bounds); | 202 gfx::Rect new_bounds_in_screen(new_bounds); |
| 203 ::wm::ConvertRectToScreen(parent, &new_bounds_in_screen); | |
| 194 if (!screen_work_area.Intersects(new_bounds_in_screen)) { | 204 if (!screen_work_area.Intersects(new_bounds_in_screen)) { |
| 195 // Make sure that the x origin does not leave the current display. | 205 // Make sure that the x origin does not leave the current display. |
| 196 new_bounds_in_screen.set_x(std::max( | 206 new_bounds_in_screen.set_x(std::max( |
| 197 screen_work_area.x() - new_bounds.width(), | 207 screen_work_area.x() - new_bounds.width(), |
| 198 std::min(screen_work_area.right(), new_bounds_in_screen.x()))); | 208 std::min(screen_work_area.right(), new_bounds_in_screen.x()))); |
| 199 new_bounds = parent->ConvertRectFromScreen(new_bounds_in_screen); | 209 new_bounds = new_bounds_in_screen; |
| 210 ::wm::ConvertRectFromScreen(parent, &new_bounds); | |
| 200 } | 211 } |
| 201 } | 212 } |
| 202 | 213 |
| 203 return new_bounds; | 214 return new_bounds; |
| 204 } | 215 } |
| 205 | 216 |
| 206 // static | 217 // static |
| 207 bool WindowResizer::IsBottomEdge(int window_component) { | 218 bool WindowResizer::IsBottomEdge(int window_component) { |
| 208 return window_component == HTBOTTOMLEFT || window_component == HTBOTTOM || | 219 return window_component == HTBOTTOMLEFT || window_component == HTBOTTOM || |
| 209 window_component == HTBOTTOMRIGHT || window_component == HTGROWBOX; | 220 window_component == HTBOTTOMRIGHT || window_component == HTGROWBOX; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 origin.Offset(delta_x, 0); | 254 origin.Offset(delta_x, 0); |
| 244 if (pos_change_direction & kBoundsChangeDirection_Vertical) | 255 if (pos_change_direction & kBoundsChangeDirection_Vertical) |
| 245 origin.Offset(0, delta_y); | 256 origin.Offset(0, delta_y); |
| 246 } | 257 } |
| 247 return origin; | 258 return origin; |
| 248 } | 259 } |
| 249 | 260 |
| 250 gfx::Size WindowResizer::GetSizeForDrag(int* delta_x, int* delta_y) { | 261 gfx::Size WindowResizer::GetSizeForDrag(int* delta_x, int* delta_y) { |
| 251 gfx::Size size = details().initial_bounds_in_parent.size(); | 262 gfx::Size size = details().initial_bounds_in_parent.size(); |
| 252 if (details().bounds_change & kBoundsChange_Resizes) { | 263 if (details().bounds_change & kBoundsChange_Resizes) { |
| 253 gfx::Size min_size = GetTarget()->GetMinimumSize(); | 264 gfx::Size min_size = GetTarget()->delegate()->GetMinimumSize(); |
|
msw
2017/05/22 19:26:12
ditto test q, and could delegate() be null here? (
sky
2017/05/22 19:50:59
I was assuming this code would never hit if not re
| |
| 254 size.SetSize(GetWidthForDrag(min_size.width(), delta_x), | 265 size.SetSize(GetWidthForDrag(min_size.width(), delta_x), |
| 255 GetHeightForDrag(min_size.height(), delta_y)); | 266 GetHeightForDrag(min_size.height(), delta_y)); |
| 256 } else if (!details().restore_bounds.IsEmpty()) { | 267 } else if (!details().restore_bounds.IsEmpty()) { |
| 257 size = details().restore_bounds.size(); | 268 size = details().restore_bounds.size(); |
| 258 } | 269 } |
| 259 return size; | 270 return size; |
| 260 } | 271 } |
| 261 | 272 |
| 262 int WindowResizer::GetWidthForDrag(int min_width, int* delta_x) { | 273 int WindowResizer::GetWidthForDrag(int min_width, int* delta_x) { |
| 263 int width = details().initial_bounds_in_parent.width(); | 274 int width = details().initial_bounds_in_parent.width(); |
| 264 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) { | 275 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) { |
| 265 // Along the right edge, positive delta_x increases the window size. | 276 // Along the right edge, positive delta_x increases the window size. |
| 266 int x_multiplier = IsRightEdge(details().window_component) ? 1 : -1; | 277 int x_multiplier = IsRightEdge(details().window_component) ? 1 : -1; |
| 267 width += x_multiplier * (*delta_x); | 278 width += x_multiplier * (*delta_x); |
| 268 | 279 |
| 269 // Ensure we don't shrink past the minimum width and clamp delta_x | 280 // Ensure we don't shrink past the minimum width and clamp delta_x |
| 270 // for the window origin computation. | 281 // for the window origin computation. |
| 271 if (width < min_width) { | 282 if (width < min_width) { |
| 272 width = min_width; | 283 width = min_width; |
| 273 *delta_x = -x_multiplier * | 284 *delta_x = -x_multiplier * |
| 274 (details().initial_bounds_in_parent.width() - min_width); | 285 (details().initial_bounds_in_parent.width() - min_width); |
| 275 } | 286 } |
| 276 | 287 |
| 277 // And don't let the window go bigger than the display. | 288 // And don't let the window go bigger than the display. |
| 278 int max_width = GetTarget()->GetDisplayNearestWindow().bounds().width(); | 289 int max_width = display::Screen::GetScreen() |
| 279 gfx::Size max_size = GetTarget()->GetMaximumSize(); | 290 ->GetDisplayNearestWindow(GetTarget()) |
| 291 .bounds() | |
| 292 .width(); | |
| 293 gfx::Size max_size = GetTarget()->delegate()->GetMaximumSize(); | |
| 280 if (max_size.width() != 0) | 294 if (max_size.width() != 0) |
| 281 max_width = std::min(max_width, max_size.width()); | 295 max_width = std::min(max_width, max_size.width()); |
| 282 if (width > max_width) { | 296 if (width > max_width) { |
| 283 width = max_width; | 297 width = max_width; |
| 284 *delta_x = -x_multiplier * | 298 *delta_x = -x_multiplier * |
| 285 (details().initial_bounds_in_parent.width() - max_width); | 299 (details().initial_bounds_in_parent.width() - max_width); |
| 286 } | 300 } |
| 287 } | 301 } |
| 288 return width; | 302 return width; |
| 289 } | 303 } |
| 290 | 304 |
| 291 int WindowResizer::GetHeightForDrag(int min_height, int* delta_y) { | 305 int WindowResizer::GetHeightForDrag(int min_height, int* delta_y) { |
| 292 int height = details().initial_bounds_in_parent.height(); | 306 int height = details().initial_bounds_in_parent.height(); |
| 293 if (details().size_change_direction & kBoundsChangeDirection_Vertical) { | 307 if (details().size_change_direction & kBoundsChangeDirection_Vertical) { |
| 294 // Along the bottom edge, positive delta_y increases the window size. | 308 // Along the bottom edge, positive delta_y increases the window size. |
| 295 int y_multiplier = IsBottomEdge(details().window_component) ? 1 : -1; | 309 int y_multiplier = IsBottomEdge(details().window_component) ? 1 : -1; |
| 296 height += y_multiplier * (*delta_y); | 310 height += y_multiplier * (*delta_y); |
| 297 | 311 |
| 298 // Ensure we don't shrink past the minimum height and clamp delta_y | 312 // Ensure we don't shrink past the minimum height and clamp delta_y |
| 299 // for the window origin computation. | 313 // for the window origin computation. |
| 300 if (height < min_height) { | 314 if (height < min_height) { |
| 301 height = min_height; | 315 height = min_height; |
| 302 *delta_y = -y_multiplier * | 316 *delta_y = -y_multiplier * |
| 303 (details().initial_bounds_in_parent.height() - min_height); | 317 (details().initial_bounds_in_parent.height() - min_height); |
| 304 } | 318 } |
| 305 | 319 |
| 306 // And don't let the window go bigger than the display. | 320 // And don't let the window go bigger than the display. |
| 307 int max_height = GetTarget()->GetDisplayNearestWindow().bounds().height(); | 321 int max_height = display::Screen::GetScreen() |
| 308 gfx::Size max_size = GetTarget()->GetMaximumSize(); | 322 ->GetDisplayNearestWindow(GetTarget()) |
| 323 .bounds() | |
| 324 .height(); | |
| 325 gfx::Size max_size = GetTarget()->delegate()->GetMaximumSize(); | |
| 309 if (max_size.height() != 0) | 326 if (max_size.height() != 0) |
| 310 max_height = std::min(max_height, max_size.height()); | 327 max_height = std::min(max_height, max_size.height()); |
| 311 if (height > max_height) { | 328 if (height > max_height) { |
| 312 height = max_height; | 329 height = max_height; |
| 313 *delta_y = -y_multiplier * | 330 *delta_y = -y_multiplier * |
| 314 (details().initial_bounds_in_parent.height() - max_height); | 331 (details().initial_bounds_in_parent.height() - max_height); |
| 315 } | 332 } |
| 316 } | 333 } |
| 317 return height; | 334 return height; |
| 318 } | 335 } |
| 319 | 336 |
| 320 } // namespace ash | 337 } // namespace ash |
| OLD | NEW |