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/panels/panel_resize_controller.h" | 5 #include "chrome/browser/ui/panels/panel_resize_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/ui/panels/panel.h" | 8 #include "chrome/browser/ui/panels/panel.h" |
9 #include "chrome/browser/ui/panels/panel_manager.h" | 9 #include "chrome/browser/ui/panels/panel_manager.h" |
| 10 #include "ui/base/hit_test.h" |
10 | 11 |
11 namespace { | 12 namespace { |
12 static bool ResizingLeft(panel::ResizingSides sides) { | 13 bool ResizingLeft(int component) { |
13 return sides == panel::RESIZE_TOP_LEFT || | 14 return component == HTTOPLEFT || |
14 sides == panel::RESIZE_LEFT || | 15 component == HTLEFT || |
15 sides == panel::RESIZE_BOTTOM_LEFT; | 16 component == HTBOTTOMLEFT; |
16 } | 17 } |
17 | 18 |
18 static bool ResizingRight(panel::ResizingSides sides) { | 19 bool ResizingRight(int component) { |
19 return sides == panel::RESIZE_TOP_RIGHT || | 20 return component == HTTOPRIGHT || |
20 sides == panel::RESIZE_RIGHT || | 21 component == HTRIGHT || |
21 sides == panel::RESIZE_BOTTOM_RIGHT; | 22 component == HTBOTTOMRIGHT; |
22 } | 23 } |
23 | 24 |
24 static bool ResizingTop(panel::ResizingSides sides) { | 25 bool ResizingTop(int component) { |
25 return sides == panel::RESIZE_TOP_LEFT || | 26 return component == HTTOPLEFT || |
26 sides == panel::RESIZE_TOP || | 27 component == HTTOP || |
27 sides == panel::RESIZE_TOP_RIGHT; | 28 component == HTTOPRIGHT; |
28 } | 29 } |
29 | 30 |
30 static bool ResizingBottom(panel::ResizingSides sides) { | 31 bool ResizingBottom(int component) { |
31 return sides == panel::RESIZE_BOTTOM_RIGHT || | 32 return component == HTBOTTOMRIGHT || |
32 sides == panel::RESIZE_BOTTOM || | 33 component == HTBOTTOM || |
33 sides == panel::RESIZE_BOTTOM_LEFT; | 34 component == HTBOTTOMLEFT; |
34 } | 35 } |
35 } | 36 } // namespace |
36 | 37 |
37 PanelResizeController::PanelResizeController(PanelManager* panel_manager) | 38 PanelResizeController::PanelResizeController(PanelManager* panel_manager) |
38 : panel_manager_(panel_manager), | 39 : panel_manager_(panel_manager), |
39 resizing_panel_(NULL), | 40 resizing_panel_(NULL), |
40 sides_resized_(panel::RESIZE_NONE) { | 41 component_(HTNOWHERE) { |
41 } | 42 } |
42 | 43 |
43 void PanelResizeController::StartResizing(Panel* panel, | 44 void PanelResizeController::StartResizing(Panel* panel, |
44 const gfx::Point& mouse_location, | 45 const gfx::Point& mouse_location, |
45 panel::ResizingSides sides) { | 46 int component) { |
46 DCHECK(!IsResizing()); | 47 DCHECK(!IsResizing()); |
47 DCHECK_NE(panel::RESIZE_NONE, sides); | 48 DCHECK_NE(HTNOWHERE, component); |
48 | 49 |
49 panel::Resizability resizability = panel->CanResizeByMouse(); | 50 panel::Resizability resizability = panel->CanResizeByMouse(); |
50 DCHECK_NE(panel::NOT_RESIZABLE, resizability); | 51 DCHECK_NE(panel::NOT_RESIZABLE, resizability); |
51 panel::Resizability resizability_to_test; | 52 panel::Resizability resizability_to_test; |
52 switch (sides) { | 53 switch (component) { |
53 case panel::RESIZE_TOP_LEFT: | 54 case HTTOPLEFT: |
54 resizability_to_test = panel::RESIZABLE_TOP_LEFT; | 55 resizability_to_test = panel::RESIZABLE_TOP_LEFT; |
55 break; | 56 break; |
56 case panel::RESIZE_TOP: | 57 case HTTOP: |
57 resizability_to_test = panel::RESIZABLE_TOP; | 58 resizability_to_test = panel::RESIZABLE_TOP; |
58 break; | 59 break; |
59 case panel::RESIZE_TOP_RIGHT: | 60 case HTTOPRIGHT: |
60 resizability_to_test = panel::RESIZABLE_TOP_RIGHT; | 61 resizability_to_test = panel::RESIZABLE_TOP_RIGHT; |
61 break; | 62 break; |
62 case panel::RESIZE_LEFT: | 63 case HTLEFT: |
63 resizability_to_test = panel::RESIZABLE_LEFT; | 64 resizability_to_test = panel::RESIZABLE_LEFT; |
64 break; | 65 break; |
65 case panel::RESIZE_RIGHT: | 66 case HTRIGHT: |
66 resizability_to_test = panel::RESIZABLE_RIGHT; | 67 resizability_to_test = panel::RESIZABLE_RIGHT; |
67 break; | 68 break; |
68 case panel::RESIZE_BOTTOM_LEFT: | 69 case HTBOTTOMLEFT: |
69 resizability_to_test = panel::RESIZABLE_BOTTOM_LEFT; | 70 resizability_to_test = panel::RESIZABLE_BOTTOM_LEFT; |
70 break; | 71 break; |
71 case panel::RESIZE_BOTTOM: | 72 case HTBOTTOM: |
72 resizability_to_test = panel::RESIZABLE_BOTTOM; | 73 resizability_to_test = panel::RESIZABLE_BOTTOM; |
73 break; | 74 break; |
74 case panel::RESIZE_BOTTOM_RIGHT: | 75 case HTBOTTOMRIGHT: |
75 resizability_to_test = panel::RESIZABLE_BOTTOM_RIGHT; | 76 resizability_to_test = panel::RESIZABLE_BOTTOM_RIGHT; |
76 break; | 77 break; |
77 default: | 78 default: |
78 resizability_to_test = panel::NOT_RESIZABLE; | 79 resizability_to_test = panel::NOT_RESIZABLE; |
79 break; | 80 break; |
80 } | 81 } |
81 if ((resizability & resizability_to_test) == 0) { | 82 if ((resizability & resizability_to_test) == 0) { |
82 DLOG(WARNING) << "Resizing not allowed. Is this a test?"; | 83 DLOG(WARNING) << "Resizing not allowed. Is this a test?"; |
83 return; | 84 return; |
84 } | 85 } |
85 | 86 |
86 mouse_location_at_start_ = mouse_location; | 87 mouse_location_at_start_ = mouse_location; |
87 bounds_at_start_ = panel->GetBounds(); | 88 bounds_at_start_ = panel->GetBounds(); |
88 sides_resized_ = sides; | 89 component_ = component; |
89 resizing_panel_ = panel; | 90 resizing_panel_ = panel; |
90 resizing_panel_->OnPanelStartUserResizing(); | 91 resizing_panel_->OnPanelStartUserResizing(); |
91 } | 92 } |
92 | 93 |
93 void PanelResizeController::Resize(const gfx::Point& mouse_location) { | 94 void PanelResizeController::Resize(const gfx::Point& mouse_location) { |
94 DCHECK(IsResizing()); | 95 DCHECK(IsResizing()); |
95 panel::Resizability resizability = resizing_panel_->CanResizeByMouse(); | 96 panel::Resizability resizability = resizing_panel_->CanResizeByMouse(); |
96 if (panel::NOT_RESIZABLE == resizability) { | 97 if (panel::NOT_RESIZABLE == resizability) { |
97 EndResizing(false); | 98 EndResizing(false); |
98 return; | 99 return; |
99 } | 100 } |
100 gfx::Rect bounds = resizing_panel_->GetBounds(); | 101 gfx::Rect bounds = resizing_panel_->GetBounds(); |
101 | 102 |
102 if (ResizingRight(sides_resized_)) { | 103 if (ResizingRight(component_)) { |
103 bounds.set_width(std::max(bounds_at_start_.width() + | 104 bounds.set_width(std::max(bounds_at_start_.width() + |
104 mouse_location.x() - mouse_location_at_start_.x(), 0)); | 105 mouse_location.x() - mouse_location_at_start_.x(), 0)); |
105 } | 106 } |
106 if (ResizingBottom(sides_resized_)) { | 107 if (ResizingBottom(component_)) { |
107 bounds.set_height(std::max(bounds_at_start_.height() + | 108 bounds.set_height(std::max(bounds_at_start_.height() + |
108 mouse_location.y() - mouse_location_at_start_.y(), 0)); | 109 mouse_location.y() - mouse_location_at_start_.y(), 0)); |
109 } | 110 } |
110 if (ResizingLeft(sides_resized_)) { | 111 if (ResizingLeft(component_)) { |
111 bounds.set_width(std::max(bounds_at_start_.width() + | 112 bounds.set_width(std::max(bounds_at_start_.width() + |
112 mouse_location_at_start_.x() - mouse_location.x(), 0)); | 113 mouse_location_at_start_.x() - mouse_location.x(), 0)); |
113 } | 114 } |
114 if (ResizingTop(sides_resized_)) { | 115 if (ResizingTop(component_)) { |
115 int new_height = std::max(bounds_at_start_.height() + | 116 int new_height = std::max(bounds_at_start_.height() + |
116 mouse_location_at_start_.y() - mouse_location.y(), 0); | 117 mouse_location_at_start_.y() - mouse_location.y(), 0); |
117 int new_y = bounds_at_start_.bottom() - new_height; | 118 int new_y = bounds_at_start_.bottom() - new_height; |
118 | 119 |
119 // Make sure that the panel's titlebar cannot be resized under the taskbar | 120 // Make sure that the panel's titlebar cannot be resized under the taskbar |
120 // or OSX menu bar that is aligned to top screen edge. | 121 // or OSX menu bar that is aligned to top screen edge. |
121 gfx::Rect display_area = panel_manager_->display_settings_provider()-> | 122 gfx::Rect display_area = panel_manager_->display_settings_provider()-> |
122 GetDisplayAreaMatching(bounds); | 123 GetDisplayAreaMatching(bounds); |
123 gfx::Rect work_area = panel_manager_->display_settings_provider()-> | 124 gfx::Rect work_area = panel_manager_->display_settings_provider()-> |
124 GetWorkAreaMatching(bounds); | 125 GetWorkAreaMatching(bounds); |
125 if (display_area.y() <= mouse_location.y() && | 126 if (display_area.y() <= mouse_location.y() && |
126 mouse_location.y() < work_area.y()) { | 127 mouse_location.y() < work_area.y()) { |
127 new_height -= work_area.y() - new_y; | 128 new_height -= work_area.y() - new_y; |
128 } | 129 } |
129 | 130 |
130 bounds.set_height(new_height); | 131 bounds.set_height(new_height); |
131 } | 132 } |
132 | 133 |
133 resizing_panel_->IncreaseMaxSize(bounds.size()); | 134 resizing_panel_->IncreaseMaxSize(bounds.size()); |
134 | 135 |
135 // This effectively only clamps using the min size, since the max_size was | 136 // This effectively only clamps using the min size, since the max_size was |
136 // updated above. | 137 // updated above. |
137 bounds.set_size(resizing_panel_->ClampSize(bounds.size())); | 138 bounds.set_size(resizing_panel_->ClampSize(bounds.size())); |
138 | 139 |
139 if (ResizingLeft(sides_resized_)) | 140 if (ResizingLeft(component_)) |
140 bounds.set_x(bounds_at_start_.right() - bounds.width()); | 141 bounds.set_x(bounds_at_start_.right() - bounds.width()); |
141 | 142 |
142 if (ResizingTop(sides_resized_)) | 143 if (ResizingTop(component_)) |
143 bounds.set_y(bounds_at_start_.bottom() - bounds.height()); | 144 bounds.set_y(bounds_at_start_.bottom() - bounds.height()); |
144 | 145 |
145 if (bounds != resizing_panel_->GetBounds()) { | 146 if (bounds != resizing_panel_->GetBounds()) { |
146 resizing_panel_->SetPanelBoundsInstantly(bounds); | 147 resizing_panel_->SetPanelBoundsInstantly(bounds); |
147 resizing_panel_->OnWindowResizedByMouse(bounds); | 148 resizing_panel_->OnWindowResizedByMouse(bounds); |
148 } | 149 } |
149 } | 150 } |
150 | 151 |
151 Panel* PanelResizeController::EndResizing(bool cancelled) { | 152 Panel* PanelResizeController::EndResizing(bool cancelled) { |
152 DCHECK(IsResizing()); | 153 DCHECK(IsResizing()); |
153 | 154 |
154 if (cancelled) { | 155 if (cancelled) { |
155 resizing_panel_->SetPanelBoundsInstantly(bounds_at_start_); | 156 resizing_panel_->SetPanelBoundsInstantly(bounds_at_start_); |
156 resizing_panel_->OnWindowResizedByMouse(bounds_at_start_); | 157 resizing_panel_->OnWindowResizedByMouse(bounds_at_start_); |
157 } | 158 } |
158 | 159 |
159 // Do a thorough cleanup. | 160 // Do a thorough cleanup. |
160 resizing_panel_->OnPanelEndUserResizing(); | 161 resizing_panel_->OnPanelEndUserResizing(); |
161 Panel* resized_panel = resizing_panel_; | 162 Panel* resized_panel = resizing_panel_; |
162 resizing_panel_ = NULL; | 163 resizing_panel_ = NULL; |
163 sides_resized_ = panel::RESIZE_NONE; | 164 component_ = HTNOWHERE; |
164 bounds_at_start_ = gfx::Rect(); | 165 bounds_at_start_ = gfx::Rect(); |
165 mouse_location_at_start_ = gfx::Point(); | 166 mouse_location_at_start_ = gfx::Point(); |
166 return resized_panel; | 167 return resized_panel; |
167 } | 168 } |
168 | 169 |
169 void PanelResizeController::OnPanelClosed(Panel* panel) { | 170 void PanelResizeController::OnPanelClosed(Panel* panel) { |
170 if (!resizing_panel_) | 171 if (!resizing_panel_) |
171 return; | 172 return; |
172 | 173 |
173 // If the resizing panel is closed, abort the resize operation. | 174 // If the resizing panel is closed, abort the resize operation. |
174 if (resizing_panel_ == panel) | 175 if (resizing_panel_ == panel) |
175 EndResizing(false); | 176 EndResizing(false); |
176 } | 177 } |
OLD | NEW |