Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: chrome/browser/ui/extensions/icon_with_badge_image_source.cc

Issue 2401363005: Remove sundry IsModeMaterial checks. (Closed)
Patch Set: remove unused fn Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/extensions/icon_with_badge_image_source.h" 5 #include "chrome/browser/ui/extensions/icon_with_badge_image_source.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h" 13 #include "build/build_config.h"
14 #include "chrome/browser/extensions/extension_action.h" 14 #include "chrome/browser/extensions/extension_action.h"
15 #include "chrome/grit/theme_resources.h" 15 #include "chrome/grit/theme_resources.h"
16 #include "third_party/skia/include/core/SkPaint.h" 16 #include "third_party/skia/include/core/SkPaint.h"
17 #include "third_party/skia/include/core/SkRefCnt.h" 17 #include "third_party/skia/include/core/SkRefCnt.h"
18 #include "third_party/skia/include/core/SkTypeface.h" 18 #include "third_party/skia/include/core/SkTypeface.h"
Peter Kasting 2016/10/11 22:49:18 Looks like this #include can disappear too? Pleas
Evan Stade 2016/10/12 00:13:07 Done.
19 #include "ui/base/material_design/material_design_controller.h"
20 #include "ui/base/resource/resource_bundle.h" 19 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/canvas.h" 20 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/color_palette.h" 21 #include "ui/gfx/color_palette.h"
23 #include "ui/gfx/font.h" 22 #include "ui/gfx/font.h"
24 #include "ui/gfx/geometry/rect.h" 23 #include "ui/gfx/geometry/rect.h"
25 #include "ui/gfx/geometry/size.h" 24 #include "ui/gfx/geometry/size.h"
26 #include "ui/gfx/image/image_skia_operations.h" 25 #include "ui/gfx/image/image_skia_operations.h"
27 #include "ui/resources/grit/ui_resources.h" 26 #include "ui/resources/grit/ui_resources.h"
28 27
29 namespace { 28 namespace {
30 29
31 // Different platforms need slightly different constants to look good.
32 // TODO(devlin): Comb through these and see if they are all still needed/
33 // appropriate.
34 #if defined(OS_WIN)
35 const float kTextSize = 10;
36 // The padding between the top of the badge and the top of the text.
37 const int kTopTextPadding = -1;
38 #elif defined(OS_MACOSX)
39 const float kTextSize = 9.0;
40 const int kTopTextPadding = 0;
41 #elif defined(OS_CHROMEOS)
42 const float kTextSize = 8.0;
43 const int kTopTextPadding = 1;
44 #elif defined(OS_POSIX)
45 const float kTextSize = 9.0;
46 const int kTopTextPadding = 0;
47 #endif
48
49 const int kPadding = 2; 30 const int kPadding = 2;
50 const int kBadgeHeight = 11; 31 const int kBadgeHeight = 11;
51 const int kMaxTextWidth = 23; 32 const int kMaxTextWidth = 23;
52 33
53 // The minimum width for center-aligning the badge. 34 // The minimum width for center-aligning the badge.
54 const int kCenterAlignThreshold = 20; 35 const int kCenterAlignThreshold = 20;
55 36
56 // Helper routine that returns a singleton SkPaint object configured for
57 // rendering badge overlay text (correct font, typeface, etc).
58 SkPaint* GetBadgeTextPaintSingleton() {
59 #if defined(OS_MACOSX)
60 const char kPreferredTypeface[] = "Helvetica Bold";
61 #else
62 const char kPreferredTypeface[] = "Arial";
63 #endif
64
65 static SkPaint* text_paint = NULL;
66 if (!text_paint) {
67 text_paint = new SkPaint;
68 text_paint->setAntiAlias(true);
69 text_paint->setTextAlign(SkPaint::kLeft_Align);
70
71 sk_sp<SkTypeface> typeface(
72 SkTypeface::MakeFromName(kPreferredTypeface,
73 SkFontStyle::FromOldStyle(SkTypeface::kBold)));
74 // Skia doesn't do any font fallback---if the user is missing the font then
75 // typeface will be NULL. If we don't do manual fallback then we'll crash.
76 if (typeface) {
77 text_paint->setFakeBoldText(true);
78 } else {
79 // Fall back to the system font. We don't bold it because we aren't sure
80 // how it will look.
81 // For the most part this code path will only be hit on Linux systems
82 // that don't have Arial.
83 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
84 const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont);
85 typeface = SkTypeface::MakeFromName(base_font.GetFontName().c_str(),
86 SkFontStyle());
87 DCHECK(typeface);
88 }
89
90 text_paint->setTypeface(std::move(typeface));
91 }
92 return text_paint;
93 }
94
95 gfx::ImageSkiaRep ScaleImageSkiaRep(const gfx::ImageSkiaRep& rep, 37 gfx::ImageSkiaRep ScaleImageSkiaRep(const gfx::ImageSkiaRep& rep,
96 int target_width_dp, 38 int target_width_dp,
97 float target_scale) { 39 float target_scale) {
98 int width_px = target_width_dp * target_scale; 40 int width_px = target_width_dp * target_scale;
99 return gfx::ImageSkiaRep( 41 return gfx::ImageSkiaRep(
100 skia::ImageOperations::Resize(rep.sk_bitmap(), 42 skia::ImageOperations::Resize(rep.sk_bitmap(),
101 skia::ImageOperations::RESIZE_BEST, 43 skia::ImageOperations::RESIZE_BEST,
102 width_px, width_px), 44 width_px, width_px),
103 target_scale); 45 target_scale);
104 } 46 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 101
160 // Paints badge with specified parameters to |canvas|. 102 // Paints badge with specified parameters to |canvas|.
161 void IconWithBadgeImageSource::PaintBadge(gfx::Canvas* canvas) { 103 void IconWithBadgeImageSource::PaintBadge(gfx::Canvas* canvas) {
162 if (!badge_ || badge_->text.empty()) 104 if (!badge_ || badge_->text.empty())
163 return; 105 return;
164 106
165 SkColor text_color = SkColorGetA(badge_->text_color) == SK_AlphaTRANSPARENT 107 SkColor text_color = SkColorGetA(badge_->text_color) == SK_AlphaTRANSPARENT
166 ? SK_ColorWHITE 108 ? SK_ColorWHITE
167 : badge_->text_color; 109 : badge_->text_color;
168 110
169 SkColor background_color = ui::MaterialDesignController::IsModeMaterial() 111 SkColor background_color =
170 ? gfx::kGoogleBlue500 112 SkColorGetA(badge_->background_color) == SK_AlphaTRANSPARENT
171 : SkColorSetRGB(218, 0, 24); 113 ? gfx::kGoogleBlue500
172 if (SkColorGetA(badge_->background_color) != SK_AlphaTRANSPARENT) 114 // Make sure the background color is opaque. See
173 background_color = badge_->background_color; 115 // http://crbug.com/619499
Peter Kasting 2016/10/11 22:49:18 Nit: I'd move this comment above the whole stateme
Evan Stade 2016/10/12 00:13:07 Done.
174 // Make sure the background color is opaque. See http://crbug.com/619499 116 : SkColorSetA(badge_->background_color, SK_AlphaOPAQUE);
175 if (ui::MaterialDesignController::IsModeMaterial())
176 background_color = SkColorSetA(background_color, SK_AlphaOPAQUE);
177 117
178 canvas->Save();
179
180 SkPaint* text_paint = nullptr;
181 int text_width = 0;
182 ResourceBundle* rb = &ResourceBundle::GetSharedInstance(); 118 ResourceBundle* rb = &ResourceBundle::GetSharedInstance();
183 gfx::FontList base_font = rb->GetFontList(ResourceBundle::BaseFont) 119 gfx::FontList base_font = rb->GetFontList(ResourceBundle::BaseFont)
184 .DeriveWithHeightUpperBound(kBadgeHeight); 120 .DeriveWithHeightUpperBound(kBadgeHeight);
185 base::string16 utf16_text = base::UTF8ToUTF16(badge_->text); 121 base::string16 utf16_text = base::UTF8ToUTF16(badge_->text);
186 122
187 // See if we can squeeze a slightly larger font into the badge given the 123 // See if we can squeeze a slightly larger font into the badge given the
188 // actual string that is to be displayed. 124 // actual string that is to be displayed.
189 const int kMaxIncrementAttempts = 5; 125 const int kMaxIncrementAttempts = 5;
190 for (size_t i = 0; i < kMaxIncrementAttempts; ++i) { 126 for (size_t i = 0; i < kMaxIncrementAttempts; ++i) {
191 int w = 0; 127 int w = 0;
192 int h = 0; 128 int h = 0;
193 gfx::FontList bigger_font = 129 gfx::FontList bigger_font =
194 base_font.Derive(1, 0, gfx::Font::Weight::NORMAL); 130 base_font.Derive(1, 0, gfx::Font::Weight::NORMAL);
195 gfx::Canvas::SizeStringInt(utf16_text, bigger_font, &w, &h, 0, 131 gfx::Canvas::SizeStringInt(utf16_text, bigger_font, &w, &h, 0,
196 gfx::Canvas::NO_ELLIPSIS); 132 gfx::Canvas::NO_ELLIPSIS);
197 if (h > kBadgeHeight) 133 if (h > kBadgeHeight)
198 break; 134 break;
199 base_font = bigger_font; 135 base_font = bigger_font;
200 } 136 }
201 137
202 if (ui::MaterialDesignController::IsModeMaterial()) { 138 const int text_width =
203 text_width =
204 std::min(kMaxTextWidth, canvas->GetStringWidth(utf16_text, base_font)); 139 std::min(kMaxTextWidth, canvas->GetStringWidth(utf16_text, base_font));
Peter Kasting 2016/10/11 22:49:18 Nit: Should this still have a blank line after?
Evan Stade 2016/10/12 00:13:07 I dunno, seems fine this way? In my view of the wo
205 } else {
206 text_paint = GetBadgeTextPaintSingleton();
207 text_paint->setColor(text_color);
208 float scale = canvas->image_scale();
209
210 // Calculate text width. Font width may not be linear with respect to the
211 // scale factor (e.g. when hinting is applied), so we need to use the font
212 // size that canvas actually uses when drawing a text.
213 text_paint->setTextSize(SkFloatToScalar(kTextSize) * scale);
214 SkScalar sk_text_width_in_pixel =
215 text_paint->measureText(badge_->text.c_str(), badge_->text.size());
216 text_paint->setTextSize(SkFloatToScalar(kTextSize));
217
218 // We clamp the width to a max size. SkPaint::measureText returns the width
219 // in pixel (as a result of scale multiplier), so convert
220 // sk_text_width_in_pixel back to DIP (density independent pixel) first.
221 text_width = std::min(
222 kMaxTextWidth, static_cast<int>(std::ceil(
223 SkScalarToFloat(sk_text_width_in_pixel) / scale)));
224 }
225
226 // Calculate badge size. It is clamped to a min width just because it looks 140 // Calculate badge size. It is clamped to a min width just because it looks
227 // silly if it is too skinny. 141 // silly if it is too skinny.
228 int badge_width = text_width + kPadding * 2; 142 int badge_width = text_width + kPadding * 2;
229 // Force the pixel width of badge to be either odd (if the icon width is odd) 143 // Force the pixel width of badge to be either odd (if the icon width is odd)
230 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. 144 // or even otherwise. If there is a mismatch you get http://crbug.com/26400.
231 if (size().width() != 0 && (badge_width % 2 != size().width() % 2)) 145 if (size().width() != 0 && (badge_width % 2 != size().width() % 2))
232 badge_width += 1; 146 badge_width += 1;
233 badge_width = std::max(kBadgeHeight, badge_width); 147 badge_width = std::max(kBadgeHeight, badge_width);
234 148
235 // Calculate the badge background rect. It is usually right-aligned, but it 149 // Calculate the badge background rect. It is usually right-aligned, but it
236 // can also be center-aligned if it is large. 150 // can also be center-aligned if it is large.
237 gfx::Rect rect(badge_width >= kCenterAlignThreshold 151 gfx::Rect rect(badge_width >= kCenterAlignThreshold
238 ? (size().width() - badge_width) / 2 152 ? (size().width() - badge_width) / 2
239 : size().width() - badge_width, 153 : size().width() - badge_width,
240 size().height() - kBadgeHeight, badge_width, kBadgeHeight); 154 size().height() - kBadgeHeight, badge_width, kBadgeHeight);
241 SkPaint rect_paint; 155 SkPaint rect_paint;
242 rect_paint.setStyle(SkPaint::kFill_Style); 156 rect_paint.setStyle(SkPaint::kFill_Style);
243 rect_paint.setAntiAlias(true); 157 rect_paint.setAntiAlias(true);
244 rect_paint.setColor(background_color); 158 rect_paint.setColor(background_color);
245 159
246 if (ui::MaterialDesignController::IsModeMaterial()) { 160 // Clear part of the background icon.
247 // Clear part of the background icon. 161 gfx::Rect cutout_rect(rect);
248 gfx::Rect cutout_rect(rect); 162 cutout_rect.Inset(-1, -1);
249 cutout_rect.Inset(-1, -1); 163 SkPaint cutout_paint = rect_paint;
250 SkPaint cutout_paint = rect_paint; 164 cutout_paint.setXfermodeMode(SkXfermode::kClear_Mode);
251 cutout_paint.setXfermodeMode(SkXfermode::kClear_Mode); 165 canvas->DrawRoundRect(cutout_rect, 2, cutout_paint);
252 canvas->DrawRoundRect(cutout_rect, 2, cutout_paint);
253 166
254 // Paint the backdrop. 167 // Paint the backdrop.
255 canvas->DrawRoundRect(rect, 1, rect_paint); 168 canvas->DrawRoundRect(rect, 1, rect_paint);
256 169
257 // Paint the text. 170 // Paint the text.
258 rect.Inset(std::max(kPadding, (rect.width() - text_width) / 2), 171 rect.Inset(std::max(kPadding, (rect.width() - text_width) / 2),
259 kBadgeHeight - base_font.GetHeight(), kPadding, 0); 172 kBadgeHeight - base_font.GetHeight(), kPadding, 0);
260 canvas->DrawStringRect(utf16_text, base_font, text_color, rect); 173 canvas->DrawStringRect(utf16_text, base_font, text_color, rect);
261 } else {
262 // Paint the backdrop.
263 canvas->DrawRoundRect(rect, 2, rect_paint);
264
265 // Overlay the gradient. It is stretchy, so we do this in three parts.
266 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
267 gfx::ImageSkia* gradient_left =
268 rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_LEFT);
269 gfx::ImageSkia* gradient_right =
270 rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_RIGHT);
271 gfx::ImageSkia* gradient_center =
272 rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_CENTER);
273
274 canvas->DrawImageInt(*gradient_left, rect.x(), rect.y());
275 canvas->TileImageInt(
276 *gradient_center, rect.x() + gradient_left->width(), rect.y(),
277 rect.width() - gradient_left->width() - gradient_right->width(),
278 rect.height());
279 canvas->DrawImageInt(*gradient_right,
280 rect.right() - gradient_right->width(), rect.y());
281
282 // Finally, draw the text centered within the badge. We set a clip in case
283 // the text was too large.
284 rect.Inset(kPadding, 0);
285 canvas->ClipRect(rect);
286 canvas->sk_canvas()->drawText(
287 badge_->text.c_str(), badge_->text.size(),
288 SkFloatToScalar(rect.x() +
289 static_cast<float>(rect.width() - text_width) / 2),
290 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), *text_paint);
291 }
292 canvas->Restore();
293 } 174 }
294 175
295 void IconWithBadgeImageSource::PaintPageActionDecoration(gfx::Canvas* canvas) { 176 void IconWithBadgeImageSource::PaintPageActionDecoration(gfx::Canvas* canvas) {
296 static const SkColor decoration_color = SkColorSetARGB(255, 70, 142, 226); 177 static const SkColor decoration_color = SkColorSetARGB(255, 70, 142, 226);
297 178
298 int major_radius = std::ceil(size().width() / 5.0); 179 int major_radius = std::ceil(size().width() / 5.0);
299 int minor_radius = std::ceil(major_radius / 2.0); 180 int minor_radius = std::ceil(major_radius / 2.0);
300 gfx::Point center_point(major_radius + 1, size().height() - (major_radius)-1); 181 gfx::Point center_point(major_radius + 1, size().height() - (major_radius)-1);
301 SkPaint paint; 182 SkPaint paint;
302 paint.setAntiAlias(true); 183 paint.setAntiAlias(true);
303 paint.setStyle(SkPaint::kFill_Style); 184 paint.setStyle(SkPaint::kFill_Style);
304 paint.setColor(SK_ColorTRANSPARENT); 185 paint.setColor(SK_ColorTRANSPARENT);
305 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 186 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
306 canvas->DrawCircle(center_point, major_radius, paint); 187 canvas->DrawCircle(center_point, major_radius, paint);
307 paint.setColor(decoration_color); 188 paint.setColor(decoration_color);
308 canvas->DrawCircle(center_point, minor_radius, paint); 189 canvas->DrawCircle(center_point, minor_radius, paint);
309 } 190 }
310 191
311 void IconWithBadgeImageSource::PaintBlockedActionDecoration( 192 void IconWithBadgeImageSource::PaintBlockedActionDecoration(
312 gfx::Canvas* canvas) { 193 gfx::Canvas* canvas) {
313 canvas->Save(); 194 canvas->Save();
314 gfx::ImageSkia img = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 195 gfx::ImageSkia img = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
315 IDR_BLOCKED_EXTENSION_SCRIPT); 196 IDR_BLOCKED_EXTENSION_SCRIPT);
316 canvas->DrawImageInt(img, size().width() - img.width(), 0); 197 canvas->DrawImageInt(img, size().width() - img.width(), 0);
317 canvas->Restore(); 198 canvas->Restore();
318 } 199 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/libgtk2ui/gtk2_ui.h » ('j') | chrome/browser/ui/views/tabs/tab_strip_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698