Chromium Code Reviews| 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 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // This is used to increase the right margin of this decoration. | 18 // This is used to increase the right margin of this decoration. |
| 19 const CGFloat kRightSideMargin = 1.0; | 19 const CGFloat kRightSideMargin = 1.0; |
| 20 | 20 |
| 21 // Padding between the icon/label and bubble edges. | 21 // Padding between the icon/label and bubble edges. |
| 22 CGFloat BubblePadding() { | 22 CGFloat BubblePadding() { |
| 23 return ui::MaterialDesignController::IsModeMaterial() ? 8.0 : 3.0; | 23 return ui::MaterialDesignController::IsModeMaterial() ? 8.0 : 3.0; |
| 24 } | 24 } |
| 25 | 25 |
| 26 // Additional padding between the divider between the omnibox text and the | 26 // Additional padding between the divider between the omnibox text and the |
| 27 // divider. The desired value is 8px. We get 3px by subtracting the existing | 27 // divider. The desired value is 8px. We get 3px by subtracting the existing |
| 28 // padding in location_bar_view from 8px. | 28 // padding in location_bar_view from 8px. |
| 29 CGFloat DividerPadding() { | 29 CGFloat DividerPadding() { |
| 30 return ui::MaterialDesignController::IsModeMaterial() ? 3.0 : 0.0; | 30 return ui::MaterialDesignController::IsModeMaterial() ? 2.0 : 0.0; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Padding between the icon and label. | 33 // Padding between the icon and label. |
| 34 const CGFloat kIconLabelPadding = 4.0; | 34 CGFloat IconLabelPadding() { |
| 35 return ui::MaterialDesignController::IsModeMaterial() ? 3.0 : 4.0; | |
|
shrike
2016/05/24 01:13:26
Actually, the 4pt of padding was fine. It's just t
spqchan
2016/05/24 21:05:24
I made the pixel pushes with the new icon. Let me
| |
| 36 } | |
| 35 | 37 |
| 36 // Inset for the background. | 38 // Inset for the background. |
| 37 const CGFloat kBackgroundYInset = 4.0; | 39 const CGFloat kBackgroundYInset = 4.0; |
| 38 | 40 |
| 39 } // namespace | 41 } // namespace |
| 40 | 42 |
| 41 BubbleDecoration::BubbleDecoration() : baseline_offset_(0) { | 43 BubbleDecoration::BubbleDecoration() : baseline_offset_(0) { |
| 42 attributes_.reset([[NSMutableDictionary alloc] init]); | 44 attributes_.reset([[NSMutableDictionary alloc] init]); |
| 43 [attributes_ setObject:LocationBarDecoration::GetFont() | 45 [attributes_ setObject:LocationBarDecoration::GetFont() |
| 44 forKey:NSFontAttributeName]; | 46 forKey:NSFontAttributeName]; |
| 45 } | 47 } |
| 46 | 48 |
| 47 BubbleDecoration::~BubbleDecoration() { | 49 BubbleDecoration::~BubbleDecoration() { |
| 48 } | 50 } |
| 49 | 51 |
| 50 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, | 52 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, |
| 51 NSString* label) { | 53 NSString* label) { |
| 52 if (!image && !label) | 54 if (!image && !label) |
| 53 return kOmittedWidth; | 55 return kOmittedWidth; |
| 54 | 56 |
| 55 const CGFloat image_width = image ? [image size].width : 0.0; | 57 const CGFloat image_width = image ? [image size].width : 0.0; |
| 56 if (!label) | 58 if (!label) |
| 57 return BubblePadding() + image_width; | 59 return BubblePadding() + image_width; |
| 58 | 60 |
| 59 // The bubble needs to take up an integral number of pixels. | 61 // The bubble needs to take up an integral number of pixels. |
| 60 // Generally -sizeWithAttributes: seems to overestimate rather than | 62 // Generally -sizeWithAttributes: seems to overestimate rather than |
| 61 // underestimate, so floor() seems to work better. | 63 // underestimate, so floor() seems to work better. |
| 62 const CGFloat label_width = | 64 const CGFloat label_width = |
| 63 std::floor([label sizeWithAttributes:attributes_].width); | 65 std::floor([label sizeWithAttributes:attributes_].width); |
| 64 return BubblePadding() + image_width + kIconLabelPadding + label_width + | 66 return BubblePadding() + image_width + IconLabelPadding() + label_width + |
| 65 DividerPadding(); | 67 DividerPadding(); |
| 66 } | 68 } |
| 67 | 69 |
| 68 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { | 70 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { |
| 69 NSRect imageRect = NSInsetRect(frame, 0.0, kBackgroundYInset); | 71 NSRect imageRect = NSInsetRect(frame, 0.0, kBackgroundYInset); |
| 70 if (image_) { | 72 if (image_) { |
| 71 // Center the image vertically. | 73 // Center the image vertically. |
| 72 const NSSize imageSize = [image_ size]; | 74 const NSSize imageSize = [image_ size]; |
| 73 imageRect.origin.y += | 75 imageRect.origin.y += |
| 74 std::floor((NSHeight(frame) - imageSize.height) / 2.0); | 76 std::floor((NSHeight(frame) - imageSize.height) / 2.0); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 98 NSRect imageRect = decoration_frame; | 100 NSRect imageRect = decoration_frame; |
| 99 imageRect.origin.y += | 101 imageRect.origin.y += |
| 100 std::floor((NSHeight(decoration_frame) - imageSize.height) / 2.0); | 102 std::floor((NSHeight(decoration_frame) - imageSize.height) / 2.0); |
| 101 imageRect.size = imageSize; | 103 imageRect.size = imageSize; |
| 102 [image_ drawInRect:imageRect | 104 [image_ drawInRect:imageRect |
| 103 fromRect:NSZeroRect // Entire image | 105 fromRect:NSZeroRect // Entire image |
| 104 operation:NSCompositeSourceOver | 106 operation:NSCompositeSourceOver |
| 105 fraction:1.0 | 107 fraction:1.0 |
| 106 respectFlipped:YES | 108 respectFlipped:YES |
| 107 hints:nil]; | 109 hints:nil]; |
| 108 textOffset = NSMaxX(imageRect) + kIconLabelPadding; | 110 textOffset = NSMaxX(imageRect) + IconLabelPadding(); |
| 109 } | 111 } |
| 110 | 112 |
| 111 // Draw the divider and set the text color. | 113 // Draw the divider and set the text color. |
| 112 if (ui::MaterialDesignController::IsModeMaterial()) { | 114 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 113 NSBezierPath* line = [NSBezierPath bezierPath]; | 115 NSBezierPath* line = [NSBezierPath bezierPath]; |
| 114 [line setLineWidth:1]; | 116 [line setLineWidth:1]; |
| 115 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), | 117 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 116 NSMinY(decoration_frame))]; | 118 NSMinY(decoration_frame))]; |
| 117 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), | 119 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 118 NSMaxY(decoration_frame))]; | 120 NSMaxY(decoration_frame))]; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 176 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 175 } | 177 } |
| 176 | 178 |
| 177 void BubbleDecoration::SetFont(NSFont* font) { | 179 void BubbleDecoration::SetFont(NSFont* font) { |
| 178 [attributes_ setObject:font forKey:NSFontAttributeName]; | 180 [attributes_ setObject:font forKey:NSFontAttributeName]; |
| 179 } | 181 } |
| 180 | 182 |
| 181 void BubbleDecoration::SetBaselineOffset(CGFloat offset) { | 183 void BubbleDecoration::SetBaselineOffset(CGFloat offset) { |
| 182 baseline_offset_ = offset; | 184 baseline_offset_ = offset; |
| 183 } | 185 } |
| OLD | NEW |