diff options
author | tapted@chromium.org <tapted@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-06 10:35:53 +0000 |
---|---|---|
committer | tapted@chromium.org <tapted@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-06 10:35:53 +0000 |
commit | 86e8855e7f34c43bcbc6e5655a6efed2744e5d8b (patch) | |
tree | 0305de0627600dfc47c058ffdec1fc954aed8152 /ui/events | |
parent | 0164e4c2548ed655de425d3fbc69ee1b9b1ad666 (diff) | |
download | chromium_src-86e8855e7f34c43bcbc6e5655a6efed2744e5d8b.zip chromium_src-86e8855e7f34c43bcbc6e5655a6efed2744e5d8b.tar.gz chromium_src-86e8855e7f34c43bcbc6e5655a6efed2744e5d8b.tar.bz2 |
MacViews: Allow ui::EventLocationFromNative to handle native titlebars
Currently EventLocationFromNative on mac only properly handles events in
windows with no native window border because it flips coordinate systems
using the entire NSWindow frame.
This change flips coordinates in the window's contentRect instead.
BUG=366021
TEST=Added: events_unittests EventsMacTest.NativeTitlebarEventLocation
Review URL: https://codereview.chromium.org/316053008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275380 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/events')
-rw-r--r-- | ui/events/cocoa/events_mac.mm | 7 | ||||
-rw-r--r-- | ui/events/cocoa/events_mac_unittest.mm | 52 |
2 files changed, 54 insertions, 5 deletions
diff --git a/ui/events/cocoa/events_mac.mm b/ui/events/cocoa/events_mac.mm index a8d9365..13098dd 100644 --- a/ui/events/cocoa/events_mac.mm +++ b/ui/events/cocoa/events_mac.mm @@ -87,13 +87,14 @@ base::TimeDelta EventTimeFromNative(const base::NativeEvent& native_event) { } gfx::Point EventLocationFromNative(const base::NativeEvent& native_event) { - if (![native_event window]) { + NSWindow* window = [native_event window]; + if (!window) { NOTIMPLEMENTED(); // Point will be in screen coordinates. return gfx::Point(); } NSPoint location = [native_event locationInWindow]; - return gfx::Point(location.x, - NSHeight([[native_event window] frame]) - location.y); + NSRect content_rect = [window contentRectForFrameRect:[window frame]]; + return gfx::Point(location.x, NSHeight(content_rect) - location.y); } gfx::Point EventSystemLocationFromNative( diff --git a/ui/events/cocoa/events_mac_unittest.mm b/ui/events/cocoa/events_mac_unittest.mm index 9b93084..adaafea 100644 --- a/ui/events/cocoa/events_mac_unittest.mm +++ b/ui/events/cocoa/events_mac_unittest.mm @@ -48,8 +48,10 @@ class EventsMacTest : public CocoaTest { EventsMacTest() {} gfx::Point Flip(gfx::Point window_location) { - window_location.set_y( - NSHeight([test_window() frame]) - window_location.y()); + NSRect window_frame = [test_window() frame]; + CGFloat content_height = + NSHeight([test_window() contentRectForFrameRect:window_frame]); + window_location.set_y(content_height - window_location.y()); return window_location; } @@ -240,4 +242,50 @@ TEST_F(EventsMacTest, ButtonEvents) { ClearSwizzle(); } +// Test correct location when the window has a native titlebar. +TEST_F(EventsMacTest, NativeTitlebarEventLocation) { + gfx::Point location(5, 10); + NSUInteger style_mask = NSTitledWindowMask | NSClosableWindowMask | + NSMiniaturizableWindowMask | NSResizableWindowMask; + + // First check that the window provided by ui::CocoaTest is how we think. + DCHECK_EQ(NSBorderlessWindowMask, [test_window() styleMask]); + [test_window() setStyleMask:style_mask]; + DCHECK_EQ(style_mask, [test_window() styleMask]); + + // EventLocationFromNative should behave the same as the ButtonEvents test. + NSEvent* event = TestMouseEvent(NSLeftMouseDown, location, 0); + EXPECT_EQ(ui::ET_MOUSE_PRESSED, ui::EventTypeFromNative(event)); + EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, ui::EventFlagsFromNative(event)); + EXPECT_EQ(location, ui::EventLocationFromNative(event)); + + // And be explicit, to ensure the test doesn't depend on some property of the + // test harness. The change to the frame rect could be OS-specfic, so set it + // to a known value. + const CGFloat kTestHeight = 400; + NSRect content_rect = NSMakeRect(0, 0, 600, kTestHeight); + NSRect frame_rect = [test_window() frameRectForContentRect:content_rect]; + [test_window() setFrame:frame_rect display:YES]; + event = [NSEvent mouseEventWithType:NSLeftMouseDown + location:NSMakePoint(0, 0) // Bottom-left corner. + modifierFlags:0 + timestamp:0 + windowNumber:[test_window() windowNumber] + context:nil + eventNumber:0 + clickCount:0 + pressure:1.0]; + // Bottom-left corner should be flipped. + EXPECT_EQ(gfx::Point(0, kTestHeight), ui::EventLocationFromNative(event)); + + // Removing the border, and sending the same event should move it down in the + // toolkit-views coordinate system. + int height_change = NSHeight(frame_rect) - kTestHeight; + EXPECT_GT(height_change, 0); + [test_window() setStyleMask:NSBorderlessWindowMask]; + [test_window() setFrame:frame_rect display:YES]; + EXPECT_EQ(gfx::Point(0, kTestHeight + height_change), + ui::EventLocationFromNative(event)); +} + } // namespace ui |