| 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 <stddef.h> | 7 #include <stddef.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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 using extensions::FeatureSwitch; | 25 using extensions::FeatureSwitch; |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // Matches the clipping radius of |GradientButtonCell|. | 29 // Matches the clipping radius of |GradientButtonCell|. |
| 30 const CGFloat kCornerRadius = 3.0; | 30 const CGFloat kCornerRadius = 3.0; |
| 31 | 31 |
| 32 // How far to inset the left- and right-hand decorations from the field's | 32 // How far to inset the left- and right-hand decorations from the field's |
| 33 // bounds. | 33 // bounds. |
| 34 const CGFloat kRightDecorationXOffset = 5.0; | 34 const CGFloat kRightDecorationXOffset = 2.0; |
| 35 const CGFloat kLeftDecorationXOffset = 6.0; | 35 const CGFloat kLeftDecorationXOffset = 1.0; |
| 36 | 36 |
| 37 // The amount of padding on either side reserved for drawing | 37 // How much the text frame needs to overlap the rightmost left decoration. |
| 38 // decorations. [Views has |kItemPadding| == 3.] | 38 const CGFloat kTextFrameDecorationOverlap = 4.0; |
| 39 const CGFloat kDecorationHorizontalPad = 4.0; | |
| 40 | 39 |
| 41 // How long to wait for mouse-up on the location icon before assuming | 40 // How long to wait for mouse-up on the location icon before assuming |
| 42 // that the user wants to drag. | 41 // that the user wants to drag. |
| 43 const NSTimeInterval kLocationIconDragTimeout = 0.25; | 42 const NSTimeInterval kLocationIconDragTimeout = 0.25; |
| 44 | 43 |
| 45 // Calculate the positions for a set of decorations. |frame| is the | 44 // Calculate the positions for a set of decorations. |frame| is the |
| 46 // overall frame to do layout in, |remaining_frame| will get the | 45 // overall frame to do layout in, |remaining_frame| will get the |
| 47 // left-over space. |all_decorations| is the set of decorations to | 46 // left-over space. |all_decorations| is the set of decorations to |
| 48 // lay out, |decorations| will be set to the decorations which are | 47 // lay out, |decorations| will be set to the decorations which are |
| 49 // visible and which fit, in the same order as |all_decorations|, | 48 // visible and which fit, in the same order as |all_decorations|, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 62 NSRect* remaining_frame) { | 61 NSRect* remaining_frame) { |
| 63 DCHECK(x_edge == NSMinXEdge || x_edge == NSMaxXEdge); | 62 DCHECK(x_edge == NSMinXEdge || x_edge == NSMaxXEdge); |
| 64 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 63 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 65 | 64 |
| 66 // The initial padding depends on whether the first visible decoration is | 65 // The initial padding depends on whether the first visible decoration is |
| 67 // a button or not. | 66 // a button or not. |
| 68 bool is_first_visible_decoration = true; | 67 bool is_first_visible_decoration = true; |
| 69 | 68 |
| 70 for (size_t i = 0; i < all_decorations.size(); ++i) { | 69 for (size_t i = 0; i < all_decorations.size(); ++i) { |
| 71 if (all_decorations[i]->IsVisible()) { | 70 if (all_decorations[i]->IsVisible()) { |
| 72 CGFloat padding = kDecorationHorizontalPad; | 71 CGFloat padding = 0; |
| 73 if (is_first_visible_decoration) { | 72 if (is_first_visible_decoration) { |
| 74 padding = regular_padding; | 73 padding = regular_padding; |
| 75 is_first_visible_decoration = false; | 74 is_first_visible_decoration = false; |
| 76 } | 75 } |
| 77 | 76 |
| 78 NSRect padding_rect, available; | 77 NSRect padding_rect, available; |
| 79 | 78 |
| 80 // Peel off the outside padding. | 79 // Peel off the outside padding. |
| 81 NSDivideRect(frame, &padding_rect, &available, padding, x_edge); | 80 NSDivideRect(frame, &padding_rect, &available, padding, x_edge); |
| 82 | 81 |
| 83 // Find out how large the decoration will be in the remaining | 82 // Find out how large the decoration will be in the remaining |
| 84 // space. | 83 // space. |
| 85 const CGFloat used_width = | 84 const CGFloat used_width = |
| 86 all_decorations[i]->GetWidthForSpace(NSWidth(available)); | 85 all_decorations[i]->GetWidthForSpace(NSWidth(available)); |
| 87 | 86 |
| 88 if (used_width != LocationBarDecoration::kOmittedWidth) { | 87 if (used_width != LocationBarDecoration::kOmittedWidth) { |
| 89 DCHECK_GT(used_width, 0.0); | 88 DCHECK_GT(used_width, 0.0); |
| 90 NSRect decoration_frame; | 89 NSRect decoration_frame; |
| 91 | 90 |
| 92 // Peel off the desired width, leaving the remainder in | 91 // Peel off the desired width, leaving the remainder in |
| 93 // |frame|. | 92 // |frame|. |
| 94 NSDivideRect(available, &decoration_frame, &frame, | 93 NSDivideRect(available, &decoration_frame, &frame, |
| 95 used_width, x_edge); | 94 used_width, x_edge); |
| 96 | 95 |
| 97 decorations->push_back(all_decorations[i]); | 96 decorations->push_back(all_decorations[i]); |
| 98 decoration_frames->push_back(decoration_frame); | 97 decoration_frames->push_back(decoration_frame); |
| 99 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 98 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 100 | |
| 101 // Adjust padding for between decorations. | |
| 102 padding = kDecorationHorizontalPad; | |
| 103 } | 99 } |
| 104 } | 100 } |
| 105 } | 101 } |
| 106 | 102 |
| 107 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 103 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 108 *remaining_frame = frame; | 104 *remaining_frame = frame; |
| 109 } | 105 } |
| 110 | 106 |
| 111 // Helper function for calculating placement of decorations w/in the cell. | 107 // Helper function for calculating placement of decorations w/in the cell. |
| 112 // |frame| is the cell's boundary rectangle, |remaining_frame| will get any | 108 // |frame| is the cell's boundary rectangle, |remaining_frame| will get any |
| (...skipping 16 matching lines...) Expand all Loading... |
| 129 | 125 |
| 130 // Layout |left_decorations| against the LHS. | 126 // Layout |left_decorations| against the LHS. |
| 131 CalculatePositionsHelper(frame, left_decorations, NSMinXEdge, | 127 CalculatePositionsHelper(frame, left_decorations, NSMinXEdge, |
| 132 kLeftDecorationXOffset, decorations, | 128 kLeftDecorationXOffset, decorations, |
| 133 decoration_frames, &frame); | 129 decoration_frames, &frame); |
| 134 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 130 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 135 | 131 |
| 136 // Capture the number of visible left-hand decorations. | 132 // Capture the number of visible left-hand decorations. |
| 137 const size_t left_count = decorations->size(); | 133 const size_t left_count = decorations->size(); |
| 138 | 134 |
| 135 // Extend the text frame so that it slightly overlaps the rightmost left |
| 136 // decoration. |
| 137 if (left_count) { |
| 138 frame.origin.x -= kTextFrameDecorationOverlap; |
| 139 frame.size.width += kTextFrameDecorationOverlap; |
| 140 } |
| 141 |
| 139 // Layout |right_decorations| against the RHS. | 142 // Layout |right_decorations| against the RHS. |
| 140 CalculatePositionsHelper(frame, right_decorations, NSMaxXEdge, | 143 CalculatePositionsHelper(frame, right_decorations, NSMaxXEdge, |
| 141 kRightDecorationXOffset, decorations, | 144 kRightDecorationXOffset, decorations, |
| 142 decoration_frames, &frame); | 145 decoration_frames, &frame); |
| 143 DCHECK_EQ(decorations->size(), decoration_frames->size()); | 146 DCHECK_EQ(decorations->size(), decoration_frames->size()); |
| 144 | 147 |
| 145 // Reverse the right-hand decorations so that overall everything is | 148 // Reverse the right-hand decorations so that overall everything is |
| 146 // sorted left to right. | 149 // sorted left to right. |
| 147 std::reverse(decorations->begin() + left_count, decorations->end()); | 150 std::reverse(decorations->begin() + left_count, decorations->end()); |
| 148 std::reverse(decoration_frames->begin() + left_count, | 151 std::reverse(decoration_frames->begin() + left_count, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, | 266 CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, |
| 264 &decorations, &decorationFrames, &textFrame); | 267 &decorations, &decorationFrames, &textFrame); |
| 265 | 268 |
| 266 // Determine the left-most extent for the i-beam cursor. | 269 // Determine the left-most extent for the i-beam cursor. |
| 267 CGFloat minX = NSMinX(textFrame); | 270 CGFloat minX = NSMinX(textFrame); |
| 268 for (size_t index = left_count; index--; ) { | 271 for (size_t index = left_count; index--; ) { |
| 269 if (decorations[index]->AcceptsMousePress()) | 272 if (decorations[index]->AcceptsMousePress()) |
| 270 break; | 273 break; |
| 271 | 274 |
| 272 // If at leftmost decoration, expand to edge of cell. | 275 // If at leftmost decoration, expand to edge of cell. |
| 273 if (!index) { | 276 if (!index) |
| 274 minX = NSMinX(cellFrame); | 277 minX = NSMinX(cellFrame); |
| 275 } else { | 278 else |
| 276 minX = NSMinX(decorationFrames[index]) - kDecorationHorizontalPad; | 279 minX = NSMinX(decorationFrames[index]); |
| 277 } | |
| 278 } | 280 } |
| 279 | 281 |
| 280 // Determine the right-most extent for the i-beam cursor. | 282 // Determine the right-most extent for the i-beam cursor. |
| 281 CGFloat maxX = NSMaxX(textFrame); | 283 CGFloat maxX = NSMaxX(textFrame); |
| 282 for (size_t index = left_count; index < decorations.size(); ++index) { | 284 for (size_t index = left_count; index < decorations.size(); ++index) { |
| 283 if (decorations[index]->AcceptsMousePress()) | 285 if (decorations[index]->AcceptsMousePress()) |
| 284 break; | 286 break; |
| 285 | 287 |
| 286 // If at rightmost decoration, expand to edge of cell. | 288 // If at rightmost decoration, expand to edge of cell. |
| 287 if (index == decorations.size() - 1) { | 289 if (index == decorations.size() - 1) |
| 288 maxX = NSMaxX(cellFrame); | 290 maxX = NSMaxX(cellFrame); |
| 289 } else { | 291 else |
| 290 maxX = NSMaxX(decorationFrames[index]) + kDecorationHorizontalPad; | 292 maxX = NSMaxX(decorationFrames[index]); |
| 291 } | |
| 292 } | 293 } |
| 293 | 294 |
| 294 // I-beam cursor covers left-most to right-most. | 295 // I-beam cursor covers left-most to right-most. |
| 295 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); | 296 return NSMakeRect(minX, NSMinY(textFrame), maxX - minX, NSHeight(textFrame)); |
| 296 } | 297 } |
| 297 | 298 |
| 298 - (void)drawWithFrame:(NSRect)frame inView:(NSView*)controlView { | 299 - (void)drawWithFrame:(NSRect)frame inView:(NSView*)controlView { |
| 299 BOOL inDarkMode = [[controlView window] inIncognitoModeWithSystemTheme]; | 300 BOOL inDarkMode = [[controlView window] inIncognitoModeWithSystemTheme]; |
| 300 BOOL showingFirstResponder = [self showsFirstResponder]; | 301 BOOL showingFirstResponder = [self showsFirstResponder]; |
| 301 // Adjust the inset by 1/2 the line width to get a crisp line (screen pixels | 302 // Adjust the inset by 1/2 the line width to get a crisp line (screen pixels |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 std::vector<LocationBarDecoration*> decorations; | 358 std::vector<LocationBarDecoration*> decorations; |
| 358 std::vector<NSRect> decorationFrames; | 359 std::vector<NSRect> decorationFrames; |
| 359 NSRect workingFrame; | 360 NSRect workingFrame; |
| 360 | 361 |
| 361 CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, | 362 CalculatePositionsInFrame(cellFrame, leftDecorations_, rightDecorations_, |
| 362 &decorations, &decorationFrames, &workingFrame); | 363 &decorations, &decorationFrames, &workingFrame); |
| 363 | 364 |
| 364 // Draw the decorations. | 365 // Draw the decorations. |
| 365 for (size_t i = 0; i < decorations.size(); ++i) { | 366 for (size_t i = 0; i < decorations.size(); ++i) { |
| 366 if (decorations[i]) { | 367 if (decorations[i]) { |
| 367 NSRect background_frame = NSInsetRect( | 368 decorations[i]->DrawWithBackgroundInFrame(decorationFrames[i], |
| 368 decorationFrames[i], -(kDecorationHorizontalPad + 1) / 2, 2); | 369 controlView); |
| 369 decorations[i]->DrawWithBackgroundInFrame( | |
| 370 background_frame, decorationFrames[i], controlView); | |
| 371 } | 370 } |
| 372 } | 371 } |
| 373 | 372 |
| 374 // NOTE: This function must closely match the logic in | 373 // NOTE: This function must closely match the logic in |
| 375 // |-textFrameForFrame:|. | 374 // |-textFrameForFrame:|. |
| 376 | 375 |
| 377 // Superclass draws text portion WRT original |cellFrame|. | 376 // Superclass draws text portion WRT original |cellFrame|. |
| 378 ui::ScopedCGContextSmoothFonts fontSmoothing; | 377 ui::ScopedCGContextSmoothFonts fontSmoothing; |
| 379 [super drawInteriorWithFrame:cellFrame inView:controlView]; | 378 [super drawInteriorWithFrame:cellFrame inView:controlView]; |
| 380 } | 379 } |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 | 659 |
| 661 - (void)handleFocusEvent:(NSEvent*)event | 660 - (void)handleFocusEvent:(NSEvent*)event |
| 662 ofView:(AutocompleteTextField*)controlView { | 661 ofView:(AutocompleteTextField*)controlView { |
| 663 if ([controlView observer]) { | 662 if ([controlView observer]) { |
| 664 const bool controlDown = ([event modifierFlags] & NSControlKeyMask) != 0; | 663 const bool controlDown = ([event modifierFlags] & NSControlKeyMask) != 0; |
| 665 [controlView observer]->OnSetFocus(controlDown); | 664 [controlView observer]->OnSetFocus(controlDown); |
| 666 } | 665 } |
| 667 } | 666 } |
| 668 | 667 |
| 669 @end | 668 @end |
| OLD | NEW |