From 0f9d29f5e4a9eb61abf399d979c1b0269ef84622 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Thu, 10 Nov 2022 18:17:58 +0800 Subject: [PATCH] feat(ios): add getBoundingClientRect method --- ios/sdk/module/uimanager/HippyUIManager.mm | 34 ++++++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/ios/sdk/module/uimanager/HippyUIManager.mm b/ios/sdk/module/uimanager/HippyUIManager.mm index ba9d9485256..669858b7ea1 100644 --- a/ios/sdk/module/uimanager/HippyUIManager.mm +++ b/ios/sdk/module/uimanager/HippyUIManager.mm @@ -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) { @@ -1291,6 +1293,18 @@ - (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) { @@ -1298,14 +1312,18 @@ - (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; } 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; } @@ -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; } @@ -1339,6 +1359,8 @@ - (void)setNeedsLayout:(NSNumber *)hippyTag { } // clang-format on +#pragma mark - + - (NSDictionary *)constantsToExport { NSMutableDictionary *allJSConstants = [NSMutableDictionary new]; NSMutableDictionary *directEvents = [NSMutableDictionary new];