Add MaxLengthAttribute support to ListControlWidget (#3469)

ListControlWidget now respects the MaxLengthAttribute by disabling the add button when the collection reaches the specified maximum length. Also updated access rules to include MaxLengthAttribute.

Implements https://github.com/Facepunch/sbox-issues/issues/6361

Co-authored-by: Braxen <braxen@braxnet.org>
This commit is contained in:
sboxbot
2025-11-27 08:37:33 +00:00
committed by GitHub
parent 8b1d58d524
commit 00f4be8242
2 changed files with 17 additions and 1 deletions

View File

@@ -214,6 +214,7 @@ internal static partial class Rules
"System.ComponentModel.Annotations/System.ComponentModel.DataAnnotations.RequiredAttribute",
"System.ComponentModel.Annotations/System.ComponentModel.DataAnnotations.RegularExpressionAttribute",
"System.ComponentModel.Annotations/System.ComponentModel.DataAnnotations.RangeAttribute",
"System.ComponentModel.Annotations/System.ComponentModel.DataAnnotations.MaxLengthAttribute",
"System.Private.CoreLib/System.EventArgs*",
"System.Private.CoreLib/System.EventHandler*",

View File

@@ -1,4 +1,6 @@
namespace Editor;
using System.ComponentModel.DataAnnotations;
namespace Editor;
[CustomEditor( typeof( List<> ) )]
[CustomEditor( typeof( Array ) )]
@@ -134,6 +136,19 @@ public class ListControlWidget : ControlWidget
if ( !ShowAddButton )
addButton.Visible = ShowAddButton;
if ( SerializedProperty.TryGetAttribute<MaxLengthAttribute>( out var maxLengthAttr ) )
{
var maxLength = maxLengthAttr.Length;
if ( Collection is not null )
{
addButton.Enabled = Collection.Count() < maxLength;
}
else
{
addButton.Enabled = GetMultipleMin() < maxLength;
}
}
buttonRow.Add( addButton );
buttonRow.AddStretchCell( 1 );
}