Skip to content

Commit

Permalink
refactor(ios): add gesture event delegate for hippyBridge
Browse files Browse the repository at this point in the history
useful for external data reporting of hippy gesture events
  • Loading branch information
wwwcg committed Jun 18, 2024
1 parent fc6d506 commit 22df117
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
23 changes: 21 additions & 2 deletions framework/ios/base/bridge/HippyBridgeDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,29 @@
*/

@class HippyBridge;

#import "HippyInvalidating.h"

@protocol HippyBridgeDelegate <NSObject>

/// An Interceptor protocol for gesture events.
@protocol HippyTouchEventInterceptorProtocol <NSObject>

@optional

/// A centralized handler for event sending,
/// which hippy calls before sending events to the JS side.
///
/// This method is convenient for external data reporting of hippy gesture events
/// - Parameters:
/// - eventName: name of event
/// - point: point in hippyRootView
/// - view: target view
- (void)willSendGestureEvent:(NSString *)eventName withPagePoint:(CGPoint)point toView:(UIView *)view;

@end


/// Delegate of HippyBridge
@protocol HippyBridgeDelegate <NSObject, HippyTouchEventInterceptorProtocol>

@optional

Expand Down
6 changes: 0 additions & 6 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,6 @@ - (void)addClickEventListenerForView:(UIView *)view onRootNode:(std::weak_ptr<Ro
};
[view setOnClick:eventListener];
}
else {
}
}

- (void)addLongClickEventListenerForView:(UIView *)view onRootNode:(std::weak_ptr<RootNode>)rootNode {
Expand Down Expand Up @@ -1295,8 +1293,6 @@ - (void)addLongClickEventListenerForView:(UIView *)view onRootNode:(std::weak_pt
};
[view setOnLongClick:eventListener];
}
else {
}
}

- (void)addPressEventListenerForType:(const std::string &)type
Expand Down Expand Up @@ -1460,8 +1456,6 @@ - (void)addPropertyEvent:(const std::string &)name
}];
}
}
else {
}
}

#pragma mark -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#import "HippyBridge.h"

/// Handles all gestures in Hippy
@interface HippyTouchHandler : UIGestureRecognizer <UIGestureRecognizerDelegate>
@interface HippyTouchHandler : UIGestureRecognizer <UIGestureRecognizerDelegate, HippyTouchEventInterceptorProtocol>

