1 // Copyright (c) 2009 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 CHROME_BROWSER_UI_TABS_DOCK_INFO_H_ 6 #define CHROME_BROWSER_UI_TABS_DOCK_INFO_H_ 7 #pragma once 8 9 #include <set> 10 11 #include "ui/gfx/native_widget_types.h" 12 #include "ui/gfx/point.h" 13 #include "ui/gfx/rect.h" 14 15 // DockInfo is used to do determine possible dock locations for a dragged 16 // tab. To use DockInfo invoke GetDockInfoAtPoint. This returns a new 17 // DockInfo whose type indicates the type of dock that should occur based 18 // on the screen location. As the user drags the mouse around invoke 19 // IsValidForPoint, this returns true if the DockInfo is still valid for the 20 // new location. If the DockInfo is not valid, invoke GetDockInfoAtPoint to 21 // get the new DockInfo. Use GetNewWindowBounds to get the position to place 22 // the new window at. 23 // 24 // DockInfos are cheap and explicitly allow copy and assignment operators. 25 class DockInfo { 26 public: 27 class Factory { 28 public: 29 virtual DockInfo GetDockInfoAtPoint( 30 const gfx::Point& screen_point, 31 const std::set<gfx::NativeView>& ignore) = 0; 32 33 virtual gfx::NativeWindow GetLocalProcessWindowAtPoint( 34 const gfx::Point& screen_point, 35 const std::set<gfx::NativeView>& ignore) = 0; 36 37 protected: 38 virtual ~Factory() {} 39 }; 40 41 // Possible dock positions. 42 enum Type { 43 // Indicates there is no valid dock position for the current location. 44 NONE, 45 46 // Indicates the new window should be positioned relative to the window 47 // identified by window(). 48 LEFT_OF_WINDOW, 49 RIGHT_OF_WINDOW, 50 BOTTOM_OF_WINDOW, 51 TOP_OF_WINDOW, 52 53 // Indicates the window should be maximized on the monitor at hot_spot. 54 MAXIMIZE, 55 56 // Indicates the window should be docked to a specific side of the monitor. 57 LEFT_HALF, 58 RIGHT_HALF, 59 BOTTOM_HALF 60 }; 61 62 DockInfo() : type_(NONE), window_(NULL), in_enable_area_(false) {} 63 64 // Returns true if |screen_loc| is close to the hotspot at |x|, |y|. If the 65 // point is close enough to the hotspot true is returned and |in_enable_area| 66 // is set appropriately. 67 static bool IsCloseToPoint(const gfx::Point& screen_loc, 68 int x, 69 int y, 70 bool* in_enable_area); 71 72 // Variant of IsCloseToPoint used for monitor relative positions. 73 static bool IsCloseToMonitorPoint(const gfx::Point& screen_loc, 74 int x, 75 int y, 76 DockInfo::Type type, 77 bool* in_enable_area); 78 79 // Sets the factory. 80 static void set_factory(Factory* factory) { factory_ = factory; } 81 82 // Size of the popup window shown to indicate a valid dock location. 83 static int popup_width(); 84 static int popup_height(); 85 86 // Returns the DockInfo for the specified point |screen_point|. |ignore| 87 // contains the set of windows to ignore from consideration. This contains the 88 // dragged window as well as any windows showing possible dock locations. 89 // 90 // If there is no docking position for the specified location the returned 91 // DockInfo has a type of NONE. 92 // 93 // If a Factory has been set, the method of the same name is invoked on the 94 // Factory to determine the DockInfo. 95 static DockInfo GetDockInfoAtPoint(const gfx::Point& screen_point, 96 const std::set<gfx::NativeView>& ignore); 97 98 // Returns the top most window from the current process at |screen_point|. 99 // See GetDockInfoAtPoint for a description of |ignore|. This returns NULL if 100 // there is no window from the current process at |screen_point|, or another 101 // window obscures the topmost window from our process at |screen_point|. 102 // 103 // If a Factory has been set, the method of the same name is invoked on the 104 // Factory to determine the DockInfo. 105 static gfx::NativeWindow GetLocalProcessWindowAtPoint( 106 const gfx::Point& screen_point, 107 const std::set<gfx::NativeView>& ignore); 108 109 // Returns true if this DockInfo is valid for the specified point. This 110 // resets in_enable_area based on the new location. 111 bool IsValidForPoint(const gfx::Point& screen_point); 112 113 // Returns the bounds for the new window in |new_window_bounds|. If the new 114 // window is to be maximized, |maximize_new_window| is set to true. 115 // This returns true if type is other than NONE or the mouse isn't in the 116 // enable area, false otherwise. 117 bool GetNewWindowBounds(gfx::Rect* new_window_bounds, 118 bool* maximize_new_window) const; 119 120 // Adjust the bounds of the other window during docking. Does nothing if type 121 // is NONE, in_enable_are is false, or the type is not window relative. 122 void AdjustOtherWindowBounds() const; 123 124 // Type of docking to occur. 125 void set_type(Type type) { type_ = type; } 126 Type type() const { return type_; } 127 128 // The window to dock too. Is null for dock types that are relative to the 129 // monitor. 130 void set_window(gfx::NativeWindow window) { window_ = window; } 131 gfx::NativeWindow window() const { return window_; } 132 133 // The location of the hotspot. 134 void set_hot_spot(const gfx::Point& hot_spot) { hot_spot_ = hot_spot; } 135 const gfx::Point& hot_spot() const { return hot_spot_; } 136 137 // Bounds of the monitor. 138 void set_monitor_bounds(const gfx::Rect& monitor_bounds) { 139 monitor_bounds_ = monitor_bounds; 140 } 141 const gfx::Rect& monitor_bounds() const { return monitor_bounds_; } 142 143 // Returns the bounds of the window to show the indicator for. 144 gfx::Rect GetPopupRect() const; 145 146 // Returns true if the drop should result in docking. DockInfo maintains two 147 // states (as indicated by this boolean): 148 // 1. The mouse is close enough to the hot spot such that a visual indicator 149 // should be shown, but if the user releases the mouse docking shouldn't 150 // result. This corresponds to a value of false for in_enable_area. 151 // 2. The mouse is close enough to the hot spot such that releasing the mouse 152 // should result in docking. This corresponds to a value of true for 153 // in_enable_area. 154 void set_in_enable_area(bool in_enable_area) { 155 in_enable_area_ = in_enable_area; 156 } 157 bool in_enable_area() const { return in_enable_area_; } 158 159 // Returns true if |other| is considered equal to this. Two DockInfos are 160 // considered equal if they have the same type and same window. 161 bool equals(const DockInfo& other) const { 162 return type_ == other.type_ && window_ == other.window_ && 163 monitor_bounds_ == other.monitor_bounds_; 164 } 165 166 // If screen_loc is close enough to the hot spot given by |x| and |y|, the 167 // type and hot_spot are set from the supplied parameters. This is used 168 // internally, there is no need to invoke this otherwise. 169 bool CheckMonitorPoint(const gfx::Point& screen_loc, 170 int x, 171 int y, 172 Type type); 173 174 private: 175 // Returns the bounds of the window. 176 bool GetWindowBounds(gfx::Rect* bounds) const; 177 void SizeOtherWindowTo(const gfx::Rect& bounds) const; 178 179 Type type_; 180 gfx::NativeWindow window_; 181 gfx::Point hot_spot_; 182 gfx::Rect monitor_bounds_; 183 bool in_enable_area_; 184 185 // Factory that creates DockInfos. By default this is NULL, which gives the 186 // default behavior. 187 static Factory* factory_; 188 }; 189 190 #endif // CHROME_BROWSER_UI_TABS_DOCK_INFO_H_ 191