Fix side button capture in chat camera

This commit is contained in:
Ilya Laktyushin 2024-06-04 05:05:32 +04:00
parent d94a9e9bb7
commit f21d2ccd84
5 changed files with 68 additions and 16 deletions

View File

@ -1,11 +1,12 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface PGCameraVolumeButtonHandler : NSObject @interface PGCameraVolumeButtonHandler : NSObject
@property (nonatomic, assign) bool enabled; @property (nonatomic, assign) bool enabled;
@property (nonatomic, assign) bool ignoring; @property (nonatomic, assign) bool ignoring;
- (instancetype)initWithUpButtonPressedBlock:(void (^)(void))upButtonPressedBlock upButtonReleasedBlock:(void (^)(void))upButtonReleasedBlock downButtonPressedBlock:(void (^)(void))downButtonPressedBlock downButtonReleasedBlock:(void (^)(void))downButtonReleasedBlock; - (instancetype)initWithIsCameraSpecific:(bool)isCameraSpecific eventView:(UIView *)eventView upButtonPressedBlock:(void (^)(void))upButtonPressedBlock upButtonReleasedBlock:(void (^)(void))upButtonReleasedBlock downButtonPressedBlock:(void (^)(void))downButtonPressedBlock downButtonReleasedBlock:(void (^)(void))downButtonReleasedBlock;
- (void)enableIn:(NSTimeInterval)timeInterval; - (void)enableIn:(NSTimeInterval)timeInterval;
- (void)disableFor:(NSTimeInterval)timeInterval; - (void)disableFor:(NSTimeInterval)timeInterval;

View File

