Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(462)

Unified Diff: ui/views/cocoa/cocoa_window_move_loop.mm

Issue 1877043003: [EXPERIMENT] MacViews: Implement Tab Dragging Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: git cl format Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/cocoa/cocoa_window_move_loop.h ('k') | ui/views/cocoa/views_nswindow_delegate.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..f3ce216bda7eacaa44129f9e03eed2315244600c
--- /dev/null
+++ b/ui/views/cocoa/cocoa_window_move_loop.mm
@@ -0,0 +1,190 @@
+// 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"
+
+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),
+ initial_mouse_in_screen_(initial_mouse_in_screen),
+ 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 = RUNNING;
+ exit_reason_ref_ = &exit_reason;
+ NSWindow* window = owner_->ns_window();
+
+ base::RunLoop run_loop;
+ quit_closure_ = run_loop.QuitClosure();
+
+ // 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);
+
+ // We can't use __block on |exit_reason| otherwise the location on the stack
+ // will change after it gets captured. We also can't use |exit_reason_ref_|
+ // because the class destructor can be called while still in Run(). So copy
+ // the stack address a second time and capture it by value.
+ LoopExitReason* exit_reason_in_block = &exit_reason;
+ NSEventMask mask = NSLeftMouseUpMask | NSKeyDownMask | NSLeftMouseDraggedMask;
+ auto handler = ^NSEvent*(NSEvent* event) {
+ if (*exit_reason_in_block != RUNNING) {
+ // 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(&CocoaWindowMoveLoop::OnPositionChanged,
+ weak_factory_.GetWeakPtr()));
+ return event;
+ }
+
+ quit_closure_.Run();
+ if ([event type] == NSLeftMouseUp) {
+ *exit_reason_in_block = MOUSE_UP;
+ return event; // Process the MouseUp.
+ }
+ *exit_reason_in_block = 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();
+ DCHECK_NE(RUNNING, exit_reason);
+ [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_, RUNNING);
+ *exit_reason_ref_ = ENDED_EXTERNALLY;
+ // Ensure the destructor doesn't replace the reason.
+ exit_reason_ref_ = nullptr;
+ quit_closure_.Run();
+ }
+}
+
+void CocoaWindowMoveLoop::OnPositionChanged() {
+ return owner_->OnPositionChanged();
+}
+
+} // namespace views
« no previous file with comments | « ui/views/cocoa/cocoa_window_move_loop.h ('k') | ui/views/cocoa/views_nswindow_delegate.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698