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/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 "third_party/skia/include/core/SkPaint.h" | 15 #include "third_party/skia/include/core/SkPaint.h" |
15 #include "third_party/skia/include/core/SkTypeface.h" | 16 #include "third_party/skia/include/core/SkTypeface.h" |
16 #include "ui/base/material_design/material_design_controller.h" | 17 #include "ui/base/material_design/material_design_controller.h" |
17 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
18 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
19 #include "ui/gfx/color_palette.h" | 20 #include "ui/gfx/color_palette.h" |
20 #include "ui/gfx/font.h" | 21 #include "ui/gfx/font.h" |
21 #include "ui/gfx/geometry/rect.h" | 22 #include "ui/gfx/geometry/rect.h" |
22 #include "ui/gfx/geometry/size.h" | 23 #include "ui/gfx/geometry/size.h" |
23 #include "ui/gfx/image/image_skia_operations.h" | 24 #include "ui/gfx/image/image_skia_operations.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 DCHECK(typeface); | 84 DCHECK(typeface); |
84 } | 85 } |
85 | 86 |
86 text_paint->setTypeface(typeface.get()); | 87 text_paint->setTypeface(typeface.get()); |
87 // |text_paint| adds its own ref. Release the ref from CreateFontName. | 88 // |text_paint| adds its own ref. Release the ref from CreateFontName. |
88 } | 89 } |
89 return text_paint; | 90 return text_paint; |
90 } | 91 } |
91 | 92 |
92 gfx::ImageSkiaRep ScaleImageSkiaRep(const gfx::ImageSkiaRep& rep, | 93 gfx::ImageSkiaRep ScaleImageSkiaRep(const gfx::ImageSkiaRep& rep, |
93 int target_width, | 94 int target_width_dp, |
94 float target_scale) { | 95 float target_scale) { |
| 96 int width_px = target_width_dp * target_scale; |
95 return gfx::ImageSkiaRep( | 97 return gfx::ImageSkiaRep( |
96 skia::ImageOperations::Resize(rep.sk_bitmap(), | 98 skia::ImageOperations::Resize(rep.sk_bitmap(), |
97 skia::ImageOperations::RESIZE_BEST, | 99 skia::ImageOperations::RESIZE_BEST, |
98 target_width, target_width), | 100 width_px, width_px), |
99 target_scale); | 101 target_scale); |
100 } | 102 } |
101 | 103 |
102 } // namespace | 104 } // namespace |
103 | 105 |
104 IconWithBadgeImageSource::Badge::Badge(const std::string& text, | 106 IconWithBadgeImageSource::Badge::Badge(const std::string& text, |
105 SkColor text_color, | 107 SkColor text_color, |
106 SkColor background_color) | 108 SkColor background_color) |
107 : text(text), text_color(text_color), background_color(background_color) {} | 109 : text(text), text_color(text_color), background_color(background_color) {} |
108 | 110 |
(...skipping 14 matching lines...) Expand all Loading... |
123 badge_ = std::move(badge); | 125 badge_ = std::move(badge); |
124 } | 126 } |
125 | 127 |
126 void IconWithBadgeImageSource::Draw(gfx::Canvas* canvas) { | 128 void IconWithBadgeImageSource::Draw(gfx::Canvas* canvas) { |
127 if (icon_.IsEmpty()) | 129 if (icon_.IsEmpty()) |
128 return; | 130 return; |
129 | 131 |
130 gfx::ImageSkia skia = icon_.AsImageSkia(); | 132 gfx::ImageSkia skia = icon_.AsImageSkia(); |
131 gfx::ImageSkiaRep rep = skia.GetRepresentation(canvas->image_scale()); | 133 gfx::ImageSkiaRep rep = skia.GetRepresentation(canvas->image_scale()); |
132 if (rep.scale() != canvas->image_scale()) { | 134 if (rep.scale() != canvas->image_scale()) { |
133 skia.AddRepresentation( | 135 skia.AddRepresentation(ScaleImageSkiaRep( |
134 ScaleImageSkiaRep(rep, skia.width(), canvas->image_scale())); | 136 rep, ExtensionAction::ActionIconSize(), canvas->image_scale())); |
135 } | 137 } |
136 if (grayscale_) | 138 if (grayscale_) |
137 skia = gfx::ImageSkiaOperations::CreateHSLShiftedImage(skia, {-1, 0, 0.6}); | 139 skia = gfx::ImageSkiaOperations::CreateHSLShiftedImage(skia, {-1, 0, 0.6}); |
138 | 140 |
139 int x_offset = std::floor((size().width() - icon_.Width()) / 2.0); | 141 int x_offset = |
140 int y_offset = std::floor((size().height() - icon_.Height()) / 2.0); | 142 std::floor((size().width() - ExtensionAction::ActionIconSize()) / 2.0); |
| 143 int y_offset = |
| 144 std::floor((size().height() - ExtensionAction::ActionIconSize()) / 2.0); |
141 canvas->DrawImageInt(skia, x_offset, y_offset); | 145 canvas->DrawImageInt(skia, x_offset, y_offset); |
142 | 146 |
143 // Draw a badge on the provided browser action icon's canvas. | 147 // Draw a badge on the provided browser action icon's canvas. |
144 PaintBadge(canvas); | 148 PaintBadge(canvas); |
145 | 149 |
146 if (paint_decoration_) | 150 if (paint_decoration_) |
147 PaintDecoration(canvas); | 151 PaintDecoration(canvas); |
148 } | 152 } |
149 | 153 |
150 // Paints badge with specified parameters to |canvas|. | 154 // Paints badge with specified parameters to |canvas|. |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 gfx::Point center_point(major_radius + 1, size().height() - (major_radius)-1); | 275 gfx::Point center_point(major_radius + 1, size().height() - (major_radius)-1); |
272 SkPaint paint; | 276 SkPaint paint; |
273 paint.setAntiAlias(true); | 277 paint.setAntiAlias(true); |
274 paint.setStyle(SkPaint::kFill_Style); | 278 paint.setStyle(SkPaint::kFill_Style); |
275 paint.setColor(SK_ColorTRANSPARENT); | 279 paint.setColor(SK_ColorTRANSPARENT); |
276 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 280 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
277 canvas->DrawCircle(center_point, major_radius, paint); | 281 canvas->DrawCircle(center_point, major_radius, paint); |
278 paint.setColor(decoration_color); | 282 paint.setColor(decoration_color); |
279 canvas->DrawCircle(center_point, minor_radius, paint); | 283 canvas->DrawCircle(center_point, minor_radius, paint); |
280 } | 284 } |
OLD | NEW |