UITKEditorAid icon indicating copy to clipboard operation
UITKEditorAid copied to clipboard

Helper Suggestions

Open lgarczyn opened this issue 5 months ago • 0 comments

Here are a few helpers I've been using, might fit the collection

using UnityEngine;
using UnityEngine.UIElements;

public static class UIToolkitExtensions
{
    public static void RegisterFocus<T>(this T focusable)
        where T: Focusable, IFocusable
    {
        focusable.RegisterCallback<BlurEvent>(evt =>
        {
            T target = (T)evt.target;
            target.SetFocus(false);
        });
        focusable.RegisterCallback<FocusEvent>(evt =>
        {
            T target = (T)evt.target;
            target.SetFocus(true);
        });
    }

    // Prevents accidentally writing '\t' instead of navigating when inside a TextField
    public static void BlockTabKeyEvent(this CallbackEventHandler element)
    {
        element.RegisterCallback<KeyDownEvent>(evt => {
            if (evt.keyCode == KeyCode.Tab || evt.character == '\t')
            {
                // Prevent write
                evt.PreventDefault();
                // re-emit
                using KeyDownEvent pooled = KeyDownEvent.GetPooled('\t', evt.keyCode, evt.modifiers);

                pooled.target = ((TextField)evt.target).parent;
                pooled.target.SendEvent(pooled);
            }

        }, TrickleDown.TrickleDown);
    }

    public static void ClearBorder(this IStyle style)
    {
        style.SetBorder(StyleKeyword.Null, 0);
    }

    public static void SetBorder(this IStyle style, StyleColor color, float width = 1)
    {
        style.borderBottomColor = color;
        style.borderTopColor = color;
        style.borderLeftColor = color;
        style.borderRightColor = color;
        style.borderBottomWidth = width;
        style.borderTopWidth = width;
        style.borderLeftWidth = width;
        style.borderRightWidth = width;
    }
}

lgarczyn avatar Oct 24 '25 14:10 lgarczyn