using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace RosettaUI { public static partial class UI { #region string interface public static DropdownElement Dropdown(Expression> targetExpression, IEnumerable options) => Dropdown(ExpressionUtility.CreateLabelString(targetExpression), targetExpression, options); public static DropdownElement Dropdown(LabelElement label, Expression> targetExpression, IEnumerable options) => Dropdown(label, UIInternalUtility.CreateBinder(targetExpression), options); public static DropdownElement Dropdown(Expression> targetExpression, Action writeValue, IEnumerable options) => Dropdown(ExpressionUtility.CreateLabelString(targetExpression) , targetExpression.Compile(), writeValue, options); public static DropdownElement Dropdown(LabelElement label, Func readValue, Action writeValue, IEnumerable options) => Dropdown(label, Binder.Create(readValue, writeValue), options); public static DropdownElement DropdownReadOnly(Expression> targetExpression, IEnumerable options) => Dropdown(ExpressionUtility.CreateLabelString(targetExpression), UIInternalUtility.CreateReadOnlyBinder(targetExpression), options); public static DropdownElement DropdownReadOnly(LabelElement label, Func readValue, IEnumerable options) => Dropdown(label, Binder.Create(readValue, null), options); public static DropdownElement Dropdown(LabelElement label, IBinder binder, IEnumerable options) { var optionList = options.ToList(); var intBinder = Binder.Create( () => optionList.IndexOf(binder.Get()), binder.IsReadOnly ? null : i => binder.Set(optionList[i]) ); return Dropdown(label, intBinder, optionList); } #endregion #region int interface public static DropdownElement Dropdown(Expression> targetExpression, IEnumerable options) => Dropdown(ExpressionUtility.CreateLabelString(targetExpression), targetExpression, options); public static DropdownElement Dropdown(LabelElement label, Expression> targetExpression, IEnumerable options) => Dropdown(label, UIInternalUtility.CreateBinder(targetExpression), options); public static DropdownElement Dropdown(Expression> targetExpression, Action writeValue, IEnumerable options) => Dropdown(ExpressionUtility.CreateLabelString(targetExpression) , targetExpression.Compile(), writeValue, options); public static DropdownElement Dropdown(LabelElement label, Func readValue, Action writeValue, IEnumerable options) => Dropdown(label, Binder.Create(readValue, writeValue), options); public static DropdownElement DropdownReadOnly(Expression> targetExpression, IEnumerable options) => Dropdown(ExpressionUtility.CreateLabelString(targetExpression), UIInternalUtility.CreateReadOnlyBinder(targetExpression), options); public static DropdownElement DropdownReadOnly(LabelElement label, Func readValue, IEnumerable options) => Dropdown(label, Binder.Create(readValue, null), options); public static DropdownElement Dropdown(LabelElement label, IBinder binder, IEnumerable options) { var element = new DropdownElement(label, binder, options); UIInternalUtility.SetInteractableWithBinder(element, binder); return element; } #endregion } }