Chromium Code Reviews| 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> | |
| 8 | |
| 9 #include "base/mac/scoped_nsobject.h" | |
| 7 #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" | |
| 12 #include "ui/base/material_design/material_design_controller.h" | |
| 8 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 9 #include "ui/resources/grit/ui_resources.h" | 14 #include "ui/resources/grit/ui_resources.h" |
| 10 | 15 |
| 16 namespace { | |
| 17 | |
| 18 const CGFloat kVectorIconSize = 16; | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 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; | |
|
Robert Sesek
2016/06/14 21:29:00
nit: capitalized O in COlor
spqchan
2016/06/15 01:35:08
Done.
| |
| 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 | |
| 11 namespace mac { | 91 namespace mac { |
| 12 | 92 |
| 13 NSImage* FaviconForWebContents(content::WebContents* contents) { | 93 NSImage* FaviconForWebContents(content::WebContents* contents, SkColor color) { |
| 14 favicon::FaviconDriver* favicon_driver = | 94 favicon::FaviconDriver* favicon_driver = |
| 15 contents ? favicon::ContentFaviconDriver::FromWebContents(contents) | 95 contents ? favicon::ContentFaviconDriver::FromWebContents(contents) |
| 16 : nullptr; | 96 : nullptr; |
| 17 if (favicon_driver && favicon_driver->FaviconIsValid()) { | 97 if (favicon_driver && favicon_driver->FaviconIsValid()) { |
| 18 NSImage* image = favicon_driver->GetFavicon().AsNSImage(); | 98 NSImage* image = favicon_driver->GetFavicon().AsNSImage(); |
| 19 // The |image| could be nil if the bitmap is null. In that case, fallback | 99 // The |image| could be nil if the bitmap is null. In that case, fallback |
| 20 // to the default image. | 100 // to the default image. |
| 21 if (image) { | 101 if (image) { |
| 22 return image; | 102 return image; |
| 23 } | 103 } |
| 24 } | 104 } |
| 25 | 105 |
| 106 if (ui::MaterialDesignController::IsModeMaterial()) | |
| 107 return [DefaultFaviconImageRep imageForColor:color]; | |
| 108 | |
| 26 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 109 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 27 return rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); | 110 return rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); |
| 28 } | 111 } |
| 29 | 112 |
| 30 } // namespace mac | 113 } // namespace mac |
| OLD | NEW |