diff --git a/BlinForms.Framework/BlontrolAdapter.cs b/BlinForms.Framework/BlontrolAdapter.cs index 99e504f..c2b6939 100644 --- a/BlinForms.Framework/BlontrolAdapter.cs +++ b/BlinForms.Framework/BlontrolAdapter.cs @@ -135,22 +135,14 @@ namespace BlinForms.Framework TargetControl = nativeControl; - if (nativeControl is ICustomParentingBehavior customChildBehavior) + // TODO: Need a more reliable way to know whether the target control is already created, e.g. a return value + // from ControlFactory.CreateControl(). Right now the check assumes that if the target control is already parented, + // there is no need to parent it. Not an awful assumption, but looks odd. + if (TargetControl.Parent == null) { - customChildBehavior.SetActualParentControl(Parent.TargetControl); - } - else - { - if (Parent.TargetControl is ICustomParentingBehavior customParentBehavior) - { - AddChildControl(customParentBehavior.GetEffectiveParentControl(), siblingIndex, TargetControl); - } - else - { - // Add the new native control to the parent's child controls (the parent adapter is our - // container, so the parent adapter's control is our control's container. - AddChildControl(Parent.TargetControl, siblingIndex, TargetControl); - } + // Add the new native control to the parent's child controls (the parent adapter is our + // container, so the parent adapter's control is our control's container. + AddChildControl(Parent.TargetControl, siblingIndex, TargetControl); } var endIndexExcl = frameIndex + frames[frameIndex].ElementSubtreeLength; diff --git a/BlinForms.Framework/Controls/ICustomParentingBehavior.cs b/BlinForms.Framework/Controls/ICustomParentingBehavior.cs deleted file mode 100644 index 7ce1e64..0000000 --- a/BlinForms.Framework/Controls/ICustomParentingBehavior.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Windows.Forms; - -namespace BlinForms.Framework.Controls -{ - /// - /// Indicates that a control has a custom behavior for determining where child controls should be parented to. - /// For example, a SplitContainer control has built-in immutable children, and should not have those children - /// created directly. - /// - internal interface ICustomParentingBehavior - { - void SetActualParentControl(Control parentControl); - Control GetEffectiveParentControl(); - } -} diff --git a/BlinForms.Framework/Controls/PlaceholderControl.cs b/BlinForms.Framework/Controls/PlaceholderControl.cs deleted file mode 100644 index 94954e3..0000000 --- a/BlinForms.Framework/Controls/PlaceholderControl.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Windows.Forms; - -namespace BlinForms.Framework.Controls -{ - public class PlaceholderControl : Control, ICustomParentingBehavior - { - private Control _parentControl; - - public string State { get; set; } - - public Control GetEffectiveParentControl() - { - var parentSplitter = (System.Windows.Forms.SplitContainer)_parentControl; - var containerPanel = State == "Panel1" ? parentSplitter.Panel1 : State == "Panel2" ? parentSplitter.Panel2 : throw new InvalidOperationException("Invalid panel state!!!"); - return containerPanel; - } - - public void SetActualParentControl(Control parentControl) - { - _parentControl = parentControl; - } - } -} diff --git a/BlinForms.Framework/Controls/SplitterPanel1.cs b/BlinForms.Framework/Controls/SplitterPanel1.cs index 38a6c13..245ca81 100644 --- a/BlinForms.Framework/Controls/SplitterPanel1.cs +++ b/BlinForms.Framework/Controls/SplitterPanel1.cs @@ -1,20 +1,10 @@ -using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.Components.RenderTree; - -namespace BlinForms.Framework.Controls +namespace BlinForms.Framework.Controls { - public class SplitterPanel1 : FormsComponentBase + public class SplitterPanel1 : SplitterPanelBase { static SplitterPanel1() { - BlontrolAdapter.KnownElements.Add(typeof(SplitterPanel1).FullName, new ComponentControlFactoryFunc((_, __) => new PlaceholderControl() { State = "Panel1", })); - } - - [Parameter] public RenderFragment ChildContent { get; set; } - - protected override void RenderContents(RenderTreeBuilder builder) - { - builder.AddContent(1000, ChildContent); + BlontrolAdapter.KnownElements.Add(typeof(SplitterPanel1).FullName, new ComponentControlFactoryFunc((_, parentControl) => GetSplitterPanel(parentControl, panelNumber: 1))); } } } diff --git a/BlinForms.Framework/Controls/SplitterPanel2.cs b/BlinForms.Framework/Controls/SplitterPanel2.cs index bf3987f..0f93592 100644 --- a/BlinForms.Framework/Controls/SplitterPanel2.cs +++ b/BlinForms.Framework/Controls/SplitterPanel2.cs @@ -1,20 +1,10 @@ -using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.Components.RenderTree; - -namespace BlinForms.Framework.Controls +namespace BlinForms.Framework.Controls { - public class SplitterPanel2 : FormsComponentBase + public class SplitterPanel2 : SplitterPanelBase { static SplitterPanel2() { - BlontrolAdapter.KnownElements.Add(typeof(SplitterPanel2).FullName, new ComponentControlFactoryFunc((_, __) => new PlaceholderControl() { State = "Panel2", })); - } - - [Parameter] public RenderFragment ChildContent { get; set; } - - protected override void RenderContents(RenderTreeBuilder builder) - { - builder.AddContent(1000, ChildContent); + BlontrolAdapter.KnownElements.Add(typeof(SplitterPanel2).FullName, new ComponentControlFactoryFunc((_, parentControl) => GetSplitterPanel(parentControl, panelNumber: 2))); } } } diff --git a/BlinForms.Framework/Controls/SplitterPanelBase.cs b/BlinForms.Framework/Controls/SplitterPanelBase.cs new file mode 100644 index 0000000..548cbed --- /dev/null +++ b/BlinForms.Framework/Controls/SplitterPanelBase.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.RenderTree; +using System; +using System.Windows.Forms; + +namespace BlinForms.Framework.Controls +{ + public abstract class SplitterPanelBase : FormsComponentBase + { + private protected static Control GetSplitterPanel(Control parentControl, int panelNumber) + { + if (!(parentControl is System.Windows.Forms.SplitContainer splitContainer)) + { + // This gets called from a static constructor, so we really don't want to throw from here + return null; + } + return panelNumber switch + { + 1 => splitContainer.Panel1, + 2 => splitContainer.Panel2, + _ => throw new InvalidOperationException($"Invalid SplitContainer panel number: {panelNumber}. The only valid values are '1' and '2'."), + }; + } + + [Parameter] public RenderFragment ChildContent { get; set; } + + protected override void RenderContents(RenderTreeBuilder builder) + { + builder.AddContent(1000, ChildContent); + } + } +}