-
Notifications
You must be signed in to change notification settings - Fork 3
/
Utils.m
104 lines (85 loc) · 2.69 KB
/
Utils.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#define trace NSLog
#define stringify2(macro) #macro
#define stringify(macro) stringify2(macro)
CGImageRef createAppIcon(CGColorRef background,CGColorRef stroke,CGColorRef fill)
{
CGRect rect=CGRectMake(0,0,1024,1024);
CGColorSpaceRef space=CGColorSpaceCreateDeviceRGB();
CGContextRef context=CGBitmapContextCreate(NULL,1024,1024,8,1024*4,space,kCGImageAlphaPremultipliedFirst);
CFRelease(space);
// TODO: doesn't precisely match Apple's template, but neither does NSIconGenericApplication, so..
CALayer* container=CALayer.layer;
container.frame=rect;
CALayer* round=CALayer.layer;
round.frame=CGRectMake(100,100,824,824);
round.backgroundColor=background;
round.cornerRadius=186;
if(@available(macOS 10.15,*))
{
round.cornerCurve=kCACornerCurveContinuous;
}
round.shadowOpacity=0.25;
round.shadowRadius=10;
round.shadowOffset=CGSizeMake(0,-10);
[container addSublayer:round];
[container renderInContext:context];
CGContextSetLineJoin(context,kCGLineJoinRound);
CGContextSetLineWidth(context,36);
CGContextSetTextDrawingMode(context,kCGTextFillStroke);
CGContextSetFillColorWithColor(context,fill);
CGContextSetStrokeColorWithColor(context,stroke);
CGContextSelectFont(context,"Futura-Bold",650,kCGEncodingMacRoman);
CGContextShowTextAtPoint(context,290,410,"y",1);
CGImageRef image=CGBitmapContextCreateImage(context);
CFRelease(context);
return image;
}
NSString* getAppName()
{
return NSProcessInfo.processInfo.arguments[0].lastPathComponent;
}
NSURL* getTempURL()
{
NSString* name=[NSString stringWithFormat:@"%@.%ld.txt",getAppName(),(long)(NSDate.date.timeIntervalSince1970*NSEC_PER_SEC)];
return [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:name]];
}
void alert(NSString* message)
{
NSAlert* alert=NSAlert.alloc.init.autorelease;
alert.messageText=getAppName();
alert.informativeText=message;
alert.runModal;
}
__attribute__((noreturn)) void alertAbort(NSString* message)
{
alert([NSString stringWithFormat:@"fatal: %@",message]);
trace(@"%@ %@",message,NSThread.callStackSymbols);
exit(1);
}
void swizzle(NSString* className,NSString* selName,BOOL isInstance,IMP newImp,IMP* oldImpOut)
{
Class class=NSClassFromString(className);
if(!class)
{
alertAbort(@"swizzle class missing");
}
SEL sel=NSSelectorFromString(selName);
Method method=isInstance?class_getInstanceMethod(class,sel):class_getClassMethod(class,sel);
if(!method)
{
alertAbort(@"swizzle method missing");
}
IMP oldImp=method_setImplementation(method,newImp);
if(oldImpOut)
{
*oldImpOut=oldImp;
}
}
id returnNil()
{
return nil;
}
// TODO: hack to compile on older macOS (10.9+ but not in headers..?)
@interface NSView()
-(void)setClipsToBounds:(BOOL)value;
@end