@ -5,6 +5,8 @@
#import "TGStringUtils.h" #import "TGStringUtils.h"
#import "Freedom.h" #import "Freedom.h"
#import <AVKit/AVKit.h>
static NSString *encodeText(NSString *string, int key) { static NSString *encodeText(NSString *string, int key) {
NSMutableString *result = [[NSMutableString alloc] init]; NSMutableString *result = [[NSMutableString alloc] init];
@ -19,8 +21,11 @@ static NSString *encodeText(NSString *string, int key) {
@interface PGCameraVolumeButtonHandler () { @interface PGCameraVolumeButtonHandler () {
id _dataSource; id _dataSource;
id<UIInteraction> _eventInteraction;
} }
@property (nonatomic, weak) UIView *eventView;
@property (nonatomic, copy) void(^upButtonPressedBlock)(void); @property (nonatomic, copy) void(^upButtonPressedBlock)(void);
@property (nonatomic, copy) void(^upButtonReleasedBlock)(void); @property (nonatomic, copy) void(^upButtonReleasedBlock)(void);
@property (nonatomic, copy) void(^downButtonPressedBlock)(void); @property (nonatomic, copy) void(^downButtonPressedBlock)(void);
@ -30,11 +35,13 @@ static NSString *encodeText(NSString *string, int key) {
@implementation PGCameraVolumeButtonHandler @implementation PGCameraVolumeButtonHandler
- (instancetype)initWithUpButtonPressedBlock:(void (^)(void))upButtonPressedBlock upButtonReleasedBlock:(void (^)(void))upButtonReleasedBlock downButtonPressedBlock:(void (^)(void))downButtonPressedBlock downButtonReleasedBlock:(void (^)(void))downButtonReleasedBlock - (instancetype)initWithIsCameraSpecific:(bool)isCameraSpecific eventView:(UIView *)eventView upButtonPressedBlock:(void (^)(void))upButtonPressedBlock upButtonReleasedBlock:(void (^)(void))upButtonReleasedBlock downButtonPressedBlock:(void (^)(void))downButtonPressedBlock downButtonReleasedBlock:(void (^)(void))downButtonReleasedBlock
{ {
self = [super init]; self = [super init];
if (self != nil) if (self != nil)
{ {
self.eventView = eventView;
self.upButtonPressedBlock = upButtonPressedBlock; self.upButtonPressedBlock = upButtonPressedBlock;
self.upButtonReleasedBlock = upButtonReleasedBlock; self.upButtonReleasedBlock = upButtonReleasedBlock;
self.downButtonPressedBlock = downButtonPressedBlock; self.downButtonPressedBlock = downButtonPressedBlock;
@ -45,16 +52,56 @@ static NSString *encodeText(NSString *string, int key) {
self.enabled = true; self.enabled = true;
if (@available(iOS 17.2, *)) { if (@available(iOS 17.2, *)) {
if (isCameraSpecific) {
__weak PGCameraVolumeButtonHandler *weakSelf = self;
AVCaptureEventInteraction *interaction = [[AVCaptureEventInteraction alloc] initWithPrimaryEventHandler:^(AVCaptureEvent * _Nonnull event) {
__strong PGCameraVolumeButtonHandler *strongSelf = weakSelf;
switch (event.phase) {
case AVCaptureEventPhaseBegan:
strongSelf.downButtonPressedBlock();
break;
case AVCaptureEventPhaseEnded:
strongSelf.downButtonReleasedBlock();
break;
case AVCaptureEventPhaseCancelled:
strongSelf.downButtonReleasedBlock();
break;
default:
break;
}
} secondaryEventHandler:^(AVCaptureEvent * _Nonnull event) {
__strong PGCameraVolumeButtonHandler *strongSelf = weakSelf;
switch (event.phase) {
case AVCaptureEventPhaseBegan:
strongSelf.upButtonPressedBlock();
break;
case AVCaptureEventPhaseEnded:
strongSelf.upButtonReleasedBlock();
break;
case AVCaptureEventPhaseCancelled:
strongSelf.upButtonReleasedBlock();
break;
default:
break;
}
}];
interaction.enabled = true;
[eventView addInteraction:interaction];
_eventInteraction = interaction;
} else {
NSString *className = encodeText(@"NQWpmvnfDpouspmmfsTztufnEbubTpvsdf", -1); NSString *className = encodeText(@"NQWpmvnfDpouspmmfsTztufnEbubTpvsdf", -1);
Class c = NSClassFromString(className); Class c = NSClassFromString(className);
_dataSource = [[c alloc] init]; _dataSource = [[c alloc] init];
} }
} }
}
return self; return self;
} }
- (void)dealloc - (void)dealloc
{ {
[self.eventView removeInteraction:_eventInteraction];
self.enabled = false; self.enabled = false;
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
} }

View File

@ -510,7 +510,7 @@ static CGPoint TGCameraControllerClampPointToScreenSize(__unused id self, __unus
strongSelf->_interfaceView.shutterReleased(true); strongSelf->_interfaceView.shutterReleased(true);
}; };
_buttonHandler = [[PGCameraVolumeButtonHandler alloc] initWithUpButtonPressedBlock:buttonPressed upButtonReleasedBlock:buttonReleased downButtonPressedBlock:buttonPressed downButtonReleasedBlock:buttonReleased]; _buttonHandler = [[PGCameraVolumeButtonHandler alloc] initWithIsCameraSpecific:true eventView:self.view upButtonPressedBlock:buttonPressed upButtonReleasedBlock:buttonReleased downButtonPressedBlock:buttonPressed downButtonReleasedBlock:buttonReleased];
[self _configureCamera]; [self _configureCamera];
} }

View File

@ -441,7 +441,7 @@ typedef enum
[self.view addGestureRecognizer:_pinchGestureRecognizer]; [self.view addGestureRecognizer:_pinchGestureRecognizer];
void (^voidBlock)(void) = ^{}; void (^voidBlock)(void) = ^{};
_buttonHandler = [[PGCameraVolumeButtonHandler alloc] initWithUpButtonPressedBlock:voidBlock upButtonReleasedBlock:voidBlock downButtonPressedBlock:voidBlock downButtonReleasedBlock:voidBlock]; _buttonHandler = [[PGCameraVolumeButtonHandler alloc] initWithIsCameraSpecific:true eventView:self.view upButtonPressedBlock:voidBlock upButtonReleasedBlock:voidBlock downButtonPressedBlock:voidBlock downButtonReleasedBlock:voidBlock];
[self configureCamera]; [self configureCamera];
} }

View File

@ -16,7 +16,10 @@ private final class LegacyHandlerImpl: VolumeButtonHandlerImpl {
context: SharedAccountContext, context: SharedAccountContext,
performAction: @escaping (VolumeButtonsListener.Action) -> Void performAction: @escaping (VolumeButtonsListener.Action) -> Void
) { ) {
self.handler = PGCameraVolumeButtonHandler(upButtonPressedBlock: { self.handler = PGCameraVolumeButtonHandler(
isCameraSpecific: false,
eventView: context.mainWindow?.viewController?.view,
upButtonPressedBlock: {
performAction(.up) performAction(.up)
}, upButtonReleasedBlock: { }, upButtonReleasedBlock: {
performAction(.upRelease) performAction(.upRelease)
@ -24,7 +27,8 @@ private final class LegacyHandlerImpl: VolumeButtonHandlerImpl {
performAction(.down) performAction(.down)
}, downButtonReleasedBlock: { }, downButtonReleasedBlock: {
performAction(.downRelease) performAction(.downRelease)
}) }
)
self.handler.enabled = true self.handler.enabled = true
} }