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_mask.h" | |
21 #include "ui/views/animation/ink_drop_ripple.h" | |
16 #include "ui/views/border.h" | 22 #include "ui/views/border.h" |
17 #include "ui/views/controls/image_view.h" | 23 #include "ui/views/controls/image_view.h" |
18 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
19 | 25 |
20 namespace { | 26 namespace { |
21 | 27 |
22 // Amount of space on either side of the separator that appears after the label. | 28 // Amount of space on either side of the separator that appears after the label. |
23 constexpr int kSpaceBesideSeparator = 8; | 29 constexpr int kSpaceBesideSeparator = 8; |
24 | 30 |
31 // Radius of the ink drop mask's rounded rectangle shape. | |
32 constexpr int kInkDropMaskRadius = 2; | |
33 | |
34 // The length of the separator's fade animation. | |
35 constexpr int kFadeInDurationMs = 250; | |
36 constexpr int kFadeOutDurationMs = 175; | |
37 | |
25 } // namespace | 38 } // namespace |
26 | 39 |
40 ////////////////////////////////////////////////////////////////// | |
41 // SeparatorView class | |
42 | |
43 IconLabelBubbleView::SeparatorView::SeparatorView(IconLabelBubbleView* owner) { | |
44 DCHECK(owner); | |
45 owner_ = owner; | |
46 SetPaintToLayer(); | |
47 layer()->SetFillsBoundsOpaquely(false); | |
48 } | |
49 | |
50 void IconLabelBubbleView::SeparatorView::OnPaint(gfx::Canvas* canvas) { | |
51 if (!owner_->ShouldShowLabel()) | |
52 return; | |
53 | |
54 const SkColor plain_text_color = owner_->GetNativeTheme()->GetSystemColor( | |
55 ui::NativeTheme::kColorId_TextfieldDefaultColor); | |
56 const SkColor separator_color = SkColorSetA( | |
57 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | |
58 | |
59 gfx::Rect bounds(owner_->label()->bounds()); | |
60 const int kSeparatorHeight = 16; | |
61 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2); | |
62 bounds.set_width(bounds.width() + kSpaceBesideSeparator); | |
63 canvas->Draw1pxLine(gfx::PointF(bounds.top_right()), | |
64 gfx::PointF(bounds.bottom_right()), separator_color); | |
65 } | |
66 | |
67 bool IconLabelBubbleView::SeparatorView::CanProcessEventsWithinSubtree() const { | |
68 return false; | |
69 } | |
70 | |
71 void IconLabelBubbleView::SeparatorView::UpdateOpacity() { | |
72 views::InkDrop* ink_drop = owner_->GetInkDrop(); | |
73 DCHECK(ink_drop); | |
74 | |
75 // If an inkdrop highlight or ripple is animating in or visible, the | |
76 // separator should fade out. | |
77 views::InkDropState state = ink_drop->GetTargetInkDropState(); | |
78 float opacity = 0.0f; | |
79 float duration = kFadeOutDurationMs; | |
80 if (!ink_drop->IsHighlightFadingInOrVisible() && | |
81 (state == views::InkDropState::HIDDEN || | |
82 state == views::InkDropState::DEACTIVATED)) { | |
bruthig
2017/03/22 17:20:16
You may want to include ACTION_TRIGGERED. It auto-
spqchan
2017/03/24 17:58:03
Done.
| |
83 opacity = 1.0f; | |
84 duration = kFadeInDurationMs; | |
85 } | |
86 | |
87 ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator()); | |
88 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(duration)); | |
89 animation.SetTweenType(gfx::Tween::Type::EASE_IN); | |
90 layer()->SetOpacity(opacity); | |
91 } | |
92 | |
93 ////////////////////////////////////////////////////////////////// | |
94 // IconLabelBubbleView class | |
95 | |
27 IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list, | 96 IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list, |
28 bool elide_in_middle) | 97 bool elide_in_middle) |
29 : image_(new views::ImageView()), | 98 : CustomButton(this), |
30 label_(new views::Label(base::string16(), {font_list})) { | 99 image_(new views::ImageView()), |
100 label_(new views::Label(base::string16(), {font_list})), | |
101 ink_drop_container_(new views::InkDropContainerView()), | |
102 suppress_button_action_(false) { | |
31 // Disable separate hit testing for |image_|. This prevents views treating | 103 // Disable separate hit testing for |image_|. This prevents views treating |
32 // |image_| as a separate mouse hover region from |this|. | 104 // |image_| as a separate mouse hover region from |this|. |
33 image_->set_can_process_events_within_subtree(false); | 105 image_->set_can_process_events_within_subtree(false); |
34 image_->SetBorder(views::CreateEmptyBorder( | 106 image_->SetBorder(views::CreateEmptyBorder( |
35 gfx::Insets(LocationBarView::kIconInteriorPadding))); | 107 gfx::Insets(LocationBarView::kIconInteriorPadding))); |
36 AddChildView(image_); | 108 AddChildView(image_); |
37 | 109 |
38 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 110 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
39 | 111 |
40 if (elide_in_middle) | 112 if (elide_in_middle) |
41 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); | 113 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); |
42 AddChildView(label_); | 114 AddChildView(label_); |
43 | 115 |
116 separator_view_.reset(new SeparatorView(this)); | |
117 AddChildView(separator_view_.get()); | |
118 | |
119 AddChildView(ink_drop_container_); | |
120 ink_drop_container_->SetVisible(false); | |
121 | |
44 // Bubbles are given the full internal height of the location bar so that all | 122 // 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 | 123 // 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 | 124 // the bubble should be smaller, so use an empty border to shrink down the |
47 // content bounds so the background gets painted correctly. | 125 // content bounds so the background gets painted correctly. |
48 SetBorder(views::CreateEmptyBorder( | 126 SetBorder(views::CreateEmptyBorder( |
49 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); | 127 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); |
50 | 128 |
51 // Flip the canvas in RTL so the separator is drawn on the correct side. | 129 // Flip the canvas in RTL so the separator is drawn on the correct side. |
52 EnableCanvasFlippingForRTLUI(true); | 130 EnableCanvasFlippingForRTLUI(true); |
53 } | 131 } |
54 | 132 |
55 IconLabelBubbleView::~IconLabelBubbleView() { | 133 IconLabelBubbleView::~IconLabelBubbleView() { |
56 } | 134 } |
57 | 135 |
136 void IconLabelBubbleView::ButtonPressed(Button* sender, | |
137 const ui::Event& event) { | |
138 if (!suppress_button_action_) | |
139 OnActivate(event); | |
140 } | |
141 | |
142 void IconLabelBubbleView::InkDropAnimationStarted() { | |
143 separator_view_->UpdateOpacity(); | |
144 } | |
145 | |
58 void IconLabelBubbleView::SetLabel(const base::string16& label) { | 146 void IconLabelBubbleView::SetLabel(const base::string16& label) { |
59 label_->SetText(label); | 147 label_->SetText(label); |
60 } | 148 } |
61 | 149 |
62 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { | 150 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { |
63 image_->SetImage(image_skia); | 151 image_->SetImage(image_skia); |
64 } | 152 } |
65 | 153 |
66 bool IconLabelBubbleView::ShouldShowLabel() const { | 154 bool IconLabelBubbleView::ShouldShowLabel() const { |
67 return label_->visible() && !label_->text().empty(); | 155 return label_->visible() && !label_->text().empty(); |
68 } | 156 } |
69 | 157 |
70 double IconLabelBubbleView::WidthMultiplier() const { | 158 double IconLabelBubbleView::WidthMultiplier() const { |
71 return 1.0; | 159 return 1.0; |
72 } | 160 } |
73 | 161 |
74 bool IconLabelBubbleView::IsShrinking() const { | 162 bool IconLabelBubbleView::IsShrinking() const { |
75 return false; | 163 return false; |
76 } | 164 } |
77 | 165 |
78 bool IconLabelBubbleView::OnActivate(const ui::Event& event) { | 166 bool IconLabelBubbleView::OnActivate(const ui::Event& event) { |
79 return false; | 167 return false; |
80 } | 168 } |
81 | 169 |
170 bool IconLabelBubbleView::IsBubbleShown() const { | |
171 return false; | |
172 } | |
173 | |
82 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 174 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
83 // Height will be ignored by the LocationBarView. | 175 // Height will be ignored by the LocationBarView. |
84 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 176 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
85 } | 177 } |
86 | 178 |
87 bool IconLabelBubbleView::OnKeyPressed(const ui::KeyEvent& event) { | 179 bool IconLabelBubbleView::OnKeyPressed(const ui::KeyEvent& event) { |
88 if (event.key_code() == ui::VKEY_RETURN) | 180 if (event.key_code() == ui::VKEY_RETURN) |
89 return OnActivate(event); | 181 return OnActivate(event); |
90 return false; | 182 return false; |
91 } | 183 } |
(...skipping 21 matching lines...) Expand all Loading... | |
113 image_->SetBounds(0, 0, image_width, height()); | 205 image_->SetBounds(0, 0, image_width, height()); |
114 | 206 |
115 // Compute the label bounds. The label gets whatever size is left over after | 207 // 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 | 208 // 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 | 209 // 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. | 210 // value to be, since it won't be visible. |
119 const int label_x = image_->bounds().right() + GetInternalSpacing(); | 211 const int label_x = image_->bounds().right() + GetInternalSpacing(); |
120 const int label_width = std::max( | 212 const int label_width = std::max( |
121 0, width() - label_x - bubble_trailing_padding - kSpaceBesideSeparator); | 213 0, width() - label_x - bubble_trailing_padding - kSpaceBesideSeparator); |
122 label_->SetBounds(label_x, 0, label_width, height()); | 214 label_->SetBounds(label_x, 0, label_width, height()); |
215 separator_view_->SetBoundsRect(GetLocalBounds()); | |
216 ink_drop_container_->SetBoundsRect(GetLocalBounds()); | |
217 } | |
218 | |
219 void IconLabelBubbleView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
220 if (ink_drop_mask_) | |
221 ink_drop_mask_->UpdateLayerSize(size()); | |
222 | |
223 InkDropHostView::OnBoundsChanged(previous_bounds); | |
224 } | |
225 | |
226 bool IconLabelBubbleView::OnMousePressed(const ui::MouseEvent& event) { | |
227 suppress_button_action_ = IsBubbleShown(); | |
228 return CustomButton::OnMousePressed(event); | |
bruthig
2017/03/24 20:48:02
Can you confirm that this isn't going to cause an
| |
123 } | 229 } |
124 | 230 |
125 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 231 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
126 label_->GetAccessibleNodeData(node_data); | 232 label_->GetAccessibleNodeData(node_data); |
127 } | 233 } |
128 | 234 |
129 void IconLabelBubbleView::OnNativeThemeChanged( | 235 void IconLabelBubbleView::OnNativeThemeChanged( |
130 const ui::NativeTheme* native_theme) { | 236 const ui::NativeTheme* native_theme) { |
131 label_->SetEnabledColor(GetTextColor()); | 237 label_->SetEnabledColor(GetTextColor()); |
132 label_->SetBackgroundColor(GetParentBackgroundColor()); | 238 label_->SetBackgroundColor(GetParentBackgroundColor()); |
133 SchedulePaint(); | 239 SchedulePaint(); |
134 } | 240 } |
135 | 241 |
136 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { | 242 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { |
137 image()->SetPaintToLayer(); | 243 ink_drop_mask_ = CreateInkDropMask(); |
138 image()->layer()->SetFillsBoundsOpaquely(false); | 244 if (ink_drop_mask_) |
139 InkDropHostView::AddInkDropLayer(ink_drop_layer); | 245 ink_drop_layer->SetMaskLayer(ink_drop_mask_->layer()); |
246 ink_drop_container_->AddInkDropLayer(ink_drop_layer); | |
140 } | 247 } |
141 | 248 |
142 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | 249 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { |
143 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); | 250 ink_drop_container_->RemoveInkDropLayer(ink_drop_layer); |
144 image()->DestroyLayer(); | 251 ink_drop_mask_.reset(); |
252 } | |
253 | |
254 std::unique_ptr<views::InkDrop> IconLabelBubbleView::CreateInkDrop() { | |
255 std::unique_ptr<views::InkDropImpl> ink_drop = | |
256 CreateDefaultFloodFillInkDropImpl(); | |
257 ink_drop->SetShowHighlightOnFocus(true); | |
258 ink_drop->SetObserver(this); | |
259 return std::move(ink_drop); | |
145 } | 260 } |
146 | 261 |
147 std::unique_ptr<views::InkDropHighlight> | 262 std::unique_ptr<views::InkDropHighlight> |
148 IconLabelBubbleView::CreateInkDropHighlight() const { | 263 IconLabelBubbleView::CreateInkDropHighlight() const { |
149 // Only show a highlight effect when the label is empty/invisible. | 264 gfx::PointF ink_drop_center = gfx::RectF(GetLocalBounds()).CenterPoint(); |
150 return label()->visible() ? nullptr | 265 gfx::Size ink_drop_size = size(); |
151 : InkDropHostView::CreateInkDropHighlight(); | 266 if (ShouldShowLabel()) { |
bruthig
2017/03/22 17:20:16
Would it be easier to position the |ink_drop_conta
spqchan
2017/03/24 17:58:03
Done.
| |
267 int padding_offset = -GetPostSeparatorPadding(); | |
268 ink_drop_center.Offset(padding_offset / 2, 0); | |
269 ink_drop_size.Enlarge(padding_offset, 0); | |
270 } | |
271 | |
272 return InkDropHostView::CreateDefaultInkDropHighlight(ink_drop_center, | |
273 ink_drop_size); | |
274 } | |
275 | |
276 std::unique_ptr<views::InkDropRipple> IconLabelBubbleView::CreateInkDropRipple() | |
277 const { | |
278 gfx::Point ink_drop_center = GetLocalBounds().CenterPoint(); | |
279 gfx::Size ink_drop_size = size(); | |
280 if (ink_drop_size.IsEmpty()) | |
281 return CreateDefaultInkDropRipple(ink_drop_center); | |
282 | |
283 if (ShouldShowLabel()) { | |
284 int padding_offset = -GetPostSeparatorPadding(); | |
285 ink_drop_center.Offset(padding_offset / 2, 0); | |
286 ink_drop_size.Enlarge(padding_offset, 0); | |
287 } | |
288 | |
289 return base::MakeUnique<views::FloodFillInkDropRipple>( | |
290 ink_drop_size, ink_drop_center, GetInkDropBaseColor(), | |
291 ink_drop_visible_opacity()); | |
292 } | |
293 | |
294 std::unique_ptr<views::InkDropMask> IconLabelBubbleView::CreateInkDropMask() | |
295 const { | |
296 return base::MakeUnique<views::RoundRectInkDropMask>(size(), gfx::Insets(), | |
297 kInkDropMaskRadius); | |
152 } | 298 } |
153 | 299 |
154 SkColor IconLabelBubbleView::GetInkDropBaseColor() const { | 300 SkColor IconLabelBubbleView::GetInkDropBaseColor() const { |
155 return color_utils::DeriveDefaultIconColor(GetTextColor()); | 301 return color_utils::DeriveDefaultIconColor(GetNativeTheme()->GetSystemColor( |
302 ui::NativeTheme::kColorId_TextfieldDefaultColor)); | |
303 } | |
304 | |
305 bool IconLabelBubbleView::IsTriggerableEvent(const ui::Event& event) { | |
306 return !IsBubbleShown(); | |
307 } | |
308 | |
309 void IconLabelBubbleView::OnClickCanceled(const ui::Event& event) { | |
310 // Overriden to prevent CustomButton from hiding the ink drop when the click | |
311 // is cancelled. The click might be cancelled because the bubble is still | |
312 // opened. In this case, the ink drop state should still be active. | |
313 } | |
314 | |
315 void IconLabelBubbleView::OnWidgetDestroying(views::Widget* widget) { | |
316 widget->RemoveObserver(this); | |
317 } | |
318 | |
319 void IconLabelBubbleView::OnWidgetVisibilityChanged(views::Widget* widget, | |
320 bool visible) { | |
321 // |widget| is a bubble that has just got shown / hidden. | |
322 if (!visible) | |
323 AnimateInkDrop(views::InkDropState::HIDDEN, nullptr /* event */); | |
156 } | 324 } |
157 | 325 |
158 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 326 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
159 return GetNativeTheme()->GetSystemColor( | 327 return GetNativeTheme()->GetSystemColor( |
160 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 328 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
161 } | 329 } |
162 | 330 |
163 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 331 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
164 gfx::Size size(image_->GetPreferredSize()); | 332 gfx::Size size(image_->GetPreferredSize()); |
165 const bool shrinking = IsShrinking(); | 333 const bool shrinking = IsShrinking(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 const views::Widget* widget = GetWidget(); | 377 const views::Widget* widget = GetWidget(); |
210 // There may be no widget in tests, and in ash there may be no compositor if | 378 // 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. | 379 // the native view of the Widget doesn't have a parent. |
212 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; | 380 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; |
213 return compositor ? compositor->device_scale_factor() : 1.0f; | 381 return compositor ? compositor->device_scale_factor() : 1.0f; |
214 } | 382 } |
215 | 383 |
216 const char* IconLabelBubbleView::GetClassName() const { | 384 const char* IconLabelBubbleView::GetClassName() const { |
217 return "IconLabelBubbleView"; | 385 return "IconLabelBubbleView"; |
218 } | 386 } |
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 bounds.set_width(bounds.width() + kSpaceBesideSeparator); | |
233 canvas->Draw1pxLine(gfx::PointF(bounds.top_right()), | |
234 gfx::PointF(bounds.bottom_right()), separator_color); | |
235 } | |
OLD | NEW |