OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/ui/views/ash/window_positioner.h" | 5 #include "chrome/browser/ui/views/ash/window_positioner.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/wm/window_cycle_controller.h" | 8 #include "ash/wm/window_cycle_controller.h" |
| 9 #include "ash/wm/window_resizer.h" |
9 #include "ash/wm/window_util.h" | 10 #include "ash/wm/window_util.h" |
10 #include "ui/aura/window.h" | 11 #include "ui/aura/window.h" |
11 #include "ui/aura/window_delegate.h" | 12 #include "ui/aura/window_delegate.h" |
12 #include "ui/gfx/compositor/layer.h" | 13 #include "ui/gfx/compositor/layer.h" |
13 #include "ui/gfx/screen.h" | 14 #include "ui/gfx/screen.h" |
14 | 15 |
15 WindowPositioner::WindowPositioner() | 16 WindowPositioner::WindowPositioner() |
16 : pop_position_offset_increment_x(0), | 17 : pop_position_offset_increment_x(0), |
17 pop_position_offset_increment_y(0), | 18 pop_position_offset_increment_y(0), |
18 popup_position_offset_from_screen_corner_x(0), | 19 popup_position_offset_from_screen_corner_x(0), |
(...skipping 29 matching lines...) Expand all Loading... |
48 aura::Window* window = ash::wm::GetActiveWindow(); | 49 aura::Window* window = ash::wm::GetActiveWindow(); |
49 const gfx::Rect work_area = window && window->IsVisible() ? | 50 const gfx::Rect work_area = window && window->IsVisible() ? |
50 gfx::Screen::GetMonitorWorkAreaNearestWindow(window) : | 51 gfx::Screen::GetMonitorWorkAreaNearestWindow(window) : |
51 gfx::Screen::GetPrimaryMonitorWorkArea(); | 52 gfx::Screen::GetPrimaryMonitorWorkArea(); |
52 // Only try to reposition the popup when it is not spanning the entire | 53 // Only try to reposition the popup when it is not spanning the entire |
53 // screen. | 54 // screen. |
54 if ((old_pos.width() + popup_position_offset_from_screen_corner_x >= | 55 if ((old_pos.width() + popup_position_offset_from_screen_corner_x >= |
55 work_area.width()) || | 56 work_area.width()) || |
56 (old_pos.height() + popup_position_offset_from_screen_corner_y >= | 57 (old_pos.height() + popup_position_offset_from_screen_corner_y >= |
57 work_area.height())) | 58 work_area.height())) |
58 return old_pos; | 59 return AlignPopupPosition(old_pos, work_area, grid); |
59 const gfx::Rect result = SmartPopupPosition(old_pos, work_area); | 60 const gfx::Rect result = SmartPopupPosition(old_pos, work_area, grid); |
60 if (!result.IsEmpty()) | 61 if (!result.IsEmpty()) |
61 return result; | 62 return AlignPopupPosition(result, work_area, grid); |
62 return NormalPopupPosition(old_pos, work_area); | 63 return NormalPopupPosition(old_pos, work_area); |
63 } | 64 } |
64 | 65 |
65 gfx::Rect WindowPositioner::NormalPopupPosition( | 66 gfx::Rect WindowPositioner::NormalPopupPosition( |
66 const gfx::Rect& old_pos, | 67 const gfx::Rect& old_pos, |
67 const gfx::Rect& work_area) { | 68 const gfx::Rect& work_area) { |
68 int w = old_pos.width(); | 69 int w = old_pos.width(); |
69 int h = old_pos.height(); | 70 int h = old_pos.height(); |
70 // Note: The 'last_popup_position' is checked and kept relative to the | 71 // Note: The 'last_popup_position' is checked and kept relative to the |
71 // screen size. The offsetting will be done in the last step when the | 72 // screen size. The offsetting will be done in the last step when the |
(...skipping 18 matching lines...) Expand all Loading... |
90 int y = last_popup_position_y_; | 91 int y = last_popup_position_y_; |
91 if (!reset) { | 92 if (!reset) { |
92 last_popup_position_x_ += pop_position_offset_increment_x; | 93 last_popup_position_x_ += pop_position_offset_increment_x; |
93 last_popup_position_y_ += pop_position_offset_increment_y; | 94 last_popup_position_y_ += pop_position_offset_increment_y; |
94 } | 95 } |
95 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h); | 96 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h); |
96 } | 97 } |
97 | 98 |
98 gfx::Rect WindowPositioner::SmartPopupPosition( | 99 gfx::Rect WindowPositioner::SmartPopupPosition( |
99 const gfx::Rect& old_pos, | 100 const gfx::Rect& old_pos, |
100 const gfx::Rect& work_area) { | 101 const gfx::Rect& work_area, |
| 102 int grid) { |
101 const std::vector<aura::Window*> windows = | 103 const std::vector<aura::Window*> windows = |
102 ash::WindowCycleController::BuildWindowList(); | 104 ash::WindowCycleController::BuildWindowList(); |
103 | 105 |
104 std::vector<const gfx::Rect*> regions; | 106 std::vector<const gfx::Rect*> regions; |
105 // Process the window list and check if we can bail immediately. | 107 // Process the window list and check if we can bail immediately. |
106 for (size_t i = 0; i < windows.size(); i++) { | 108 for (size_t i = 0; i < windows.size(); i++) { |
107 // We only include opaque and visible windows. | 109 // We only include opaque and visible windows. |
108 if (windows[i] && windows[i]->IsVisible() && windows[i]->layer() && | 110 if (windows[i] && windows[i]->IsVisible() && windows[i]->layer() && |
109 (!windows[i]->transparent() || | 111 (!windows[i]->transparent() || |
110 windows[i]->layer()->GetTargetOpacity() == 1.0)) { | 112 windows[i]->layer()->GetTargetOpacity() == 1.0)) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } | 145 } |
144 // Note: The passing (x,y,w,h) window is always relative to the work area's | 146 // Note: The passing (x,y,w,h) window is always relative to the work area's |
145 // origin. | 147 // origin. |
146 for (; x_increment > 0 ? (x < x_end) : (x > x_end); x += x_increment) { | 148 for (; x_increment > 0 ? (x < x_end) : (x > x_end); x += x_increment) { |
147 int y = 0; | 149 int y = 0; |
148 while (y + h <= work_area.height()) { | 150 while (y + h <= work_area.height()) { |
149 size_t i; | 151 size_t i; |
150 for (i = 0; i < regions.size(); i++) { | 152 for (i = 0; i < regions.size(); i++) { |
151 if (regions[i]->Intersects(gfx::Rect(x + work_area.x(), | 153 if (regions[i]->Intersects(gfx::Rect(x + work_area.x(), |
152 y + work_area.y(), w, h))) { | 154 y + work_area.y(), w, h))) { |
153 y = regions[i]->y() + regions[i]->height() - work_area.y(); | 155 y = regions[i]->bottom() - work_area.y(); |
| 156 if (grid > 1) { |
| 157 // Align to the (next) grid step. |
| 158 y = ash::WindowResizer::AlignToGridRoundUp(y, grid); |
| 159 } |
154 break; | 160 break; |
155 } | 161 } |
156 } | 162 } |
157 if (i >= regions.size()) | 163 if (i >= regions.size()) |
158 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h); | 164 return gfx::Rect(x + work_area.x(), y + work_area.y(), w, h); |
159 } | 165 } |
160 } | 166 } |
161 } | 167 } |
162 return gfx::Rect(0, 0, 0, 0); | 168 return gfx::Rect(0, 0, 0, 0); |
163 } | 169 } |
| 170 |
| 171 gfx::Rect WindowPositioner::AlignPopupPosition( |
| 172 const gfx::Rect& pos, |
| 173 const gfx::Rect& work_area, |
| 174 int grid) { |
| 175 if (grid <= 1) |
| 176 return pos; |
| 177 |
| 178 int x = pos.x() - (pos.x() - work_area.x()) % grid; |
| 179 int y = pos.y() - (pos.y() - work_area.y()) % grid; |
| 180 int w = pos.width(); |
| 181 int h = pos.height(); |
| 182 |
| 183 // If the alignment was pushing the window out of the screen, we ignore the |
| 184 // alignment for that call. |
| 185 if (abs(pos.right() - work_area.right()) < grid) |
| 186 x = work_area.right() - w; |
| 187 if (abs(pos.bottom() - work_area.bottom()) < grid) |
| 188 y = work_area.bottom() - h; |
| 189 return gfx::Rect(x, y, w, h); |
| 190 } |
OLD | NEW |