uno: [Wasm] When Css Style "clip-path" applied to Canvas, Canvas receive wrong Pointer Event.

Current behavior

Consider below example, GreenCanvas inside the RedCanvas, and I set css style “clip-path” on GreenCanvas : GreenCanvas.SetCssStyle(“clip-path”, “path(‘M300,0 500,50 300,100z’)”);

When pointer moving on red area, GreenCanvas wrongly received the pointer move events.

This error start from Uno.UI/ Uno.UI.WebAssembly version 5.4.0-dev.1485 . before this version it’s correct.

image

        <Canvas Width="500" Height="100" Background="Red" x:Name="RedCanvas" PointerMoved="Canvas_PointerMoved" >
            <Canvas  Width="500" Height="100" Background="Green" x:Name="GreenCanvas" PointerMoved="Canvas_PointerMoved"> </Canvas>
        </Canvas>
        public TestPanel()
        {
            this.InitializeComponent();
            GreenCanvas.SetCssStyle("clip-path", "path('M300,0 500,50 300,100z')");
        }

        private void Canvas_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            Console.WriteLine((sender as Canvas).Name + " moved");
            e.Handled = true;
        }

Thank you!

Expected behavior

No response

How to reproduce it (as minimally and precisely as possible)

No response

Workaround

No response

Works on UWP/WinUI

None

Environment

Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia

NuGet package version(s)

No response

Affected platforms

WebAssembly

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (9 by maintainers)

Most upvoted comments

You can implement the same for “NativePointerPressed” via pointerdown and “NativePointerReleased” via pointerup following the click example. For NativePointerMoved that would be pointerover, but you would likely have to pass back the pointer x,y coords & possibly need to remap it into winui coords space.

If you need further assistance in these areas, please share with us what you intent with do with these events (some examples or existing pointer handlers), so we can better update the sample.

So we did some major update regarding how the pointer are handled in wasm back in 5.4 (#17633). Instead of handling pointer events from every single view, we are now handling them centrally at document.body level with uno taking care of dispatching, rather than the browser. Now of course there are some discrepancies, as you have noticed. However, this is not something that can be easily reverted as it addresses others problem, and we can neither make it a toggle.

That said, we can compromise with what we already have or take the best of both worlds. If the end goal was to clip-path an <Image>, while having its hitbox matches the clipped shape, we can combine your approach and the workaround proposed above. The idea is to clip-path the image (or its container) and make them not hit-visible, and then superpose a path with the same clip as the hitbox for the pointer events. (Here, unlike clip-path which uno are blind to, the Path.Data is something known by us, so we can dispatch the event properly.)

ClippableImage.cs
using Microsoft.UI.Xaml.Input;
using Path = Microsoft.UI.Xaml.Shapes.Path;

namespace u18458;

#if __WASM__

[TemplatePart(Name = TemplateParts.InnerImage, Type = typeof(Image))]
[TemplatePart(Name = TemplateParts.HitBoxPath, Type = typeof(Path))]
public partial class ClippableImage : Control
{
	private static class TemplateParts
	{
		public const string InnerImage = nameof(InnerImage);
		public const string HitBoxPath = nameof(HitBoxPath);
	}

	private Image? _image;
	private Path? _hitbox;

	public event PointerEventHandler? ClippedPointerEntered;
	public event PointerEventHandler? ClippedPointerExited;
	public event PointerEventHandler? ClippedPointerMoved;
	public event PointerEventHandler? ClippedPointerPressed;

	#region DependencyProperty: Source

	public static DependencyProperty SourceProperty { get; } = DependencyProperty.Register(
		nameof(Source),
		typeof(ImageSource),
		typeof(ClippableImage),
		new PropertyMetadata(default(ImageSource)));

	public ImageSource? Source
	{
		get => (ImageSource?)GetValue(SourceProperty);
		set => SetValue(SourceProperty, value);
	}

	#endregion
	#region DependencyProperty: ClipPath

	public static DependencyProperty ClipPathProperty { get; } = DependencyProperty.Register(
		nameof(ClipPath),
		typeof(string),
		typeof(ClippableImage),
		new PropertyMetadata(default(string), OnClipPathChanged));

	public string? ClipPath
	{
		get => (string?)GetValue(ClipPathProperty);
		set => SetValue(ClipPathProperty, value);
	}

	#endregion

	public ClippableImage()
	{
		this.Loaded += (s, e) => UpdateClip();
	}
	protected override void OnApplyTemplate()
	{
		base.OnApplyTemplate();

		_image = GetTemplateChild(TemplateParts.InnerImage) as Image;
		_hitbox = GetTemplateChild(TemplateParts.HitBoxPath) as Path;
		if (_hitbox is { })
		{
			_hitbox.PointerMoved += (s, e) => ClippedPointerEntered?.Invoke(this, e);
			_hitbox.PointerExited += (s, e) => ClippedPointerExited?.Invoke(this, e);
			_hitbox.PointerMoved += (s, e) => ClippedPointerMoved?.Invoke(this, e);
			_hitbox.PointerPressed += (s, e) => ClippedPointerPressed?.Invoke(this, e);
		}

		UpdateClip();
	}

	private static void OnClipPathChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) => (sender as ClippableImage)?.UpdateClip();

	private void UpdateClip()
	{
		if (!IsLoaded) return;

		if (!string.IsNullOrEmpty(ClipPath))
		{
			this.SetCssStyle("clip-path", $"path('{ClipPath}')");
		}
		else
		{
			this.ClearCssStyle("clip-path");
		}

		if (_hitbox is { })
		{
			_hitbox.Data = ClipPath;
		}
	}
}
#endif
ClippableImage.xaml

make sure to reference this in your app.xaml directly or indirectly

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
					xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
					xmlns:local="using:u18458">

	<Style TargetType="local:ClippableImage">
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="local:ClippableImage">
					<Grid Background="{x:Null}">
						<Image x:Name="InnerImage" Source="{TemplateBinding Source}" IsHitTestVisible="False" />
						<Path x:Name="HitBoxPath" Fill="Transparent" />
					</Grid>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>

</ResourceDictionary>

usage:

<Canvas x:Name="RedCanvas2"
		Background="Red"
		Width="500"
		Height="100"
		PointerMoved="Canvas_PointerMoved">
	<local:ClippableImage Width="500"
						  Height="100"
						  Source="https://placehold.co/500x100/orange/white"
						  ClipPath="M300,0 500,50 300,100z"
						  ClippedPointerMoved="Canvas_PointerMoved" />
</Canvas>

wasm clippedimage

full solution: u18458.zip

@easy28 unfortunately I doubt switching between managed and native pointers is possible, as it is a completely different infrastructure involved (am I correct @dr1rrb?)

@easy28 IMO, it’s in general not a good idea to alter “clip-path” yourself. For example, if we implemented https://github.com/unoplatform/uno/issues/13782, then our own layout clipping will be competing on the same property that you are trying to set (though it won’t happen for Canvas specifically as it’s never layout clipped by us).

I think a better way to achieve the same rendering would be something like this:

<Canvas Width="500" Height="100" Background="Red" x:Name="RedCanvas">
    <Path Width="500" Height="100" Fill="Green" x:Name="GreenPath" Data="M 300,0 500,50 300,100z" />
</Canvas>

Can you try that and let us know if pointers don’t work properly with that code?

Note: the above will also work on all platforms, while what you have will only work on Wasm

I’m pretty sure that moving WASM pointers to managed means that any native clipping won’t affect hit-testing at all (since we do our own hit tests), but I didn’t work on that, so I’m not sure. Maybe @dr1rrb can chime in?