Skip to content

Commit bb33da1

Browse files
committed
优化清除按钮和速度按钮的可见性管理逻辑,确保在不同状态下正确显示或隐藏
1 parent 43477f1 commit bb33da1

File tree

2 files changed

+161
-98
lines changed

2 files changed

+161
-98
lines changed

DYYYFloatClearButton.xm

Lines changed: 110 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ BOOL isInPlayInteractionVC = NO;
2020
BOOL isPureViewVisible = NO;
2121
BOOL clearButtonForceHidden = NO;
2222
BOOL isAppActive = YES;
23+
BOOL dyyyIsPerformingFloatClearOperation = NO;
24+
25+
static NSInteger dyyyClearButtonMutationDepth = 0;
26+
27+
static inline void DYYYBeginClearButtonMutation(void) {
28+
dyyyClearButtonMutationDepth++;
29+
dyyyIsPerformingFloatClearOperation = YES;
30+
}
31+
32+
static inline void DYYYEndClearButtonMutation(void) {
33+
if (dyyyClearButtonMutationDepth > 0) {
34+
dyyyClearButtonMutationDepth--;
35+
}
36+
dyyyIsPerformingFloatClearOperation = dyyyClearButtonMutationDepth > 0;
37+
}
38+
39+
static void DYYYPerformClearButtonMutation(dispatch_block_t block) {
40+
if (!block) {
41+
return;
42+
}
43+
DYYYBeginClearButtonMutation();
44+
@try {
45+
block();
46+
} @finally {
47+
DYYYEndClearButtonMutation();
48+
}
49+
}
2350

2451

2552
HideUIButton *hideButton;
@@ -49,26 +76,60 @@ UIWindow *getKeyWindow(void) {
4976
return keyWindow;
5077
}
5178

