UNPKG

3.71 kBPlain TextView Raw
1/*
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#import "RCTScrollEvent.h"
9#import <React/RCTAssert.h>
10
11@implementation RCTScrollEvent
12{
13 CGPoint _scrollViewContentOffset;
14 UIEdgeInsets _scrollViewContentInset;
15 CGSize _scrollViewContentSize;
16 CGRect _scrollViewFrame;
17 CGFloat _scrollViewZoomScale;
18 NSDictionary *_userData;
19 uint16_t _coalescingKey;
20}
21
22@synthesize viewTag = _viewTag;
23@synthesize eventName = _eventName;
24
25- (instancetype)initWithEventName:(NSString *)eventName
26 reactTag:(NSNumber *)reactTag
27 scrollViewContentOffset:(CGPoint)scrollViewContentOffset
28 scrollViewContentInset:(UIEdgeInsets)scrollViewContentInset
29 scrollViewContentSize:(CGSize)scrollViewContentSize
30 scrollViewFrame:(CGRect)scrollViewFrame
31 scrollViewZoomScale:(CGFloat)scrollViewZoomScale
32 userData:(NSDictionary *)userData
33 coalescingKey:(uint16_t)coalescingKey
34{
35 RCTAssertParam(reactTag);
36
37 if ((self = [super init])) {
38 _eventName = [eventName copy];
39 _viewTag = reactTag;
40 _scrollViewContentOffset = scrollViewContentOffset;
41 _scrollViewContentInset = scrollViewContentInset;
42 _scrollViewContentSize = scrollViewContentSize;
43 _scrollViewFrame = scrollViewFrame;
44 _scrollViewZoomScale = scrollViewZoomScale;
45 _userData = userData;
46 _coalescingKey = coalescingKey;
47 }
48 return self;
49}
50
51RCT_NOT_IMPLEMENTED(- (instancetype)init)
52
53- (uint16_t)coalescingKey
54{
55 return _coalescingKey;
56}
57
58- (NSDictionary *)body
59{
60 NSDictionary *body = @{
61 @"contentOffset": @{
62 @"x": @(_scrollViewContentOffset.x),
63 @"y": @(_scrollViewContentOffset.y)
64 },
65 @"contentInset": @{
66 @"top": @(_scrollViewContentInset.top),
67 @"left": @(_scrollViewContentInset.left),
68 @"bottom": @(_scrollViewContentInset.bottom),
69 @"right": @(_scrollViewContentInset.right)
70 },
71 @"contentSize": @{
72 @"width": @(_scrollViewContentSize.width),
73 @"height": @(_scrollViewContentSize.height)
74 },
75 @"layoutMeasurement": @{
76 @"width": @(_scrollViewFrame.size.width),
77 @"height": @(_scrollViewFrame.size.height)
78 },
79 @"zoomScale": @(_scrollViewZoomScale ?: 1),
80 };
81
82 if (_userData) {
83 NSMutableDictionary *mutableBody = [body mutableCopy];
84 [mutableBody addEntriesFromDictionary:_userData];
85 body = mutableBody;
86 }
87
88 return body;
89}
90
91- (BOOL)canCoalesce
92{
93 return YES;
94}
95
96- (RCTScrollEvent *)coalesceWithEvent:(RCTScrollEvent *)newEvent
97{
98 NSArray<NSDictionary *> *updatedChildFrames = [_userData[@"updatedChildFrames"] arrayByAddingObjectsFromArray:newEvent->_userData[@"updatedChildFrames"]];
99 if (updatedChildFrames) {
100 NSMutableDictionary *userData = [newEvent->_userData mutableCopy];
101 userData[@"updatedChildFrames"] = updatedChildFrames;
102 newEvent->_userData = userData;
103 }
104
105 return newEvent;
106}
107
108+ (NSString *)moduleDotMethod
109{
110 return @"RCTEventEmitter.receiveEvent";
111}
112
113- (NSArray *)arguments
114{
115 return @[self.viewTag, RCTNormalizeInputEventName(self.eventName), [self body]];
116}
117
118@end