-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTweak.xm
366 lines (304 loc) · 11.8 KB
/
Tweak.xm
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "Gravitation.h"
// Custom properties for Gravitation
@interface SBIconListView (Gravitation)
@property (nonatomic, strong)CMMotionManager *gravitation_motionManager;
@property (nonatomic, retain)UIDynamicAnimator *gravitation_gravitationAnimator;
@property (nonatomic, retain)UIGravityBehavior *gravitation_gravitationBehavior;
@property (nonatomic, retain)UIFieldBehavior *gravitation_fingerGravBehavior;
@property (nonatomic, retain)UICollisionBehavior *gravitation_gravitationCollisionBehavior;
@property (nonatomic, assign)BOOL gravitation_observersAdded;
@property (nonatomic, assign)BOOL gravitation_isReadyForMemoryFuck;
@end
// #pragma Globals
// If the icons are currently unhinged
static BOOL _rtGravityActive = NO;
// Preferences
static BOOL _pfTweakEnabled = YES;
static BOOL _pfFingerGravityEnabled = YES;
//static BOOL _pfConglomerate = YES; // Try and find a way to let all icons into viewframe
NSDictionary *prefs = nil;
// Global toggle for animations
// #pragma Toggle Animations
void toggleAnimations()
{
if (!_rtGravityActive && _pfTweakEnabled)[[NSNotificationCenter defaultCenter] postNotificationName:@"GravitationStart" object:nil];
else [[NSNotificationCenter defaultCenter] postNotificationName:@"GravitationStop" object:nil];
_rtGravityActive = !_rtGravityActive;
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
//
// Hooks
// #pragma Hooks -=-=-=-=-=-=-
//
//
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
%hook SBIconListView
%property (nonatomic, strong)CMMotionManager *gravitation_motionManager;
%property (nonatomic, retain)UIDynamicAnimator *gravitation_gravitationAnimator;
%property (nonatomic, retain)UIGravityBehavior *gravitation_gravitationBehavior;
%property (nonatomic, retain)UICollisionBehavior *gravitation_gravitationCollisionBehavior;
%property (nonatomic, retain)UIFieldBehavior *gravitation_fingerGravBehavior;
%property (nonatomic, assign)BOOL gravitation_observersAdded;
%property (nonatomic, assign)BOOL gravitation_isReadyForMemoryFuck;
- (id)init
{
id o = %orig;
return o;
}
- (void)layoutIconsNow
{
%orig;
if (!self.gravitation_observersAdded)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gravitation_startAnimations)name:@"GravitationStart" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gravitation_endAnimations)name:@"GravitationStop" object:nil];
self.gravitation_observersAdded=YES;
}
}
//
// Hack to force all icon views to always be loaded into memory even
// when they are off screen.
//
// Default iOS behavior is to reuse icon views, which is smart, but
// completely breaks the gravity engine.
//
// #pragma IconHack
//
- (void)setVisibleColumnRange:(NSRange)range
{
// We need to wait until icons have been initially loaded, at least, to do this.
// Otherwise it will completely screw up icon labels (wtf apple??)
if (self.gravitation_isReadyForMemoryFuck && range.length == 0)range.length = self.iconsInRowForSpacingCalculation;
if (!_rtGravityActive)%orig(range);
}
// Hook both setter and getter in case they aren't called consecutively
- (NSRange)visibleColumnRange
{
NSRange range = %orig;
if (self.gravitation_isReadyForMemoryFuck && range.length == 0)range.length = self.iconsInRowForSpacingCalculation;
return range;
}
// ios 12 version of the icon hack above
// needs work
- (void)showIconImagesFromColumn:(NSInteger)arg1 toColumn:(NSInteger)arg2 totalColumns:(NSInteger)arg3 allowAnimations:(BOOL)arg4
{
if (!(arg3==4))
{
%orig(0,3,4,NO);
}
else
%orig(arg1, arg2, arg3, arg4);
}
// #pragma Animation Start/Stop
%new
- (void)gravitation_startAnimations
{
if (_pfFingerGravityEnabled)[self setValue:@NO forKey:@"deliversTouchesForGesturesToSuperview"];
self.gravitation_isReadyForMemoryFuck = YES;
if ([self respondsToSelector:@selector(setVisibleColumnRange:)])
[self setVisibleColumnRange:NSMakeRange(0,4)];
else
[self showIconImagesFromColumn:0 toColumn:3 totalColumns:4 allowAnimations:NO];
[self layoutIconsNow];
//UIView *refView = (_pfConglomerate)? self.superview : self;
self.gravitation_gravitationAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self];
self.gravitation_gravitationBehavior = [[UIGravityBehavior alloc] initWithItems:@[]];
self.gravitation_gravitationCollisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[]];
self.gravitation_gravitationCollisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
self.gravitation_gravitationCollisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
self.gravitation_gravitationCollisionBehavior.collisionDelegate = self;
if (self.gravitation_gravitationBehavior)
{
for (UIView *i in self.subviews)
{
if (![i.description containsString:@"SBIcon"])continue;
if (![self.gravitation_gravitationBehavior.items containsObject:i])
{
[self.gravitation_gravitationBehavior addItem:i];
}
}
}
if (self.gravitation_gravitationCollisionBehavior)
{
for (UIView *i in self.subviews)
{
if (![i.description containsString:@"SBIcon"])continue;
if (![self.gravitation_gravitationCollisionBehavior.items containsObject:i])
{
[self.gravitation_gravitationCollisionBehavior addItem:i];
}
}
}
__weak SBIconListView *weakSelf = self;
self.gravitation_motionManager = [[CMMotionManager alloc] init];
[self.gravitation_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error)
{
if (error != nil)
{
NSLog(@"Error %@",error);
return;
}
weakSelf.gravitation_gravitationBehavior.gravityDirection = CGVectorMake(motion.gravity.x * 3, -motion.gravity.y * 3);
}
];
[self.gravitation_gravitationAnimator addBehavior:self.gravitation_gravitationCollisionBehavior];
[self.gravitation_gravitationAnimator addBehavior:self.gravitation_gravitationBehavior];
}
%new
- (void)gravitation_endAnimations
{
[self setValue:@YES forKey:@"deliversTouchesForGesturesToSuperview"];
[self.gravitation_gravitationAnimator removeAllBehaviors];
[UIView animateWithDuration:1.0
delay:0.0
usingSpringWithDamping:.8
initialSpringVelocity:5
options:UIViewAnimationOptionCurveLinear
animations:^{
for (UIView *obj in self.subviews)
{
CGPoint origin = [self originForIconAtIndex:[self.subviews indexOfObject:obj]];
[(UIView *)obj setTransform:CGAffineTransformIdentity];
[(UIView *)obj setFrame:CGRectMake(origin.x, origin.y, ((UIView *)obj).frame.size.width, ((UIView *)obj).frame.size.height)];
}
}
completion:^(BOOL finished)
{
}];
self.gravitation_gravitationAnimator = nil;
self.gravitation_gravitationBehavior = nil;
self.gravitation_gravitationCollisionBehavior = nil;
self.gravitation_motionManager = nil;
}
// #pragma Collision Behavior
%new
- (void)collisionBehavior:(UICollisionBehavior *)behavior
beganContactForItem:(id)item
withBoundaryIdentifier:(id)identifier
atPoint:(CGPoint)p
{
// I'm leaving this here because eventually something fun can be done with it
}
%new
- (void)collisionBehavior:(UICollisionBehavior *)behavior
endedContactForItem:(id)item
withBoundaryIdentifier:(id)identifier
{
// see previous comment
}
// #pragma Touch Handling
- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event
{
if (!_pfFingerGravityEnabled)return;
UITouch *touch = [[event allTouches] anyObject];
CGPoint point = [touch locationInView:touch.view];
if (self.gravitation_fingerGravBehavior)
self.gravitation_fingerGravBehavior.position = point;
if (!self.gravitation_fingerGravBehavior)
{
self.gravitation_fingerGravBehavior = [UIFieldBehavior radialGravityFieldWithPosition:point];
for (UIView *v in self.subviews)
{
[self.gravitation_fingerGravBehavior addItem:v];
}
self.gravitation_fingerGravBehavior.strength =50;
self.gravitation_fingerGravBehavior.minimumRadius=100;
self.gravitation_fingerGravBehavior.region = UIRegion.infiniteRegion; // swift? in my objc? more likely than you think!
[self.gravitation_gravitationAnimator addBehavior:self.gravitation_fingerGravBehavior];
}
}
- (void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
{
if (!_pfFingerGravityEnabled)return;
UITouch *touch = [[event allTouches] anyObject];
CGPoint point = [touch locationInView:touch.view];
CGFloat force = (touch.force == 0)? 2 : touch.force;
if (self.gravitation_fingerGravBehavior)
{
self.gravitation_fingerGravBehavior.strength =50*force;
self.gravitation_fingerGravBehavior.position = point;
}
}
- (void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
if (!_pfFingerGravityEnabled)return;
[self.gravitation_gravitationAnimator removeBehavior:self.gravitation_fingerGravBehavior];
self.gravitation_fingerGravBehavior = nil;
}
%end
// #pragma Gesture Handler
%hook SBHomeScreenWindow
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
%orig;
if(event.type == UIEventSubtypeMotionShake)
{
toggleAnimations();
}
}
%end
%hook SBRootFolderView
// IDK if this actually helps us, but it cant hurt? /shrug
- (BOOL)hidesOffscreenCustomPageViews
{
return NO;
}
%end
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//
//
// Gravitation Preferences
// #pragma Preferences -=-=-=-=-=-=-
//
//
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#define kIdentifier @"me.kritanta.gravitationprefs"
#define kSettingsChangedNotification (CFStringRef)@"me.kritanta.gravitationprefs/Prefs"
#define kSettingsPath @"/var/mobile/Library/Preferences/me.kritanta.gravitationprefs.plist"
static void *observer = NULL;
static void reloadPrefs()
{
if ([NSHomeDirectory()isEqualToString:@"/var/mobile"])
{
CFArrayRef keyList = CFPreferencesCopyKeyList((CFStringRef)kIdentifier, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
if (keyList)
{
prefs = (NSDictionary *)CFBridgingRelease(CFPreferencesCopyMultiple(keyList, (CFStringRef)kIdentifier, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
if (!prefs)
{
prefs = [NSDictionary new];
}
CFRelease(keyList);
}
}
else
{
prefs = [NSDictionary dictionaryWithContentsOfFile:kSettingsPath];
}
}
static void preferencesChanged()
{
CFPreferencesAppSynchronize((CFStringRef)kIdentifier);
reloadPrefs();
_pfTweakEnabled = [prefs objectForKey:@"tweakEnabled"] ? [[prefs valueForKey:@"tweakEnabled"] boolValue] : YES;
_pfFingerGravityEnabled = [prefs objectForKey:@"fingerGravity"] ? [[prefs valueForKey:@"fingerGravity"] boolValue] : YES;
}
// #pragma constructor
%ctor
{
preferencesChanged();
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
&observer,
(CFNotificationCallback)preferencesChanged,
(CFStringRef)@"me.kritanta.gravitation/Prefs",
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately
);
NSLog(@"Gravitation: Initialized");
}