using System.ComponentModel; using System.Reflection; using MyIA.AI.ComponentModel.Attributes; namespace MyIA.AI.ComponentModel.PropertyEditor.Explorer; /// /// Builds an from a type by reflecting over the T2 attribute /// vocabulary — the renderer-agnostic core of the Explorer pattern (EPIC #7265, A3-T3). /// /// /// Placement semantics (modernized reading of the VB AriciePropertyEditorControl /// category slotting, which the obsolete DefaultCategoryAttribute delegated to /// MainCategoryAttribute): a member with [ExtendedCategory] lands in its /// declared tab/section/column; a member without one lands in the type-level /// [MainCategory] tab when present, else in "(General)". Tabs, sections and fields /// keep first-appearance (declaration) order — the VB comparer /// (AriciePropertySortOrderComparer) lived in the control layer and is deliberately /// not ported; declaration order is the deterministic renderer-agnostic default. /// public static class EditorModelBuilder { /// Fallback tab name for members with no placement metadata at all. public const string DefaultTabName = "(General)"; /// Builds the full tab/section/field model for . public static EditorModel Build() => Build(typeof(T)); /// Builds the full tab/section/field model for a type. public static EditorModel Build(Type editedType) { if (editedType is null) { throw new ArgumentNullException(nameof(editedType)); } var mainCategory = editedType.GetCustomAttribute(); var fallbackTab = mainCategory is null ? DefaultTabName : mainCategory.Name; var tabs = new Dictionary>(StringComparer.Ordinal); var sectionFields = new Dictionary>(ReferenceEqualityComparer.Instance); var sectionKeys = new Dictionary<(string Tab, string Section, int Column), PropertySection>(); var order = 0; foreach (var property in editedType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { var field = BuildField(property, order++); var category = field.Category; var tabName = category is null ? fallbackTab : string.IsNullOrEmpty(category.TabName) ? fallbackTab : category.TabName; var sectionName = category?.SectionName ?? string.Empty; var column = category?.Column ?? 0; if (!tabs.TryGetValue(tabName, out var sections)) { sections = new List(); tabs.Add(tabName, sections); } var key = (tabName, sectionName, column); if (!sectionKeys.TryGetValue(key, out var section)) { section = new PropertySection(sectionName, column, Array.Empty()); sectionKeys.Add(key, section); sections.Add(section); sectionFields.Add(section, new List()); } sectionFields[section].Add(field); } var builtTabs = tabs.Select(pair => new PropertyTab( pair.Key, pair.Value.Select(s => new PropertySection(s.Name, s.Column, sectionFields[s])).ToList())) .ToList(); return new EditorModel(editedType, builtTabs); } private static EditorField BuildField(PropertyInfo property, int order) { ExtendedCategory? category = property.GetCustomAttribute()?.ExtendedCategory; PropertyEditorMode? mode = null; var single = property.GetCustomAttribute(); if (single is not null) { mode = single.EditorMode; } var visibility = ConditionalVisibleInfo.FromMember(property) ?? new List(); int? width = property.GetCustomAttribute() is { } w ? w.Width : null; int? size = property.GetCustomAttribute() is { } s ? s.Size : null; int? lines = null; bool? autoResize = null; if (property.GetCustomAttribute() is { } lc) { lines = lc.Lines; autoResize = lc.AutoResize; } var selector = property.GetCustomAttribute()?.SelectorInfo; var textFieldName = property.GetCustomAttribute() is { } tf ? tf.TextField : null; var valueFieldName = property.GetCustomAttribute() is { } vf ? vf.ValueField : null; // [InnerEditor] aggregates standard EditorAttribute bindings; surface consumers // resolve the qualified editor type names without reflecting again. var editorTypeNames = property.GetCustomAttribute() is { } inner ? inner.GetAttributes(property.PropertyType) .OfType() .Select(e => e.EditorTypeName) .Where(n => !string.IsNullOrEmpty(n)) .ToList() : (IReadOnlyList)Array.Empty(); var collectionOptions = property.GetCustomAttribute(); MultiSelectionType? multiSelection = property.GetCustomAttribute() is { } ms ? ms.SelectionType : null; return new EditorField( property.Name, property.PropertyType, order, property.GetCustomAttribute() is not null, category, mode, visibility, width, size, lines, autoResize, property.GetCustomAttribute(), selector, textFieldName, valueFieldName, editorTypeNames, collectionOptions is not null, collectionOptions, multiSelection, property.GetCustomAttribute() is not null, property.GetCustomAttribute() is { } od ? od.Enabled : null, property.GetCustomAttribute() is not null, property.GetCustomAttribute() is not null, property.GetCustomAttribute() is not null); } }