AppKit: Prevent a search field's I-beam cursor area from overlapping its buttons

Sizing the I-beam cursor area from the bounds inset by the content inset,
rather than from the content rect, placed the area over portions of the text
field that subclasses exclude from their content rect.

In a search field, which keeps its text between its search and cancel buttons,
the I-beam area covered both buttons and overlapped their tracking areas,
causing cursor update conflicts whenever the pointer entered a button.

- CPTextField: Size the I-beam cursor area in -updateTrackingAreas from
  -contentRectForBounds:.

- CPSearchFieldTest: Add tests verifying that the text cursor area of a search
  field overlaps neither its search button area nor, when the field holds text,
  its cancel button area.
This commit is contained in:
David Richardson committed 2026-11-17 09:42:23 -06:00
1 parent 977875306d
commit 78aca89b2e
2 files changed
+107 -4

No files matched your search

+8 -4
View File
@@ -2468,12 +2468,16 @@ var CPTextFieldIsEditableKey = "CPTextFieldIsEditableKey",
_textFieldTrackingArea = nil;
}
/*
The I-beam area was the bounds inset by the content inset, not the content rect. A
subclass that narrows its content rect, such as CPSearchField, which keeps its text
between its buttons, kept an I-beam area over the parts it excludes, and that area
overlapped the cursor areas of the subclass's own controls. The area now takes the
content rect, which places the text and the editing input.
*/
if ([self isEnabled] && (_isEditable || _isSelectable))
{
var myBounds = CGRectMakeCopy([self bounds]),
contentInset = [self currentValueForThemeAttribute:@"content-inset"];
_textFieldTrackingArea = [[CPTrackingArea alloc] initWithRect:CGRectInsetByInset(myBounds, contentInset)
_textFieldTrackingArea = [[CPTrackingArea alloc] initWithRect:[self contentRectForBounds:[self bounds]]
options:CPTrackingCursorUpdate | CPTrackingActiveInKeyWindow
owner:self
userInfo:nil];
+99
View File
@@ -1,5 +1,24 @@
@import <AppKit/AppKit.j>
/*
Under ojtest the theme of a search field provides no button images, and the button rect
methods fall back to a zero size, so the buttons have empty frames. This subclass gives the
buttons the rects that Aristo3 computes for a field 200 points wide with its 16-point images.
*/
@implementation _CursorAreaTestSearchField : CPSearchField
- (CGRect)searchButtonRectForBounds:(CGRect)aRect
{
return CGRectMake(2.0, 2.0, 16.0, 16.0);
}
- (CGRect)cancelButtonRectForBounds:(CGRect)aRect
{
return CGRectMake(CGRectGetWidth(aRect) - 24.0, 2.0, 16.0, 16.0);
}
@end
@implementation CPSearchFieldTest : OJTestCase
{
CPSearchField _searchField;
@@ -30,4 +49,84 @@
[self assertTrue:[[_searchField recentSearches] count] == 3 message:@"After setRecentSearches array doesn't include results"];
}
// MARK: Cursor areas
/*
A search field owns a cursor area for each visible button and an I-beam cursor area for
its text. The text area must not overlap a button area: the pointer over a button must
find one area only.
*/
- (CPSearchField)searchFieldWithText:(CPString)aString
{
var searchField = [[_CursorAreaTestSearchField alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 22.0)];
/*
A search field creates its buttons only when a DOM is available, which is not the case
under ojtest, so the test gives the field its buttons.
*/
[searchField setSearchButton:[[CPButton alloc] initWithFrame:CGRectMakeZero()]];
[searchField resetSearchButton];
[searchField setCancelButton:[[CPButton alloc] initWithFrame:CGRectMakeZero()]];
[searchField resetCancelButton];
[searchField setStringValue:aString];
[searchField layoutSubviews];
return searchField;
}
- (CPTrackingArea)textAreaOf:(CPSearchField)aSearchField
{
return [[aSearchField trackingAreas] objectAtIndex:[[aSearchField trackingAreas] indexOfObjectPassingTest:function(anArea)
{
return ![[anArea userInfo] objectForKey:@"isButton"];
}]];
}
- (CPArray)buttonAreasOf:(CPSearchField)aSearchField
{
var areas = [aSearchField trackingAreas],
buttonAreas = [];
for (var i = 0; i < [areas count]; i++)
if ([[areas[i] userInfo] objectForKey:@"isButton"])
buttonAreas.push(areas[i]);
return buttonAreas;
}
- (void)assertTextAreaOf:(CPSearchField)aSearchField overlapsNoneOf:(CPArray)buttonAreas
{
var textRect = [[self textAreaOf:aSearchField] rect];
[self assertTrue:CGRectGetWidth(textRect) > 0 message:@"the text area is empty"];
for (var i = 0; i < [buttonAreas count]; i++)
{
var buttonRect = [buttonAreas[i] rect];
[self assertTrue:CGRectGetWidth(buttonRect) > 0 message:@"button area " + i + " is empty"];
[self assertFalse:CGRectIntersectsRect(textRect, buttonRect)
message:@"the text area " + CPStringFromRect(textRect) + " overlaps button area " + CPStringFromRect(buttonRect)];
}
}
- (void)testTheTextAreaDoesNotOverlapTheSearchButtonArea
{
var searchField = [self searchFieldWithText:@""],
buttonAreas = [self buttonAreasOf:searchField];
[self assert:1 equals:[buttonAreas count] message:@"an empty field has a search button area only"];
[self assertTextAreaOf:searchField overlapsNoneOf:buttonAreas];
}
- (void)testTheTextAreaDoesNotOverlapEitherButtonAreaWhenTheFieldHoldsText
{
var searchField = [self searchFieldWithText:@"text"],
buttonAreas = [self buttonAreasOf:searchField];
[self assert:2 equals:[buttonAreas count] message:@"a field with text has a search button area and a cancel button area"];
[self assertTextAreaOf:searchField overlapsNoneOf:buttonAreas];
}
@end