forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 16
NativeMeasurer for Android #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type {TurboModule} from '../TurboModule/RCTExport'; | ||
| import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; | ||
|
|
||
| type MeasureOnSuccessCallback = ( | ||
| x: number, | ||
| y: number, | ||
| width: number, | ||
| height: number, | ||
| pageX: number, | ||
| pageY: number, | ||
| ) => void; | ||
|
|
||
| type MeasureInWindowOnSuccessCallback = ( | ||
| x: number, | ||
| y: number, | ||
| width: number, | ||
| height: number, | ||
| ) => void; | ||
|
|
||
| export interface Spec extends TurboModule { | ||
| +measureNatively: (viewTag: number, callback: MeasureOnSuccessCallback) => void, | ||
| +measureInWindowNatively: ( | ||
| viewTag: number, | ||
| callback: MeasureInWindowOnSuccessCallback, | ||
| ) => void, | ||
| } | ||
|
|
||
| export default (TurboModuleRegistry.get<Spec>( | ||
| 'NativeFabricMeasurerTurboModule', | ||
| ): ?Spec); |
59 changes: 59 additions & 0 deletions
59
ReactAndroid/src/main/java/com/facebook/react/animated/NativeFabricMeasurerModule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package com.facebook.react.animated; | ||
|
|
||
| import android.util.Log; | ||
| import android.view.View; | ||
|
|
||
| import com.facebook.fbreact.specs.NativeFabricMeasurerTurboModuleSpec; | ||
| import com.facebook.react.bridge.Callback; | ||
| import com.facebook.react.bridge.ReactApplicationContext; | ||
| import com.facebook.react.bridge.UIManager; | ||
| import com.facebook.react.bridge.UiThreadUtil; | ||
| import com.facebook.react.bridge.WritableNativeMap; | ||
| import com.facebook.react.module.annotations.ReactModule; | ||
| import com.facebook.react.uimanager.NativeViewMeasurer; | ||
| import com.facebook.react.uimanager.PixelUtil; | ||
| import com.facebook.react.uimanager.UIManagerHelper; | ||
| import com.facebook.react.uimanager.common.UIManagerType; | ||
|
|
||
| @ReactModule(name = NativeFabricMeasurerTurboModuleSpec.NAME) | ||
| public class NativeFabricMeasurerModule extends NativeFabricMeasurerTurboModuleSpec implements NativeViewMeasurer.ViewProvider { | ||
| private final NativeViewMeasurer measurer = new NativeViewMeasurer(this); | ||
|
|
||
| public NativeFabricMeasurerModule(ReactApplicationContext reactContext) { | ||
| super(reactContext); | ||
| } | ||
|
|
||
| @Override | ||
| public void measureNatively(double viewTag, Callback callback) { | ||
| getReactApplicationContext().runOnUiQueueThread(() -> { | ||
| int[] output = measurer.measure((int) viewTag); | ||
| float x = PixelUtil.toDIPFromPixel(output[0]); | ||
| float y = PixelUtil.toDIPFromPixel(output[1]); | ||
| float width = PixelUtil.toDIPFromPixel(output[2]); | ||
| float height = PixelUtil.toDIPFromPixel(output[3]); | ||
| callback.invoke(0, 0, width, height, x, y); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void measureInWindowNatively(double viewTag, Callback callback) { | ||
| getReactApplicationContext().runOnUiQueueThread(() -> { | ||
| int[] output = measurer.measureInWindow((int) viewTag); | ||
| float x = PixelUtil.toDIPFromPixel(output[0]); | ||
| float y = PixelUtil.toDIPFromPixel(output[1]); | ||
| float width = PixelUtil.toDIPFromPixel(output[2]); | ||
| float height = PixelUtil.toDIPFromPixel(output[3]); | ||
| callback.invoke(x, y, width, height); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public View provideView(int tag) { | ||
| UIManager uiManager = UIManagerHelper.getUIManager(getReactApplicationContext(), UIManagerType.FABRIC); | ||
| if (uiManager == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return uiManager.resolveView(tag); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewMeasurer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
| package com.facebook.react.uimanager; | ||
|
|
||
| import android.graphics.Matrix; | ||
| import android.graphics.Rect; | ||
| import android.graphics.RectF; | ||
| import android.view.View; | ||
| import android.view.ViewParent; | ||
|
|
||
| import com.facebook.common.logging.FLog; | ||
| import com.facebook.react.bridge.UiThreadUtil; | ||
|
|
||
| public class NativeViewMeasurer { | ||
| public static final String TAG = "NativeViewMeasurer"; | ||
| private final ViewProvider viewProvider; | ||
| public NativeViewMeasurer(ViewProvider viewProvider) { | ||
| this.viewProvider = viewProvider; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true on success, false on failure. If successful, after calling, output buffer will be | ||
| * {x, y, width, height}. | ||
| */ | ||
| public int[] measure(int tag) { | ||
| UiThreadUtil.assertOnUiThread(); | ||
|
|
||
| int[] outputBuffer = {0, 0, 0, 0, 0, 0}; | ||
| View v = viewProvider.provideView(tag); | ||
| if (v == null) { | ||
| FLog.w(TAG, "measure: No native view for " + tag + " currently exists"); | ||
| return outputBuffer; | ||
| } | ||
|
|
||
| View rootView = (View) RootViewUtil.getRootView(v); | ||
| // It is possible that the RootView can't be found because this view is no longer on the screen | ||
| // and has been removed by clipping | ||
| if (rootView == null) { | ||
| FLog.w(TAG, "measure: Native view " + tag + " is no longer on screen"); | ||
| return outputBuffer; | ||
| } | ||
|
|
||
| computeBoundingBox(rootView, outputBuffer); | ||
| int rootX = outputBuffer[0]; | ||
| int rootY = outputBuffer[1]; | ||
| computeBoundingBox(v, outputBuffer); | ||
| outputBuffer[0] -= rootX; | ||
| outputBuffer[1] -= rootY; | ||
| return outputBuffer; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the coordinates of a view relative to the window (not just the RootView which is what | ||
| * measure will return) | ||
| * | ||
| * @param tag - the tag for the view | ||
| */ | ||
| public int[] measureInWindow(int tag) { | ||
| UiThreadUtil.assertOnUiThread(); | ||
| View v = viewProvider.provideView(tag); | ||
| int[] outputBuffer = {0, 0, 0, 0}; | ||
| if (v == null) { | ||
| FLog.w(TAG, "measureInWindow: No native view for " + tag + " currently exists"); | ||
| return outputBuffer; | ||
| } | ||
|
|
||
| int[] locationOutputBuffer = new int[2]; | ||
| v.getLocationOnScreen(locationOutputBuffer); | ||
|
|
||
| // we need to subtract visibleWindowCoords - to subtract possible window insets, split screen or | ||
| // multi window | ||
| Rect visibleWindowFrame = new Rect(); | ||
| v.getWindowVisibleDisplayFrame(visibleWindowFrame); | ||
| outputBuffer[0] = locationOutputBuffer[0] - visibleWindowFrame.left; | ||
| outputBuffer[1] = locationOutputBuffer[1] - visibleWindowFrame.top; | ||
|
|
||
| // outputBuffer[0,1] already contain what we want | ||
| outputBuffer[2] = v.getWidth(); | ||
| outputBuffer[3] = v.getHeight(); | ||
| return outputBuffer; | ||
| } | ||
|
|
||
| private void computeBoundingBox(View view, int[] outputBuffer) { | ||
| RectF boundingBox = new RectF(0, 0, view.getWidth(), view.getHeight()); | ||
| boundingBox.set(0, 0, view.getWidth(), view.getHeight()); | ||
| mapRectFromViewToWindowCoords(view, boundingBox); | ||
|
|
||
| outputBuffer[0] = Math.round(boundingBox.left); | ||
| outputBuffer[1] = Math.round(boundingBox.top); | ||
| outputBuffer[2] = Math.round(boundingBox.right - boundingBox.left); | ||
| outputBuffer[3] = Math.round(boundingBox.bottom - boundingBox.top); | ||
| outputBuffer[4] = Math.round(view.getLeft()); | ||
| outputBuffer[5] = Math.round(view.getTop()); | ||
| } | ||
|
|
||
| private void mapRectFromViewToWindowCoords(View view, RectF rect) { | ||
| Matrix matrix = view.getMatrix(); | ||
| if (!matrix.isIdentity()) { | ||
| matrix.mapRect(rect); | ||
| } | ||
|
|
||
| rect.offset(view.getLeft(), view.getTop()); | ||
|
|
||
| ViewParent parent = view.getParent(); | ||
| while (parent instanceof View) { | ||
| View parentView = (View) parent; | ||
|
|
||
| rect.offset(-parentView.getScrollX(), -parentView.getScrollY()); | ||
|
|
||
| matrix = parentView.getMatrix(); | ||
| if (!matrix.isIdentity()) { | ||
| matrix.mapRect(rect); | ||
| } | ||
|
|
||
| rect.offset(parentView.getLeft(), parentView.getTop()); | ||
|
|
||
| parent = parentView.getParent(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public interface ViewProvider { | ||
| View provideView(int tag); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check will return false for Paper?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! Luckily this entire file is only used for Fabric, so we don't need to check for Paper anywhere here.