mouse_cursor_event_filter.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright (c) 2012 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#ifndef ASH_DISPLAY_MOUSE_CURSOR_EVENT_FILTER_H 6#define ASH_DISPLAY_MOUSE_CURSOR_EVENT_FILTER_H 7 8#include "ash/ash_export.h" 9#include "ash/display/display_controller.h" 10#include "base/compiler_specific.h" 11#include "base/gtest_prod_util.h" 12#include "base/memory/scoped_ptr.h" 13#include "ui/events/event_handler.h" 14#include "ui/gfx/rect.h" 15 16namespace aura { 17class RootWindow; 18class Window; 19} 20 21namespace ash { 22class SharedDisplayEdgeIndicator; 23 24// An event filter that controls mouse location in extended desktop 25// environment. 26class ASH_EXPORT MouseCursorEventFilter : public ui::EventHandler, 27 public DisplayController::Observer { 28 public: 29 enum MouseWarpMode { 30 WARP_ALWAYS, // Always warp the mouse when possible. 31 WARP_DRAG, // Used when dragging a window. Top and bottom 32 // corner of the shared edge is reserved for window 33 // snapping. 34 WARP_NONE, // No mouse warping. Used when resizing the window. 35 }; 36 37 MouseCursorEventFilter(); 38 virtual ~MouseCursorEventFilter(); 39 40 void set_mouse_warp_mode(MouseWarpMode mouse_warp_mode) { 41 mouse_warp_mode_ = mouse_warp_mode; 42 } 43 44 // Shows/Hide the indicator for window dragging. The |from| 45 // is the window where the dragging started. 46 void ShowSharedEdgeIndicator(aura::Window* from); 47 void HideSharedEdgeIndicator(); 48 49 // DisplayController::Observer: 50 virtual void OnDisplaysInitialized() OVERRIDE; 51 virtual void OnDisplayConfigurationChanged() OVERRIDE; 52 53 // ui::EventHandler: 54 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE; 55 56 private: 57 friend class DragWindowResizerTest; 58 friend class MouseCursorEventFilterTest; 59 FRIEND_TEST_ALL_PREFIXES(MouseCursorEventFilterTest, DoNotWarpTwice); 60 FRIEND_TEST_ALL_PREFIXES(MouseCursorEventFilterTest, SetMouseWarpModeFlag); 61 FRIEND_TEST_ALL_PREFIXES(MouseCursorEventFilterTest, 62 IndicatorBoundsTestOnRight); 63 FRIEND_TEST_ALL_PREFIXES(MouseCursorEventFilterTest, 64 IndicatorBoundsTestOnLeft); 65 FRIEND_TEST_ALL_PREFIXES(MouseCursorEventFilterTest, 66 IndicatorBoundsTestOnTopBottom); 67 FRIEND_TEST_ALL_PREFIXES(MouseCursorEventFilterTest, 68 WarpMouseDifferentScaleDisplaysInNative); 69 70 FRIEND_TEST_ALL_PREFIXES(DragWindowResizerTest, WarpMousePointer); 71 72 // Moves the cursor to the point inside the root that is closest to 73 // the point_in_screen, which is outside of the root window. 74 static void MoveCursorTo(aura::Window* root, 75 const gfx::Point& point_in_screen); 76 77 // Warps the mouse cursor to an alternate root window when the 78 // mouse location in |event|, hits the edge of the event target's root and 79 // the mouse cursor is considered to be in an alternate display. 80 // Returns true if/ the cursor was moved. 81 bool WarpMouseCursorIfNecessary(ui::MouseEvent* event); 82 83#if defined(USE_OZONE) 84 bool WarpMouseCursorInScreenCoords(aura::Window* target_root, 85 const gfx::Point& point_in_screen); 86 87#else 88 bool WarpMouseCursorInNativeCoords(const gfx::Point& point_in_native, 89 const gfx::Point& point_in_screen); 90#endif 91 92 // Update the edge/indicator bounds based on the current 93 // display configuration. 94 void UpdateHorizontalEdgeBounds(); 95 void UpdateVerticalEdgeBounds(); 96 97 // Returns the source and destination window. When the 98 // mouse_warp_mode_ is WARP_DRAG, src_window is the root window 99 // where the drag starts. When the mouse_warp_mode_ is WARP_ALWAYS, 100 // the src_window is always the primary root window, because there 101 // is no difference between moving src to dst and moving dst to src. 102 void GetSrcAndDstRootWindows(aura::Window** src_window, 103 aura::Window** dst_window); 104 105 void reset_was_mouse_warped_for_test() { was_mouse_warped_ = false; } 106 107 bool WarpMouseCursorIfNecessaryForTest(aura::Window* target_root, 108 const gfx::Point& point_in_screen); 109 110 MouseWarpMode mouse_warp_mode_; 111 112 // This flag is used to suppress the accidental mouse warp back to the 113 // original display. 114 bool was_mouse_warped_; 115 116 // The bounds for warp hole windows. |dst_indicator_bounds_| is kept 117 // in the instance for testing. 118 gfx::Rect src_indicator_bounds_; 119 gfx::Rect dst_indicator_bounds_; 120 121 gfx::Rect src_edge_bounds_in_native_; 122 gfx::Rect dst_edge_bounds_in_native_; 123 124 // The root window in which the dragging started. 125 aura::Window* drag_source_root_; 126 127 float scale_when_drag_started_; 128 129 // Shows the area where a window can be dragged in to/out from 130 // another display. 131 scoped_ptr<SharedDisplayEdgeIndicator> shared_display_edge_indicator_; 132 133 DISALLOW_COPY_AND_ASSIGN(MouseCursorEventFilter); 134}; 135 136} // namespace ash 137 138#endif // ASH_DISPLAY_MOUSE_CURSOR_EVENT_FILTER_H 139