-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Added feature to use rounded values #256
Conversation
Thanks! Pretty hard to review whether this does what we want though. Could you add a test (CSSLayoutPixelRoundingTest.cpp) which tests this thoroughly? I'm specifically interested how this works together with flex-grow and flex-shrink. |
I just tried to add some test, but I can't get it to work to modify <div id="pixel_rounding_flex_basis_flex_grow_row" style="width: 100px; height: 100px; flex-direction: row;">
<div style="flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
<div style="flex-grow: 1;"></div>
</div> it returns 33, 34, 33 as width. But it should return 33.3333, 33.3333, 33.3333. Did I miss something? |
I suggest for this that you right a test manually which tests this specific
|
Jep, I think this is the way to go. I just wanted to use
I had the wrong assumption that there are tests which return numbers with decimal places already. As there where some in the older code base. Had some wtf moments while figuring that out 😅. |
@woehrl01 updated the pull request - view changes |
I added some tests and changed the distribution logic of the remaining pixels. You now can even use the output from |
@woehrl01 updated the pull request - view changes |
@woehrl01 updated the pull request - view changes |
Because browsers do rounding of pixel values automatically I would argue that this perhaps should not be an experimental feature but instead just part of the core library. Also the tests you committed don't seem to be generated via gentest.rb from a fixture provided in /gentext/fixtures. Also generated tests should not be hand modified in any way as they are not able to be re-generated. |
I haven't added the .html file for the generated test as it would be overriden on the next gen. I would suggest to change the generated tests to use |
To date we haven't needed any test fixtures so it has not been a problem really, once we do I think that might be a good idea. I think using |
I'll provide a PR for the gentest change. I'm not sure if this should be a core feature. There are a few pros/cons for this. Pro:
Con:
I did some tests with nodes like |
I'm not all too worried about this. We can test this and currently a lot of the higher level frameworks do their own rounding of values anyways. May be worth keeping this as experimental to be able to run these tests first though.
Let's not speculate in what people may want. We have seen many people have the need for rounded output so let's only tackle this in the case we see a need for it.
Could you elaborate? |
I agree that we shouldn't speculate. Sure, I tried the following tests: <div id="rounding_feature_fractial_1" style="height: 113.4px; width: 100px;">
<div style="height: 20px; flex-grow:1; flex-basis:50px;"></div>
<div style="height: 10px; flex-grow:1;"></div>
<div style="height: 10px; flex-grow:1;"></div>
</div>
<div id="rounding_feature_fractial_2" style="height: 113.6px; width: 100px;">
<div style="height: 20px; flex-grow:1; flex-basis:50px;"></div>
<div style="height: 10px; flex-grow:1;"></div>
<div style="height: 10px; flex-grow:1;"></div>
</div>
<div id="rounding_feature_fractial_3" style="top: 0.3px; height: 113.4px; width: 100px;">
<div style="height: 20px; flex-grow:1; flex-basis:50px;"></div>
<div style="height: 10px; flex-grow:1;"></div>
<div style="height: 10px; flex-grow:1;"></div>
</div>
<div id="rounding_feature_fractial_4" style="top: 0.7px; height: 113.4px; width: 100px;">
<div style="height: 20px; flex-grow:1; flex-basis:50px;"></div>
<div style="height: 10px; flex-grow:1;"></div>
<div style="height: 10px; flex-grow:1;"></div>
</div> I got the code so that it almost works except for one test:
I'm not sure how "bad" those differences are, and if this is even relevant? (In my use case it isn't ;) ) But I only had an hour to look into this, yet. I just wanted to try out how it behaves with fractial input. |
Yeah I was going to mention that. We should expand the tests to handle when a user explicitly sets a fractional input value. We could round the number in the setter perhaps? |
Rounding the numbers via setter could be a possibility to lower the calculation overhead. But you won't get your input back via the getter. My current implementation is rounding inside Do we have the requirement to have the distribution of the values over the children to be pixel perfect with chrome? |
This is fine as long as we document it.
We try to where possible. This makes it much easier to test our implementation. |
I reworked the algorithm, now its more opt-out/-in and the tests are passing as expected. It's also more clean imho. |
@@ -2462,6 +2462,23 @@ bool layoutNodeInternal(const CSSNodeRef node, | |||
return (needToVisitNode || cachedResults == NULL); | |||
} | |||
|
|||
|
|||
void roundToPixelGrid(const CSSNodeRef node){ |
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.
static
@woehrl01 updated the pull request - view changes |
This is starting to look very good. Please also add tests for when input is a decimal value (width, height, flex-basis, left, top, right, bottom) as well as for when a measure function returns a decimal value. |
@woehrl01 updated the pull request - view changes |
made the method |
…t, as they are manually edited afterwards
@woehrl01 updated the pull request - view changes |
@woehrl01 updated the pull request - view changes |
@@ -2549,7 +2549,7 @@ void CSSLog(CSSLogLevel level, const char *format, ...) { | |||
va_end(args); | |||
} | |||
|
|||
static bool experimentalFeatures[CSSExperimentalFeatureCount + 1]; |
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.
Let's keep this in here as it is very little memory and changing this back and forth when adding/removing experiments is just annoying.
For measure function test just return something like |
@woehrl01 updated the pull request - view changes |
Added two tests one for rounding down and one for rounding up. |
@woehrl01 updated the pull request - view changes |
Awesome! I'll import and test this. Once we have run this in production i'll probably remove the experimental flag. Sound good? |
Sounds pretty good to me! Looking forward to your results. 😄 |
@emilsjolander has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
Summary: Added an experimental feature to allow to use only rounded values. See #184. It's not a perfect solution and definitely can be further improved. I'm looking forward to your ideas. Closes facebook/yoga#256 Reviewed By: splhack Differential Revision: D4214168 Pulled By: emilsjolander fbshipit-source-id: 6293352d479b7b4dad258eb3f9e0afaa11cf7236
FYI, the code that does the same thing in React Native is: https://github.com/facebook/react-native/blob/master/React/Views/RCTShadowView.m#L107-L182 The way the algorithm works is to first get the global absolute position by summing up your position + the position of all your parents unrounded and then round it. There are two benefits:
It also not only rounds to the nearest integer but to the nearest pixel density. For example on a 2x retina you can have a value of 0.5 and on 3x you can have 0.33333. |
Thanks @vjeux . I'm not sure if
Please don't get me wrong but I tried to match the calculation, as it is done in chrome, I took the absolute position into account and the result differed from the chrome output. Thanks for the hint with the boarder, those values aren't rounded yet, this should definitely be done. True that the pixel density is not taken into account, maybe density should be configurable? |
Summary: Added an experimental feature to allow to use only rounded values. See facebook#184. It's not a perfect solution and definitely can be further improved. I'm looking forward to your ideas. Closes facebook/yoga#256 Reviewed By: splhack Differential Revision: D4214168 Pulled By: emilsjolander fbshipit-source-id: 6293352d479b7b4dad258eb3f9e0afaa11cf7236
Added an experimental feature to allow to use only rounded values. See #184. It's not a perfect solution and definitely can be further improved. I'm looking forward to your ideas.