| 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" | 15 #include "ui/gfx/path.h" |
| 16 #include "ui/gfx/scoped_sk_region.h" |
| 16 #include "ui/gfx/scrollbar_size.h" | 17 #include "ui/gfx/scrollbar_size.h" |
| 17 #include "ui/views/focus/external_focus_tracker.h" | 18 #include "ui/views/focus/external_focus_tracker.h" |
| 18 #include "ui/views/focus/view_storage.h" | 19 #include "ui/views/focus/view_storage.h" |
| 19 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 20 | 21 |
| 21 #if defined(USE_AURA) | |
| 22 #include "ui/gfx/scoped_sk_region.h" | |
| 23 #elif defined(OS_WIN) | |
| 24 #include "base/win/scoped_gdi_object.h" | |
| 25 #endif | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 #if defined(USE_AURA) | |
| 30 typedef gfx::ScopedSkRegion ScopedPlatformRegion; | |
| 31 #elif defined(OS_WIN) | |
| 32 typedef base::win::ScopedRegion ScopedPlatformRegion; | |
| 33 #endif | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 using gfx::Path; | 22 using gfx::Path; |
| 38 | 23 |
| 39 // static | 24 // static |
| 40 bool DropdownBarHost::disable_animations_during_testing_ = false; | 25 bool DropdownBarHost::disable_animations_during_testing_ = false; |
| 41 | 26 |
| 42 //////////////////////////////////////////////////////////////////////////////// | 27 //////////////////////////////////////////////////////////////////////////////// |
| 43 // DropdownBarHost, public: | 28 // DropdownBarHost, public: |
| 44 | 29 |
| 45 DropdownBarHost::DropdownBarHost(BrowserView* browser_view) | 30 DropdownBarHost::DropdownBarHost(BrowserView* browser_view) |
| 46 : browser_view_(browser_view), | 31 : browser_view_(browser_view), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 59 DCHECK(delegate); | 44 DCHECK(delegate); |
| 60 | 45 |
| 61 view_ = view; | 46 view_ = view; |
| 62 delegate_ = delegate; | 47 delegate_ = delegate; |
| 63 | 48 |
| 64 // Initialize the host. | 49 // Initialize the host. |
| 65 host_.reset(new views::Widget); | 50 host_.reset(new views::Widget); |
| 66 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | 51 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
| 67 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 52 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 68 params.parent = browser_view_->GetWidget()->GetNativeView(); | 53 params.parent = browser_view_->GetWidget()->GetNativeView(); |
| 69 #if defined(USE_AURA) | |
| 70 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 54 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 71 #endif | |
| 72 host_->Init(params); | 55 host_->Init(params); |
| 73 host_->SetContentsView(view_); | 56 host_->SetContentsView(view_); |
| 74 | 57 |
| 75 SetHostViewNative(host_view); | 58 SetHostViewNative(host_view); |
| 76 | 59 |
| 77 // Start listening to focus changes, so we can register and unregister our | 60 // Start listening to focus changes, so we can register and unregister our |
| 78 // own handler for Escape. | 61 // own handler for Escape. |
| 79 focus_manager_ = host_->GetFocusManager(); | 62 focus_manager_ = host_->GetFocusManager(); |
| 80 if (focus_manager_) { | 63 if (focus_manager_) { |
| 81 focus_manager_->AddFocusChangeListener(this); | 64 focus_manager_->AddFocusChangeListener(this); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 // Find the largest x and y value in the polygon. | 236 // Find the largest x and y value in the polygon. |
| 254 int max_x = 0, max_y = 0; | 237 int max_x = 0, max_y = 0; |
| 255 for (size_t i = 0; i < arraysize(polygon); i++) { | 238 for (size_t i = 0; i < arraysize(polygon); i++) { |
| 256 max_x = std::max(max_x, static_cast<int>(polygon[i].x)); | 239 max_x = std::max(max_x, static_cast<int>(polygon[i].x)); |
| 257 max_y = std::max(max_y, static_cast<int>(polygon[i].y)); | 240 max_y = std::max(max_y, static_cast<int>(polygon[i].y)); |
| 258 } | 241 } |
| 259 | 242 |
| 260 // We then create the polygon and use SetWindowRgn to force the window to draw | 243 // We then create the polygon and use SetWindowRgn to force the window to draw |
| 261 // only within that area. This region may get reduced in size below. | 244 // only within that area. This region may get reduced in size below. |
| 262 Path path(polygon, arraysize(polygon)); | 245 Path path(polygon, arraysize(polygon)); |
| 263 ScopedPlatformRegion region(path.CreateNativeRegion()); | 246 gfx::ScopedSkRegion region(path.CreateNativeRegion()); |
| 264 // Are we animating? | 247 // Are we animating? |
| 265 if (animation_offset() > 0) { | 248 if (animation_offset() > 0) { |
| 266 // The animation happens in two steps: First, we clip the window and then in | 249 // The animation happens in two steps: First, we clip the window and then in |
| 267 // GetWidgetPosition we offset the window position so that it still looks | 250 // GetWidgetPosition we offset the window position so that it still looks |
| 268 // attached to the toolbar as it grows. We clip the window by creating a | 251 // attached to the toolbar as it grows. We clip the window by creating a |
| 269 // rectangle region (that gradually increases as the animation progresses) | 252 // rectangle region (that gradually increases as the animation progresses) |
| 270 // and find the intersection between the two regions using CombineRgn. | 253 // and find the intersection between the two regions using CombineRgn. |
| 271 | 254 |
| 272 // |y| shrinks as the animation progresses from the height of the view down | 255 // |y| shrinks as the animation progresses from the height of the view down |
| 273 // to 0 (and reverses when closing). | 256 // to 0 (and reverses when closing). |
| 274 int y = animation_offset(); | 257 int y = animation_offset(); |
| 275 // |y| shrinking means the animation (visible) region gets larger. In other | 258 // |y| shrinking means the animation (visible) region gets larger. In other |
| 276 // words: the rectangle grows upward (when the widget is opening). | 259 // words: the rectangle grows upward (when the widget is opening). |
| 277 Path animation_path; | 260 Path animation_path; |
| 278 SkRect animation_rect = { SkIntToScalar(0), SkIntToScalar(y), | 261 SkRect animation_rect = { SkIntToScalar(0), SkIntToScalar(y), |
| 279 SkIntToScalar(max_x), SkIntToScalar(max_y) }; | 262 SkIntToScalar(max_x), SkIntToScalar(max_y) }; |
| 280 animation_path.addRect(animation_rect); | 263 animation_path.addRect(animation_rect); |
| 281 ScopedPlatformRegion animation_region( | 264 gfx::ScopedSkRegion animation_region( |
| 282 animation_path.CreateNativeRegion()); | 265 animation_path.CreateNativeRegion()); |
| 283 region.Set(Path::IntersectRegions(animation_region.Get(), region.Get())); | 266 region.Set(Path::IntersectRegions(animation_region.Get(), region.Get())); |
| 284 | 267 |
| 285 // Next, we need to increase the region a little bit to account for the | 268 // Next, we need to increase the region a little bit to account for the |
| 286 // curved edges that the view will draw to make it look like grows out of | 269 // curved edges that the view will draw to make it look like grows out of |
| 287 // the toolbar. | 270 // the toolbar. |
| 288 Path::Point left_curve[] = { | 271 Path::Point left_curve[] = { |
| 289 {2, y+0}, {3, y+1}, {3, y+0}, {2, y+0} | 272 {2, y+0}, {3, y+1}, {3, y+0}, {2, y+0} |
| 290 }; | 273 }; |
| 291 Path::Point right_curve[] = { | 274 Path::Point right_curve[] = { |
| 292 {w+3, y+1}, {w+4, y+0}, {w+3, y+0}, {w+3, y+1} | 275 {w+3, y+1}, {w+4, y+0}, {w+3, y+0}, {w+3, y+1} |
| 293 }; | 276 }; |
| 294 | 277 |
| 295 // Combine the region for the curve on the left with our main region. | 278 // Combine the region for the curve on the left with our main region. |
| 296 Path left_path(left_curve, arraysize(left_curve)); | 279 Path left_path(left_curve, arraysize(left_curve)); |
| 297 ScopedPlatformRegion r(left_path.CreateNativeRegion()); | 280 gfx::ScopedSkRegion r(left_path.CreateNativeRegion()); |
| 298 region.Set(Path::CombineRegions(r.Get(), region.Get())); | 281 region.Set(Path::CombineRegions(r.Get(), region.Get())); |
| 299 | 282 |
| 300 // Combine the region for the curve on the right with our main region. | 283 // Combine the region for the curve on the right with our main region. |
| 301 Path right_path(right_curve, arraysize(right_curve)); | 284 Path right_path(right_curve, arraysize(right_curve)); |
| 302 region.Set(Path::CombineRegions(r.Get(), region.Get())); | 285 region.Set(Path::CombineRegions(r.Get(), region.Get())); |
| 303 } | 286 } |
| 304 | 287 |
| 305 // Now see if we need to truncate the region because parts of it obscures | 288 // Now see if we need to truncate the region because parts of it obscures |
| 306 // the main window border. | 289 // the main window border. |
| 307 gfx::Rect widget_bounds; | 290 gfx::Rect widget_bounds; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 325 exclude[1].y = 0; | 308 exclude[1].y = 0; |
| 326 | 309 |
| 327 exclude[2].x = max_x; // Bottom right corner. | 310 exclude[2].x = max_x; // Bottom right corner. |
| 328 exclude[2].y = max_y; | 311 exclude[2].y = max_y; |
| 329 | 312 |
| 330 exclude[3].x = max_x - difference; // Bottom left corner. | 313 exclude[3].x = max_x - difference; // Bottom left corner. |
| 331 exclude[3].y = max_y; | 314 exclude[3].y = max_y; |
| 332 | 315 |
| 333 // Subtract this region from the original region. | 316 // Subtract this region from the original region. |
| 334 gfx::Path exclude_path(exclude, arraysize(exclude)); | 317 gfx::Path exclude_path(exclude, arraysize(exclude)); |
| 335 ScopedPlatformRegion exclude_region(exclude_path.CreateNativeRegion()); | 318 gfx::ScopedSkRegion exclude_region(exclude_path.CreateNativeRegion()); |
| 336 region.Set(Path::SubtractRegion(region.Get(), exclude_region.Get())); | 319 region.Set(Path::SubtractRegion(region.Get(), exclude_region.Get())); |
| 337 } | 320 } |
| 338 | 321 |
| 339 // Window takes ownership of the region. | 322 // Window takes ownership of the region. |
| 340 host()->SetShape(region.release()); | 323 host()->SetShape(region.release()); |
| 341 } | 324 } |
| 342 | 325 |
| 343 void DropdownBarHost::RegisterAccelerators() { | 326 void DropdownBarHost::RegisterAccelerators() { |
| 344 DCHECK(!esc_accel_target_registered_); | 327 DCHECK(!esc_accel_target_registered_); |
| 345 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | 328 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); |
| 346 focus_manager_->RegisterAccelerator( | 329 focus_manager_->RegisterAccelerator( |
| 347 escape, ui::AcceleratorManager::kNormalPriority, this); | 330 escape, ui::AcceleratorManager::kNormalPriority, this); |
| 348 esc_accel_target_registered_ = true; | 331 esc_accel_target_registered_ = true; |
| 349 } | 332 } |
| 350 | 333 |
| 351 void DropdownBarHost::UnregisterAccelerators() { | 334 void DropdownBarHost::UnregisterAccelerators() { |
| 352 DCHECK(esc_accel_target_registered_); | 335 DCHECK(esc_accel_target_registered_); |
| 353 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | 336 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); |
| 354 focus_manager_->UnregisterAccelerator(escape, this); | 337 focus_manager_->UnregisterAccelerator(escape, this); |
| 355 esc_accel_target_registered_ = false; | 338 esc_accel_target_registered_ = false; |
| 356 } | 339 } |
| OLD | NEW |