-
Notifications
You must be signed in to change notification settings - Fork 12
/
Tweak.x
193 lines (153 loc) · 6.56 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
#import "../YTVideoOverlay/Header.h"
#import "../YTVideoOverlay/Init.x"
#import <YouTubeHeader/YTColor.h>
#import <YouTubeHeader/YTMainAppVideoPlayerOverlayViewController.h>
#import <YouTubeHeader/YTSingleVideoController.h>
#import <YouTubeHeader/YTTypeStyle.h>
#import <YouTubeHeader/MLFormat.h>
#define TweakKey @"YouQuality"
@interface YTMainAppControlsOverlayView (YouQuality)
@property (retain, nonatomic) YTQTMButton *qualityButton;
- (void)didPressYouQuality:(id)arg;
- (void)updateYouQualityButton:(id)arg;
@end
@interface YTInlinePlayerBarContainerView (YouQuality)
@property (retain, nonatomic) YTQTMButton *qualityButton;
- (void)didPressYouQuality:(id)arg;
- (void)updateYouQualityButton:(id)arg;
@end
NSString *YouQualityUpdateNotification = @"YouQualityUpdateNotification";
NSString *currentQualityLabel = @"N/A";
NSBundle *YouQualityBundle() {
static NSBundle *bundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *tweakBundlePath = [[NSBundle mainBundle] pathForResource:TweakKey ofType:@"bundle"];
if (tweakBundlePath)
bundle = [NSBundle bundleWithPath:tweakBundlePath];
else
bundle = [NSBundle bundleWithPath:[NSString stringWithFormat:ROOT_PATH_NS(@"/Library/Application Support/%@.bundle"), TweakKey]];
});
return bundle;
}
static UIImage *qualityImage(NSString *qualityLabel) {
return [%c(QTMIcon) tintImage:[UIImage imageNamed:qualityLabel inBundle:YouQualityBundle() compatibleWithTraitCollection:nil] color:[%c(YTColor) white1]];
}
static void configureButtonStyle(YTQTMButton *button) {
[button setTitleColor:[%c(YTColor) white1] forState:0];
YTDefaultTypeStyle *defaultTypeStyle = [%c(YTTypeStyle) defaultTypeStyle];
UIFont *font = [defaultTypeStyle respondsToSelector:@selector(ytSansFontOfSize:weight:)]
? [defaultTypeStyle ytSansFontOfSize:10 weight:UIFontWeightSemibold]
: [defaultTypeStyle fontOfSize:10 weight:UIFontWeightSemibold];
button.titleLabel.font = font;
button.titleLabel.numberOfLines = 3;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
}
%group Video
NSString *getCompactQualityLabel(MLFormat *format) {
NSString *qualityLabel = [format qualityLabel];
BOOL shouldShowFPS = [format FPS] > 30;
if ([qualityLabel hasPrefix:@"2160p"])
qualityLabel = [qualityLabel stringByReplacingOccurrencesOfString:@"2160p" withString:shouldShowFPS ? @"4K\n" : @"4K"];
else if ([qualityLabel hasPrefix:@"1440p"])
qualityLabel = [qualityLabel stringByReplacingOccurrencesOfString:@"1440p" withString:shouldShowFPS ? @"2K\n" : @"2K"];
else if ([qualityLabel hasPrefix:@"1080p"])
qualityLabel = [qualityLabel stringByReplacingOccurrencesOfString:@"1080p" withString:shouldShowFPS ? @"HD\n" : @"HD"];
else if (shouldShowFPS)
qualityLabel = [qualityLabel stringByReplacingOccurrencesOfString:@"p" withString:@"p\n"];
if ([qualityLabel hasSuffix:@" HDR"])
qualityLabel = [qualityLabel stringByReplacingOccurrencesOfString:@" HDR" withString:@"\nHDR"];
return qualityLabel;
}
%hook YTVideoQualitySwitchOriginalController
- (void)singleVideo:(id)singleVideo didSelectVideoFormat:(MLFormat *)format {
currentQualityLabel = getCompactQualityLabel(format);
[[NSNotificationCenter defaultCenter] postNotificationName:YouQualityUpdateNotification object:nil];
%orig;
}
%end
%hook YTVideoQualitySwitchRedesignedController
- (void)singleVideo:(id)singleVideo didSelectVideoFormat:(MLFormat *)format {
currentQualityLabel = getCompactQualityLabel(format);
[[NSNotificationCenter defaultCenter] postNotificationName:YouQualityUpdateNotification object:nil];
%orig;
}
%end
%end
%group Top
%hook YTMainAppControlsOverlayView
%property (retain, nonatomic) YTQTMButton *qualityButton;
- (id)initWithDelegate:(id)delegate {
self = %orig;
self.qualityButton = [self createTextButton:TweakKey accessibilityLabel:@"Quality" selector:@selector(didPressYouQuality:)];
configureButtonStyle(self.qualityButton);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateYouQualityButton:) name:YouQualityUpdateNotification object:nil];
return self;
}
- (id)initWithDelegate:(id)delegate autoplaySwitchEnabled:(BOOL)autoplaySwitchEnabled {
self = %orig;
self.qualityButton = [self createTextButton:TweakKey accessibilityLabel:@"Quality" selector:@selector(didPressYouQuality:)];
configureButtonStyle(self.qualityButton);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateYouQualityButton:) name:YouQualityUpdateNotification object:nil];
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
%orig;
}
- (YTQTMButton *)button:(NSString *)tweakId {
return [tweakId isEqualToString:TweakKey] ? self.qualityButton : %orig;
}
- (UIImage *)buttonImage:(NSString *)tweakId {
return [tweakId isEqualToString:TweakKey] ? qualityImage(currentQualityLabel) : %orig;
}
%new(v@:@)
- (void)updateYouQualityButton:(id)arg {
[self.qualityButton setTitle:currentQualityLabel forState:0];
}
%new(v@:@)
- (void)didPressYouQuality:(id)arg {
YTMainAppVideoPlayerOverlayViewController *c = [self valueForKey:@"_eventsDelegate"];
[c didPressVideoQuality:arg];
[self updateYouQualityButton:nil];
}
%end
%end
%group Bottom
%hook YTInlinePlayerBarContainerView
%property (retain, nonatomic) YTQTMButton *qualityButton;
- (id)init {
self = %orig;
self.qualityButton = [self createTextButton:TweakKey accessibilityLabel:@"Quality" selector:@selector(didPressYouQuality:)];
configureButtonStyle(self.qualityButton);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateYouQualityButton:) name:YouQualityUpdateNotification object:nil];
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
%orig;
}
- (YTQTMButton *)button:(NSString *)tweakId {
return [tweakId isEqualToString:TweakKey] ? self.qualityButton : %orig;
}
- (UIImage *)buttonImage:(NSString *)tweakId {
return [tweakId isEqualToString:TweakKey] ? qualityImage(currentQualityLabel) : %orig;
}
%new(v@:@)
- (void)updateYouQualityButton:(id)arg {
[self.qualityButton setTitle:currentQualityLabel forState:0];
}
%new(v@:@)
- (void)didPressYouQuality:(id)arg {
YTMainAppVideoPlayerOverlayViewController *c = [self.delegate valueForKey:@"_delegate"];
[c didPressVideoQuality:arg];
[self updateYouQualityButton:nil];
}
%end
%end
%ctor {
initYTVideoOverlay(TweakKey);
%init(Video);
%init(Top);
%init(Bottom);
}