Skip to content

Commit

Permalink
fix(ios): edit getBoundingClientRect callback format
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg authored and zoomchan-cxj committed Nov 16, 2022
1 parent e19a18a commit 55103b8
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions ios/sdk/module/uimanager/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,6 @@ - (void)setNeedsLayout:(NSNumber *)hippyTag {

#pragma mark - Measure Functions

// clang-format off
HIPPY_EXPORT_METHOD(measure:(nonnull NSNumber *)hippyTag
callback:(HippyResponseSenderBlock)callback) {
[self addUIBlock:^(__unused HippyUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
Expand Down Expand Up @@ -1291,39 +1290,52 @@ - (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];
[self measureInWindow:hippyTag withErrMsg:YES callback:callback];
} else {
[self measureInAppWindow:hippyTag callback:callback];
[self measureInAppWindow:hippyTag withErrMsg:YES callback:callback];
}
}

// clang-format off
HIPPY_EXPORT_METHOD(measureInWindow:(nonnull NSNumber *)hippyTag
callback:(HippyResponseSenderBlock)callback) {
callback:(HippyResponseSenderBlock)callback) {
// keep the same as the old version, no errMsg return
[self measureInWindow:hippyTag withErrMsg:NO callback:callback];
}

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

Expand All @@ -1334,30 +1346,37 @@ - (void)setNeedsLayout:(NSNumber *)hippyTag {
@"y":@(windowFrame.origin.y)}]);
}];
}
// clang-format on

// clang-format off
HIPPY_EXPORT_METHOD(measureInAppWindow:(nonnull NSNumber *)hippyTag
callback:(HippyResponseSenderBlock)callback) {
callback:(HippyResponseSenderBlock)callback) {
// keep the same as the old version, no errMsg return
[self measureInAppWindow:hippyTag withErrMsg:NO callback:callback];
}

- (void)measureInAppWindow:(nonnull NSNumber *)hippyTag
withErrMsg:(BOOL)withErrMsg
callback:(HippyResponseSenderBlock)callback {
[self addUIBlock:^(__unused HippyUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[hippyTag];
if (!view) {
// this view was probably collapsed out
NSString *formatStr = @"measure cannot find view with tag #%@";
NSString *errMsg = [NSString stringWithFormat:formatStr, hippyTag];
HippyLogWarn(formatStr, hippyTag);
callback(@[@{HippyUIManagerGetBoundingErrMsgrKey : errMsg}]);
if (withErrMsg) {
NSString *errMsg = [NSString stringWithFormat:formatStr, hippyTag];
callback(@[@{HippyUIManagerGetBoundingErrMsgrKey : errMsg}]);
} else {
callback(@[]);
}
return;
}

CGRect windowFrame = [view.window convertRect:view.frame fromView:view.superview];
callback(@[@{@"width":@(CGRectGetWidth(windowFrame)),
@"height": @(CGRectGetHeight(windowFrame)),
@"x":@(windowFrame.origin.x),
@"y":@(windowFrame.origin.y)}]);
}];
}
// clang-format on

#pragma mark -

Expand Down

0 comments on commit 55103b8

Please sign in to comment.