AvaloniaUI / avalonia-docs

https://docs.avaloniaui.net/docs/welcome
64 stars 212 forks source link

TextTrimming - Trim at start #283

Open Symbai opened 11 months ago

Symbai commented 11 months ago

Is your feature request related to a problem? Please describe.

Sometimes its more useful to trim a text at the beginning instead of end.

Describe the solution you'd like

Be able to trim at the beginning. Either by extending the existing TextTrimming enum or by adding a new property

enum TextTrimLocation
{
    End = 0,
    Start = 1,
    Middle = 2 //not requested but in this case to complete the list
}

Describe alternatives you've considered

Using a custom converter

Additional context

timunie commented 11 months ago

Isn't it supported already? https://github.com/AvaloniaUI/Avalonia/pull/7322

timunie commented 11 months ago

works already

<TextBlock Text="Hello world from long text" MaxWidth="100">
    <TextBlock.TextTrimming>
        <TextLeadingPrefixTrimming>
            <x:Arguments>
                <system:String> ▲ ▲ ▲ </system:String>
                <system:Int32>3</system:Int32>
            </x:Arguments>
        </TextLeadingPrefixTrimming>
    </TextBlock.TextTrimming>
</TextBlock>

image

Symbai commented 11 months ago

Jesus, this looks so "over complicated ". Instead of a single line we have now 8 lines to write... for EVERY single textblock. Also in your example it trims at the middle of text. How to trim at the beginning? I dont even understand how this works. Is there any documentation?!

jp2masa commented 11 months ago

Jesus, this looks so "over complicated ". Instead of a single line we have now 8 lines to write... for EVERY single textblock.

I think you can define a resource and then use it: <TextBlock TextTrimming="{StaticResource MyTrimming}" />

Also in your example it trims at the middle of text. How to trim at the beginning?

I think the second argument corresponds to the length of the prefix to keep, so you would set it to 0 instead of 3.

Not sure if there are docs for this yet. You can see the docs in this class:

https://github.com/AvaloniaUI/Avalonia/blob/73f7fd1d3eed0a9cb0bb7757a1ee1b728f68af6a/src/Avalonia.Base/Media/TextTrimming.cs

You're probably looking for this:

https://github.com/AvaloniaUI/Avalonia/blob/73f7fd1d3eed0a9cb0bb7757a1ee1b728f68af6a/src/Avalonia.Base/Media/TextTrimming.cs#L33-L36

So I guess you can do <TextBlock TextTrimming="{x:Static TextTrimming.LeadingCharacterEllipsis}" />

timunie commented 11 months ago

@Symbai that was only one possible syntax if you need trimming in middle of the string and want pure XAML.

other working solutions:

... We may want to extend docs here, so I transferred this issue.

Symbai commented 11 months ago

So I guess you can do <TextBlock TextTrimming="{x:Static TextTrimming.LeadingCharacterEllipsis}" />

That is pretty cool, thank you. I hope the docs will show this example. While its cool that you can do crazy stuff I think many people are only interesting in basic scenarios and prefer short and clean solutions.

timunie commented 11 months ago

<TextBlock TextTrimming="LeadingCharacterEllipsis" /> can you try if this also works?

Gillibald commented 11 months ago

Looks like we did not add these new trimming modes to the parse method: https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/Media/TextTrimming.cs#L49

timunie commented 11 months ago

@Gillibald should I create a tracking issue for this? Would there be an option to make parameters work like LeadingCharacterEllipsis('...',3) which already works for scale(1.5)?