-
Notifications
You must be signed in to change notification settings - Fork 12
/
Tweak.x
379 lines (325 loc) · 12.7 KB
/
Tweak.x
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
#import <YouTubeHeader/YTColor.h>
#import <YouTubeHeader/YTCommonUtils.h>
#import <YouTubeHeader/YTMainAppVideoPlayerOverlayViewController.h>
#import <YouTubeHeader/YTSingleVideoController.h>
#import <YouTubeHeader/YTSettingsPickerViewController.h>
#import <YouTubeHeader/YTSettingsViewController.h>
#import <YouTubeHeader/YTSettingsSectionItem.h>
#import <YouTubeHeader/YTSettingsSectionItemManager.h>
#import <YouTubeHeader/YTQTMButton.h>
#import <YouTubeHeader/QTMIcon.h>
#import <YouTubeHeader/MLFormat.h>
#import <YouTubeHeader/UIView+YouTube.h>
#import "Header.h"
static const NSInteger YTVideoOverlaySection = 1222;
NSMutableArray <NSString *> *tweaks;
NSMutableArray <NSString *> *topButtons;
NSMutableArray <NSString *> *bottomButtons;
static NSBundle *TweakBundle(NSString *name) {
NSString *tweakBundlePath = [[NSBundle mainBundle] pathForResource:name ofType:@"bundle"];
return tweakBundlePath
? [NSBundle bundleWithPath:tweakBundlePath]
: [NSBundle bundleWithPath:[NSString stringWithFormat:ROOT_PATH_NS(@"/Library/Application Support/%@.bundle"), name]];
}
static NSString *EnabledKey(NSString *name) {
return [NSString stringWithFormat:@"YTVideoOverlay-%@-Enabled", name];
}
static BOOL TweakEnabled(NSString *name) {
return [[NSUserDefaults standardUserDefaults] boolForKey:EnabledKey(name)];
}
static NSString *PositionKey(NSString *name) {
return [NSString stringWithFormat:@"YTVideoOverlay-%@-Position", name];
}
static int ButtonPosition(NSString *name) {
return [[NSUserDefaults standardUserDefaults] integerForKey:PositionKey(name)];
}
static BOOL UseTopButton(NSString *name) {
return TweakEnabled(name) && ButtonPosition(name) == 0;
}
static BOOL UseBottomButton(NSString *name) {
return TweakEnabled(name) && ButtonPosition(name) == 1;
}
static NSMutableArray *topControls(YTMainAppControlsOverlayView *self, NSMutableArray *controls) {
for (NSString *name in topButtons) {
if (UseTopButton(name))
[controls insertObject:[self button:name] atIndex:0];
}
return controls;
}
static YTQTMButton *createButtonTop(BOOL isText, YTMainAppControlsOverlayView *self, NSString *buttonId, NSString *accessibilityLabel, SEL selector) {
if (!self) return nil;
CGFloat padding = [[self class] topButtonAdditionalPadding];
YTQTMButton *button;
if (isText) {
button = [%c(YTQTMButton) textButton];
button.accessibilityLabel = accessibilityLabel;
button.verticalContentPadding = padding;
[button setTitle:@"Auto" forState:0];
[button sizeToFit];
} else {
UIImage *image = [self buttonImage:buttonId];
button = [self buttonWithImage:image accessibilityLabel:accessibilityLabel verticalContentPadding:padding];
}
button.hidden = YES;
button.alpha = 0;
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
if (![topButtons containsObject:buttonId])
[topButtons addObject:buttonId];
@try {
[[self valueForKey:@"_topControlsAccessibilityContainerView"] addSubview:button];
} @catch (id ex) {
[self addSubview:button];
}
return button;
}
static YTQTMButton *createButtonBottom(BOOL isText, YTInlinePlayerBarContainerView *self, NSString *buttonId, NSString *accessibilityLabel, SEL selector) {
if (!self) return nil;
YTQTMButton *button;
if (isText) {
button = [%c(YTQTMButton) textButton];
button.accessibilityLabel = accessibilityLabel;
} else {
UIImage *image = [self buttonImage:buttonId];
button = [%c(YTQTMButton) iconButton];
[button setImage:image forState:0];
[button sizeToFit];
}
button.hidden = YES;
button.exclusiveTouch = YES;
button.alpha = 0;
button.minHitTargetSize = 60;
button.accessibilityLabel = accessibilityLabel;
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
if (![bottomButtons containsObject:buttonId])
[bottomButtons addObject:buttonId];
[self addSubview:button];
return button;
}
%group Top
%hook YTMainAppVideoPlayerOverlayViewController
- (void)updateTopRightButtonAvailability {
%orig;
YTMainAppVideoPlayerOverlayView *v = [self videoPlayerOverlayView];
YTMainAppControlsOverlayView *c = [v valueForKey:@"_controlsOverlayView"];
for (NSString *name in topButtons)
[c button:name].hidden = !UseTopButton(name);
[c setNeedsLayout];
}
%end
%hook YTMainAppControlsOverlayView
%new(@@:@@@:)
- (YTQTMButton *)createButton:(NSString *)buttonId accessibilityLabel:(NSString *)accessibilityLabel selector:(SEL)selector {
return createButtonTop(NO, self, buttonId, accessibilityLabel, selector);
}
%new(@@:@@@:)
- (YTQTMButton *)createTextButton:(NSString *)buttonId accessibilityLabel:(NSString *)accessibilityLabel selector:(SEL)selector {
return createButtonTop(YES, self, buttonId, accessibilityLabel, selector);
}
%new(@@:@)
- (YTQTMButton *)button:(NSString *)tweakId {
return nil;
}
%new(@@:@)
- (UIImage *)buttonImage:(NSString *)tweakId {
return nil;
}
- (NSMutableArray *)topButtonControls {
return topControls(self, %orig);
}
- (NSMutableArray *)topControls {
return topControls(self, %orig);
}
- (void)setTopOverlayVisible:(BOOL)visible isAutonavCanceledState:(BOOL)canceledState {
CGFloat alpha = canceledState || !visible ? 0.0 : 1.0;
for (NSString *name in topButtons)
[self button:name].alpha = UseTopButton(name) ? alpha : 0;
%orig;
}
%end
%end
%group Bottom
%hook YTInlinePlayerBarContainerView
%new(@@:@@@:)
- (YTQTMButton *)createButton:(NSString *)buttonId accessibilityLabel:(NSString *)accessibilityLabel selector:(SEL)selector {
return createButtonBottom(NO, self, buttonId, accessibilityLabel, selector);
}
%new(@@:@@@:)
- (YTQTMButton *)createTextButton:(NSString *)buttonId accessibilityLabel:(NSString *)accessibilityLabel selector:(SEL)selector {
return createButtonBottom(YES, self, buttonId, accessibilityLabel, selector);
}
%new(@@:@)
- (YTQTMButton *)button:(NSString *)tweakId {
return nil;
}
%new(@@:@)
- (UIImage *)buttonImage:(NSString *)tweakId {
return nil;
}
- (NSMutableArray *)rightIcons {
NSMutableArray *icons = %orig;
for (NSString *name in bottomButtons) {
if (UseBottomButton(name)) {
YTQTMButton *button = [self button:name];
[icons insertObject:button atIndex:0];
}
}
return icons;
}
- (void)updateIconVisibility {
%orig;
for (NSString *name in bottomButtons) {
if (UseBottomButton(name))
[self button:name].hidden = NO;
}
}
- (void)updateIconsHiddenAttribute {
%orig;
for (NSString *name in bottomButtons) {
if (UseBottomButton(name))
[self button:name].hidden = NO;
}
}
- (void)hideScrubber {
%orig;
for (NSString *name in bottomButtons) {
if (UseBottomButton(name))
[self button:name].alpha = 0;
}
}
- (void)setPeekableViewVisible:(BOOL)visible {
%orig;
for (NSString *name in bottomButtons) {
if (UseBottomButton(name))
[self button:name].alpha = visible ? 1 : 0;
}
}
- (void)setPeekableViewVisible:(BOOL)visible fullscreenButtonVisibleShouldMatchPeekableView:(BOOL)match {
%orig;
for (NSString *name in bottomButtons) {
if (UseBottomButton(name))
[self button:name].alpha = visible ? 1 : 0;
}
}
- (void)peekWithShowScrubber:(BOOL)scrubber setControlsAbovePlayerBarVisible:(BOOL)visible {
%orig;
for (NSString *name in bottomButtons) {
if (UseBottomButton(name))
[self button:name].alpha = visible ? 1 : 0;
}
}
- (void)layoutSubviews {
%orig;
CGFloat multiFeedWidth = [self respondsToSelector:@selector(multiFeedElementView)] ? [self multiFeedElementView].frame.size.width : 0;
YTQTMButton *enter = [self enterFullscreenButton];
CGFloat shift = 0;
CGRect frame = CGRectZero;
if ([enter yt_isVisible]) {
frame = enter.frame;
shift = multiFeedWidth + (2 * frame.size.width);
} else {
YTQTMButton *exit = [self exitFullscreenButton];
if ([exit yt_isVisible]) {
frame = exit.frame;
shift = multiFeedWidth + (2 * frame.size.width);
}
}
if (CGRectIsEmpty(frame) || frame.origin.x <= 0 || frame.origin.y < -4) return;
frame.origin.x -= shift;
for (NSString *name in bottomButtons) {
if (UseBottomButton(name)) {
[self button:name].frame = frame;
frame.origin.x -= (2 * frame.size.width);
if (frame.origin.x < 0) frame.origin.x = 0;
}
}
}
%end
%end
%group Settings
%hook YTAppSettingsPresentationData
+ (NSArray *)settingsCategoryOrder {
NSArray *order = %orig;
NSMutableArray *mutableOrder = [order mutableCopy];
NSUInteger insertIndex = [order indexOfObject:@(1)];
if (insertIndex != NSNotFound)
[mutableOrder insertObject:@(YTVideoOverlaySection) atIndex:insertIndex + 1];
return mutableOrder;
}
%end
%hook YTSettingsSectionItemManager
%new(v@:@)
+ (void)registerTweak:(NSString *)tweakId {
[tweaks addObject:tweakId];
}
%new(v@:@)
- (void)updateYTVideoOverlaySectionWithEntry:(id)entry {
NSMutableArray *sectionItems = [NSMutableArray array];
NSBundle *tweakBundle = TweakBundle(@"YTVideoOverlay");
Class YTSettingsSectionItemClass = %c(YTSettingsSectionItem);
YTSettingsViewController *settingsViewController = [self valueForKey:@"_settingsViewControllerDelegate"];
for (NSString *name in tweaks) {
NSBundle *bundle = TweakBundle(name);
YTSettingsSectionItem *master = [YTSettingsSectionItemClass switchItemWithTitle:_LOC(bundle, @"ENABLED")
titleDescription:nil
accessibilityIdentifier:nil
switchOn:TweakEnabled(name)
switchBlock:^BOOL (YTSettingsCell *cell, BOOL enabled) {
[[NSUserDefaults standardUserDefaults] setBool:enabled forKey:EnabledKey(name)];
return YES;
}
settingItemId:0];
[sectionItems addObject:master];
YTSettingsSectionItem *position = [YTSettingsSectionItemClass itemWithTitle:_LOC(bundle, @"POSITION")
accessibilityIdentifier:nil
detailTextBlock:^NSString *() {
return ButtonPosition(name) ? LOC(@"BOTTOM") : LOC(@"TOP");
}
selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) {
NSArray <YTSettingsSectionItem *> *rows = @[
[YTSettingsSectionItemClass checkmarkItemWithTitle:LOC(@"TOP") titleDescription:LOC(@"TOP_DESC") selectBlock:^BOOL (YTSettingsCell *top, NSUInteger arg1) {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:PositionKey(name)];
[settingsViewController reloadData];
return YES;
}],
[YTSettingsSectionItemClass checkmarkItemWithTitle:LOC(@"BOTTOM") titleDescription:LOC(@"BOTTOM_DESC") selectBlock:^BOOL (YTSettingsCell *bottom, NSUInteger arg1) {
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:PositionKey(name)];
[settingsViewController reloadData];
return YES;
}]
];
YTSettingsPickerViewController *picker = [[%c(YTSettingsPickerViewController) alloc] initWithNavTitle:_LOC(bundle, @"POSITION") pickerSectionTitle:nil rows:rows selectedItemIndex:ButtonPosition(name) parentResponder:[self parentResponder]];
[settingsViewController pushViewController:picker];
return YES;
}];
[sectionItems addObject:position];
}
NSString *title = LOC(@"VIDEO_OVERLAY");
if ([settingsViewController respondsToSelector:@selector(setSectionItems:forCategory:title:icon:titleDescription:headerHidden:)]) {
YTIIcon *icon = [%c(YTIIcon) new];
icon.iconType = YT_TV;
[settingsViewController setSectionItems:sectionItems forCategory:YTVideoOverlaySection title:title icon:icon titleDescription:nil headerHidden:NO];
} else
[settingsViewController setSectionItems:sectionItems forCategory:YTVideoOverlaySection title:title titleDescription:nil headerHidden:NO];
}
- (void)updateSectionForCategory:(NSUInteger)category withEntry:(id)entry {
if (category == YTVideoOverlaySection) {
[self updateYTVideoOverlaySectionWithEntry:entry];
return;
}
%orig;
}
%end
%end
%ctor {
tweaks = [NSMutableArray array];
topButtons = [NSMutableArray array];
bottomButtons = [NSMutableArray array];
%init(Settings);
%init(Top);
%init(Bottom);
}
%dtor {
[tweaks removeAllObjects];
[topButtons removeAllObjects];
[bottomButtons removeAllObjects];
}