OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_WINDOW_SIZER_H_ | 5 #ifndef CHROME_BROWSER_UI_WINDOW_SIZER_H_ |
6 #define CHROME_BROWSER_UI_WINDOW_SIZER_H_ | 6 #define CHROME_BROWSER_UI_WINDOW_SIZER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
13 #include "ui/gfx/rect.h" | 11 #include "ui/gfx/rect.h" |
14 | 12 |
15 class Browser; | 13 class Browser; |
16 | 14 |
| 15 // An interface implemented by an object that can retrieve information about |
| 16 // the monitors on the system. |
| 17 class MonitorInfoProvider { |
| 18 public: |
| 19 virtual ~MonitorInfoProvider() {} |
| 20 |
| 21 // Returns the bounds of the work area of the primary monitor. |
| 22 virtual gfx::Rect GetPrimaryMonitorWorkArea() const = 0; |
| 23 |
| 24 // Returns the bounds of the primary monitor. |
| 25 virtual gfx::Rect GetPrimaryMonitorBounds() const = 0; |
| 26 |
| 27 // Returns the bounds of the work area of the monitor that most closely |
| 28 // intersects the provided bounds. |
| 29 virtual gfx::Rect GetMonitorWorkAreaMatching( |
| 30 const gfx::Rect& match_rect) const = 0; |
| 31 }; |
| 32 |
17 /////////////////////////////////////////////////////////////////////////////// | 33 /////////////////////////////////////////////////////////////////////////////// |
18 // WindowSizer | 34 // WindowSizer |
19 // | 35 // |
20 // A class that determines the best new size and position for a window to be | 36 // A class that determines the best new size and position for a window to be |
21 // shown at based several factors, including the position and size of the last | 37 // shown at based several factors, including the position and size of the last |
22 // window of the same type, the last saved bounds of the window from the | 38 // window of the same type, the last saved bounds of the window from the |
23 // previous session, and default system metrics if neither of the above two | 39 // previous session, and default system metrics if neither of the above two |
24 // conditions exist. The system has built-in providers for monitor metrics | 40 // conditions exist. The system has built-in providers for monitor metrics |
25 // and persistent storage (using preferences) but can be overrided with mocks | 41 // and persistent storage (using preferences) but can be overrided with mocks |
26 // for testing. | 42 // for testing. |
27 // | 43 // |
28 class WindowSizer { | 44 class WindowSizer { |
29 public: | 45 public: |
30 class MonitorInfoProvider; | |
31 class StateProvider; | 46 class StateProvider; |
32 | 47 |
33 // The WindowSizer assumes ownership of these objects. | 48 // WindowSizer owns |state_provider| and will create a default |
| 49 // MonitorInfoProvider using the physical screen. |
| 50 explicit WindowSizer(StateProvider* state_provider); |
| 51 |
| 52 // WindowSizer owns |state_provider| and |monitor_info_provider|. |
| 53 // It will use the supplied monitor info provider. Used only for testing. |
34 WindowSizer(StateProvider* state_provider, | 54 WindowSizer(StateProvider* state_provider, |
35 MonitorInfoProvider* monitor_info_provider); | 55 MonitorInfoProvider* monitor_info_provider); |
| 56 |
36 virtual ~WindowSizer(); | 57 virtual ~WindowSizer(); |
37 | 58 |
38 // Static factory methods to create default MonitorInfoProvider | |
39 // instances. The returned object is owned by the caller. | |
40 static MonitorInfoProvider* CreateDefaultMonitorInfoProvider(); | |
41 | |
42 // An interface implemented by an object that can retrieve information about | |
43 // the monitors on the system. | |
44 class MonitorInfoProvider { | |
45 public: | |
46 MonitorInfoProvider() {} | |
47 virtual ~MonitorInfoProvider() {} | |
48 | |
49 // Returns the bounds of the work area of the primary monitor. | |
50 virtual gfx::Rect GetPrimaryMonitorWorkArea() const = 0; | |
51 | |
52 // Returns the bounds of the primary monitor. | |
53 virtual gfx::Rect GetPrimaryMonitorBounds() const = 0; | |
54 | |
55 // Returns the bounds of the work area of the monitor that most closely | |
56 // intersects the provided bounds. | |
57 virtual gfx::Rect GetMonitorWorkAreaMatching( | |
58 const gfx::Rect& match_rect) const = 0; | |
59 | |
60 // Ensures number and coordinates of work areas are up-to-date. You must | |
61 // call this before calling either of the below functions, as work areas can | |
62 // change while the program is running. | |
63 virtual void UpdateWorkAreas() = 0; | |
64 | |
65 // Returns the number of monitors on the system. | |
66 size_t GetMonitorCount() const { | |
67 return work_areas_.size(); | |
68 } | |
69 | |
70 // Returns the bounds of the work area of the monitor at the specified | |
71 // index. | |
72 gfx::Rect GetWorkAreaAt(size_t index) const { | |
73 return work_areas_[index]; | |
74 } | |
75 | |
76 protected: | |
77 std::vector<gfx::Rect> work_areas_; | |
78 }; | |
79 | |
80 // An interface implemented by an object that can retrieve state from either a | 59 // An interface implemented by an object that can retrieve state from either a |
81 // persistent store or an existing window. | 60 // persistent store or an existing window. |
82 class StateProvider { | 61 class StateProvider { |
83 public: | 62 public: |
84 virtual ~StateProvider() {} | 63 virtual ~StateProvider() {} |
85 | 64 |
86 // Retrieve the persisted bounds of the window. Returns true if there was | 65 // Retrieve the persisted bounds of the window. Returns true if there was |
87 // persisted data to retrieve state information, false otherwise. | 66 // persisted data to retrieve state information, false otherwise. |
88 virtual bool GetPersistentState(gfx::Rect* bounds, | 67 virtual bool GetPersistentState(gfx::Rect* bounds, |
89 gfx::Rect* work_area) const = 0; | 68 gfx::Rect* work_area) const = 0; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 // in local state preferences. Returns true if local state exists containing | 112 // in local state preferences. Returns true if local state exists containing |
134 // this information, false if this information does not exist and a default | 113 // this information, false if this information does not exist and a default |
135 // size should be used. | 114 // size should be used. |
136 bool GetSavedWindowBounds(gfx::Rect* bounds) const; | 115 bool GetSavedWindowBounds(gfx::Rect* bounds) const; |
137 | 116 |
138 // Gets the default window position and size if there is no last window and | 117 // Gets the default window position and size if there is no last window and |
139 // no saved window placement in prefs. This function determines the default | 118 // no saved window placement in prefs. This function determines the default |
140 // size based on monitor size, etc. | 119 // size based on monitor size, etc. |
141 void GetDefaultWindowBounds(gfx::Rect* default_bounds) const; | 120 void GetDefaultWindowBounds(gfx::Rect* default_bounds) const; |
142 | 121 |
143 // Returns true if the specified position is "offscreen" for the given edge, | |
144 // meaning that it's outside all work areas in the direction of that edge. | |
145 bool PositionIsOffscreen(int position, Edge edge) const; | |
146 | |
147 // Adjusts |bounds| to be visible onscreen, biased toward the work area of the | 122 // Adjusts |bounds| to be visible onscreen, biased toward the work area of the |
148 // monitor containing |other_bounds|. Despite the name, this doesn't | 123 // monitor containing |other_bounds|. Despite the name, this doesn't |
149 // guarantee the bounds are fully contained within this monitor's work rect; | 124 // guarantee the bounds are fully contained within this monitor's work rect; |
150 // it just tried to ensure the edges are visible on _some_ work rect. | 125 // it just tried to ensure the edges are visible on _some_ work rect. |
151 // If |saved_work_area| is non-empty, it is used to determine whether the | 126 // If |saved_work_area| is non-empty, it is used to determine whether the |
152 // monitor cofiguration has changed. If it has, bounds are repositioned and | 127 // monitor cofiguration has changed. If it has, bounds are repositioned and |
153 // resized if necessary to make them completely contained in the current work | 128 // resized if necessary to make them completely contained in the current work |
154 // area. | 129 // area. |
155 void AdjustBoundsToBeVisibleOnMonitorContaining( | 130 void AdjustBoundsToBeVisibleOnMonitorContaining( |
156 const gfx::Rect& other_bounds, | 131 const gfx::Rect& other_bounds, |
157 const gfx::Rect& saved_work_area, | 132 const gfx::Rect& saved_work_area, |
158 gfx::Rect* bounds) const; | 133 gfx::Rect* bounds) const; |
159 | 134 |
160 // Providers for persistent storage and monitor metrics. | 135 // Providers for persistent storage and monitor metrics. |
161 scoped_ptr<StateProvider> state_provider_; | 136 scoped_ptr<StateProvider> state_provider_; |
162 scoped_ptr<MonitorInfoProvider> monitor_info_provider_; | 137 scoped_ptr<MonitorInfoProvider> monitor_info_provider_; |
163 | 138 |
164 DISALLOW_COPY_AND_ASSIGN(WindowSizer); | 139 DISALLOW_COPY_AND_ASSIGN(WindowSizer); |
165 }; | 140 }; |
166 | 141 |
167 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_H_ | 142 #endif // CHROME_BROWSER_UI_WINDOW_SIZER_H_ |
OLD | NEW |