From 67cd0f055ce7ff41399b048a63a97103b519f7ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pol=C3=A1=C5=A1ek?= Date: Fri, 15 Aug 2025 13:48:54 +0200 Subject: [PATCH] CmdPal: Check icon parent before adding in ContentIcon (Closes: #40928) (#40931) ## Summary of the Pull Request This pull request introduces a minor but important update to the `ContentIcon` control in the `Microsoft.CmdPal.UI` module. The changes improve robustness by adding checks to prevent duplicate parenting of the `Content` element and include a debug assertion for better diagnostics during development. ## PR Checklist - [x] Closes: #40928 - [ ] **Communication:** not yet - [ ] **Tests:** nope - [ ] **Localization:** none - [ ] **Dev docs:** nay - [ ] **New binaries:** no nothing - [ ] **Documentation updated:** too lazy for that ## Detailed Description of the Pull Request / Additional comments ### Key changes: #### Diagnostics and robustness improvements: * Added a `Debug.Assert` statement to verify that the `Content` element is not already parented to another element, helping to catch potential issues during development. (`[src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContentIcon.csR39-R49](diffhunk://#diff-330aad69f925cf7a9e07bb7147af8e6cd09776a4c745455ac8a91a24b482d076R39-R49)`) * Introduced checks to ensure the `Content` element is not added to the `Grid`'s `Children` collection if it already exists there, preventing redundant operations. (`[src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContentIcon.csR39-R49](diffhunk://#diff-330aad69f925cf7a9e07bb7147af8e6cd09776a4c745455ac8a91a24b482d076R39-R49)`) #### Code maintenance: * Added a `using System.Diagnostics` directive to enable the use of the `Debug` class for assertions. (`[src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContentIcon.csR5](diffhunk://#diff-330aad69f925cf7a9e07bb7147af8e6cd09776a4c745455ac8a91a24b482d076R5)`) ## Validation Steps Performed Turned extensions off and on and off and on and off and on and off and on and off and on and off and on and off and on and off and on and off and on and off and on and off and on and off and on and off and on and off and on and off and on. And then off and on again, just to be sure. --------- Co-authored-by: Mike Griese --- .../Microsoft.CmdPal.UI/Controls/ContentIcon.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContentIcon.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContentIcon.cs index 1c4945d131..211d28b410 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContentIcon.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/ContentIcon.cs @@ -2,6 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; using CommunityToolkit.WinUI; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -35,6 +36,17 @@ public partial class ContentIcon : FontIcon { if (this.FindDescendants().OfType().FirstOrDefault() is Grid grid && Content is not null) { + if (grid.Children.Contains(Content)) + { + return; + } + + if (Content is FrameworkElement element && element.Parent is not null) + { + Debug.Assert(false, $"IconBoxElement Content is already parented to {element.Parent.GetType().Name}"); + return; + } + grid.Children.Add(Content); } }