Skip to content

Commit

Permalink
feat(ios): add getBoundingClientRect method
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Nov 14, 2022
1 parent 8ba8d3f commit 0f9d29f
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions ios/sdk/module/uimanager/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,8 @@ - (void)setNeedsLayout:(NSNumber *)hippyTag {
}
}

#pragma mark - Measure Functions

// clang-format off
HIPPY_EXPORT_METHOD(measure:(nonnull NSNumber *)hippyTag
callback:(HippyResponseSenderBlock)callback) {
Expand Down Expand Up @@ -1291,21 +1293,37 @@ - (void)setNeedsLayout:(NSNumber *)hippyTag {
}
// clang-format on

static NSString * const HippyUIManagerGetBoundingRelToContainerKey = @"relToContainer";
static NSString * const HippyUIManagerGetBoundingErrMsgrKey = @"errMsg";
HIPPY_EXPORT_METHOD(getBoundingClientRect:(nonnull NSNumber *)hippyTag
options:(nullable NSDictionary *)options
callback:(HippyResponseSenderBlock)callback ) {
if (options && [[options objectForKey:HippyUIManagerGetBoundingRelToContainerKey] boolValue]) {
[self measureInWindow:hippyTag callback:callback];
} else {
[self measureInAppWindow:hippyTag callback:callback];
}
}

// clang-format off
HIPPY_EXPORT_METHOD(measureInWindow:(nonnull NSNumber *)hippyTag
callback:(HippyResponseSenderBlock)callback) {
[self addUIBlock:^(__unused HippyUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[hippyTag];
if (!view) {
// this view was probably collapsed out
HippyLogWarn(@"measure cannot find view with tag #%@", hippyTag);
callback(@[]);
NSString *formatStr = @"measure cannot find view with tag #%@";
NSString *errMsg = [NSString stringWithFormat:formatStr, hippyTag];
HippyLogWarn(formatStr, hippyTag);
callback(@[@{HippyUIManagerGetBoundingErrMsgrKey : errMsg}]);
return;
}
UIView *rootView = viewRegistry[view.rootTag];
if (!rootView) {
HippyLogWarn(@"measure cannot find view's root view with tag #%@", hippyTag);
callback(@[]);
NSString *formatStr = @"measure cannot find view's root view with tag #%@";
NSString *errMsg = [NSString stringWithFormat:formatStr, hippyTag];
HippyLogWarn(formatStr, hippyTag);
callback(@[@{HippyUIManagerGetBoundingErrMsgrKey : errMsg}]);
return;
}

Expand All @@ -1325,8 +1343,10 @@ - (void)setNeedsLayout:(NSNumber *)hippyTag {
UIView *view = viewRegistry[hippyTag];
if (!view) {
// this view was probably collapsed out
HippyLogWarn(@"measure cannot find view with tag #%@", hippyTag);
callback(@[]);
NSString *formatStr = @"measure cannot find view with tag #%@";
NSString *errMsg = [NSString stringWithFormat:formatStr, hippyTag];
HippyLogWarn(formatStr, hippyTag);
callback(@[@{HippyUIManagerGetBoundingErrMsgrKey : errMsg}]);
return;
}

Expand All @@ -1339,6 +1359,8 @@ - (void)setNeedsLayout:(NSNumber *)hippyTag {
}
// clang-format on

#pragma mark -

- (NSDictionary<NSString *, id> *)constantsToExport {
NSMutableDictionary<NSString *, NSDictionary *> *allJSConstants = [NSMutableDictionary new];
NSMutableDictionary<NSString *, NSDictionary *> *directEvents = [NSMutableDictionary new];
Expand Down

0 comments on commit 0f9d29f

Please sign in to comment.