| 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 |
| 25 // Additional padding between the divider between the omnibox text and the | 26 // Additional padding between the divider between the omnibox text and the |
| 26 // 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 |
| 27 // padding in location_bar_view from 8px. | 28 // padding in location_bar_view from 8px. |
| 28 CGFloat DividerPadding() { | 29 CGFloat DividerPadding() { |
| 29 return ui::MaterialDesignController::IsModeMaterial() ? 2.0 : 0.0; | 30 return ui::MaterialDesignController::IsModeMaterial() ? 2.0 : 0.0; |
| 30 } | 31 } |
| 31 | 32 |
| 32 // Padding between the icon and label. | 33 // Padding between the icon and label. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 53 | 54 |
| 54 const CGFloat image_width = image ? [image size].width : 0.0; | 55 const CGFloat image_width = image ? [image size].width : 0.0; |
| 55 if (!label) | 56 if (!label) |
| 56 return BubblePadding() + image_width; | 57 return BubblePadding() + image_width; |
| 57 | 58 |
| 58 // The bubble needs to take up an integral number of pixels. | 59 // The bubble needs to take up an integral number of pixels. |
| 59 // Generally -sizeWithAttributes: seems to overestimate rather than | 60 // Generally -sizeWithAttributes: seems to overestimate rather than |
| 60 // underestimate, so floor() seems to work better. | 61 // underestimate, so floor() seems to work better. |
| 61 const CGFloat label_width = | 62 const CGFloat label_width = |
| 62 std::floor([label sizeWithAttributes:attributes_].width); | 63 std::floor([label sizeWithAttributes:attributes_].width); |
| 63 return BubblePadding() + image_width + kIconLabelPadding + label_width | 64 return BubblePadding() + image_width + kIconLabelPadding + label_width + |
| 64 + DividerPadding(); | 65 DividerPadding(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { | 68 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { |
| 68 NSRect imageRect = NSInsetRect(frame, 0.0, kBackgroundYInset); | 69 NSRect imageRect = NSInsetRect(frame, 0.0, kBackgroundYInset); |
| 69 if (image_) { | 70 if (image_) { |
| 70 // Center the image vertically. | 71 // Center the image vertically. |
| 71 const NSSize imageSize = [image_ size]; | 72 const NSSize imageSize = [image_ size]; |
| 72 | 73 |
| 73 imageRect.origin.y += | 74 imageRect.origin.y += |
| 74 std::floor((NSHeight(frame) - imageSize.height) / 2.0); | 75 std::floor((NSHeight(frame) - imageSize.height) / 2.0); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 101 imageRect.size = imageSize; | 102 imageRect.size = imageSize; |
| 102 [image_ drawInRect:imageRect | 103 [image_ drawInRect:imageRect |
| 103 fromRect:NSZeroRect // Entire image | 104 fromRect:NSZeroRect // Entire image |
| 104 operation:NSCompositeSourceOver | 105 operation:NSCompositeSourceOver |
| 105 fraction:1.0 | 106 fraction:1.0 |
| 106 respectFlipped:YES | 107 respectFlipped:YES |
| 107 hints:nil]; | 108 hints:nil]; |
| 108 textOffset = NSMaxX(imageRect) + kIconLabelPadding; | 109 textOffset = NSMaxX(imageRect) + kIconLabelPadding; |
| 109 } | 110 } |
| 110 | 111 |
| 112 // Draw the divider and set the text color. |
| 113 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 114 NSBezierPath* line = [NSBezierPath bezierPath]; |
| 115 [line setLineWidth:1]; |
| 116 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 117 NSMinY(decoration_frame))]; |
| 118 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 119 NSMaxY(decoration_frame))]; |
| 120 |
| 121 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 122 [GetDividerColor(in_dark_mode) set]; |
| 123 [line stroke]; |
| 124 |
| 125 NSColor* text_color = |
| 126 in_dark_mode |
| 127 ? skia::SkColorToCalibratedNSColor(kMaterialDarkModeTextColor) |
| 128 : GetBackgroundBorderColor(); |
| 129 SetTextColor(text_color); |
| 130 } |
| 131 |
| 111 if (label_) { | 132 if (label_) { |
| 112 NSRect textRect = frame; | 133 NSRect textRect = frame; |
| 113 textRect.origin.x = textOffset; | 134 textRect.origin.x = textOffset; |
| 114 textRect.origin.y += baseline_offset_; | 135 textRect.origin.y += baseline_offset_; |
| 115 textRect.size.width = NSMaxX(decoration_frame) - NSMinX(textRect); | 136 textRect.size.width = NSMaxX(decoration_frame) - NSMinX(textRect); |
| 116 DrawLabel(label_, attributes_, textRect); | 137 DrawLabel(label_, attributes_, textRect); |
| 117 } | 138 } |
| 118 } | 139 } |
| 119 | 140 |
| 120 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, | 141 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, |
| 121 NSRect frame, | 142 NSRect frame, |
| 122 NSView* control_view) { | 143 NSView* control_view) { |
| 123 NSRect rect = NSInsetRect(background_frame, 0, 1); | 144 NSRect rect = NSInsetRect(background_frame, 0, 1); |
| 124 rect.size.width -= kRightSideMargin; | 145 rect.size.width -= kRightSideMargin; |
| 125 if (ui::MaterialDesignController::IsModeMaterial()) { | 146 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 126 CGFloat lineWidth = [control_view cr_lineWidth]; | |
| 127 rect = NSInsetRect(rect, lineWidth / 2., lineWidth / 2.); | |
| 128 NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:rect | |
| 129 xRadius:3 | |
| 130 yRadius:3]; | |
| 131 [path setLineWidth:lineWidth]; | |
| 132 bool inDarkMode = [[control_view window] inIncognitoModeWithSystemTheme]; | |
| 133 if (inDarkMode) { | |
| 134 [[NSColor whiteColor] set]; | |
| 135 [path fill]; | |
| 136 } else { | |
| 137 [GetBackgroundBorderColor() set]; | |
| 138 [path stroke]; | |
| 139 } | |
| 140 } else { | |
| 141 ui::DrawNinePartImage( | 147 ui::DrawNinePartImage( |
| 142 rect, GetBubbleImageIds(), NSCompositeSourceOver, 1.0, true); | 148 rect, GetBubbleImageIds(), NSCompositeSourceOver, 1.0, true); |
| 143 } | 149 } |
| 144 | 150 |
| 145 DrawInFrame(frame, control_view); | 151 DrawInFrame(frame, control_view); |
| 146 } | 152 } |
| 147 | 153 |
| 148 NSFont* BubbleDecoration::GetFont() const { | 154 NSFont* BubbleDecoration::GetFont() const { |
| 149 return [attributes_ objectForKey:NSFontAttributeName]; | 155 return [attributes_ objectForKey:NSFontAttributeName]; |
| 150 } | 156 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 169 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 175 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 170 } | 176 } |
| 171 | 177 |
| 172 void BubbleDecoration::SetFont(NSFont* font) { | 178 void BubbleDecoration::SetFont(NSFont* font) { |
| 173 [attributes_ setObject:font forKey:NSFontAttributeName]; | 179 [attributes_ setObject:font forKey:NSFontAttributeName]; |
| 174 } | 180 } |
| 175 | 181 |
| 176 void BubbleDecoration::SetBaselineOffset(CGFloat offset) { | 182 void BubbleDecoration::SetBaselineOffset(CGFloat offset) { |
| 177 baseline_offset_ = offset; | 183 baseline_offset_ = offset; |
| 178 } | 184 } |
| OLD | NEW |