# Shared pointer gestures

Build a slider-like surface that keeps following a drag when the pointer leaves its track. Press anywhere on the track, move horizontally, and release. A second contact cannot take over the first.

<div class="docs-demo-layout">

```csharp
using System.Globalization;
using Xui.Core.Canvas;
using Xui.Core.Math2D;
using Xui.Core.UI;
using Xui.Core.UI.Input;
using NFloat = System.Runtime.InteropServices.NFloat;

public sealed class DragPad : View
{
    private int? pointer;
    private NFloat fraction = 0.5f;
    private int displayedPercent = 50;
    private string valueLabel = "50%";
    public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
    {
        if (phase != EventPhase.Bubble)
            return;
        if (e.Type == PointerEventType.Down && pointer is null && e.State.Button == PointerButton.Left)
        {
            pointer = e.PointerId;
            CapturePointer(e.PointerId, PointerGestures.Drag);
        }

        if (pointer != e.PointerId)
            return;
        if (e.Type is PointerEventType.Down or PointerEventType.Move or PointerEventType.Up)
        {
            // Routing already maps this point through ancestor transforms.
            fraction = NFloat.Clamp((e.State.Position.X - Frame.X - 32) / NFloat.Max(1, Frame.Width - 64), 0, 1);
            var percent = (int)(fraction * 100);
            if (percent != displayedPercent)
            {
                displayedPercent = percent;
                valueLabel = percent.ToString(CultureInfo.InvariantCulture) + "%";
            }

            InvalidateRender();
        }

        if (e.Type is PointerEventType.Up or PointerEventType.Cancel or PointerEventType.LostCapture)
        {
            pointer = null;
            ReleasePointer(e.PointerId);
            InvalidateRender();
        }
    }

    protected override void OnDeactivate()
    {
        var old = pointer;
        pointer = null;
        if (old is { } id)
            ReleasePointer(id);
        base.OnDeactivate();
    }

    protected override void RenderCore(IContext context)
    {
        context.Save();
        try
        {
            context.SetFill(new Color(0xFAF7FFFF));
            context.FillRect(Frame);
            var y = Frame.Y + Frame.Height / 2;
            var x = Frame.X + 32 + fraction * (Frame.Width - 64);
            context.SetFill(new Color(0xD9B6F4FF));
            context.FillRect(new Rect(Frame.X + 32, y - 4, Frame.Width - 64, 8));
            context.SetFill(new Color(pointer is null ? 0x8538CEFFu : 0xD957A8FFu));
            context.BeginPath();
            context.Arc((x, y), 24, 0, NFloat.Pi * 2);
            context.Fill();
            context.SetFont(new Font(18, "Inter"));
            context.TextAlign = TextAlign.Center;
            context.TextBaseline = TextBaseline.Middle;
            context.FillText(valueLabel, (Frame.X + Frame.Width / 2, y - 64));
            context.SetFont(new Font(14, "Inter"));
            context.FillText("Drag across the track", (Frame.X + Frame.Width / 2, y + 72));
        }
        finally
        {
            context.Restore();
        }
    }
}
```

<figure class="docs-demo" data-demo-url="../../../wasm/?embed=1&amp;demo=input/drag-pad">
<div class="docs-demo-stage">
<img src="input-drag-pad.svg" width="400" height="600" alt="Drag pad: initial rendered view" />
<button type="button" class="docs-demo-start" hidden><span class="docs-demo-pill">Start demo</span></button>
</div>
<div class="docs-demo-actions">
<button type="button" class="docs-demo-close" hidden>Close demo</button>
<span class="docs-demo-status" role="status" aria-live="polite"></span>
</div>
<figcaption>Drag pad · <a href="input-drag-pad.cs">C# source</a> · <a href="input-drag-pad.svg">SVG</a> · <a href="../../../wasm/?embed=1&amp;demo=input/drag-pad">Open demo</a></figcaption>
</figure>

</div>


## Capture the sequence

`pointer` starts empty. A primary-button `Down` stores the ID and calls `CapturePointer` with `PointerGestures.Drag`. Later events must match that ID before they can change the value. The fraction stays between zero and one even when capture delivers a point outside the view.

The example subtracts `Frame.X` and the track's inset from the routed point. It then divides by the usable track width. Rendering applies the inverse relationship to place the thumb. Keep input and drawing geometry together like this; independent magic numbers eventually drift apart.

On release, clear state before releasing capture because capture changes can synchronously deliver `LostCapture`. Cancellation and lost capture clear the state too. Deactivation releases an unfinished sequence so the old page cannot retain the interaction.

The value label is formatted only when its displayed percentage changes. `RenderCore` reuses that string, so drawing another frame does not allocate another label. The explicit string also avoids the current browser issue with the Canvas interpolated-string overload.

## Tell containers what you recognize

| Marker | Meaning for cooperating containers |
| --- | --- |
| `PointerGestures.Tap` | A possible tap; an ancestor drag may steal it after movement crosses a threshold. |
| `PointerGestures.Drag` | A committed drag; ancestor scroll views should stand down. |
| `DragHorizontalTentative` | A horizontal candidate that can yield to vertical movement before commitment. |
| `DragVerticalTentative` | A vertical candidate that can yield to horizontal movement before commitment. |

These markers describe the gesture; they do not recognize it for you. Your control still decides when motion crosses its threshold and when to replace a tentative capture with `Drag`.

During `Down`/`Tunnel`, an ancestor can advertise a potential gesture with `PushPotentialGesture`. Descendants read `PotentialGestures` to adjust press feedback while recognition remains pending. The span belongs to that dispatch and must not be stored.

## Choose the right interaction

This example commits immediately, which suits a dedicated drag surface. For a horizontal slider inside a vertical scroller, use tentative horizontal recognition so an unmistakably vertical swipe can scroll the parent. For a button, commit the click only after a valid release inside its bounds; a press or cancelled sequence is not a click.

The demo concentrates on pointer ownership. A production slider also needs keyboard adjustments, visible focus, and a meaningful label. See [Focus and keyboard navigation](../../text-input/focus/index.md) for the next part.
