Chromium Code Reviews| Index: ui/views/cocoa/bridged_native_widget.mm |
| diff --git a/ui/views/cocoa/bridged_native_widget.mm b/ui/views/cocoa/bridged_native_widget.mm |
| index 107896f931f200368571f936db5dd58cc4eebeb7..2fd991c3b01abfac8d845549d4c6c6b4adf5b9e0 100644 |
| --- a/ui/views/cocoa/bridged_native_widget.mm |
| +++ b/ui/views/cocoa/bridged_native_widget.mm |
| @@ -7,9 +7,11 @@ |
| #import <objc/runtime.h> |
| #include "base/logging.h" |
| +#import "base/mac/foundation_util.h" |
| #include "base/mac/mac_util.h" |
| #import "base/mac/sdk_forward_declarations.h" |
| #include "base/thread_task_runner_handle.h" |
| +#include "ui/base/hit_test.h" |
| #include "ui/base/ime/input_method.h" |
| #include "ui/base/ime/input_method_factory.h" |
| #include "ui/base/ui_base_switches_util.h" |
| @@ -76,6 +78,94 @@ gfx::Size GetClientSizeForWindowSize(NSWindow* window, |
| return gfx::Size([window contentRectForFrameRect:frame_rect].size); |
| } |
| +BOOL WindowWantsMouseDownReposted(NSEvent* ns_event) { |
| + id delegate = [[ns_event window] delegate]; |
| + return |
| + [delegate |
| + respondsToSelector:@selector(shouldRepostPendingLeftMouseDown:)] && |
| + [delegate shouldRepostPendingLeftMouseDown:[ns_event locationInWindow]]; |
| +} |
| + |
| +// Check if a mouse-down event should drag the window. If so, repost the event |
| +// twice. It doesn't seem to work the first time it's reposted. |
| +NSEvent* DoubleRepostEventIfHandledByWindow(NSEvent* ns_event) { |
| + // Which repost we're expecting to receive. |
| + static int expected_repost_count = 0; |
| + // The event number of the reposted event. This let's us track whether an |
| + // event is actually a repost since user-generated events have increasing |
| + // event numbers. |
| + static NSInteger reposted_event_number = -1; |
| + |
| + CGEventRef cg_event = [ns_event CGEvent]; |
| + NSInteger event_number = [ns_event eventNumber]; |
| + |
| + // The logic here is a bit convoluted because we want to mitigate race |
| + // conditions if somehow a different mousedown occurs between reposts. |
| + // Specifically, we want to avoid: |
| + // - BridgedNativeWidget's draggability getting out of sync (e.g. it's |
| + // draggable outside of a repost cycle), |
| + // - any repost loop. |
| + if (reposted_event_number == event_number) { |
| + // This is a reposted event. |
| + if (expected_repost_count == 1) { |
| + // There has been no intermediate mouse-down, repost the event again. |
| + expected_repost_count = 2; |
| + CGEventPost(kCGSessionEventTap, cg_event); |
| + } else if (expected_repost_count == 2) { |
| + expected_repost_count = 0; |
| + // This is the final repost, call through to make the window |
| + // non-draggable. |
| + WindowWantsMouseDownReposted(ns_event); |
| + } |
|
tapted
2015/05/22 04:03:12
else { NOTREACHED(); }? (i.e. we should never see
jackhou1
2015/05/25 04:50:19
In the if (expected_repost_count > 0) block below,
|
| + return nil; // Ignore the event. |
| + } else { |
|
tapted
2015/05/22 04:03:12
nit: no else after return
jackhou1
2015/05/25 04:50:19
Done.
|
| + // This is a new event. |
| + if (expected_repost_count > 0) { |
| + // We were expecting a repost, but since this is a new mouse-down, cancel |
| + // reposting and allow event to continue as usual. |
| + expected_repost_count = 0; |
| + // Call through so that the window is made non-draggable again. |
| + WindowWantsMouseDownReposted(ns_event); |
| + return ns_event; |
| + } else { |
|
tapted
2015/05/22 04:03:12
no else after return
jackhou1
2015/05/25 04:50:19
Done.
|
| + // We're not in the middle of reposting, process this new event. |
| + if (WindowWantsMouseDownReposted(ns_event)) { |
| + expected_repost_count = 1; |
| + reposted_event_number = [ns_event eventNumber]; |
| + CGEventPost(kCGSessionEventTap, cg_event); |
| + return nil; |
| + } |
| + return ns_event; |
| + } |
| + } |
| +} |
| + |
| +// Support window caption/draggable regions. |
| +// In AppKit, non-client regions are set by overriding |
| +// -[NSView mouseDownCanMoveWindow]. NSApplication caches this area as views are |
| +// installed and performs window moving when mouse-downs land in the area. |
| +// In Views, non-client regions are determined via hit-tests when the event |
| +// occurs. |
| +// To bridge the two models, we monitor mouse-downs with |
| +// +[NSEvent addLocalMonitorForEventsMatchingMask:handler:]. This receives |
| +// events after window dragging is handled, so for mouse-downs that land on a |
| +// draggable point, we cancel the event and repost it at the CGSessionEventTap |
| +// level so that window dragging will be handled again. |
| +void SetupDragEventMonitor() { |
| + static id g_event_monitor = nil; |
|
tapted
2015/05/22 04:03:12
nit: no `g_` for local statics. I'd maybe just cal
jackhou1
2015/05/25 04:50:19
Done.
|
| + if (g_event_monitor) |
| + return; |
| + |
| + NSEvent* (^monitor_callback)(NSEvent* ns_event); |
|
tapted
2015/05/22 04:03:12
nit: move this into the function call (just to hid
jackhou1
2015/05/25 04:50:19
Done.
|
| + monitor_callback = ^NSEvent*(NSEvent* ns_event) { |
| + return DoubleRepostEventIfHandledByWindow(ns_event); |
| + }; |
| + |
| + g_event_monitor = |
| + [NSEvent addLocalMonitorForEventsMatchingMask:NSLeftMouseDownMask |
| + handler:monitor_callback]; |
| +} |
| + |
| } // namespace |
| namespace views { |
| @@ -99,6 +189,7 @@ BridgedNativeWidget::BridgedNativeWidget(NativeWidgetMac* parent) |
| in_fullscreen_transition_(false), |
| window_visible_(false), |
| wants_to_be_visible_(false) { |
| + SetupDragEventMonitor(); |
| DCHECK(parent); |
| window_delegate_.reset( |
| [[ViewsNSWindowDelegate alloc] initWithBridgedNativeWidget:this]); |
| @@ -522,6 +613,32 @@ void BridgedNativeWidget::OnWindowKeyStatusChangedTo(bool is_key) { |
| } |
| } |
| +bool BridgedNativeWidget::ShouldRepostPendingLeftMouseDown( |
| + NSPoint location_in_window) { |
| + if (!bridged_view_) |
| + return false; |
| + |
| + if ([bridged_view_ mouseDownCanMoveWindow]) { |
| + // This is a re-post, the movement has already started, so we can make the |
| + // window non-draggable again. |
| + SetDraggable(false); |
| + return false; |
| + } |
| + |
| + gfx::Point point(location_in_window.x, |
| + NSHeight([window_ frame]) - location_in_window.y); |
| + bool should_move_window = |
| + native_widget_mac()->GetWidget()->GetNonClientComponent(point) == |
| + HTCAPTION; |
| + |
| + if (!should_move_window) |
| + return false; |
| + |
| + // Make the window draggable, then return true to repost the event. |
| + SetDraggable(true); |
| + return true; |
| +} |
| + |
| void BridgedNativeWidget::OnSizeConstraintsChanged() { |
| NSWindow* window = ns_window(); |
| Widget* widget = native_widget_mac()->GetWidget(); |
| @@ -855,4 +972,14 @@ NSMutableDictionary* BridgedNativeWidget::GetWindowProperties() const { |
| return properties; |
| } |
| +void BridgedNativeWidget::SetDraggable(bool draggable) { |
| + [bridged_view_ setMouseDownCanMoveWindow:draggable]; |
| + // AppKit will not update it's cache of mouseDownCanMoveWindow unless |
|
tapted
2015/05/22 04:03:12
nit: it's -> its
jackhou1
2015/05/25 04:50:19
Done.
|
| + // something changes. |
| + base::scoped_nsobject<NSView> temp_view( |
| + [[NSView alloc] initWithFrame:[bridged_view_ bounds]]); |
| + [bridged_view_ addSubview:temp_view]; |
| + [temp_view removeFromSuperview]; |
| +} |
| + |
| } // namespace views |