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 "ui/views/bubble/bubble_delegate.h" | 5 #include "ui/views/bubble/bubble_delegate.h" |
6 | 6 |
7 #include "ui/base/animation/slide_animation.h" | 7 #include "ui/base/animation/slide_animation.h" |
8 #include "ui/gfx/color_utils.h" | 8 #include "ui/gfx/color_utils.h" |
9 #include "ui/views/bubble/bubble_frame_view.h" | 9 #include "ui/views/bubble/bubble_frame_view.h" |
10 #include "ui/views/widget/widget.h" | 10 #include "ui/views/widget/widget.h" |
11 | 11 |
12 // The duration of the fade animation in milliseconds. | 12 // The duration of the fade animation in milliseconds. |
13 static const int kHideFadeDurationMS = 200; | 13 static const int kHideFadeDurationMS = 200; |
14 | 14 |
15 // The defaut margin between the content and the inside border, in pixels. | 15 // The defaut margin between the content and the inside border, in pixels. |
16 static const int kDefaultMargin = 6; | 16 static const int kDefaultMargin = 6; |
17 | 17 |
18 namespace views { | 18 namespace views { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Create a widget to host the bubble. | 22 // Create a widget to host the bubble. |
23 Widget* CreateBubbleWidget(BubbleDelegateView* bubble, Widget* parent) { | 23 Widget* CreateBubbleWidget(BubbleDelegateView* bubble) { |
24 Widget* bubble_widget = new Widget(); | 24 Widget* bubble_widget = new Widget(); |
25 Widget::InitParams bubble_params(Widget::InitParams::TYPE_BUBBLE); | 25 Widget::InitParams bubble_params(Widget::InitParams::TYPE_BUBBLE); |
26 bubble_params.delegate = bubble; | 26 bubble_params.delegate = bubble; |
27 bubble_params.transparent = true; | 27 bubble_params.transparent = true; |
28 if (bubble->parent_window()) | 28 if (bubble->parent_window()) |
29 bubble_params.parent = bubble->parent_window(); | 29 bubble_params.parent = bubble->parent_window(); |
30 else | 30 else |
31 bubble_params.parent_widget = parent; | 31 bubble_params.parent_widget = bubble->anchor_widget(); |
32 if (bubble->use_focusless()) | 32 if (bubble->use_focusless()) |
33 bubble_params.can_activate = false; | 33 bubble_params.can_activate = false; |
34 #if defined(OS_WIN) && !defined(USE_AURA) | 34 #if defined(OS_WIN) && !defined(USE_AURA) |
35 bubble_params.type = Widget::InitParams::TYPE_WINDOW_FRAMELESS; | 35 bubble_params.type = Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
36 bubble_params.transparent = false; | 36 bubble_params.transparent = false; |
37 #endif | 37 #endif |
38 bubble_widget->Init(bubble_params); | 38 bubble_widget->Init(bubble_params); |
39 return bubble_widget; | 39 return bubble_widget; |
40 } | 40 } |
41 | 41 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 } | 74 } |
75 | 75 |
76 private: | 76 private: |
77 BubbleDelegateView* bubble_; | 77 BubbleDelegateView* bubble_; |
78 Widget* widget_; | 78 Widget* widget_; |
79 | 79 |
80 DISALLOW_COPY_AND_ASSIGN(BubbleBorderDelegate); | 80 DISALLOW_COPY_AND_ASSIGN(BubbleBorderDelegate); |
81 }; | 81 }; |
82 | 82 |
83 // Create a widget to host the bubble's border. | 83 // Create a widget to host the bubble's border. |
84 Widget* CreateBorderWidget(BubbleDelegateView* bubble, Widget* parent) { | 84 Widget* CreateBorderWidget(BubbleDelegateView* bubble) { |
85 Widget* border_widget = new Widget(); | 85 Widget* border_widget = new Widget(); |
86 Widget::InitParams border_params(Widget::InitParams::TYPE_BUBBLE); | 86 Widget::InitParams border_params(Widget::InitParams::TYPE_BUBBLE); |
87 border_params.delegate = new BubbleBorderDelegate(bubble, border_widget); | 87 border_params.delegate = new BubbleBorderDelegate(bubble, border_widget); |
88 border_params.transparent = true; | 88 border_params.transparent = true; |
89 border_params.parent_widget = parent; | 89 border_params.parent_widget = bubble->anchor_widget(); |
90 border_widget->Init(border_params); | 90 border_widget->Init(border_params); |
91 return border_widget; | 91 return border_widget; |
92 } | 92 } |
93 #endif | 93 #endif |
94 | 94 |
95 } // namespace | 95 } // namespace |
96 | 96 |
97 #if defined(OS_WIN) && !defined(USE_AURA) | 97 #if defined(OS_WIN) && !defined(USE_AURA) |
98 const SkColor BubbleDelegateView::kBackgroundColor = | 98 const SkColor BubbleDelegateView::kBackgroundColor = |
99 color_utils::GetSysSkColor(COLOR_WINDOW); | 99 color_utils::GetSysSkColor(COLOR_WINDOW); |
100 #else | 100 #else |
101 // TODO(beng): source from theme provider. | 101 // TODO(beng): source from theme provider. |
102 const SkColor BubbleDelegateView::kBackgroundColor = SK_ColorWHITE; | 102 const SkColor BubbleDelegateView::kBackgroundColor = SK_ColorWHITE; |
103 #endif | 103 #endif |
104 | 104 |
105 BubbleDelegateView::BubbleDelegateView() | 105 BubbleDelegateView::BubbleDelegateView() |
106 : close_on_esc_(true), | 106 : close_on_esc_(true), |
107 close_on_deactivate_(true), | 107 close_on_deactivate_(true), |
108 anchor_view_(NULL), | 108 anchor_view_(NULL), |
| 109 anchor_widget_(NULL), |
| 110 move_with_anchor_(false), |
109 arrow_location_(BubbleBorder::TOP_LEFT), | 111 arrow_location_(BubbleBorder::TOP_LEFT), |
110 color_(kBackgroundColor), | 112 color_(kBackgroundColor), |
111 margin_(kDefaultMargin), | 113 margin_(kDefaultMargin), |
112 original_opacity_(255), | 114 original_opacity_(255), |
113 border_widget_(NULL), | 115 border_widget_(NULL), |
114 use_focusless_(false), | 116 use_focusless_(false), |
115 parent_window_(NULL) { | 117 parent_window_(NULL) { |
116 set_background(views::Background::CreateSolidBackground(color_)); | 118 set_background(views::Background::CreateSolidBackground(color_)); |
117 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, 0)); | 119 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, 0)); |
118 } | 120 } |
119 | 121 |
120 BubbleDelegateView::BubbleDelegateView( | 122 BubbleDelegateView::BubbleDelegateView( |
121 View* anchor_view, | 123 View* anchor_view, |
122 BubbleBorder::ArrowLocation arrow_location) | 124 BubbleBorder::ArrowLocation arrow_location) |
123 : close_on_esc_(true), | 125 : close_on_esc_(true), |
124 close_on_deactivate_(true), | 126 close_on_deactivate_(true), |
125 anchor_view_(anchor_view), | 127 anchor_view_(anchor_view), |
| 128 anchor_widget_(NULL), |
| 129 move_with_anchor_(false), |
126 arrow_location_(arrow_location), | 130 arrow_location_(arrow_location), |
127 color_(kBackgroundColor), | 131 color_(kBackgroundColor), |
128 margin_(kDefaultMargin), | 132 margin_(kDefaultMargin), |
129 original_opacity_(255), | 133 original_opacity_(255), |
130 border_widget_(NULL), | 134 border_widget_(NULL), |
131 use_focusless_(false), | 135 use_focusless_(false), |
132 parent_window_(NULL) { | 136 parent_window_(NULL) { |
133 set_background(views::Background::CreateSolidBackground(color_)); | 137 set_background(views::Background::CreateSolidBackground(color_)); |
134 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, 0)); | 138 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, 0)); |
135 } | 139 } |
136 | 140 |
137 BubbleDelegateView::~BubbleDelegateView() {} | 141 BubbleDelegateView::~BubbleDelegateView() {} |
138 | 142 |
139 // static | 143 // static |
140 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) { | 144 Widget* BubbleDelegateView::CreateBubble(BubbleDelegateView* bubble_delegate) { |
141 bubble_delegate->Init(); | 145 bubble_delegate->Init(); |
142 Widget* parent = bubble_delegate->anchor_view() ? | 146 // Determine the anchor widget from the anchor view at bubble creation time. |
| 147 bubble_delegate->anchor_widget_ = bubble_delegate->anchor_view() ? |
143 bubble_delegate->anchor_view()->GetWidget() : NULL; | 148 bubble_delegate->anchor_view()->GetWidget() : NULL; |
144 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate, parent); | 149 Widget* bubble_widget = CreateBubbleWidget(bubble_delegate); |
145 | 150 |
146 #if defined(OS_WIN) && !defined(USE_AURA) | 151 #if defined(OS_WIN) && !defined(USE_AURA) |
147 // First set the contents view to initialize view bounds for widget sizing. | 152 // First set the contents view to initialize view bounds for widget sizing. |
148 bubble_widget->SetContentsView(bubble_delegate->GetContentsView()); | 153 bubble_widget->SetContentsView(bubble_delegate->GetContentsView()); |
149 bubble_delegate->border_widget_ = CreateBorderWidget(bubble_delegate, parent); | 154 bubble_delegate->border_widget_ = CreateBorderWidget(bubble_delegate); |
150 #endif | 155 #endif |
151 | 156 |
152 bubble_delegate->SizeToContents(); | 157 bubble_delegate->SizeToContents(); |
153 bubble_widget->AddObserver(bubble_delegate); | 158 bubble_widget->AddObserver(bubble_delegate); |
154 return bubble_widget; | 159 return bubble_widget; |
155 } | 160 } |
156 | 161 |
157 View* BubbleDelegateView::GetInitiallyFocusedView() { | 162 View* BubbleDelegateView::GetInitiallyFocusedView() { |
158 return this; | 163 return this; |
159 } | 164 } |
160 | 165 |
161 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() { | 166 BubbleDelegateView* BubbleDelegateView::AsBubbleDelegate() { |
162 return this; | 167 return this; |
163 } | 168 } |
164 | 169 |
165 View* BubbleDelegateView::GetContentsView() { | 170 View* BubbleDelegateView::GetContentsView() { |
166 return this; | 171 return this; |
167 } | 172 } |
168 | 173 |
169 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView( | 174 NonClientFrameView* BubbleDelegateView::CreateNonClientFrameView( |
170 Widget* widget) { | 175 Widget* widget) { |
171 return new BubbleFrameView(arrow_location(), color(), margin()); | 176 return new BubbleFrameView(arrow_location(), color(), margin()); |
172 } | 177 } |
173 | 178 |
| 179 void BubbleDelegateView::OnWidgetClosing(Widget* widget) { |
| 180 if (anchor_widget() == widget) { |
| 181 anchor_view_ = NULL; |
| 182 anchor_widget_ = NULL; |
| 183 } |
| 184 } |
| 185 |
174 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget, | 186 void BubbleDelegateView::OnWidgetVisibilityChanged(Widget* widget, |
175 bool visible) { | 187 bool visible) { |
176 if (widget == GetWidget()) { | 188 if (widget != GetWidget()) |
177 if (visible) { | 189 return; |
178 if (border_widget_) | 190 |
179 border_widget_->Show(); | 191 if (visible) { |
180 GetFocusManager()->SetFocusedView(GetInitiallyFocusedView()); | 192 if (border_widget_) |
181 Widget* anchor_widget = anchor_view() ? anchor_view()->GetWidget() : NULL; | 193 border_widget_->Show(); |
182 if (anchor_widget && anchor_widget->GetTopLevelWidget()) | 194 if (anchor_widget()) |
183 anchor_widget->GetTopLevelWidget()->DisableInactiveRendering(); | 195 anchor_widget()->AddObserver(this); |
184 } else if (border_widget_) { | 196 GetFocusManager()->SetFocusedView(GetInitiallyFocusedView()); |
| 197 if (anchor_widget() && anchor_widget()->GetTopLevelWidget()) |
| 198 anchor_widget()->GetTopLevelWidget()->DisableInactiveRendering(); |
| 199 } else { |
| 200 if (border_widget_) |
185 border_widget_->Hide(); | 201 border_widget_->Hide(); |
186 } | 202 if (anchor_widget()) |
| 203 anchor_widget()->RemoveObserver(this); |
187 } | 204 } |
188 } | 205 } |
189 | 206 |
190 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget, | 207 void BubbleDelegateView::OnWidgetActivationChanged(Widget* widget, |
191 bool active) { | 208 bool active) { |
192 if (close_on_deactivate() && widget == GetWidget() && !active) | 209 if (close_on_deactivate() && widget == GetWidget() && !active) |
193 GetWidget()->Close(); | 210 GetWidget()->Close(); |
194 } | 211 } |
195 | 212 |
| 213 void BubbleDelegateView::OnWidgetMoved(Widget* widget) { |
| 214 if (move_with_anchor() && anchor_widget() == widget) |
| 215 SizeToContents(); |
| 216 } |
| 217 |
196 gfx::Rect BubbleDelegateView::GetAnchorRect() { | 218 gfx::Rect BubbleDelegateView::GetAnchorRect() { |
197 return anchor_view() ? anchor_view()->GetScreenBounds() : gfx::Rect(); | 219 return anchor_view() ? anchor_view()->GetScreenBounds() : gfx::Rect(); |
198 } | 220 } |
199 | 221 |
200 void BubbleDelegateView::Show() { | 222 void BubbleDelegateView::Show() { |
201 GetWidget()->Show(); | 223 GetWidget()->Show(); |
202 } | 224 } |
203 | 225 |
204 void BubbleDelegateView::StartFade(bool fade_in) { | 226 void BubbleDelegateView::StartFade(bool fade_in) { |
205 fade_animation_.reset(new ui::SlideAnimation(this)); | 227 fade_animation_.reset(new ui::SlideAnimation(this)); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 | 325 |
304 #if defined(OS_WIN) && !defined(USE_AURA) | 326 #if defined(OS_WIN) && !defined(USE_AURA) |
305 gfx::Rect BubbleDelegateView::GetBubbleClientBounds() const { | 327 gfx::Rect BubbleDelegateView::GetBubbleClientBounds() const { |
306 gfx::Rect client_bounds(GetBubbleFrameView()->GetBoundsForClientView()); | 328 gfx::Rect client_bounds(GetBubbleFrameView()->GetBoundsForClientView()); |
307 client_bounds.Offset(border_widget_->GetWindowScreenBounds().origin()); | 329 client_bounds.Offset(border_widget_->GetWindowScreenBounds().origin()); |
308 return client_bounds; | 330 return client_bounds; |
309 } | 331 } |
310 #endif | 332 #endif |
311 | 333 |
312 } // namespace views | 334 } // namespace views |
OLD | NEW |