From f21d2ccd8455427031025af87ed468c240e20836 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Tue, 4 Jun 2024 05:05:32 +0400 Subject: [PATCH] Fix side button capture in chat camera --- .../PGCameraVolumeButtonHandler.h | 3 +- .../Sources/PGCameraVolumeButtonHandler.m | 55 +++++++++++++++++-- .../Sources/TGCameraController.m | 2 +- .../Sources/TGVideoMessageCaptureController.m | 2 +- .../VolumeButtons/Sources/VolumeButtons.swift | 22 +++++--- 5 files changed, 68 insertions(+), 16 deletions(-) diff --git a/submodules/LegacyComponents/PublicHeaders/LegacyComponents/PGCameraVolumeButtonHandler.h b/submodules/LegacyComponents/PublicHeaders/LegacyComponents/PGCameraVolumeButtonHandler.h index a958f1a9f4..73c52072fa 100644 --- a/submodules/LegacyComponents/PublicHeaders/LegacyComponents/PGCameraVolumeButtonHandler.h +++ b/submodules/LegacyComponents/PublicHeaders/LegacyComponents/PGCameraVolumeButtonHandler.h @@ -1,11 +1,12 @@ #import +#import @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; diff --git a/submodules/LegacyComponents/Sources/PGCameraVolumeButtonHandler.m b/submodules/LegacyComponents/Sources/PGCameraVolumeButtonHandler.m index 1b4d63d5b0..0de9673a99 100644 --- a/submodules/LegacyComponents/Sources/PGCameraVolumeButtonHandler.m +++ b/submodules/LegacyComponents/Sources/PGCameraVolumeButtonHandler.m @@ -5,6 +5,8 @@ #import "TGStringUtils.h" #import "Freedom.h" +#import + 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 _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,9 +52,47 @@ static NSString *encodeText(NSString *string, int key) { self.enabled = true; if (@available(iOS 17.2, *)) { - NSString *className = encodeText(@"NQWpmvnfDpouspmmfsTztufnEbubTpvsdf", -1); - Class c = NSClassFromString(className); - _dataSource = [[c alloc] init]; + 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; @@ -55,6 +100,8 @@ static NSString *encodeText(NSString *string, int key) { - (void)dealloc { + [self.eventView removeInteraction:_eventInteraction]; + self.enabled = false; [[NSNotificationCenter defaultCenter] removeObserver:self]; } diff --git a/submodules/LegacyComponents/Sources/TGCameraController.m b/submodules/LegacyComponents/Sources/TGCameraController.m index 985cfbb945..fb5e591389 100644 --- a/submodules/LegacyComponents/Sources/TGCameraController.m +++ b/submodules/LegacyComponents/Sources/TGCameraController.m @@ -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]; } diff --git a/submodules/LegacyComponents/Sources/TGVideoMessageCaptureController.m b/submodules/LegacyComponents/Sources/TGVideoMessageCaptureController.m index f0dd4501ae..d05b594a21 100644 --- a/submodules/LegacyComponents/Sources/TGVideoMessageCaptureController.m +++ b/submodules/LegacyComponents/Sources/TGVideoMessageCaptureController.m @@ -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]; } diff --git a/submodules/Utils/VolumeButtons/Sources/VolumeButtons.swift b/submodules/Utils/VolumeButtons/Sources/VolumeButtons.swift index 703dee1d13..5ac2ff4f61 100644 --- a/submodules/Utils/VolumeButtons/Sources/VolumeButtons.swift +++ b/submodules/Utils/VolumeButtons/Sources/VolumeButtons.swift @@ -16,15 +16,19 @@ private final class LegacyHandlerImpl: VolumeButtonHandlerImpl { context: SharedAccountContext, performAction: @escaping (VolumeButtonsListener.Action) -> Void ) { - self.handler = PGCameraVolumeButtonHandler(upButtonPressedBlock: { - performAction(.up) - }, upButtonReleasedBlock: { - performAction(.upRelease) - }, downButtonPressedBlock: { - performAction(.down) - }, downButtonReleasedBlock: { - performAction(.downRelease) - }) + self.handler = PGCameraVolumeButtonHandler( + isCameraSpecific: false, + eventView: context.mainWindow?.viewController?.view, + upButtonPressedBlock: { + performAction(.up) + }, upButtonReleasedBlock: { + performAction(.upRelease) + }, downButtonPressedBlock: { + performAction(.down) + }, downButtonReleasedBlock: { + performAction(.downRelease) + } + ) self.handler.enabled = true }