-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweak.xm
More file actions
160 lines (128 loc) · 5.27 KB
/
Tweak.xm
File metadata and controls
160 lines (128 loc) · 5.27 KB
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
#import <UIKit/UIKit.h>
#import <objc/message.h>
#import <objc/runtime.h>
static char kSHFS_GestureKey;
static char kSHFS_HandlerKey;
static char kSHFS_EdgePanFixedKey;
static inline UIViewController *NearestViewController(UIView *v) {
UIResponder *r = v;
while (r) {
if ([r isKindOfClass:[UIViewController class]]) return (UIViewController *)r;
r = [r nextResponder];
}
return nil;
}
static inline UIView *FindSubviewByClassName(UIView *root, NSString *className) {
if (!root) return nil;
if ([NSStringFromClass([root class]) isEqualToString:className]) return root;
for (UIView *v in root.subviews) {
UIView *hit = FindSubviewByClassName(v, className);
if (hit) return hit;
}
return nil;
}
static inline void ForceRateOnPVC(id pvc, float rate) {
if (!pvc) return;
SEL vsSel = @selector(varispeedController);
SEL setSel = @selector(varispeedSwitchController:didSelectRate:);
if (![pvc respondsToSelector:vsSel] || ![pvc respondsToSelector:setSel]) return;
id vs = ((id(*)(id, SEL))objc_msgSend)(pvc, vsSel);
if (!vs) return;
((void(*)(id, SEL, id, float))objc_msgSend)(pvc, setSel, vs, rate);
}
@interface SHFSLongPressHandler : NSObject
@property (nonatomic, weak) UIView *hostView;
@property (nonatomic) BOOL didHaptic;
@end
@implementation SHFSLongPressHandler
- (id)playerViewControllerFromHost {
UIView *playerView = FindSubviewByClassName(self.hostView, @"YTPlayerView");
UIViewController *vc = playerView ? NearestViewController(playerView) : NearestViewController(self.hostView);
if (vc && [NSStringFromClass([vc class]) isEqualToString:@"YTPlayerViewController"]) return (id)vc;
return nil;
}
- (void)onLongPress:(UILongPressGestureRecognizer *)gr {
id pvc = [self playerViewControllerFromHost];
if (gr.state == UIGestureRecognizerStateBegan) {
ForceRateOnPVC(pvc, 2.0f);
if (!self.didHaptic) {
UIImpactFeedbackGenerator *fg =
[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
[fg impactOccurred];
self.didHaptic = YES;
}
return;
}
if (gr.state == UIGestureRecognizerStateEnded ||
gr.state == UIGestureRecognizerStateCancelled ||
gr.state == UIGestureRecognizerStateFailed) {
ForceRateOnPVC(pvc, 1.0f);
self.didHaptic = NO;
return;
}
}
@end
static inline void SHFSDisableEdgePanInShortsIfNeeded(UIView *v, UILongPressGestureRecognizer *lp) {
// Esegui una sola volta per window (evita ripetizioni)
UIWindow *w = v.window;
if (!w) return;
if (objc_getAssociatedObject(w, &kSHFS_EdgePanFixedKey)) return;
objc_setAssociatedObject(w, &kSHFS_EdgePanFixedKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// Cerca UIScreenEdgePanGestureRecognizer salendo nella superview chain e nella window
for (UIView *cur = v; cur != nil; cur = cur.superview) {
for (UIGestureRecognizer *gr in cur.gestureRecognizers) {
if ([gr isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
// Quello che ti rompe i 16px: delaysTouchesBegan=YES. Noi lo disabilitiamo sempre in Shorts.
gr.enabled = NO;
gr.delaysTouchesBegan = NO;
[(UIScreenEdgePanGestureRecognizer *)gr requireGestureRecognizerToFail:lp];
}
}
}
for (UIGestureRecognizer *gr in w.gestureRecognizers) {
if ([gr isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
gr.enabled = NO;
gr.delaysTouchesBegan = NO;
[(UIScreenEdgePanGestureRecognizer *)gr requireGestureRecognizerToFail:lp];
}
}
}
static inline void SHFSInstallGestureIfNeeded(UIView *v) {
if (objc_getAssociatedObject(v, &kSHFS_GestureKey)) return;
SHFSLongPressHandler *h = [SHFSLongPressHandler new];
h.hostView = v;
UILongPressGestureRecognizer *lp =
[[UILongPressGestureRecognizer alloc] initWithTarget:h action:@selector(onLongPress:)];
lp.minimumPressDuration = 0.15; // try 0.12 for faster
lp.allowableMovement = 60;
lp.cancelsTouchesInView = YES;
[v addGestureRecognizer:lp];
// Make taps wait for our long-press
for (UIGestureRecognizer *gr in v.gestureRecognizers) {
if ([gr isKindOfClass:[UITapGestureRecognizer class]]) {
[(UITapGestureRecognizer *)gr requireGestureRecognizerToFail:lp];
}
}
// Make pans wait for our long-press
for (UIGestureRecognizer *gr in v.gestureRecognizers) {
if ([gr isKindOfClass:[UIPanGestureRecognizer class]]) {
[(UIPanGestureRecognizer *)gr requireGestureRecognizerToFail:lp];
}
}
// KEY FIX: disable the UIScreenEdgePanGestureRecognizer that delays touches on the left strip
SHFSDisableEdgePanInShortsIfNeeded(v, lp);
objc_setAssociatedObject(v, &kSHFS_GestureKey, lp, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(v, &kSHFS_HandlerKey, h, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
%hook YTShortsContentView
- (void)didMoveToWindow {
%orig;
if (((UIView *)self).window) {
SHFSInstallGestureIfNeeded((UIView *)self);
}
}
// Always block the stock menu long-press path
- (void)didLongPressForMenu:(UILongPressGestureRecognizer *)gr {
return;
}
%end