Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" | 5 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/mac_logging.h" | 8 #include "base/mac/mac_logging.h" |
| 9 #import "chrome/browser/ui/cocoa/image_utils.h" | 9 #import "chrome/browser/ui/cocoa/image_utils.h" |
| 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" | 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" |
| 11 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" | 11 #import "chrome/browser/ui/cocoa/location_bar/location_bar_decoration.h" |
| 12 #import "chrome/common/extensions/extension_switch_utils.h" | |
| 12 #import "third_party/mozilla/NSPasteboard+Utils.h" | 13 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 const CGFloat kBaselineAdjust = 3.0; | 17 const CGFloat kBaselineAdjust = 3.0; |
| 17 | 18 |
| 18 // Matches the clipping radius of |GradientButtonCell|. | 19 // Matches the clipping radius of |GradientButtonCell|. |
| 19 const CGFloat kCornerRadius = 4.0; | 20 const CGFloat kCornerRadius = 4.0; |
| 20 | 21 |
| 21 // How far to inset the left-hand decorations from the field's bounds. | 22 // How far to inset the left-hand decorations from the field's bounds. |
| 22 const CGFloat kLeftDecorationXOffset = 5.0; | 23 const CGFloat kLeftDecorationXOffset = 5.0; |
| 23 | 24 |
| 24 // How far to inset the right-hand decorations from the field's bounds. | 25 // How far to inset the right-hand decorations from the field's bounds. |
| 25 // TODO(shess): Why is this different from |kLeftDecorationXOffset|? | 26 // TODO(shess): Why is this different from |kLeftDecorationXOffset|? |
| 26 // |kDecorationOuterXOffset|? | 27 // |kDecorationOuterXOffset|? |
| 27 const CGFloat kRightDecorationXOffset = 5.0; | 28 const CGFloat kRightDecorationXOffset = 5.0; |
| 29 const CGFloat kScriptBadgeRightDecorationXOffset = 9.0; | |
| 30 | |
| 31 inline CGFloat RightDecorationXOffset() { | |
|
Scott Hess - ex-Googler
2012/09/12 22:54:08
Shouldn't need inline. It's static to this file,
Jeffrey Yasskin
2012/09/14 22:13:45
Done.
| |
| 32 return extensions::switch_utils::AreScriptBadgesEnabled() ? | |
| 33 kScriptBadgeRightDecorationXOffset : kRightDecorationXOffset; | |
| 34 } | |
| 28 | 35 |
| 29 // The amount of padding on either side reserved for drawing | 36 // The amount of padding on either side reserved for drawing |
| 30 // decorations. [Views has |kItemPadding| == 3.] | 37 // decorations. [Views has |kItemPadding| == 3.] |
| 31 const CGFloat kDecorationHorizontalPad = 3.0; | 38 const CGFloat kDecorationHorizontalPad = 3.0; |
| 39 const CGFloat kScriptBadgeDecorationHorizontalPad = 9.0; | |
| 32 | 40 |
| 33 // How long to wait for mouse-up on the location icon before assuming | 41 // How long to wait for mouse-up on the location icon before assuming |
| 34 // that the user wants to drag. | 42 // that the user wants to drag. |
| 35 const NSTimeInterval kLocationIconDragTimeout = 0.25; | 43 const NSTimeInterval kLocationIconDragTimeout = 0.25; |
| 36 | 44 |
| 37 // Calculate the positions for a set of decorations. |frame| is the | 45 // Calculate the positions for a set of decorations. |frame| is the |
| 38 // overall frame to do layout in, |remaining_frame| will get the | 46 // overall frame to do layout in, |remaining_frame| will get the |
| 39 // left-over space. |all_decorations| is the set of decorations to | 47 // left-over space. |all_decorations| is the set of decorations to |
| 40 // lay out, |decorations| will be set to the decorations which are | 48 // lay out, |decorations| will be set to the decorations which are |
| 41 // visible and which fit, in the same order as |all_decorations|, | 49 // visible and which fit, in the same order as |all_decorations|, |
| 42 // while |decoration_frames| will be the corresponding frames. | 50 // while |decoration_frames| will be the corresponding frames. |
| 43 // |x_edge| describes the edge to layout the decorations against | 51 // |x_edge| describes the edge to layout the decorations against |
| 44 // (|NSMinXEdge| or |NSMaxXEdge|). |initial_padding| is the padding | 52 // (|NSMinXEdge| or |NSMaxXEdge|). |initial_padding| is the padding |
| 45 // from the edge of |cell_frame| (|kDecorationHorizontalPad| is used | 53 // from the edge of |cell_frame| (|+decorationHorizontalPad| is used |
| 46 // between decorations). | 54 // between decorations). |
| 47 void CalculatePositionsHelper( | 55 void CalculatePositionsHelper( |
| 48 NSRect frame, | 56 NSRect frame, |
| 49 const std::vector<LocationBarDecoration*>& all_decorations, | 57 const std::vector<LocationBarDecoration*>& all_decorations, |
| 50 NSRectEdge x_edge, | 58 NSRectEdge x_edge, |
| 51 CGFloat initial_padding, | 59 CGFloat initial_padding, |
| 52 std::vector<LocationBarDecoration*>* decorations, | 60 std::vector<LocationBarDecoration*>* decorations, |
| 53 std::vector<NSRect>* decoration_frames, | 61 std::vector<NSRect>* decoration_frames, |
| 54 NSRect* remaining_frame) { | 62 NSRect* remaining_frame) { |
| 55 DCHECK(x_edge == NSMinXEdge || x_edge == NSMaxXEdge); | 63 DCHECK(x_edge == NSMinXEdge || x_edge == NSMaxXEdge); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 78 // Peel off the desired width, leaving the remainder in | 86 // Peel off the desired width, leaving the remainder in |
| 79 // |frame|. | 87 // |frame|. |
| 80 NSDivideRect(available, &decoration_frame, &frame, | 88 NSDivideRect(available, &decoration_frame, &frame, |
| 81 used_width, x_edge); | 89 used_width, x_edge); |
| 82 | 90 |
| 83 decorations->push_back(all_decorations[i]); | 91 decorations->push_back(all_decorations[i]); |
| 84 decoration_frames->push_back(decoration_frame); | 92 decoration_frames->push_back(decoration_frame); |
| 85 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 93 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 86 | 94 |
| 87 // Adjust padding for between decorations. | 95 // Adjust padding for between decorations. |
| 88 padding = kDecorationHorizontalPad; | 96 padding = [AutocompleteTextFieldCell decorationHorizontalPad]; |
| 89 } | 97 } |
| 90 } | 98 } |
| 91 } | 99 } |
| 92 | 100 |
| 93 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 101 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 94 *remaining_frame = frame; | 102 *remaining_frame = frame; |
| 95 } | 103 } |
| 96 | 104 |
| 97 // Helper function for calculating placement of decorations w/in the | 105 // Helper function for calculating placement of decorations w/in the |
| 98 // cell. |frame| is the cell's boundary rectangle, |remaining_frame| | 106 // cell. |frame| is the cell's boundary rectangle, |remaining_frame| |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 118 CalculatePositionsHelper(frame, left_decorations, | 126 CalculatePositionsHelper(frame, left_decorations, |
| 119 NSMinXEdge, kLeftDecorationXOffset, | 127 NSMinXEdge, kLeftDecorationXOffset, |
| 120 decorations, decoration_frames, &frame); | 128 decorations, decoration_frames, &frame); |
| 121 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 129 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 122 | 130 |
| 123 // Capture the number of visible left-hand decorations. | 131 // Capture the number of visible left-hand decorations. |
| 124 const size_t left_count = decorations->size(); | 132 const size_t left_count = decorations->size(); |
| 125 | 133 |
| 126 // Layout |right_decorations| against the RHS. | 134 // Layout |right_decorations| against the RHS. |
| 127 CalculatePositionsHelper(frame, right_decorations, | 135 CalculatePositionsHelper(frame, right_decorations, |
| 128 NSMaxXEdge, kRightDecorationXOffset, | 136 NSMaxXEdge, RightDecorationXOffset(), |
| 129 decorations, decoration_frames, &frame); | 137 decorations, decoration_frames, &frame); |
| 130 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 138 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 131 | 139 |
| 132 // Reverse the right-hand decorations so that overall everything is | 140 // Reverse the right-hand decorations so that overall everything is |
| 133 // sorted left to right. | 141 // sorted left to right. |
| 134 std::reverse(decorations->begin() + left_count, decorations->end()); | 142 std::reverse(decorations->begin() + left_count, decorations->end()); |
| 135 std::reverse(decoration_frames->begin() + left_count, | 143 std::reverse(decoration_frames->begin() + left_count, |
| 136 decoration_frames->end()); | 144 decoration_frames->end()); |
| 137 | 145 |
| 138 *remaining_frame = frame; | 146 *remaining_frame = frame; |
| 147 if (extensions::switch_utils::AreScriptBadgesEnabled()) { | |
| 148 // Keep the padding distance between the right-most decoration and the edit | |
| 149 // box, so that any decoration background isn't overwritten by the edit | |
| 150 // box's background. | |
| 151 NSRect dummy; | |
| 152 NSDivideRect(frame, &dummy, remaining_frame, | |
| 153 [AutocompleteTextFieldCell decorationHorizontalPad], | |
| 154 NSMaxXEdge); | |
| 155 } | |
| 139 return left_count; | 156 return left_count; |
| 140 } | 157 } |
| 141 | 158 |
| 142 } // namespace | 159 } // namespace |
| 143 | 160 |
| 144 @implementation AutocompleteTextFieldCell | 161 @implementation AutocompleteTextFieldCell |
| 145 | 162 |
| 163 + (CGFloat)decorationHorizontalPad { | |
| 164 return extensions::switch_utils::AreScriptBadgesEnabled() ? | |
| 165 kScriptBadgeDecorationHorizontalPad : kDecorationHorizontalPad; | |
| 166 } | |
| 167 | |
| 146 - (CGFloat)baselineAdjust { | 168 - (CGFloat)baselineAdjust { |
| 147 return kBaselineAdjust; | 169 return kBaselineAdjust; |
| 148 } | 170 } |
| 149 | 171 |
| 150 - (CGFloat)cornerRadius { | 172 - (CGFloat)cornerRadius { |
| 151 return kCornerRadius; | 173 return kCornerRadius; |
| 152 } | 174 } |
| 153 | 175 |
| 154 - (BOOL)shouldDrawBezel { | 176 - (BOOL)shouldDrawBezel { |
| 155 return YES; | 177 return YES; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 // Determine the left-most extent for the i-beam cursor. | 254 // Determine the left-most extent for the i-beam cursor. |
| 233 CGFloat minX = NSMinX(textFrame); | 255 CGFloat minX = NSMinX(textFrame); |
| 234 for (size_t index = left_count; index--; ) { | 256 for (size_t index = left_count; index--; ) { |
| 235 if (decorations[index]->AcceptsMousePress()) | 257 if (decorations[index]->AcceptsMousePress()) |
| 236 break; | 258 break; |
| 237 | 259 |
| 238 // If at leftmost decoration, expand to edge of cell. | 260 // If at leftmost decoration, expand to edge of cell. |
| 239 if (!index) { | 261 if (!index) { |
| 240 minX = NSMinX(cellFrame); | 262 minX = NSMinX(cellFrame); |
| 241 } else { | 263 } else { |
| 242 minX = NSMinX(decorationFrames[index]) - kDecorationHorizontalPad; | 264 minX = NSMinX(decorationFrames[index]) - |
| 265 [AutocompleteTextFieldCell decorationHorizontalPad]; | |
| 243 } | 266 } |
| 244 } | 267 } |
| 245 | 268 |
| 246 // Determine the right-most extent for the i-beam cursor. | 269 // Determine the right-most extent for the i-beam cursor. |
| 247 CGFloat maxX = NSMaxX(textFrame); | 270 CGFloat maxX = NSMaxX(textFrame); |
| 248 for (size_t index = left_count; index < decorations.size(); ++index) { | 271 for (size_t index = left_count; index < decorations.size(); ++index) { |
| 249 if (decorations[index]->AcceptsMousePress()) | 272 if (decorations[index]->AcceptsMousePress()) |
| 250 break; | 273 break; |
| 251 | 274 |
| 252 // If at rightmost decoration, expand to edge of cell. | 275 // If at rightmost decoration, expand to edge of cell. |
| 253 if (index == decorations.size() - 1) { | 276 if (index == decorations.size() - 1) { |
| 254 maxX = NSMaxX(cellFrame); | 277 maxX = NSMaxX(cellFrame); |
| 255 } else { | 278 } else { |
| 256 maxX = NSMaxX(decorationFrames[index]) + kDecorationHorizontalPad; | 279 maxX = NSMaxX(decorationFrames[index]) + |
| 280 [AutocompleteTextFieldCell decorationHorizontalPad]; | |
| 257 } | 281 } |
| 258 } | 282 } |
| 259 | 283 |
| 260 // I-beam cursor covers left-most to right-most. | 284 // I-beam cursor covers left-most to right-most. |
| 261 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); | 285 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); |
| 262 } | 286 } |
| 263 | 287 |
| 264 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { | 288 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { |
| 265 std::vector<LocationBarDecoration*> decorations; | 289 std::vector<LocationBarDecoration*> decorations; |
| 266 std::vector<NSRect> decorationFrames; | 290 std::vector<NSRect> decorationFrames; |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 557 &decorations, &decorationFrames, &textFrame); | 581 &decorations, &decorationFrames, &textFrame); |
| 558 | 582 |
| 559 for (size_t i = 0; i < decorations.size(); ++i) { | 583 for (size_t i = 0; i < decorations.size(); ++i) { |
| 560 NSString* tooltip = decorations[i]->GetToolTip(); | 584 NSString* tooltip = decorations[i]->GetToolTip(); |
| 561 if ([tooltip length] > 0) | 585 if ([tooltip length] > 0) |
| 562 [controlView addToolTip:tooltip forRect:decorationFrames[i]]; | 586 [controlView addToolTip:tooltip forRect:decorationFrames[i]]; |
| 563 } | 587 } |
| 564 } | 588 } |
| 565 | 589 |
| 566 @end | 590 @end |
| OLD | NEW |