| 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 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 base_font.GetFontName().c_str(), SkTypeface::kNormal)); | 82 base_font.GetFontName().c_str(), SkTypeface::kNormal)); |
| 83 DCHECK(typeface); | 83 DCHECK(typeface); |
| 84 } | 84 } |
| 85 | 85 |
| 86 text_paint->setTypeface(typeface.get()); | 86 text_paint->setTypeface(typeface.get()); |
| 87 // |text_paint| adds its own ref. Release the ref from CreateFontName. | 87 // |text_paint| adds its own ref. Release the ref from CreateFontName. |
| 88 } | 88 } |
| 89 return text_paint; | 89 return text_paint; |
| 90 } | 90 } |
| 91 | 91 |
| 92 gfx::ImageSkiaRep ScaleImageSkiaRep(const gfx::ImageSkiaRep& rep, |
| 93 float target_scale) { |
| 94 gfx::Size scaled_size = |
| 95 gfx::ScaleToCeiledSize(rep.pixel_size(), target_scale / rep.scale()); |
| 96 return gfx::ImageSkiaRep(skia::ImageOperations::Resize( |
| 97 rep.sk_bitmap(), |
| 98 skia::ImageOperations::RESIZE_BEST, |
| 99 scaled_size.width(), |
| 100 scaled_size.height()), target_scale); |
| 101 } |
| 102 |
| 92 } // namespace | 103 } // namespace |
| 93 | 104 |
| 94 IconWithBadgeImageSource::Badge::Badge(const std::string& text, | 105 IconWithBadgeImageSource::Badge::Badge(const std::string& text, |
| 95 SkColor text_color, | 106 SkColor text_color, |
| 96 SkColor background_color) | 107 SkColor background_color) |
| 97 : text(text), text_color(text_color), background_color(background_color) {} | 108 : text(text), text_color(text_color), background_color(background_color) {} |
| 98 | 109 |
| 99 IconWithBadgeImageSource::Badge::~Badge() {} | 110 IconWithBadgeImageSource::Badge::~Badge() {} |
| 100 | 111 |
| 101 IconWithBadgeImageSource::IconWithBadgeImageSource(const gfx::Size& size) | 112 IconWithBadgeImageSource::IconWithBadgeImageSource(const gfx::Size& size) |
| 102 : gfx::CanvasImageSource(size, false), | 113 : gfx::CanvasImageSource(size, false), |
| 103 grayscale_(false), | 114 grayscale_(false), |
| 104 paint_decoration_(false) {} | 115 paint_decoration_(false) {} |
| 105 | 116 |
| 106 IconWithBadgeImageSource::~IconWithBadgeImageSource() {} | 117 IconWithBadgeImageSource::~IconWithBadgeImageSource() {} |
| 107 | 118 |
| 108 void IconWithBadgeImageSource::SetIcon(const gfx::Image& icon) { | 119 void IconWithBadgeImageSource::SetIcon(const gfx::Image& icon) { |
| 109 icon_ = icon; | 120 icon_ = icon; |
| 110 } | 121 } |
| 111 | 122 |
| 112 void IconWithBadgeImageSource::SetBadge(scoped_ptr<Badge> badge) { | 123 void IconWithBadgeImageSource::SetBadge(scoped_ptr<Badge> badge) { |
| 113 badge_ = std::move(badge); | 124 badge_ = std::move(badge); |
| 114 } | 125 } |
| 115 | 126 |
| 116 void IconWithBadgeImageSource::Draw(gfx::Canvas* canvas) { | 127 void IconWithBadgeImageSource::Draw(gfx::Canvas* canvas) { |
| 117 if (icon_.IsEmpty()) | 128 if (icon_.IsEmpty()) |
| 118 return; | 129 return; |
| 119 | 130 |
| 131 gfx::ImageSkia skia = icon_.AsImageSkia(); |
| 132 // TODO(estade): Fix setIcon and enable this on !MD. |
| 133 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 134 gfx::ImageSkiaRep rep = skia.GetRepresentation(canvas->image_scale()); |
| 135 if (rep.scale() != canvas->image_scale()) |
| 136 skia.AddRepresentation(ScaleImageSkiaRep(rep, canvas->image_scale())); |
| 137 } |
| 138 if (grayscale_) |
| 139 skia = gfx::ImageSkiaOperations::CreateHSLShiftedImage(skia, {-1, 0, 0.6}); |
| 140 |
| 120 int x_offset = std::floor((size().width() - icon_.Width()) / 2.0); | 141 int x_offset = std::floor((size().width() - icon_.Width()) / 2.0); |
| 121 int y_offset = std::floor((size().height() - icon_.Height()) / 2.0); | 142 int y_offset = std::floor((size().height() - icon_.Height()) / 2.0); |
| 122 gfx::ImageSkia skia = icon_.AsImageSkia(); | |
| 123 if (grayscale_) | |
| 124 skia = gfx::ImageSkiaOperations::CreateHSLShiftedImage(skia, {-1, 0, 0.6}); | |
| 125 canvas->DrawImageInt(skia, x_offset, y_offset); | 143 canvas->DrawImageInt(skia, x_offset, y_offset); |
| 126 | 144 |
| 127 // Draw a badge on the provided browser action icon's canvas. | 145 // Draw a badge on the provided browser action icon's canvas. |
| 128 PaintBadge(canvas); | 146 PaintBadge(canvas); |
| 129 | 147 |
| 130 if (paint_decoration_) | 148 if (paint_decoration_) |
| 131 PaintDecoration(canvas); | 149 PaintDecoration(canvas); |
| 132 } | 150 } |
| 133 | 151 |
| 134 // Paints badge with specified parameters to |canvas|. | 152 // Paints badge with specified parameters to |canvas|. |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 gfx::Point center_point(major_radius + 1, size().height() - (major_radius)-1); | 273 gfx::Point center_point(major_radius + 1, size().height() - (major_radius)-1); |
| 256 SkPaint paint; | 274 SkPaint paint; |
| 257 paint.setAntiAlias(true); | 275 paint.setAntiAlias(true); |
| 258 paint.setStyle(SkPaint::kFill_Style); | 276 paint.setStyle(SkPaint::kFill_Style); |
| 259 paint.setColor(SK_ColorTRANSPARENT); | 277 paint.setColor(SK_ColorTRANSPARENT); |
| 260 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 278 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 261 canvas->DrawCircle(center_point, major_radius, paint); | 279 canvas->DrawCircle(center_point, major_radius, paint); |
| 262 paint.setColor(decoration_color); | 280 paint.setColor(decoration_color); |
| 263 canvas->DrawCircle(center_point, minor_radius, paint); | 281 canvas->DrawCircle(center_point, minor_radius, paint); |
| 264 } | 282 } |
| OLD | NEW |