CPWindow: Clear sheet context before reporting sheet dismissal

CPWindow previously posted CPWindowDidEndSheetNotification and invoked
the did-end selector while the closing sheet remained attached.
Consequently, observers and delegates saw -attachedSheet return the
dismissed sheet, and queued sheets relied on a notification observer and
a setTimeout workaround to attach.

-_cleanupSheetWindow now clears the sheet context first, then posts the
notification, invokes the did-end selector, and directly attaches any
queued sheet, removing the observer and timer workaround.

Additionally, CPToolbar now validates its visible items upon sheet
dismissal, removing redundant validation logic from the customization
palette.

Tests:
- CPWindowTest: Verify -attachedSheet state during dismissal
  notifications and automatic attachment of queued sheets.

- CPToolbarTest: Verify item validation on sheet dismissal.
This commit is contained in:
David Richardson committed 2026-11-23 11:16:19 -06:00
1 parent 87d052fbd0
commit 5ba275a7ce
4 files changed
+234 -40

No files matched your search

+17 -3
View File
@@ -30,6 +30,7 @@
@import "_CPToolbarCustomizationPalette.j"
@global CPApp
@global CPWindowDidEndSheetNotification
var CPToolbarDelegate_toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar_ = 1 << 0,
CPToolbarDelegate_toolbarAllowedItemIdentifiers_ = 1 << 1,
@@ -283,13 +284,27 @@ var CPToolbarConfigurationDisplayModeKey = @"TB Display Mode",
- (void)_setWindow:(CPWindow)aWindow
{
var defaultCenter = [CPNotificationCenter defaultCenter];
if (_window)
[[CPNotificationCenter defaultCenter] removeObserver:self name:_CPWindowDidChangeFirstResponderNotification object:_window];
{
[defaultCenter removeObserver:self name:_CPWindowDidChangeFirstResponderNotification object:_window];
[defaultCenter removeObserver:self name:CPWindowDidEndSheetNotification object:_window];
}
_window = aWindow;
if (_window)
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_autoValidateVisibleItems) name:_CPWindowDidChangeFirstResponderNotification object:aWindow];
{
[defaultCenter addObserver:self selector:@selector(_autoValidateVisibleItems) name:_CPWindowDidChangeFirstResponderNotification object:aWindow];
/*
Items that a validator disables while a sheet is attached kept that state after the
sheet ended: nothing validated them again until the first responder changed. The
toolbar now validates its items when a sheet of its window ends.
*/
[defaultCenter addObserver:self selector:@selector(_autoValidateVisibleItems) name:CPWindowDidEndSheetNotification object:aWindow];
}
}
/*!
@@ -718,7 +733,6 @@ var CPToolbarConfigurationDisplayModeKey = @"TB Display Mode",
- (void)_customizationPaletteDidEnd
{
_customizationPalette = nil;
[self validateVisibleItems];
}
/*
+29 -37
View File
@@ -3355,23 +3355,6 @@ CPTexturedBackgroundWindowMask
[attachedSheet setFrame:sheetFrame display:YES animate:NO];
}
- (void)_previousSheetIsClosedNotification:(CPNotification)aNotification
{
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPWindowDidEndSheetNotification object:self];
var sheet = _sheetContext[@"nextSheet"],
modalDelegate = _sheetContext[@"nextModalDelegate"],
endSelector = _sheetContext[@"nextEndSelector"],
contextInfo = _sheetContext[@"nextContextInfo"];
// Needed, because when the notification CPWindowDidEndSheetNotification is sent, the sheetContext is not up to date...
setTimeout(function()
{
[sheet._windowView _enableSheet:YES inWindow:self];
[self _attachSheet:sheet modalDelegate:modalDelegate didEndSelector:endSelector contextInfo:contextInfo];
}, 0)
}
/*
Starting point for sheet session, called from CPApplication beginSheet:
*/
@@ -3389,7 +3372,7 @@ CPTexturedBackgroundWindowMask
_sheetContext[@"nextEndSelector"] = didEndSelector;
_sheetContext[@"nextContextInfo"] = contextInfo;
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_previousSheetIsClosedNotification:) name:CPWindowDidEndSheetNotification object:self];
/* -_cleanupSheetWindow attaches the saved sheet when the current one is gone. */
return;
}
else
@@ -3471,7 +3454,15 @@ CPTexturedBackgroundWindowMask
- (void)_cleanupSheetWindow
{
var sheet = _sheetContext["sheet"],
deferDidEnd = _sheetContext["deferDidEndSelector"];
deferDidEnd = _sheetContext["deferDidEndSelector"],
delegate = _sheetContext["modalDelegate"],
selector = _sheetContext["endSelector"],
returnCode = _sheetContext["returnCode"],
contextInfo = _sheetContext["contextInfo"],
nextSheet = _sheetContext["nextSheet"],
nextModalDelegate = _sheetContext["nextModalDelegate"],
nextEndSelector = _sheetContext["nextEndSelector"],
nextContextInfo = _sheetContext["nextContextInfo"];
// If the parent window is modal, the sheet started its own modal session
if (sheet._isModal)
@@ -3487,28 +3478,29 @@ CPTexturedBackgroundWindowMask
// Close it
[sheet orderOut:self];
/*
The window posted CPWindowDidEndSheetNotification while the sheet context was still
set, so -attachedSheet still answered the sheet that had ended. An observer that
revalidated its state, such as the window's toolbar, saw a sheet that was gone and
kept items disabled. A sheet saved by -_attachSheet:... while this one closed was
attached from an observer of the notification, which had to read the stale context
and wait for a timeout. The context is now cleared first, as AppKit has no attached
sheet when NSWindowDidEndSheetNotification arrives, and the saved sheet is attached
here after the end of this one is reported.
*/
_sheetContext = nil;
sheet._parentView = nil;
[sheet _updateEffectiveAppearance];
[[CPNotificationCenter defaultCenter] postNotificationName:CPWindowDidEndSheetNotification object:self];
if (deferDidEnd)
{
var delegate = _sheetContext["modalDelegate"],
selector = _sheetContext["endSelector"],
returnCode = _sheetContext["returnCode"],
contextInfo = _sheetContext["contextInfo"];
if (deferDidEnd && delegate != null)
delegate.isa.objj_msgSend3(delegate, selector, sheet, returnCode, contextInfo);
// Context must be destroyed, since didEnd might want to attach another sheet
_sheetContext = nil;
sheet._parentView = nil;
[sheet _updateEffectiveAppearance];
if (delegate != null)
delegate.isa.objj_msgSend3(delegate, selector, sheet, returnCode, contextInfo);
}
else
if (nextSheet)
{
_sheetContext = nil;
sheet._parentView = nil;
[sheet _updateEffectiveAppearance];
[nextSheet._windowView _enableSheet:YES inWindow:self];
[self _attachSheet:nextSheet modalDelegate:nextModalDelegate didEndSelector:nextEndSelector contextInfo:nextContextInfo];
}
}
+81
View File
@@ -157,6 +157,47 @@ var ToolbarTestIdentifiersOf = function(aToolbar)
/*
Dragging information for a drop on the customization overlay.
*/
/*
A toolbar delegate and validator with one item, which is enabled only while its window has
no attached sheet.
*/
@implementation _ToolbarTestSheetValidator : CPObject
{
CPWindow window @accessors;
}
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
return [@"SheetValidatedItem"];
}
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
return [@"SheetValidatedItem"];
}
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
{
var item = [[CPToolbarItem alloc] initWithItemIdentifier:anIdentifier];
[item setLabel:@"Validated"];
[item setTarget:self];
[item setAction:@selector(act:)];
return item;
}
- (void)act:(id)aSender
{
}
- (BOOL)validateToolbarItem:(CPToolbarItem)anItem
{
return ![window attachedSheet];
}
@end
@implementation _ToolbarTestDraggingInfo : CPObject
{
CPPasteboard draggingPasteboard @accessors;
@@ -664,6 +705,46 @@ var ToolbarTestIdentifiersOf = function(aToolbar)
[self assert:[CPToolbarSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(toolbar)];
}
/*
An item that its validator disables while a sheet is attached is enabled again when the
sheet ends: the toolbar validates its items on CPWindowDidEndSheetNotification, and the
window has no attached sheet by then.
*/
- (void)testTheToolbarValidatesItsItemsWhenASheetEnds
{
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 800.0, 300.0) styleMask:CPTitledWindowMask],
sheet = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 200.0, 100.0) styleMask:CPDocModalWindowMask],
toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestSheetValidation"],
delegate = [_ToolbarTestSheetValidator new];
[delegate setWindow:window];
[toolbar setDelegate:delegate];
[window setToolbar:toolbar];
var item = [toolbar items][0];
[self assertTrue:[[toolbar visibleItems] containsObject:item] message:@"the item is visible"];
[sheet._windowView _enableSheet:YES inWindow:window];
window._sheetContext = {
"sheet": sheet,
"modalDelegate": nil,
"endSelector": nil,
"contextInfo": nil,
"returnCode": -1,
"opened": NO,
"isAttached": NO,
"isClosing": YES,
"savedConstrains": sheet._constrainsToUsableScreen
};
[toolbar validateVisibleItems];
[self assertFalse:[item isEnabled] message:@"disabled while the sheet is attached"];
[window _cleanupSheetWindow];
[self assertTrue:[item isEnabled] message:@"enabled when the sheet ends"];
}
/*
A drop from the palette of an item that is already in the toolbar moves that item; only the
space items can appear more than once.
+107
View File
@@ -1,5 +1,6 @@
@import <AppKit/CPWindow.j>
@import <AppKit/CPMenuItem.j>
@import <AppKit/CPPanel.j>
@import <AppKit/_CPBorderlessWindowView.j>
@import <Foundation/CPURL.j>
@@ -408,6 +409,84 @@
});
}
/*
The steps below run the end of a sheet without the timers of
-[CPApplication beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:]: the test
makes the sheet context that -_attachSheet:modalDelegate:didEndSelector:contextInfo: makes and
calls -_cleanupSheetWindow, which the animation out calls last.
*/
- (CPWindow)_attachTestSheetTo:(CPWindow)aParent modalDelegate:(id)aDelegate
{
var sheet = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 200.0, 100.0) styleMask:CPDocModalWindowMask];
[sheet._windowView _enableSheet:YES inWindow:aParent];
aParent._sheetContext = {
"sheet": sheet,
"modalDelegate": aDelegate,
"endSelector": @selector(sheetDidEnd:returnCode:contextInfo:),
"contextInfo": nil,
"returnCode": 7,
"opened": NO,
"isAttached": NO,
"isClosing": YES,
"deferDidEndSelector": YES,
"savedConstrains": sheet._constrainsToUsableScreen
};
return sheet;
}
/*
As in AppKit, the window has no attached sheet when CPWindowDidEndSheetNotification arrives
and when the deferred did-end selector runs.
*/
- (void)testTheWindowHasNoAttachedSheetWhenTheEndOfASheetIsReported
{
var parent = [self window],
observer = [_CPWindowTestSheetObserver new],
sheet = [self _attachTestSheetTo:parent modalDelegate:observer];
[observer setWindow:parent];
[[CPNotificationCenter defaultCenter] addObserver:observer selector:@selector(windowDidEndSheet:) name:CPWindowDidEndSheetNotification object:parent];
[parent _cleanupSheetWindow];
[[CPNotificationCenter defaultCenter] removeObserver:observer];
[self assertTrue:[observer notified] message:@"the end of the sheet is reported"];
[self assertNull:[observer sheetAtNotification] message:@"no attached sheet at the notification"];
[self assert:sheet same:[observer endedSheet] message:@"the did-end selector receives the sheet"];
[self assert:7 equals:[observer returnCode]];
[self assertNull:[observer sheetAtDidEnd] message:@"no attached sheet in the did-end selector"];
}
/*
A sheet begun while another closes waits for it; when the first one is gone, the window
attaches the waiting sheet.
*/
- (void)testASheetBegunWhileAnotherClosesIsAttachedWhenTheFirstEnds
{
var parent = [self window],
observer = [_CPWindowTestSheetObserver new],
first = [self _attachTestSheetTo:parent modalDelegate:observer],
second = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 200.0, 100.0) styleMask:CPDocModalWindowMask];
[parent _attachSheet:second modalDelegate:nil didEndSelector:nil contextInfo:nil];
[self assert:first same:[parent attachedSheet] message:@"the second sheet waits"];
[parent _cleanupSheetWindow];
[self assert:second same:[parent attachedSheet] message:@"the second sheet is attached"];
/*
-_attachSheet:... schedules the animation in on a timer. A closing context makes that
timer do nothing after the test.
*/
parent._sheetContext["isClosing"] = YES;
}
@end
/*
@@ -475,3 +554,31 @@ var CPWindowTestZoomingWindowStandardFrame = CGRectMake(40.0, 50.0, 600.0, 450.0
}
@end
/*
Records what the window reports when a sheet ends.
*/
@implementation _CPWindowTestSheetObserver : CPObject
{
CPWindow window @accessors;
BOOL notified @accessors(readonly);
CPWindow sheetAtNotification @accessors(readonly);
CPWindow endedSheet @accessors(readonly);
int returnCode @accessors(readonly);
CPWindow sheetAtDidEnd @accessors(readonly);
}
- (void)windowDidEndSheet:(CPNotification)aNotification
{
notified = YES;
sheetAtNotification = [window attachedSheet];
}
- (void)sheetDidEnd:(CPWindow)aSheet returnCode:(int)aCode contextInfo:(id)aContext
{
endedSheet = aSheet;
returnCode = aCode;
sheetAtDidEnd = [window attachedSheet];
}
@end