Chromium Code Reviews| 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 #if DCHECK_IS_ON() | |
| 46 NSWindow* WindowAtScreenPoint(const gfx::Point& screen_point) { | |
| 47 NSPoint ns_point = gfx::ScreenPointToNSPoint(screen_point); | |
| 48 NSInteger window_number = [NSWindow windowNumberAtPoint:ns_point | |
| 49 belowWindowWithWindowNumber:0]; | |
| 50 return | |
| 51 [[NSApplication sharedApplication] windowWithWindowNumber:window_number]; | |
| 52 } | |
| 53 #endif // DCHECK_IS_ON() | |
| 54 | |
| 55 // Send the mouse event of |type| to the |expected_window|, located at | |
| 56 // |mouse_position|. |expected_window| could be nil if we don't know which | |
| 57 // window is supposed to receive it. | |
| 58 void SendCustomLeftMouseEvent(NSWindow* expected_window, | |
| 59 gfx::Point mouse_position, | |
| 60 CGEventType type) { | |
| 61 DCHECK(type == kCGEventLeftMouseDown || type == kCGEventLeftMouseUp); | |
| 62 | |
| 63 // Check that we're sending the event to the window we expect. | |
| 64 if (expected_window) | |
| 65 DCHECK_EQ(expected_window, WindowAtScreenPoint(mouse_position)); | |
| 66 | |
| 67 base::ScopedCFTypeRef<CGEventRef> event(CGEventCreateMouseEvent( | |
| 68 nullptr, type, CGPointMake(mouse_position.x(), mouse_position.y()), | |
| 69 kCGMouseButtonLeft)); | |
| 70 | |
| 71 CGEventSetIntegerValueField( | |
| 72 event, kCGEventSourceUserData, | |
| 73 views::CocoaWindowMoveLoop::kCocoaWindowMoveLoopSimulatedEventUserData); | |
| 74 CGEventPost(kCGSessionEventTap, event); | |
| 75 } | |
| 76 | |
| 77 } // namespace | |
| 78 | |
| 79 namespace views { | |
| 80 | |
| 81 CocoaWindowMoveLoop::CocoaWindowMoveLoop( | |
| 82 BridgedNativeWidget* owner, | |
| 83 const gfx::Point& initial_mouse_in_screen) | |
| 84 : owner_(owner), | |
| 85 initial_mouse_in_screen_(initial_mouse_in_screen), | |
| 86 weak_factory_(this) { | |
| 87 // AppKit continues to send mouse events to the window, but toolkit-views | |
| 88 // doesn't expect them during RunMoveLoop(). | |
| 89 [BridgedContentView setIgnoreMouseEvents:YES]; | |
| 90 owner_->SetDraggable(true); | |
| 91 } | |
| 92 | |
| 93 CocoaWindowMoveLoop::~CocoaWindowMoveLoop() { | |
| 94 owner_->SetDraggable(false); | |
| 95 // Note the crafted events in Run() should not make their way through to | |
| 96 // toolkit-views, but regular events after that should be. So stop ignoring. | |
| 97 [BridgedContentView setIgnoreMouseEvents:NO]; | |
| 98 | |
| 99 // Handle the pathological case, where |this| is destroyed while running. | |
| 100 if (exit_reason_ref_) { | |
| 101 *exit_reason_ref_ = WINDOW_DESTROYED; | |
| 102 quit_closure_.Run(); | |
| 103 } | |
| 104 | |
| 105 owner_ = nullptr; | |
| 106 } | |
| 107 | |
| 108 Widget::MoveLoopResult CocoaWindowMoveLoop::Run() { | |
| 109 LoopExitReason exit_reason = ENDED_EXTERNALLY; | |
| 110 exit_reason_ref_ = &exit_reason; | |
| 111 NSWindow* window = owner_->ns_window(); | |
| 112 | |
| 113 base::RunLoop run_loop; | |
| 114 quit_closure_ = run_loop.QuitClosure(); | |
| 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(&CocoaWindowMoveLoop::OnPositionChanged, | |
| 142 [weak_cocoa_window_move_loop weak])); | |
| 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 | |
|
tapted
2016/04/20 07:30:22
this Epic comment needs rewording now that we no l
themblsha
2016/04/20 13:38:40
Done.
| |
| 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 SendCustomLeftMouseEvent(nil, GetMousePosition(), | |
| 191 kCGEventLeftMouseDown); | |
| 192 } else { | |
| 193 SendCustomLeftMouseEvent(window, GetMousePosition(), | |
| 194 kCGEventLeftMouseUp); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 return exit_reason == ESCAPE_PRESSED ? Widget::MOVE_LOOP_CANCELED | |
| 199 : Widget::MOVE_LOOP_SUCCESSFUL; | |
| 200 } | |
| 201 | |
| 202 void CocoaWindowMoveLoop::End() { | |
| 203 if (exit_reason_ref_) { | |
| 204 DCHECK_EQ(*exit_reason_ref_, ENDED_EXTERNALLY); | |
| 205 // Ensure the destructor doesn't replace the reason. | |
| 206 exit_reason_ref_ = nullptr; | |
| 207 quit_closure_.Run(); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 void CocoaWindowMoveLoop::OnPositionChanged() { | |
| 212 return owner_->OnPositionChanged(); | |
| 213 } | |
| 214 | |
| 215 } // namespace views | |
| OLD | NEW |