AppKit: Post dynamic image load notification for each resolved image

- Observe resolved image in -size, as in -loadStatus and -load

- Observe every resolved image that still loads, not only last one

- Post CPImageDidLoadNotification once for each resolved image that loads

- Resolve image again in CPImageView -imageDidLoad: in view's own appearance

- Wait in _CPImageAndTextView layout for image of view's own appearance

- Stop observation of replaced image in _CPImageAndTextView -setImage:

- Keep CPTextAttachment waiting while its image still loads

- Add CPDynamicImageLoadTest
This commit is contained in:
David Richardson committed 2026-11-11 10:37:14 -06:00
1 parent c397f4b649
commit dadd5e5996
5 files changed
+326 -16

No files matched your search

+32 -12
View File
@@ -1401,7 +1401,7 @@ var CPNinePartImageImageSlicesKey = @"CPNinePartImageImageSlicesKey";
@implementation _CPDynamicImage : CPImage
{
CGSize _sizeOverride;
CPImage _observedImage;
CPArray _pendingImages;
}
- (id)_initWithDynamicImageName:(CPString)aName
@@ -1462,7 +1462,12 @@ var CPNinePartImageImageSlicesKey = @"CPNinePartImageImageSlicesKey";
var resolvedImage = [self _resolvedImage];
return resolvedImage ? [resolvedImage size] : CGSizeMakeZero();
if (!resolvedImage)
return CGSizeMakeZero();
[self _observeImage:resolvedImage];
return [resolvedImage size];
}
- (Image)image
@@ -1579,28 +1584,43 @@ var CPNinePartImageImageSlicesKey = @"CPNinePartImageImageSlicesKey";
/*
A view that finds the image not yet loaded waits for CPImageDidLoadNotification from the
system image, so the system image posts it when the resolved image loads.
dynamic image, so the dynamic image posts it when a resolved image loads. The dynamic image
watched the resolved image only in -loadStatus and -load, and only one image at a time.
CPImageView reads -size, so it never received the notification, and an image resolved in a
second appearance replaced the first. The dynamic image now watches each resolved image
that -size, -loadStatus or -load finds loading, and posts once for each one that loads. A
view that receives the notification checks its own image again, because the image that
loaded can belong to another appearance.
*/
- (void)_observeImage:(CPImage)anImage
{
if (!anImage || anImage === _observedImage || [anImage loadStatus] === CPImageLoadStatusCompleted)
if (!anImage)
return;
var center = [CPNotificationCenter defaultCenter];
/*
An image that has loaded posts nothing more, and an image that failed posts nothing.
*/
var status = [anImage loadStatus];
if (_observedImage)
[center removeObserver:self name:CPImageDidLoadNotification object:_observedImage];
if (status !== CPImageLoadStatusInitialized && status !== CPImageLoadStatusLoading)
return;
_observedImage = anImage;
if (!_pendingImages)
_pendingImages = [];
[center addObserver:self selector:@selector(_observedImageDidLoad:) name:CPImageDidLoadNotification object:anImage];
if ([_pendingImages indexOfObjectIdenticalTo:anImage] !== CPNotFound)
return;
[_pendingImages addObject:anImage];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_pendingImageDidLoad:) name:CPImageDidLoadNotification object:anImage];
}
- (void)_observedImageDidLoad:(CPNotification)aNotification
- (void)_pendingImageDidLoad:(CPNotification)aNotification
{
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPImageDidLoadNotification object:_observedImage];
var loadedImage = [aNotification object];
_observedImage = nil;
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPImageDidLoadNotification object:loadedImage];
[_pendingImages removeObjectIdenticalTo:loadedImage];
[self _imageDidLoad];
}
+29 -1
View File
@@ -282,9 +282,37 @@ function _CPImageViewRecordsAreSame(aRecord, anotherRecord)
[self _applyImageInCurrentAppearance];
}
/*
A dynamic image posts the notification when any of its resolved images loads, and that
image can belong to another appearance. The view took every notification as the load of its
own image, so it showed an image that was still loading and stopped waiting for it. The view
now resolves its image again in its effective appearance: a new image is applied, an image
that is still loading is awaited further, and a loaded image is shown.
*/
- (void)imageDidLoad:(CPNotification)aNotification
{
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPImageDidLoadNotification object:[self objectValue]];
var image = [self objectValue],
previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
try
{
if (!_CPImageViewRecordsAreSame(_appliedImageRecord, _CPImageViewRecordForImage(image)))
{
[self _applyImageInCurrentAppearance];
return;
}
var size = [image size];
if (size && size.width === -1 && size.height === -1)
return;
}
finally
{
_CPAppearanceEndDrawing(previousAppearance);
}
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPImageDidLoadNotification object:image];
[self hideOrDisplayContents];
[self setNeedsLayout];
+8
View File
@@ -279,6 +279,14 @@ _CPTextAttachmentDidChangeSizeNotification = @"_CPTextAttachmentDidChangeSizeNot
*/
- (void)_imageDidLoad:(CPNotification)aNotification
{
/*
A dynamic image posts the notification when any of its resolved images loads. The
attachment took every notification as the load of its own image, so it took the size of
an image that was still loading and stopped waiting for it.
*/
if ([_image loadStatus] !== CPImageLoadStatusCompleted)
return;
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPImageDidLoadNotification object:_image];
if (_imageView)
+30 -3
View File
@@ -376,15 +376,33 @@ var _CPimageAndTextViewFrameSizeChangedFlag = 1 << 0,
if (_image == anImage)
return;
/*
The view kept observing a replaced image, so the load of that image laid the view out
and removed the observation of the new one.
*/
if (_image)
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPImageDidLoadNotification object:_image];
_image = anImage;
_flags |= _CPImageAndTextViewImageChangedFlag;
if ([_image loadStatus] !== CPImageLoadStatusCompleted)
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(imageDidLoad:) name:CPImageDidLoadNotification object:_image];
[self _waitForImageIfLoading];
[self setNeedsLayout];
}
/*
Observes the image while it is still loading in the current drawing appearance.
*/
- (void)_waitForImageIfLoading
{
var center = [CPNotificationCenter defaultCenter];
[center removeObserver:self name:CPImageDidLoadNotification object:_image];
if (_image && [_image loadStatus] !== CPImageLoadStatusCompleted)
[center addObserver:self selector:@selector(imageDidLoad:) name:CPImageDidLoadNotification object:_image];
}
- (void)setImageOffset:(float)theImageOffset
{
if (_imageOffset === theImageOffset)
@@ -868,6 +886,15 @@ var _CPimageAndTextViewFrameSizeChangedFlag = 1 << 0,
}
}
/*
The view waited for an image only when it received it, outside layout. A dynamic image
that resolved to a loading image in this view's appearance, after a change of appearance
or after a notification for another appearance's image, left the view without the
picture. Layout resolves the image in this view's appearance, so the view waits here.
*/
if (_flags & _CPImageAndTextViewImageChangedFlag)
[self _waitForImageIfLoading];
_flags = 0;
}
+227
View File
@@ -0,0 +1,227 @@
/*
* CPDynamicImageLoadTest.j
* AppKit
*
* Created by David Richardson on 2026-11-11.
* 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>
@implementation CPDynamicImageLoadTest : OJTestCase
{
CPTheme _previousTheme;
CPArray _views;
}
- (void)setUp
{
// This will init the global var CPApp which are used internally in the AppKit
[[CPApplication alloc] init];
_previousTheme = [CPTheme defaultTheme];
_views = [];
}
/*
The add image is one shared object, so each view stops observing it.
*/
- (void)tearDown
{
for (var i = 0; i < _views.length; i++)
[[CPNotificationCenter defaultCenter] removeObserver:_views[i]];
[CPTheme setDefaultTheme:_previousTheme];
}
/*
A theme whose add image is anImage in every appearance and aDarkImage in DarkAqua.
*/
- (CPTheme)themeWithAddImage:(CPImage)anImage darkImage:(CPImage)aDarkImage
{
var theme = [[CPTheme alloc] initWithName:@"CPDynamicImageLoadTest"],
template = [[CPImage alloc] init];
[template setValue:anImage forThemeAttribute:@"add-template"];
if (aDarkImage)
[template setValue:aDarkImage forThemeAttribute:@"add-template" inState:CPThemeStateAppearanceDarkAqua];
[theme takeThemeFromObject:template];
return theme;
}
- (CPImage)addImage
{
return [CPImage imageNamed:CPImageNameAddTemplate];
}
/*
Returns an image whose size is unknown until it loads, as an image file that the browser
still loads.
*/
- (CPImage)loadingImage:(CPString)aFilename
{
return [[CPImage alloc] initByReferencingFile:aFilename size:CGSizeMake(-1, -1)];
}
/*
Completes the load of anImage as the browser does: the image element gets its size, and the
image posts CPImageDidLoadNotification.
*/
- (void)completeLoadOf:(CPImage)anImage
{
anImage._image = {width: 10, height: 10};
[anImage _imageDidLoad];
}
- (CPImageView)imageView
{
var imageView = [[CPImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
_views.push(imageView);
return imageView;
}
/*
Returns the objects of the load notifications that anImage posts while aFunction runs.
*/
- (CPArray)notificationsFrom:(CPImage)anImage during:(Function)aFunction
{
var objects = [],
observer = [[CPNotificationCenter defaultCenter] addObserverForName:CPImageDidLoadNotification object:anImage queue:nil usingBlock:function(aNotification)
{
objects.push([aNotification object]);
}];
aFunction();
[[CPNotificationCenter defaultCenter] removeObserver:observer];
return objects;
}
- (void)testASizeQueryWaitsForTheResolvedImage
{
var resolvedImage = [self loadingImage:@"later.png"],
image = [self addImage];
[CPTheme setDefaultTheme:[self themeWithAddImage:resolvedImage darkImage:nil]];
var posted = [self notificationsFrom:image during:function()
{
[self assert:-1 equals:[image size].width];
[self completeLoadOf:resolvedImage];
}];
[self assert:[image] equals:posted];
}
- (void)testEachResolvedImagePostsWhenItLoads
{
var lightImage = [self loadingImage:@"light.png"],
darkImage = [self loadingImage:@"dark.png"],
image = [self addImage];
[CPTheme setDefaultTheme:[self themeWithAddImage:lightImage darkImage:darkImage]];
var posted = [self notificationsFrom:image during:function()
{
[image size];
[[CPAppearance appearanceNamed:CPAppearanceNameDarkAqua] performAsCurrentDrawingAppearance:function()
{
[image size];
}];
[self completeLoadOf:darkImage];
[self completeLoadOf:lightImage];
}];
[self assert:[image, image] equals:posted];
}
- (void)testAnImageViewShowsAnImageThatLoadsLater
{
var resolvedImage = [self loadingImage:@"later.png"],
imageView = [self imageView];
[CPTheme setDefaultTheme:[self themeWithAddImage:resolvedImage darkImage:nil]];
[imageView setImage:[self addImage]];
[imageView layoutIfNeeded];
[self assertFalse:[imageView needsLayout] message:@"The image view waits while the image loads"];
[self completeLoadOf:resolvedImage];
[self assertTrue:[imageView needsLayout] message:@"The image view lays the image out once it loads"];
}
/*
Another view resolves the add image in Aqua, the application's appearance, and that image
loads first.
*/
- (void)testAnImageViewWaitsForTheImageOfItsOwnAppearance
{
var lightImage = [self loadingImage:@"light.png"],
darkImage = [self loadingImage:@"dark.png"],
image = [self addImage],
imageView = [self imageView];
[CPTheme setDefaultTheme:[self themeWithAddImage:lightImage darkImage:darkImage]];
[imageView setAppearance:[CPAppearance appearanceNamed:CPAppearanceNameDarkAqua]];
[imageView setImage:image];
[imageView layoutIfNeeded];
[image size];
[self completeLoadOf:lightImage];
[self assertFalse:[imageView needsLayout] message:@"The load of the Aqua image leaves the DarkAqua image view waiting"];
[self completeLoadOf:darkImage];
[self assertTrue:[imageView needsLayout] message:@"The load of the DarkAqua image lays the image view out"];
}
/*
The image has loaded in Aqua, where the view receives it, and still loads in DarkAqua, the
appearance in which the view lays out.
*/
- (void)testAnImageAndTextViewWaitsForTheImageOfItsOwnAppearance
{
var lightImage = [[CPImage alloc] initByReferencingFile:@"light.png" size:CGSizeMake(10, 10)],
darkImage = [[CPImage alloc] initByReferencingFile:@"dark.png" size:CGSizeMake(10, 10)],
view = [[_CPImageAndTextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
_views.push(view);
[lightImage _imageDidLoad];
[CPTheme setDefaultTheme:[self themeWithAddImage:lightImage darkImage:darkImage]];
[view setAppearance:[CPAppearance appearanceNamed:CPAppearanceNameDarkAqua]];
[view setImage:[self addImage]];
[view layoutIfNeeded];
[self assertFalse:[view needsLayout] message:@"The view waits while its image loads"];
[darkImage _imageDidLoad];
[self assertTrue:[view needsLayout] message:@"The load of the DarkAqua image lays the view out"];
}
@end