diff options
| author | jackhou <jackhou@chromium.org> | 2015-08-11 15:06:40 -0700 |
|---|---|---|
| committer | Commit bot <commit-bot@chromium.org> | 2015-08-11 22:07:16 +0000 |
| commit | 148c98707491251276ec2380e269e1b7f0edc699 (patch) | |
| tree | 0527e003ffa612c532c2903f3a66d535dfe1322e | |
| parent | 9533b6fcb817836b26cbe56b505ab6c789dca2b6 (diff) | |
| download | chromium_src-148c98707491251276ec2380e269e1b7f0edc699.zip chromium_src-148c98707491251276ec2380e269e1b7f0edc699.tar.gz chromium_src-148c98707491251276ec2380e269e1b7f0edc699.tar.bz2 | |
[Mac] Add gfx::ScreenPoint[To|From]NSPoint.
Similarly to ScreenRect[To|From]NSRect, we want to avoid a proliferation of the
following patterns for doing this conversion:
gfx::Point point(ns_point.x,
NSMaxY([[[NSScreen screens] objectAtIndex:0] frame]) - ns_point.y);
Or alternatively, creating an NSRect, converting with gfx::ScreenRectFromNSRect(),
and taking the origin.
BUG=None
Review URL: https://codereview.chromium.org/1287573002
Cr-Commit-Position: refs/heads/master@{#342914}
| -rw-r--r-- | ui/gfx/mac/coordinate_conversion.h | 13 | ||||
| -rw-r--r-- | ui/gfx/mac/coordinate_conversion.mm | 20 | ||||
| -rw-r--r-- | ui/gfx/mac/coordinate_conversion_unittest.mm | 37 |
3 files changed, 58 insertions, 12 deletions
diff --git a/ui/gfx/mac/coordinate_conversion.h b/ui/gfx/mac/coordinate_conversion.h index 3deec9a..3b75485 100644 --- a/ui/gfx/mac/coordinate_conversion.h +++ b/ui/gfx/mac/coordinate_conversion.h @@ -11,15 +11,24 @@ namespace gfx { +class Point; class Rect; // Convert a gfx::Rect specified with the origin at the top left of the primary // display into AppKit secreen coordinates (origin at the bottom left). -GFX_EXPORT NSRect ScreenRectToNSRect(const gfx::Rect& rect); +GFX_EXPORT NSRect ScreenRectToNSRect(const Rect& rect); // Convert an AppKit NSRect with origin in the bottom left of the primary // display into a gfx::Rect with origin at the top left of the primary display. -GFX_EXPORT gfx::Rect ScreenRectFromNSRect(const NSRect& point); +GFX_EXPORT Rect ScreenRectFromNSRect(const NSRect& point); + +// Convert a gfx::Point specified with the origin at the top left of the primary +// display into AppKit screen coordinates (origin at the bottom left). +GFX_EXPORT NSPoint ScreenPointToNSPoint(const Point& point); + +// Convert an AppKit NSPoint with origin in the bottom left of the primary +// display into a gfx::Point with origin at the top left of the primary display. +GFX_EXPORT Point ScreenPointFromNSPoint(const NSPoint& point); } // namespace gfx diff --git a/ui/gfx/mac/coordinate_conversion.mm b/ui/gfx/mac/coordinate_conversion.mm index b2c41a4..54b16e9 100644 --- a/ui/gfx/mac/coordinate_conversion.mm +++ b/ui/gfx/mac/coordinate_conversion.mm @@ -6,6 +6,7 @@ #import <Cocoa/Cocoa.h> +#include "ui/gfx/geometry/point.h" #include "ui/gfx/geometry/rect.h" namespace gfx { @@ -20,18 +21,25 @@ CGFloat PrimaryDisplayHeight() { } // namespace -NSRect ScreenRectToNSRect(const gfx::Rect& rect) { +NSRect ScreenRectToNSRect(const Rect& rect) { return NSMakeRect(rect.x(), PrimaryDisplayHeight() - rect.y() - rect.height(), rect.width(), rect.height()); } -gfx::Rect ScreenRectFromNSRect(const NSRect& rect) { - return gfx::Rect(rect.origin.x, - PrimaryDisplayHeight() - rect.origin.y - rect.size.height, - rect.size.width, - rect.size.height); +Rect ScreenRectFromNSRect(const NSRect& rect) { + return Rect(rect.origin.x, + PrimaryDisplayHeight() - rect.origin.y - rect.size.height, + rect.size.width, rect.size.height); +} + +NSPoint ScreenPointToNSPoint(const Point& point) { + return NSMakePoint(point.x(), PrimaryDisplayHeight() - point.y()); +} + +Point ScreenPointFromNSPoint(const NSPoint& point) { + return Point(point.x, PrimaryDisplayHeight() - point.y); } } // namespace gfx diff --git a/ui/gfx/mac/coordinate_conversion_unittest.mm b/ui/gfx/mac/coordinate_conversion_unittest.mm index ae49317..ac882fa 100644 --- a/ui/gfx/mac/coordinate_conversion_unittest.mm +++ b/ui/gfx/mac/coordinate_conversion_unittest.mm @@ -81,28 +81,57 @@ void MacCoordinateConversionTest::TearDown() { // .. .. // 0 199 TEST_F(MacCoordinateConversionTest, ScreenRectToFromNSRect) { + // Window on the primary screen. Rect gfx_rect = Rect(10, 20, 30, 40); NSRect ns_rect = ScreenRectToNSRect(gfx_rect); EXPECT_NSEQ(NSMakeRect(10, 140, 30, 40), ns_rect); - EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString()); + EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect)); // Window in a screen to the left of the primary screen. gfx_rect = Rect(-40, 20, 30, 40); ns_rect = ScreenRectToNSRect(gfx_rect); EXPECT_NSEQ(NSMakeRect(-40, 140, 30, 40), ns_rect); - EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString()); + EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect)); // Window in a screen below the primary screen. gfx_rect = Rect(10, 220, 30, 40); ns_rect = ScreenRectToNSRect(gfx_rect); EXPECT_NSEQ(NSMakeRect(10, -60, 30, 40), ns_rect); - EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString()); + EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect)); // Window in a screen below and to the left primary screen. gfx_rect = Rect(-40, 220, 30, 40); ns_rect = ScreenRectToNSRect(gfx_rect); EXPECT_NSEQ(NSMakeRect(-40, -60, 30, 40), ns_rect); - EXPECT_EQ(gfx_rect.ToString(), ScreenRectFromNSRect(ns_rect).ToString()); + EXPECT_EQ(gfx_rect, ScreenRectFromNSRect(ns_rect)); +} + +// Test point conversions using the same setup as ScreenRectToFromNSRect, but +// using only the origin. +TEST_F(MacCoordinateConversionTest, ScreenPointToFromNSPoint) { + // Point on the primary screen. + Point gfx_point = Point(10, 20); + NSPoint ns_point = ScreenPointToNSPoint(gfx_point); + EXPECT_NSEQ(NSMakePoint(10, 180), ns_point); + EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point)); + + // Point in a screen to the left of the primary screen. + gfx_point = Point(-40, 20); + ns_point = ScreenPointToNSPoint(gfx_point); + EXPECT_NSEQ(NSMakePoint(-40, 180), ns_point); + EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point)); + + // Point in a screen below the primary screen. + gfx_point = Point(10, 220); + ns_point = ScreenPointToNSPoint(gfx_point); + EXPECT_NSEQ(NSMakePoint(10, -20), ns_point); + EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point)); + + // Point in a screen below and to the left primary screen. + gfx_point = Point(-40, 220); + ns_point = ScreenPointToNSPoint(gfx_point); + EXPECT_NSEQ(NSMakePoint(-40, -20), ns_point); + EXPECT_EQ(gfx_point, ScreenPointFromNSPoint(ns_point)); } } // namespace gfx |
