Skip to content
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

feat(android): percentage support in translate #43193

Conversation

intergalacticspacehighway
Copy link
Contributor

Summary:

This PR adds percentage support in translate properties for android. Isolating this PR for easier reviews.

Changelog:

[Android] [ADDED] - Percentage support in translate

Test Plan:

  • Checkout TransformExample.js -> Translate percentage example.
  • Added a simple test in processTransform-test.js. The regex is not perfect (values like 20px%, 20%px will pass, can be improved, let me know!)

Related PRs - #43191, #43192

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Feb 25, 2024
@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 17,882,328 +198
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 21,240,669 +98
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: 12f2c3c
Branch: main

Copy link
Member

@javache javache left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! A few questions.

@@ -177,18 +177,15 @@ public void setBackgroundColor(@NonNull T view, int backgroundColor) {
public void setTransform(@NonNull T view, @Nullable ReadableArray matrix) {
view.setTag(R.id.transform, matrix);
view.setTag(R.id.invalidate_transform, true);
view.addOnLayoutChangeListener(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably discussed this in the original transformOrigin PR, but why can't we override layout (or as Android prefers onLayout) to update the transform?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i remember discussing it here - #38558 (comment). To override layout i think we'll have to move transform logic to ReactViewGroup.

onLayout runs during layout pass and can be used to update child view positions, in our case we update transform of the view. Not very ideal, i guess in the long run moving it to RootViewGroup might be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked code, and Android will no-op if adding existing listener, but it's a little bit smelly that we can add ourselves as a listener, after we have already added ourselves as listener. Not sure a better way though (probably don't want to waste cycles to always attach).

facebook-github-bot pushed a commit that referenced this pull request May 17, 2024
Summary:
This PR adds percentage support in translate properties for new arch iOS. Isolating this PR for easier reviews.

The approach taken here introduces usage of `ValueUnit` struct for transform operations so it can support `%` in translates and delay the generation of actual transform matrix until view dimensions are known. I have tried to keep the changes minimal and reuse existing APIs, open to changes if there's an alternative approach.

## Changelog:
[IOS] [ADDED] - Percentage support in translate in new arch.
<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: #43192

Test Plan:
- Checkout TransformExample.js -> Translate percentage example.
- Added a simple test in `processTransform-test.js`. The regex is not perfect (values like 20px%, 20%px will pass, can be improved, let me know!)

Related PRs - #43193, #43191

Reviewed By: javache

Differential Revision: D56802425

Pulled By: NickGerleman

fbshipit-source-id: 978cbbdde004afe1e68ffee9a3c7eb7d16336b46
Copy link
Contributor

@NickGerleman NickGerleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of minor comments, but this seems pretty nicely scoped 👍

@@ -177,18 +177,15 @@ public void setBackgroundColor(@NonNull T view, int backgroundColor) {
public void setTransform(@NonNull T view, @Nullable ReadableArray matrix) {
view.setTag(R.id.transform, matrix);
view.setTag(R.id.invalidate_transform, true);
view.addOnLayoutChangeListener(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked code, and Android will no-op if adding existing listener, but it's a little bit smelly that we can add ourselves as a listener, after we have already added ourselves as listener. Not sure a better way though (probably don't want to waste cycles to always attach).

@@ -171,7 +171,7 @@ public void onLayoutChange(
if ((currentHeight != oldHeight || currentWidth != oldWidth)) {
ReadableArray transformOrigin = (ReadableArray) v.getTag(R.id.transform_origin);
ReadableArray transformMatrix = (ReadableArray) v.getTag(R.id.transform);
if (transformMatrix != null && transformOrigin != null) {
if (transformMatrix != null || transformOrigin != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a matrix most of the time, right? Can we rename?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, renamed to transforms since it's an array.

@@ -130,6 +152,14 @@ public static void processTransform(
}
}

private static double parsePercentageToValue(String stringValue, double dimension) {
if (stringValue.endsWith("%")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the old code was reliable here, but we usually want to avoid native crashes on invalid style values. So we probably want to be handling NumberFormatException somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, updated!

@intergalacticspacehighway
Copy link
Contributor Author

I checked code, and Android will no-op if adding existing listener, but it's a little bit smelly that we can add ourselves as a listener, after we have already added ourselves as listener. Not sure a better way though (probably don't want to waste cycles to always attach).

i moved it in onAfterUpdateTransaction so it gets called a bit less and yes duplicate calls for same listener will be ignored. But i agree this is not very optimal. If all views are getting extended from ReactViewGroup then we can move the entire transform logic there as it already has a onSizeCallback as mentioned here.

@facebook-github-bot
Copy link
Contributor

@NickGerleman has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label May 24, 2024
@facebook-github-bot
Copy link
Contributor

@NickGerleman merged this pull request in c13790f.

Copy link

This pull request was successfully merged by @intergalacticspacehighway in c13790f.

When will my fix make it into a release? | How to file a pick request?

kosmydel pushed a commit to kosmydel/react-native that referenced this pull request Jun 11, 2024
Summary:
This PR adds percentage support in translate properties for new arch iOS. Isolating this PR for easier reviews.

The approach taken here introduces usage of `ValueUnit` struct for transform operations so it can support `%` in translates and delay the generation of actual transform matrix until view dimensions are known. I have tried to keep the changes minimal and reuse existing APIs, open to changes if there's an alternative approach.

## Changelog:
[IOS] [ADDED] - Percentage support in translate in new arch.
<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#43192

Test Plan:
- Checkout TransformExample.js -> Translate percentage example.
- Added a simple test in `processTransform-test.js`. The regex is not perfect (values like 20px%, 20%px will pass, can be improved, let me know!)

Related PRs - facebook#43193, facebook#43191

Reviewed By: javache

Differential Revision: D56802425

Pulled By: NickGerleman

fbshipit-source-id: 978cbbdde004afe1e68ffee9a3c7eb7d16336b46
kosmydel pushed a commit to kosmydel/react-native that referenced this pull request Jun 11, 2024
Summary:
This PR adds percentage support in translate properties for android. Isolating this PR for easier reviews.

## Changelog:
[Android] [ADDED] - Percentage support in translate
<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#43193

Test Plan:
- Checkout TransformExample.js -> Translate percentage example.
- Added a simple test in `processTransform-test.js`. The regex is not perfect (values like 20px%, 20%px will pass, can be improved, let me know!)

Related PRs - facebook#43191, facebook#43192

Reviewed By: joevilches

Differential Revision: D57723216

Pulled By: NickGerleman

fbshipit-source-id: c9da007678341b62745df858f043821bcc662a98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants