using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using RosettaUI.UndoSystem; namespace RosettaUI { [SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Global")] public struct ListViewOption { public static ListViewOption Default => new (true); public static ListViewOptionOfType OfType(IList _) { return new ListViewOptionOfType(Default); } public Func createItemElementFunc; public Func createItemInstanceFunc; public Func createItemRestoreRecordFunc; public bool reorderable; public bool fixedSize; public bool header; public bool suppressAutoIndent; public ListViewOption(bool reorderable, bool fixedSize = false, bool header = true, bool suppressAutoIndent = false) { createItemElementFunc = null; createItemInstanceFunc = null; createItemRestoreRecordFunc = null; this.reorderable = reorderable; this.fixedSize = fixedSize; this.header = header; this.suppressAutoIndent = suppressAutoIndent; } public ListViewOption(ListViewOption other) { createItemElementFunc = other.createItemElementFunc; createItemInstanceFunc = other.createItemInstanceFunc; createItemRestoreRecordFunc = other.createItemRestoreRecordFunc; reorderable = other.reorderable; fixedSize = other.fixedSize; header = other.header; suppressAutoIndent = other.suppressAutoIndent; } } public static class ListViewOptionExtensions { public static ListViewOptionOfType OfType(this ListViewOption option, IList _) { return new ListViewOptionOfType(option); } } /// /// Wrapper class for ListViewOption specifying the item type of the list /// ListViewOption is type-independent, so use it to guarantee type safety /// public struct ListViewOptionOfType { public static implicit operator ListViewOption (ListViewOptionOfType optionOfType) { return optionOfType._option; } private ListViewOption _option; public ListViewOptionOfType(ListViewOption option) { _option = option; } public ListViewOptionOfType SetCreateItemInstanceFunc(Func, int, TItem> func) { _option.createItemInstanceFunc = func == null ? null : (list, _, index) => func((IReadOnlyList)list, index); return this; } public ListViewOptionOfType SetCreateItemRestoreRecordFunc(Func> func) { _option.createItemRestoreRecordFunc = func == null ? null : obj => func((TItem)obj); return this; } public ListViewOptionOfType SetItemRestoreFunc(Func> func) { return SetCreateItemRestoreRecordFunc(item => new ObjectRestoreRecord(func(item))); } } }