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