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 "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/ui/views/layout_constants.h" | 8 #include "chrome/browser/ui/views/layout_constants.h" |
9 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" | 9 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" |
10 #include "ui/base/resource/material_design/material_design_controller.h" | 10 #include "ui/base/resource/material_design/material_design_controller.h" |
11 #include "ui/base/resource/resource_bundle.h" | 11 #include "ui/base/resource/resource_bundle.h" |
12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
13 #include "ui/gfx/color_utils.h" | 13 #include "ui/gfx/color_utils.h" |
| 14 #include "ui/native_theme/native_theme.h" |
14 #include "ui/views/controls/image_view.h" | 15 #include "ui/views/controls/image_view.h" |
15 #include "ui/views/painter.h" | 16 #include "ui/views/painter.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 SkColor CalculateImageColor(gfx::ImageSkia* image) { | 20 SkColor CalculateImageColor(gfx::ImageSkia* image) { |
20 // We grab the color of the middle pixel of the image, which we treat as | 21 // We grab the color of the middle pixel of the image, which we treat as |
21 // the representative color of the entire image (reasonable, given the current | 22 // the representative color of the entire image (reasonable, given the current |
22 // appearance of these assets). | 23 // appearance of these assets). |
23 const SkBitmap& bitmap(image->GetRepresentation(1.0f).sk_bitmap()); | 24 const SkBitmap& bitmap(image->GetRepresentation(1.0f).sk_bitmap()); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 height()); | 116 height()); |
116 } | 117 } |
117 | 118 |
118 void IconLabelBubbleView::OnNativeThemeChanged( | 119 void IconLabelBubbleView::OnNativeThemeChanged( |
119 const ui::NativeTheme* native_theme) { | 120 const ui::NativeTheme* native_theme) { |
120 if (!ui::MaterialDesignController::IsModeMaterial()) | 121 if (!ui::MaterialDesignController::IsModeMaterial()) |
121 return; | 122 return; |
122 | 123 |
123 label_->SetEnabledColor(GetTextColor()); | 124 label_->SetEnabledColor(GetTextColor()); |
124 | 125 |
125 bool inverted = | 126 bool inverted = color_utils::IsDark(GetParentBackgroundColor()); |
126 color_utils::GetLuminanceForColor(parent_background_color_) < 128; | |
127 SkColor border_color = inverted ? SK_ColorWHITE : GetBorderColor(); | 127 SkColor border_color = inverted ? SK_ColorWHITE : GetBorderColor(); |
128 SkColor background_color = | 128 SkColor background_color = |
129 inverted ? SK_ColorWHITE : SkColorSetA(border_color, 0x13); | 129 inverted ? SK_ColorWHITE : SkColorSetA(border_color, 0x13); |
130 set_background( | 130 set_background( |
131 new BackgroundWith1PxBorder(background_color, border_color, false)); | 131 new BackgroundWith1PxBorder(background_color, border_color, false)); |
132 SetLabelBackgroundColor(background_color); | 132 SetLabelBackgroundColor(background_color); |
133 } | 133 } |
134 | 134 |
| 135 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
| 136 return ui::MaterialDesignController::IsModeMaterial() |
| 137 ? GetNativeTheme()->GetSystemColor( |
| 138 ui::NativeTheme::kColorId_TextfieldDefaultBackground) |
| 139 : parent_background_color_; |
| 140 } |
| 141 |
135 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { | 142 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int width) const { |
136 gfx::Size size(image_->GetPreferredSize()); | 143 gfx::Size size(image_->GetPreferredSize()); |
137 if (ShouldShowBackground()) { | 144 if (ShouldShowBackground()) { |
138 const int non_label_width = GetBubbleOuterPadding(true) + | 145 const int non_label_width = GetBubbleOuterPadding(true) + |
139 GetImageAndPaddingWidth() + | 146 GetImageAndPaddingWidth() + |
140 GetBubbleOuterPadding(false); | 147 GetBubbleOuterPadding(false); |
141 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); | 148 size = gfx::Size(WidthMultiplier() * (width + non_label_width), 0); |
142 if (!ui::MaterialDesignController::IsModeMaterial()) | 149 if (!ui::MaterialDesignController::IsModeMaterial()) |
143 size.SetToMax(background_painter_->GetMinimumSize()); | 150 size.SetToMax(background_painter_->GetMinimumSize()); |
144 } | 151 } |
(...skipping 13 matching lines...) Expand all Loading... |
158 int IconLabelBubbleView::GetBubbleOuterPaddingMd(bool leading) const { | 165 int IconLabelBubbleView::GetBubbleOuterPaddingMd(bool leading) const { |
159 // When the image is empty, leading and trailing padding are equal. | 166 // When the image is empty, leading and trailing padding are equal. |
160 if (image_->GetPreferredSize().IsEmpty() || !leading) | 167 if (image_->GetPreferredSize().IsEmpty() || !leading) |
161 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); | 168 return GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING); |
162 | 169 |
163 // Leading padding is 2dp. | 170 // Leading padding is 2dp. |
164 return 2; | 171 return 2; |
165 } | 172 } |
166 | 173 |
167 void IconLabelBubbleView::SetLabelBackgroundColor( | 174 void IconLabelBubbleView::SetLabelBackgroundColor( |
168 SkColor background_image_color) { | 175 SkColor chip_background_color) { |
169 // The background images are painted atop |parent_background_color_|. | 176 // The background images are painted atop |parent_background_color_|. |
170 // Alpha-blend |background_image_color| with |parent_background_color_| to | 177 // Alpha-blend |chip_background_color| with |parent_background_color_| to |
171 // determine the actual color the label text will sit atop. | 178 // determine the actual color the label text will sit atop. |
172 // Tricky bit: We alpha blend an opaque version of |background_image_color| | 179 // Tricky bit: We alpha blend an opaque version of |chip_background_color| |
173 // against |parent_background_color_| using the original image grid color's | 180 // against |parent_background_color_| using the original image grid color's |
174 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged | 181 // alpha. This is because AlphaBlend(a, b, 255) always returns |a| unchanged |
175 // even if |a| is a color with non-255 alpha. | 182 // even if |a| is a color with non-255 alpha. |
176 label_->SetBackgroundColor(color_utils::AlphaBlend( | 183 label_->SetBackgroundColor(color_utils::AlphaBlend( |
177 SkColorSetA(background_image_color, 255), parent_background_color_, | 184 SkColorSetA(chip_background_color, 255), GetParentBackgroundColor(), |
178 SkColorGetA(background_image_color))); | 185 SkColorGetA(chip_background_color))); |
179 } | 186 } |
180 | 187 |
181 const char* IconLabelBubbleView::GetClassName() const { | 188 const char* IconLabelBubbleView::GetClassName() const { |
182 return "IconLabelBubbleView"; | 189 return "IconLabelBubbleView"; |
183 } | 190 } |
184 | 191 |
185 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 192 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
186 if (!ShouldShowBackground()) | 193 if (!ShouldShowBackground()) |
187 return; | 194 return; |
188 if (background_painter_) | 195 if (background_painter_) |
189 background_painter_->Paint(canvas, size()); | 196 background_painter_->Paint(canvas, size()); |
190 if (background()) | 197 if (background()) |
191 background()->Paint(canvas, this); | 198 background()->Paint(canvas, this); |
192 } | 199 } |
OLD | NEW |