Chromium Code Reviews| 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 gfx::Rect dlg_rect = GetDialogPosition(gfx::Rect()); | |
| 89 SetDialogPosition(dlg_rect); | |
|
Peter Kasting
2015/11/24 22:39:43
Nit: Just: SetDialogPosition(GetDialogPosition(gfx
bruthig
2015/11/25 16:55:06
Done.
| |
| 90 | |
| 91 host_->Show(); | |
| 92 | |
| 81 bool was_visible = is_visible_; | 93 bool was_visible = is_visible_; |
| 82 is_visible_ = true; | 94 is_visible_ = true; |
| 83 if (!animate || disable_animations_during_testing_) { | 95 if (!animate || disable_animations_during_testing_) { |
| 84 animation_->Reset(1); | 96 animation_->Reset(1); |
| 85 AnimationProgressed(animation_.get()); | 97 AnimationProgressed(animation_.get()); |
| 86 } else if (!was_visible) { | 98 } else if (!was_visible) { |
| 87 // Don't re-start the animation. | 99 // Don't re-start the animation. |
| 88 animation_->Reset(); | 100 animation_->Reset(); |
| 89 animation_->Show(); | 101 animation_->Show(); |
| 90 } | 102 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 } | 137 } |
| 126 | 138 |
| 127 void DropdownBarHost::StopAnimation() { | 139 void DropdownBarHost::StopAnimation() { |
| 128 animation_->End(); | 140 animation_->End(); |
| 129 } | 141 } |
| 130 | 142 |
| 131 bool DropdownBarHost::IsVisible() const { | 143 bool DropdownBarHost::IsVisible() const { |
| 132 return is_visible_; | 144 return is_visible_; |
| 133 } | 145 } |
| 134 | 146 |
| 147 void DropdownBarHost::SetDialogPosition(const gfx::Rect& new_pos) { | |
| 148 view_->SetSize(new_pos.size()); | |
| 149 | |
| 150 if (new_pos.IsEmpty()) | |
| 151 return; | |
|
Peter Kasting
2015/11/24 22:39:43
Should this hide the host if it's currently visibl
bruthig
2015/11/25 16:55:06
No, not from what I can tell anyway. From what I
Peter Kasting
2015/11/25 23:16:54
I don't really know what this code does so I can't
| |
| 152 | |
| 153 if (!host()->IsVisible()) | |
| 154 host()->Show(); | |
| 155 host()->SetBounds(new_pos); | |
| 156 } | |
| 157 | |
| 135 //////////////////////////////////////////////////////////////////////////////// | 158 //////////////////////////////////////////////////////////////////////////////// |
| 136 // DropdownBarHost, views::FocusChangeListener implementation: | 159 // DropdownBarHost, views::FocusChangeListener implementation: |
| 137 void DropdownBarHost::OnWillChangeFocus(views::View* focused_before, | 160 void DropdownBarHost::OnWillChangeFocus(views::View* focused_before, |
| 138 views::View* focused_now) { | 161 views::View* focused_now) { |
| 139 // First we need to determine if one or both of the views passed in are child | 162 // First we need to determine if one or both of the views passed in are child |
| 140 // views of our view. | 163 // views of our view. |
| 141 bool our_view_before = focused_before && view_->Contains(focused_before); | 164 bool our_view_before = focused_before && view_->Contains(focused_before); |
| 142 bool our_view_now = focused_now && view_->Contains(focused_now); | 165 bool our_view_now = focused_now && view_->Contains(focused_now); |
| 143 | 166 |
| 144 // When both our_view_before and our_view_now are false, it means focus is | 167 // 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, | 183 void DropdownBarHost::OnDidChangeFocus(views::View* focused_before, |
| 161 views::View* focused_now) { | 184 views::View* focused_now) { |
| 162 } | 185 } |
| 163 | 186 |
| 164 //////////////////////////////////////////////////////////////////////////////// | 187 //////////////////////////////////////////////////////////////////////////////// |
| 165 // DropdownBarHost, gfx::AnimationDelegate implementation: | 188 // DropdownBarHost, gfx::AnimationDelegate implementation: |
| 166 | 189 |
| 167 void DropdownBarHost::AnimationProgressed(const gfx::Animation* animation) { | 190 void DropdownBarHost::AnimationProgressed(const gfx::Animation* animation) { |
| 168 // First, we calculate how many pixels to slide the widget. | 191 // First, we calculate how many pixels to slide the widget. |
| 169 gfx::Size pref_size = view_->GetPreferredSize(); | 192 gfx::Size pref_size = view_->GetPreferredSize(); |
| 170 animation_offset_ = static_cast<int>((1.0 - animation_->GetCurrentValue()) * | 193 int view_offset = static_cast<int>((animation_->GetCurrentValue() - 1.0) * |
| 171 pref_size.height()); | 194 pref_size.height()); |
| 172 | 195 |
| 173 // This call makes sure it appears in the right location, the size and shape | 196 // This call makes sure |view_| appears in the right location, the size and |
| 174 // is correct and that it slides in the right direction. | 197 // shape is correct and that it slides in the right direction. |
| 175 gfx::Rect dlg_rect = GetDialogPosition(gfx::Rect()); | 198 gfx::Rect dlg_rect = GetDialogPosition(gfx::Rect()); |
|
Peter Kasting
2015/11/24 22:39:43
This temp is now unused.
bruthig
2015/11/25 16:55:06
Done.
| |
| 176 SetDialogPosition(dlg_rect); | 199 view_->SetPosition(gfx::Point(0, view_offset)); |
| 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 } | 200 } |
| 183 | 201 |
| 184 void DropdownBarHost::AnimationEnded(const gfx::Animation* animation) { | 202 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()) { | 203 if (!animation_->IsShowing()) { |
| 189 // Animation has finished closing. | 204 // Animation has finished closing. |
| 190 host_->Hide(); | 205 host_->Hide(); |
| 191 is_visible_ = false; | 206 is_visible_ = false; |
| 192 OnVisibilityChanged(); | 207 OnVisibilityChanged(); |
| 193 } else { | 208 } else { |
| 194 // Animation has finished opening. | 209 // Animation has finished opening. |
| 195 } | 210 } |
| 196 } | 211 } |
| 197 | 212 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 217 escape, ui::AcceleratorManager::kNormalPriority, this); | 232 escape, ui::AcceleratorManager::kNormalPriority, this); |
| 218 esc_accel_target_registered_ = true; | 233 esc_accel_target_registered_ = true; |
| 219 } | 234 } |
| 220 | 235 |
| 221 void DropdownBarHost::UnregisterAccelerators() { | 236 void DropdownBarHost::UnregisterAccelerators() { |
| 222 DCHECK(esc_accel_target_registered_); | 237 DCHECK(esc_accel_target_registered_); |
| 223 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | 238 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); |
| 224 focus_manager_->UnregisterAccelerator(escape, this); | 239 focus_manager_->UnregisterAccelerator(escape, this); |
| 225 esc_accel_target_registered_ = false; | 240 esc_accel_target_registered_ = false; |
| 226 } | 241 } |
| OLD | NEW |