Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[BUG] Android: LineBreakMode="WordWrap" not working when label in popup

Open PureWeen opened this issue 1 year ago • 6 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

  • [X] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug

Current Behavior

Copying from here https://github.com/dotnet/maui/issues/20092

Expected Behavior

https://github.com/dotnet/maui/issues/20092

Steps To Reproduce

  • Run the repro
  • click the button to trigger the popup

Link to public reproduction project repository

https://github.com/DawidBester/LabelTextWrapTest

Environment

- .NET MAUI CommunityToolkit: 7.0.3
- OS: macos
- .NET MAUI: 8.0.7-nightly.9897

Anything else?

This isn't really a regression on the MAUI side (AFAICT) https://github.com/dotnet/maui/issues/20092#issuecomment-1910959379

Our SR2 build just causes the defect to happen faster

From debugging through the issue it looks like the culprit here is TryCalculateSize that iterates the entire tree and calls measure on everything.

During that process it looks like every view is measured with the width/height of the window

image

This causes the internal bits of the textView to go somewhat haywire and treat the TextView like a single line control. This might be an android bug? Though I'm guessing this is an issue with popup because it tries to premeasure everything outside of a layout cycle which confuses the internal book keeping.

All the info beyond this point is just my interpretation of the Android code so it might be off a bit :-)

If you dive into the TextView onMeasure code you'll see it has this thing called "mBoringLayout" which is what the layout engine is using for a single line text field. The width of this is just the measured Text on a single line.

The width initially just gets set to the boring width

https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/android/widget/TextView.java;l=10911?q=TextView.java

and then it only adjusts if the measure spec is at_most https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/android/widget/TextView.java;l=10969?q=TextView.java

The calls from the MCT Popup cause the internal bookkeeping to switch to mBoringLayout because it's doing an exact measure spec using the entire size of the screen.

The code that would need to run in order for it to switch away from BoringLayout is here. https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/android/widget/TextView.java;l=11000?q=TextView.java

But the width and maximum haven't changed so subsequent runs of measure don't revaluate the layout.

Like, I can fix this issue by calling "this.SetMaxWidth(this.MeasuredWidth);" inside our measure code. But we don't really want to do that.

Calling Measure and layout with arbitrary things outside of the normal measure/layout cycles usually causes android controls to get into a really weird state.

PureWeen avatar Jan 25 '24 23:01 PureWeen

@PureWeen Is there a workaroud? Or is wordwrap in popups just broken in 8.0.7?

czmirek avatar Mar 01 '24 14:03 czmirek

I am going to try and look at the Popup issues. We have a number of them, especially on Android. I hope they are related as they seem to be described in a similar way

bijington avatar Mar 01 '24 16:03 bijington

+1 on this one

  • MAUI 8.0.7
  • MCT 7.0.1 (p.s. I think @PureWeen is slightly off with MCT version?)

Syed-RI avatar Mar 11 '24 12:03 Syed-RI

Has anyone found any workaround instead of reverting back to MAUI 8.0.6?

Syed-RI avatar Mar 12 '24 10:03 Syed-RI

I've been building a branch of sample updates to cover all or at least the majority of our Popup related issues to help us verify fixes moving forwards. There is a PR out to improve Android sizing but I'm just keen to make sure we cover as many scenarios as possible

bijington avatar Mar 12 '24 19:03 bijington

thank you @bijington !

Syed-RI avatar Mar 13 '24 09:03 Syed-RI

@bijington Thanks for investigating the text wrapping issues in popups. I also hit the issue of text not wrapping in popups, and given that a requirement of my app is that it runs well with the device's large font setting, it means the app fails in many places. I reverted back from 8.0.10 to 8.0.6, and this seemed to avoid the issue in all places except for text in CollectionView items in popups. Many of my app's settings are shown in CollectionView items in popups, and so I'm hitting it in many places. I've not been able to workaround this, either by moving to different packages, or by messing with all sorts of properties in the item, (LineBreakMode, MaximumWidthRequest, MaxLines, etc). Is this something your sample branch already reproduces, or should I build a sample app to demonstrate it? Thanks.

gbarkerz avatar Mar 19 '24 15:03 gbarkerz

I hope this can get resolved soon as it's a dealbreaker for me and I can't upgrade beyond 8.0.6 because of it. The only option I would have otherwise is to ditch the popup and use modal pages.

primozcerar avatar Mar 21 '24 07:03 primozcerar

@gbarkerz let me try and push my branch up later and we can work out how to enhance it. I haven't added a CollectionView sample but the aim of my changes should make it easy to add more samples.

bijington avatar Mar 21 '24 08:03 bijington

Thanks @bijington. Interestingly in my app, the CollectionView item's height is automatically increased to account for the wrapping text, the trouble is that the text doesn't actually wrap. This image shows an example of the issue occurring in the Settings UI in my app.

NoWrap

gbarkerz avatar Mar 21 '24 10:03 gbarkerz

@gbarkerz I have pushed my branch here, added a basic CollectionView sample and also opened a PR to make it easier to see the changes: https://github.com/CommunityToolkit/Maui/pull/1770

I am not sure this is the easiest to follow moving forward (see my comment on magic in the description). Hopefully people can weigh in with some input here

bijington avatar Mar 21 '24 20:03 bijington

Seems to be fixed with toolkit version 8.0.0. and MAUI version 8.0.14. Can someone else confirm?

primozcerar avatar Apr 04 '24 09:04 primozcerar

@mattyjjpearce As I understand it PR #1770 was just a set of tests to track changes. I'm not sure where and how the issue got fixed in MCT 8.0.0 and MAUI 8.0.14. It might have regressed again in newer versions of MAUI which I have not tested. And as I mentioned I had only one specific use case where this issue appeared, it might be different for other use cases, that's why I asked others for input. I suspect the ticket was closed due to my comment and no one else reported otherwise.

primozcerar avatar May 13 '24 10:05 primozcerar

Once I updated my app to use the MAUI 8.0.14, text in popups wrapped as I needed it to. I shared a picture of text correctly wrapping in my app at https://x.com/gbarkerZ/status/1777429048357949443. I've not updated to newer packages since then.

gbarkerz avatar May 14 '24 12:05 gbarkerz