| 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 #import "ui/base/cocoa/nsview_additions.h" | 13 #import "ui/base/cocoa/nsview_additions.h" |
| 13 #include "ui/base/material_design/material_design_controller.h" | 14 #include "ui/base/material_design/material_design_controller.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // This is used to increase the right margin of this decoration. | 18 // This is used to increase the right margin of this decoration. |
| 18 const CGFloat kRightSideMargin = 1.0; | 19 const CGFloat kRightSideMargin = 1.0; |
| 19 | 20 |
| 20 // Padding between the icon/label and bubble edges. | 21 // Padding between the icon/label and bubble edges. |
| 21 CGFloat BubblePadding() { | 22 CGFloat BubblePadding() { |
| 22 return ui::MaterialDesignController::IsModeMaterial() ? 7 : 3; | 23 return ui::MaterialDesignController::IsModeMaterial() ? 8.0 : 3.0; |
| 23 } | 24 } |
| 24 | 25 |
| 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 |
| 28 // padding in location_bar_view from 8px. |
| 29 CGFloat DividerPadding() { |
| 30 return ui::MaterialDesignController::IsModeMaterial() ? 3.0 : 0.0; |
| 31 } |
| 25 | 32 |
| 26 // Padding between the icon and label. | 33 // Padding between the icon and label. |
| 27 const CGFloat kIconLabelPadding = 4.0; | 34 const CGFloat kIconLabelPadding = 4.0; |
| 28 | 35 |
| 29 // Inset for the background. | 36 // Inset for the background. |
| 30 const CGFloat kBackgroundYInset = 4.0; | 37 const CGFloat kBackgroundYInset = 4.0; |
| 31 | 38 |
| 32 } // namespace | 39 } // namespace |
| 33 | 40 |
| 34 BubbleDecoration::BubbleDecoration() : baseline_offset_(0) { | 41 BubbleDecoration::BubbleDecoration() : baseline_offset_(0) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 47 | 54 |
| 48 const CGFloat image_width = image ? [image size].width : 0.0; | 55 const CGFloat image_width = image ? [image size].width : 0.0; |
| 49 if (!label) | 56 if (!label) |
| 50 return BubblePadding() + image_width; | 57 return BubblePadding() + image_width; |
| 51 | 58 |
| 52 // The bubble needs to take up an integral number of pixels. | 59 // The bubble needs to take up an integral number of pixels. |
| 53 // Generally -sizeWithAttributes: seems to overestimate rather than | 60 // Generally -sizeWithAttributes: seems to overestimate rather than |
| 54 // underestimate, so floor() seems to work better. | 61 // underestimate, so floor() seems to work better. |
| 55 const CGFloat label_width = | 62 const CGFloat label_width = |
| 56 std::floor([label sizeWithAttributes:attributes_].width); | 63 std::floor([label sizeWithAttributes:attributes_].width); |
| 57 return BubblePadding() + image_width + kIconLabelPadding + label_width; | 64 return BubblePadding() + image_width + kIconLabelPadding + label_width + |
| 65 DividerPadding(); |
| 58 } | 66 } |
| 59 | 67 |
| 60 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { | 68 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { |
| 61 NSRect imageRect = NSInsetRect(frame, 0.0, kBackgroundYInset); | 69 NSRect imageRect = NSInsetRect(frame, 0.0, kBackgroundYInset); |
| 62 if (image_) { | 70 if (image_) { |
| 63 // Center the image vertically. | 71 // Center the image vertically. |
| 64 const NSSize imageSize = [image_ size]; | 72 const NSSize imageSize = [image_ size]; |
| 65 imageRect.origin.y += | 73 imageRect.origin.y += |
| 66 std::floor((NSHeight(frame) - imageSize.height) / 2.0); | 74 std::floor((NSHeight(frame) - imageSize.height) / 2.0); |
| 67 imageRect.size = imageSize; | 75 imageRect.size = imageSize; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 93 imageRect.size = imageSize; | 101 imageRect.size = imageSize; |
| 94 [image_ drawInRect:imageRect | 102 [image_ drawInRect:imageRect |
| 95 fromRect:NSZeroRect // Entire image | 103 fromRect:NSZeroRect // Entire image |
| 96 operation:NSCompositeSourceOver | 104 operation:NSCompositeSourceOver |
| 97 fraction:1.0 | 105 fraction:1.0 |
| 98 respectFlipped:YES | 106 respectFlipped:YES |
| 99 hints:nil]; | 107 hints:nil]; |
| 100 textOffset = NSMaxX(imageRect) + kIconLabelPadding; | 108 textOffset = NSMaxX(imageRect) + kIconLabelPadding; |
| 101 } | 109 } |
| 102 | 110 |
| 111 // Draw the divider and set the text color. |
| 112 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 113 NSBezierPath* line = [NSBezierPath bezierPath]; |
| 114 [line setLineWidth:1]; |
| 115 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 116 NSMinY(decoration_frame))]; |
| 117 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 118 NSMaxY(decoration_frame))]; |
| 119 |
| 120 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 121 [GetDividerColor(in_dark_mode) set]; |
| 122 [line stroke]; |
| 123 |
| 124 NSColor* text_color = |
| 125 in_dark_mode |
| 126 ? skia::SkColorToCalibratedNSColor(kMaterialDarkModeTextColor) |
| 127 : GetBackgroundBorderColor(); |
| 128 SetTextColor(text_color); |
| 129 } |
| 130 |
| 103 if (label_) { | 131 if (label_) { |
| 104 NSRect textRect = frame; | 132 NSRect textRect = frame; |
| 105 textRect.origin.x = textOffset; | 133 textRect.origin.x = textOffset; |
| 106 textRect.origin.y += baseline_offset_; | 134 textRect.origin.y += baseline_offset_; |
| 107 textRect.size.width = NSMaxX(decoration_frame) - NSMinX(textRect); | 135 textRect.size.width = NSMaxX(decoration_frame) - NSMinX(textRect); |
| 108 DrawLabel(label_, attributes_, textRect); | 136 DrawLabel(label_, attributes_, textRect); |
| 109 } | 137 } |
| 110 } | 138 } |
| 111 | 139 |
| 112 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, | 140 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, |
| 113 NSRect frame, | 141 NSRect frame, |
| 114 NSView* control_view) { | 142 NSView* control_view) { |
| 115 NSRect rect = NSInsetRect(background_frame, 0, 1); | 143 NSRect rect = NSInsetRect(background_frame, 0, 1); |
| 116 rect.size.width -= kRightSideMargin; | 144 rect.size.width -= kRightSideMargin; |
| 117 if (ui::MaterialDesignController::IsModeMaterial()) { | 145 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 118 CGFloat lineWidth = [control_view cr_lineWidth]; | |
| 119 rect = NSInsetRect(rect, lineWidth / 2., lineWidth / 2.); | |
| 120 NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:rect | |
| 121 xRadius:3 | |
| 122 yRadius:3]; | |
| 123 [path setLineWidth:lineWidth]; | |
| 124 bool inDarkMode = [[control_view window] inIncognitoModeWithSystemTheme]; | |
| 125 if (inDarkMode) { | |
| 126 [[NSColor whiteColor] set]; | |
| 127 [path fill]; | |
| 128 } else { | |
| 129 [GetBackgroundBorderColor() set]; | |
| 130 [path stroke]; | |
| 131 } | |
| 132 } else { | |
| 133 ui::DrawNinePartImage( | 146 ui::DrawNinePartImage( |
| 134 rect, GetBubbleImageIds(), NSCompositeSourceOver, 1.0, true); | 147 rect, GetBubbleImageIds(), NSCompositeSourceOver, 1.0, true); |
| 135 } | 148 } |
| 136 | 149 |
| 137 DrawInFrame(frame, control_view); | 150 DrawInFrame(frame, control_view); |
| 138 } | 151 } |
| 139 | 152 |
| 140 NSFont* BubbleDecoration::GetFont() const { | 153 NSFont* BubbleDecoration::GetFont() const { |
| 141 return [attributes_ objectForKey:NSFontAttributeName]; | 154 return [attributes_ objectForKey:NSFontAttributeName]; |
| 142 } | 155 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 161 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 174 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 162 } | 175 } |
| 163 | 176 |
| 164 void BubbleDecoration::SetFont(NSFont* font) { | 177 void BubbleDecoration::SetFont(NSFont* font) { |
| 165 [attributes_ setObject:font forKey:NSFontAttributeName]; | 178 [attributes_ setObject:font forKey:NSFontAttributeName]; |
| 166 } | 179 } |
| 167 | 180 |
| 168 void BubbleDecoration::SetBaselineOffset(CGFloat offset) { | 181 void BubbleDecoration::SetBaselineOffset(CGFloat offset) { |
| 169 baseline_offset_ = offset; | 182 baseline_offset_ = offset; |
| 170 } | 183 } |
| OLD | NEW |