Commit 3e5e262e authored by Wojciech Dzierżanowski's avatar Wojciech Dzierżanowski Committed by Chromium LUCI CQ

Move WindowResizeUtils to //ui/gfx/geometry

//components/remote_cocoa will start using these utilities in a
subsequent CL.

Bug: 1123249
Change-Id: I3a19b0233e59d7be6782e0c28b596a9a368864f5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2563669Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Wojciech Dzierżanowski <wdzierzanowski@opera.com>
Cr-Commit-Position: refs/heads/master@{#832397}
parent 39f4e4a5
......@@ -22,9 +22,9 @@
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/resize_utils.h"
#include "ui/gfx/presentation_feedback.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/window_resize_utils.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace ash {
......@@ -39,28 +39,29 @@ bool IsRightEdge(int window_component) {
window_component == HTBOTTOMRIGHT || window_component == HTGROWBOX;
}
// Convert |window_component| to the HitTest used in views::WindowResizeUtils.
views::HitTest GetWindowResizeHitTest(int window_component) {
// Convert |window_component| to the ResizeEdge used in
// gfx::SizeRectToAspectRatio().
gfx::ResizeEdge GetWindowResizeEdge(int window_component) {
switch (window_component) {
case HTBOTTOM:
return views::HitTest::kBottom;
return gfx::ResizeEdge::kBottom;
case HTTOP:
return views::HitTest::kTop;
return gfx::ResizeEdge::kTop;
case HTLEFT:
return views::HitTest::kLeft;
return gfx::ResizeEdge::kLeft;
case HTRIGHT:
return views::HitTest::kRight;
return gfx::ResizeEdge::kRight;
case HTTOPLEFT:
return views::HitTest::kTopLeft;
return gfx::ResizeEdge::kTopLeft;
case HTTOPRIGHT:
return views::HitTest::kTopRight;
return gfx::ResizeEdge::kTopRight;
case HTBOTTOMLEFT:
return views::HitTest::kBottomLeft;
return gfx::ResizeEdge::kBottomLeft;
case HTBOTTOMRIGHT:
return views::HitTest::kBottomRight;
return gfx::ResizeEdge::kBottomRight;
default:
NOTREACHED();
return views::HitTest::kBottomRight;
return gfx::ResizeEdge::kBottomRight;
}
}
......@@ -468,11 +469,9 @@ void WindowResizer::CalculateBoundsWithAspectRatio(float aspect_ratio,
DCHECK(!min_size.IsEmpty());
DCHECK(!max_size.IsEmpty());
views::WindowResizeUtils::SizeMinMaxToAspectRatio(aspect_ratio, &min_size,
&max_size);
views::WindowResizeUtils::SizeRectToAspectRatio(
GetWindowResizeHitTest(details().window_component), aspect_ratio,
min_size, max_size, new_bounds);
gfx::SizeMinMaxToAspectRatio(aspect_ratio, &min_size, &max_size);
gfx::SizeRectToAspectRatio(GetWindowResizeEdge(details().window_component),
aspect_ratio, min_size, max_size, new_bounds);
}
} // namespace ash
......@@ -4164,6 +4164,7 @@ static_library("ui") {
"//device/vr/buildflags:buildflags",
"//services/media_session/public/mojom",
"//ui/base/dragdrop/mojom:mojom_shared",
"//ui/gfx/geometry",
"//ui/views:buildflags",
]
......
......@@ -35,12 +35,12 @@
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/resize_utils.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/vector_icons.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/non_client_view.h"
#include "ui/views/window/window_resize_utils.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/public/cpp/ash_features.h"
......@@ -290,30 +290,29 @@ gfx::Rect OverlayWindowViews::CalculateAndUpdateWindowBounds() {
WindowQuadrant quadrant =
GetCurrentWindowQuadrant(GetBounds(), controller_);
views::HitTest hit_test;
gfx::ResizeEdge resize_edge;
switch (quadrant) {
case OverlayWindowViews::WindowQuadrant::kBottomRight:
hit_test = views::HitTest::kTopLeft;
resize_edge = gfx::ResizeEdge::kTopLeft;
break;
case OverlayWindowViews::WindowQuadrant::kBottomLeft:
hit_test = views::HitTest::kTopRight;
resize_edge = gfx::ResizeEdge::kTopRight;
break;
case OverlayWindowViews::WindowQuadrant::kTopLeft:
hit_test = views::HitTest::kBottomRight;
resize_edge = gfx::ResizeEdge::kBottomRight;
break;
case OverlayWindowViews::WindowQuadrant::kTopRight:
hit_test = views::HitTest::kBottomLeft;
resize_edge = gfx::ResizeEdge::kBottomLeft;
break;
}
// Update the window size to adhere to the aspect ratio.
gfx::Size min_size = min_size_;
gfx::Size max_size = max_size_;
views::WindowResizeUtils::SizeMinMaxToAspectRatio(aspect_ratio, &min_size,
&max_size);
gfx::SizeMinMaxToAspectRatio(aspect_ratio, &min_size, &max_size);
gfx::Rect window_rect(GetBounds().origin(), window_size);
views::WindowResizeUtils::SizeRectToAspectRatio(
hit_test, aspect_ratio, min_size, max_size, &window_rect);
gfx::SizeRectToAspectRatio(resize_edge, aspect_ratio, min_size, max_size,
&window_rect);
window_size.SetSize(window_rect.width(), window_rect.height());
UpdateLayerBoundsWithLetterboxing(window_size);
......
......@@ -760,6 +760,7 @@ test("gfx_unittests") {
"geometry/quad_unittest.cc",
"geometry/quaternion_unittest.cc",
"geometry/rect_unittest.cc",
"geometry/resize_utils_unittest.cc",
"geometry/rounded_corners_f_unittest.cc",
"geometry/scroll_offset_unittest.cc",
"geometry/size_unittest.cc",
......
......@@ -41,6 +41,8 @@ component("geometry") {
"rect_conversions.h",
"rect_f.cc",
"rect_f.h",
"resize_utils.cc",
"resize_utils.h",
"rounded_corners_f.cc",
"rounded_corners_f.h",
"scroll_offset.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/window/window_resize_utils.h"
#include "ui/gfx/geometry/resize_utils.h"
#include <algorithm>
......@@ -10,12 +10,11 @@
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace views {
namespace gfx {
// static
void WindowResizeUtils::SizeMinMaxToAspectRatio(float aspect_ratio,
gfx::Size* min_window_size,
gfx::Size* max_window_size) {
void SizeMinMaxToAspectRatio(float aspect_ratio,
Size* min_window_size,
Size* max_window_size) {
DCHECK_GT(aspect_ratio, 0.0f);
// Calculate the height using the min-width and aspect ratio.
......@@ -42,21 +41,20 @@ void WindowResizeUtils::SizeMinMaxToAspectRatio(float aspect_ratio,
DCHECK_GE(max_window_size->height(), min_window_size->height());
}
// static
void WindowResizeUtils::SizeRectToAspectRatio(HitTest param,
float aspect_ratio,
const gfx::Size& min_window_size,
const gfx::Size& max_window_size,
gfx::Rect* rect) {
void SizeRectToAspectRatio(ResizeEdge resize_edge,
float aspect_ratio,
const Size& min_window_size,
const Size& max_window_size,
Rect* rect) {
DCHECK_GT(aspect_ratio, 0.0f);
DCHECK_GE(max_window_size.width(), min_window_size.width());
DCHECK_GE(max_window_size.height(), min_window_size.height());
float rect_width = 0.0;
float rect_height = 0.0;
if (param == HitTest::kLeft || param == HitTest::kRight ||
param == HitTest::kTopLeft ||
param == HitTest::kBottomLeft) { /* horizontal axis to pivot */
if (resize_edge == ResizeEdge::kLeft || resize_edge == ResizeEdge::kRight ||
resize_edge == ResizeEdge::kTopLeft ||
resize_edge == ResizeEdge::kBottomLeft) { /* horizontal axis to pivot */
rect_width = std::min(max_window_size.width(),
std::max(rect->width(), min_window_size.width()));
rect_height = rect_width / aspect_ratio;
......@@ -72,30 +70,30 @@ void WindowResizeUtils::SizeRectToAspectRatio(HitTest param,
int right = rect->right();
int bottom = rect->bottom();
switch (param) {
case HitTest::kRight:
case HitTest::kBottom:
switch (resize_edge) {
case ResizeEdge::kRight:
case ResizeEdge::kBottom:
right = rect_width + left;
bottom = top + rect_height;
break;
case HitTest::kTop:
case ResizeEdge::kTop:
right = rect_width + left;
top = bottom - rect_height;
break;
case HitTest::kLeft:
case HitTest::kTopLeft:
case ResizeEdge::kLeft:
case ResizeEdge::kTopLeft:
left = right - rect_width;
top = bottom - rect_height;
break;
case HitTest::kTopRight:
case ResizeEdge::kTopRight:
right = left + rect_width;
top = bottom - rect_height;
break;
case HitTest::kBottomLeft:
case ResizeEdge::kBottomLeft:
left = right - rect_width;
bottom = top + rect_height;
break;
case HitTest::kBottomRight:
case ResizeEdge::kBottomRight:
right = left + rect_width;
bottom = top + rect_height;
break;
......@@ -104,4 +102,4 @@ void WindowResizeUtils::SizeRectToAspectRatio(HitTest param,
rect->SetByBounds(left, top, right, bottom);
}
} // namespace views
} // namespace gfx
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_GFX_GEOMETRY_RESIZE_UTILS_H_
#define UI_GFX_GEOMETRY_RESIZE_UTILS_H_
#include "ui/gfx/geometry/geometry_export.h"
namespace gfx {
class Rect;
class Size;
enum class ResizeEdge {
kBottom,
kBottomLeft,
kBottomRight,
kLeft,
kRight,
kTop,
kTopLeft,
kTopRight
};
// Force the min and max window sizes to adhere to the aspect ratio.
// |aspect_ratio| must be valid and is found using width / height.
void GEOMETRY_EXPORT SizeMinMaxToAspectRatio(float aspect_ratio,
Size* min_window_size,
Size* max_window_size);
// Updates |rect| to adhere to the |aspect_ratio| of the window, if it has
// been set. |resize_edge| refers to the edge of the window being sized.
// |min_window_size| and |max_window_size| are expected to adhere to the
// given aspect ratio.
// |aspect_ratio| must be valid and is found using width / height.
// TODO(apacible): |max_window_size| is expected to be non-empty. Handle
// unconstrained max sizes and sizing when windows are maximized.
void GEOMETRY_EXPORT SizeRectToAspectRatio(ResizeEdge resize_edge,
float aspect_ratio,
const Size& min_window_size,
const Size& max_window_size,
Rect* rect);
} // namespace gfx
#endif // UI_GFX_GEOMETRY_RESIZE_UTILS_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/geometry/resize_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace gfx {
namespace {
// Aspect ratio is defined by width / height.
constexpr float kAspectRatioSquare = 1.0f;
constexpr float kAspectRatioHorizontal = 2.0f;
constexpr float kAspectRatioVertical = 0.5f;
const Size kMinSizeSquare = Size(10, 10);
const Size kMaxSizeSquare = Size(50, 50);
const Size kMinSizeHorizontal = Size(20, 10);
const Size kMaxSizeHorizontal = Size(50, 25);
const Size kMinSizeVertical = Size(10, 20);
const Size kMaxSizeVertical = Size(25, 50);
} // namespace
// Tests resizing of window with a 1:1 aspect ratio. This test also tests the
// 'pivot points' when resizing, i.e. the opposite side or corner of the
// window.
TEST(WindowResizeUtilsTest, SizeToSquareAspectRatio) {
// Size from the top of the window.
// |window_rect| within the bounds of kMinSizeSquare and kMaxSizeSquare.
Rect window_rect(100, 100, 15, 15);
SizeRectToAspectRatio(ResizeEdge::kTop, kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect, Rect(100, 100, 15, 15));
// Size from the bottom right corner of the window.
// |window_rect| smaller than kMinSizeSquare.
window_rect.SetRect(100, 100, 5, 5);
SizeRectToAspectRatio(ResizeEdge::kBottomRight, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect,
Rect(100, 100, kMinSizeSquare.width(), kMinSizeSquare.height()));
// Size from the top of the window.
// |window_rect| larger than kMaxSizeSquare.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kTop, kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect,
Rect(100, 150, kMaxSizeSquare.width(), kMaxSizeSquare.height()));
// Size from the bottom of the window.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kBottom, kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect,
Rect(100, 100, kMaxSizeSquare.width(), kMaxSizeSquare.height()));
// Size from the left of the window.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kLeft, kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect,
Rect(150, 150, kMaxSizeSquare.width(), kMaxSizeSquare.height()));
// Size from the right of the window.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kRight, kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect,
Rect(100, 100, kMaxSizeSquare.width(), kMaxSizeSquare.height()));
// Size from the top left corner of the window.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kTopLeft, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect,
Rect(150, 150, kMaxSizeSquare.width(), kMaxSizeSquare.height()));
// Size from the top right corner of the window.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kTopRight, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect,
Rect(100, 150, kMaxSizeSquare.width(), kMaxSizeSquare.height()));
// Size from the bottom left corner of the window.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kBottomLeft, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect,
Rect(150, 100, kMaxSizeSquare.width(), kMaxSizeSquare.height()));
}
// Tests the aspect ratio of the Rect adheres to the horizontal aspect
// ratio.
TEST(WindowResizeUtilsTest, SizeToHorizontalAspectRatio) {
// |window_rect| within bounds of kMinSizeHorizontal and kMaxSizeHorizontal.
Rect window_rect(100, 100, 20, 10);
SizeRectToAspectRatio(ResizeEdge::kTop, kAspectRatioHorizontal,
kMinSizeHorizontal, kMaxSizeHorizontal, &window_rect);
EXPECT_EQ(window_rect, Rect(100, 100, 20, 10));
// |window_rect| smaller than kMinSizeHorizontal.
window_rect.SetRect(100, 100, 5, 5);
SizeRectToAspectRatio(ResizeEdge::kBottomRight, kAspectRatioHorizontal,
kMinSizeHorizontal, kMaxSizeHorizontal, &window_rect);
EXPECT_EQ(window_rect, Rect(100, 100, kMinSizeHorizontal.width(),
kMinSizeHorizontal.height()));
// |window_rect| greater than kMaxSizeHorizontal.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kTop, kAspectRatioHorizontal,
kMinSizeHorizontal, kMaxSizeHorizontal, &window_rect);
EXPECT_EQ(window_rect, Rect(100, 175, kMaxSizeHorizontal.width(),
kMaxSizeHorizontal.height()));
}
// Tests the aspect ratio of the Rect adheres to the vertical aspect ratio.
TEST(WindowResizeUtilsTest, SizeToVerticalAspectRatio) {
// |window_rect| within bounds of kMinSizeVertical and kMaxSizeVertical.
Rect window_rect(100, 100, 10, 20);
SizeRectToAspectRatio(ResizeEdge::kBottomRight, kAspectRatioVertical,
kMinSizeVertical, kMaxSizeVertical, &window_rect);
EXPECT_EQ(window_rect, Rect(100, 100, 10, 20));
// |window_rect| smaller than kMinSizeVertical.
window_rect.SetRect(100, 100, 5, 5);
SizeRectToAspectRatio(ResizeEdge::kBottomRight, kAspectRatioVertical,
kMinSizeVertical, kMaxSizeVertical, &window_rect);
EXPECT_EQ(window_rect, Rect(100, 100, kMinSizeVertical.width(),
kMinSizeVertical.height()));
// |window_rect| greater than kMaxSizeVertical.
window_rect.SetRect(100, 100, 100, 100);
SizeRectToAspectRatio(ResizeEdge::kBottomRight, kAspectRatioVertical,
kMinSizeVertical, kMaxSizeVertical, &window_rect);
EXPECT_EQ(window_rect, Rect(100, 100, kMaxSizeVertical.width(),
kMaxSizeVertical.height()));
}
} // namespace gfx
......@@ -274,7 +274,6 @@ component("views") {
"window/native_frame_view.h",
"window/non_client_view.h",
"window/window_button_order_provider.h",
"window/window_resize_utils.h",
"window/window_resources.h",
"window/window_shape.h",
"word_lookup_client.h",
......@@ -460,7 +459,6 @@ component("views") {
"window/native_frame_view.cc",
"window/non_client_view.cc",
"window/window_button_order_provider.cc",
"window/window_resize_utils.cc",
"window/window_shape.cc",
]
......@@ -1153,7 +1151,6 @@ test("views_unittests") {
"window/frame_caption_button_unittest.cc",
"window/hit_test_utils_unittest.cc",
"window/non_client_view_unittest.cc",
"window/window_resize_utils_unittest.cc",
]
configs += [ "//build/config:precompiled_headers" ]
......
......@@ -55,6 +55,7 @@
#include "ui/events/win/system_event_state_lookup.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/resize_utils.h"
#include "ui/gfx/icon_util.h"
#include "ui/gfx/path_win.h"
#include "ui/gfx/win/hwnd_util.h"
......@@ -255,28 +256,28 @@ bool IsHitTestOnResizeHandle(LRESULT hittest) {
hittest == HTBOTTOMLEFT || hittest == HTBOTTOMRIGHT;
}
// Convert |param| to the HitTest used in WindowResizeUtils.
HitTest GetWindowResizeHitTest(UINT param) {
// Convert |param| to the gfx::ResizeEdge used in gfx::SizeRectToAspectRatio().
gfx::ResizeEdge GetWindowResizeEdge(UINT param) {
switch (param) {
case WMSZ_BOTTOM:
return HitTest::kBottom;
return gfx::ResizeEdge::kBottom;
case WMSZ_TOP:
return HitTest::kTop;
return gfx::ResizeEdge::kTop;
case WMSZ_LEFT:
return HitTest::kLeft;
return gfx::ResizeEdge::kLeft;
case WMSZ_RIGHT:
return HitTest::kRight;
return gfx::ResizeEdge::kRight;
case WMSZ_TOPLEFT:
return HitTest::kTopLeft;
return gfx::ResizeEdge::kTopLeft;
case WMSZ_TOPRIGHT:
return HitTest::kTopRight;
return gfx::ResizeEdge::kTopRight;
case WMSZ_BOTTOMLEFT:
return HitTest::kBottomLeft;
return gfx::ResizeEdge::kBottomLeft;
case WMSZ_BOTTOMRIGHT:
return HitTest::kBottomRight;
return gfx::ResizeEdge::kBottomRight;
default:
NOTREACHED();
return HitTest::kBottomRight;
return gfx::ResizeEdge::kBottomRight;
}
}
......@@ -3540,13 +3541,12 @@ void HWNDMessageHandler::SizeRectToAspectRatio(UINT param,
gfx::Size min_window_size;
gfx::Size max_window_size;
delegate_->GetMinMaxSize(&min_window_size, &max_window_size);
WindowResizeUtils::SizeMinMaxToAspectRatio(
aspect_ratio_.value(), &min_window_size, &max_window_size);
gfx::SizeMinMaxToAspectRatio(aspect_ratio_.value(), &min_window_size,
&max_window_size);
min_window_size = delegate_->DIPToScreenSize(min_window_size);
max_window_size = delegate_->DIPToScreenSize(max_window_size);
WindowResizeUtils::SizeRectToAspectRatio(
GetWindowResizeHitTest(param), aspect_ratio_.value(), min_window_size,
max_window_size, window_rect);
gfx::SizeRectToAspectRatio(GetWindowResizeEdge(param), aspect_ratio_.value(),
min_window_size, max_window_size, window_rect);
}
POINT HWNDMessageHandler::GetCursorPos() const {
......
......@@ -36,7 +36,6 @@
#include "ui/views/views_export.h"
#include "ui/views/win/pen_event_processor.h"
#include "ui/views/win/scoped_enable_unadjusted_mouse_events_win.h"
#include "ui/views/window/window_resize_utils.h"
namespace gfx {
class ImageSkia;
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_VIEWS_WINDOW_WINDOW_RESIZE_UTILS_H_
#define UI_VIEWS_WINDOW_WINDOW_RESIZE_UTILS_H_
#include "base/macros.h"
#include "ui/views/views_export.h"
namespace gfx {
class Size;
class Rect;
} // namespace gfx
namespace views {
enum class HitTest {
kBottom,
kBottomLeft,
kBottomRight,
kLeft,
kRight,
kTop,
kTopLeft,
kTopRight
};
class VIEWS_EXPORT WindowResizeUtils {
public:
// Force the min and max window sizes to adhere to the aspect ratio.
// |aspect_ratio| must be valid and is found using width / height.
static void SizeMinMaxToAspectRatio(float aspect_ratio,
gfx::Size* min_window_size,
gfx::Size* max_window_size);
// Updates |rect| to adhere to the |aspect_ratio| of the window, if it has
// been set. |param| refers to the edge of the window being sized.
// |min_window_size| and |max_window_size| are expected to adhere to the
// given aspect ratio.
// |aspect_ratio| must be valid and is found using width / height.
// TODO(apacible): |max_window_size| is expected to be non-empty. Handle
// unconstrained max sizes and sizing when windows are maximized.
static void SizeRectToAspectRatio(HitTest param,
float aspect_ratio,
const gfx::Size& min_window_size,
const gfx::Size& max_window_size,
gfx::Rect* rect);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(WindowResizeUtils);
};
} // namespace views
#endif // UI_VIEWS_WINDOW_WINDOW_RESIZE_UTILS_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/window/window_resize_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace views {
namespace {
// Aspect ratio is defined by width / height.
constexpr float kAspectRatioSquare = 1.0f;
constexpr float kAspectRatioHorizontal = 2.0f;
constexpr float kAspectRatioVertical = 0.5f;
const gfx::Size kMinSizeSquare = gfx::Size(10, 10);
const gfx::Size kMaxSizeSquare = gfx::Size(50, 50);
const gfx::Size kMinSizeHorizontal = gfx::Size(20, 10);
const gfx::Size kMaxSizeHorizontal = gfx::Size(50, 25);
const gfx::Size kMinSizeVertical = gfx::Size(10, 20);
const gfx::Size kMaxSizeVertical = gfx::Size(25, 50);
} // namespace
// Tests resizing of window with a 1:1 aspect ratio. This test also tests the
// 'pivot points' when resizing, i.e. the opposite side or corner of the
// window.
TEST(WindowResizeUtilsTest, SizeToSquareAspectRatio) {
// Size from the top of the window.
// |window_rect| within the bounds of kMinSizeSquare and kMaxSizeSquare.
gfx::Rect window_rect(100, 100, 15, 15);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kTop, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare,
&window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, 15, 15));
// Size from the bottom right corner of the window.
// |window_rect| smaller than kMinSizeSquare.
window_rect.SetRect(100, 100, 5, 5);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kBottomRight,
kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, kMinSizeSquare.width(),
kMinSizeSquare.height()));
// Size from the top of the window.
// |window_rect| larger than kMaxSizeSquare.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kTop, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare,
&window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 150, kMaxSizeSquare.width(),
kMaxSizeSquare.height()));
// Size from the bottom of the window.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kBottom, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare,
&window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, kMaxSizeSquare.width(),
kMaxSizeSquare.height()));
// Size from the left of the window.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kLeft, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare,
&window_rect);
EXPECT_EQ(window_rect, gfx::Rect(150, 150, kMaxSizeSquare.width(),
kMaxSizeSquare.height()));
// Size from the right of the window.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kRight, kAspectRatioSquare,
kMinSizeSquare, kMaxSizeSquare,
&window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, kMaxSizeSquare.width(),
kMaxSizeSquare.height()));
// Size from the top left corner of the window.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kTopLeft,
kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(150, 150, kMaxSizeSquare.width(),
kMaxSizeSquare.height()));
// Size from the top right corner of the window.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kTopRight,
kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 150, kMaxSizeSquare.width(),
kMaxSizeSquare.height()));
// Size from the bottom left corner of the window.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(HitTest::kBottomLeft,
kAspectRatioSquare, kMinSizeSquare,
kMaxSizeSquare, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(150, 100, kMaxSizeSquare.width(),
kMaxSizeSquare.height()));
}
// Tests the aspect ratio of the gfx::Rect adheres to the horizontal aspect
// ratio.
TEST(WindowResizeUtilsTest, SizeToHorizontalAspectRatio) {
// |window_rect| within bounds of kMinSizeHorizontal and kMaxSizeHorizontal.
gfx::Rect window_rect(100, 100, 20, 10);
WindowResizeUtils::SizeRectToAspectRatio(
HitTest::kTop, kAspectRatioHorizontal, kMinSizeHorizontal,
kMaxSizeHorizontal, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, 20, 10));
// |window_rect| smaller than kMinSizeHorizontal.
window_rect.SetRect(100, 100, 5, 5);
WindowResizeUtils::SizeRectToAspectRatio(
HitTest::kBottomRight, kAspectRatioHorizontal, kMinSizeHorizontal,
kMaxSizeHorizontal, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, kMinSizeHorizontal.width(),
kMinSizeHorizontal.height()));
// |window_rect| greater than kMaxSizeHorizontal.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(
HitTest::kTop, kAspectRatioHorizontal, kMinSizeHorizontal,
kMaxSizeHorizontal, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 175, kMaxSizeHorizontal.width(),
kMaxSizeHorizontal.height()));
}
// Tests the aspect ratio of the gfx::Rect adheres to the vertical aspect ratio.
TEST(WindowResizeUtilsTest, SizeToVerticalAspectRatio) {
// |window_rect| within bounds of kMinSizeVertical and kMaxSizeVertical.
gfx::Rect window_rect(100, 100, 10, 20);
WindowResizeUtils::SizeRectToAspectRatio(
HitTest::kBottomRight, kAspectRatioVertical, kMinSizeVertical,
kMaxSizeVertical, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, 10, 20));
// |window_rect| smaller than kMinSizeVertical.
window_rect.SetRect(100, 100, 5, 5);
WindowResizeUtils::SizeRectToAspectRatio(
HitTest::kBottomRight, kAspectRatioVertical, kMinSizeVertical,
kMaxSizeVertical, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, kMinSizeVertical.width(),
kMinSizeVertical.height()));
// |window_rect| greater than kMaxSizeVertical.
window_rect.SetRect(100, 100, 100, 100);
WindowResizeUtils::SizeRectToAspectRatio(
HitTest::kBottomRight, kAspectRatioVertical, kMinSizeVertical,
kMaxSizeVertical, &window_rect);
EXPECT_EQ(window_rect, gfx::Rect(100, 100, kMaxSizeVertical.width(),
kMaxSizeVertical.height()));
}
} // namespace views
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment