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