52-
void updateClearButtonVisibility() {
53-
if (!hideButton || !isAppActive)
79+
static void DYYYApplyClearButtonHiddenState(HideUIButton *button, BOOL hidden) {
80+
if (!button) {
5481
return;
55-
56-
__weak HideUIButton *weakButton = hideButton;
57-
dispatch_async(dispatch_get_main_queue(), ^{
58-
HideUIButton *strongButton = weakButton;
59-
if (!strongButton) {
82+
}
83+
void (^applyBlock)(HideUIButton *) = ^(HideUIButton *target) {
84+
if (!target) {
6085
return;
6186
}
62-
if (!dyyyInteractionViewVisible) {
63-
strongButton.hidden = YES;
64-
return;
87+
if (target.hidden != hidden) {
88+
target.hidden = hidden;
6589
}
90+
};
6691

67-
BOOL shouldHide = dyyyCommentViewVisible || clearButtonForceHidden || isPureViewVisible;
68-
if (strongButton.hidden != shouldHide) {
69-
strongButton.hidden = shouldHide;
92+
if ([NSThread isMainThread]) {
93+
applyBlock(button);
94+
} else {
95+
__weak HideUIButton *weakButton = button;
96+
dispatch_async(dispatch_get_main_queue(), ^{
97+
applyBlock(weakButton);
98+
});
99+
}
100+
}
101+
102+
static BOOL DYYYShouldHideClearButton(void) {
103+
BOOL clearModeActive = (hideButton && hideButton.isElementsHidden);
104+
if (clearModeActive) {
105+
if (!isAppActive) {
106+
return YES;
70107
}
71-
});
108+
return clearButtonForceHidden;
109+
}
110+
if (!isAppActive) {
111+
return YES;
112+
}
113+
if (!dyyyInteractionViewVisible) {
114+
return YES;
115+
}
116+
if (dyyyCommentViewVisible) {
117+
return YES;
118+
}
119+
if (isPureViewVisible) {
120+
return YES;
121+
}
122+
if (clearButtonForceHidden) {
123+
return YES;
124+
}
125+
return NO;
126+
}
127+
128+
void updateClearButtonVisibility() {
129+
if (!hideButton) {
130+
return;
131+
}
132+
DYYYApplyClearButtonHiddenState(hideButton, DYYYShouldHideClearButton());
72133
}
73134

74135
void showClearButton(void) {
@@ -78,37 +139,30 @@ void showClearButton(void) {
78139

79140
void hideClearButton(void) {
80141
clearButtonForceHidden = YES;
81-
if (hideButton) {
82-
__weak HideUIButton *weakButton = hideButton;
83-
dispatch_async(dispatch_get_main_queue(), ^{
84-
HideUIButton *strongButton = weakButton;
85-
if (!strongButton) {
86-
return;
87-
}
88-
strongButton.hidden = YES;
89-
});
90-
}
142+
updateClearButtonVisibility();
91143
}
92144

93145
static void forceResetAllUIElements(void) {
94-
UIWindow *window = getKeyWindow();
95-
if (!window)
96-
return;
97-
Class StackViewClass = NSClassFromString(@"AWEElementStackView");
98-
for (NSString *className in targetClassNames) {
99-
Class viewClass = NSClassFromString(className);
100-
if (!viewClass)
101-
continue;
102-
NSMutableArray *views = [NSMutableArray array];
103-
findViewsOfClassHelper(window, viewClass, views);
104-
for (UIView *view in views) {
105-
if ([view isKindOfClass:StackViewClass]) {
106-
view.alpha = DYGetGlobalAlpha();
107-
} else {
108-
view.alpha = 1.0; // 恢复透明度
146+
DYYYPerformClearButtonMutation(^{
147+
UIWindow *window = getKeyWindow();
148+
if (!window)
149+
return;
150+
Class StackViewClass = NSClassFromString(@"AWEElementStackView");
151+
for (NSString *className in targetClassNames) {
152+
Class viewClass = NSClassFromString(className);
153+
if (!viewClass)
154+
continue;
155+
NSMutableArray *views = [NSMutableArray array];
156+
findViewsOfClassHelper(window, viewClass, views);
157+
for (UIView *view in views) {
158+
if ([view isKindOfClass:StackViewClass]) {
159+
view.alpha = DYGetGlobalAlpha();
160+
} else {
161+
view.alpha = 1.0; // 恢复透明度
162+
}
109163
}
110164
}
111-
}
165+
});
112166
}
113167
static void reapplyHidingToAllElements(HideUIButton *button) {
114168
if (!button || !button.isElementsHidden)
@@ -415,9 +469,11 @@ void initTargetClassNames(void) {
415469

416470
- (void)restoreAWEPlayInteractionProgressContainerView {
417471
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DYYYRemoveTimeProgress"] || [[NSUserDefaults standardUserDefaults] boolForKey:@"DYYYHideTimeProgress"]) {
418-
for (UIWindow *window in [UIApplication sharedApplication].windows) {
419-
[self recursivelyRestoreAWEPlayInteractionProgressContainerViewInView:window];
420-
}
472+
DYYYPerformClearButtonMutation(^{
473+
for (UIWindow *window in [UIApplication sharedApplication].windows) {
474+
[self recursivelyRestoreAWEPlayInteractionProgressContainerViewInView:window];
475+
}
476+
});
421477
}
422478
}
423479

@@ -450,15 +506,17 @@ void initTargetClassNames(void) {
450506
}
451507
}
452508
- (void)hideUIElements {
453-
[self.hiddenViewsList removeAllObjects];
454-
[self findAndHideViews:targetClassNames];
455-
[self hideAWEPlayInteractionProgressContainerView];
456-
self.isElementsHidden = YES;
457-
// self.hidden should be managed by updateClearButtonVisibility
458-
updateClearButtonVisibility();
459-
if (self.superview) {
460-
[self.superview bringSubviewToFront:self];
461-
}
509+
DYYYPerformClearButtonMutation(^{
510+
[self.hiddenViewsList removeAllObjects];
511+
[self findAndHideViews:targetClassNames];
512+
[self hideAWEPlayInteractionProgressContainerView];
513+
self.isElementsHidden = YES;
514+
// self.hidden should be managed by updateClearButtonVisibility
515+
updateClearButtonVisibility();
516+
if (self.superview) {
517+
[self.superview bringSubviewToFront:self];
518+
}
519+
});
462520
}
463521

464522
- (void)hideAWEPlayInteractionProgressContainerView {

DYYYFloatSpeedButton.m

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "AwemeHeaders.h"
2+
#import "DYYYFloatClearButton.h"
23
#import "DYYYFloatSpeedButton.h"
34
#import "DYYYUtils.h"
45
#import <UIKit/UIKit.h>
@@ -14,6 +15,50 @@
1415
BOOL speedButtonForceHidden = NO;
1516
BOOL dyyyInteractionViewVisible = NO;
1617

18+
static void DYYYApplySpeedButtonHiddenState(UIView *button, BOOL hidden) {
19+
if (!button) {
20+
return;
21+
}
22+
void (^applyBlock)(UIView *) = ^(UIView *target) {
23+
if (!target) {
24+
return;
25+
}
26+
if (target.hidden != hidden) {
27+
target.hidden = hidden;
28+
}
29+
};
30+
31+
if ([NSThread isMainThread]) {
32+
applyBlock(button);
33+
} else {
34+
__weak UIView *weakButton = button;
35+
dispatch_async(dispatch_get_main_queue(), ^{
36+
applyBlock(weakButton);
37+
});
38+
}
39+
}
40+
41+
static BOOL DYYYShouldHideSpeedButton(void) {
42+
BOOL clearModeActive = (hideButton && hideButton.isElementsHidden);
43+
if (clearModeActive) {
44+
BOOL hideSpeedInClearMode = [[NSUserDefaults standardUserDefaults] boolForKey:@"DYYYHideSpeed"];
45+
if (hideSpeedInClearMode) {
46+
return YES;
47+
}
48+
return speedButtonForceHidden;
49+
}
50+
if (!dyyyInteractionViewVisible) {
51+
return YES;
52+
}
53+
if (dyyyCommentViewVisible) {
54+
return YES;
55+
}
56+
if (speedButtonForceHidden) {
57+
return YES;
58+
}
59+
return NO;
60+
}
61+
1762
NSArray *getSpeedOptions() {
1863
NSString *speedConfig = [[NSUserDefaults standardUserDefaults] stringForKey:@"DYYYSpeedSettings"] ?: @"1.0,1.25,1.5,2.0";
1964
return [speedConfig componentsSeparatedByString:@","];
@@ -106,61 +151,21 @@ void updateSpeedButtonUI() {
106151
return viewControllers;
107152
}
108153

109-
void showSpeedButton(void) { speedButtonForceHidden = NO; }
154+
void showSpeedButton(void) {
155+
speedButtonForceHidden = NO;
156+
updateSpeedButtonVisibility();
157+
}
110158

111159
void hideSpeedButton(void) {
112160
speedButtonForceHidden = YES;
113-
if (speedButton) {
114-
if ([NSThread isMainThread]) {
115-
speedButton.hidden = YES;
116-
} else {
117-
__weak FloatingSpeedButton *weakButton = speedButton;
118-
dispatch_async(dispatch_get_main_queue(), ^{
119-
FloatingSpeedButton *strongButton = weakButton;
120-
if (!strongButton) {
121-
return;
122-
}
123-
strongButton.hidden = YES;
124-
});
125-
}
126-
}
161+
updateSpeedButtonVisibility();
127162
}
128163

129164
void updateSpeedButtonVisibility() {
130165
if (!speedButton || !isFloatSpeedButtonEnabled)
131166
return;
132167

133-
// 如果已经在主线程,直接执行;否则异步到主线程
134-
if ([NSThread isMainThread]) {
135-
if (!dyyyInteractionViewVisible) {
136-
speedButton.hidden = YES;
137-
return;
138-
}
139-
140-
// 在交互界面时,根据评论界面状态决定是否显示
141-
BOOL shouldHide = dyyyCommentViewVisible || speedButtonForceHidden;
142-
if (speedButton.hidden != shouldHide) {
143-
speedButton.hidden = shouldHide;
144-
}
145-
} else {
146-
__weak FloatingSpeedButton *weakButton = speedButton;
147-
dispatch_async(dispatch_get_main_queue(), ^{
148-
FloatingSpeedButton *strongButton = weakButton;
149-
if (!strongButton) {
150-
return;
151-
}
152-
if (!dyyyInteractionViewVisible) {
153-
strongButton.hidden = YES;
154-
return;
155-
}
156-
157-
// 在交互界面时,根据评论界面状态决定是否显示
158-
BOOL shouldHide = dyyyCommentViewVisible || speedButtonForceHidden;
159-
if (strongButton.hidden != shouldHide) {
160-
strongButton.hidden = shouldHide;
161-
}
162-
});
163-
}
168+
DYYYApplySpeedButtonHiddenState(speedButton, DYYYShouldHideSpeedButton());
164169
}
165170

166171
@implementation FloatingSpeedButton

0 commit comments

Comments
 (0)