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 #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 CGFloat BubblePadding() { |
| 24 return ui::MaterialDesignController::IsModeMaterial() ? 8.0 : 3.0; | 24 return 8.0; |
| 25 } | 25 } |
|
Avi (use Gerrit)
2016/09/28 16:01:58
return to being a const CGFloat like lines 20, 28,
Elly Fong-Jones
2016/09/28 19:02:27
Done.
| |
| 26 | 26 |
| 27 // Additional padding between the divider and the label. | 27 // Additional padding between the divider and the label. |
| 28 const CGFloat kDividerPadding = 2.0; | 28 const CGFloat kDividerPadding = 2.0; |
| 29 | 29 |
| 30 // Padding between the icon and label. | 30 // Padding between the icon and label. |
| 31 CGFloat kIconLabelPadding = 4.0; | 31 CGFloat kIconLabelPadding = 4.0; |
| 32 | 32 |
| 33 // Inset for the background. | 33 // Inset for the background. |
| 34 const CGFloat kBackgroundYInset = 4.0; | 34 const CGFloat kBackgroundYInset = 4.0; |
| 35 | 35 |
| 36 } // namespace | 36 } // namespace |
| 37 | 37 |
| 38 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { | 38 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { |
| 39 attributes_.reset([[NSMutableDictionary alloc] init]); | 39 attributes_.reset([[NSMutableDictionary alloc] init]); |
| 40 [attributes_ setObject:LocationBarDecoration::GetFont() | 40 [attributes_ setObject:LocationBarDecoration::GetFont() |
| 41 forKey:NSFontAttributeName]; | 41 forKey:NSFontAttributeName]; |
| 42 } | 42 } |
| 43 | 43 |
| 44 BubbleDecoration::~BubbleDecoration() { | 44 BubbleDecoration::~BubbleDecoration() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 CGFloat BubbleDecoration::DividerPadding() const { | 47 CGFloat BubbleDecoration::DividerPadding() const { |
| 48 return ui::MaterialDesignController::IsModeMaterial() ? kDividerPadding : 0.0; | 48 return kDividerPadding; |
| 49 } | 49 } |
| 50 | 50 |
| 51 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, | 51 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, |
| 52 NSString* label) { | 52 NSString* label) { |
| 53 if (!image && !label) | 53 if (!image && !label) |
| 54 return kOmittedWidth; | 54 return kOmittedWidth; |
| 55 | 55 |
| 56 const CGFloat image_width = image ? [image size].width : 0.0; | 56 const CGFloat image_width = image ? [image size].width : 0.0; |
| 57 if (!label) | 57 if (!label) |
| 58 return BubblePadding() + image_width; | 58 return BubblePadding() + image_width; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 [image_ drawInRect:image_rect | 108 [image_ drawInRect:image_rect |
| 109 fromRect:NSZeroRect // Entire image | 109 fromRect:NSZeroRect // Entire image |
| 110 operation:NSCompositeSourceOver | 110 operation:NSCompositeSourceOver |
| 111 fraction:1.0 | 111 fraction:1.0 |
| 112 respectFlipped:YES | 112 respectFlipped:YES |
| 113 hints:nil]; | 113 hints:nil]; |
| 114 text_offset = NSMaxX(image_rect) + kIconLabelPadding; | 114 text_offset = NSMaxX(image_rect) + kIconLabelPadding; |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Draw the divider and set the text color. | 117 // Draw the divider and set the text color. |
| 118 if (ui::MaterialDesignController::IsModeMaterial()) { | 118 NSBezierPath* line = [NSBezierPath bezierPath]; |
| 119 NSBezierPath* line = [NSBezierPath bezierPath]; | 119 [line setLineWidth:1]; |
| 120 [line setLineWidth:1]; | 120 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 121 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), | 121 NSMinY(decoration_frame))]; |
| 122 NSMinY(decoration_frame))]; | 122 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 123 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), | 123 NSMaxY(decoration_frame))]; |
| 124 NSMaxY(decoration_frame))]; | |
| 125 | 124 |
| 126 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; | 125 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 127 [GetDividerColor(in_dark_mode) set]; | 126 [GetDividerColor(in_dark_mode) set]; |
| 128 [line stroke]; | 127 [line stroke]; |
| 129 | 128 |
| 130 NSColor* text_color = | 129 NSColor* text_color = |
| 131 in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor(); | 130 in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor(); |
| 132 SetTextColor(text_color); | 131 SetTextColor(text_color); |
| 133 } | |
| 134 | 132 |
| 135 if (label_) { | 133 if (label_) { |
| 136 NSRect text_rect = frame; | 134 NSRect text_rect = frame; |
| 137 text_rect.origin.x = text_offset; | 135 text_rect.origin.x = text_offset; |
| 138 text_rect.size.width = NSMaxX(decoration_frame) - NSMinX(text_rect); | 136 text_rect.size.width = NSMaxX(decoration_frame) - NSMinX(text_rect); |
| 139 // Transform the coordinate system to adjust the baseline on Retina. This is | 137 // Transform the coordinate system to adjust the baseline on Retina. This is |
| 140 // the only way to get fractional adjustments. | 138 // the only way to get fractional adjustments. |
| 141 gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; | 139 gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; |
| 142 CGFloat line_width = [control_view cr_lineWidth]; | 140 CGFloat line_width = [control_view cr_lineWidth]; |
| 143 if (line_width < 1) { | 141 if (line_width < 1) { |
| 144 NSAffineTransform* transform = [NSAffineTransform transform]; | 142 NSAffineTransform* transform = [NSAffineTransform transform]; |
| 145 [transform translateXBy:0 yBy:retina_baseline_offset_]; | 143 [transform translateXBy:0 yBy:retina_baseline_offset_]; |
| 146 [transform concat]; | 144 [transform concat]; |
| 147 } | 145 } |
| 148 DrawLabel(label_, attributes_, text_rect); | 146 DrawLabel(label_, attributes_, text_rect); |
| 149 } | 147 } |
| 150 } | 148 } |
| 151 | 149 |
| 152 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, | 150 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, |
| 153 NSRect frame, | 151 NSRect frame, |
| 154 NSView* control_view) { | 152 NSView* control_view) { |
| 155 NSRect rect = NSInsetRect(background_frame, 0, 1); | 153 NSRect rect = NSInsetRect(background_frame, 0, 1); |
| 156 rect.size.width -= kRightSideMargin; | 154 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); | 155 DrawInFrame(frame, control_view); |
| 163 } | 156 } |
| 164 | 157 |
| 165 NSFont* BubbleDecoration::GetFont() const { | 158 NSFont* BubbleDecoration::GetFont() const { |
| 166 return [attributes_ objectForKey:NSFontAttributeName]; | 159 return [attributes_ objectForKey:NSFontAttributeName]; |
| 167 } | 160 } |
| 168 | 161 |
| 169 NSImage* BubbleDecoration::GetImage() { | 162 NSImage* BubbleDecoration::GetImage() { |
| 170 return image_; | 163 return image_; |
| 171 } | 164 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 186 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 179 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
| 187 } | 180 } |
| 188 | 181 |
| 189 void BubbleDecoration::SetFont(NSFont* font) { | 182 void BubbleDecoration::SetFont(NSFont* font) { |
| 190 [attributes_ setObject:font forKey:NSFontAttributeName]; | 183 [attributes_ setObject:font forKey:NSFontAttributeName]; |
| 191 } | 184 } |
| 192 | 185 |
| 193 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { | 186 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { |
| 194 retina_baseline_offset_ = offset; | 187 retina_baseline_offset_ = offset; |
| 195 } | 188 } |
| OLD | NEW |