OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/cocoa/autocomplete_text_field.h" | 5 #include "chrome/browser/cocoa/autocomplete_text_field.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/cocoa/autocomplete_text_field_cell.h" | 8 #include "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
9 | 9 |
10 @implementation AutocompleteTextField | 10 @implementation AutocompleteTextField |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 // Cocoa text fields are edited by placing an NSTextView as subview, | 32 // Cocoa text fields are edited by placing an NSTextView as subview, |
33 // positioned by the cell's -editWithFrame:inView:... method. Using | 33 // positioned by the cell's -editWithFrame:inView:... method. Using |
34 // the standard -makeFirstResponder: machinery to reposition the field | 34 // the standard -makeFirstResponder: machinery to reposition the field |
35 // editor results in resetting the field editor's editing state, which | 35 // editor results in resetting the field editor's editing state, which |
36 // AutocompleteEditViewMac monitors. This causes problems because | 36 // AutocompleteEditViewMac monitors. This causes problems because |
37 // editing can require the field editor to be repositioned, which | 37 // editing can require the field editor to be repositioned, which |
38 // could disrupt editing. This code repositions the subview directly, | 38 // could disrupt editing. This code repositions the subview directly, |
39 // which causes no editing-state changes. | 39 // which causes no editing-state changes. |
40 - (void)resetFieldEditorFrameIfNeeded { | 40 - (void)resetFieldEditorFrameIfNeeded { |
41 AutocompleteTextFieldCell* cell = [self cell]; | 41 // No action if not editing. |
42 if ([cell fieldEditorNeedsReset]) { | 42 NSText* editor = [self currentEditor]; |
43 // No change to bounds necessary if not editing. | 43 if (!editor) { |
44 NSText* editor = [self currentEditor]; | 44 return; |
45 if (editor) { | 45 } |
46 // When editing, we should have exactly one subview, which is a | |
47 // clipview containing the editor (for purposes of scrolling). | |
48 NSArray* subviews = [self subviews]; | |
49 DCHECK_EQ([subviews count], 1U); | |
50 DCHECK([editor isDescendantOf:self]); | |
51 if ([subviews count] > 0) { | |
52 const NSRect frame([cell drawingRectForBounds:[self bounds]]); | |
53 [[subviews objectAtIndex:0] setFrame:frame]; | |
54 | 46 |
55 // Make sure the selection remains visible. | 47 // When editing, we should have exactly one subview, which is a |
56 // TODO(shess) Could this be janky? | 48 // clipview containing the editor (for purposes of scrolling). |
57 [editor scrollRangeToVisible:[editor selectedRange]]; | 49 NSArray* subviews = [self subviews]; |
58 } | 50 DCHECK_EQ([subviews count], 1U); |
59 } | 51 DCHECK([editor isDescendantOf:self]); |
60 [cell setFieldEditorNeedsReset:NO]; | 52 if ([subviews count] == 0) { |
| 53 return; |
61 } | 54 } |
| 55 |
| 56 // If the frame is already right, don't make any visible changes. |
| 57 AutocompleteTextFieldCell* cell = [self autocompleteTextFieldCell]; |
| 58 const NSRect frame([cell drawingRectForBounds:[self bounds]]); |
| 59 NSView* subview = [subviews objectAtIndex:0]; |
| 60 if (NSEqualRects(frame, [subview frame])) { |
| 61 return; |
| 62 } |
| 63 |
| 64 [subview setFrame:frame]; |
| 65 |
| 66 // Make sure the selection remains visible. |
| 67 // TODO(shess) This could be janky if it jerks the visible region |
| 68 // around too much. I believe that text fields only scroll in |
| 69 // response to selection movement (continuing the selection past the |
| 70 // edge, or arrowing the cursor around). |
| 71 [editor scrollRangeToVisible:[editor selectedRange]]; |
62 } | 72 } |
63 | 73 |
64 // Reroute events for the decoration area to the field editor. This | 74 // Reroute events for the decoration area to the field editor. This |
65 // will cause the cursor to be moved as close to the edge where the | 75 // will cause the cursor to be moved as close to the edge where the |
66 // event was seen as possible. | 76 // event was seen as possible. |
67 // | 77 // |
68 // The reason for this code's existence is subtle. NSTextField | 78 // The reason for this code's existence is subtle. NSTextField |
69 // implements text selection and editing in terms of a "field editor". | 79 // implements text selection and editing in terms of a "field editor". |
70 // This is an NSTextView which is installed as a subview of the | 80 // This is an NSTextView which is installed as a subview of the |
71 // control when the field becomes first responder. When the field | 81 // control when the field becomes first responder. When the field |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } | 182 } |
173 | 183 |
174 - (void)setFrame:(NSRect)frameRect { | 184 - (void)setFrame:(NSRect)frameRect { |
175 [super setFrame:frameRect]; | 185 [super setFrame:frameRect]; |
176 if (observer_) { | 186 if (observer_) { |
177 observer_->OnFrameChanged(); | 187 observer_->OnFrameChanged(); |
178 } | 188 } |
179 } | 189 } |
180 | 190 |
181 @end | 191 @end |
OLD | NEW |