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 <UIKit/UIKit.h>
@interface PGCameraVolumeButtonHandler : NSObject
@property (nonatomic, assign) bool enabled;
@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)disableFor:(NSTimeInterval)timeInterval;

View File

@ -5,6 +5,8 @@
#import "TGStringUtils.h"
#import "Freedom.h"
#import <AVKit/AVKit.h>
static NSString *encodeText(NSString *string, int key) {
NSMutableString *result = [[NSMutableString alloc] init];
@ -19,8 +21,11 @@ static NSString *encodeText(NSString *string, int key) {
@interface PGCameraVolumeButtonHandler () {
id _dataSource;
id<UIInteraction> _eventInteraction;
}
@property (nonatomic, weak) UIView *eventView;
@property (nonatomic, copy) void(^upButtonPressedBlock)(void);
@property (nonatomic, copy) void(^upButtonReleasedBlock)(void);
@property (nonatomic, copy) void(^downButtonPressedBlock)(void);
@ -30,11 +35,13 @@ static NSString *encodeText(NSString *string, int key) {
@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];
if (self != nil)
{
self.eventView = eventView;
self.upButtonPressedBlock = upButtonPressedBlock;
self.upButtonReleasedBlock = upButtonReleasedBlock;
self.downButtonPressedBlock = downButtonPressedBlock;
@ -45,16 +52,56 @@ static NSString *encodeText(NSString *string, int key) {
self.enabled = true;
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);
Class c = NSClassFromString(className);
_dataSource = [[c alloc] init];
}
}
}
return self;
}
- (void)dealloc
{
[self.eventView removeInteraction:_eventInteraction];
self.enabled = false;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

View File

@ -510,7 +510,7 @@ static CGPoint TGCameraControllerClampPointToScreenSize(__unused id self, __unus
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];
}

View File

@ -441,7 +441,7 @@ typedef enum
[self.view addGestureRecognizer:_pinchGestureRecognizer];
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];
}

View File

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