Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhuy committed Jun 6, 2017
1 parent a8f1bbf commit 061e5ee
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Source/ASDisplayNode+Layout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ - (void)_locked_setCalculatedDisplayNodeLayout:(std::shared_ptr<ASDisplayNodeLay
ASDisplayNodeAssertTrue(displayNodeLayout->layout.size.height >= 0.0);

// Flatten the layout if it wasn't done before (@see -calculateLayoutThatFits:).
if (ASDisplayNode.shouldStoreUnflattenedLayouts) {
if ([ASDisplayNode shouldStoreUnflattenedLayouts]) {
_unflattenedLayout = displayNodeLayout->layout;
displayNodeLayout->layout = [_unflattenedLayout filteredNodeLayoutTree];
}
Expand Down
19 changes: 15 additions & 4 deletions Source/ASDisplayNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,25 @@ extern NSInteger const ASDefaultDrawingPriority;
@interface ASDisplayNode (Debugging) <ASDebugNameProvider>

/**
* Whether ASDisplayNode should store their unflattened layouts. The layout can be accessed via `-unflattenedCalculatedLayout`.
* Flattened layouts use less memory and are faster to lookup, while unflattened ones are useful for debugging
* because they reserve original information.
* Set to YES to tell all ASDisplayNode instances to store their unflattened layouts.
*
* Defaults to NO.
* The layout can be accessed via `-unflattenedCalculatedLayout`.
*
* Flattened layouts use less memory and are faster to lookup. On the other hand, unflattened layouts are useful for debugging
* because they preserve original information.
*/
+ (void)setShouldStoreUnflattenedLayouts:(BOOL)shouldStore;

/**
* Whether or not ASDisplayNode instances should store their unflattened layouts.
*
* The layout can be accessed via `-unflattenedCalculatedLayout`.
*
* Flattened layouts use less memory and are faster to lookup. On the other hand, unflattened layouts are useful for debugging
* because they preserve original information.
*
* Defaults to NO.
*/
+ (BOOL)shouldStoreUnflattenedLayouts;

@property (nonatomic, strong, readonly, nullable) ASLayout *unflattenedCalculatedLayout;
Expand Down
9 changes: 5 additions & 4 deletions Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ @implementation ASDisplayNode
@synthesize threadSafeBounds = _threadSafeBounds;

static BOOL suppressesInvalidCollectionUpdateExceptions = NO;
static BOOL storesUnflattenedLayouts = NO;
static std::atomic_bool storesUnflattenedLayouts = ATOMIC_VAR_INIT(NO);

+ (BOOL)suppressesInvalidCollectionUpdateExceptions
{
Expand Down Expand Up @@ -1042,7 +1042,7 @@ - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize

// Return the (original) unflattened layout if it needs to be stored. The layout will be flattened later on (@see _locked_setCalculatedDisplayNodeLayout:).
// Otherwise, flatten it right away.
if (! storesUnflattenedLayouts) {
if (! [ASDisplayNode shouldStoreUnflattenedLayouts]) {
layout = [layout filteredNodeLayoutTree];
}

Expand Down Expand Up @@ -3296,16 +3296,17 @@ @implementation ASDisplayNode (Debugging)

+ (void)setShouldStoreUnflattenedLayouts:(BOOL)shouldStore
{
storesUnflattenedLayouts = shouldStore;
storesUnflattenedLayouts.store(shouldStore);
}

+ (BOOL)shouldStoreUnflattenedLayouts
{
return storesUnflattenedLayouts;
return storesUnflattenedLayouts.load();
}

- (ASLayout *)unflattenedCalculatedLayout
{
ASDN::MutexLocker l(__instanceLock__);
return _unflattenedLayout;
}

Expand Down
10 changes: 8 additions & 2 deletions Source/Layout/ASLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,17 @@ ASDISPLAYNODE_EXTERN_C_END
@interface ASLayout (Debugging)

/**
* Set to YES to tell all ASLayout instances to retain their sublayouts. Defaults to NO.
* Can be overridden at instance level.
* Set to YES to tell all ASLayout instances to retain their sublayout elements. Defaults to NO.
* Can be overridden at instance level.
*/
+ (void)setShouldRetainSublayoutLayoutElements:(BOOL)shouldRetain;

/**
* Whether or not ASLayout instances should retain their sublayout elements.
* Can be overridden at instance level.
*/
+ (BOOL)shouldRetainSublayoutLayoutElements;

/**
* Recrusively output the description of the layout tree.
*/
Expand Down
11 changes: 8 additions & 3 deletions Source/Layout/ASLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,16 @@ @implementation ASLayout

@dynamic frame, type;

static BOOL static_retainsSublayoutLayoutElements = NO;
static std::atomic_bool static_retainsSublayoutLayoutElements = ATOMIC_VAR_INIT(NO);

+ (void)setShouldRetainSublayoutLayoutElements:(BOOL)shouldRetain
{
static_retainsSublayoutLayoutElements = shouldRetain;
static_retainsSublayoutLayoutElements.store(shouldRetain);
}

+ (BOOL)shouldRetainSublayoutLayoutElements
{
return static_retainsSublayoutLayoutElements.load();
}

- (instancetype)initWithLayoutElement:(id<ASLayoutElement>)layoutElement
Expand Down Expand Up @@ -125,7 +130,7 @@ - (instancetype)initWithLayoutElement:(id<ASLayoutElement>)layoutElement
}

_flattened = NO;
self.retainSublayoutLayoutElements = static_retainsSublayoutLayoutElements;
self.retainSublayoutLayoutElements = [ASLayout shouldRetainSublayoutLayoutElements];
}

return self;
Expand Down

0 comments on commit 061e5ee

Please sign in to comment.