using System;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
namespace io.github.hatayama.uLoopMCP
{
///
/// Utility for binding UI Toolkit elements to data without triggering change events.
/// SetValueWithoutNotify prevents infinite loops when updating UI from model changes.
///
public static class ViewDataBinder
{
public static void BindToggle(Toggle toggle, Func getter, Action onChanged)
{
toggle.SetValueWithoutNotify(getter());
toggle.RegisterValueChangedCallback(evt => onChanged(evt.newValue));
}
public static void BindIntegerField(IntegerField field, Func getter, Action onChanged)
{
field.SetValueWithoutNotify(getter());
field.RegisterValueChangedCallback(evt => onChanged(evt.newValue));
}
public static void BindEnumField(EnumField field, Func getter, Action onChanged) where T : Enum
{
field.Init(getter());
field.RegisterValueChangedCallback(evt =>
{
if (evt.newValue is T newValue)
{
onChanged(newValue);
}
});
}
public static void BindFoldout(Foldout foldout, Func getter, Action onChanged)
{
foldout.SetValueWithoutNotify(getter());
foldout.RegisterValueChangedCallback(evt => onChanged(evt.newValue));
}
public static void BindButton(Button button, Action onClick)
{
button.clicked += onClick;
}
public static void BindLabel(Label label, Action