| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <cmath> | 5 #include <cmath> |
| 6 | 6 |
| 7 #import "chrome/browser/cocoa/location_bar/image_decoration.h" | 7 #import "chrome/browser/cocoa/location_bar/image_decoration.h" |
| 8 | 8 |
| 9 #import "chrome/browser/cocoa/image_utils.h" | 9 #import "chrome/browser/cocoa/image_utils.h" |
| 10 | 10 |
| 11 ImageDecoration::ImageDecoration() { | 11 ImageDecoration::ImageDecoration() { |
| 12 } | 12 } |
| 13 | 13 |
| 14 ImageDecoration::~ImageDecoration() { | 14 ImageDecoration::~ImageDecoration() { |
| 15 } | 15 } |
| 16 | 16 |
| 17 NSImage* ImageDecoration::GetImage() { | 17 NSImage* ImageDecoration::GetImage() { |
| 18 return image_; | 18 return image_; |
| 19 } | 19 } |
| 20 | 20 |
| 21 void ImageDecoration::SetImage(NSImage* image) { | 21 void ImageDecoration::SetImage(NSImage* image) { |
| 22 image_.reset([image retain]); | 22 image_.reset([image retain]); |
| 23 } | 23 } |
| 24 | 24 |
| 25 NSRect ImageDecoration::GetDrawRectInFrame(NSRect frame) { | 25 NSRect ImageDecoration::GetDrawRectInFrame(NSRect frame) { |
| 26 NSImage* image = GetImage(); | 26 NSImage* image = GetImage(); |
| 27 if (!image) | 27 if (!image) |
| 28 return frame; | 28 return frame; |
| 29 | 29 |
| 30 // Center the image within the frame. |
| 30 const CGFloat delta_height = NSHeight(frame) - [image size].height; | 31 const CGFloat delta_height = NSHeight(frame) - [image size].height; |
| 31 const CGFloat y_inset = std::floor(delta_height / 2.0); | 32 const CGFloat y_inset = std::floor(delta_height / 2.0); |
| 32 return NSInsetRect(frame, 0.0, y_inset); | 33 const CGFloat delta_width = NSWidth(frame) - [image size].width; |
| 34 const CGFloat x_inset = std::floor(delta_width / 2.0); |
| 35 return NSInsetRect(frame, x_inset, y_inset); |
| 33 } | 36 } |
| 34 | 37 |
| 35 CGFloat ImageDecoration::GetWidthForSpace(CGFloat width) { | 38 CGFloat ImageDecoration::GetWidthForSpace(CGFloat width) { |
| 36 NSImage* image = GetImage(); | 39 NSImage* image = GetImage(); |
| 37 if (image) { | 40 if (image) { |
| 38 const CGFloat image_width = [image size].width; | 41 const CGFloat image_width = [image size].width; |
| 39 if (image_width <= width) | 42 if (image_width <= width) |
| 40 return image_width; | 43 return image_width; |
| 41 } | 44 } |
| 42 return kOmittedWidth; | 45 return kOmittedWidth; |
| 43 } | 46 } |
| 44 | 47 |
| 45 void ImageDecoration::DrawInFrame(NSRect frame, NSView* control_view) { | 48 void ImageDecoration::DrawInFrame(NSRect frame, NSView* control_view) { |
| 46 [GetImage() drawInRect:GetDrawRectInFrame(frame) | 49 [GetImage() drawInRect:GetDrawRectInFrame(frame) |
| 47 fromRect:NSZeroRect // Entire image | 50 fromRect:NSZeroRect // Entire image |
| 48 operation:NSCompositeSourceOver | 51 operation:NSCompositeSourceOver |
| 49 fraction:1.0 | 52 fraction:1.0 |
| 50 neverFlipped:YES]; | 53 neverFlipped:YES]; |
| 51 } | 54 } |
| OLD | NEW |