@@ -183,6 +183,7 @@ internal sealed class VerbPanel : InfoPanel {
183183 private readonly ClientVerbSystem ? _verbSystem ;
184184
185185 private readonly VerbPanelGrid _grid ;
186+ private readonly Dictionary < ( int VerbId , ClientObjectReference Src ) , Button > _verbButtons = new ( ) ;
186187
187188 private Color _highlightColor ;
188189 private Color _textColor ;
@@ -218,35 +219,52 @@ public override void UpdateElementDescriptor(ControlDescriptorInfo descriptor) {
218219 }
219220
220221 public void RefreshVerbs ( IEnumerable < ( int , ClientObjectReference , VerbSystem . VerbInfo ) > verbs ) {
221- _grid . Children . Clear ( ) ;
222+ var panelVerbs = verbs
223+ . Where ( v => v . Item3 . GetCategoryOrDefault ( DefaultVerbPanel ) == PanelName )
224+ . Order ( VerbNameComparer . OrdinalInstance ) ;
225+
226+ var seenKeys = new HashSet < ( int , ClientObjectReference ) > ( ) ;
227+ var gridIndex = 0 ;
228+ foreach ( var ( verbId , src , verbInfo ) in panelVerbs ) {
229+ var key = ( verbId , src ) ;
230+ seenKeys . Add ( key ) ;
231+
232+ if ( ! _verbButtons . ContainsKey ( key ) ) {
233+ var verbButton = new Button {
234+ Margin = new Thickness ( 2 ) ,
235+ Text = verbInfo . Name ,
236+ TextAlign = Label . AlignMode . Center
237+ } ;
238+
239+ verbButton . Label . Margin = new Thickness ( 6 , 0 , 6 , 2 ) ;
240+ verbButton . Label . FontColorOverride = _textColor ;
241+ verbButton . StyleBoxOverride = new StyleBoxEmpty ( ) ;
222242
223- foreach ( var ( verbId , src , verbInfo ) in verbs . Order ( VerbNameComparer . OrdinalInstance ) ) {
224- if ( verbInfo . GetCategoryOrDefault ( DefaultVerbPanel ) != PanelName )
225- continue ;
243+ verbButton . OnButtonDown += _ => {
244+ _verbSystem ? . ExecuteVerb ( src , verbId ) ;
245+ } ;
226246
227- Button verbButton = new Button {
228- Margin = new Thickness ( 2 ) ,
229- Text = verbInfo . Name ,
230- TextAlign = Label . AlignMode . Center
231- } ;
247+ verbButton . OnMouseEntered += _ => {
248+ verbButton . Label . FontColorOverride = _highlightColor ;
249+ } ;
232250
233- verbButton . Label . Margin = new Thickness ( 6 , 0 , 6 , 2 ) ;
234- verbButton . Label . FontColorOverride = _textColor ;
235- verbButton . StyleBoxOverride = new StyleBoxEmpty ( ) ;
251+ verbButton . OnMouseExited += _ => {
252+ verbButton . Label . FontColorOverride = _textColor ;
253+ } ;
236254
237- verbButton . OnButtonDown += _ => {
238- _verbSystem ? . ExecuteVerb ( src , verbId ) ;
239- } ;
255+ _verbButtons [ key ] = verbButton ;
256+ _grid . AddChild ( verbButton ) ;
257+ verbButton . SetPositionInParent ( gridIndex ) ;
258+ }
240259
241- verbButton . OnMouseEntered += _ => {
242- verbButton . Label . FontColorOverride = _highlightColor ;
243- } ;
260+ gridIndex ++ ;
261+ }
244262
245- verbButton . OnMouseExited += _ => {
246- verbButton . Label . FontColorOverride = _textColor ;
247- } ;
263+ foreach ( var key in _verbButtons . Keys ) {
264+ if ( seenKeys . Contains ( key ) ) continue ;
248265
249- _grid . Children . Add ( verbButton ) ;
266+ _grid . RemoveChild ( _verbButtons [ key ] ) ;
267+ _verbButtons . Remove ( key ) ;
250268 }
251269 }
252270}
0 commit comments