| 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/scrollbar_size.h" | 15 #include "ui/gfx/scrollbar_size.h" |
| 16 #include "ui/views/focus/external_focus_tracker.h" | 16 #include "ui/views/focus/external_focus_tracker.h" |
| 17 #include "ui/views/focus/view_storage.h" | 17 #include "ui/views/focus/view_storage.h" |
| 18 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
| 19 | 19 |
| 20 // static | 20 // static |
| 21 bool DropdownBarHost::disable_animations_during_testing_ = false; | 21 bool DropdownBarHost::disable_animations_during_testing_ = false; |
| 22 | 22 |
| 23 //////////////////////////////////////////////////////////////////////////////// | 23 //////////////////////////////////////////////////////////////////////////////// |
| 24 // DropdownBarHost, public: | 24 // DropdownBarHost, public: |
| 25 | 25 |
| 26 DropdownBarHost::DropdownBarHost(BrowserView* browser_view) | 26 DropdownBarHost::DropdownBarHost(BrowserView* browser_view) |
| 27 : browser_view_(browser_view), | 27 : browser_view_(browser_view), |
| 28 clip_view_(new views::View()), |
| 28 view_(NULL), | 29 view_(NULL), |
| 29 delegate_(NULL), | 30 delegate_(NULL), |
| 30 animation_offset_(0), | |
| 31 focus_manager_(NULL), | 31 focus_manager_(NULL), |
| 32 esc_accel_target_registered_(false), | 32 esc_accel_target_registered_(false), |
| 33 is_visible_(false) { | 33 is_visible_(false) { |
| 34 // The |clip_view_| must paint to a layer so that it can clip descendent Views |
| 35 // which also paint to a Layer. |
| 36 clip_view_->SetPaintToLayer(true); |
| 37 clip_view_->SetFillsBoundsOpaquely(false); |
| 38 clip_view_->layer()->SetMasksToBounds(true); |
| 34 } | 39 } |
| 35 | 40 |
| 36 void DropdownBarHost::Init(views::View* host_view, | 41 void DropdownBarHost::Init(views::View* host_view, |
| 37 views::View* view, | 42 views::View* view, |
| 38 DropdownBarHostDelegate* delegate) { | 43 DropdownBarHostDelegate* delegate) { |
| 39 DCHECK(view); | 44 DCHECK(view); |
| 40 DCHECK(delegate); | 45 DCHECK(delegate); |
| 41 | 46 |
| 42 view_ = view; | 47 view_ = view; |
| 43 delegate_ = delegate; | 48 delegate_ = delegate; |
| 44 | 49 |
| 45 // Initialize the host. | 50 // Initialize the host. |
| 46 host_.reset(new views::Widget); | 51 host_.reset(new views::Widget); |
| 47 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | 52 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
| 48 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 53 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 49 params.parent = browser_view_->GetWidget()->GetNativeView(); | 54 params.parent = browser_view_->GetWidget()->GetNativeView(); |
| 50 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 55 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 51 host_->Init(params); | 56 host_->Init(params); |
| 52 host_->SetContentsView(view_); | 57 host_->SetContentsView(clip_view_); |
| 58 clip_view_->AddChildView(view_); |
| 53 | 59 |
| 54 SetHostViewNative(host_view); | 60 SetHostViewNative(host_view); |
| 55 | 61 |
| 56 // Start listening to focus changes, so we can register and unregister our | 62 // Start listening to focus changes, so we can register and unregister our |
| 57 // own handler for Escape. | 63 // own handler for Escape. |
| 58 focus_manager_ = host_->GetFocusManager(); | 64 focus_manager_ = host_->GetFocusManager(); |
| 59 if (focus_manager_) { | 65 if (focus_manager_) { |
| 60 focus_manager_->AddFocusChangeListener(this); | 66 focus_manager_->AddFocusChangeListener(this); |
| 61 } else { | 67 } else { |
| 62 // In some cases (see bug http://crbug.com/17056) it seems we may not have | 68 // In some cases (see bug http://crbug.com/17056) it seems we may not have |
| 63 // a focus manager. Please reopen the bug if you hit this. | 69 // a focus manager. Please reopen the bug if you hit this. |
| 64 NOTREACHED(); | 70 NOTREACHED(); |
| 65 } | 71 } |
| 66 | 72 |
| 67 // Start the process of animating the opening of the widget. | |
| 68 animation_.reset(new gfx::SlideAnimation(this)); | 73 animation_.reset(new gfx::SlideAnimation(this)); |
| 74 // Update the widget and |view_| bounds to the hidden state. |
| 75 AnimationProgressed(animation_.get()); |
| 69 } | 76 } |
| 70 | 77 |
| 71 DropdownBarHost::~DropdownBarHost() { | 78 DropdownBarHost::~DropdownBarHost() { |
| 72 focus_manager_->RemoveFocusChangeListener(this); | 79 focus_manager_->RemoveFocusChangeListener(this); |
| 73 focus_tracker_.reset(NULL); | 80 focus_tracker_.reset(NULL); |
| 74 } | 81 } |
| 75 | 82 |
| 76 void DropdownBarHost::Show(bool animate) { | 83 void DropdownBarHost::Show(bool animate) { |
| 77 // Stores the currently focused view, and tracks focus changes so that we can | 84 // Stores the currently focused view, and tracks focus changes so that we can |
| 78 // restore focus when the dropdown widget is closed. | 85 // restore focus when the dropdown widget is closed. |
| 79 focus_tracker_.reset(new views::ExternalFocusTracker(view_, focus_manager_)); | 86 focus_tracker_.reset(new views::ExternalFocusTracker(view_, focus_manager_)); |
| 80 | 87 |
| 88 SetDialogPosition(GetDialogPosition(gfx::Rect())); |
| 89 |
| 90 host_->Show(); |
| 91 |
| 81 bool was_visible = is_visible_; | 92 bool was_visible = is_visible_; |
| 82 is_visible_ = true; | 93 is_visible_ = true; |
| 83 if (!animate || disable_animations_during_testing_) { | 94 if (!animate || disable_animations_during_testing_) { |
| 84 animation_->Reset(1); | 95 animation_->Reset(1); |
| 85 AnimationProgressed(animation_.get()); | 96 AnimationProgressed(animation_.get()); |
| 86 } else if (!was_visible) { | 97 } else if (!was_visible) { |
| 87 // Don't re-start the animation. | 98 // Don't re-start the animation. |
| 88 animation_->Reset(); | 99 animation_->Reset(); |
| 89 animation_->Show(); | 100 animation_->Show(); |
| 90 } | 101 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } | 136 } |
| 126 | 137 |
| 127 void DropdownBarHost::StopAnimation() { | 138 void DropdownBarHost::StopAnimation() { |
| 128 animation_->End(); | 139 animation_->End(); |
| 129 } | 140 } |
| 130 | 141 |
| 131 bool DropdownBarHost::IsVisible() const { | 142 bool DropdownBarHost::IsVisible() const { |
| 132 return is_visible_; | 143 return is_visible_; |
| 133 } | 144 } |
| 134 | 145 |
| 146 void DropdownBarHost::SetDialogPosition(const gfx::Rect& new_pos) { |
| 147 view_->SetSize(new_pos.size()); |
| 148 |
| 149 if (new_pos.IsEmpty()) |
| 150 return; |
| 151 |
| 152 host()->SetBounds(new_pos); |
| 153 } |
| 154 |
| 135 //////////////////////////////////////////////////////////////////////////////// | 155 //////////////////////////////////////////////////////////////////////////////// |
| 136 // DropdownBarHost, views::FocusChangeListener implementation: | 156 // DropdownBarHost, views::FocusChangeListener implementation: |
| 137 void DropdownBarHost::OnWillChangeFocus(views::View* focused_before, | 157 void DropdownBarHost::OnWillChangeFocus(views::View* focused_before, |
| 138 views::View* focused_now) { | 158 views::View* focused_now) { |
| 139 // First we need to determine if one or both of the views passed in are child | 159 // First we need to determine if one or both of the views passed in are child |
| 140 // views of our view. | 160 // views of our view. |
| 141 bool our_view_before = focused_before && view_->Contains(focused_before); | 161 bool our_view_before = focused_before && view_->Contains(focused_before); |
| 142 bool our_view_now = focused_now && view_->Contains(focused_now); | 162 bool our_view_now = focused_now && view_->Contains(focused_now); |
| 143 | 163 |
| 144 // When both our_view_before and our_view_now are false, it means focus is | 164 // When both our_view_before and our_view_now are false, it means focus is |
| (...skipping 15 matching lines...) Expand all Loading... |
| 160 void DropdownBarHost::OnDidChangeFocus(views::View* focused_before, | 180 void DropdownBarHost::OnDidChangeFocus(views::View* focused_before, |
| 161 views::View* focused_now) { | 181 views::View* focused_now) { |
| 162 } | 182 } |
| 163 | 183 |
| 164 //////////////////////////////////////////////////////////////////////////////// | 184 //////////////////////////////////////////////////////////////////////////////// |
| 165 // DropdownBarHost, gfx::AnimationDelegate implementation: | 185 // DropdownBarHost, gfx::AnimationDelegate implementation: |
| 166 | 186 |
| 167 void DropdownBarHost::AnimationProgressed(const gfx::Animation* animation) { | 187 void DropdownBarHost::AnimationProgressed(const gfx::Animation* animation) { |
| 168 // First, we calculate how many pixels to slide the widget. | 188 // First, we calculate how many pixels to slide the widget. |
| 169 gfx::Size pref_size = view_->GetPreferredSize(); | 189 gfx::Size pref_size = view_->GetPreferredSize(); |
| 170 animation_offset_ = static_cast<int>((1.0 - animation_->GetCurrentValue()) * | 190 int view_offset = static_cast<int>((animation_->GetCurrentValue() - 1.0) * |
| 171 pref_size.height()); | 191 pref_size.height()); |
| 172 | 192 |
| 173 // This call makes sure it appears in the right location, the size and shape | 193 // This call makes sure |view_| appears in the right location, the size and |
| 174 // is correct and that it slides in the right direction. | 194 // shape is correct and that it slides in the right direction. |
| 175 gfx::Rect dlg_rect = GetDialogPosition(gfx::Rect()); | 195 view_->SetPosition(gfx::Point(0, view_offset)); |
| 176 SetDialogPosition(dlg_rect); | |
| 177 | |
| 178 // Let the view know if we are animating, and at which offset to draw the | |
| 179 // edges. | |
| 180 delegate_->SetAnimationOffset(animation_offset_); | |
| 181 view_->SchedulePaint(); | |
| 182 } | 196 } |
| 183 | 197 |
| 184 void DropdownBarHost::AnimationEnded(const gfx::Animation* animation) { | 198 void DropdownBarHost::AnimationEnded(const gfx::Animation* animation) { |
| 185 // Place the dropdown widget in its fully opened state. | |
| 186 animation_offset_ = 0; | |
| 187 | |
| 188 if (!animation_->IsShowing()) { | 199 if (!animation_->IsShowing()) { |
| 189 // Animation has finished closing. | 200 // Animation has finished closing. |
| 190 host_->Hide(); | 201 host_->Hide(); |
| 191 is_visible_ = false; | 202 is_visible_ = false; |
| 192 OnVisibilityChanged(); | 203 OnVisibilityChanged(); |
| 193 } else { | 204 } else { |
| 194 // Animation has finished opening. | 205 // Animation has finished opening. |
| 195 } | 206 } |
| 196 } | 207 } |
| 197 | 208 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 217 escape, ui::AcceleratorManager::kNormalPriority, this); | 228 escape, ui::AcceleratorManager::kNormalPriority, this); |
| 218 esc_accel_target_registered_ = true; | 229 esc_accel_target_registered_ = true; |
| 219 } | 230 } |
| 220 | 231 |
| 221 void DropdownBarHost::UnregisterAccelerators() { | 232 void DropdownBarHost::UnregisterAccelerators() { |
| 222 DCHECK(esc_accel_target_registered_); | 233 DCHECK(esc_accel_target_registered_); |
| 223 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | 234 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); |
| 224 focus_manager_->UnregisterAccelerator(escape, this); | 235 focus_manager_->UnregisterAccelerator(escape, this); |
| 225 esc_accel_target_registered_ = false; | 236 esc_accel_target_registered_ = false; |
| 226 } | 237 } |
| OLD | NEW |