| 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/ui/cocoa/location_bar/bubble_decoration.h" | 7 #import "chrome/browser/ui/cocoa/location_bar/bubble_decoration.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 // Padding between the icon/label and bubble edges. | 13 // Padding between the icon/label and bubble edges. |
| 14 const CGFloat kBubblePadding = 3.0; | 14 const CGFloat kBubblePadding = 3.0; |
| 15 | 15 |
| 16 // The image needs to be in the same position as for the location | 16 // The image needs to be in the same position as for the location |
| 17 // icon, which implies that the bubble's padding in the Omnibox needs | 17 // icon, which implies that the bubble's padding in the Omnibox needs |
| 18 // to differ from the location icon's. Indeed, that's how the views | 18 // to differ from the location icon's. Indeed, that's how the views |
| 19 // implementation handles the problem. This draws the bubble edge a | 19 // implementation handles the problem. This draws the bubble edge a |
| 20 // little bit further left, which is easier but no less hacky. | 20 // little bit further left, which is easier but no less hacky. |
| 21 const CGFloat kLeftSideOverdraw = 2.0; | 21 const CGFloat kLeftSideOverdraw = 2.0; |
| 22 | 22 |
| 23 // Omnibox corner radius is |4.0|, this needs to look tight WRT that. | 23 // Omnibox corner radius is |4.0|, this needs to look tight WRT that. |
| 24 const CGFloat kBubbleCornerRadius = 2.0; | 24 const CGFloat kBubbleCornerRadius = 2.0; |
| 25 | 25 |
| 26 // How far to inset the bubble from the top and bottom of the drawing | |
| 27 // frame. | |
| 28 // TODO(shess): Would be nicer to have the drawing code factor out the | |
| 29 // space outside the border, and perhaps the border. Then this could | |
| 30 // reflect the single pixel space w/in that. | |
| 31 const CGFloat kBubbleYInset = 4.0; | |
| 32 | |
| 33 // Padding between the icon and label. | 26 // Padding between the icon and label. |
| 34 const CGFloat kIconLabelPadding = 4.0; | 27 const CGFloat kIconLabelPadding = 4.0; |
| 35 | 28 |
| 36 } // namespace | 29 } // namespace |
| 37 | 30 |
| 38 BubbleDecoration::BubbleDecoration(NSFont* font) { | 31 BubbleDecoration::BubbleDecoration(NSFont* font) { |
| 39 DCHECK(font); | 32 DCHECK(font); |
| 40 if (font) { | 33 if (font) { |
| 41 NSDictionary* attributes = | 34 NSDictionary* attributes = |
| 42 [NSDictionary dictionaryWithObject:font | 35 [NSDictionary dictionaryWithObject:font |
| (...skipping 16 matching lines...) Expand all Loading... |
| 59 | 52 |
| 60 // The bubble needs to take up an integral number of pixels. | 53 // The bubble needs to take up an integral number of pixels. |
| 61 // Generally -sizeWithAttributes: seems to overestimate rather than | 54 // Generally -sizeWithAttributes: seems to overestimate rather than |
| 62 // underestimate, so floor() seems to work better. | 55 // underestimate, so floor() seems to work better. |
| 63 const CGFloat label_width = | 56 const CGFloat label_width = |
| 64 std::floor([label sizeWithAttributes:attributes_].width); | 57 std::floor([label sizeWithAttributes:attributes_].width); |
| 65 return kBubblePadding + image_width + kIconLabelPadding + label_width; | 58 return kBubblePadding + image_width + kIconLabelPadding + label_width; |
| 66 } | 59 } |
| 67 | 60 |
| 68 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { | 61 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { |
| 69 NSRect imageRect = NSInsetRect(frame, 0.0, kBubbleYInset); | 62 NSRect imageRect = NSInsetRect(frame, 0.0, kTextYInset); |
| 70 if (image_) { | 63 if (image_) { |
| 71 // Center the image vertically. | 64 // Center the image vertically. |
| 72 const NSSize imageSize = [image_ size]; | 65 const NSSize imageSize = [image_ size]; |
| 73 imageRect.origin.y += | 66 imageRect.origin.y += |
| 74 std::floor((NSHeight(frame) - imageSize.height) / 2.0); | 67 std::floor((NSHeight(frame) - imageSize.height) / 2.0); |
| 75 imageRect.size = imageSize; | 68 imageRect.size = imageSize; |
| 76 } | 69 } |
| 77 return imageRect; | 70 return imageRect; |
| 78 } | 71 } |
| 79 | 72 |
| 80 CGFloat BubbleDecoration::GetWidthForSpace(CGFloat width) { | 73 CGFloat BubbleDecoration::GetWidthForSpace(CGFloat width, CGFloat text_width) { |
| 81 const CGFloat all_width = GetWidthForImageAndLabel(image_, label_); | 74 const CGFloat all_width = GetWidthForImageAndLabel(image_, label_); |
| 82 if (all_width <= width) | 75 if (all_width <= width) |
| 83 return all_width; | 76 return all_width; |
| 84 | 77 |
| 85 const CGFloat image_width = GetWidthForImageAndLabel(image_, nil); | 78 const CGFloat image_width = GetWidthForImageAndLabel(image_, nil); |
| 86 if (image_width <= width) | 79 if (image_width <= width) |
| 87 return image_width; | 80 return image_width; |
| 88 | 81 |
| 89 return kOmittedWidth; | 82 return kOmittedWidth; |
| 90 } | 83 } |
| 91 | 84 |
| 92 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { | 85 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { |
| 93 const NSRect decorationFrame = NSInsetRect(frame, 0.0, kBubbleYInset); | 86 const NSRect decorationFrame = NSInsetRect(frame, 0.0, kTextYInset); |
| 94 | 87 |
| 95 // The inset is to put the border down the middle of the pixel. | 88 // The inset is to put the border down the middle of the pixel. |
| 96 NSRect bubbleFrame = NSInsetRect(decorationFrame, 0.5, 0.5); | 89 NSRect bubbleFrame = NSInsetRect(decorationFrame, 0.5, 0.5); |
| 97 bubbleFrame.origin.x -= kLeftSideOverdraw; | 90 bubbleFrame.origin.x -= kLeftSideOverdraw; |
| 98 bubbleFrame.size.width += kLeftSideOverdraw; | 91 bubbleFrame.size.width += kLeftSideOverdraw; |
| 99 NSBezierPath* path = | 92 NSBezierPath* path = |
| 100 [NSBezierPath bezierPathWithRoundedRect:bubbleFrame | 93 [NSBezierPath bezierPathWithRoundedRect:bubbleFrame |
| 101 xRadius:kBubbleCornerRadius | 94 xRadius:kBubbleCornerRadius |
| 102 yRadius:kBubbleCornerRadius]; | 95 yRadius:kBubbleCornerRadius]; |
| 103 | 96 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 void BubbleDecoration::SetColors(NSColor* border_color, | 145 void BubbleDecoration::SetColors(NSColor* border_color, |
| 153 NSColor* background_color, | 146 NSColor* background_color, |
| 154 NSColor* text_color) { | 147 NSColor* text_color) { |
| 155 border_color_.reset([border_color retain]); | 148 border_color_.reset([border_color retain]); |
| 156 background_color_.reset([background_color retain]); | 149 background_color_.reset([background_color retain]); |
| 157 | 150 |
| 158 scoped_nsobject<NSMutableDictionary> attributes([attributes_ mutableCopy]); | 151 scoped_nsobject<NSMutableDictionary> attributes([attributes_ mutableCopy]); |
| 159 [attributes setObject:text_color forKey:NSForegroundColorAttributeName]; | 152 [attributes setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 160 attributes_.reset([attributes copy]); | 153 attributes_.reset([attributes copy]); |
| 161 } | 154 } |
| OLD | NEW |