microsoft-ui-xaml: Many layout problems and at least one bug caused by layout rounding
Describe the bug
The value returned from MeasureOverride in the layout process seems to be rounded to the nearest display pixel (not view pixel). For example, the desired size of the element will be set to this rounded value. This is not a bug in itself, but there needs to be a clear strategy for handling this, and there need to be examples of how to deal with this issue when writing custom controls.
For an example of one of the bugs caused by layout round errors in the layout process, see https://github.com/microsoft/microsoft-ui-xaml/issues/477. I wrote a long comment in that thread. I think the bug is because a value is alternately being rounded up and down - so this needs to be made consistent, perhaps with some tolerance.
Now on to another example that causes a problem: trying to write a WrapPanel component. At the moment, the one in the community toolkit works mainly by accident and will not work properly for certain layout measurements. The basic problem is, you compute the size in MeasureOverride and return that value. But what if the width was between two display pixels, for example, 10.2 at 100% display scaling? This will be rounded down to 10 by the layout system. Then when it comes to ArrangeOverride, you will find that the child elements do not quite fit in the space available and wrap onto another line, overflowing the panel.
What is a potential solution to the WrapPanel problem? Well, one solution is to round up to the nearest display pixel before returning the measured size in the MeasureOverride function. However, to do this one needs access to DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel. I have checked this and it’s actually a relatively expensive operation to perform on every measure (takes 0.1ms), therefore I’d like to offer one suggestion. Provide fast access to a cached value of DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel. For example, you could add a listener for DisplayInformation.GetForCurrentView().DpiChanged and update some static property for the view that can be easily accessed - not sure where. Either that, or provide a fast helper method for rounding view pixels to display pixels (closest or up or down). Note: it needs to be fast from C#, not just C++.
| Windows 10 version | Saw the problem? |
|---|---|
| May 2019 Update (18362) | Yes |
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 15 (6 by maintainers)
@codendone - thanks for the discussion. I think that you are still thinking of render rounding in not quite the right conceptual way, which is probably my fault. Perhaps I should have called it pixel snapping. I have tried to explain it better in this feature proposal - https://github.com/microsoft/microsoft-ui-xaml/issues/9114. You can go ahead and close this issue and we can use the feature proposal for any further discussion. I should add that I have done no research on the subject and maybe it is more difficult than I imagine.
Thanks for the reply. A pixel snapping API that happens on render relative to a global origin would be great, assuming it could apply to all controls. Hopefully this could include shapes like Rectangle (maybe via an option) - at the moment Rectangle stroke is not snapped to display pixels, which means a Rectangle with a stroke width of 1 at 150% display scaling looks slightly blurred compared to a Border control with a stroke width of 1.
If you don’t intend to change the behavior of layout rounding, then adding it to the docs would certainly be useful. It works as a concept (other than the slight issues it causes with designing a wrap panel and similar controls that I mentioned at the beginning) if everything has its natural width or Grid ‘star’ widths, with only a few things specifying a width, which probably covers most use cases, including how I’ve been working, but it breaks down when controls with specified widths need to fit together exactly, due to the acculumated error effect that I pointed out. If I had the option to replace it with pixel snapping/rounding on render, then I would, so I’d be keen to see that feature.