Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

Support Clipping to rounded corners

Open danwalmsley opened this issue 5 years ago • 19 comments

If you have something like:

<Border Height="40" Background="Red" Width="100" BorderBrush="Green" BorderThickness="1" CornerRadius="5">
        <Border Width="40" Background="Blue" HorizontalAlignment="Right" />
      </Border>

the containing border will have round corners, but with the inner border overlaying the right hand corners.

WPF would also produce the same result, but UWP would correctly clip the corners like so. image

I think this would be an advantage to allow the UWP behaviour and would greatly help in polishing controls.

danwalmsley avatar Nov 14 '18 15:11 danwalmsley

Any updates on this? Is it currently possible at all to achieve this behavior, even with an ugly workaround?

frederik-hoeft avatar Nov 29 '20 18:11 frederik-hoeft

Doesn't ClipToBounds property fix it now?

maxkatz6 avatar Nov 29 '20 19:11 maxkatz6

the problem with cliptobounds is the background of the control in a border will still overlap the border. Would be nice if the border overlaps the child control to avoid this issue.

dlukach avatar Jul 28 '21 20:07 dlukach

cliptoboundsproblem ^ Here is a screenshot showing what happens when you add ClipToBounds="True" to a parent element. The child element still overlaps with the border.

davidackemandev avatar Jul 28 '21 20:07 davidackemandev

The GeometryClip feature is needed to support this

Gillibald avatar Jul 28 '21 21:07 Gillibald

Probably box-sizing from CSS solves similar problem in web. And not sure about UWP, but I think it's configurable there with BackgroundSizing. Can somebody say, who are familiar with them, if I am right about these properties?

Apart from that, I see only one good and not ugly solution - set corner radius also on child element. Still it might be not convenient. Also we can't just follow UWP approach without any way to configure it, because some applications might depend on that WPF-like behavior.

maxkatz6 avatar Jul 28 '21 21:07 maxkatz6

In CSS you can set overflow="hidden" to clip the child element correctly: codepen

I tried setting a corner radius on the child element, but it still isn't perfect, both parent and child have corner radius = 6: cornerproblem2

Does corner radius in Avalonia/WPF scale based on the element's width?

davidackemandev avatar Jul 29 '21 14:07 davidackemandev

Corner radius depends on the borders thickness. There are multiple radii that are calculated. For uniform corners and thicknesses this is directly calculated by the render backend (Skia by default)

Gillibald avatar Jul 29 '21 17:07 Gillibald

Some trick using opacity mask:

<Border BorderBrush="Black" Background="Red" BorderThickness="3" Margin="10" Width="400" Height="267" CornerRadius="40" >
    <Grid>
        <Border Name="myBorder" CornerRadius="40" Background="White" Margin="1"/>
        <Rectangle Fill="Red" >
            <Rectangle.OpacityMask>
                <VisualBrush Visual="{Binding ElementName=myBorder}"/>
            </Rectangle.OpacityMask>
        </Rectangle>
    </Grid>
</Border>

image

wieslawsoltes avatar Aug 06 '21 06:08 wieslawsoltes

@wieslawsoltes I tried to use your exact XAML, but I'm getting a tiny white space near the border:

Capture

nathanAjacobs avatar Aug 21 '21 23:08 nathanAjacobs

Problem here seems to be that although clipping to a rounded rect was added in #4081, it clips to the outer bounds of the rounded rect, not taking into account the border thickness.

UWP seems to handle this correctly, and given that we added this feature in part because UWP had it, we should match their behavior as it's more useful.

grokys avatar Jan 11 '22 15:01 grokys

Just ran across this one. It makes it appear that CornerRadius doesn't work at all when authoring controls.

robloo avatar Jan 24 '22 18:01 robloo

CornerRadius doesn't work at all when authoring controls.

Shouldn't be the case, this issue is not about that

maxkatz6 avatar Jan 24 '22 20:01 maxkatz6

Still an issue on Avalonia 0.10.12. Notice how when the ComboBoxItem gets it's color changed from transparent to a color, it overlays the parent border's corners: https://i.gyazo.com/9c8c3e28915f15ad15d59f7799d72473.mp4

JamDoggie avatar Feb 19 '22 18:02 JamDoggie

Again, ran across this for a new control. Children should always be clipped within their parents.

robloo avatar Mar 19 '22 21:03 robloo

Any update on this :/ ? Setting CornerRadius on ContentPresenter seems to correctly clip its child, but not on Border.

pr8x avatar Sep 27 '22 12:09 pr8x

I'm running into this too. It would be great if you have ClipToBounds=true set, the Border's Child would be automatically clipped to be exactly inside the visible BorderThickness.

billhenn avatar Nov 02 '22 00:11 billhenn