ScriptControllerIconView: Update to reflect policy type
This commit is contained in:
@@ -6,10 +6,11 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class UIImage;
|
||||
@class UIImage, UIColor;
|
||||
|
||||
typedef NS_ENUM(NSInteger, SBRScriptOriginPolicyType) {
|
||||
SBRScriptOriginPolicyTypeAlpha,
|
||||
@@ -19,6 +20,15 @@ typedef NS_ENUM(NSInteger, SBRScriptOriginPolicyType) {
|
||||
SBRScriptOriginPolicyTypeEcho,
|
||||
} NS_SWIFT_NAME(ScriptPolicy.PolicyType);
|
||||
|
||||
NS_SWIFT_NAME(ScriptPolicy.IconConfiguration)
|
||||
@interface SBRScriptPolicyIconConfiguration : NSObject
|
||||
@property (nonatomic, readwrite) CGSize size;
|
||||
@property (nonatomic, strong, nullable) UIColor *foregroundColor;
|
||||
@property (nonatomic, strong, nullable) UIColor *backgroundColor;
|
||||
|
||||
- (instancetype)initWithSize:(CGSize)size foregroundColor:(nullable UIColor *)foregroundColor backgroundColor:(nullable UIColor *)backgroundColor;
|
||||
@end
|
||||
|
||||
NS_SWIFT_NAME(ScriptPolicy)
|
||||
@interface SBRScriptPolicy : NSObject <NSSecureCoding>
|
||||
@property (nonatomic, copy) NSString *origin;
|
||||
@@ -30,8 +40,8 @@ NS_SWIFT_NAME(ScriptPolicy)
|
||||
+ (NSString *)localizedDescriptionForPolicyType:(SBRScriptOriginPolicyType)policyType
|
||||
NS_SWIFT_NAME(localizedDescription(forPolicyType:));
|
||||
|
||||
+ (UIImage *)iconRepresentationForPolicyType:(SBRScriptOriginPolicyType)policyType withSize:(CGSize)size
|
||||
NS_SWIFT_NAME(iconRepresentation(forPolicyType:size:));
|
||||
+ (UIImage *)iconRepresentationForPolicyType:(SBRScriptOriginPolicyType)policyType withConfiguration:(SBRScriptPolicyIconConfiguration *)configuration
|
||||
NS_SWIFT_NAME(iconRepresentation(forPolicyType:configuration:));
|
||||
|
||||
- (instancetype)initWithSecurityOrigin:(NSString *)origin policyType:(SBRScriptOriginPolicyType)policyType;
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@
|
||||
// For icon drawing
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
static CGFloat RoundToScale(CGFloat v, CGFloat s) {
|
||||
return round(v * s) / s;
|
||||
}
|
||||
|
||||
@implementation SBRScriptPolicy
|
||||
|
||||
+ (NSSet<NSString *> *)_commonCDNOrigins {
|
||||
@@ -70,17 +74,20 @@
|
||||
}
|
||||
}
|
||||
|
||||
+ (UIImage *)iconRepresentationForPolicyType:(SBRScriptOriginPolicyType)policyType withSize:(CGSize)size
|
||||
+ (UIImage *)iconRepresentationForPolicyType:(SBRScriptOriginPolicyType)policyType withConfiguration:(nonnull SBRScriptPolicyIconConfiguration *)configuration
|
||||
{
|
||||
const CGSize size = [configuration size];
|
||||
UIFont *font = [UIFont boldSystemFontOfSize:size.height - 2];
|
||||
NSDictionary<NSAttributedStringKey, id> *attrs = @{
|
||||
NSFontAttributeName : font,
|
||||
NSForegroundColorAttributeName : [UIColor whiteColor]
|
||||
NSForegroundColorAttributeName : [configuration foregroundColor] ?: [UIColor whiteColor],
|
||||
};
|
||||
|
||||
const CGFloat scale = UIScreen.mainScreen.scale;
|
||||
const CGRect rect = (CGRect) { .origin = CGPointZero, .size = size };
|
||||
UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
|
||||
UIGraphicsBeginImageContextWithOptions(size, NO, scale);
|
||||
|
||||
[([configuration backgroundColor] ?: [UIColor blackColor]) setFill];
|
||||
UIBezierPath *backgroundPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:4.0];
|
||||
[backgroundPath fill];
|
||||
|
||||
@@ -98,11 +105,14 @@
|
||||
character = @"𝝴"; break;
|
||||
}
|
||||
|
||||
const CGSize charSize = [character sizeWithAttributes:attrs];
|
||||
CGSize charSize = [character sizeWithAttributes:attrs];
|
||||
charSize.width = RoundToScale(charSize.width, scale);
|
||||
charSize.height = RoundToScale(charSize.height, scale);
|
||||
|
||||
const CGRect charRect = (CGRect) {
|
||||
.origin = (CGPoint) {
|
||||
.x = (size.width - charSize.width) / 2,
|
||||
.y = -(charSize.height - size.height) / 2,
|
||||
.y = -(charSize.height - size.height) / 2,
|
||||
},
|
||||
|
||||
.size = charSize
|
||||
@@ -203,3 +213,18 @@
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation SBRScriptPolicyIconConfiguration
|
||||
|
||||
- (instancetype)initWithSize:(CGSize)size foregroundColor:(nullable UIColor *)foregroundColor backgroundColor:(nullable UIColor *)backgroundColor
|
||||
{
|
||||
if (self = [super init]) {
|
||||
_size = size;
|
||||
_foregroundColor = foregroundColor;
|
||||
_backgroundColor = backgroundColor;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user