-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTweak.xm
executable file
·448 lines (355 loc) · 14.2 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
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
#include <libcolorpicker.h>
#include "UIHeaders.h"
//#define LOGGING
#ifdef LOGGING
#define DBG(fmt, ...) NSLog(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif
#define BG_BLUR_STYLE_LIGHT 0
#define BG_BLUR_STYLE_DARK 1
#define BG_BLUR_STYLE_ADAPTIVE 2
#define BG_BLUR_STYLE_GLASS 3
#define BUTTON_BLUR_STYLE_LIGHT 0
#define BUTTON_BLUR_STYLE_DARK 1
#define BUTTON_BLUR_STYLE_ADAPTIVE 2
#define BUTTON_BLUR_STYLE_NONE 3
static BOOL tweakEnabled = YES;
static BOOL displaySheetsAsAlerts = NO;
static BOOL dismissAnywhere = NO;
static BOOL removeCancelAction = NO;
static long backgroundBlurStyle = BG_BLUR_STYLE_GLASS;
static int backgroundBlurIntensity = 10;
static float backgroundBlurColorIntensity = 0.2;
static long buttonBlurStyle = BUTTON_BLUR_STYLE_NONE;
static float buttonBackgroundColorAlpha = 0.5;
static UIColor *buttonBackgroundColor;
static int buttonBlurIntensity = 10;
static float buttonBlurColorIntensity = 0.3;
static float buttonDestructiveColorAlpha = 1;
static UIColor *buttonDestructiveColor;
%hook _UIAlertControllerActionView
%property (nonatomic, retain) UIVisualEffectView *baActionBackgroundBlurView;
// Workaround for sheets
-(void)tintColorDidChange {
%orig;
UIAlertController *controller = MSHookIvar<UIAlertController*>(self, "_alertController");
if(controller.isBAEnabled) {
[self applyButtonStyle:NO];
}
}
-(void)setHighlighted:(BOOL)arg1 {
%orig;
UIAlertController *controller = MSHookIvar<UIAlertController*>(self, "_alertController");
if(controller.isBAEnabled) {
[self applyButtonStyle:arg1];
}
}
%new
- (void)applyButtonStyle:(BOOL)isHighlighted {
self.layer.cornerRadius = 5;
self.layer.masksToBounds = true;
UIAlertAction *action = MSHookIvar<UIAlertAction*>(self, "_action");
UILabel *label = MSHookIvar<UILabel*>(self, "_label");
UIFontDescriptor *fontBold = [label.font.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
if(self.baActionBackgroundBlurView) {
[self.baActionBackgroundBlurView setHidden:isHighlighted];
}
if(isHighlighted) {
label.tintColor = [UIColor blackColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithDescriptor:fontBold size:0];
self.backgroundColor = [UIColor whiteColor];
} else {
label.tintColor = [UIColor whiteColor];
label.textColor = [UIColor whiteColor];
if(action.style == UIAlertActionStyleDestructive) {
self.backgroundColor = buttonDestructiveColor;
label.font = [UIFont fontWithDescriptor:fontBold size:0];
} else {
if(buttonBlurStyle == BUTTON_BLUR_STYLE_NONE) {
self.backgroundColor = buttonBackgroundColor;
} else {
switch(buttonBlurStyle) {
case BUTTON_BLUR_STYLE_LIGHT:
[self setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:buttonBlurColorIntensity]];
break;
case BUTTON_BLUR_STYLE_DARK:
[self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:buttonBlurColorIntensity]];
break;
case BUTTON_BLUR_STYLE_ADAPTIVE: {
if([[UITraitCollection currentTraitCollection] userInterfaceStyle] != UIUserInterfaceStyleDark) { // Inverted
[self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:buttonBlurColorIntensity]];
} else {
[self setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:buttonBlurColorIntensity]];
}
break;
}
default:
break;
}
if(!self.baActionBackgroundBlurView) {
UIBlurEffect *blurEffect = [UIBlurEffect effectWithBlurRadius:buttonBlurIntensity];
UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
visualEffectView.frame = self.bounds;
self.autoresizesSubviews = YES;
self.clipsToBounds = YES;
[self addSubview:visualEffectView];
[self sendSubviewToBack:visualEffectView];
self.baActionBackgroundBlurView = visualEffectView;
}
}
}
}
}
%end
%hook _UIAlertControllerView
- (void)_configureActionGroupViewToAllowHorizontalLayout:(bool)arg1 {
UIAlertController *controller = MSHookIvar<UIAlertController*>(self, "_alertController");
if(!controller.isBAEnabled) {
return %orig;
}
if(removeCancelAction && arg1) {
%orig(false);
return;
}
%orig;
}
%end
%hook UIAlertController
%property (nonatomic, assign) BOOL isBAEnabled;
%property (nonatomic, assign) BOOL isBAActionSheet;
%property (retain) UIInterfaceActionRepresentationView *baCancelActionView;
+ (id)alertControllerWithTitle:(id)title message:(id)message preferredStyle:(long long)style {
UIAlertController *alertController = nil;
if(displaySheetsAsAlerts) {
alertController = %orig(title, message, UIAlertControllerStyleAlert);
} else {
alertController = %orig;
}
if(style == UIAlertControllerStyleActionSheet) {
alertController.isBAActionSheet = YES;
}
alertController.isBAEnabled = NO;
if(tweakEnabled && alertController.preferredStyle != UIAlertControllerStyleActionSheet) {
alertController.isBAEnabled = YES;
}
return alertController;
}
- (id)init {
UIAlertController *alertController = %orig;
alertController.isBAEnabled = tweakEnabled && alertController.preferredStyle != UIAlertControllerStyleActionSheet;
if(displaySheetsAsAlerts) {
alertController.isBAEnabled = YES;
[alertController setPreferredStyle:UIAlertControllerStyleAlert];
}
return alertController;
}
- (void)setPreferredStyle:(long long)style {
if(!self.isBAEnabled) {
%orig;
return;
}
if(style == UIAlertControllerStyleActionSheet) {
self.isBAActionSheet = YES;
}
if(displaySheetsAsAlerts) {
%orig(UIAlertControllerStyleAlert);
} else {
%orig;
self.isBAEnabled = self.preferredStyle != UIAlertControllerStyleActionSheet;
}
}
-(void)setContentViewController:(id)arg1 {
self.isBAEnabled = NO; // Disable here we can´t style every custom controller
if(self.isBAActionSheet) {
BOOL prevTmp = displaySheetsAsAlerts;
displaySheetsAsAlerts = NO;
[self setPreferredStyle:UIAlertControllerStyleActionSheet];
displaySheetsAsAlerts = prevTmp;
}
%orig;
DBG(@"[BlurryAlerts] Custom Controller: %@", arg1);
}
// _dimmingView -> blurView
- (void)viewDidLayoutSubviews {
%orig;
if(!self.isBAEnabled)
return;
// Remove background
_UIAlertControllerView *view = MSHookIvar<_UIAlertControllerView*>(self, "_view");
view.shouldHaveBackdropView = NO;
_UIAlertControllerInterfaceActionGroupView *mainView = MSHookIvar<_UIAlertControllerInterfaceActionGroupView*>(view, "_mainInterfaceActionsGroupView");
UIView *itemsView = MSHookIvar<UIView*>(mainView, "_topLevelItemsView");
itemsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
DBG(@"[BlurryAlerts] Top Level: %@", itemsView);
UIView *bgView = MSHookIvar<UIView*>(mainView, "_backgroundView");
if(bgView) {
bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
}
DBG(@"[BlurryAlerts] Background: %@", bgView);
// Color Text
[self colorAlertTextRecursive:view];
// Get Buttons
UIInterfaceActionGroup *actionGroup = MSHookIvar<UIInterfaceActionGroup*>(mainView, "_actionGroup");
NSArray *actions = MSHookIvar<NSArray*>(actionGroup, "_actions");
for(id actionInterface in actions) {
DBG(@"[BlurryAlerts] Action: %@", actionInterface);
if([actionInterface isKindOfClass:%c(_UIAlertControllerActionViewInterfaceAction)]) {
_UIAlertControllerActionViewInterfaceAction *action = (_UIAlertControllerActionViewInterfaceAction*)actionInterface;
_UIAlertControllerActionView *actionView = action.alertControllerActionView;
UIAlertAction *actionTmp = MSHookIvar<UIAlertAction*>(actionView, "_action");
if(actionTmp.style == UIAlertActionStyleCancel) {
self.baCancelActionView = (UIInterfaceActionRepresentationView *)actionView.superview;
if(removeCancelAction) {
actionView.superview.hidden = YES;
}
}
[actionView applyButtonStyle:NO];
} else {
DBG(@"[BlurryAlerts] Unknown action!");
}
}
// Remove main seperators
[self removeSeperatorViews:itemsView.subviews];
// Get Stack View
_UIInterfaceActionRepresentationsSequenceView *seqView = MSHookIvar<_UIInterfaceActionRepresentationsSequenceView*>(mainView, "_actionSequenceView");
_UIInterfaceActionSeparatableSequenceView *sepView = MSHookIvar<_UIInterfaceActionSeparatableSequenceView*>(seqView, "_separatedContentSequenceView");
UIStackView *stackView = MSHookIvar<UIStackView*>(sepView, "_stackView");
DBG(@"[BlurryAlerts] StackView: %@ Views: %@", stackView, stackView.arrangedSubviews);
// Remove seperators and add spacing
[self removeSeperatorViews:stackView.arrangedSubviews];
for(UIView *view in stackView.arrangedSubviews) {
[stackView setCustomSpacing:5.0 afterView:view];
}
CGFloat scrollViewWidth = 0.0f;
CGFloat scrollViewHeight = 0.0f;
for (UIView* view in seqView.subviews) {
scrollViewHeight += view.frame.size.height + 5;
if(view.frame.size.width > scrollViewWidth) {
scrollViewWidth = view.frame.size.width;
}
}
[seqView setContentSize:(CGSizeMake(scrollViewWidth, scrollViewHeight))];
for (NSLayoutConstraint *c in seqView.constraints) {
DBG(@"[BlurryAlerts] Constraint: %@", c);
if([[NSString stringWithFormat: @"%@", c] containsString:@"height =="]) {
c.constant = scrollViewHeight;
}
}
}
- (void)viewWillAppear:(BOOL)arg1 {
%orig;
if(!self.isBAEnabled)
return;
// Add dismiss recognizer
if(dismissAnywhere) {
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self._dimmingView addGestureRecognizer:singleFingerTap];
}
// Apply blur
UIView *blurView = self._dimmingView;
blurView.alpha = 1;
UIView *bgView = nil;
if(backgroundBlurStyle != BG_BLUR_STYLE_GLASS) {
bgView = [[UIView alloc] init];
bgView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
bgView.alpha = backgroundBlurColorIntensity;
}
switch(backgroundBlurStyle) {
case BG_BLUR_STYLE_LIGHT:
[bgView setBackgroundColor:[UIColor whiteColor]];
break;
case BG_BLUR_STYLE_DARK:
[bgView setBackgroundColor:[UIColor blackColor]];
break;
case BG_BLUR_STYLE_ADAPTIVE: {
if([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
[bgView setBackgroundColor:[UIColor blackColor]];
} else {
[bgView setBackgroundColor:[UIColor whiteColor]];
}
break;
}
default:
break;
}
UIBlurEffect *blurEffect = [UIBlurEffect effectWithBlurRadius:backgroundBlurIntensity];
UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
visualEffectView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
blurView.autoresizesSubviews = YES;
blurView.clipsToBounds = YES;
if(backgroundBlurStyle != BG_BLUR_STYLE_GLASS) {
[blurView addSubview:bgView];
[blurView insertSubview:visualEffectView aboveSubview:bgView];
} else {
[blurView addSubview:visualEffectView];
}
}
%new
- (void)handleSingleTap:(UITapGestureRecognizer*)recognizer {
DBG(@"[BlurryAlerts] Dismiss");
[self.baCancelActionView invokeInterfaceAction];
}
%new
- (void)colorAlertTextRecursive:(UIView *)view {
NSArray *subviews = [view subviews];
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if([subview isKindOfClass:%c(UILabel)]) {
UILabel *label = (UILabel*)subview;
label.tintColor = [UIColor whiteColor];
label.textColor = [UIColor whiteColor];
}
[self colorAlertTextRecursive:subview];
}
}
%new
- (void)removeSeperatorViews:(NSArray*)subviews {
for(UIView *view in subviews) {
if([view isKindOfClass:%c(_UIInterfaceActionVibrantSeparatorView)]) {
DBG(@"[BlurryAlerts] View: Removed seperator!");
[view setHidden:YES];
}
}
}
%end
static void loadPrefs() {
NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.slyfabi.blurryalerts.plist"];
if([prefs objectForKey:@"isEnabled"] != nil)
tweakEnabled = [[prefs objectForKey:@"isEnabled"] boolValue];
if([prefs objectForKey:@"displaySheetsAsAlerts"] != nil)
displaySheetsAsAlerts = [[prefs objectForKey:@"displaySheetsAsAlerts"] boolValue];
if([prefs objectForKey:@"dismissAnywhere"] != nil)
dismissAnywhere = [[prefs objectForKey:@"dismissAnywhere"] boolValue];
if([prefs objectForKey:@"removeCancelAction"] != nil)
removeCancelAction = [[prefs objectForKey:@"removeCancelAction"] boolValue];
// Background Blur
if([prefs objectForKey:@"backgroundBlurType"] != nil)
backgroundBlurStyle = [[prefs objectForKey:@"backgroundBlurType"] intValue];
if([prefs objectForKey:@"backgroundBlurIntensity"] != nil)
backgroundBlurIntensity = [[prefs objectForKey:@"backgroundBlurIntensity"] intValue];
if([prefs objectForKey:@"backgroundBlurColorIntensity"] != nil)
backgroundBlurColorIntensity = [[prefs objectForKey:@"backgroundBlurColorIntensity"] floatValue];
// Button Blur
if([prefs objectForKey:@"buttonBlurType"] != nil)
buttonBlurStyle = [[prefs objectForKey:@"buttonBlurType"] intValue];
if([prefs objectForKey:@"buttonBlurIntensity"] != nil)
buttonBlurIntensity = [[prefs objectForKey:@"buttonBlurIntensity"] intValue];
if([prefs objectForKey:@"buttonBlurColorIntensity"] != nil)
buttonBlurColorIntensity = [[prefs objectForKey:@"buttonBlurColorIntensity"] floatValue];
if([prefs objectForKey:@"buttonBackgroundColorAlpha"] != nil)
buttonBackgroundColorAlpha = [[prefs objectForKey:@"buttonBackgroundColorAlpha"] floatValue];
buttonBackgroundColor = LCPParseColorString([prefs objectForKey:@"buttonBackgroundColor"], @"#333333");
buttonBackgroundColor = [buttonBackgroundColor colorWithAlphaComponent:buttonBackgroundColorAlpha];
// Button destructive color
if([prefs objectForKey:@"buttonDestructiveColorAlpha"] != nil)
buttonDestructiveColorAlpha = [[prefs objectForKey:@"buttonDestructiveColorAlpha"] floatValue];
buttonDestructiveColor = LCPParseColorString([prefs objectForKey:@"buttonDestructiveColor"], @"#990000");
buttonDestructiveColor = [buttonDestructiveColor colorWithAlphaComponent:buttonDestructiveColorAlpha];
}
%ctor {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPrefs, CFSTR("com.slyfabi.blurryalerts.settingschanged"), NULL, CFNotificationSuspensionBehaviorCoalesce);
loadPrefs();
}