Build with XuiView Markdown

Mouse interaction on desktop

Make a surface respond to hover without making hover necessary. The pointer pad changes its background under a mouse and draws a circle while a contact is pressed. A touch user can still press and drag without needing the hover state.

using System.Collections.Generic;
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 PointerPad : View
{
    private readonly Dictionary<int, Point> contacts = new(10);
    private bool hovering;
    private int displayedCount;
    private string countLabel = "Active contacts: 0";
    public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
    {
        if (e.State.PointerType == PointerType.Mouse)
        {
            if (e.Type == PointerEventType.Enter)
            {
                hovering = true;
                InvalidateRender();
            }

            if (e.Type == PointerEventType.Leave)
            {
                hovering = false;
                InvalidateRender();
            }
        }

        if (phase != EventPhase.Bubble)
            return;
        if (e.Type == PointerEventType.Down)
        {
            contacts[e.PointerId] = e.State.Position;
            CapturePointer(e.PointerId, PointerGestures.Drag);
        }
        else if (e.Type == PointerEventType.Move && contacts.ContainsKey(e.PointerId))
            contacts[e.PointerId] = e.State.Position;
        else if (e.Type is PointerEventType.Up or PointerEventType.Cancel or PointerEventType.LostCapture)
        {
            contacts.Remove(e.PointerId);
            ReleasePointer(e.PointerId);
        }

        UpdateCountLabel();
        InvalidateRender();
    }

    protected override void OnDeactivate()
    {
        // Release one at a time: LostCapture may synchronously remove the entry.
        while (contacts.Count > 0)
        {
            using var iterator = contacts.Keys.GetEnumerator();
            iterator.MoveNext();
            var id = iterator.Current;
            contacts.Remove(id);
            ReleasePointer(id);
        }

        hovering = false;
        UpdateCountLabel();
        base.OnDeactivate();
    }

    private void UpdateCountLabel()
    {
        if (contacts.Count == displayedCount)
            return;
        displayedCount = contacts.Count;
        countLabel = "Active contacts: " + displayedCount.ToString(CultureInfo.InvariantCulture);
    }

    protected override void RenderCore(IContext context)
    {
        context.Save();
        try
        {
            context.SetFill(new Color(hovering ? 0xEEE2FAFFu : 0xFAF7FFFFu));
            context.FillRect(Frame);
            context.SetFill(new Color(0x8538CEFF));
            foreach (var contact in contacts.Values)
            {
                context.BeginPath();
                context.Arc(contact, 32, 0, NFloat.Pi * 2);
                context.Fill();
            }

            context.SetFont(new Font(18, "Inter"));
            context.TextAlign = TextAlign.Center;
            context.TextBaseline = TextBaseline.Middle;
            context.FillText(countLabel, (Frame.X + Frame.Width / 2, Frame.Y + 64));
            context.SetFont(new Font(14, "Inter"));
            context.FillText("Press, drag, then release", (Frame.X + Frame.Width / 2, Frame.Bottom - 64));
        }
        finally
        {
            context.Restore();
        }
    }
}
Pointer pad: initial rendered view
Pointer pad · C# source · SVG · Open demo

Separate hover from pressing

Enter and Leave change presentation. Down, Move, and Up describe the action. The example checks PointerType.Mouse before showing hover feedback; touch can generate enter/leave-related notifications as contacts appear, but that is not a persistent mouse hover.

The current router emits enter/leave transitions for the changed target; do not assume a complete DOM-style enter/leave sequence for every ancestor. Keep hover behavior local to the hit target and clear it when your view deactivates.

Recognize the intended button

State.Button identifies the button associated with an event. A primary-action control should check PointerButton.Left on Down; a context action can explicitly choose Right. The pad intentionally visualizes any received press. Do not copy that permissive behavior into a destructive action.

Do not infer a drag solely from State.Buttons on Move. Some native event adapters synthesize move states without a pressed-button mask. Remember the active pointer ID from Down, capture it, and stop on Up, Cancel, or LostCapture.

Pressure is optional. Only treat State.Pressure as a measured value when HasPressure is true; a synthesized pressure of one can merely mean “button pressed.” Do not require pressure or a persistent hardware ID for ordinary interaction.

Keep the action reachable

Hover should reveal feedback, not hide the only way to discover or invoke an action. Supply a visible label or affordance, and add focus plus keyboard behavior for custom controls. A View does not become keyboard-interactive merely because it handles mouse input.

Try moving across the pad, pressing, leaving its bounds while still pressed, and releasing. Capture keeps the active sequence attached to the pad. For a real slider that tracks one primary contact and ignores additional fingers, continue with Shared pointer gestures.