OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/cocoa/cocoa_window_move_loop.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "base/run_loop.h" |
| 10 #import "ui/gfx/mac/coordinate_conversion.mm" |
| 11 #import "ui/views/cocoa/bridged_content_view.h" |
| 12 #import "ui/views/cocoa/bridged_native_widget.h" |
| 13 |
| 14 // CocoaWindowMoveLoop can be deleted just before its local event monitor is |
| 15 // processed and in that case it's too late to call removeMonitor: and we have a |
| 16 // dangling this pointer. Use a proxy Obj-C class to store the weakptr. |
| 17 @interface WeakCocoaWindowMoveLoop : NSObject { |
| 18 @private |
| 19 base::WeakPtr<views::CocoaWindowMoveLoop> weak_; |
| 20 } |
| 21 @end |
| 22 |
| 23 @implementation WeakCocoaWindowMoveLoop |
| 24 - (id)initWithWeakPtr:(const base::WeakPtr<views::CocoaWindowMoveLoop>&)weak { |
| 25 if ((self = [super init])) { |
| 26 weak_ = weak; |
| 27 } |
| 28 return self; |
| 29 } |
| 30 |
| 31 - (base::WeakPtr<views::CocoaWindowMoveLoop>&)weak { |
| 32 return weak_; |
| 33 } |
| 34 @end |
| 35 |
| 36 namespace { |
| 37 |
| 38 // Returns current mouse position in screen coordinates. |
| 39 gfx::Point GetMousePosition() { |
| 40 CGPoint cg_point = CGEventGetLocation( |
| 41 base::ScopedCFTypeRef<CGEventRef>(CGEventCreate(nullptr))); |
| 42 return gfx::Point(cg_point.x, cg_point.y); |
| 43 } |
| 44 |
| 45 // Send the mouse event of |type| to the |expected_window|, located at |
| 46 // |mouse_position|. |expected_window| could be nil if we don't know which |
| 47 // window is supposed to receive it. |
| 48 void SendCustomLeftMouseEvent(NSWindow* expected_window, |
| 49 gfx::Point mouse_position, |
| 50 CGEventType type) { |
| 51 DCHECK(type == kCGEventLeftMouseDown || type == kCGEventLeftMouseUp); |
| 52 |
| 53 // Check that we're sending the event to the window we expect. |
| 54 if (expected_window) { |
| 55 NSPoint ns_mouse_position = gfx::ScreenPointToNSPoint(mouse_position); |
| 56 NSInteger window_number = [NSWindow windowNumberAtPoint:ns_mouse_position |
| 57 belowWindowWithWindowNumber:0]; |
| 58 NSWindow* current_window = [[NSApplication sharedApplication] |
| 59 windowWithWindowNumber:window_number]; |
| 60 DCHECK_EQ(expected_window, current_window); |
| 61 } |
| 62 |
| 63 base::ScopedCFTypeRef<CGEventRef> event(CGEventCreateMouseEvent( |
| 64 nullptr, type, CGPointMake(mouse_position.x(), mouse_position.y()), |
| 65 kCGMouseButtonLeft)); |
| 66 |
| 67 CGEventSetIntegerValueField( |
| 68 event, kCGEventSourceUserData, |
| 69 views::kCocoaWindowMoveLoopSimulatedEventUserData); |
| 70 CGEventPost(kCGSessionEventTap, event); |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 namespace views { |
| 76 |
| 77 const int kCocoaWindowMoveLoopSimulatedEventUserData = 1; |
| 78 |
| 79 CocoaWindowMoveLoop::CocoaWindowMoveLoop(BridgedNativeWidget* owner, |
| 80 gfx::Point initial_mouse_in_screen) |
| 81 : owner_(owner), |
| 82 run_loop_(new base::RunLoop), |
| 83 initial_mouse_in_screen_(initial_mouse_in_screen), |
| 84 quit_closure_(run_loop_->QuitClosure()), |
| 85 weak_factory_(this) { |
| 86 // AppKit continues to send mouse events to the window, but toolkit-views |
| 87 // doesn't expect them during RunMoveLoop(). |
| 88 [BridgedContentView setIgnoreMouseEvents:YES]; |
| 89 owner_->SetDraggable(true); |
| 90 } |
| 91 |
| 92 CocoaWindowMoveLoop::~CocoaWindowMoveLoop() { |
| 93 owner_->SetDraggable(false); |
| 94 // Note the crafted events in Run() should not make their way through to |
| 95 // toolkit-views, but regular events after that should be. So stop ignoring. |
| 96 [BridgedContentView setIgnoreMouseEvents:NO]; |
| 97 |
| 98 // Handle the pathological case, where |this| is destroyed while running. |
| 99 if (exit_reason_ref_) { |
| 100 *exit_reason_ref_ = WINDOW_DESTROYED; |
| 101 quit_closure_.Run(); |
| 102 } |
| 103 |
| 104 owner_ = nullptr; |
| 105 } |
| 106 |
| 107 Widget::MoveLoopResult CocoaWindowMoveLoop::Run() { |
| 108 LoopExitReason exit_reason = ENDED_EXTERNALLY; |
| 109 exit_reason_ref_ = &exit_reason; |
| 110 NSWindow* window = owner_->ns_window(); |
| 111 |
| 112 // Move the RunLoop to the stack so our destructor can be called while it is |
| 113 // running. |
| 114 scoped_ptr<base::RunLoop> run_loop(std::move(run_loop_)); |
| 115 |
| 116 // A new window may have just been created, so post an event at the session |
| 117 // tap level to initiate a window drag. |
| 118 SendCustomLeftMouseEvent(window, initial_mouse_in_screen_, |
| 119 kCGEventLeftMouseDown); |
| 120 |
| 121 // Will be retained by the monitor handler block. |
| 122 WeakCocoaWindowMoveLoop* weak_cocoa_window_move_loop = |
| 123 [[[WeakCocoaWindowMoveLoop alloc] |
| 124 initWithWeakPtr:weak_factory_.GetWeakPtr()] autorelease]; |
| 125 |
| 126 NSEventMask mask = NSLeftMouseUpMask | NSKeyDownMask | NSLeftMouseDraggedMask; |
| 127 auto handler = ^NSEvent*(NSEvent* event) { |
| 128 CocoaWindowMoveLoop* strong = [weak_cocoa_window_move_loop weak].get(); |
| 129 if (!strong) { |
| 130 // By this point CocoaWindowMoveLoop was deleted while processing this |
| 131 // same event, and this event monitor was not unregistered in time. It is |
| 132 // reproducible by the PressEscapeWhileDetached test. |
| 133 // Continue processing the event. |
| 134 return event; |
| 135 } |
| 136 |
| 137 if ([event type] == NSLeftMouseDragged) { |
| 138 // AppKit doesn't supply position updates during a drag, so post a task to |
| 139 // notify observers once AppKit has moved the window. |
| 140 base::MessageLoop::current()->PostTask( |
| 141 FROM_HERE, base::Bind(&BridgedNativeWidget::OnPositionChanged, |
| 142 base::Unretained(strong->owner_))); |
| 143 return event; |
| 144 } |
| 145 |
| 146 strong->quit_closure_.Run(); |
| 147 if ([event type] == NSLeftMouseUp) { |
| 148 *strong->exit_reason_ref_ = MOUSE_UP; |
| 149 return event; // Process the MouseUp. |
| 150 } |
| 151 *strong->exit_reason_ref_ = ESCAPE_PRESSED; |
| 152 return nil; // Swallow the keypress. |
| 153 }; |
| 154 id monitor = |
| 155 [NSEvent addLocalMonitorForEventsMatchingMask:mask handler:handler]; |
| 156 |
| 157 // NSKeyDownMask doesn't work inside addLocalMonitorForEventsMatchingMask: |
| 158 // the event is swallowed by the window move loop before it gets to -[NSApp |
| 159 // sendEvent:]. To see an escape keypress, hook in an event tap lower. |
| 160 |
| 161 run_loop->Run(); |
| 162 [NSEvent removeMonitor:monitor]; |
| 163 |
| 164 if (exit_reason != WINDOW_DESTROYED && exit_reason != ENDED_EXTERNALLY) { |
| 165 exit_reason_ref_ = nullptr; // Ensure End() doesn't replace the reason. |
| 166 owner_->EndMoveLoop(); // Deletes |this|. |
| 167 } |
| 168 |
| 169 if (exit_reason != MOUSE_UP) { |
| 170 // Tell AppKit to stop moving the window. Otherwise, AppKit will refuse to |
| 171 // start a new window drag. Note the window being dragged is going away in |
| 172 // this case, so it doesn't really matter where the event is located. |
| 173 |
| 174 if (exit_reason == ENDED_EXTERNALLY) { |
| 175 // When not canceled, the non-moving drag in the original window must |
| 176 // resume. To do this, AppKit needs to see a mouseDown so that it sends |
| 177 // the correct events. Ideally, it also needs to be at the original |
| 178 // offset, so that it hits a non-draggable region of the original |
| 179 // window: The tab being dragged may move some distance from the cursor |
| 180 // when it "snaps in", so the cursor may not be over a tab. Sadly, this |
| 181 // method doesn't know which window that is. But all that really needs |
| 182 // to be done is to prevent a custom-dragging area from starting a |
| 183 // window-drag. So hook into the logic in RepostEventIfHandledByWindow() |
| 184 // by setting a flag here. Note this assumes the custom mouseDown event |
| 185 // is guaranteed to hit another BridgedNativeWidget when it gets to the |
| 186 // front of the event queue. |
| 187 // TODO(tapted): A better fix would be to keep the temporary window |
| 188 // around and never call EndMoveLoop() on Mac, making this block of code |
| 189 // obsolete. |
| 190 BridgedNativeWidget::IgnoreNextMouseDownForDraggableRegions(); |
| 191 SendCustomLeftMouseEvent(nil, GetMousePosition(), |
| 192 kCGEventLeftMouseDown); |
| 193 } else { |
| 194 SendCustomLeftMouseEvent(window, GetMousePosition(), |
| 195 kCGEventLeftMouseUp); |
| 196 } |
| 197 } |
| 198 |
| 199 return exit_reason == ESCAPE_PRESSED ? Widget::MOVE_LOOP_CANCELED |
| 200 : Widget::MOVE_LOOP_SUCCESSFUL; |
| 201 } |
| 202 |
| 203 void CocoaWindowMoveLoop::End() { |
| 204 if (exit_reason_ref_) { |
| 205 DCHECK_EQ(*exit_reason_ref_, ENDED_EXTERNALLY); |
| 206 // Ensure the destructor doesn't replace the reason. |
| 207 exit_reason_ref_ = nullptr; |
| 208 quit_closure_.Run(); |
| 209 } |
| 210 } |
| 211 |
| 212 } // namespace views |
OLD | NEW |