Remove PlaceholderControl and custom parenting logic

New logic is to assume that if target control is already parented, no need to add it as a child control.
This commit is contained in:
Eilon Lipton 2019-08-19 09:36:20 -07:00
Родитель 1fa2e77f63
Коммит c088a950aa
6 изменённых файлов: 45 добавлений и 80 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -1,15 +0,0 @@
using System.Windows.Forms;
namespace BlinForms.Framework.Controls
{
/// <summary>
/// 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.
/// </summary>
internal interface ICustomParentingBehavior
{
void SetActualParentControl(Control parentControl);
Control GetEffectiveParentControl();
}
}

Просмотреть файл

@ -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;
}
}
}

Просмотреть файл

@ -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)));
}
}
}

Просмотреть файл

@ -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)));
}
}
}

Просмотреть файл

@ -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);
}
}
}