OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "views/bubble/bubble_view.h" | 5 #include "views/bubble/bubble_view.h" |
6 | 6 |
7 #include "ui/base/animation/slide_animation.h" | 7 #include "ui/base/animation/slide_animation.h" |
8 #include "views/bubble/bubble_border.h" | 8 #include "views/bubble/bubble_border.h" |
9 #include "views/controls/label.h" | |
10 #include "views/layout/box_layout.h" | |
11 #include "views/layout/layout_constants.h" | |
12 #include "views/views_delegate.h" | |
13 #include "views/window/client_view.h" | |
14 #include "views/widget/widget.h" | 9 #include "views/widget/widget.h" |
15 | 10 |
16 // How long the fade should last for. | 11 // The duration of the fade animation in milliseconds. |
17 static const int kHideFadeDurationMS = 1000; | 12 static const int kHideFadeDurationMS = 1000; |
18 | 13 |
19 namespace views { | 14 namespace views { |
20 | 15 |
21 BubbleView::BubbleView(Widget* owner, View* contents_view) | 16 BubbleView::BubbleView(Widget* owner, View* contents_view) |
22 : ClientView(owner, contents_view), | 17 : ClientView(owner, contents_view), |
23 animation_delegate_(NULL), | 18 close_on_esc_(true) { |
24 registered_accelerator_(false), | 19 AddAccelerator(Accelerator(ui::VKEY_ESCAPE, 0)); |
25 should_fade_(false) { | |
26 ResetLayoutManager(); | |
27 InitAnimation(); | |
28 } | 20 } |
29 | 21 |
30 void BubbleView::InitAnimation() { | |
31 fade_animation_.reset(new ui::SlideAnimation(this)); | |
32 fade_animation_->SetSlideDuration(kHideFadeDurationMS); | |
33 fade_animation_->Reset(1.0); | |
34 } | |
35 | |
36 BubbleView* BubbleView::AsBubbleView() { return this; } | |
37 const BubbleView* BubbleView::AsBubbleView() const { return this; } | |
38 | |
39 BubbleView::~BubbleView() {} | 22 BubbleView::~BubbleView() {} |
40 | 23 |
41 void BubbleView::Show() { | 24 void BubbleView::StartFade(bool fade_in) { |
42 if (!registered_accelerator_) | 25 fade_animation_.reset(new ui::SlideAnimation(this)); |
43 registered_accelerator_ = true; | 26 fade_animation_->SetSlideDuration(kHideFadeDurationMS); |
44 GetWidget()->Show(); | 27 fade_animation_->Reset(fade_in ? 0.0 : 1.0); |
45 GetWidget()->Activate(); | 28 if (fade_in) { |
46 SchedulePaint(); | 29 GetWidget()->SetOpacity(0); |
47 } | 30 GetWidget()->Show(); |
48 | 31 fade_animation_->Show(); |
49 void BubbleView::StartFade() { | 32 } else { |
50 should_fade_ = true; | 33 fade_animation_->Hide(); |
51 fade_animation_->Hide(); | 34 } |
52 } | |
53 | |
54 void BubbleView::ResetLayoutManager() { | |
55 SetLayoutManager(new views::BoxLayout( | |
56 views::BoxLayout::kHorizontal, 0, 0, 1)); | |
57 } | |
58 | |
59 void BubbleView::set_animation_delegate(ui::AnimationDelegate* delegate) { | |
60 animation_delegate_ = delegate; | |
61 } | |
62 | |
63 void BubbleView::ViewHierarchyChanged(bool is_add, | |
64 views::View* parent, | |
65 views::View* child) { | |
66 if (is_add && parent == this) | |
67 child->SetBoundsRect(bounds()); | |
68 } | |
69 | |
70 void BubbleView::Layout() { | |
71 gfx::Rect lb = GetContentsBounds(); | |
72 contents_view()->SetBoundsRect(lb); | |
73 contents_view()->Layout(); | |
74 } | |
75 | |
76 gfx::Size BubbleView::GetPreferredSize() { | |
77 return bounds().size(); | |
78 } | 35 } |
79 | 36 |
80 bool BubbleView::AcceleratorPressed(const Accelerator& accelerator) { | 37 bool BubbleView::AcceleratorPressed(const Accelerator& accelerator) { |
81 if (registered_accelerator_) { | 38 if (!close_on_esc_ || accelerator.key_code() != ui::VKEY_ESCAPE) |
82 GetWidget()->GetFocusManager()->UnregisterAccelerator( | 39 return false; |
83 views::Accelerator(ui::VKEY_ESCAPE, false, false, false), this); | 40 if (fade_animation_.get()) |
84 registered_accelerator_ = false; | 41 fade_animation_->Reset(); |
85 } | |
86 // Turn off animation, if any. | |
87 if (should_fade_ && fade_animation_->is_animating()) { | |
88 fade_animation_->Reset(1.0); | |
89 fade_animation_->Show(); | |
90 } | |
91 GetWidget()->Close(); | 42 GetWidget()->Close(); |
92 return true; | 43 return true; |
93 } | 44 } |
94 | 45 |
95 void BubbleView::AnimationEnded(const ui::Animation* animation) { | 46 void BubbleView::AnimationEnded(const ui::Animation* animation) { |
96 if (animation_delegate_) | 47 DCHECK_EQ(animation, fade_animation_.get()); |
97 animation_delegate_->AnimationEnded(animation); | 48 bool closed = fade_animation_->GetCurrentValue() == 0; |
98 | 49 fade_animation_->Reset(); |
99 fade_animation_->Reset(0.0); | 50 if (closed) |
100 // Close the widget. | 51 GetWidget()->Close(); |
101 registered_accelerator_ = false; | |
102 GetWidget()->Close(); | |
103 } | 52 } |
104 | 53 |
105 void BubbleView::AnimationProgressed(const ui::Animation* animation) { | 54 void BubbleView::AnimationProgressed(const ui::Animation* animation) { |
106 if (fade_animation_->is_animating()) { | 55 DCHECK_EQ(animation, fade_animation_.get()); |
107 if (animation_delegate_) | 56 DCHECK(fade_animation_->is_animating()); |
108 animation_delegate_->AnimationProgressed(animation); | 57 GetWidget()->SetOpacity(fade_animation_->GetCurrentValue() * 255); |
109 | 58 SchedulePaint(); |
110 GetWidget()->SetOpacity(animation->GetCurrentValue() * 255); | |
111 SchedulePaint(); | |
112 } | |
113 } | 59 } |
114 | 60 |
115 } // namespace views | 61 } // namespace views |
OLD | NEW |