-
Notifications
You must be signed in to change notification settings - Fork 9
/
Tweak.xm
298 lines (262 loc) · 8.77 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
#include <substrate.h>
#include <UIKit/UIStatusBar.h>
#import <CoreGraphics/CoreGraphics.h>
#include <SpringBoard/SpringBoard.h>
#include <AppSupport/CPDistributedMessagingCenter.h>
#import <rocketbootstrap/rocketbootstrap.h>
CPDistributedMessagingCenter *messagingCenter; //message center
UIWindow *noNotchW; //window which will contain everything
UIView *noNotch; //the black border which will cover the notch
UIView *cover; //a supporting view which will help us hide and show the status bar
UIAccelerometer *accelerometer;
UIInterfaceOrientation oldOrientation;
//our hide and show methods. Add a nice transition
void hide() {
[UIView animateWithDuration:1.0 animations:^(void) {
noNotchW.alpha = 0;
}];
}
void show() {
if (oldOrientation != 1) return;
[UIView animateWithDuration:1.0 animations:^(void) {
noNotchW.alpha = 1;
}];
}
void hideSB() {
[UIView animateWithDuration:1.0 animations:^(void) {
cover.alpha = 0;
}];
}
void showSB() {
[UIView animateWithDuration:1.0 animations:^(void) {
cover.alpha = 1;
}];
}
@interface SpringBoard ()
-(BOOL)isShowingHomescreen;
@end
@interface UIApplication ()
-(int)activeInterfaceOrientation;
@end
@interface UIStatusBarWindow : UIWindow
@end
@interface UIStatusBarWindow () <UIAccelerometerDelegate>
@end
@interface _UIStatusBar : UIView
@property (nonatomic, retain) UIColor *foregroundColor;
@end
BOOL isOnSpringBoard() {
return [((SpringBoard*)[%c(SpringBoard) sharedApplication]) isShowingHomescreen];
}
%group SBHooks
%hook UIStatusBarWindow
- (void)layoutSubviews
{
CGRect wholeFrame = [UIScreen mainScreen].bounds; //whole screen
CGRect sbFrame = wholeFrame;
sbFrame.size.height = 32;
CGRect frame = CGRectMake(-50, -16, wholeFrame.size.width+100, wholeFrame.size.height+200); //this is the border which will cover the notch
if (!noNotchW) {
[messagingCenter registerForMessageName:@"hide" target:self selector:@selector(hide:)];
[messagingCenter registerForMessageName:@"hide2" target:self selector:@selector(hide:)]; //apps need special treatment
[messagingCenter registerForMessageName:@"show" target:self selector:@selector(show:)];
noNotchW = [[UIWindow alloc] initWithFrame:sbFrame]; //window will be as small as the status bar
cover = [[UIView alloc] initWithFrame:sbFrame]; //the support view
}
if (!noNotch) {
noNotch = [[UIView alloc] initWithFrame:frame]; //the notch view
}
noNotch.layer.borderColor = [UIColor blackColor].CGColor; //add a black border
noNotch.layer.borderWidth = 50.0f; //something thinner than the status bar
[noNotch setClipsToBounds:YES]; //we want the border to be round
[noNotch.layer setMasksToBounds:YES]; //^^
noNotch.layer.cornerRadius = 70; //corner radius
noNotchW.windowLevel = 1096;
noNotchW.hidden = NO; //we don't want it hidden for whatever reason
noNotchW.userInteractionEnabled = YES; //touches will pass through the window
noNotch.userInteractionEnabled = NO; //they won't pass through the notch cover because that's big and will block touches
cover.userInteractionEnabled = YES; //touches will pass through the status bar
[noNotchW addSubview:noNotch]; //add the notch cover inside the window
UIStatusBar_Base *statusBar = [self valueForKey:@"_statusBar"];
((UIView*)statusBar).tag = 414141;
[cover addSubview:(UIView*)statusBar]; //add status bar inside our supporting view
[noNotchW addSubview:cover]; //add supporting view inside the window
//start listening for orientation changes
if (!accelerometer) {
accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.updateInterval = 0.5; //listen every half a second
accelerometer.delegate = self;
}
%orig; //make SpringBoard do whatever it was gonna do before we kicked in and stole the notch
}
%new
- (void)accelerometer:(UIAccelerometer *)meter didAccelerate:(UIAcceleration *)acceleration {
//if old orientation isn't equal to current orientation => orientation changed
if (oldOrientation != [[%c(SpringBoard) sharedApplication] activeInterfaceOrientation]) {
oldOrientation = [[%c(SpringBoard) sharedApplication] activeInterfaceOrientation];
if (oldOrientation == 1 && noNotchW.alpha != 1)
show();
else if (oldOrientation != 1 && noNotchW.alpha != 0)
hide();
}
}
%new
- (void)hide:(NSString *)name {
if ([name isEqualToString:@"hide2"] && isOnSpringBoard()) return;
[UIView animateWithDuration:1.0 animations:^(void) {
noNotchW.alpha = 0;
}];
}
%new
- (void)show:(NSString *)name {
[UIView animateWithDuration:1.0 animations:^(void) {
noNotchW.alpha = 1;
}];
}
%end
//status bar window always visible
%hook UIStatusBarWindow
-(void)setHidden:(BOOL)arg1 {
%orig(NO);
}
%end
//status bar always visible
%hook UIStatusBar_Base
-(void)setAlpha:(CGFloat)arg1 {
//if the system wants to show the status bar make sure the notch cover window is also there
if (arg1 == 1) {
if (noNotchW.alpha == 0)
show();
}
if (((UIView*)self).tag == 414141)
%orig(1);
else
%orig(arg1);
}
%end
//align the status bar properly
%hook _UIStatusBar
- (void)setFrame:(CGRect)frame {
frame.origin.y = -2;
frame.size.height = 32;
%orig(frame);
}
- (CGRect)bounds {
CGRect frame = %orig;
frame.origin.y = -2;
frame.size.height = 32;
return frame;
}
//make the status bar always white
-(void)layoutSubviews {
%orig;
self.foregroundColor = [UIColor whiteColor];
}
%end
//when we open an app make sure the notch cover is visible
/*%hook SpringBoard
-(void)frontDisplayDidChange:(id)newDisplay {
%orig;
if ([newDisplay isKindOfClass:%c(SBApplication)]) {
if (cover.alpha == 0)
showSB();
if (noNotchW.alpha == 0)
show();
}
}
%end*/
//when control center is opened hide the status bar
%hook SBControlCenterController
-(void)presentAnimated:(BOOL)arg1 {
if (cover.alpha != 0)
hideSB();
%orig;
}
-(void)presentAnimated:(BOOL)arg1 completion:(id)arg2 {
if (cover.alpha != 0)
hideSB();
%orig;
}
//when control center is dismissed show the status bar
-(void)dismissAnimated:(BOOL)arg1 {
if (cover.alpha == 0)
showSB();
%orig;
}
-(void)dismissAnimated:(BOOL)arg1 completion:(id)arg2 {
if (cover.alpha == 0)
showSB();
%orig;
}
-(void)grabberTongueBeganPulling:(id)arg1 withDistance:(double)arg2 andVelocity:(double)arg3 {
if (cover.alpha != 0)
hideSB();
%orig;
}
-(void)_didDismiss {
if (cover.alpha == 0)
showSB();
%orig;
}
%end
//get rid of the notch cover when user enters wiggle mode. Can't think of an alternative
%hook SBEditingDoneButton
-(id)initWithFrame:(CGRect)arg1 {
if (noNotchW.alpha != 0)
hide();
return %orig;
}
%end
%end
%group AppHooks
//if status bar is hidden => fullscreen app, therefore we need to hide the notch cover
%hook UIStatusBar_Base
-(void)setAlpha:(CGFloat)arg1 {
if (arg1 == 0)
[messagingCenter sendMessageName:@"hide2" userInfo:nil];
else
[messagingCenter sendMessageName:@"show" userInfo:nil];
%orig(arg1);
}
-(void)setHidden:(BOOL)arg1 {
if (arg1 == YES)
[messagingCenter sendMessageName:@"hide2" userInfo:nil];
else
[messagingCenter sendMessageName:@"show" userInfo:nil];
%orig(arg1);
}
-(CGFloat)alpha {
if (%orig == 0)
[messagingCenter sendMessageName:@"hide2" userInfo:nil];
else
[messagingCenter sendMessageName:@"show" userInfo:nil];
return %orig;
}
-(BOOL)isHidden {
if (%orig == YES)
[messagingCenter sendMessageName:@"hide2" userInfo:nil];
else
[messagingCenter sendMessageName:@"show" userInfo:nil];
return %orig;
}
%end
//check again after we reopen the app. This doesn't seem to be working that well
%hook UIApplicationDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
if ([[[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"] alpha] == 0 || [[[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"] isHidden] == YES)
[messagingCenter sendMessageName:@"hide" userInfo:nil];
%orig(application);
}
%end
%end
%ctor {
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.jakeashacks.noNotch"]; //setup our message center
rocketbootstrap_distributedmessagingcenter_apply(messagingCenter); //use rocketbootstrap to get around sandbox limits
if ([[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.apple.springboard"]) {
[messagingCenter runServerOnCurrentThread];
%init(SBHooks);
}
else {
%init(AppHooks);
}
}