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.
5 #include "ash/wm/workspace/snap_sizer.h"
9 #include "ash/screen_ash.h"
10 #include "ash/wm/property_util.h"
11 #include "ash/wm/window_resizer.h"
12 #include "ash/wm/window_util.h"
13 #include "ui/aura/window.h"
14 #include "ui/gfx/screen.h"
21 // A list of ideal window width in pixel which will be used to populate the
22 // |usable_width_| list.
23 const int kIdealWidth
[] = { 1280, 1024, 768, 640 };
25 // Windows are initially snapped to the size in |usable_width_| at index 0.
26 // The index into |usable_width_| is changed if any of the following happen:
27 // . The user stops moving the mouse for |kDelayBeforeIncreaseMS| and then
28 // moves the mouse again.
29 // . The mouse moves |kPixelsBeforeAdjust| horizontal pixels.
30 // . The mouse is against the edge of the screen and the mouse is moved
31 // |kMovesBeforeAdjust| times.
32 const int kDelayBeforeIncreaseMS
= 500;
33 const int kMovesBeforeAdjust
= 25;
34 const int kPixelsBeforeAdjust
= 100;
36 // When the smallest resolution does not fit on the screen, we take this
37 // fraction of the available space.
38 const int kMinimumScreenPercent
= 90;
40 // Create the list of possible width for the current screen configuration:
41 // Fill the |usable_width_| list with items from |kIdealWidth| which fit on
42 // the screen and supplement it with the 'half of screen' size. Furthermore,
43 // add an entry for 90% of the screen size if it is smaller then the biggest
44 // value in the |kIdealWidth| list (to get a step between the values).
45 std::vector
<int> BuildIdealWidthList(aura::Window
* window
) {
46 std::vector
<int> ideal_width_list
;
47 gfx::Rect
work_area(ScreenAsh::GetDisplayWorkAreaBoundsInParent(window
));
48 int half_size
= work_area
.width() / 2;
49 int maximum_width
= (kMinimumScreenPercent
* work_area
.width()) / 100;
50 for (size_t i
= 0; i
< arraysize(kIdealWidth
); i
++) {
51 if (maximum_width
>= kIdealWidth
[i
]) {
52 if (i
&& !ideal_width_list
.size() && maximum_width
!= kIdealWidth
[i
])
53 ideal_width_list
.push_back(maximum_width
);
54 if (half_size
> kIdealWidth
[i
])
55 ideal_width_list
.push_back(half_size
);
56 if (half_size
>= kIdealWidth
[i
])
58 ideal_width_list
.push_back(kIdealWidth
[i
]);
62 ideal_width_list
.push_back(half_size
);
64 return ideal_width_list
;
69 SnapSizer::SnapSizer(aura::Window
* window
,
70 const gfx::Point
& start
,
75 time_last_update_(base::TimeTicks::Now()),
77 resize_disabled_(false),
78 num_moves_since_adjust_(0),
79 last_adjust_x_(start
.x()),
80 last_update_x_(start
.x()),
82 input_type_(input_type
),
83 usable_width_(BuildIdealWidthList(window
)) {
84 DCHECK(!usable_width_
.empty());
85 target_bounds_
= GetTargetBounds();
88 SnapSizer::~SnapSizer() {
91 void SnapSizer::SnapWindow(aura::Window
* window
, SnapSizer::Edge edge
) {
92 if (!wm::CanSnapWindow(window
))
94 internal::SnapSizer
sizer(window
, gfx::Point(), edge
,
95 internal::SnapSizer::OTHER_INPUT
);
96 if (wm::IsWindowFullscreen(window
) || wm::IsWindowMaximized(window
)) {
97 // Before we can set the bounds we need to restore the window.
98 // Restoring the window will set the window to its restored bounds.
99 // To avoid an unnecessary bounds changes (which may have side effects)
100 // we set the restore bounds to the bounds we want, restore the window,
101 // then reset the restore bounds. This way no unnecessary bounds
102 // changes occurs and the original restore bounds is remembered.
103 gfx::Rect restore
= *GetRestoreBoundsInScreen(window
);
104 SetRestoreBoundsInParent(window
, sizer
.GetSnapBounds(window
->bounds()));
105 wm::RestoreWindow(window
);
106 SetRestoreBoundsInScreen(window
, restore
);
108 window
->SetBounds(sizer
.GetSnapBounds(window
->bounds()));
112 void SnapSizer::Update(const gfx::Point
& location
) {
113 // See description above for details on this behavior.
114 num_moves_since_adjust_
++;
115 if ((base::TimeTicks::Now() - time_last_update_
).InMilliseconds() >
116 kDelayBeforeIncreaseMS
) {
117 ChangeBounds(location
.x(),
118 CalculateIncrement(location
.x(), last_update_x_
));
120 bool along_edge
= AlongEdge(location
.x());
121 int pixels_before_adjust
= kPixelsBeforeAdjust
;
122 if (input_type_
== TOUCH_MAXIMIZE_BUTTON_INPUT
) {
123 const gfx::Rect
& workspace_bounds
= window_
->parent()->bounds();
124 if (start_x_
> location
.x()) {
125 pixels_before_adjust
=
126 std::min(pixels_before_adjust
, start_x_
/ 10);
128 pixels_before_adjust
=
129 std::min(pixels_before_adjust
,
130 (workspace_bounds
.width() - start_x_
) / 10);
133 if (std::abs(location
.x() - last_adjust_x_
) >= pixels_before_adjust
||
134 (along_edge
&& num_moves_since_adjust_
>= kMovesBeforeAdjust
)) {
135 ChangeBounds(location
.x(),
136 CalculateIncrement(location
.x(), last_adjust_x_
));
139 last_update_x_
= location
.x();
140 time_last_update_
= base::TimeTicks::Now();
143 gfx::Rect
SnapSizer::GetSnapBounds(const gfx::Rect
& bounds
) {
145 if (!resize_disabled_
) {
146 for (current
= usable_width_
.size() - 1; current
>= 0; current
--) {
147 gfx::Rect target
= GetTargetBoundsForSize(current
);
148 if (target
== bounds
) {
154 return GetTargetBoundsForSize(current
% usable_width_
.size());
157 void SnapSizer::SelectDefaultSizeAndDisableResize() {
158 resize_disabled_
= true;
160 target_bounds_
= GetTargetBounds();
163 gfx::Rect
SnapSizer::GetTargetBoundsForSize(size_t size_index
) const {
164 gfx::Rect
work_area(ScreenAsh::GetDisplayWorkAreaBoundsInParent(window_
));
165 int y
= work_area
.y();
166 // We don't align to the bottom of the grid as the launcher may not
167 // necessarily align to the grid (happens when auto-hidden).
168 int max_y
= work_area
.bottom();
170 if (resize_disabled_
) {
171 // Make sure that we keep the size of the window smaller then a certain
172 // fraction of the screen space.
173 int minimum_size
= (kMinimumScreenPercent
* work_area
.width()) / 100;
174 width
= std::max(std::min(minimum_size
, 1024), work_area
.width() / 2);
176 DCHECK(size_index
< usable_width_
.size());
177 width
= usable_width_
[size_index
];
180 if (edge_
== LEFT_EDGE
) {
181 int x
= work_area
.x();
182 int mid_x
= x
+ width
;
183 return gfx::Rect(x
, y
, mid_x
- x
, max_y
- y
);
185 int max_x
= work_area
.right();
186 int x
= max_x
- width
;
187 return gfx::Rect(x
, y
, max_x
- x
, max_y
- y
);
190 int SnapSizer::CalculateIncrement(int x
, int reference_x
) const {
193 if (x
== reference_x
)
195 if (edge_
== LEFT_EDGE
) {
200 // edge_ == RIGHT_EDGE.
206 void SnapSizer::ChangeBounds(int x
, int delta
) {
207 int index
= std::min(static_cast<int>(usable_width_
.size()) - 1,
208 std::max(size_index_
+ delta
, 0));
209 if (index
!= size_index_
) {
211 target_bounds_
= GetTargetBounds();
213 num_moves_since_adjust_
= 0;
217 gfx::Rect
SnapSizer::GetTargetBounds() const {
218 return GetTargetBoundsForSize(size_index_
);
221 bool SnapSizer::AlongEdge(int x
) const {
222 gfx::Rect
area(ScreenAsh::GetDisplayBoundsInParent(window_
));
223 return (x
<= area
.x()) || (x
>= area
.right() - 1);
226 } // namespace internal