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

Grid with auto width columns overflows its container #23735

Open
RsZoli opened this issue Jul 20, 2024 · 15 comments
Open

Grid with auto width columns overflows its container #23735

RsZoli opened this issue Jul 20, 2024 · 15 comments
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter layout-grid platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@RsZoli
Copy link

RsZoli commented Jul 20, 2024

Description

111

The lightblue background indicates a stacklayout, the gold/lightgreen/pink trio is a grid with 3 columns, each set to auto width!

The 3 columns each has a label with linebreakmode set to middletruncation, yet this does not happen, the grid overflows its container!

But, if i remove the auto width from the 2 outside columns, the linkebreakmode kicks in and it works perfectly!

333

However, if i reduce the content of the columns, truncation still happens but it should not be, since there is plenty space for the grid to grow!

222

Steps to Reproduce

Create a new .NET MAUI app, put a 3 coulm Grid inside a StackLayout in a ContentPage, set the Grid's columns to auto with and observe!

Link to public reproduction project repository

No response

Version with bug

8.0.61 SR6.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@RsZoli RsZoli added the t/bug Something isn't working label Jul 20, 2024
Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@MartyIX MartyIX added layout-grid area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter labels Jul 22, 2024
@mattleibow mattleibow added the s/needs-repro Attach a solution or code which reproduces the issue label Jul 23, 2024
@dotnet-policy-service dotnet-policy-service bot added the s/no-recent-activity Issue has had no recent activity label Jul 29, 2024
@RsZoli
Copy link
Author

RsZoli commented Jul 29, 2024

Here you go: https://github.com/RsZoli/GitHubRepros

Screenshot_1722267083

@dotnet-policy-service dotnet-policy-service bot added s/needs-attention Issue has more information and needs another look and removed s/needs-repro Attach a solution or code which reproduces the issue s/no-recent-activity Issue has had no recent activity labels Jul 29, 2024
@ninachen03 ninachen03 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 31, 2024
@ninachen03
Copy link

ninachen03 commented Jul 31, 2024

I can repro this issue at Android platform on the latest 17.11.0 Preview 5.0 (8.0.70 & 8.0.61 &8.0.3)
GitHubRepros-main.zip

@FlavioGoncalves-Cayas
Copy link

@mattleibow Please, can you guys do something about this?
You pretty much tell everyone to use Grids instead of StackLayouts if we want to have child elements to be constrained in their size, but somehow not even the Grid works properly.
Why should an Auto column ever let it's child render larger than the grid itself?

@michiel-nwa
Copy link

michiel-nwa commented Oct 21, 2024

According to #10494 (comment) this is expected behaviour. It seems Auto is pretty much only usable for fixed width items and just save you from duplicating the size explicitly.

For your specific case I guess *,auto,* should work, but there doesn't seem to be anything generic that performs like you would expect (basically HTML table layout logic that optimizes your grid to make the best use of available space)

@FlavioGoncalves-Cayas
Copy link

That doesn't make any sense to me.
So a layout that behaves like the following images would not be achievable:

XAML left:

<Grid ColumnDefinitions="Auto,42" ColumnSpacing="16">
    <Label Grid.Column="0"
           LineBreakMode="TailTruncation"
           Text="short text" />
    <Border Grid.Column="1"
            BackgroundColor="Red"
            WidthRequest="42"
            HeightRequest="42"
            VerticalOptions="Start" />
  </Grid>

XAML right if it would work propertly:

<Grid ColumnDefinitions="Auto,42" ColumnSpacing="16">
    <Label Grid.Column="0"
           LineBreakMode="TailTruncation"
           Text="loger text that should truncate at the end when there is too much text to fit in the available space" />
    <Border Grid.Column="1"
            BackgroundColor="Red"
            WidthRequest="42"
            HeightRequest="42"
            VerticalOptions="Start" />
  </Grid>

Instead, above XAML produces this:

So my requirement that the text should only take the space it needs, but never more than is available can not be satisfied with any layout in a responsive way. Using a WidthRequest or MaximumWidthRequest for the Label is not a solution as that makes the layout not adapt automatically to different screen sizes.

