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/dropdown_bar_host.h" | 5 #include "chrome/browser/ui/views/dropdown_bar_host.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "chrome/browser/ui/view_ids.h" | 9 #include "chrome/browser/ui/view_ids.h" |
10 #include "chrome/browser/ui/views/dropdown_bar_host_delegate.h" | 10 #include "chrome/browser/ui/views/dropdown_bar_host_delegate.h" |
11 #include "chrome/browser/ui/views/dropdown_bar_view.h" | 11 #include "chrome/browser/ui/views/dropdown_bar_view.h" |
12 #include "chrome/browser/ui/views/frame/browser_view.h" | 12 #include "chrome/browser/ui/views/frame/browser_view.h" |
13 #include "ui/events/keycodes/keyboard_codes.h" | 13 #include "ui/events/keycodes/keyboard_codes.h" |
14 #include "ui/gfx/animation/slide_animation.h" | 14 #include "ui/gfx/animation/slide_animation.h" |
15 #include "ui/gfx/path.h" | |
16 #include "ui/gfx/scoped_sk_region.h" | |
17 #include "ui/gfx/scrollbar_size.h" | 15 #include "ui/gfx/scrollbar_size.h" |
18 #include "ui/views/focus/external_focus_tracker.h" | 16 #include "ui/views/focus/external_focus_tracker.h" |
19 #include "ui/views/focus/view_storage.h" | 17 #include "ui/views/focus/view_storage.h" |
20 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
21 | 19 |
22 using gfx::Path; | |
23 | |
24 // static | 20 // static |
25 bool DropdownBarHost::disable_animations_during_testing_ = false; | 21 bool DropdownBarHost::disable_animations_during_testing_ = false; |
26 | 22 |
27 //////////////////////////////////////////////////////////////////////////////// | 23 //////////////////////////////////////////////////////////////////////////////// |
28 // DropdownBarHost, public: | 24 // DropdownBarHost, public: |
29 | 25 |
30 DropdownBarHost::DropdownBarHost(BrowserView* browser_view) | 26 DropdownBarHost::DropdownBarHost(BrowserView* browser_view) |
31 : browser_view_(browser_view), | 27 : browser_view_(browser_view), |
32 view_(NULL), | 28 view_(NULL), |
33 delegate_(NULL), | 29 delegate_(NULL), |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } | 203 } |
208 | 204 |
209 void DropdownBarHost::OnVisibilityChanged() { | 205 void DropdownBarHost::OnVisibilityChanged() { |
210 } | 206 } |
211 | 207 |
212 void DropdownBarHost::GetWidgetBounds(gfx::Rect* bounds) { | 208 void DropdownBarHost::GetWidgetBounds(gfx::Rect* bounds) { |
213 DCHECK(bounds); | 209 DCHECK(bounds); |
214 *bounds = browser_view_->bounds(); | 210 *bounds = browser_view_->bounds(); |
215 } | 211 } |
216 | 212 |
217 void DropdownBarHost::UpdateWindowEdges(const gfx::Rect& new_pos) { | |
218 // |w| is used to make it easier to create the part of the polygon that curves | |
219 // the right side of the Find window. It essentially keeps track of the | |
220 // x-pixel position of the right-most background image inside the view. | |
221 // TODO(finnur): Let the view tell us how to draw the curves or convert | |
222 // this to a CustomFrameWindow. | |
223 int w = new_pos.width() - 6; // -6 positions us at the left edge of the | |
224 // rightmost background image of the view. | |
225 int h = new_pos.height(); | |
226 | |
227 // This polygon array represents the outline of the background image for the | |
228 // window. Basically, it encompasses only the visible pixels of the | |
229 // concatenated find_dlg_LMR_bg images (where LMR = [left | middle | right]). | |
230 const Path::Point polygon[] = { | |
231 {2, 0}, {3, 1}, {3, h - 2}, {4, h - 1}, | |
232 {4, h}, {w+0, h}, | |
233 {w+2, h - 1}, {w+3, h - 2}, {w+3, 1}, {w+4, 0} | |
234 }; | |
235 | |
236 // Find the largest x and y value in the polygon. | |
237 int max_x = 0, max_y = 0; | |
238 for (size_t i = 0; i < arraysize(polygon); i++) { | |
239 max_x = std::max(max_x, static_cast<int>(polygon[i].x)); | |
240 max_y = std::max(max_y, static_cast<int>(polygon[i].y)); | |
241 } | |
242 | |
243 // We then create the polygon and use SetWindowRgn to force the window to draw | |
244 // only within that area. This region may get reduced in size below. | |
245 Path path(polygon, arraysize(polygon)); | |
246 gfx::ScopedSkRegion region(path.CreateNativeRegion()); | |
247 // Are we animating? | |
248 if (animation_offset() > 0) { | |
249 // The animation happens in two steps: First, we clip the window and then in | |
250 // GetWidgetPosition we offset the window position so that it still looks | |
251 // attached to the toolbar as it grows. We clip the window by creating a | |
252 // rectangle region (that gradually increases as the animation progresses) | |
253 // and find the intersection between the two regions using CombineRgn. | |
254 | |
255 // |y| shrinks as the animation progresses from the height of the view down | |
256 // to 0 (and reverses when closing). | |
257 int y = animation_offset(); | |
258 // |y| shrinking means the animation (visible) region gets larger. In other | |
259 // words: the rectangle grows upward (when the widget is opening). | |
260 Path animation_path; | |
261 SkRect animation_rect = { SkIntToScalar(0), SkIntToScalar(y), | |
262 SkIntToScalar(max_x), SkIntToScalar(max_y) }; | |
263 animation_path.addRect(animation_rect); | |
264 gfx::ScopedSkRegion animation_region( | |
265 animation_path.CreateNativeRegion()); | |
266 region.Set(Path::IntersectRegions(animation_region.Get(), region.Get())); | |
267 | |
268 // Next, we need to increase the region a little bit to account for the | |
269 // curved edges that the view will draw to make it look like grows out of | |
270 // the toolbar. | |
271 Path::Point left_curve[] = { | |
272 {2, y+0}, {3, y+1}, {3, y+0}, {2, y+0} | |
273 }; | |
274 Path::Point right_curve[] = { | |
275 {w+3, y+1}, {w+4, y+0}, {w+3, y+0}, {w+3, y+1} | |
276 }; | |
277 | |
278 // Combine the region for the curve on the left with our main region. | |
279 Path left_path(left_curve, arraysize(left_curve)); | |
280 gfx::ScopedSkRegion r(left_path.CreateNativeRegion()); | |
281 region.Set(Path::CombineRegions(r.Get(), region.Get())); | |
282 | |
283 // Combine the region for the curve on the right with our main region. | |
284 Path right_path(right_curve, arraysize(right_curve)); | |
285 region.Set(Path::CombineRegions(r.Get(), region.Get())); | |
286 } | |
287 | |
288 // Now see if we need to truncate the region because parts of it obscures | |
289 // the main window border. | |
290 gfx::Rect widget_bounds; | |
291 GetWidgetBounds(&widget_bounds); | |
292 | |
293 // Calculate how much our current position overlaps our boundaries. If we | |
294 // overlap, it means we have too little space to draw the whole widget and | |
295 // we allow overwriting the scrollbar before we start truncating our widget. | |
296 // | |
297 // TODO(brettw) this constant is evil. This is the amount of room we've added | |
298 // to the window size, when we set the region, it can change the size. | |
299 static const int kAddedWidth = 7; | |
300 int difference = new_pos.right() - kAddedWidth - widget_bounds.right() - | |
301 gfx::scrollbar_size() + 1; | |
302 if (difference > 0) { | |
303 Path::Point exclude[4]; | |
304 exclude[0].x = max_x - difference; // Top left corner. | |
305 exclude[0].y = 0; | |
306 | |
307 exclude[1].x = max_x; // Top right corner. | |
308 exclude[1].y = 0; | |
309 | |
310 exclude[2].x = max_x; // Bottom right corner. | |
311 exclude[2].y = max_y; | |
312 | |
313 exclude[3].x = max_x - difference; // Bottom left corner. | |
314 exclude[3].y = max_y; | |
315 | |
316 // Subtract this region from the original region. | |
317 gfx::Path exclude_path(exclude, arraysize(exclude)); | |
318 gfx::ScopedSkRegion exclude_region(exclude_path.CreateNativeRegion()); | |
319 region.Set(Path::SubtractRegion(region.Get(), exclude_region.Get())); | |
320 } | |
321 | |
322 // Window takes ownership of the region. | |
323 host()->SetShape(region.release()); | |
324 } | |
325 | |
326 void DropdownBarHost::RegisterAccelerators() { | 213 void DropdownBarHost::RegisterAccelerators() { |
327 DCHECK(!esc_accel_target_registered_); | 214 DCHECK(!esc_accel_target_registered_); |
328 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | 215 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); |
329 focus_manager_->RegisterAccelerator( | 216 focus_manager_->RegisterAccelerator( |
330 escape, ui::AcceleratorManager::kNormalPriority, this); | 217 escape, ui::AcceleratorManager::kNormalPriority, this); |
331 esc_accel_target_registered_ = true; | 218 esc_accel_target_registered_ = true; |
332 } | 219 } |
333 | 220 |
334 void DropdownBarHost::UnregisterAccelerators() { | 221 void DropdownBarHost::UnregisterAccelerators() { |
335 DCHECK(esc_accel_target_registered_); | 222 DCHECK(esc_accel_target_registered_); |
336 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | 223 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); |
337 focus_manager_->UnregisterAccelerator(escape, this); | 224 focus_manager_->UnregisterAccelerator(escape, this); |
338 esc_accel_target_registered_ = false; | 225 esc_accel_target_registered_ = false; |
339 } | 226 } |
OLD | NEW |