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 ui::MaterialDesignController::IsModeMaterial() ? 8.0 : 3.0; |
25 } | 25 } |
26 | 26 |
27 // Additional padding between the divider between the omnibox text and the | 27 const CGFloat kDividerPadding = 2.0; |
28 // divider. The desired value is 8px. We get 3px by subtracting the existing | |
29 // padding in location_bar_view from 8px. | |
30 CGFloat DividerPadding() { | |
31 return ui::MaterialDesignController::IsModeMaterial() ? 2.0 : 0.0; | |
32 } | |
33 | 28 |
34 // Padding between the icon and label. | 29 // Padding between the icon and label. |
35 CGFloat kIconLabelPadding = 4.0; | 30 CGFloat kIconLabelPadding = 4.0; |
36 | 31 |
37 // Inset for the background. | 32 // Inset for the background. |
38 const CGFloat kBackgroundYInset = 4.0; | 33 const CGFloat kBackgroundYInset = 4.0; |
39 | 34 |
40 } // namespace | 35 } // namespace |
41 | 36 |
42 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { | 37 BubbleDecoration::BubbleDecoration() : retina_baseline_offset_(0) { |
43 attributes_.reset([[NSMutableDictionary alloc] init]); | 38 attributes_.reset([[NSMutableDictionary alloc] init]); |
44 [attributes_ setObject:LocationBarDecoration::GetFont() | 39 [attributes_ setObject:LocationBarDecoration::GetFont() |
45 forKey:NSFontAttributeName]; | 40 forKey:NSFontAttributeName]; |
| 41 |
| 42 base::scoped_nsobject<NSMutableParagraphStyle> style( |
| 43 [[NSMutableParagraphStyle alloc] init]); |
| 44 [style setLineBreakMode:NSLineBreakByClipping]; |
| 45 [attributes_ setObject:style forKey:NSParagraphStyleAttributeName]; |
46 } | 46 } |
47 | 47 |
48 BubbleDecoration::~BubbleDecoration() { | 48 BubbleDecoration::~BubbleDecoration() { |
49 } | 49 } |
50 | 50 |
| 51 // Additional padding between the divider between the omnibox text and the |
| 52 // divider. The desired value is 8px. We get 3px by subtracting the existing |
| 53 // padding in location_bar_view from 8px. |
| 54 CGFloat BubbleDecoration::DividerPadding() const { |
| 55 return ui::MaterialDesignController::IsModeMaterial() ? kDividerPadding : 0.0; |
| 56 } |
| 57 |
51 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, | 58 CGFloat BubbleDecoration::GetWidthForImageAndLabel(NSImage* image, |
52 NSString* label) { | 59 NSString* label) { |
53 if (!image && !label) | 60 if (!image && !label) |
54 return kOmittedWidth; | 61 return kOmittedWidth; |
55 | 62 |
56 const CGFloat image_width = image ? [image size].width : 0.0; | 63 const CGFloat image_width = image ? [image size].width : 0.0; |
57 if (!label) | 64 if (!label) |
58 return BubblePadding() + image_width; | 65 return BubblePadding() + image_width; |
59 | 66 |
60 // The bubble needs to take up an integral number of pixels. | 67 // The bubble needs to take up an integral number of pixels. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 textRect.size.width = NSMaxX(decoration_frame) - NSMinX(textRect); | 143 textRect.size.width = NSMaxX(decoration_frame) - NSMinX(textRect); |
137 // Transform the coordinate system to adjust the baseline on Retina. This is | 144 // Transform the coordinate system to adjust the baseline on Retina. This is |
138 // the only way to get fractional adjustments. | 145 // the only way to get fractional adjustments. |
139 gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; | 146 gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; |
140 CGFloat lineWidth = [control_view cr_lineWidth]; | 147 CGFloat lineWidth = [control_view cr_lineWidth]; |
141 if (lineWidth < 1) { | 148 if (lineWidth < 1) { |
142 NSAffineTransform* transform = [NSAffineTransform transform]; | 149 NSAffineTransform* transform = [NSAffineTransform transform]; |
143 [transform translateXBy:0 yBy:retina_baseline_offset_]; | 150 [transform translateXBy:0 yBy:retina_baseline_offset_]; |
144 [transform concat]; | 151 [transform concat]; |
145 } | 152 } |
146 DrawLabel(label_, attributes_, textRect); | 153 |
| 154 base::scoped_nsobject<NSAttributedString> str([[NSAttributedString alloc] |
| 155 initWithString:label_ |
| 156 attributes:attributes_]); |
| 157 |
| 158 DrawAttributedString(str, textRect); |
147 } | 159 } |
148 } | 160 } |
149 | 161 |
150 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, | 162 void BubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, |
151 NSRect frame, | 163 NSRect frame, |
152 NSView* control_view) { | 164 NSView* control_view) { |
153 NSRect rect = NSInsetRect(background_frame, 0, 1); | 165 NSRect rect = NSInsetRect(background_frame, 0, 1); |
154 rect.size.width -= kRightSideMargin; | 166 rect.size.width -= kRightSideMargin; |
155 if (!ui::MaterialDesignController::IsModeMaterial()) { | 167 if (!ui::MaterialDesignController::IsModeMaterial()) { |
156 ui::DrawNinePartImage( | 168 ui::DrawNinePartImage( |
(...skipping 27 matching lines...) Expand all Loading... |
184 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 196 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
185 } | 197 } |
186 | 198 |
187 void BubbleDecoration::SetFont(NSFont* font) { | 199 void BubbleDecoration::SetFont(NSFont* font) { |
188 [attributes_ setObject:font forKey:NSFontAttributeName]; | 200 [attributes_ setObject:font forKey:NSFontAttributeName]; |
189 } | 201 } |
190 | 202 |
191 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { | 203 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { |
192 retina_baseline_offset_ = offset; | 204 retina_baseline_offset_ = offset; |
193 } | 205 } |
OLD | NEW |