From 236551f5a272bb5a416fe8f047f39db1680ade97 Mon Sep 17 00:00:00 2001 From: ay8s Date: Tue, 2 Jul 2019 14:53:00 -0700 Subject: [PATCH] First pass at supporting new traits --- Source/Details/ASTraitCollection.h | 10 ++++- Source/Details/ASTraitCollection.mm | 69 +++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/Source/Details/ASTraitCollection.h b/Source/Details/ASTraitCollection.h index 17e1740c1..32d72a10b 100644 --- a/Source/Details/ASTraitCollection.h +++ b/Source/Details/ASTraitCollection.h @@ -48,6 +48,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 @@ -142,10 +146,14 @@ AS_SUBCLASSING_RESTRICTED #if AS_BUILD_UIUSERINTERFACESTYLE @property (readonly) UIUserInterfaceStyle userInterfaceStyle API_AVAILABLE(tvos(10.0), ios(12.0)); #endif -@property (readonly) UIContentSizeCategory preferredContentSizeCategory API_AVAILABLE(ios(10.0)); +@property (readonly) UIContentSizeCategory preferredContentSizeCategory API_AVAILABLE(ios(10.0)); @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 diff --git a/Source/Details/ASTraitCollection.mm b/Source/Details/ASTraitCollection.mm index e88523921..29bd33267 100644 --- a/Source/Details/ASTraitCollection.mm +++ b/Source/Details/ASTraitCollection.mm @@ -43,6 +43,11 @@ ASPrimitiveTraitCollection ASPrimitiveTraitCollectionMakeDefault() { tc.userInterfaceStyle = UIUserInterfaceStyleUnspecified; } #endif + if(AS_AVAILABLE_IOS(13)){ + tc.userInterfaceLevel = UIUserInterfaceLevelUnspecified; + tc.accessibilityContrast = UIAccessibilityContrastUnspecified; + tc.legibilityWeight = UILegibilityWeightUnspecified; + } return tc; } @@ -65,6 +70,11 @@ ASPrimitiveTraitCollection ASPrimitiveTraitCollectionFromUITraitCollection(UITra environmentTraitCollection.userInterfaceStyle = traitCollection.userInterfaceStyle; } #endif + if(AS_AVAILABLE_IOS(13)){ + environmentTraitCollection.userInterfaceLevel = traitCollection.userInterfaceLevel; + environmentTraitCollection.accessibilityContrast = traitCollection.accessibilityContrast; + environmentTraitCollection.legibilityWeight = traitCollection.legibilityWeight; + } return environmentTraitCollection; } @@ -153,6 +163,47 @@ BOOL ASPrimitiveTraitCollectionIsEqualToASPrimitiveTraitCollection(ASPrimitiveTr } #endif +// 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 *props = [NSMutableArray array]; [props addObject:@{ @"verticalSizeClass": AS_NSStringFromUIUserInterfaceSizeClass(traits.verticalSizeClass) }]; @@ -170,6 +221,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); } @@ -230,6 +286,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)); }