| 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 #include "base/mac/foundation_util.h" |
| 11 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 12 #import "ui/base/cocoa/nsview_additions.h" |
| 13 #include "ui/base/material_design/material_design_controller.h" |
| 10 | 14 |
| 11 namespace { | 15 namespace { |
| 12 | 16 |
| 13 // This is used to increase the right margin of this decoration. | 17 // This is used to increase the right margin of this decoration. |
| 14 const CGFloat kRightSideMargin = 1.0; | 18 const CGFloat kRightSideMargin = 1.0; |
| 15 | 19 |
| 16 // Padding between the icon/label and bubble edges. | 20 // Padding between the icon/label and bubble edges. |
| 17 const CGFloat kBubblePadding = 3.0; | 21 CGFloat BubblePadding() { |
| 22 return ui::MaterialDesignController::IsModeMaterial() ? 7 : 3; |
| 23 } |
| 24 |
| 18 | 25 |
| 19 // Padding between the icon and label. | 26 // Padding between the icon and label. |
| 20 const CGFloat kIconLabelPadding = 4.0; | 27 const CGFloat kIconLabelPadding = 4.0; |
| 21 | 28 |
| 22 // Inset for the background. | 29 // Inset for the background. |
| 23 const CGFloat kBackgroundYInset = 4.0; | 30 const CGFloat kBackgroundYInset = 4.0; |
| 24 | 31 |
| 25 } // namespace | 32 } // namespace |
| 26 | 33 |
| 27 BubbleDecoration::BubbleDecoration() { | 34 BubbleDecoration::BubbleDecoration() { |
| 28 attributes_.reset([[NSMutableDictionary alloc] init]); | 35 attributes_.reset([[NSMutableDictionary alloc] init]); |
| 29 [attributes_ setObject:GetFont() forKey:NSFontAttributeName]; | 36 [attributes_ setObject:GetFont() forKey:NSFontAttributeName]; |
| 30 } | 37 } |
| 31 | 38 |
| 32 BubbleDecoration::~BubbleDecoration() { | 39 BubbleDecoration::~BubbleDecoration() { |
| 33 } | 40 } |
| 34 | 41 |
| 35 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, | 42 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, |
| 36 NSString* label) { | 43 NSString* label) { |
| 37 if (!image && !label) | 44 if (!image && !label) |
| 38 return kOmittedWidth; | 45 return kOmittedWidth; |
| 39 | 46 |
| 40 const CGFloat image_width = image ? [image size].width : 0.0; | 47 const CGFloat image_width = image ? [image size].width : 0.0; |
| 41 if (!label) | 48 if (!label) |
| 42 return kBubblePadding + image_width; | 49 return BubblePadding() + image_width; |
| 43 | 50 |
| 44 // The bubble needs to take up an integral number of pixels. | 51 // The bubble needs to take up an integral number of pixels. |
| 45 // Generally -sizeWithAttributes: seems to overestimate rather than | 52 // Generally -sizeWithAttributes: seems to overestimate rather than |
| 46 // underestimate, so floor() seems to work better. | 53 // underestimate, so floor() seems to work better. |
| 47 const CGFloat label_width = | 54 const CGFloat label_width = |
| 48 std::floor([label sizeWithAttributes:attributes_].width); | 55 std::floor([label sizeWithAttributes:attributes_].width); |
| 49 return kBubblePadding + image_width + kIconLabelPadding + label_width; | 56 return BubblePadding() + image_width + kIconLabelPadding + label_width; |
| 50 } | 57 } |
| 51 | 58 |
| 52 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { | 59 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { |
| 53 NSRect imageRect = NSInsetRect(frame, 0.0, kBackgroundYInset); | 60 NSRect imageRect = NSInsetRect(frame, 0.0, kBackgroundYInset); |
| 54 if (image_) { | 61 if (image_) { |
| 55 // Center the image vertically. | 62 // Center the image vertically. |
| 56 const NSSize imageSize = [image_ size]; | 63 const NSSize imageSize = [image_ size]; |
| 57 imageRect.origin.y += | 64 imageRect.origin.y += |
| 58 std::floor((NSHeight(frame) - imageSize.height) / 2.0); | 65 std::floor((NSHeight(frame) - imageSize.height) / 2.0); |
| 59 imageRect.size = imageSize; | 66 imageRect.size = imageSize; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 textRect.size.width = NSMaxX(decoration_frame) - NSMinX(textRect); | 105 textRect.size.width = NSMaxX(decoration_frame) - NSMinX(textRect); |
| 99 DrawLabel(label_, attributes_, textRect); | 106 DrawLabel(label_, attributes_, textRect); |
| 100 } | 107 } |
| 101 } | 108 } |
| 102 | 109 |
| 103 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, | 110 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, |
| 104 NSRect frame, | 111 NSRect frame, |
| 105 NSView* control_view) { | 112 NSView* control_view) { |
| 106 NSRect rect = NSInsetRect(background_frame, 0, 1); | 113 NSRect rect = NSInsetRect(background_frame, 0, 1); |
| 107 rect.size.width -= kRightSideMargin; | 114 rect.size.width -= kRightSideMargin; |
| 108 ui::DrawNinePartImage( | 115 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 109 rect, GetBubbleImageIds(), NSCompositeSourceOver, 1.0, true); | 116 CGFloat lineWidth = [control_view cr_lineWidth]; |
| 117 rect = NSInsetRect(rect, lineWidth / 2., lineWidth / 2.); |
| 118 NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:rect |
| 119 xRadius:3 |
| 120 yRadius:3]; |
| 121 [path setLineWidth:lineWidth]; |
| 122 bool inDarkMode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 123 if (inDarkMode) { |
| 124 [[NSColor whiteColor] set]; |
| 125 [path fill]; |
| 126 } else { |
| 127 [GetBackgroundBorderColor() set]; |
| 128 [path stroke]; |
| 129 } |
| 130 } else { |
| 131 ui::DrawNinePartImage( |
| 132 rect, GetBubbleImageIds(), NSCompositeSourceOver, 1.0, true); |
| 133 } |
| 110 | 134 |
| 111 DrawInFrame(frame, control_view); | 135 DrawInFrame(frame, control_view); |
| 112 } | 136 } |
| 113 | 137 |
| 114 NSImage* BubbleDecoration::GetImage() { | 138 NSImage* BubbleDecoration::GetImage() { |
| 115 return image_; | 139 return image_; |
| 116 } | 140 } |
| 117 | 141 |
| 118 void BubbleDecoration::SetImage(NSImage* image) { | 142 void BubbleDecoration::SetImage(NSImage* image) { |
| 119 image_.reset([image retain]); | 143 image_.reset([image retain]); |
| 120 } | 144 } |
| 121 | 145 |
| 122 void BubbleDecoration::SetLabel(NSString* label) { | 146 void BubbleDecoration::SetLabel(NSString* label) { |
| 123 // If the initializer was called with |nil|, then the code cannot | 147 // If the initializer was called with |nil|, then the code cannot |
| 124 // process a label. | 148 // process a label. |
| 125 DCHECK(attributes_); | 149 DCHECK(attributes_); |
| 126 if (attributes_) | 150 if (attributes_) |
| 127 label_.reset([label copy]); | 151 label_.reset([label copy]); |
| 128 } | 152 } |
| 129 | 153 |
| 130 void BubbleDecoration::SetTextColor(NSColor* text_color) { | 154 void BubbleDecoration::SetTextColor(NSColor* text_color) { |
| 131 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 155 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 132 } | 156 } |
| OLD | NEW |