| 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" | 10 #include "base/mac/foundation_util.h" |
| 11 #import "chrome/browser/ui/cocoa/themed_window.h" | 11 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 12 #include "skia/ext/skia_utils_mac.h" | 12 #include "skia/ext/skia_utils_mac.h" |
| 13 #import "ui/base/cocoa/nsview_additions.h" | 13 #import "ui/base/cocoa/nsview_additions.h" |
| 14 #include "ui/base/material_design/material_design_controller.h" | 14 #include "ui/base/material_design/material_design_controller.h" |
| 15 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 15 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // This is used to increase the right margin of this decoration. | 19 // This is used to increase the right margin of this decoration. |
| 20 const CGFloat kRightSideMargin = 1.0; | 20 const CGFloat kLeftSidePadding = 6.0; |
| 21 | 21 |
| 22 // Padding between the icon/label and bubble edges. | 22 // Padding between the icon/label and bubble edges. |
| 23 const CGFloat kBubblePadding = 8.0; | 23 const CGFloat kBubblePadding = 7.0; |
| 24 | 24 |
| 25 // Additional padding between the divider and the label. | 25 // Additional padding between the divider and the label. |
| 26 const CGFloat kDividerPadding = 2.0; | 26 const CGFloat kDividerPadding = 6.0; |
| 27 | 27 |
| 28 // Padding between the icon and label. | 28 // Padding between the icon and label. |
| 29 const CGFloat kIconLabelPadding = 4.0; | 29 const CGFloat kIconLabelPadding = 4.0; |
| 30 | 30 |
| 31 // Inset for the background. | 31 // Inset for the image frame. |
| 32 const CGFloat kBackgroundYInset = 4.0; | 32 const CGFloat kImageFrameYInset = 4.0; |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { | 36 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { |
| 37 attributes_.reset([[NSMutableDictionary alloc] init]); | 37 attributes_.reset([[NSMutableDictionary alloc] init]); |
| 38 [attributes_ setObject:LocationBarDecoration::GetFont() | 38 [attributes_ setObject:LocationBarDecoration::GetFont() |
| 39 forKey:NSFontAttributeName]; | 39 forKey:NSFontAttributeName]; |
| 40 } | 40 } |
| 41 | 41 |
| 42 BubbleDecoration::~BubbleDecoration() { | 42 BubbleDecoration::~BubbleDecoration() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 54 const CGFloat image_width = image ? [image size].width : 0.0; | 54 const CGFloat image_width = image ? [image size].width : 0.0; |
| 55 if (!label) | 55 if (!label) |
| 56 return kBubblePadding + image_width; | 56 return kBubblePadding + image_width; |
| 57 | 57 |
| 58 // The bubble needs to take up an integral number of pixels. | 58 // The bubble needs to take up an integral number of pixels. |
| 59 // Generally -sizeWithAttributes: seems to overestimate rather than | 59 // Generally -sizeWithAttributes: seems to overestimate rather than |
| 60 // underestimate, so floor() seems to work better. | 60 // underestimate, so floor() seems to work better. |
| 61 const CGFloat label_width = | 61 const CGFloat label_width = |
| 62 std::floor([label sizeWithAttributes:attributes_].width); | 62 std::floor([label sizeWithAttributes:attributes_].width); |
| 63 return kBubblePadding + image_width + kIconLabelPadding + label_width + | 63 return kBubblePadding + image_width + kIconLabelPadding + label_width + |
| 64 DividerPadding(); | 64 DividerPadding() + kLeftSidePadding; |
| 65 } | 65 } |
| 66 | 66 |
| 67 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { | 67 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { |
| 68 NSRect image_rect = NSInsetRect(frame, 0.0, kBackgroundYInset); | 68 NSRect image_rect = NSInsetRect(frame, 0.0, kImageFrameYInset); |
| 69 if (image_) { | 69 if (image_) { |
| 70 // Center the image vertically. | 70 // Center the image vertically. |
| 71 const NSSize image_size = [image_ size]; | 71 const NSSize image_size = [image_ size]; |
| 72 | 72 |
| 73 image_rect.origin.y += | 73 image_rect.origin.y += |
| 74 std::floor((NSHeight(frame) - image_size.height) / 2.0); | 74 std::floor((NSHeight(frame) - image_size.height) / 2.0); |
| 75 image_rect.size = image_size; | 75 image_rect.size = image_size; |
| 76 } | 76 } |
| 77 return image_rect; | 77 return image_rect; |
| 78 } | 78 } |
| 79 | 79 |
| 80 NSColor* BubbleDecoration::GetDarkModeTextColor() { | 80 NSColor* BubbleDecoration::GetDarkModeTextColor() { |
| 81 return skia::SkColorToSRGBNSColor(kMaterialDarkModeTextColor); | 81 return skia::SkColorToSRGBNSColor(kMaterialDarkModeTextColor); |
| 82 } | 82 } |
| 83 | 83 |
| 84 CGFloat BubbleDecoration::GetWidthForSpace(CGFloat width) { | 84 CGFloat BubbleDecoration::GetWidthForSpace(CGFloat width) { |
| 85 const CGFloat all_width = GetWidthForImageAndLabel(image_, label_); | 85 const CGFloat all_width = GetWidthForImageAndLabel(image_, label_); |
| 86 if (all_width <= width) | 86 if (all_width <= width) |
| 87 return all_width; | 87 return all_width; |
| 88 | 88 |
| 89 const CGFloat image_width = GetWidthForImageAndLabel(image_, nil); | 89 const CGFloat image_width = GetWidthForImageAndLabel(image_, nil); |
| 90 if (image_width <= width) | 90 if (image_width <= width) |
| 91 return image_width; | 91 return image_width; |
| 92 | 92 |
| 93 return kOmittedWidth; | 93 return kOmittedWidth; |
| 94 } | 94 } |
| 95 | 95 |
| 96 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { | 96 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { |
| 97 const NSRect decoration_frame = NSInsetRect(frame, 0.0, kBackgroundYInset); | 97 const NSRect decoration_frame = NSInsetRect(frame, 0.0, kImageFrameYInset); |
| 98 CGFloat text_offset = NSMinX(decoration_frame); | 98 CGFloat text_offset = NSMinX(decoration_frame); |
| 99 if (image_) { | 99 if (image_) { |
| 100 // Center the image vertically. | 100 // Center the image vertically. |
| 101 const NSSize image_size = [image_ size]; | 101 const NSSize image_size = [image_ size]; |
| 102 NSRect image_rect = decoration_frame; | 102 NSRect image_rect = decoration_frame; |
| 103 image_rect.origin.x += kLeftSidePadding; |
| 103 image_rect.origin.y += | 104 image_rect.origin.y += |
| 104 std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0); | 105 std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0); |
| 105 image_rect.size = image_size; | 106 image_rect.size = image_size; |
| 106 [image_ drawInRect:image_rect | 107 [image_ drawInRect:image_rect |
| 107 fromRect:NSZeroRect // Entire image | 108 fromRect:NSZeroRect // Entire image |
| 108 operation:NSCompositeSourceOver | 109 operation:NSCompositeSourceOver |
| 109 fraction:1.0 | 110 fraction:1.0 |
| 110 respectFlipped:YES | 111 respectFlipped:YES |
| 111 hints:nil]; | 112 hints:nil]; |
| 112 text_offset = NSMaxX(image_rect) + kIconLabelPadding; | 113 text_offset = NSMaxX(image_rect) + kIconLabelPadding; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 138 CGFloat line_width = [control_view cr_lineWidth]; | 139 CGFloat line_width = [control_view cr_lineWidth]; |
| 139 if (line_width < 1) { | 140 if (line_width < 1) { |
| 140 NSAffineTransform* transform = [NSAffineTransform transform]; | 141 NSAffineTransform* transform = [NSAffineTransform transform]; |
| 141 [transform translateXBy:0 yBy:retina_baseline_offset_]; | 142 [transform translateXBy:0 yBy:retina_baseline_offset_]; |
| 142 [transform concat]; | 143 [transform concat]; |
| 143 } | 144 } |
| 144 DrawLabel(label_, attributes_, text_rect); | 145 DrawLabel(label_, attributes_, text_rect); |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 | 148 |
| 148 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, | |
| 149 NSRect frame, | |
| 150 NSView* control_view) { | |
| 151 NSRect rect = NSInsetRect(background_frame, 0, 1); | |
| 152 rect.size.width -= kRightSideMargin; | |
| 153 DrawInFrame(frame, control_view); | |
| 154 } | |
| 155 | |
| 156 NSFont* BubbleDecoration::GetFont() const { | 149 NSFont* BubbleDecoration::GetFont() const { |
| 157 return [attributes_ objectForKey:NSFontAttributeName]; | 150 return [attributes_ objectForKey:NSFontAttributeName]; |
| 158 } | 151 } |
| 159 | 152 |
| 160 NSImage* BubbleDecoration::GetImage() { | 153 NSImage* BubbleDecoration::GetImage() { |
| 161 return image_; | 154 return image_; |
| 162 } | 155 } |
| 163 | 156 |
| 164 void BubbleDecoration::SetImage(NSImage* image) { | 157 void BubbleDecoration::SetImage(NSImage* image) { |
| 165 image_.reset([image retain]); | 158 image_.reset([image retain]); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 177 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 170 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 178 } | 171 } |
| 179 | 172 |
| 180 void BubbleDecoration::SetFont(NSFont* font) { | 173 void BubbleDecoration::SetFont(NSFont* font) { |
| 181 [attributes_ setObject:font forKey:NSFontAttributeName]; | 174 [attributes_ setObject:font forKey:NSFontAttributeName]; |
| 182 } | 175 } |
| 183 | 176 |
| 184 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { | 177 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { |
| 185 retina_baseline_offset_ = offset; | 178 retina_baseline_offset_ = offset; |
| 186 } | 179 } |
| OLD | NEW |