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