forked from rpetrich/IconRotator
/
Tweak.x
136 lines (120 loc) · 3.88 KB
/
Tweak.x
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#import <SpringBoard/SpringBoard.h>
#import <QuartzCore/QuartzCore.h>
#import <notify.h>
#import <CaptainHook/CaptainHook.h>
static CFMutableSetRef icons;
static CATransform3D currentTransform;
static CGFloat reflectionOpacity;
static int notify_token;
static uint64_t lastOrientation;
@interface SBIconView : UIView
@end
@interface SBIconView (Springtomize)
- (CGFloat)springtomizeScaleFactor;
@end
static CATransform3D (*ScaledTransform)(SBIconView *);
static CATransform3D ScaledTransformSpringtomize(SBIconView *iconView)
{
CGFloat scale = [iconView springtomizeScaleFactor];
return CATransform3DScale(currentTransform, scale, scale, 1.0f);
}
static CATransform3D ScaledTransformDefault(SBIconView *iconView)
{
return currentTransform;
}
%hook SBIconView
- (void)didMoveToWindow
{
%orig;
if (self.window) {
CFSetSetValue(icons, self);
self.layer.sublayerTransform = ScaledTransform(self);
CHIvar(self, _reflection, UIImageView *).alpha = reflectionOpacity;
} else {
CFSetRemoveValue(icons, self);
}
}
- (void)didMoveToSuperview
{
%orig;
if (self.superview) {
self.layer.sublayerTransform = ScaledTransform(self);
}
}
- (void)dealloc
{
CFSetRemoveValue(icons, self);
%orig;
}
%end
%hook SpringBoard
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
%orig;
if ([%c(SBIconView) instancesRespondToSelector:@selector(springtomizeScaleFactor)])
ScaledTransform = ScaledTransformSpringtomize;
// This code is quite evil
SBAccelerometerInterface *accelerometer = [%c(SBAccelerometerInterface) sharedInstance];
NSMutableArray **_clients = CHIvarRef(accelerometer, _clients, NSMutableArray *);
if (_clients) {
NSMutableArray *clients = *_clients;
if (!clients)
*_clients = clients = [[NSMutableArray alloc] init];
SBAccelerometerClient *client = [[%c(SBAccelerometerClient) alloc] init];
if (client) {
[client setUpdateInterval:0.1];
[clients addObject:client];
}
[client release];
}
}
%end
static void OrientationChangedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
uint64_t orientation = 0;
notify_get_state(notify_token, &orientation);
if (orientation == lastOrientation)
return;
switch (orientation) {
case UIDeviceOrientationPortrait:
currentTransform = CATransform3DIdentity;
reflectionOpacity = 1.0f;
break;
case UIDeviceOrientationPortraitUpsideDown:
currentTransform = CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
break;
case UIDeviceOrientationLandscapeLeft:
currentTransform = CATransform3DMakeRotation(0.5f * M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
break;
case UIDeviceOrientationLandscapeRight:
currentTransform = CATransform3DMakeRotation(-0.5f * M_PI, 0.0f, 0.0f, 1.0f);
reflectionOpacity = 0.0f;
break;
default:
return;
}
lastOrientation = orientation;
for (SBIconView *view in (id)icons) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"sublayerTransform"];
CATransform3D transform = ScaledTransform(view);
animation.toValue = [NSValue valueWithCATransform3D:transform];
animation.duration = 0.2;
animation.removedOnCompletion = YES;
CALayer *layer = view.layer;
animation.fromValue = [NSValue valueWithCATransform3D:layer.sublayerTransform];
layer.sublayerTransform = transform;
[layer addAnimation:animation forKey:@"IconRotator"];
CHIvar(view, _reflection, UIImageView *).alpha = reflectionOpacity;
}
}
%ctor
{
%init;
ScaledTransform = ScaledTransformDefault;
currentTransform = CATransform3DIdentity;
icons = CFSetCreateMutable(kCFAllocatorDefault, 0, NULL);
notify_register_check("com.apple.springboard.rawOrientation", ¬ify_token);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, OrientationChangedCallback, CFSTR("com.apple.springboard.rawOrientation"), NULL, CFNotificationSuspensionBehaviorCoalesce);
}