-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTweak.xm
More file actions
463 lines (389 loc) · 14.5 KB
/
Tweak.xm
File metadata and controls
463 lines (389 loc) · 14.5 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#import "headers.h"
NSMutableDictionary *prefs, *defaultPrefs;
// prefs values
static bool ENABLED = YES;
static int NUM_OPTIONS = 3;
static float CELL_HEIGHT = 40;
static float SEP_WIDTH = 80;
static float SIZE_HEIGHT = 0;
static float WINDOW_WIDTH = 40;
static float FONT_MULTIPLIER = 50;
static UIColor *BACKGROUND_COLOR;// = [UIColor colorWithRed:14.0f/255.0f green:14.0f/255.0f blue:14.0f/255.0f alpha:0.8f];
static UIColor *BORDER_COLOR;// = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
static UIColor *SEPARATOR_COLOR;// = [UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1];
static int TEXT_ALIGNMENT = 2;
static float getFontMultiplier(){
return FONT_MULTIPLIER/100.0f;
}
static UIColor* sepColor(){
loadPrefs();
return SEPARATOR_COLOR == nil ? LCPParseColorString(@"#808080", @"#808080") : SEPARATOR_COLOR;
}
static UIColor* borderColor(){
loadPrefs();
return BORDER_COLOR == nil ? LCPParseColorString(@"#ffffff", @"#ffffff") : BORDER_COLOR;
}
static UIColor* bgColor(){
loadPrefs();
return BACKGROUND_COLOR == nil ? LCPParseColorString(@"#0e0e0e:0.8f", @"#0e0e0e:0.8f") : BACKGROUND_COLOR;
}
CGPoint targetPoint;
int targetDirection;
static float getSizeWidth(){
if([UIScreen mainScreen] != nil){
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float desiredWidth = screenWidth * (WINDOW_WIDTH/100.0f);
// NSLog(@"TARGET %@ %i", NSStringFromCGPoint(targetPoint), targetDirection);
// // none of this should run since we are forcing the arrow to never be this direction
// if(targetDirection == 3){
// // arrow on right
// float maxWidth = (screenWidth - targetPoint.x) * 0.9;
//
// if(desiredWidth > maxWidth){
// NSLog(@"MAXED %f %f", desiredWidth, maxWidth);
// return maxWidth;
// }
// }
//
// if(targetDirection == 0){
// float maxWidth = (targetPoint.x) * 0.9;
//
// if(desiredWidth > maxWidth){
// NSLog(@"MAXED %f %f", desiredWidth, maxWidth);
// return maxWidth;
// }
// }
float maxWidth = screenWidth * 0.95;
if(desiredWidth > maxWidth){
return maxWidth;
}
return desiredWidth;
}
return 0;
}
static float MAX_DEFAULT_HEIGHT(){
if([UIScreen mainScreen] != nil){
float screenHeight = [UIScreen mainScreen].bounds.size.height;
float desiredHeight = NUM_OPTIONS * CELL_HEIGHT;
// NSLog(@"TARGET2 %@ %i %f", NSStringFromCGPoint(targetPoint), targetDirection, screenHeight);
// if(targetDirection == 1){
// // arrow on bottom
// float maxHeight = (screenHeight - targetPoint.y) * 0.9;
//
// if(desiredHeight > maxHeight){
// NSLog(@"MAXEDH %f %f", desiredHeight, maxHeight);
// return maxHeight;
// }
// }
//
// if(targetDirection == 2){
// float maxHeight = (targetPoint.y) * 0.9;
//
// if(desiredHeight > maxHeight){
// NSLog(@"MAXEDH2 %f %f", desiredHeight, maxHeight);
// return maxHeight;
// }
// }
//
float maxHeight = screenHeight * 0.25;
if(desiredHeight > maxHeight){
return maxHeight;
}
return desiredHeight;
}
return 120;
}
static float getSepInset(){
return (1.0f - (SEP_WIDTH/100.0f))/2.0f;
}
static CGSize theSize(){
return CGSizeMake(getSizeWidth(), SIZE_HEIGHT);
}
%hook UICalloutBar
-(id)hitTest:(CGPoint)arg1 withEvent:(id)arg2 {
if(ENABLED){
if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) {
return nil;
}
if ([self pointInside:arg1 withEvent:arg2]) {
for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subview convertPoint:arg1 fromView:self];
UIView *hitTestView = [subview hitTest:convertedPoint withEvent:arg2];
if (hitTestView) {
if([hitTestView isMemberOfClass:%c(UICalloutBarButton)]){
[UIView animateWithDuration:0.2f delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^{
hitTestView.layer.backgroundColor = UIColor.whiteColor.CGColor;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2f delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^{
hitTestView.layer.backgroundColor = UIColor.clearColor.CGColor;
} completion:nil];
}];
}
return hitTestView;
}
}
return self;
}
return nil;
}else{
return %orig;
}
}
-(bool)setFrameForSize:(CGSize)arg1 {
if(ENABLED){
return %orig(theSize());
}else{
return %orig;
}
}
-(bool)calculateControlFrameForCalloutSize:(CGSize)arg1 below:(bool)arg2 {
if(ENABLED){
return %orig(theSize(), arg2);
}else{
return %orig;
}
}
-(bool)calculateControlFrameForCalloutSize:(CGSize)arg1 right:(bool)arg2 {
if(ENABLED){
return %orig(theSize(), arg2);
}else{
return %orig;
}
}
-(bool)calculateControlFrameInsideTargetRect:(CGSize)arg1{
if(ENABLED){
return %orig(theSize());
}else{
return %orig;
}
}
-(void)setTargetHorizontal:(bool)arg1{
%orig(0);
}
+(id)sharedCalloutBar {
UICalloutBar* r = %orig;
if(ENABLED){
MSHookIvar<UIView *>(r, "m_nextButton").hidden = YES;
MSHookIvar<UIView *>(r, "m_previousButton").hidden = YES;
if(viewWithTag(r, 1020) == nil){
CGSize windowSize = theSize();
UIScrollView* sub = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, windowSize.width, windowSize.height)];
sub.tag = 1020;
sub.indicatorStyle = UIScrollViewIndicatorStyleWhite;
r.clipsToBounds = true;
sub.contentSize = CGSizeMake(windowSize.width, windowSize.height);
sub.backgroundColor = bgColor();
sub.layer.borderWidth = 2;
sub.layer.borderColor = borderColor().CGColor;
[r addSubview: sub];
}
return r;
}else{
return %orig;
}
}
-(void)shrinkButtonTextSize:(id)arg1 {
if(ENABLED == NO){
%orig;
}
}
-(int)targetDirection{
int dir = %orig;
targetDirection = dir;
return dir;//targetPoint.y > ([UIScreen mainScreen].bounds.size.height - targetPoint.y) ? 2 : 1;
}
-(void)setTargetPoint:(CGPoint)arg1{
targetPoint = arg1;
%orig;
}
-(CGPoint)targetPoint{
CGPoint point = %orig;
targetPoint = point;
return point;
}
-(void)updateForCurrentPage {
if(ENABLED){
for(UIView* sub in self.subviews){
if([sub isMemberOfClass:%c(UICalloutBarBackground)]){
MSHookIvar<UIView *>(sub, "_separatorView").hidden = YES;
MSHookIvar<UIView *>(sub, "_blurView").hidden = YES;
UIView* tint = MSHookIvar<UIView *>(sub, "_tintView");
tint.hidden = YES;
}
}
int buttonCount = 0;
removeAllSubviewsWithTagRecursive(self, 1022);
for(UIView* sub in self.subviews){
if([sub isMemberOfClass:%c(UICalloutBarButton)]){
float thickness = 1.5;
float insetPercent = getSepInset();
float SEP_WIDTH = sub.superview.frame.size.width;
if(buttonCount > 0){
UIView* sepView = [[UIView alloc] initWithFrame:CGRectMake(SEP_WIDTH * insetPercent, (CELL_HEIGHT*buttonCount)-(thickness/2), SEP_WIDTH - (SEP_WIDTH*insetPercent*2), thickness)];
sepView.backgroundColor = sepColor();
sepView.tag = 1022;
[viewWithTag(self, 1020) addSubview: sepView];
}
sub.frame = CGRectMake(0,CELL_HEIGHT*buttonCount, theSize().width, CELL_HEIGHT);
for(UIView* sub2 in sub.subviews){
if([sub2 isMemberOfClass:%c(UIButtonLabel)]){
UIButtonLabel* labelView = (UIButtonLabel*)sub2;
CGRect newLabelFrame = CGRectMake(0, 0, labelView.superview.frame.size.width, labelView.superview.frame.size.height);
labelView.frame = newLabelFrame;
labelView.numberOfLines = 2;
}
}
if(sub.hidden == NO){
buttonCount += 1;
}
if(sub.superview.tag != 1020){
[sub removeFromSuperview];
[viewWithTag(self, 1020) addSubview: sub];
}
}
}
((UIScrollView*)viewWithTag(self, 1020)).contentSize = CGSizeMake(theSize().width, buttonCount*CELL_HEIGHT);
[((UIScrollView*)viewWithTag(self, 1020)) flashScrollIndicators];
if(viewWithTag(self, 1020) != nil){
float max = MAX_DEFAULT_HEIGHT();
SIZE_HEIGHT = (buttonCount*CELL_HEIGHT) < max ? (buttonCount*CELL_HEIGHT) : max;
CGSize winSize = theSize();
((UIScrollView*)viewWithTag(self, 1020)).frame = CGRectMake(0, 0, winSize.width, winSize.height);
((UIScrollView*)viewWithTag(self, 1020)).layer.cornerRadius = winSize.width*0.05;
[self setFrameForSize: winSize];
}
%orig;
}else{
%orig;
}
}
%end
static UIView* viewWithTag(UIView* superview, int tag){
for(UIView *s in superview.subviews){
if(s.tag == tag){
return s;
}
viewWithTag(s, tag);
}
return nil;
}
static void removeAllSubviewsWithTagRecursive(UIView* superview, int tag){
for(UIView *s in superview.subviews){
if(s.tag == tag){
[s removeFromSuperview];
}
removeAllSubviewsWithTagRecursive(s, tag);
}
}
%hook UICalloutBarButton
-(void)setPage:(long long)arg1{
if(ENABLED){
self.hidden = NO;
}else{
%orig;
}
}
-(long long)page{
if(ENABLED){
return 0;
}else{
return %orig;
}
}
-(void)fadeAndSendAction{
%orig;
// [UIView animateWithDuration:0.25f delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^{
// self.layer.backgroundColor = UIColor.whiteColor.CGColor;
// } completion:^(BOOL finished) {
// [UIView animateWithDuration:0.25f delay:0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^{
// self.layer.backgroundColor = UIColor.clearColor.CGColor;
// } completion:nil];
// }];
}
-(void)layoutSubviews{
if(ENABLED){
%orig;
for(UIView* sub in self.subviews){
if([sub isMemberOfClass:%c(UIButtonLabel)]){
float widthInsetPercent = 0.02;
float width = self.frame.size.width;
sub.frame = CGRectMake(width*widthInsetPercent, 0, width*(1.0f-(widthInsetPercent*2)), self.frame.size.height);
((UILabel*)sub).adjustsFontSizeToFitWidth = YES;
sub.backgroundColor = UIColor.clearColor;
((UILabel*)sub).textAlignment = TEXT_ALIGNMENT == 1 ? NSTextAlignmentLeft : TEXT_ALIGNMENT == 2 ? NSTextAlignmentCenter : TEXT_ALIGNMENT == 3 ? NSTextAlignmentRight : NSTextAlignmentCenter; // default center
((UILabel*)sub).font = findAdaptiveFontWithName(((UILabel*)sub).font.fontName/*@"HelveticaNeue-Light"*/, sub.frame.size, 1, getFontMultiplier());
}else if([sub isMemberOfClass:%c(UIImageView)]){
float fontMult = getFontMultiplier();
float width = self.frame.size.width;
float height = self.frame.size.height*fontMult;
sub.frame = CGRectMake(0, height/2, width, height);
((UIImageView*)sub).contentMode = UIViewContentModeScaleAspectFit;
}
}
}else{
%orig;
}
}
%end
//https://stackoverflow.com/questions/8812192/how-to-set-font-size-to-fill-uilabel-height/17622215#17622215
static UIFont* findAdaptiveFontWithName(NSString *fontName, CGSize labelSize, NSInteger minSize, float multiplier)
{
UIFont *tempFont = nil;
NSString *testString = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSInteger tempMin = minSize;
NSInteger tempMax = 256;
NSInteger mid = 0;
NSInteger difference = 0;
while (tempMin <= tempMax) {
mid = tempMin + (tempMax - tempMin) / 2;
tempFont = [UIFont fontWithName:fontName size:mid];
difference = labelSize.height - [testString sizeWithFont:tempFont].height;
if (mid == tempMin || mid == tempMax) {
if (difference < 0) {
return [UIFont fontWithName:fontName size:(mid - 1)];
}
return [UIFont fontWithName:fontName size:mid*multiplier];
}
if (difference < 0) {
tempMax = mid - 1;
} else if (difference > 0) {
tempMin = mid + 1;
} else {
return [UIFont fontWithName:fontName size:mid*multiplier];
}
}
return [UIFont fontWithName:fontName size:mid*multiplier];
}
static void initPrefs() {
// Copy the default preferences file when the actual preference file doesn't exist
NSString *path = @"/var/mobile/Library/Preferences/com.satvikb.selectionplusprefs.plist";
NSString *pathDefault = @"/Library/PreferenceBundles/SelectionPlusPrefs.bundle/defaults.plist";
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
[fileManager copyItemAtPath:pathDefault toPath:path error:nil];
}
defaultPrefs = [[NSMutableDictionary alloc] initWithContentsOfFile:pathDefault];
}
static void loadPrefs()
{
prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.satvikb.selectionplusprefs.plist"];
if(prefs){
ENABLED = ( [prefs objectForKey:@"EnabledSwitch"] ? [[prefs objectForKey:@"EnabledSwitch"] boolValue] : ENABLED );
NUM_OPTIONS = ( [prefs objectForKey:@"NumOptions"] ? [[prefs objectForKey:@"NumOptions"] intValue] : NUM_OPTIONS );
CELL_HEIGHT = ( [prefs objectForKey:@"CellHeight"] ? [[prefs objectForKey:@"CellHeight"] floatValue] : CELL_HEIGHT );
SEP_WIDTH = ( [prefs objectForKey:@"SepWidth"] ? [[prefs objectForKey:@"SepWidth"] floatValue] : SEP_WIDTH );
WINDOW_WIDTH = ( [prefs objectForKey:@"WindowWidth"] ? [[prefs objectForKey:@"WindowWidth"] floatValue] : WINDOW_WIDTH );
FONT_MULTIPLIER = ( [prefs objectForKey:@"FontMultiplier"] ? [[prefs objectForKey:@"FontMultiplier"] floatValue] : FONT_MULTIPLIER );
TEXT_ALIGNMENT = ( [prefs objectForKey:@"TextAlignment"] ? [[prefs objectForKey:@"TextAlignment"] intValue] : TEXT_ALIGNMENT );
BACKGROUND_COLOR = LCPParseColorString([prefs objectForKey:@"BackgroundColor"], @"#0e0e0e:0.8f");
BORDER_COLOR = LCPParseColorString([prefs objectForKey:@"BorderColor"], @"#808080");
SEPARATOR_COLOR = LCPParseColorString([prefs objectForKey:@"SeparatorColor"], @"#ffffff");
// NSLog(@"Load bg color %@", BACKGROUND_COLOR);
}
}
%ctor {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPrefs, CFSTR("com.satvikb.selectionplusprefs/settingschanged"), NULL, CFNotificationSuspensionBehaviorCoalesce);
initPrefs();
loadPrefs();
%init();
}