| 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" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 else | 110 else |
| 111 bubble_trailing_padding -= space_shortage; | 111 bubble_trailing_padding -= space_shortage; |
| 112 } | 112 } |
| 113 image_->SetBounds(0, 0, image_width, height()); | 113 image_->SetBounds(0, 0, image_width, height()); |
| 114 | 114 |
| 115 // Compute the label bounds. The label gets whatever size is left over after | 115 // 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 | 116 // 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 | 117 // 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. | 118 // value to be, since it won't be visible. |
| 119 const int label_x = image_->bounds().right() + GetInternalSpacing(); | 119 const int label_x = image_->bounds().right() + GetInternalSpacing(); |
| 120 const int label_width = std::max( | 120 const int label_width = |
| 121 0, width() - label_x - bubble_trailing_padding - kSpaceBesideSeparator); | 121 std::max(0, width() - label_x - bubble_trailing_padding - |
| 122 kSpaceBesideSeparator - GetSeparatorLayoutWidth()); |
| 122 label_->SetBounds(label_x, 0, label_width, height()); | 123 label_->SetBounds(label_x, 0, label_width, height()); |
| 123 } | 124 } |
| 124 | 125 |
| 125 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 126 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 126 label_->GetAccessibleNodeData(node_data); | 127 label_->GetAccessibleNodeData(node_data); |
| 127 } | 128 } |
| 128 | 129 |
| 129 void IconLabelBubbleView::OnNativeThemeChanged( | 130 void IconLabelBubbleView::OnNativeThemeChanged( |
| 130 const ui::NativeTheme* native_theme) { | 131 const ui::NativeTheme* native_theme) { |
| 131 label_->SetEnabledColor(GetTextColor()); | 132 label_->SetEnabledColor(GetTextColor()); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 161 } | 162 } |
| 162 | 163 |
| 163 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 164 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
| 164 gfx::Size size(image_->GetPreferredSize()); | 165 gfx::Size size(image_->GetPreferredSize()); |
| 165 const bool shrinking = IsShrinking(); | 166 const bool shrinking = IsShrinking(); |
| 166 // Animation continues for the last few pixels even after the label is not | 167 // Animation continues for the last few pixels even after the label is not |
| 167 // visible in order to slide the icon into its final position. Therefore it | 168 // visible in order to slide the icon into its final position. Therefore it |
| 168 // is necessary to animate |total_width| even when the background is hidden | 169 // is necessary to animate |total_width| even when the background is hidden |
| 169 // as long as the animation is still shrinking. | 170 // as long as the animation is still shrinking. |
| 170 if (ShouldShowLabel() || shrinking) { | 171 if (ShouldShowLabel() || shrinking) { |
| 171 // On scale factors < 2, we reserve 1 DIP for the 1 px separator. For | |
| 172 // higher scale factors, we simply take the separator px out of the | |
| 173 // kSpaceBesideSeparator region before the separator, as that results in a | |
| 174 // width closer to the desired gap than if we added a whole DIP for the | |
| 175 // separator px. (For scale 2, the two methods have equal error: 1 px.) | |
| 176 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; | |
| 177 const int post_label_width = | 172 const int post_label_width = |
| 178 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); | 173 (kSpaceBesideSeparator + GetSeparatorLayoutWidth() + |
| 174 GetPostSeparatorPadding()); |
| 179 | 175 |
| 180 // |multiplier| grows from zero to one, stays equal to one and then shrinks | 176 // |multiplier| grows from zero to one, stays equal to one and then shrinks |
| 181 // to zero again. The view width should correspondingly grow from zero to | 177 // to zero again. The view width should correspondingly grow from zero to |
| 182 // fully showing both label and icon, stay there, then shrink to just large | 178 // fully showing both label and icon, stay there, then shrink to just large |
| 183 // enough to show the icon. We don't want to shrink all the way back to | 179 // enough to show the icon. We don't want to shrink all the way back to |
| 184 // zero, since this would mean the view would completely disappear and then | 180 // zero, since this would mean the view would completely disappear and then |
| 185 // pop back to an icon after the animation finishes. | 181 // pop back to an icon after the animation finishes. |
| 186 const int max_width = | 182 const int max_width = |
| 187 size.width() + GetInternalSpacing() + label_width + post_label_width; | 183 size.width() + GetInternalSpacing() + label_width + post_label_width; |
| 188 const int current_width = WidthMultiplier() * max_width; | 184 const int current_width = WidthMultiplier() * max_width; |
| 189 size.set_width(shrinking ? std::max(current_width, size.width()) | 185 size.set_width(shrinking ? std::max(current_width, size.width()) |
| 190 : current_width); | 186 : current_width); |
| 191 } | 187 } |
| 192 return size; | 188 return size; |
| 193 } | 189 } |
| 194 | 190 |
| 195 int IconLabelBubbleView::GetInternalSpacing() const { | 191 int IconLabelBubbleView::GetInternalSpacing() const { |
| 196 return image_->GetPreferredSize().IsEmpty() | 192 return image_->GetPreferredSize().IsEmpty() |
| 197 ? 0 | 193 ? 0 |
| 198 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); | 194 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); |
| 199 } | 195 } |
| 200 | 196 |
| 201 int IconLabelBubbleView::GetPostSeparatorPadding() const { | 197 int IconLabelBubbleView::GetPostSeparatorPadding() const { |
| 202 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us. | 198 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us. |
| 203 return kSpaceBesideSeparator - | 199 return kSpaceBesideSeparator - |
| 204 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) - | 200 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) - |
| 205 next_element_interior_padding_; | 201 next_element_interior_padding_; |
| 206 } | 202 } |
| 207 | 203 |
| 204 int IconLabelBubbleView::GetSeparatorLayoutWidth() const { |
| 205 // On scale factors < 2, we reserve 1 DIP for the 1 px separator. For |
| 206 // higher scale factors, we simply take the separator px out of the |
| 207 // kSpaceBesideSeparator region before the separator, as that results in a |
| 208 // width closer to the desired gap than if we added a whole DIP for the |
| 209 // separator px. (For scale 2, the two methods have equal error: 1 px.) |
| 210 return (GetScaleFactor() >= 2) ? 0 : 1; |
| 211 } |
| 212 |
| 208 float IconLabelBubbleView::GetScaleFactor() const { | 213 float IconLabelBubbleView::GetScaleFactor() const { |
| 209 const views::Widget* widget = GetWidget(); | 214 const views::Widget* widget = GetWidget(); |
| 210 // There may be no widget in tests, and in ash there may be no compositor if | 215 // 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. | 216 // the native view of the Widget doesn't have a parent. |
| 212 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; | 217 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; |
| 213 return compositor ? compositor->device_scale_factor() : 1.0f; | 218 return compositor ? compositor->device_scale_factor() : 1.0f; |
| 214 } | 219 } |
| 215 | 220 |
| 216 const char* IconLabelBubbleView::GetClassName() const { | 221 const char* IconLabelBubbleView::GetClassName() const { |
| 217 return "IconLabelBubbleView"; | 222 return "IconLabelBubbleView"; |
| 218 } | 223 } |
| 219 | 224 |
| 220 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 225 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 221 if (!ShouldShowLabel()) | 226 if (!ShouldShowLabel()) |
| 222 return; | 227 return; |
| 223 | 228 |
| 224 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( | 229 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( |
| 225 ui::NativeTheme::kColorId_TextfieldDefaultColor); | 230 ui::NativeTheme::kColorId_TextfieldDefaultColor); |
| 226 const SkColor separator_color = SkColorSetA( | 231 const SkColor separator_color = SkColorSetA( |
| 227 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | 232 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); |
| 228 | 233 |
| 229 gfx::Rect bounds(label_->bounds()); | 234 gfx::Rect bounds(label_->bounds()); |
| 230 const int kSeparatorHeight = 16; | 235 const int kSeparatorHeight = 16; |
| 231 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2); | 236 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2); |
| 232 bounds.set_width(bounds.width() + kSpaceBesideSeparator); | 237 bounds.set_width(bounds.width() + kSpaceBesideSeparator); |
| 233 canvas->Draw1pxLine(gfx::PointF(bounds.top_right()), | 238 canvas->Draw1pxLine(gfx::PointF(bounds.top_right()), |
| 234 gfx::PointF(bounds.bottom_right()), separator_color); | 239 gfx::PointF(bounds.bottom_right()), separator_color); |
| 235 } | 240 } |
| OLD | NEW |