@FlavioGoncalves-Cayas
Copy link

@hartez you said in the linked comment that this is by design. And sure the label can measure with infinite space, that's fine. But shouldn't the arrange step of the layout process limit the Labels width to something that does not break the Grids bounds and still fits the element with the fixed size.

@michiel-nwa
Copy link

@FlavioGoncalves-Cayas for that specific case I think you can do:
<Grid HorizontalOptions="Start" ColumnDefinitions="*,42" ColumnSpacing="16">

@FlavioGoncalves-Cayas
Copy link

FlavioGoncalves-Cayas commented Oct 21, 2024

Doesn't work. That will always position the red box at the end. But I want it to always be next to the label independent of the labels width

EDIT: You're right. Thanks for pointing that out.

@RsZoli
Copy link
Author

RsZoli commented Oct 21, 2024

According to #10494 (comment) this is expected behaviour. It seems Auto is pretty much only usable for fixed width items and just save you from duplicating the size explicitly.

For your specific case I guess ,auto, should work, but there doesn't seem to be anything generic that performs like you would expect (basically HTML table layout logic that optimizes your grid to make the best use of available space)

Okay, but for me, the truncation is the bigger issue here! But still, a content shouldn't overflow its container, in my opinion!

@michiel-nwa
Copy link

@RsZoli sorry messed up my formatting, intended to write *,auto,* I think that also fixes the truncation. Just limits the two elements to be the same width. (which may look extremely weird if one of the strings is much shorter than the other)

@RsZoli
Copy link
Author

RsZoli commented Oct 21, 2024

@RsZoli sorry messed up my formatting, intended to write *,auto,* I think that also fixes the truncation. Just limits the two elements to be the same width. (which may look extremely weird if one of the strings is much shorter than the other)

Sadly, that doesnt look good for my case!

@jsuarezruiz jsuarezruiz removed the s/needs-attention Issue has more information and needs another look label Oct 30, 2024
@jsuarezruiz jsuarezruiz added this to the .NET 9 Servicing milestone Oct 30, 2024
@hartez
Copy link
Contributor

hartez commented Nov 13, 2024

This is not a bug, this is just what "Auto" in a Grid Column means. What you're expecting them to do (allocate the column space based on the measurement of all of the Labels in the row) can't happen for a couple reasons. Each cell of a Grid can have more than one item of content in it, and columns can span multiple rows. Accounting for those conditions means that automatic sizing based on flexible sizing of content is at best an expensive recursive process, and at worst impossible.

But all is not lost! .NET MAUI has a whole layout type dedicated to handling "flexible sizing of content": FlexLayout. To achieve the layout you want, you can do something like this:

<FlexLayout FlexLayout.Wrap="NoWrap"
			JustifyContent="Center">

	<Label Text="Left Side Text"
		   HorizontalTextAlignment="End"
		   FlexLayout.AlignSelf="Start"
		   BackgroundColor="Orange"
		   LineBreakMode="MiddleTruncation" />

	<Label FlexLayout.Shrink="0"
		   Text=" - "
		   BackgroundColor="Green" />

	<Label Text="Right Side Text"
		   HorizontalTextAlignment="Start"
		   FlexLayout.AlignSelf="End"
		   LineBreakMode="MiddleTruncation"
		   BackgroundColor="Magenta" />

</FlexLayout>

Doing that with several variations on the length of the text yields this:

Image

@RsZoli
Copy link
Author

RsZoli commented Nov 14, 2024

@hartez Thank you for your answer, i will definetly change my layout per your recommendation!

However my main problem is not the sizing of Grid cells rather than the trail truncation which doesn't happen when needed and happen when should not!

@hartez
Copy link
Contributor

hartez commented Nov 14, 2024

I don't know what to tell you, it's not a bug. Auto Columns are not going to produce truncated text the way you want; that's just not how they work. There's another Layout which does precisely what you're asking for. This issue should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter layout-grid platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants