Skip to content

Commit

Permalink
Add UIManager.measureInWindow to get window coordinates
Browse files Browse the repository at this point in the history
Summary: When embedding in a hybrid app, we sometimes present new modal views or windows that have a different frame from the original root view. This API allows us to get coordinates in the application's window frame, which should be valid in any fullscreen view.

Reviewed By: majak

Differential Revision: D2939827

fb-gh-sync-id: 06b93cc2cb3519a25819c6efa445c779314dd673
shipit-source-id: 06b93cc2cb3519a25819c6efa445c779314dd673
  • Loading branch information
javache authored and Facebook Github Bot 4 committed Mar 1, 2016
1 parent d94a567 commit f67fa82
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Libraries/ReactIOS/NativeMethodsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ type MeasureOnSuccessCallback = (
pageY: number
) => void

type MeasureInWindowOnSuccessCallback = (
x: number,
y: number,
width: number,
height: number,
) => void

type MeasureLayoutOnSuccessCallback = (
left: number,
top: number,
Expand Down Expand Up @@ -83,6 +90,28 @@ var NativeMethodsMixin = {
);
},

/**
* Determines the location of the given view in the window and returns the
* values via an async callback. If the React root view is embedded in
* another native view, this will give you the absolute coordinates. If
* successful, the callback will be called be called with the following
* arguments:
*
* - x
* - y
* - width
* - height
*
* Note that these measurements are not available until after the rendering
* has been completed in native.
*/
measureInWindow: function(callback: MeasureInWindowOnSuccessCallback) {
UIManager.measureInWindow(
findNodeHandle(this),
mountSafeCallback(this, callback)
);
},

/**
* Like [`measure()`](#measure), but measures the view relative an ancestor,
* specified as `relativeToNativeNode`. This means that the returned x, y
Expand Down
23 changes: 23 additions & 0 deletions React/Modules/RCTUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,29 @@ - (void)setNeedsLayout
}];
}

RCT_EXPORT_METHOD(measureInWindow:(nonnull NSNumber *)reactTag
callback:(RCTResponseSenderBlock)callback)
{
[self addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[reactTag];
if (!view) {
// this view was probably collapsed out
RCTLogWarn(@"measure cannot find view with tag #%@", reactTag);
callback(@[]);
return;
}

// Return frame coordinates in window
CGRect windowFrame = [view.window convertRect:view.frame fromView:view.superview];
callback(@[
@(windowFrame.origin.x),
@(windowFrame.origin.y),
@(windowFrame.size.width),
@(windowFrame.size.height),
]);
}];
}

static void RCTMeasureLayout(RCTShadowView *view,
RCTShadowView *ancestor,
RCTResponseSenderBlock callback)
Expand Down

0 comments on commit f67fa82

Please sign in to comment.