forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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