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 #include "chrome/browser/ui/cocoa/l10n_util.h" |
11 #import "chrome/browser/ui/cocoa/themed_window.h" | 12 #import "chrome/browser/ui/cocoa/themed_window.h" |
12 #include "skia/ext/skia_utils_mac.h" | 13 #include "skia/ext/skia_utils_mac.h" |
13 #import "ui/base/cocoa/nsview_additions.h" | 14 #import "ui/base/cocoa/nsview_additions.h" |
14 #include "ui/base/material_design/material_design_controller.h" | 15 #include "ui/base/material_design/material_design_controller.h" |
15 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 16 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 // This is used to increase the left padding of this decoration. | 20 // This is used to increase the left padding of this decoration. |
20 const CGFloat kLeftSidePadding = 5.0; | 21 const CGFloat kLeftSidePadding = 5.0; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 102 |
102 NSRect BubbleDecoration::GetBackgroundFrame(NSRect frame) { | 103 NSRect BubbleDecoration::GetBackgroundFrame(NSRect frame) { |
103 NSRect background_frame = NSInsetRect(frame, 0.0, kBackgroundFrameYInset); | 104 NSRect background_frame = NSInsetRect(frame, 0.0, kBackgroundFrameYInset); |
104 background_frame.origin.x += kBackgroundFrameLeftMargin; | 105 background_frame.origin.x += kBackgroundFrameLeftMargin; |
105 background_frame.size.width -= kDividerPadding; | 106 background_frame.size.width -= kDividerPadding; |
106 return background_frame; | 107 return background_frame; |
107 } | 108 } |
108 | 109 |
109 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { | 110 void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { |
110 const NSRect decoration_frame = NSInsetRect(frame, 0.0, kImageFrameYInset); | 111 const NSRect decoration_frame = NSInsetRect(frame, 0.0, kImageFrameYInset); |
111 CGFloat text_offset = NSMinX(decoration_frame); | 112 CGFloat text_left_offset = NSMinX(decoration_frame); |
| 113 CGFloat text_right_offset = NSMaxX(decoration_frame); |
| 114 const BOOL is_rtl = cocoa_l10n_util::ShouldDoExperimentalRTLLayout(); |
| 115 |
112 if (image_) { | 116 if (image_) { |
113 // Center the image vertically. | 117 // Center the image vertically. |
114 const NSSize image_size = [image_ size]; | 118 const NSSize image_size = [image_ size]; |
115 NSRect image_rect = decoration_frame; | 119 NSRect image_rect = decoration_frame; |
116 image_rect.origin.x += kLeftSidePadding; | 120 if (is_rtl) { |
| 121 image_rect.origin.x = |
| 122 NSMaxX(decoration_frame) - image_size.width - kLeftSidePadding; |
| 123 } else { |
| 124 image_rect.origin.x += kLeftSidePadding; |
| 125 } |
117 image_rect.origin.y += | 126 image_rect.origin.y += |
118 std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0); | 127 std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0); |
119 image_rect.size = image_size; | 128 image_rect.size = image_size; |
120 [image_ drawInRect:image_rect | 129 [image_ drawInRect:image_rect |
121 fromRect:NSZeroRect // Entire image | 130 fromRect:NSZeroRect // Entire image |
122 operation:NSCompositeSourceOver | 131 operation:NSCompositeSourceOver |
123 fraction:1.0 | 132 fraction:1.0 |
124 respectFlipped:YES | 133 respectFlipped:YES |
125 hints:nil]; | 134 hints:nil]; |
126 text_offset = NSMaxX(image_rect) + kIconLabelPadding; | 135 if (is_rtl) |
| 136 text_right_offset = NSMinX(image_rect) - kIconLabelPadding; |
| 137 else |
| 138 text_left_offset = NSMaxX(image_rect) + kIconLabelPadding; |
127 } | 139 } |
128 | 140 |
129 // Draw the divider and set the text color. | 141 // Draw the divider and set the text color. |
130 NSBezierPath* line = [NSBezierPath bezierPath]; | 142 NSBezierPath* line = [NSBezierPath bezierPath]; |
| 143 const CGFloat divider_x_position = |
| 144 is_rtl ? NSMinX(decoration_frame) + DividerPadding() |
| 145 : NSMaxX(decoration_frame) - DividerPadding(); |
| 146 |
131 [line setLineWidth:1]; | 147 [line setLineWidth:1]; |
132 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), | 148 [line moveToPoint:NSMakePoint(divider_x_position, NSMinY(decoration_frame))]; |
133 NSMinY(decoration_frame))]; | 149 [line lineToPoint:NSMakePoint(divider_x_position, NSMaxY(decoration_frame))]; |
134 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), | |
135 NSMaxY(decoration_frame))]; | |
136 | 150 |
137 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; | 151 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
138 [GetDividerColor(in_dark_mode) set]; | 152 [GetDividerColor(in_dark_mode) set]; |
139 [line stroke]; | 153 [line stroke]; |
140 | 154 |
141 NSColor* text_color = | 155 NSColor* text_color = |
142 in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor(); | 156 in_dark_mode ? GetDarkModeTextColor() : GetBackgroundBorderColor(); |
143 SetTextColor(text_color); | 157 SetTextColor(text_color); |
144 | 158 |
145 if (label_) { | 159 if (label_) { |
146 NSRect text_rect = frame; | 160 NSRect text_rect = frame; |
147 text_rect.origin.x = text_offset; | 161 text_rect.origin.x = text_left_offset; |
148 text_rect.size.width = NSMaxX(decoration_frame) - NSMinX(text_rect); | 162 text_rect.size.width = text_right_offset - text_left_offset; |
149 // Transform the coordinate system to adjust the baseline on Retina. This is | 163 // Transform the coordinate system to adjust the baseline on Retina. This is |
150 // the only way to get fractional adjustments. | 164 // the only way to get fractional adjustments. |
151 gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; | 165 gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; |
152 CGFloat line_width = [control_view cr_lineWidth]; | 166 CGFloat line_width = [control_view cr_lineWidth]; |
153 if (line_width < 1) { | 167 if (line_width < 1) { |
154 NSAffineTransform* transform = [NSAffineTransform transform]; | 168 NSAffineTransform* transform = [NSAffineTransform transform]; |
155 [transform translateXBy:0 yBy:retina_baseline_offset_]; | 169 [transform translateXBy:0 yBy:retina_baseline_offset_]; |
156 [transform concat]; | 170 [transform concat]; |
157 } | 171 } |
158 DrawLabel(label_, attributes_, text_rect); | 172 DrawLabel(label_, attributes_, text_rect); |
(...skipping 24 matching lines...) Expand all Loading... |
183 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; | 197 [attributes_ setObject:text_color forKey:NSForegroundColorAttributeName]; |
184 } | 198 } |
185 | 199 |
186 void BubbleDecoration::SetFont(NSFont* font) { | 200 void BubbleDecoration::SetFont(NSFont* font) { |
187 [attributes_ setObject:font forKey:NSFontAttributeName]; | 201 [attributes_ setObject:font forKey:NSFontAttributeName]; |
188 } | 202 } |
189 | 203 |
190 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { | 204 void BubbleDecoration::SetRetinaBaselineOffset(CGFloat offset) { |
191 retina_baseline_offset_ = offset; | 205 retina_baseline_offset_ = offset; |
192 } | 206 } |
OLD | NEW |