-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ASTraitCollection] Add missing properties to ASTraitCollection #625
Changes from 1 commit
70fbac1
5afe689
e0c395e
185a139
10f3d8f
663d272
11dc888
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// ASContentSizeCategory.h | ||
// Texture | ||
// | ||
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import <AsyncDisplayKit/ASBaseDefines.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/** | ||
* ASContentSizeCategory is a UIContentSizeCategory that can be used in a struct. | ||
*/ | ||
typedef NS_ENUM(NSInteger, ASContentSizeCategory) { | ||
ASContentSizeCategoryUnspecified, | ||
ASContentSizeCategoryExtraSmall, | ||
ASContentSizeCategorySmall, | ||
ASContentSizeCategoryMedium, | ||
ASContentSizeCategoryLarge, | ||
ASContentSizeCategoryExtraLarge, | ||
ASContentSizeCategoryExtraExtraLarge, | ||
ASContentSizeCategoryExtraExtraExtraLarge, | ||
|
||
// Accessibility sizes | ||
ASContentSizeCategoryAccessibilityMedium, | ||
ASContentSizeCategoryAccessibilityLarge, | ||
ASContentSizeCategoryAccessibilityExtraLarge, | ||
ASContentSizeCategoryAccessibilityExtraExtraLarge, | ||
ASContentSizeCategoryAccessibilityExtraExtraExtraLarge | ||
}; | ||
|
||
/** | ||
* Mapping from UIContentSizeCategory | ||
*/ | ||
extern ASContentSizeCategory ASContentSizeCategoryFromUIContentSizeCategory(UIContentSizeCategory contentSizeCategory); | ||
|
||
/** | ||
* Mapping to UIContentSizeCategory | ||
*/ | ||
extern UIContentSizeCategory UIContentSizeCategoryFromASContentSizeCategory(ASContentSizeCategory contentSizeCategory); | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// ASContentSizeCategory.m | ||
// Texture | ||
// | ||
// Copyright (c) 2017-present, Pinterest, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import <AsyncDisplayKit/ASContentSizeCategory.h> | ||
#import <AsyncDisplayKit/ASAvailability.h> | ||
|
||
// UIContentSizeCategoryUnspecified is available only in iOS 10.0 and later. | ||
// This constant is used as a fallback for older iOS versions. | ||
UIContentSizeCategory const AS_UIContentSizeCategoryUnspecified = @"_UICTContentSizeCategoryUnspecified"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this static? |
||
|
||
/** | ||
* Defines a dictionary of pairs of corresponding ASContentSizeCategory and UIContentSizeCategory values. | ||
*/ | ||
ASDISPLAYNODE_INLINE NSDictionary<NSNumber *, UIContentSizeCategory> *_as_contentSizeCategory_correspondingValues() { | ||
static NSDictionary<NSNumber *, UIContentSizeCategory> *correspondingValues; | ||
|
||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
correspondingValues = | ||
@{ | ||
// We will fallback to ASContentSizeCategoryUnspecified so there is no need to include it in this dictionary. | ||
|
||
[NSNumber numberWithInteger:ASContentSizeCategoryExtraSmall]: UIContentSizeCategoryExtraSmall, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use object literals in this case? |
||
[NSNumber numberWithInteger:ASContentSizeCategorySmall]: UIContentSizeCategorySmall, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryMedium]: UIContentSizeCategoryMedium, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryLarge]: UIContentSizeCategoryLarge, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryExtraLarge]: UIContentSizeCategoryExtraLarge, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryExtraExtraLarge]: UIContentSizeCategoryExtraExtraLarge, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryExtraExtraExtraLarge]: UIContentSizeCategoryExtraExtraExtraLarge, | ||
|
||
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityMedium]: UIContentSizeCategoryAccessibilityMedium, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityLarge]: UIContentSizeCategoryAccessibilityLarge, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityExtraLarge]: UIContentSizeCategoryAccessibilityExtraLarge, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityExtraExtraLarge]: UIContentSizeCategoryAccessibilityExtraExtraLarge, | ||
[NSNumber numberWithInteger:ASContentSizeCategoryAccessibilityExtraExtraExtraLarge]: UIContentSizeCategoryAccessibilityExtraExtraExtraLarge, | ||
}; | ||
}); | ||
|
||
return correspondingValues; | ||
} | ||
|
||
ASContentSizeCategory ASContentSizeCategoryFromUIContentSizeCategory(UIContentSizeCategory contentSizeCategory) { | ||
if (!contentSizeCategory) { | ||
return ASContentSizeCategoryUnspecified; | ||
} | ||
|
||
NSNumber *key = [[_as_contentSizeCategory_correspondingValues() allKeysForObject:contentSizeCategory] firstObject]; | ||
if (key) { | ||
return key.integerValue; | ||
} | ||
else { | ||
return ASContentSizeCategoryUnspecified; | ||
} | ||
} | ||
|
||
UIContentSizeCategory UIContentSizeCategoryFromASContentSizeCategory(ASContentSizeCategory contentSizeCategory) { | ||
UIContentSizeCategory result = _as_contentSizeCategory_correspondingValues()[[NSNumber numberWithInteger:contentSizeCategory]]; | ||
if (result) { | ||
return result; | ||
} else if (AS_AT_LEAST_IOS10) { | ||
return UIContentSizeCategoryUnspecified; | ||
} else { | ||
return AS_UIContentSizeCategoryUnspecified; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
|
||
|
||
#import <UIKit/UIKit.h> | ||
|
||
#import <AsyncDisplayKit/ASContentSizeCategory.h> | ||
#import <AsyncDisplayKit/ASBaseDefines.h> | ||
|
||
@class ASTraitCollection; | ||
|
@@ -30,11 +32,20 @@ ASDISPLAYNODE_EXTERN_C_BEGIN | |
#pragma mark - ASPrimitiveTraitCollection | ||
|
||
typedef struct ASPrimitiveTraitCollection { | ||
CGFloat displayScale; | ||
UIUserInterfaceSizeClass horizontalSizeClass; | ||
UIUserInterfaceIdiom userInterfaceIdiom; | ||
UIUserInterfaceSizeClass verticalSizeClass; | ||
|
||
CGFloat displayScale; | ||
UIDisplayGamut displayGamut; | ||
|
||
UIUserInterfaceIdiom userInterfaceIdiom; | ||
UIForceTouchCapability forceTouchCapability; | ||
UITraitEnvironmentLayoutDirection layoutDirection; | ||
#if TARGET_OS_TV | ||
UIUserInterfaceStyle userInterfaceStyle; | ||
#endif | ||
|
||
ASContentSizeCategory preferredContentSizeCategory; | ||
|
||
CGSize containerSize; | ||
} ASPrimitiveTraitCollection; | ||
|
@@ -124,25 +135,45 @@ ASDISPLAYNODE_EXTERN_C_END | |
AS_SUBCLASSING_RESTRICTED | ||
@interface ASTraitCollection : NSObject | ||
|
||
@property (nonatomic, assign, readonly) CGFloat displayScale; | ||
@property (nonatomic, assign, readonly) UIUserInterfaceSizeClass horizontalSizeClass; | ||
@property (nonatomic, assign, readonly) UIUserInterfaceIdiom userInterfaceIdiom; | ||
@property (nonatomic, assign, readonly) UIUserInterfaceSizeClass verticalSizeClass; | ||
|
||
@property (nonatomic, assign, readonly) CGFloat displayScale; | ||
@property (nonatomic, assign, readonly) UIDisplayGamut displayGamut; | ||
|
||
@property (nonatomic, assign, readonly) UIUserInterfaceIdiom userInterfaceIdiom; | ||
@property (nonatomic, assign, readonly) UIForceTouchCapability forceTouchCapability; | ||
@property (nonatomic, assign, readonly) UITraitEnvironmentLayoutDirection layoutDirection; | ||
#if TARGET_OS_TV | ||
@property (nonatomic, assign, readonly) UIUserInterfaceStyle userInterfaceStyle; | ||
#endif | ||
|
||
@property (nonatomic, assign, readonly) UIContentSizeCategory preferredContentSizeCategory; | ||
|
||
@property (nonatomic, assign, readonly) CGSize containerSize; | ||
|
||
+ (ASTraitCollection *)traitCollectionWithASPrimitiveTraitCollection:(ASPrimitiveTraitCollection)traits; | ||
|
||
+ (ASTraitCollection *)traitCollectionWithUITraitCollection:(UITraitCollection *)traitCollection | ||
containerSize:(CGSize)windowSize; | ||
|
||
|
||
+ (ASTraitCollection *)traitCollectionWithDisplayScale:(CGFloat)displayScale | ||
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom | ||
horizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass | ||
verticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass | ||
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability | ||
containerSize:(CGSize)windowSize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be considered a breaking API change. Instead, I think we should preserve this API and call the full version internally with default values. We can also deprecate it, but that's as far as we can go for now, IMHO. |
||
+ (ASTraitCollection *)traitCollectionWithUITraitCollection:(UITraitCollection *)traitCollection | ||
containerSize:(CGSize)windowSize | ||
fallbackContentSizeCategory:(UIContentSizeCategory)fallbackContentSizeCategory; | ||
|
||
|
||
+ (ASTraitCollection *)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass | ||
verticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass | ||
displayScale:(CGFloat)displayScale | ||
displayGamut:(UIDisplayGamut)displayGamut | ||
userInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom | ||
forceTouchCapability:(UIForceTouchCapability)forceTouchCapability | ||
layoutDirection:(UITraitEnvironmentLayoutDirection)layoutDirection | ||
#if TARGET_OS_TV | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand your reasoning here. However, for maintainability, I think it's best to declare a separate API for tvOS with this extra param. |
||
userInterfaceStyle:(UIUserInterfaceStyle)userInterfaceStyle | ||
#endif | ||
preferredContentSizeCategory:(UIContentSizeCategory)preferredContentSizeCategory | ||
containerSize:(CGSize)windowSize; | ||
|
||
|
||
- (ASPrimitiveTraitCollection)primitiveTraitCollection; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do something clever, and use
__unsafe_unretained UIContentSizeCategory
as a struct member: