Skip to content

Commit

Permalink
First pass at supporting new traits (#1568)
Browse files Browse the repository at this point in the history
  • Loading branch information
ay8s authored and rahul-malik committed Jan 2, 2020
1 parent 6a2fdb2 commit 005e2d5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Source/Details/ASTraitCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ typedef struct {
unowned UIContentSizeCategory preferredContentSizeCategory API_AVAILABLE(ios(10.0));

CGSize containerSize;

UIUserInterfaceLevel userInterfaceLevel API_AVAILABLE(ios(13.0));
UIAccessibilityContrast accessibilityContrast API_AVAILABLE(ios(13.0));
UILegibilityWeight legibilityWeight API_AVAILABLE(ios(13.0));
} ASPrimitiveTraitCollection;
#pragma clang diagnostic pop

Expand Down Expand Up @@ -148,6 +152,10 @@ AS_SUBCLASSING_RESTRICTED

@property (readonly) CGSize containerSize;

@property (readonly) UIUserInterfaceLevel userInterfaceLevel API_AVAILABLE(ios(13.0));
@property (readonly) UIAccessibilityContrast accessibilityContrast API_AVAILABLE(ios(13.0));
@property (readonly) UILegibilityWeight legibilityWeight API_AVAILABLE(ios(13.0));

- (BOOL)isEqualToTraitCollection:(ASTraitCollection *)traitCollection;

@end
Expand Down
69 changes: 69 additions & 0 deletions Source/Details/ASTraitCollection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ ASPrimitiveTraitCollection ASPrimitiveTraitCollectionMakeDefault() {
if (AS_AVAILABLE_IOS_TVOS(12, 10)) {
tc.userInterfaceStyle = UIUserInterfaceStyleUnspecified;
}
if(AS_AVAILABLE_IOS(13)){
tc.userInterfaceLevel = UIUserInterfaceLevelUnspecified;
tc.accessibilityContrast = UIAccessibilityContrastUnspecified;
tc.legibilityWeight = UILegibilityWeightUnspecified;
}
return tc;
}

Expand All @@ -61,6 +66,11 @@ ASPrimitiveTraitCollection ASPrimitiveTraitCollectionFromUITraitCollection(UITra
if (AS_AVAILABLE_IOS_TVOS(12, 10)) {
environmentTraitCollection.userInterfaceStyle = traitCollection.userInterfaceStyle;
}
if(AS_AVAILABLE_IOS(13)){
environmentTraitCollection.userInterfaceLevel = traitCollection.userInterfaceLevel;
environmentTraitCollection.accessibilityContrast = traitCollection.accessibilityContrast;
environmentTraitCollection.legibilityWeight = traitCollection.legibilityWeight;
}
return environmentTraitCollection;
}

Expand Down Expand Up @@ -169,6 +179,47 @@ BOOL ASPrimitiveTraitCollectionIsEqualToASPrimitiveTraitCollection(ASPrimitiveTr
}
}

// Named so as not to conflict with a hidden Apple function, in case compiler decides not to inline
API_AVAILABLE(ios(13))
ASDISPLAYNODE_INLINE NSString *AS_NSStringFromUITraitEnvironmentUserInterfaceLevel(UIUserInterfaceLevel userInterfaceLevel) {
switch (userInterfaceLevel) {
case UIUserInterfaceLevelBase:
return @"Base";
case UIUserInterfaceLevelElevated:
return @"Elevated";
default:
return @"Unspecified";
}
}

// Named so as not to conflict with a hidden Apple function, in case compiler decides not to inline
API_AVAILABLE(ios(13))
ASDISPLAYNODE_INLINE NSString *AS_NSStringFromUITraitEnvironmentAccessibilityContrast(UIAccessibilityContrast accessibilityContrast) {
switch (accessibilityContrast) {
case UIAccessibilityContrastNormal:
return @"Normal";
case UIAccessibilityContrastHigh:
return @"High";
default:
return @"Unspecified";
}
}

// Named so as not to conflict with a hidden Apple function, in case compiler decides not to inline
API_AVAILABLE(ios(13))
ASDISPLAYNODE_INLINE NSString *AS_NSStringFromUITraitEnvironmentLegibilityWeight(UILegibilityWeight legibilityWeight) {
switch (legibilityWeight) {
case UILegibilityWeightRegular:
return @"Regular";
case UILegibilityWeightBold:
return @"Bold";
default:
return @"Unspecified";
}
}



NSString *NSStringFromASPrimitiveTraitCollection(ASPrimitiveTraitCollection traits) {
NSMutableArray<NSDictionary *> *props = [NSMutableArray array];
[props addObject:@{ @"verticalSizeClass": AS_NSStringFromUIUserInterfaceSizeClass(traits.verticalSizeClass) }];
Expand All @@ -184,6 +235,11 @@ BOOL ASPrimitiveTraitCollectionIsEqualToASPrimitiveTraitCollection(ASPrimitiveTr
[props addObject:@{ @"preferredContentSizeCategory": traits.preferredContentSizeCategory }];
[props addObject:@{ @"displayGamut": AS_NSStringFromUIDisplayGamut(traits.displayGamut) }];
}
if (AS_AVAILABLE_IOS(13)){
[props addObject:@{ @"userInterfaceLevel": AS_NSStringFromUITraitEnvironmentUserInterfaceLevel(traits.userInterfaceLevel) }];
[props addObject:@{ @"accessibilityContrast": AS_NSStringFromUITraitEnvironmentAccessibilityContrast(traits.accessibilityContrast) }];
[props addObject:@{ @"legibilityWeight": AS_NSStringFromUITraitEnvironmentLegibilityWeight(traits.legibilityWeight) }];
}
[props addObject:@{ @"containerSize": NSStringFromCGSize(traits.containerSize) }];
return ASObjectDescriptionMakeWithoutObject(props);
}
Expand Down Expand Up @@ -244,6 +300,19 @@ - (UIContentSizeCategory)preferredContentSizeCategory
{
return _prim.preferredContentSizeCategory;
}
- (UIUserInterfaceLevel)userInterfaceLevel
{
return _prim.userInterfaceLevel;
}
- (UIAccessibilityContrast)accessibilityContrast
{
return _prim.accessibilityContrast;
}
- (UILegibilityWeight)legibilityWeight
{
return _prim.legibilityWeight;
}

- (NSUInteger)hash {
return ASHashBytes(&_prim, sizeof(ASPrimitiveTraitCollection));
}
Expand Down

0 comments on commit 005e2d5

Please sign in to comment.