| 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 kRightSideMargin = 1.0; |
| 21 | 21 |
| 22 // Padding between the icon/label and bubble edges. | 22 // Padding between the icon/label and bubble edges. |
| 23 CGFloat BubblePadding() { | 23 const CGFloat kBubblePadding = 8.0; |
| 24 return ui::MaterialDesignController::IsModeMaterial() ? 8.0 : 3.0; | |
| 25 } | |
| 26 | 24 |
| 27 // Additional padding between the divider and the label. | 25 // Additional padding between the divider and the label. |
| 28 const CGFloat kDividerPadding = 2.0; | 26 const CGFloat kDividerPadding = 2.0; |
| 29 | 27 |
| 30 // Padding between the icon and label. | 28 // Padding between the icon and label. |
| 31 CGFloat kIconLabelPadding = 4.0; | 29 const CGFloat kIconLabelPadding = 4.0; |
| 32 | 30 |
| 33 // Inset for the background. | 31 // Inset for the background. |
| 34 const CGFloat kBackgroundYInset = 4.0; | 32 const CGFloat kBackgroundYInset = 4.0; |
| 35 | 33 |
| 36 } // namespace | 34 } // namespace |
| 37 | 35 |
| 38 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { | 36 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { |
| 39 attributes_.reset([[NSMutableDictionary alloc] init]); | 37 attributes_.reset([[NSMutableDictionary alloc] init]); |
| 40 [attributes_ setObject:LocationBarDecoration::GetFont() | 38 [attributes_ setObject:LocationBarDecoration::GetFont() |
| 41 forKey:NSFontAttributeName]; | 39 forKey:NSFontAttributeName]; |
| 42 } | 40 } |
| 43 | 41 |
| 44 BubbleDecoration::~BubbleDecoration() { | 42 BubbleDecoration::~BubbleDecoration() { |
| 45 } | 43 } |
| 46 | 44 |
| 47 CGFloat BubbleDecoration::DividerPadding() const { | 45 CGFloat BubbleDecoration::DividerPadding() const { |
| 48 return ui::MaterialDesignController::IsModeMaterial() ? kDividerPadding : 0.0; | 46 return kDividerPadding; |
| 49 } | 47 } |
| 50 | 48 |
| 51 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, | 49 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, |
| 52 NSString* label) { | 50 NSString* label) { |
| 53 if (!image && !label) | 51 if (!image && !label) |
| 54 return kOmittedWidth; | 52 return kOmittedWidth; |
| 55 | 53 |
| 56 const CGFloat image_width = image ? [image size].width : 0.0; | 54 const CGFloat image_width = image ? [image size].width : 0.0; |
| 57 if (!label) | 55 if (!label) |
| 58 return BubblePadding() + image_width; | 56 return kBubblePadding + image_width; |
| 59 | 57 |
| 60 // The bubble needs to take up an integral number of pixels. | 58 // The bubble needs to take up an integral number of pixels. |
| 61 // Generally -sizeWithAttributes: seems to overestimate rather than | 59 // Generally -sizeWithAttributes: seems to overestimate rather than |
| 62 // underestimate, so floor() seems to work better. | 60 // underestimate, so floor() seems to work better. |
| 63 const CGFloat label_width = | 61 const CGFloat label_width = |
| 64 std::floor([label sizeWithAttributes:attributes_].width); | 62 std::floor([label sizeWithAttributes:attributes_].width); |
| 65 return BubblePadding() + image_width + kIconLabelPadding + label_width + | 63 return kBubblePadding + image_width + kIconLabelPadding + label_width + |
| 66 DividerPadding(); | 64 DividerPadding(); |
| 67 } | 65 } |
| 68 | 66 |
| 69 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { | 67 NSRect BubbleDecoration::GetImageRectInFrame(NSRect frame) { |
| 70 NSRect image_rect = NSInsetRect(frame, 0.0, kBackgroundYInset); | 68 NSRect image_rect = NSInsetRect(frame, 0.0, kBackgroundYInset); |
| 71 if (image_) { | 69 if (image_) { |
| 72 // Center the image vertically. | 70 // Center the image vertically. |
| 73 const NSSize image_size = [image_ size]; | 71 const NSSize image_size = [image_ size]; |
| 74 | 72 |
| 75 image_rect.origin.y += | 73 image_rect.origin.y += |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 [image_ drawInRect:image_rect | 106 [image_ drawInRect:image_rect |
| 109 fromRect:NSZeroRect // Entire image | 107 fromRect:NSZeroRect // Entire image |
| 110 operation:NSCompositeSourceOver | 108 operation:NSCompositeSourceOver |
| 111 fraction:1.0 | 109 fraction:1.0 |
| 112 respectFlipped:YES | 110 respectFlipped:YES |
| 113 hints:nil]; | 111 hints:nil]; |
| 114 text_offset = NSMaxX(image_rect) + kIconLabelPadding; | 112 text_offset = NSMaxX(image_rect) + kIconLabelPadding; |
| 115 } | 113 } |
| 116 | 114 |
| 117 // Draw the divider and set the text color. | 115 // Draw the divider and set the text color. |
| 118 if (ui::MaterialDesignController::IsModeMaterial()) { | 116 NSBezierPath* line = [NSBezierPath bezierPath]; |
| 119 NSBezierPath* line = [NSBezierPath bezierPath]; | 117 [line setLineWidth:1]; |
| 120 [line setLineWidth:1]; | 118 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 121 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), | 119 NSMinY(decoration_frame))]; |
| 122 NSMinY(decoration_frame))]; | 120 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 123 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), | 121 NSMaxY(decoration_frame))]; |
| 124 NSMaxY(decoration_frame))]; | |
| 125 | 122 |
| 126 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; | 123 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 127 [GetDividerColor(in_dark_mode) set]; | 124 [GetDividerColor(in_dark_mode) set]; |
| 128 [line stroke]; | 125 [line stroke]; |
| 129 | 126 |
| 130 NSColor* text_color = | 127 NSColor* text_color = |
| 131 in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor(); | 128 in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor(); |
| 132 SetTextColor(text_color); | 129 SetTextColor(text_color); |
| 133 } | |
| 134 | 130 |
| 135 if (label_) { | 131 if (label_) { |
| 136 NSRect text_rect = frame; | 132 NSRect text_rect = frame; |
| 137 text_rect.origin.x = text_offset; | 133 text_rect.origin.x = text_offset; |
| 138 text_rect.size.width = NSMaxX(decoration_frame) - NSMinX(text_rect); | 134 text_rect.size.width = NSMaxX(decoration_frame) - NSMinX(text_rect); |
| 139 // Transform the coordinate system to adjust the baseline on Retina. This is | 135 // Transform the coordinate system to adjust the baseline on Retina. This is |
| 140 // the only way to get fractional adjustments. | 136 // the only way to get fractional adjustments. |
| 141 gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; | 137 gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; |
| 142 CGFloat line_width = [control_view cr_lineWidth]; | 138 CGFloat line_width = [control_view cr_lineWidth]; |
| 143 if (line_width < 1) { | 139 if (line_width < 1) { |
| 144 NSAffineTransform* transform = [NSAffineTransform transform]; | 140 NSAffineTransform* transform = [NSAffineTransform transform]; |
| 145 [transform translateXBy:0 yBy:retina_baseline_offset_]; | 141 [transform translateXBy:0 yBy:retina_baseline_offset_]; |
| 146 [transform concat]; | 142 [transform concat]; |
| 147 } | 143 } |
| 148 DrawLabel(label_, attributes_, text_rect); | 144 DrawLabel(label_, attributes_, text_rect); |
| 149 } | 145 } |
| 150 } | 146 } |
| 151 | 147 |
| 152 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, | 148 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, |
| 153 NSRect frame, | 149 NSRect frame, |
| 154 NSView* control_view) { | 150 NSView* control_view) { |
| 155 NSRect rect = NSInsetRect(background_frame, 0, 1); | 151 NSRect rect = NSInsetRect(background_frame, 0, 1); |
| 156 rect.size.width -= kRightSideMargin; | 152 rect.size.width -= kRightSideMargin; |
| 157 if (!ui::MaterialDesignController::IsModeMaterial()) { | |
| 158 ui::DrawNinePartImage( | |
| 159 rect, GetBubbleImageIds(), NSCompositeSourceOver, 1.0, true); | |
| 160 } | |
| 161 | |
| 162 DrawInFrame(frame, control_view); | 153 DrawInFrame(frame, control_view); |
| 163 } | 154 } |
| 164 | 155 |
| 165 NSFont* BubbleDecoration::GetFont() const { | 156 NSFont* BubbleDecoration::GetFont() const { |
| 166 return [attributes_ objectForKey:NSFontAttributeName]; | 157 return [attributes_ objectForKey:NSFontAttributeName]; |
| 167 } | 158 } |
| 168 | 159 |
| 169 NSImage* BubbleDecoration::GetImage() { | 160 NSImage* BubbleDecoration::GetImage() { |
| 170 return image_; | 161 return image_; |
| 171 } | 162 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 186 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 177 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 187 } | 178 } |
| 188 | 179 |
| 189 void BubbleDecoration::SetFont(NSFont* font) { | 180 void BubbleDecoration::SetFont(NSFont* font) { |
| 190 [attributes_ setObject:font forKey:NSFontAttributeName]; | 181 [attributes_ setObject:font forKey:NSFontAttributeName]; |
| 191 } | 182 } |
| 192 | 183 |
| 193 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { | 184 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { |
| 194 retina_baseline_offset_ = offset; | 185 retina_baseline_offset_ = offset; |
| 195 } | 186 } |
| OLD | NEW |