From 730d8e31032fbc685e680d6ff2be61fb19fbf4b4 Mon Sep 17 00:00:00 2001 From: Brandon Thetford Date: Sun, 18 Aug 2024 17:38:56 -0700 Subject: [PATCH] Add a generic form of Dim.Has --- Terminal.Gui/View/Layout/Dim.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Terminal.Gui/View/Layout/Dim.cs b/Terminal.Gui/View/Layout/Dim.cs index da46f2b72..a964bb2e1 100644 --- a/Terminal.Gui/View/Layout/Dim.cs +++ b/Terminal.Gui/View/Layout/Dim.cs @@ -196,6 +196,29 @@ public abstract record Dim : IEqualityOperators return false; } + /// + /// Indicates whether the specified type is in the hierarchy of this Dim object. + /// + /// A reference to this instance. + /// + public bool Has (out Dim dim) where T : Dim + { + dim = this; + + return this switch + { + T => true, + + // INTENT: Is it intentional that, when T is already DimCombine, this won't get called? + // If not, the fix is to do this one first. + // (That's also the case in the original) + // If we are a DimCombine, we have to check the left and right + // to see if they are of the type we are looking for. + DimCombine combine => combine.Left.Has (out dim) || combine.Right.Has (out dim), + _ => false + }; + } + #region virtual methods ///