Skip to content

Commit

Permalink
Add extension points for global node behaviors, potentially replacing…
Browse files Browse the repository at this point in the history
… interface state delegates (#1229)

* Add extension points for global node behaviors via categories and a public ivar

* Update documentation on context ivar
  • Loading branch information
Adlai-Holler authored Feb 14, 2019
1 parent affa588 commit 6e7cdea
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 48 deletions.
9 changes: 8 additions & 1 deletion Source/ASDisplayNode+Beta.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ typedef struct {
* this hook could be called up to 1 + (pJPEGcount * pJPEGrenderCount) times. The render count depends on how many times the downloader calls the
* progressImage block.
*/
- (void)hierarchyDisplayDidFinish;
AS_CATEGORY_IMPLEMENTABLE
- (void)hierarchyDisplayDidFinish NS_REQUIRES_SUPER;

/**
* Only called on the root during yoga layout.
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)willCalculateLayout:(ASSizeRange)constrainedSize NS_REQUIRES_SUPER;

/**
* Only ASLayoutRangeModeVisibleOnly or ASLayoutRangeModeLowMemory are recommended. Default is ASLayoutRangeModeVisibleOnly,
Expand Down
27 changes: 27 additions & 0 deletions Source/ASDisplayNode+Subclasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,22 @@ NS_ASSUME_NONNULL_BEGIN
*
* @discussion This is the best time to add gesture recognizers to the view.
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)didLoad ASDISPLAYNODE_REQUIRES_SUPER;

/**
* An empty method that you can implement in a category to add global
* node initialization behavior. This method will be called by [ASDisplayNode init].
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)baseDidInit;

/**
* An empty method that you can implement in a category to add global
* node deallocation behavior. This method will be called by [ASDisplayNode dealloc].
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)baseWillDealloc;

#pragma mark - Layout
/** @name Layout */
Expand All @@ -88,6 +102,7 @@ NS_ASSUME_NONNULL_BEGIN
* @discussion Gives a chance for subclasses to perform actions after the subclass and superclass have finished laying
* out.
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)layoutDidFinish ASDISPLAYNODE_REQUIRES_SUPER;

/**
Expand All @@ -96,6 +111,7 @@ NS_ASSUME_NONNULL_BEGIN
* @discussion When the .calculatedLayout property is set to a new ASLayout (directly from -calculateLayoutThatFits: or
* calculated via use of -layoutSpecThatFits:), subclasses may inspect it here.
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)calculatedLayoutDidChange ASDISPLAYNODE_REQUIRES_SUPER;


Expand Down Expand Up @@ -162,15 +178,25 @@ NS_ASSUME_NONNULL_BEGIN
* For descriptions, see <ASInterfaceStateDelegate> definition.
*/

AS_CATEGORY_IMPLEMENTABLE
- (void)didEnterVisibleState ASDISPLAYNODE_REQUIRES_SUPER;

AS_CATEGORY_IMPLEMENTABLE
- (void)didExitVisibleState ASDISPLAYNODE_REQUIRES_SUPER;

AS_CATEGORY_IMPLEMENTABLE
- (void)didEnterDisplayState ASDISPLAYNODE_REQUIRES_SUPER;

AS_CATEGORY_IMPLEMENTABLE
- (void)didExitDisplayState ASDISPLAYNODE_REQUIRES_SUPER;

AS_CATEGORY_IMPLEMENTABLE
- (void)didEnterPreloadState ASDISPLAYNODE_REQUIRES_SUPER;

AS_CATEGORY_IMPLEMENTABLE
- (void)didExitPreloadState ASDISPLAYNODE_REQUIRES_SUPER;

AS_CATEGORY_IMPLEMENTABLE
- (void)interfaceStateDidChange:(ASInterfaceState)newState
fromState:(ASInterfaceState)oldState ASDISPLAYNODE_REQUIRES_SUPER;

Expand All @@ -179,6 +205,7 @@ NS_ASSUME_NONNULL_BEGIN
*
* @discussion Subclasses can override this method to react to a trait collection change.
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)asyncTraitCollectionDidChange;

#pragma mark - Drawing
Expand Down
1 change: 1 addition & 0 deletions Source/ASDisplayNode+Yoga.mm
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ - (void)calculateLayoutFromYogaRoot:(ASSizeRange)rootConstrainedSize
return;
}

[self willCalculateLayout:rootConstrainedSize];
[self enumerateInterfaceStateDelegates:^(id<ASInterfaceStateDelegate> _Nonnull delegate) {
if ([delegate respondsToSelector:@selector(nodeWillCalculateLayout:)]) {
[delegate nodeWillCalculateLayout:rootConstrainedSize];
Expand Down
10 changes: 9 additions & 1 deletion Source/ASDisplayNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ AS_EXTERN NSInteger const ASDefaultDrawingPriority;
*
*/

@interface ASDisplayNode : NSObject <ASLocking>
@interface ASDisplayNode : NSObject <ASLocking> {
@public
/**
* The _context ivar is unused by Texture, but provided to enable advanced clients to make powerful extensions to base class functionality.
* For example, _context can be used to implement category methods on ASDisplayNode that add functionality to all node subclass types.
* Code demonstrating this technique can be found in the CatDealsCollectionView example.
*/
void *_context;
}

/** @name Initializing a node object */

Expand Down
Loading

0 comments on commit 6e7cdea

Please sign in to comment.