-
Notifications
You must be signed in to change notification settings - Fork 15
/
Tweak.x
229 lines (208 loc) · 8.35 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
#import "Tweak.h"
static CGFloat videoAspectRatio = 16/9;
static BOOL isZoomedToFill = NO;
static BOOL isEngagementPanelVisible = NO;
static BOOL isEngagementPanelViewControllerRemoved = NO;
static UIView *renderingView; // MLHAMSBDLSampleBufferRenderingView *
static NSLayoutConstraint *widthConstraint, *heightConstraint, *centerXConstraint, *centerYConstraint;
static void DEMC_activateConstraints();
static void DEMC_deactivateConstraints();
static void DEMC_centerRenderingView();
%hook YTPlayerViewController
- (void)viewDidAppear:(BOOL)animated {
YTPlayerView *playerView = [self playerView];
UIView *renderingViewContainer = [playerView valueForKey:@"_renderingViewContainer"];
renderingView = [playerView renderingView];
if (IS_ENABLED(kDisableAmbientMode)) {
playerView.backgroundColor = [UIColor blackColor];;
renderingViewContainer.backgroundColor = [UIColor blackColor];
renderingView.backgroundColor = [UIColor blackColor];
} else {
playerView.backgroundColor = [UIColor blackColor];
renderingViewContainer.backgroundColor = nil;
renderingView.backgroundColor = nil;
}
if (IS_ENABLED(kTweak)) {
widthConstraint = [renderingView.widthAnchor constraintEqualToAnchor:renderingViewContainer.safeAreaLayoutGuide.widthAnchor constant:constant];
heightConstraint = [renderingView.heightAnchor constraintEqualToAnchor:renderingViewContainer.safeAreaLayoutGuide.heightAnchor constant:constant];
centerXConstraint = [renderingView.centerXAnchor constraintEqualToAnchor:renderingViewContainer.centerXAnchor];
centerYConstraint = [renderingView.centerYAnchor constraintEqualToAnchor:renderingViewContainer.centerYAnchor];
if (IS_ENABLED(kColorViews)) {
playerView.backgroundColor = [UIColor blueColor];
renderingViewContainer.backgroundColor = [UIColor greenColor];
renderingView.backgroundColor = [UIColor redColor];
}
YTMainAppVideoPlayerOverlayViewController *activeVideoPlayerOverlay = [self activeVideoPlayerOverlay];
// Must check class since YTInlineMutedPlaybackPlayerOverlayViewController doesn't have -(BOOL)isFullscreen
if ([activeVideoPlayerOverlay isKindOfClass:%c(YTMainAppVideoPlayerOverlayViewController)] &&
[activeVideoPlayerOverlay isFullscreen] && !isZoomedToFill && !isEngagementPanelVisible)
DEMC_activateConstraints();
}
%orig;
}
// New video played
- (void)playbackController:(id)playbackController didActivateVideo:(id)video withPlaybackData:(id)playbackData {
%orig;
isEngagementPanelVisible = NO;
isEngagementPanelViewControllerRemoved = NO;
if ([[self activeVideoPlayerOverlay] isFullscreen])
// New video played while in full screen (landscape)
// Activate since new videos played in full screen aren't zoomed-to-fill by default
// (i.e. the notch/Dynamic Island will cut into content when playing a new video in full screen)
DEMC_activateConstraints();
else if (![self isCurrentVideoVertical] && ((YTPlayerView *)[self playerView]).userInteractionEnabled)
DEMC_deactivateConstraints();
}
- (void)setPlayerViewLayout:(int)layout {
%orig;
if (![[self activeVideoPlayerOverlay] isKindOfClass:%c(YTMainAppVideoPlayerOverlayViewController)])
return;
switch (layout) {
case 1: // Mini bar
break;
case 2:
DEMC_deactivateConstraints();
break;
case 3: // Fullscreen
if (!isZoomedToFill && !isEngagementPanelVisible)
DEMC_activateConstraints();
break;
default:
break;
}
}
%end
#pragma mark - Retrieve video aspect ratio
%hook YTPlayerView
- (void)setAspectRatio:(CGFloat)aspectRatio {
%orig;
videoAspectRatio = aspectRatio;
}
%end
#pragma mark - Detect zoom to fill
%hook YTVideoFreeZoomOverlayView // This hook will be used if pinch to zoom is available
- (void)didRecognizePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer {
DEMC_deactivateConstraints();
%orig;
}
- (void)showLabelForSnapState:(NSInteger)snapState {
if (snapState == 0) { // Original
isZoomedToFill = NO;
DEMC_activateConstraints();
} else if (snapState == 1) { // Zoomed to fill
isZoomedToFill = YES;
// No need to deactivate constraints as it's already done in -(void)didRecognizePinch:(UIPinchGestureRecognizer *)
}
%orig;
}
%end
%hook YTVideoZoomOverlayView // This hook will be used if pinch to zoom is unavailable
- (void)didRecognizePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer {
DEMC_deactivateConstraints();
%orig;
}
- (void)showLabelForSnapState:(NSInteger)snapState {
if (snapState == 0) {
isZoomedToFill = NO;
DEMC_activateConstraints();
} else if (snapState == 1) {
isZoomedToFill = YES;
}
%orig;
}
%end
#pragma mark - Mini bar dismiss
%hook YTWatchMiniBarViewController
- (void)dismissMiniBarWithVelocity:(CGFloat)velocity gestureType:(int)gestureType {
%orig;
isZoomedToFill = NO; // YouTube undoes zoom-to-fill when mini bar is dismissed
}
- (void)dismissMiniBarWithVelocity:(CGFloat)velocity gestureType:(int)gestureType skipShouldDismissCheck:(BOOL)skipShouldDismissCheck {
%orig;
isZoomedToFill = NO;
}
%end
#pragma mark - Engagement panels
%hook YTMainAppEngagementPanelViewController
// Engagement panel (comment, description, etc.) about to show up
- (void)viewWillAppear:(BOOL)animated {
if ([self isPeekingSupported]) {
// Shorts (only Shorts support peeking, I think)
} else {
// Everything else
isEngagementPanelVisible = YES;
if ([self isLandscapeEngagementPanel]) {
DEMC_deactivateConstraints();
}
}
%orig;
}
%end
%hook YTEngagementPanelContainerViewController
// Engagement panel about to dismiss
- (void)notifyEngagementPanelContainerControllerWillHideFinalPanel {
// Crashes if plays new video while in full screen causing engagement panel dismissal
// Must check if engagement panel was dismissed because new video played
// (i.e. if -(void)removeEngagementPanelViewControllerWithIdentifier:(id) was called prior)
if (![self isPeekingSupported] && !isEngagementPanelViewControllerRemoved) {
isEngagementPanelVisible = NO;
if ([self isLandscapeEngagementPanel] && !isZoomedToFill) {
DEMC_activateConstraints();
}
}
%orig;
}
- (void)removeEngagementPanelViewControllerWithIdentifier:(id)identifier {
// Usually called when engagement panel is open & new video is played or mini bar is dismissed
isEngagementPanelViewControllerRemoved = YES;
%orig;
}
%end
#pragma mark - Constructor
%ctor {
constant = [[NSUserDefaults standardUserDefaults] floatForKey:kSafeAreaConstant];
if (constant == 0) { // First launch probably
constant = DEFAULT_CONSTANT;
[[NSUserDefaults standardUserDefaults] setFloat:constant forKey:kSafeAreaConstant];
}
%init;
}
#pragma mark - Functions
static void DEMC_activateConstraints() {
if (!IS_ENABLED(kTweak)) return;
if (videoAspectRatio < THRESHOLD && !IS_ENABLED(kEnableForAllVideos)) {
DEMC_deactivateConstraints();
return;
}
// NSLog(@"activate");
DEMC_centerRenderingView();
renderingView.translatesAutoresizingMaskIntoConstraints = NO;
widthConstraint.active = YES;
heightConstraint.active = YES;
}
static void DEMC_deactivateConstraints() {
if (!IS_ENABLED(kTweak)) return;
// NSLog(@"deactivate");
renderingView.translatesAutoresizingMaskIntoConstraints = YES;
}
static void DEMC_centerRenderingView() {
centerXConstraint.active = YES;
centerYConstraint.active = YES;
}
void DEMC_showSnackBar(NSString *text) {
YTHUDMessage *message = [%c(YTHUDMessage) messageWithText:text];
GOOHUDManagerInternal *manager = [%c(GOOHUDManagerInternal) sharedInstance];
[manager showMessageMainThread:message];
}
NSBundle *DEMC_getTweakBundle() {
static NSBundle *bundle = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"DontEatMyContent" ofType:@"bundle"];
if (bundlePath)
bundle = [NSBundle bundleWithPath:bundlePath];
else // Rootless
bundle = [NSBundle bundleWithPath:ROOT_PATH_NS(@"/Library/Application Support/DontEatMyContent.bundle")];
});
return bundle;
}