| 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/cocoa/tab_contents/favicon_util_mac.h" | 5 #include "chrome/browser/ui/cocoa/tab_contents/favicon_util_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/mac/scoped_nsobject.h" | 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "components/favicon/content/content_favicon_driver.h" | 10 #include "components/favicon/content/content_favicon_driver.h" |
| 11 #include "skia/ext/skia_utils_mac.h" | 11 #include "skia/ext/skia_utils_mac.h" |
| 12 #include "ui/base/material_design/material_design_controller.h" | 12 #include "ui/base/material_design/material_design_controller.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/image/image_skia_util_mac.h" |
| 15 #include "ui/gfx/paint_vector_icon.h" |
| 16 #include "ui/gfx/vector_icons_public.h" |
| 14 #include "ui/resources/grit/ui_resources.h" | 17 #include "ui/resources/grit/ui_resources.h" |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 const CGFloat kVectorIconSize = 16; | 21 const CGFloat kVectorIconSize = 16; |
| 19 | 22 |
| 20 } // namespace | 23 } // namespace |
| 21 | 24 |
| 22 // A temporary class that draws the default favicon using vector commands. | |
| 23 // This class will be removed once a more general solution that works for all | |
| 24 // platforms is developed. | |
| 25 @interface DefaultFaviconImageRep : NSCustomImageRep | |
| 26 @property(retain, nonatomic) NSColor* strokeColor; | |
| 27 | |
| 28 + (NSImage*)imageForColor:(SkColor)strokeColor; | |
| 29 | |
| 30 // NSCustomImageRep delegate method that performs the drawing. | |
| 31 + (void)drawDefaultFavicon:(DefaultFaviconImageRep*)imageRep; | |
| 32 | |
| 33 @end | |
| 34 | |
| 35 @implementation DefaultFaviconImageRep | |
| 36 | |
| 37 @synthesize strokeColor = strokeColor_; | |
| 38 | |
| 39 - (void)dealloc { | |
| 40 [strokeColor_ release]; | |
| 41 [super dealloc]; | |
| 42 } | |
| 43 | |
| 44 + (NSImage*)imageForColor:(SkColor)strokeColor { | |
| 45 base::scoped_nsobject<DefaultFaviconImageRep> imageRep( | |
| 46 [[DefaultFaviconImageRep alloc] | |
| 47 initWithDrawSelector:@selector(drawDefaultFavicon:) | |
| 48 delegate:[DefaultFaviconImageRep class]]); | |
| 49 [imageRep setStrokeColor:skia::SkColorToSRGBNSColor(strokeColor)]; | |
| 50 | |
| 51 // Create the image from the image rep. | |
| 52 const NSSize imageSize = NSMakeSize(kVectorIconSize, kVectorIconSize); | |
| 53 NSImage* faviconImage = | |
| 54 [[[NSImage alloc] initWithSize:imageSize] autorelease]; | |
| 55 [faviconImage setCacheMode:NSImageCacheAlways]; | |
| 56 [faviconImage addRepresentation:imageRep]; | |
| 57 | |
| 58 [imageRep setSize:imageSize]; | |
| 59 | |
| 60 return faviconImage; | |
| 61 } | |
| 62 | |
| 63 + (void)drawDefaultFavicon:(DefaultFaviconImageRep*)imageRep { | |
| 64 // Translate by 1/2pt to ensure crisp lines. | |
| 65 CGContextRef context = static_cast<CGContextRef>( | |
| 66 [[NSGraphicsContext currentContext] graphicsPort]); | |
| 67 CGContextTranslateCTM(context, 0.5, 0.5); | |
| 68 | |
| 69 NSBezierPath* iconPath = [NSBezierPath bezierPath]; | |
| 70 | |
| 71 // Create the horizontal and vertical parts of the shape. | |
| 72 [iconPath moveToPoint:NSMakePoint(3, 1)]; | |
| 73 [iconPath relativeLineToPoint:NSMakePoint(0, 13)]; | |
| 74 [iconPath relativeLineToPoint:NSMakePoint(5, 0)]; | |
| 75 [iconPath relativeLineToPoint:NSMakePoint(0, -4)]; | |
| 76 [iconPath relativeLineToPoint:NSMakePoint(4, 0)]; | |
| 77 [iconPath relativeLineToPoint:NSMakePoint(0, -9)]; | |
| 78 [iconPath closePath]; | |
| 79 | |
| 80 // Add the diagonal line. | |
| 81 [iconPath moveToPoint:NSMakePoint(8, 14)]; | |
| 82 [iconPath relativeLineToPoint:NSMakePoint(4, -4)]; | |
| 83 | |
| 84 // Draw it in the desired color. | |
| 85 [[imageRep strokeColor] set]; | |
| 86 [iconPath stroke]; | |
| 87 } | |
| 88 | |
| 89 @end | |
| 90 | |
| 91 namespace mac { | 25 namespace mac { |
| 92 | 26 |
| 93 NSImage* FaviconForWebContents(content::WebContents* contents, SkColor color) { | 27 NSImage* FaviconForWebContents(content::WebContents* contents, SkColor color) { |
| 94 favicon::FaviconDriver* favicon_driver = | 28 favicon::FaviconDriver* favicon_driver = |
| 95 contents ? favicon::ContentFaviconDriver::FromWebContents(contents) | 29 contents ? favicon::ContentFaviconDriver::FromWebContents(contents) |
| 96 : nullptr; | 30 : nullptr; |
| 97 if (favicon_driver && favicon_driver->FaviconIsValid()) { | 31 if (favicon_driver && favicon_driver->FaviconIsValid()) { |
| 98 NSImage* image = favicon_driver->GetFavicon().AsNSImage(); | 32 NSImage* image = favicon_driver->GetFavicon().AsNSImage(); |
| 99 // The |image| could be nil if the bitmap is null. In that case, fallback | 33 // The |image| could be nil if the bitmap is null. In that case, fallback |
| 100 // to the default image. | 34 // to the default image. |
| 101 if (image) { | 35 if (image) { |
| 102 return image; | 36 return image; |
| 103 } | 37 } |
| 104 } | 38 } |
| 105 | 39 |
| 106 if (ui::MaterialDesignController::IsModeMaterial()) | 40 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 107 return [DefaultFaviconImageRep imageForColor:color]; | 41 return NSImageFromImageSkia(gfx::CreateVectorIcon( |
| 42 gfx::VectorIconId::DEFAULT_FAVICON, kVectorIconSize, color)); |
| 43 } |
| 108 | 44 |
| 109 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 45 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 110 return rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); | 46 return rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); |
| 111 } | 47 } |
| 112 | 48 |
| 113 } // namespace mac | 49 } // namespace mac |
| OLD | NEW |