AppKit: Prevent hangs from overlapping cursor areas in views outside a window’s content view

Limiting the frontmost-area traversal check to the content view allowed it to run past the window view for views outside the content view (such as those in a toolbar), causing an application hang whenever the pointer entered two overlapping cursor areas within such a view (such as the cancel button and text area of a search field containing text).

- CPWindow: Traverse superviews up to the window view (the root of every view
  in the window) in -_handleCursorUpdateEventsForEvent:atPoint:dragging:, and
  raise the existing view hierarchy exception if a traversal reaches nil.

- CPWindowCursorUpdateTest: Add tests covering overlapping cursor areas of
  sibling views in the content view, a single view outside the content view,
  and a nested view outside the content view.
This commit is contained in:
David Richardson committed 2026-11-17 08:54:36 -06:00
1 parent 2afea4f8ee
commit 977875306d
2 files changed
+155 -5

No files matched your search

+16 -5
View File
@@ -4404,15 +4404,26 @@ var interpolate = function(fromValue, toValue, progress)
continue;
}
/*
The walks below climbed the superviews until they reached the content view. A view
outside the content view, such as a toolbar item under the window view, never reaches
it: the walk passed the window view to nil and ran for ever, because a nil superview is
neither the content view nor the frontmost view. The window view is the root of every
view in the window, so the walks now stop there, and a walk that reaches nil raises.
*/
// Next verify if aView is a subview of frontmostView
// If so, it's our new frontmost view
var searchingView = aView;
while ((searchingView !== _contentView) && ([searchingView superview] !== frontmostView))
while (searchingView && (searchingView !== _windowView) && ([searchingView superview] !== frontmostView))
searchingView = [searchingView superview];
if (searchingView !== _contentView)
if (!searchingView)
[CPException raise:CPInternalInconsistencyException reason:"Problem with view hierarchy"];
if (searchingView !== _windowView)
{
frontmostTrackingArea = aTrackingArea;
frontmostView = aView;
@@ -4427,12 +4438,12 @@ var interpolate = function(fromValue, toValue, progress)
var firstView = frontmostView,
firstSuperview = [firstView superview];
while (firstView !== _contentView)
while (firstView && (firstView !== _windowView))
{
var secondView = aView,
secondSuperview = [secondView superview];
while ((secondSuperview !== _contentView) && (firstSuperview !== secondSuperview))
while (secondSuperview && (secondSuperview !== _windowView) && (firstSuperview !== secondSuperview))
{
secondView = secondSuperview;
secondSuperview = [secondView superview];
@@ -4445,7 +4456,7 @@ var interpolate = function(fromValue, toValue, progress)
firstSuperview = [firstView superview];
}
if (firstSuperview !== secondSuperview)
if (!firstSuperview || (firstSuperview !== secondSuperview))
[CPException raise:CPInternalInconsistencyException reason:"Problem with view hierarchy"];
var firstSuperviewSubviews = [firstSuperview subviews],
+139
View File
@@ -0,0 +1,139 @@
/*
* CPWindowCursorUpdateTest.j
* AppKit
*
* Created by David Richardson on 2026-11-17.
* Copyright David Richardson 2026.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <AppKit/AppKit.j>
/*
When the pointer enters several cursor-update tracking areas at once, the window sends
cursorUpdate: for the area of the frontmost view only. These tests cover views inside the
content view and views outside it. A toolbar is a subview of the window view, not of the
content view, so a view added to the window view stands for a toolbar item.
*/
var CursorUpdateTestUpdatedAreas = [];
/*
A view that records the name of each tracking area for which it receives cursorUpdate:.
*/
@implementation _CursorUpdateTestView : CPView
{
}
- (CPTrackingArea)addCursorAreaNamed:(CPString)aName
{
var trackingArea = [[CPTrackingArea alloc] initWithRect:[self bounds]
options:CPTrackingCursorUpdate | CPTrackingActiveInActiveApp
owner:self
userInfo:@{ @"name": aName }];
[self addTrackingArea:trackingArea];
return trackingArea;
}
- (void)cursorUpdate:(CPEvent)anEvent
{
CursorUpdateTestUpdatedAreas.push([[[anEvent trackingArea] userInfo] objectForKey:@"name"]);
}
@end
@implementation CPWindowCursorUpdateTest : OJTestCase
{
CPWindow window;
}
- (void)setUp
{
// This will init the global var CPApp which are used internally in the AppKit
[[CPApplication alloc] init];
window = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 400.0) styleMask:CPWindowNotSizable];
CursorUpdateTestUpdatedAreas = [];
}
- (CPView)frameView
{
return [[window contentView] superview];
}
- (void)moveMouseToPoint:(CGPoint)aPoint
{
var anEvent = [CPEvent mouseEventWithType:CPMouseMoved
location:aPoint
modifierFlags:0
timestamp:0
windowNumber:[window windowNumber]
context:nil
eventNumber:-1
clickCount:0
pressure:0];
[[CPApplication sharedApplication] sendEvent:anEvent];
}
- (void)testTheTopSiblingTakesTheCursorInTheContentView
{
var lowerView = [[_CursorUpdateTestView alloc] initWithFrame:CGRectMake(10.0, 10.0, 100.0, 100.0)],
upperView = [[_CursorUpdateTestView alloc] initWithFrame:CGRectMake(50.0, 50.0, 100.0, 100.0)];
[[window contentView] addSubview:lowerView];
[[window contentView] addSubview:upperView];
[lowerView addCursorAreaNamed:@"lower"];
[upperView addCursorAreaNamed:@"upper"];
[self moveMouseToPoint:CGPointMake(75.0, 75.0)];
[self assert:[@"upper"] equals:CursorUpdateTestUpdatedAreas];
}
- (void)testOneViewOutsideTheContentViewTakesTheCursorForItsFirstArea
{
var view = [[_CursorUpdateTestView alloc] initWithFrame:CGRectMake(10.0, 10.0, 100.0, 100.0)];
[[self frameView] addSubview:view];
[view addCursorAreaNamed:@"first"];
[view addCursorAreaNamed:@"second"];
[self moveMouseToPoint:CGPointMake(50.0, 50.0)];
[self assert:[@"first"] equals:CursorUpdateTestUpdatedAreas];
}
- (void)testANestedViewOutsideTheContentViewTakesTheCursorFromItsParent
{
var parentView = [[_CursorUpdateTestView alloc] initWithFrame:CGRectMake(10.0, 10.0, 200.0, 200.0)],
childView = [[_CursorUpdateTestView alloc] initWithFrame:CGRectMake(10.0, 10.0, 100.0, 100.0)];
[[self frameView] addSubview:parentView];
[parentView addSubview:childView];
// The child's area is added first, so the window compares the parent against the child.
[childView addCursorAreaNamed:@"child"];
[parentView addCursorAreaNamed:@"parent"];
[self moveMouseToPoint:CGPointMake(50.0, 50.0)];
[self assert:[@"child"] equals:CursorUpdateTestUpdatedAreas];
}
@end