| Index: ui/views/cocoa/cocoa_window_move_loop.mm
|
| diff --git a/ui/views/cocoa/cocoa_window_move_loop.mm b/ui/views/cocoa/cocoa_window_move_loop.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9eb06596d84e6cea68e9d2ad51ada90305f044b4
|
| --- /dev/null
|
| +++ b/ui/views/cocoa/cocoa_window_move_loop.mm
|
| @@ -0,0 +1,212 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ui/views/cocoa/cocoa_window_move_loop.h"
|
| +
|
| +#import <Cocoa/Cocoa.h>
|
| +
|
| +#include "base/run_loop.h"
|
| +#import "ui/gfx/mac/coordinate_conversion.mm"
|
| +#import "ui/views/cocoa/bridged_content_view.h"
|
| +#import "ui/views/cocoa/bridged_native_widget.h"
|
| +
|
| +// CocoaWindowMoveLoop can be deleted just before its local event monitor is
|
| +// processed and in that case it's too late to call removeMonitor: and we have a
|
| +// dangling this pointer. Use a proxy Obj-C class to store the weakptr.
|
| +@interface WeakCocoaWindowMoveLoop : NSObject {
|
| + @private
|
| + base::WeakPtr<views::CocoaWindowMoveLoop> weak_;
|
| +}
|
| +@end
|
| +
|
| +@implementation WeakCocoaWindowMoveLoop
|
| +- (id)initWithWeakPtr:(const base::WeakPtr<views::CocoaWindowMoveLoop>&)weak {
|
| + if ((self = [super init])) {
|
| + weak_ = weak;
|
| + }
|
| + return self;
|
| +}
|
| +
|
| +- (base::WeakPtr<views::CocoaWindowMoveLoop>&)weak {
|
| + return weak_;
|
| +}
|
| +@end
|
| +
|
| +namespace {
|
| +
|
| +// Returns current mouse position in screen coordinates.
|
| +gfx::Point GetMousePosition() {
|
| + CGPoint cg_point = CGEventGetLocation(
|
| + base::ScopedCFTypeRef<CGEventRef>(CGEventCreate(nullptr)));
|
| + return gfx::Point(cg_point.x, cg_point.y);
|
| +}
|
| +
|
| +// Send the mouse event of |type| to the |expected_window|, located at
|
| +// |mouse_position|. |expected_window| could be nil if we don't know which
|
| +// window is supposed to receive it.
|
| +void SendCustomLeftMouseEvent(NSWindow* expected_window,
|
| + gfx::Point mouse_position,
|
| + CGEventType type) {
|
| + DCHECK(type == kCGEventLeftMouseDown || type == kCGEventLeftMouseUp);
|
| +
|
| + // Check that we're sending the event to the window we expect.
|
| + if (expected_window) {
|
| + NSPoint ns_mouse_position = gfx::ScreenPointToNSPoint(mouse_position);
|
| + NSInteger window_number = [NSWindow windowNumberAtPoint:ns_mouse_position
|
| + belowWindowWithWindowNumber:0];
|
| + NSWindow* current_window = [[NSApplication sharedApplication]
|
| + windowWithWindowNumber:window_number];
|
| + DCHECK_EQ(expected_window, current_window);
|
| + }
|
| +
|
| + base::ScopedCFTypeRef<CGEventRef> event(CGEventCreateMouseEvent(
|
| + nullptr, type, CGPointMake(mouse_position.x(), mouse_position.y()),
|
| + kCGMouseButtonLeft));
|
| +
|
| + CGEventSetIntegerValueField(
|
| + event, kCGEventSourceUserData,
|
| + views::kCocoaWindowMoveLoopSimulatedEventUserData);
|
| + CGEventPost(kCGSessionEventTap, event);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace views {
|
| +
|
| +const int kCocoaWindowMoveLoopSimulatedEventUserData = 1;
|
| +
|
| +CocoaWindowMoveLoop::CocoaWindowMoveLoop(BridgedNativeWidget* owner,
|
| + gfx::Point initial_mouse_in_screen)
|
| + : owner_(owner),
|
| + run_loop_(new base::RunLoop),
|
| + initial_mouse_in_screen_(initial_mouse_in_screen),
|
| + quit_closure_(run_loop_->QuitClosure()),
|
| + weak_factory_(this) {
|
| + // AppKit continues to send mouse events to the window, but toolkit-views
|
| + // doesn't expect them during RunMoveLoop().
|
| + [BridgedContentView setIgnoreMouseEvents:YES];
|
| + owner_->SetDraggable(true);
|
| +}
|
| +
|
| +CocoaWindowMoveLoop::~CocoaWindowMoveLoop() {
|
| + owner_->SetDraggable(false);
|
| + // Note the crafted events in Run() should not make their way through to
|
| + // toolkit-views, but regular events after that should be. So stop ignoring.
|
| + [BridgedContentView setIgnoreMouseEvents:NO];
|
| +
|
| + // Handle the pathological case, where |this| is destroyed while running.
|
| + if (exit_reason_ref_) {
|
| + *exit_reason_ref_ = WINDOW_DESTROYED;
|
| + quit_closure_.Run();
|
| + }
|
| +
|
| + owner_ = nullptr;
|
| +}
|
| +
|
| +Widget::MoveLoopResult CocoaWindowMoveLoop::Run() {
|
| + LoopExitReason exit_reason = ENDED_EXTERNALLY;
|
| + exit_reason_ref_ = &exit_reason;
|
| + NSWindow* window = owner_->ns_window();
|
| +
|
| + // Move the RunLoop to the stack so our destructor can be called while it is
|
| + // running.
|
| + scoped_ptr<base::RunLoop> run_loop(std::move(run_loop_));
|
| +
|
| + // A new window may have just been created, so post an event at the session
|
| + // tap level to initiate a window drag.
|
| + SendCustomLeftMouseEvent(window, initial_mouse_in_screen_,
|
| + kCGEventLeftMouseDown);
|
| +
|
| + // Will be retained by the monitor handler block.
|
| + WeakCocoaWindowMoveLoop* weak_cocoa_window_move_loop =
|
| + [[[WeakCocoaWindowMoveLoop alloc]
|
| + initWithWeakPtr:weak_factory_.GetWeakPtr()] autorelease];
|
| +
|
| + NSEventMask mask = NSLeftMouseUpMask | NSKeyDownMask | NSLeftMouseDraggedMask;
|
| + auto handler = ^NSEvent*(NSEvent* event) {
|
| + CocoaWindowMoveLoop* strong = [weak_cocoa_window_move_loop weak].get();
|
| + if (!strong) {
|
| + // By this point CocoaWindowMoveLoop was deleted while processing this
|
| + // same event, and this event monitor was not unregistered in time. It is
|
| + // reproducible by the PressEscapeWhileDetached test.
|
| + // Continue processing the event.
|
| + return event;
|
| + }
|
| +
|
| + if ([event type] == NSLeftMouseDragged) {
|
| + // AppKit doesn't supply position updates during a drag, so post a task to
|
| + // notify observers once AppKit has moved the window.
|
| + base::MessageLoop::current()->PostTask(
|
| + FROM_HERE, base::Bind(&BridgedNativeWidget::OnPositionChanged,
|
| + base::Unretained(strong->owner_)));
|
| + return event;
|
| + }
|
| +
|
| + strong->quit_closure_.Run();
|
| + if ([event type] == NSLeftMouseUp) {
|
| + *strong->exit_reason_ref_ = MOUSE_UP;
|
| + return event; // Process the MouseUp.
|
| + }
|
| + *strong->exit_reason_ref_ = ESCAPE_PRESSED;
|
| + return nil; // Swallow the keypress.
|
| + };
|
| + id monitor =
|
| + [NSEvent addLocalMonitorForEventsMatchingMask:mask handler:handler];
|
| +
|
| + // NSKeyDownMask doesn't work inside addLocalMonitorForEventsMatchingMask:
|
| + // the event is swallowed by the window move loop before it gets to -[NSApp
|
| + // sendEvent:]. To see an escape keypress, hook in an event tap lower.
|
| +
|
| + run_loop->Run();
|
| + [NSEvent removeMonitor:monitor];
|
| +
|
| + if (exit_reason != WINDOW_DESTROYED && exit_reason != ENDED_EXTERNALLY) {
|
| + exit_reason_ref_ = nullptr; // Ensure End() doesn't replace the reason.
|
| + owner_->EndMoveLoop(); // Deletes |this|.
|
| + }
|
| +
|
| + if (exit_reason != MOUSE_UP) {
|
| + // Tell AppKit to stop moving the window. Otherwise, AppKit will refuse to
|
| + // start a new window drag. Note the window being dragged is going away in
|
| + // this case, so it doesn't really matter where the event is located.
|
| +
|
| + if (exit_reason == ENDED_EXTERNALLY) {
|
| + // When not canceled, the non-moving drag in the original window must
|
| + // resume. To do this, AppKit needs to see a mouseDown so that it sends
|
| + // the correct events. Ideally, it also needs to be at the original
|
| + // offset, so that it hits a non-draggable region of the original
|
| + // window: The tab being dragged may move some distance from the cursor
|
| + // when it "snaps in", so the cursor may not be over a tab. Sadly, this
|
| + // method doesn't know which window that is. But all that really needs
|
| + // to be done is to prevent a custom-dragging area from starting a
|
| + // window-drag. So hook into the logic in RepostEventIfHandledByWindow()
|
| + // by setting a flag here. Note this assumes the custom mouseDown event
|
| + // is guaranteed to hit another BridgedNativeWidget when it gets to the
|
| + // front of the event queue.
|
| + // TODO(tapted): A better fix would be to keep the temporary window
|
| + // around and never call EndMoveLoop() on Mac, making this block of code
|
| + // obsolete.
|
| + BridgedNativeWidget::IgnoreNextMouseDownForDraggableRegions();
|
| + SendCustomLeftMouseEvent(nil, GetMousePosition(),
|
| + kCGEventLeftMouseDown);
|
| + } else {
|
| + SendCustomLeftMouseEvent(window, GetMousePosition(),
|
| + kCGEventLeftMouseUp);
|
| + }
|
| + }
|
| +
|
| + return exit_reason == ESCAPE_PRESSED ? Widget::MOVE_LOOP_CANCELED
|
| + : Widget::MOVE_LOOP_SUCCESSFUL;
|
| +}
|
| +
|
| +void CocoaWindowMoveLoop::End() {
|
| + if (exit_reason_ref_) {
|
| + DCHECK_EQ(*exit_reason_ref_, ENDED_EXTERNALLY);
|
| + // Ensure the destructor doesn't replace the reason.
|
| + exit_reason_ref_ = nullptr;
|
| + quit_closure_.Run();
|
| + }
|
| +}
|
| +
|
| +} // namespace views
|
|
|