Skip to content

Commit 0c184c6

Browse files
committed
Add MaxLengthAttribute support to ListControlWidget
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
1 parent 8b1d58d commit 0c184c6

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

engine/Sandbox.Access/Rules/BaseAccess.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ internal static partial class Rules
214214
"System.ComponentModel.Annotations/System.ComponentModel.DataAnnotations.RequiredAttribute",
215215
"System.ComponentModel.Annotations/System.ComponentModel.DataAnnotations.RegularExpressionAttribute",
216216
"System.ComponentModel.Annotations/System.ComponentModel.DataAnnotations.RangeAttribute",
217+
"System.ComponentModel.Annotations/System.ComponentModel.DataAnnotations.MaxLengthAttribute",
217218

218219
"System.Private.CoreLib/System.EventArgs*",
219220
"System.Private.CoreLib/System.EventHandler*",

game/addons/tools/Code/Widgets/ControlWidgets/ListControlWidget.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Editor;
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Editor;
24

35
[CustomEditor( typeof( List<> ) )]
46
[CustomEditor( typeof( Array ) )]
@@ -133,6 +135,19 @@ public void Rebuild()
133135

134136
if ( !ShowAddButton )
135137
addButton.Visible = ShowAddButton;
138+
139+
if ( SerializedProperty.TryGetAttribute<MaxLengthAttribute>( out var maxLengthAttr ) )
140+
{
141+
var maxLength = maxLengthAttr.Length;
142+
if ( Collection is not null )
143+
{
144+
addButton.Enabled = Collection.Count() < maxLength;
145+
}
146+
else
147+
{
148+
addButton.Enabled = GetMultipleMin() < maxLength;
149+
}
150+
}
136151

137152
buttonRow.Add( addButton );
138153
buttonRow.AddStretchCell( 1 );

0 commit comments

Comments
 (0)