forked from Bensge/FullSafari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
251 lines (211 loc) · 11.1 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
#import <substrate.h>
#import <version.h>
#import "Private.h"
BOOL fakeHorizontalSizeClass = NO;
BOOL fakeUserInterfaceIdiom = NO;
BOOL stopNarrowLayout = NO;
BOOL dontUseNarrowLayout = NO;
%hook UIDevice
- (UIUserInterfaceIdiom)userInterfaceIdiom {
return fakeUserInterfaceIdiom ? UIUserInterfaceIdiomPad : %orig;
}
%end
%hook UITraitCollection
- (UIUserInterfaceSizeClass)horizontalSizeClass {
return fakeHorizontalSizeClass ? UIUserInterfaceSizeClassRegular : %orig;
}
%end
%hook NSUserDefaults
- (BOOL)boolForKey: (NSString *)key {
return ([key isEqualToString:@"ShowTabBar"] || [key isEqualToString:@"IconsInTabsEnabled"]) ? YES : %orig;
}
%end
%hook TabController
- (BOOL)canAddNewTab {
return YES;
}
%group iOS11
- (BOOL)canAddNewTabForPrivateBrowsing:(BOOL)arg1 {
return YES;
}
- (BOOL)canAddNewTabForCurrentBrowsingMode {
return YES;
}
- (void)setUsesTabBar:(BOOL)arg1 {
arg1 = YES;
%orig;
}
%end
%end
%hook BrowserController
- (_Bool)_isScreenSizeBigEnoughForTabBar:(struct CGSize)arg1 {
return YES;
}
- (BOOL)_shouldShowTabBar {
return YES;
}
- (CGFloat)_navigationBarOverlapHeight {
fakeUserInterfaceIdiom = YES;
CGFloat orig = %orig;
fakeUserInterfaceIdiom = NO;
return orig;
}
- (void)updateUsesTabBar {
// explanation: on iOS 11, if you are gonna log the whole stacktrace of the universe, you will notice that at some point,
// apple decides in this function (or right in the previous call) whether to leave the tab bar or not based on whatever reasons they see fit
// (aka is ipad or not or whatever). After they do all their calculations and reach a decision,
// they do not nil the tab or touch it, but they simply do removeFromSuperview...
// which is absolutely lovely because we can bypass that and everything's intact,
// untouched and kept natural
if (!self.tabController.tabBar.superview || kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_11_0) {
fakeHorizontalSizeClass = YES;
%orig;
fakeHorizontalSizeClass = NO;
}
}
// This is iOS 11+ as well and needed but it shouldn't need grouping, I very much doubt it even exists in lower versions
- (BOOL)_isScreenBigEnoughForTabBar {
BOOL orig = %orig;
return (kCFCoreFoundationVersionNumber >= 1535.12) ? YES : orig;
}
%group preiOS10
- (BOOL)usesNarrowLayout {
return stopNarrowLayout ? NO : %orig;
}
- (void)_updateUsesNarrowLayout {
stopNarrowLayout = YES;
fakeUserInterfaceIdiom = YES;
%orig;
stopNarrowLayout = NO;
fakeUserInterfaceIdiom = NO;
}
- (void)updateShowingTabBarAnimated:(BOOL)arg1 {
fakeHorizontalSizeClass = YES;
%orig;
fakeHorizontalSizeClass = NO;
}
%end
%end
%hook BrowserToolbar
%property (nonatomic, retain) UIBarButtonItem *addTabItemManual;
// Force-add the "add tab" button to the toolbar
- (NSMutableArray *)defaultItems {
NSMutableArray *orig = %orig;
if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_11_2) {
GestureRecognizingBarButtonItem *addTabItem = MSHookIvar<GestureRecognizingBarButtonItem *>(self, "_addTabItem");
if (!addTabItem || ![orig containsObject:addTabItem]) {
if (!addTabItem) {
// Recreate the "add tab" button for iOS versions that don't do that by default on iPhone models
addTabItem = [[NSClassFromString(@"GestureRecognizingBarButtonItem") alloc] initWithImage:[[UIImage imageNamed:@"AddTab"] retain] style:0 target:[self valueForKey:@"_browserDelegate"] action:@selector(addTabFromButtonBar)];
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_addTabLongPressRecognized:)];
recognizer.allowableMovement = 3.0;
addTabItem.gestureRecognizer = recognizer;
}
// iOS 11 doubles the + button because of this and the thing below
// Also, SafariPlus ditches your "+" button on iOS 11 - perhaps of this bypass, perhaps not; todo: make it SafariPlus compatible, sometime later on perhaps
if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_11_0) {
[orig addObject:addTabItem];
}
NSMutableDictionary *defaultItemsForToolbarSize = [self valueForKey:@"_defaultItemsForToolbarSize"];
if (defaultItemsForToolbarSize) {
[self setValue:addTabItem forKey:@"_addTabItem"];
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_11_0) {
UIBarButtonItem *space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
[MSHookIvar<NSMutableDictionary *>(self, "_defaultItemsForToolbarSize")[@([self toolbarSize])] addObject:space];
}
[MSHookIvar<NSMutableDictionary *>(self, "_defaultItemsForToolbarSize")[@([self toolbarSize])] addObject:[self valueForKey:@"_addTabItem"]];
}
if (kCFCoreFoundationVersionNumber > kCFCoreFoundationVersionNumber_iOS_11_0) {
// Replace fixed-width spacers by flexible ones again to circumvent layout issues
for (UIBarButtonItem *item in [orig.copy autorelease]) {
if ([item isSystemItem] && [item systemItem] == UIBarButtonSystemItemFixedSpace && [item width] > 0.1) {
NSUInteger indexOfItem = [orig indexOfObject:item];
if (indexOfItem != NSNotFound)
[orig replaceObjectAtIndex:indexOfItem withObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]];
}
}
}
}
}
else {
// //LonestarX - This gon' need some testing for iOS 11 portrait/landscape - iOS 12 portrait/landscape but idk, at first glance it looks ok; PS: no idea how it is on 11.4, would also need testing.
//LE: FOUND THIS LOVELY FLAG FROM OPA334, THE AUTHOR OF SAFARIPLUS. ALL HAIL OPA.
if (self.placement == 1) { //don't reverse this to IsPortrait because it doesn't detect it as such initially
//iOS 11.2.x - 11.3.1 implementation - LonestarX
//NOTE: this part was a bitch to work out, I've spent at least 5 hours toying with the freaking toolbar because it kept rejecting new items. tread safely around this code
//since AAPL decided to screw me over and remove _addTabItem ivar, we're just gonna do a MITM grab&swap
//bar items are refreshed upon this call and it appears there's 2 sets, one for portrait and one for landscape, hence why we don't need to remove anything when switching from one to another
NSMutableDictionary *defaultItemsForToolbarSize = [self valueForKey:@"_defaultItemsForToolbarSize"];
NSMutableArray *items = [[defaultItemsForToolbarSize objectForKey:[[defaultItemsForToolbarSize allKeys] firstObject]] mutableCopy]; //grabbing the current items, don't switch to mshookivar, it causes trouble
if (![items containsObject:self.addTabItemManual]) {
//doing all them tasty init here so we save up memory by not spamming object creation every call
TabController *tbc = [[self valueForKey:@"_browserDelegate"] valueForKey:@"_tabController"];
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:tbc action:@selector(_addTabLongPressRecognized:)];
UIImage *addTabImage;
if (kCFCoreFoundationVersionNumber < 1535.12) { //if iOS < 12
addTabImage = [UIImage imageNamed:@"AddTab"];
}
else {
addTabImage = [UIImage imageNamed:@"AddTab" inBundle:[NSBundle bundleWithPath:@"/System/Library/Frameworks/SafariServices.framework"] compatibleWithTraitCollection:nil];
//LonestarX: dem crooks moved the addTab image (and possibly other assets) from the MobileSafari app to SafariServices framework. possibly in an attempt to shift functionality to a macos/iOS framework. idk, it's Appel, who knows. idk where this is on 11.4, feel free to test
}
self.addTabItemManual = [[UIBarButtonItem alloc] initWithImage:[addTabImage retain] style:0 target:[self valueForKey:@"_browserDelegate"] action:@selector(openNewTab)];
NSMutableArray *gestures = [[self.addTabItemManual valueForKey:@"_gestureRecognizers"] mutableCopy];
[gestures addObject:recognizer];
[self.addTabItemManual setValue:gestures forKey:@"_gestureRecognizers"];
UIBarButtonItem *space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
[items addObject:space];
[items addObject:self.addTabItemManual];
}
[defaultItemsForToolbarSize setObject:items forKey:[[defaultItemsForToolbarSize allKeys] firstObject]];
[self setValue:defaultItemsForToolbarSize forKey:@"_defaultItemsForToolbarSize"]; //putting the shizzle back where we took it from. i repeat, don't use mshookivar, 1131 doesn't like it
orig = items;
if (kCFCoreFoundationVersionNumber > kCFCoreFoundationVersionNumber_iOS_11_0) {
// Replace fixed-width spacers by flexible ones again to circumvent layout issues
for (UIBarButtonItem *item in [orig.copy autorelease]) {
if ([item isSystemItem] && [item systemItem] == UIBarButtonSystemItemFixedSpace && [item width] > 0.1) {
NSUInteger indexOfItem = [orig indexOfObject:item];
if (indexOfItem != NSNotFound)
[orig replaceObjectAtIndex:indexOfItem withObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]];
}
}
}
}
}
return orig;
}
- (void)test {
NSLog(@"BEEP");
}
- (void)setItems:(NSArray *)items animated:(BOOL)arg2 {
if ([self respondsToSelector:@selector(toolbarSize)] && [self toolbarSize] == 0) {
NSMutableArray *newItems = [items mutableCopy];
// Replace fixed spacers with flexible ones
for (UIBarButtonItem *item in [newItems.copy autorelease]) {
if ([item isSystemItem] && [item systemItem] == UIBarButtonSystemItemFixedSpace && [item width] > 0.1) {
NSUInteger indexOfItem = [items indexOfObject:item];
if (indexOfItem != NSNotFound)
[newItems replaceObjectAtIndex:indexOfItem withObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]];
}
}
items = [newItems copy];
[newItems release];
}
%orig(items, arg2);
}
%end
%hook TabController
- (UIView *)tiltedTabView: (UIView *)arg1 borrowContentViewForItem: (id)arg2 withTopBackdropView:(id *)arg3 ofHeight:(CGFloat)height {
height += [objc_getClass("TabBar") defaultHeight];
return %orig;
}
%end
%ctor {
%init();
if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_10_0) {
%init(preiOS10);
}
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_11_0) {
%init(iOS11);
}
}