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/location_bar/icon_label_bubble_view.h" | 5 #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h" |
6 | 6 |
7 #include "chrome/browser/ui/layout_constants.h" | 7 #include "chrome/browser/ui/layout_constants.h" |
8 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" | 8 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" |
9 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 9 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
10 #include "ui/accessibility/ax_node_data.h" | 10 #include "ui/accessibility/ax_node_data.h" |
11 #include "ui/compositor/layer_animator.h" | |
12 #include "ui/compositor/scoped_layer_animation_settings.h" | |
11 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
12 #include "ui/gfx/color_utils.h" | 14 #include "ui/gfx/color_utils.h" |
13 #include "ui/gfx/scoped_canvas.h" | 15 #include "ui/gfx/scoped_canvas.h" |
14 #include "ui/native_theme/native_theme.h" | 16 #include "ui/native_theme/native_theme.h" |
17 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" | |
15 #include "ui/views/animation/ink_drop_highlight.h" | 18 #include "ui/views/animation/ink_drop_highlight.h" |
19 #include "ui/views/animation/ink_drop_impl.h" | |
20 #include "ui/views/animation/ink_drop_ripple.h" | |
16 #include "ui/views/border.h" | 21 #include "ui/views/border.h" |
17 #include "ui/views/controls/image_view.h" | 22 #include "ui/views/controls/image_view.h" |
18 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
19 | 24 |
20 namespace { | 25 namespace { |
21 | 26 |
22 // Amount of space on either side of the separator that appears after the label. | 27 // Amount of space on either side of the separator that appears after the label. |
23 constexpr int kSpaceBesideSeparator = 8; | 28 constexpr int kSpaceBesideSeparator = 8; |
24 | 29 |
30 // The length of the separator's fade animation. | |
31 constexpr int kFadeInDurationMs = 250; | |
32 constexpr int kFadeOutDurationMs = 175; | |
33 | |
25 } // namespace | 34 } // namespace |
26 | 35 |
36 ////////////////////////////////////////////////////////////////// | |
37 // SeparatorView class | |
38 | |
39 // A view that draws the separator. | |
40 class SeparatorView : public views::View, public gfx::AnimationDelegate { | |
41 public: | |
42 explicit SeparatorView(IconLabelBubbleView* owner); | |
43 | |
44 // View: | |
45 void OnPaint(gfx::Canvas* canvas) override; | |
46 bool CanProcessEventsWithinSubtree() const override; | |
47 | |
48 // Updates the opacity based on the ink drop's state. | |
49 void UpdateOpacity(); | |
50 | |
51 private: | |
52 // Weak. | |
53 IconLabelBubbleView* owner_; | |
54 }; | |
55 | |
56 SeparatorView::SeparatorView(IconLabelBubbleView* owner) { | |
57 DCHECK(owner); | |
58 owner_ = owner; | |
59 | |
60 SetPaintToLayer(); | |
61 layer()->SetFillsBoundsOpaquely(false); | |
62 } | |
63 | |
64 void SeparatorView::OnPaint(gfx::Canvas* canvas) { | |
65 if (!owner_->ShouldShowLabel()) | |
66 return; | |
67 | |
68 const SkColor plain_text_color = owner_->GetNativeTheme()->GetSystemColor( | |
69 ui::NativeTheme::kColorId_TextfieldDefaultColor); | |
70 const SkColor separator_color = SkColorSetA( | |
71 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | |
72 | |
73 gfx::Rect bounds(owner_->label()->bounds()); | |
74 const int kSeparatorHeight = 16; | |
75 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2); | |
76 | |
77 // Draw the 1 px separator. | |
78 gfx::ScopedCanvas scoped_canvas(canvas); | |
79 const float scale = canvas->UndoDeviceScaleFactor(); | |
80 // Keep the separator aligned on a pixel center. | |
81 const gfx::RectF pixel_aligned_bounds = | |
82 gfx::ScaleRect(gfx::RectF(bounds), scale) - gfx::Vector2dF(0.5f, 0); | |
83 canvas->DrawLine(pixel_aligned_bounds.top_right(), | |
84 pixel_aligned_bounds.bottom_right(), separator_color); | |
85 } | |
86 | |
87 bool SeparatorView::CanProcessEventsWithinSubtree() const { | |
88 return false; | |
89 } | |
90 | |
91 void SeparatorView::UpdateOpacity() { | |
92 views::InkDrop* ink_drop = owner_->GetInkDrop(); | |
93 DCHECK(ink_drop); | |
94 | |
95 views::InkDropState state = ink_drop->GetTargetInkDropState(); | |
96 float opacity = 0.0f; | |
97 float duration = kFadeOutDurationMs; | |
98 if (!ink_drop->ShouldHighlight() && | |
bruthig
2017/03/08 18:58:24
Should this really care about the logical hover st
spqchan
2017/03/14 00:07:52
The separator needs to be invisible when the rippl
| |
99 (state == views::InkDropState::HIDDEN || | |
100 state == views::InkDropState::DEACTIVATED)) { | |
101 opacity = 1.0f; | |
102 duration = kFadeInDurationMs; | |
103 } | |
104 | |
105 ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator()); | |
106 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(duration)); | |
107 animation.SetTweenType(gfx::Tween::Type::EASE_IN); | |
108 layer()->SetOpacity(opacity); | |
109 } | |
110 | |
111 ////////////////////////////////////////////////////////////////// | |
112 // IconLabelBubbleView class | |
113 | |
27 IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list, | 114 IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list, |
28 bool elide_in_middle) | 115 bool elide_in_middle) |
29 : image_(new views::ImageView()), | 116 : image_(new views::ImageView()), |
30 label_(new views::Label(base::string16(), font_list)) { | 117 label_(new views::Label(base::string16(), font_list)), |
118 ink_drop_container_(new views::InkDropContainerView()) { | |
31 // Disable separate hit testing for |image_|. This prevents views treating | 119 // Disable separate hit testing for |image_|. This prevents views treating |
32 // |image_| as a separate mouse hover region from |this|. | 120 // |image_| as a separate mouse hover region from |this|. |
33 image_->set_interactive(false); | 121 image_->set_interactive(false); |
34 image_->SetBorder(views::CreateEmptyBorder( | 122 image_->SetBorder(views::CreateEmptyBorder( |
35 gfx::Insets(LocationBarView::kIconInteriorPadding))); | 123 gfx::Insets(LocationBarView::kIconInteriorPadding))); |
36 AddChildView(image_); | 124 AddChildView(image_); |
37 | 125 |
38 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 126 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
39 | 127 |
40 if (elide_in_middle) | 128 if (elide_in_middle) |
41 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); | 129 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); |
42 AddChildView(label_); | 130 AddChildView(label_); |
43 | 131 |
132 separator_view_.reset(new SeparatorView(this)); | |
133 AddChildView(separator_view_.get()); | |
134 | |
135 AddChildView(ink_drop_container_); | |
136 ink_drop_container_->SetPaintToLayer(); | |
bruthig
2017/03/08 18:58:24
Why does the |ink_drop_container_| have to paint t
spqchan
2017/03/14 00:07:52
That's a good point. I just realized that the cont
| |
137 ink_drop_container_->layer()->SetFillsBoundsOpaquely(false); | |
138 ink_drop_container_->SetVisible(false); | |
139 | |
44 // Bubbles are given the full internal height of the location bar so that all | 140 // Bubbles are given the full internal height of the location bar so that all |
45 // child views in the location bar have the same height. The visible height of | 141 // child views in the location bar have the same height. The visible height of |
46 // the bubble should be smaller, so use an empty border to shrink down the | 142 // the bubble should be smaller, so use an empty border to shrink down the |
47 // content bounds so the background gets painted correctly. | 143 // content bounds so the background gets painted correctly. |
48 SetBorder(views::CreateEmptyBorder( | 144 SetBorder(views::CreateEmptyBorder( |
49 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); | 145 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); |
50 | 146 |
51 // Flip the canvas in RTL so the separator is drawn on the correct side. | 147 // Flip the canvas in RTL so the separator is drawn on the correct side. |
52 EnableCanvasFlippingForRTLUI(true); | 148 EnableCanvasFlippingForRTLUI(true); |
53 } | 149 } |
(...skipping 14 matching lines...) Expand all Loading... | |
68 } | 164 } |
69 | 165 |
70 double IconLabelBubbleView::WidthMultiplier() const { | 166 double IconLabelBubbleView::WidthMultiplier() const { |
71 return 1.0; | 167 return 1.0; |
72 } | 168 } |
73 | 169 |
74 bool IconLabelBubbleView::IsShrinking() const { | 170 bool IconLabelBubbleView::IsShrinking() const { |
75 return false; | 171 return false; |
76 } | 172 } |
77 | 173 |
174 bool IconLabelBubbleView::IsInkDropEnabled() const { | |
175 return false; | |
176 } | |
177 | |
78 bool IconLabelBubbleView::OnActivate(const ui::Event& event) { | 178 bool IconLabelBubbleView::OnActivate(const ui::Event& event) { |
79 return false; | 179 return false; |
80 } | 180 } |
81 | 181 |
82 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 182 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
83 // Height will be ignored by the LocationBarView. | 183 // Height will be ignored by the LocationBarView. |
84 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 184 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
85 } | 185 } |
86 | 186 |
87 bool IconLabelBubbleView::OnKeyPressed(const ui::KeyEvent& event) { | 187 bool IconLabelBubbleView::OnKeyPressed(const ui::KeyEvent& event) { |
(...skipping 25 matching lines...) Expand all Loading... | |
113 image_->SetBounds(0, 0, image_width, height()); | 213 image_->SetBounds(0, 0, image_width, height()); |
114 | 214 |
115 // Compute the label bounds. The label gets whatever size is left over after | 215 // Compute the label bounds. The label gets whatever size is left over after |
116 // accounting for the preferred image width and padding amounts. Note that if | 216 // accounting for the preferred image width and padding amounts. Note that if |
117 // the label has zero size it doesn't actually matter what we compute its X | 217 // the label has zero size it doesn't actually matter what we compute its X |
118 // value to be, since it won't be visible. | 218 // value to be, since it won't be visible. |
119 const int label_x = image_->bounds().right() + GetInternalSpacing(); | 219 const int label_x = image_->bounds().right() + GetInternalSpacing(); |
120 const int label_width = | 220 const int label_width = |
121 std::max(0, width() - label_x - bubble_trailing_padding); | 221 std::max(0, width() - label_x - bubble_trailing_padding); |
122 label_->SetBounds(label_x, 0, label_width, height()); | 222 label_->SetBounds(label_x, 0, label_width, height()); |
223 separator_view_->SetBoundsRect(GetLocalBounds()); | |
224 ink_drop_container_->SetBoundsRect(GetLocalBounds()); | |
225 } | |
226 | |
227 bool IconLabelBubbleView::OnMousePressed(const ui::MouseEvent& event) { | |
228 if (event.IsOnlyLeftMouseButton() && IsInkDropEnabled()) | |
229 AnimateInkDrop(views::InkDropState::ACTION_PENDING, &event); | |
230 return true; | |
231 } | |
232 | |
233 void IconLabelBubbleView::OnMouseReleased(const ui::MouseEvent& event) { | |
234 if (!event.IsLeftMouseButton() || !IsInkDropEnabled()) | |
235 return; | |
236 | |
237 const bool activated = HitTestPoint(event.location()); | |
238 AnimateInkDrop( | |
239 activated ? views::InkDropState::ACTIVATED : views::InkDropState::HIDDEN, | |
240 &event); | |
123 } | 241 } |
124 | 242 |
125 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 243 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
126 label_->GetAccessibleNodeData(node_data); | 244 label_->GetAccessibleNodeData(node_data); |
127 } | 245 } |
128 | 246 |
129 void IconLabelBubbleView::OnNativeThemeChanged( | 247 void IconLabelBubbleView::OnNativeThemeChanged( |
130 const ui::NativeTheme* native_theme) { | 248 const ui::NativeTheme* native_theme) { |
131 label_->SetEnabledColor(GetTextColor()); | 249 label_->SetEnabledColor(GetTextColor()); |
132 label_->SetBackgroundColor(GetParentBackgroundColor()); | 250 label_->SetBackgroundColor(GetParentBackgroundColor()); |
133 SchedulePaint(); | 251 SchedulePaint(); |
134 } | 252 } |
135 | 253 |
136 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { | 254 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { |
137 image()->SetPaintToLayer(); | 255 ink_drop_container_->AddInkDropLayer(ink_drop_layer); |
138 image()->layer()->SetFillsBoundsOpaquely(false); | 256 SchedulePaint(); |
bruthig
2017/03/08 18:58:24
Why is this SchedulePaint() necessary?
spqchan
2017/03/14 00:07:52
Whoops. Thanks for catching it, this should be rem
| |
139 InkDropHostView::AddInkDropLayer(ink_drop_layer); | |
140 } | 257 } |
141 | 258 |
142 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | 259 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { |
143 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); | 260 ink_drop_container_->RemoveInkDropLayer(ink_drop_layer); |
144 image()->DestroyLayer(); | 261 SchedulePaint(); |
bruthig
2017/03/08 18:58:24
Why is this SchedulePaint() necessary?
spqchan
2017/03/14 00:07:52
Whoops. Thanks for catching it, this should be rem
| |
262 } | |
263 | |
264 std::unique_ptr<views::InkDrop> IconLabelBubbleView::CreateInkDrop() { | |
265 std::unique_ptr<views::InkDropImpl> ink_drop = CreateDefaultInkDropImpl(); | |
266 ink_drop->SetShowHighlightOnFocus(true); | |
bruthig
2017/03/08 18:58:24
|ink_drop| doesn't appear to be used?
spqchan
2017/03/14 00:07:52
Whoops, thanks for the catch!
| |
267 return CreateDefaultFloodFillInkDropImpl(); | |
145 } | 268 } |
146 | 269 |
147 std::unique_ptr<views::InkDropHighlight> | 270 std::unique_ptr<views::InkDropHighlight> |
148 IconLabelBubbleView::CreateInkDropHighlight() const { | 271 IconLabelBubbleView::CreateInkDropHighlight() const { |
149 // Only show a highlight effect when the label is empty/invisible. | 272 if (!IsInkDropEnabled()) |
150 return label()->visible() ? nullptr | 273 return nullptr; |
151 : InkDropHostView::CreateInkDropHighlight(); | 274 |
275 gfx::PointF ink_drop_center = gfx::RectF(GetLocalBounds()).CenterPoint(); | |
276 gfx::Size ink_drop_size = size(); | |
277 if (ink_drop_size.IsEmpty()) | |
278 ink_drop_size = gfx::Size(kDefaultInkDropSize, kDefaultInkDropSize); | |
279 | |
280 if (ShouldShowLabel()) { | |
281 int padding_offset = -GetPostSeparatorPadding(); | |
282 ink_drop_center.Offset(padding_offset / 2, 0); | |
283 ink_drop_size.Enlarge(padding_offset, 0); | |
284 } | |
285 | |
286 return InkDropHostView::CreateDefaultInkDropHighlight(ink_drop_center, | |
287 ink_drop_size); | |
288 } | |
289 | |
290 std::unique_ptr<views::InkDropRipple> IconLabelBubbleView::CreateInkDropRipple() | |
291 const { | |
292 if (!IsInkDropEnabled()) | |
293 return nullptr; | |
294 | |
295 gfx::Point ink_drop_center = GetLocalBounds().CenterPoint(); | |
296 gfx::Size ink_drop_size = size(); | |
297 if (ink_drop_size.IsEmpty()) | |
298 return CreateDefaultInkDropRipple(ink_drop_center); | |
299 | |
300 if (ShouldShowLabel()) { | |
301 int padding_offset = -GetPostSeparatorPadding(); | |
302 ink_drop_center.Offset(padding_offset / 2, 0); | |
303 ink_drop_size.Enlarge(padding_offset, 0); | |
304 } | |
305 | |
306 return base::MakeUnique<views::FloodFillInkDropRipple>( | |
307 ink_drop_size, ink_drop_center, GetInkDropBaseColor(), | |
308 ink_drop_visible_opacity()); | |
152 } | 309 } |
153 | 310 |
154 SkColor IconLabelBubbleView::GetInkDropBaseColor() const { | 311 SkColor IconLabelBubbleView::GetInkDropBaseColor() const { |
155 return color_utils::DeriveDefaultIconColor(GetTextColor()); | 312 return color_utils::DeriveDefaultIconColor(GetNativeTheme()->GetSystemColor( |
313 ui::NativeTheme::kColorId_TextfieldDefaultColor)); | |
314 } | |
315 | |
316 void IconLabelBubbleView::InkDropAnimationStarted() { | |
317 separator_view_->UpdateOpacity(); | |
318 } | |
319 | |
320 void IconLabelBubbleView::OnWidgetDestroying(views::Widget* widget) { | |
321 widget->RemoveObserver(this); | |
322 } | |
323 | |
324 void IconLabelBubbleView::OnWidgetVisibilityChanged(views::Widget* widget, | |
325 bool visible) { | |
326 // |widget| is a bubble that has just got shown / hidden. | |
327 if (!visible) | |
328 AnimateInkDrop(views::InkDropState::DEACTIVATED, nullptr /* event */); | |
156 } | 329 } |
157 | 330 |
158 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 331 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
159 return GetNativeTheme()->GetSystemColor( | 332 return GetNativeTheme()->GetSystemColor( |
160 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 333 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
161 } | 334 } |
162 | 335 |
163 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 336 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
164 gfx::Size size(image_->GetPreferredSize()); | 337 gfx::Size size(image_->GetPreferredSize()); |
165 const bool shrinking = IsShrinking(); | 338 const bool shrinking = IsShrinking(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 const views::Widget* widget = GetWidget(); | 382 const views::Widget* widget = GetWidget(); |
210 // There may be no widget in tests, and in ash there may be no compositor if | 383 // There may be no widget in tests, and in ash there may be no compositor if |
211 // the native view of the Widget doesn't have a parent. | 384 // the native view of the Widget doesn't have a parent. |
212 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; | 385 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; |
213 return compositor ? compositor->device_scale_factor() : 1.0f; | 386 return compositor ? compositor->device_scale_factor() : 1.0f; |
214 } | 387 } |
215 | 388 |
216 const char* IconLabelBubbleView::GetClassName() const { | 389 const char* IconLabelBubbleView::GetClassName() const { |
217 return "IconLabelBubbleView"; | 390 return "IconLabelBubbleView"; |
218 } | 391 } |
219 | |
220 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | |
221 if (!ShouldShowLabel()) | |
222 return; | |
223 | |
224 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( | |
225 ui::NativeTheme::kColorId_TextfieldDefaultColor); | |
226 const SkColor separator_color = SkColorSetA( | |
227 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | |
228 | |
229 gfx::Rect bounds(label_->bounds()); | |
230 const int kSeparatorHeight = 16; | |
231 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2); | |
232 | |
233 // Draw the 1 px separator. | |
234 gfx::ScopedCanvas scoped_canvas(canvas); | |
235 const float scale = canvas->UndoDeviceScaleFactor(); | |
236 // Keep the separator aligned on a pixel center. | |
237 const gfx::RectF pixel_aligned_bounds = | |
238 gfx::ScaleRect(gfx::RectF(bounds), scale) - gfx::Vector2dF(0.5f, 0); | |
239 canvas->DrawLine(pixel_aligned_bounds.top_right(), | |
240 pixel_aligned_bounds.bottom_right(), separator_color); | |
241 } | |
OLD | NEW |