using RosettaUI.UndoSystem;
namespace RosettaUI
{
///
/// 値を持ち外部と同期するElement
///
public abstract class FieldBaseElement : ReadOnlyFieldElement, IUndoRestoreElement
{
public FieldOption Option { get; }
protected virtual bool ShouldRecordUndo => true;
private readonly IBinder _binder;
protected FieldBaseElement(LabelElement label, IBinder binder, in FieldOption option = default) : base(label, binder)
{
_binder = binder;
Option = option;
Interactable = !binder.IsReadOnly;
}
#region IUndoRestoreElement
public IElementRestoreRecord CreateRestoreRecord() => FieldBaseElementRestoreRecord.Create(this);
#endregion
protected override ElementViewBridge CreateViewBridge() => new FieldViewBridgeBase(this);
public class FieldViewBridgeBase : ReadOnlyValueViewBridgeBase
{
private FieldBaseElement Element => (FieldBaseElement)element;
public FieldViewBridgeBase(FieldBaseElement element) : base(element)
{
}
public void SetValueFromView(T value)
{
if (Element.ShouldRecordUndo)
{
var before = Element.Value;
Undo.RecordFieldBaseElement(Element, before, value);
}
Element._binder?.Set(value);
Element.NotifyViewValueChanged();
}
}
}
public static partial class ElementViewBridgeExtensions
{
public static FieldBaseElement.FieldViewBridgeBase GetViewBridge(this FieldBaseElement element) => (FieldBaseElement.FieldViewBridgeBase)element.ViewBridge;
}
}