ScriptControllerIconView: Update to reflect policy type

This commit is contained in:
James Magahern
2021-10-21 15:08:04 -07:00
parent fc7380ed21
commit 291abfcd6a
8 changed files with 96 additions and 32 deletions

View File

@@ -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