/// Init method
/// - Parameters:
Expand Down
27 changes: 21 additions & 6 deletions renderer/native/ios/renderer/touch_handler/HippyTouchHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
point = [view convertPoint:point toView:_rootView];
if (view.onTouchDown) {
if ([self checkViewBelongToTouchHandler:view]) {
// view.onTouchDown(@{ @"page_x": @(point.x), @"page_y": @(point.y) });
const char *name = hippy::kTouchStartEvent;
[self willSendGestureEvent:@(name) withPagePoint:point toView:view];
view.onTouchDown(point,
[self canCapture:name],
[self canBubble:name],
Expand Down Expand Up @@ -269,8 +269,8 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
point = [view convertPoint:point toView:_rootView];
if (view.onTouchEnd) {
if ([self checkViewBelongToTouchHandler:view]) {
// view.onTouchEnd(@{ @"page_x": @(point.x), @"page_y": @(point.y) });
const char *name = hippy::kTouchEndEvent;
[self willSendGestureEvent:@(name) withPagePoint:point toView:view];
view.onTouchEnd(point,
[self canCapture:name],
[self canBubble:name],
Expand All @@ -288,8 +288,8 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
point = [theView convertPoint:point toView:_rootView];
if (theView.onTouchEnd) {
if ([self checkViewBelongToTouchHandler:theView]) {
// theView.onTouchEnd(@{ @"page_x": @(point.x), @"page_y": @(point.y) });
const char *name = hippy::kTouchEndEvent;
[self willSendGestureEvent:@(name) withPagePoint:point toView:theView];
theView.onTouchEnd(point,
[self canCapture:name],
[self canBubble:name],
Expand All @@ -306,6 +306,7 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (pressOutView == _onPressInView && pressOutView.onPressOut) {
if ([self checkViewBelongToTouchHandler:pressOutView]) {
const char *name = hippy::kPressOut;
[self willSendGestureEvent:@(name) withPagePoint:CGPointZero toView:pressOutView];
pressOutView.onPressOut(CGPointZero,
[self canCapture:name],
[self canBubble:name],
Expand All @@ -321,6 +322,7 @@ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (!_bLongClick && clickView.onClick) {
if ([self checkViewBelongToTouchHandler:clickView]) {
const char *name = hippy::kClickEvent;
[self willSendGestureEvent:@(name) withPagePoint:CGPointZero toView:clickView];
clickView.onClick(CGPointZero,
[self canCapture:name],
[self canBubble:name],
Expand Down Expand Up @@ -377,8 +379,8 @@ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
point = [view convertPoint:point toView:_rootView];
if (view.onTouchCancel) {
if ([self checkViewBelongToTouchHandler:view]) {
// view.onTouchCancel(@{ @"page_x": @(point.x), @"page_y": @(point.y) });
const char *name = hippy::kTouchCancelEvent;
[self willSendGestureEvent:@(name) withPagePoint:point toView:view];
view.onTouchCancel(point,
[self canCapture:name],
[self canBubble:name],
Expand All @@ -394,6 +396,7 @@ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
if (pressOutView == _onPressInView && pressOutView.onPressOut) {
if ([self checkViewBelongToTouchHandler:pressOutView]) {
const char *name = hippy::kPressOut;
[self willSendGestureEvent:@(name) withPagePoint:CGPointZero toView:pressOutView];
pressOutView.onPressOut(CGPointZero,
[self canCapture:name],
[self canBubble:name],
Expand Down Expand Up @@ -470,8 +473,8 @@ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGPoint point = [touch locationInView:view];
point = [view convertPoint:point toView:_rootView];
if ([self checkViewBelongToTouchHandler:view]) {
// view.onTouchMove(@{ @"page_x": @(point.x), @"page_y": @(point.y) });
const char *name = hippy::kTouchMoveEvent;
[self willSendGestureEvent:@(name) withPagePoint:point toView:view];
view.onTouchMove(point,
[self canCapture:name],
[self canBubble:name],
Expand Down Expand Up @@ -517,6 +520,7 @@ - (void)scheduleTimer:(__unused NSTimer *)timer {
if (_onPressInView && _onPressInView.onPressIn) {
if ([self checkViewBelongToTouchHandler:_onPressInView]) {
const char *name = hippy::kPressIn;
[self willSendGestureEvent:@(name) withPagePoint:CGPointZero toView:_onPressInView];
_onPressInView.onPressIn(CGPointZero,
[self canCapture:name],
[self canBubble:name],
Expand All @@ -533,8 +537,8 @@ - (void)longClickTimer:(__unused NSTimer *)timer {
_bLongClick = YES;
if (_onLongClickView && _onLongClickView.onLongClick) {
if ([self checkViewBelongToTouchHandler:_onLongClickView]) {
// _onLongClickView.onLongClick(@{});
const char *name = hippy::kLongClickEvent;
[self willSendGestureEvent:@(name) withPagePoint:CGPointZero toView:_onLongClickView];
_onLongClickView.onLongClick(CGPointZero,
[self canCapture:name],
[self canBubble:name],
Expand Down Expand Up @@ -692,6 +696,7 @@ - (void)cancelTouch {
if (_onPressInView.onPressOut) {
if ([self checkViewBelongToTouchHandler:_onPressInView]) {
const char *name = hippy::kPressOut;
[self willSendGestureEvent:@(name) withPagePoint:CGPointZero toView:_onPressInView];
_onPressInView.onPressOut(CGPointZero,
[self canCapture:name],
[self canBubble:name],
Expand Down Expand Up @@ -720,6 +725,7 @@ - (void)reset {
if (_onPressInView.onPressOut) {
if ([self checkViewBelongToTouchHandler:_onPressInView]) {
const char *name = hippy::kPressOut;
[self willSendGestureEvent:@(name) withPagePoint:CGPointZero toView:_onPressInView];
_onPressInView.onPressOut(CGPointZero,
[self canCapture:name],
[self canBubble:name],
Expand Down Expand Up @@ -829,5 +835,14 @@ - (BOOL)canBePreventInBubbling:(const char *)name {
}


#pragma mark - HippyTouchEventInterceptorProtocol

- (void)willSendGestureEvent:(NSString *)eventName withPagePoint:(CGPoint)point toView:(UIView *)view {
if ([_bridge.delegate respondsToSelector:@selector(willSendGestureEvent:withPagePoint:toView:)]) {
[_bridge.delegate willSendGestureEvent:eventName withPagePoint:point toView:view];
}
}


@end

0 comments on commit 22df117

Please sign in to comment.