Remove all generics from main APIs

- They were almost entirely noise because the only place the generics mattered is on the [Native]ElementManager
- To make writing generic ElementManagers easier, a new generic ElementManager<T> was added
This commit is contained in:
Eilon Lipton 2019-09-26 14:26:43 -07:00
Родитель c7b0a3de15
Коммит 27d48dc908
50 изменённых файлов: 180 добавлений и 123 удалений

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

@ -6,9 +6,12 @@ namespace Blaxamarin.Framework
{
internal class BlaxamarinElementManager : ElementManager<IXamarinFormsElementHandler>
{
public override bool IsParented(IXamarinFormsElementHandler handler) => handler.ElementControl.Parent != null;
protected override bool IsParented(IXamarinFormsElementHandler handler)
{
return handler.ElementControl.Parent != null;
}
public override void AddChildElement(
protected override void AddChildElement(
IXamarinFormsElementHandler parentHandler,
IXamarinFormsElementHandler childHandler,
int physicalSiblingIndex)
@ -109,11 +112,10 @@ namespace Blaxamarin.Framework
}
}
public override int GetPhysicalSiblingIndex(
protected override int GetPhysicalSiblingIndex(
IXamarinFormsElementHandler handler)
{
// TODO: What is the set of types that support child elements? Do they all need to be special-cased here? (Maybe...)
var nativeComponent = handler.ElementControl;
switch (nativeComponent.Parent)
@ -154,9 +156,10 @@ namespace Blaxamarin.Framework
}
}
public override void RemoveElement(IXamarinFormsElementHandler handler)
protected override void RemoveElement(IXamarinFormsElementHandler handler)
{
// TODO: Need to make this logic more generic; not all parents are Layouts, not all children are Views
var control = handler.ElementControl;
var physicalParent = control.Parent;
if (physicalParent is Layout<View> physicalParentAsLayout)
@ -166,9 +169,9 @@ namespace Blaxamarin.Framework
}
}
public override bool IsParentOfChild(IXamarinFormsElementHandler parentControl, IXamarinFormsElementHandler childControl)
protected override bool IsParentOfChild(IXamarinFormsElementHandler parentHandler, IXamarinFormsElementHandler childHandler)
{
return childControl.ElementControl.Parent == parentControl.ElementControl;
return childHandler.ElementControl.Parent == parentHandler.ElementControl;
}
}
}

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

@ -5,7 +5,7 @@ using System.Diagnostics;
namespace Blaxamarin.Framework
{
public class BlaxamarinRenderer : EmblazonRenderer<IXamarinFormsElementHandler>
public class BlaxamarinRenderer : EmblazonRenderer
{
public BlaxamarinRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
: base(serviceProvider, loggerFactory)
@ -20,7 +20,7 @@ namespace Blaxamarin.Framework
//MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
protected override ElementManager<IXamarinFormsElementHandler> CreateNativeControlManager()
protected override ElementManager CreateNativeControlManager()
{
return new BlaxamarinElementManager();
}

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

@ -9,7 +9,7 @@ namespace Blaxamarin.Framework.Elements
{
static Button()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>
ElementHandlerRegistry
.RegisterElementHandler<Button>(renderer => new ButtonHandler(renderer, new XF.Button()));
}

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

@ -8,7 +8,7 @@ namespace Blaxamarin.Framework.Elements
{
static ContentPage()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>
ElementHandlerRegistry
.RegisterElementHandler<ContentPage>(renderer => new ContentPageHandler(renderer, new XF.ContentPage()));
}
}

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

@ -10,7 +10,7 @@ namespace Blaxamarin.Framework.Elements
{
static Entry()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>.RegisterElementHandler<Entry>(
ElementHandlerRegistry.RegisterElementHandler<Entry>(
renderer => new EntryHandler(renderer, new XF.Entry()));
}

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

@ -6,7 +6,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class ButtonHandler : ViewHandler
{
public ButtonHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.Button buttonControl) : base(renderer, buttonControl)
public ButtonHandler(EmblazonRenderer renderer, XF.Button buttonControl) : base(renderer, buttonControl)
{
ButtonControl = buttonControl ?? throw new ArgumentNullException(nameof(buttonControl));
ButtonControl.Clicked += (s, e) =>

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

@ -6,7 +6,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class ContentPageHandler : TemplatedPageHandler
{
public ContentPageHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.ContentPage contentPageControl) : base(renderer, contentPageControl)
public ContentPageHandler(EmblazonRenderer renderer, XF.ContentPage contentPageControl) : base(renderer, contentPageControl)
{
ContentPageControl = contentPageControl ?? throw new ArgumentNullException(nameof(contentPageControl));
}

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

@ -4,7 +4,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public sealed class DetailPageHandler : ContentPageHandler
{
public DetailPageHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.ContentPage masterDetailPageControl) : base(renderer, masterDetailPageControl)
public DetailPageHandler(EmblazonRenderer renderer, XF.ContentPage masterDetailPageControl) : base(renderer, masterDetailPageControl)
{
MasterDetailPageControl = masterDetailPageControl ?? throw new System.ArgumentNullException(nameof(masterDetailPageControl));
}

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

@ -6,13 +6,13 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class ElementHandler : IXamarinFormsElementHandler
{
public ElementHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.Element elementControl)
public ElementHandler(EmblazonRenderer renderer, XF.Element elementControl)
{
Renderer = renderer ?? throw new ArgumentNullException(nameof(renderer));
ElementControl = elementControl ?? throw new ArgumentNullException(nameof(elementControl));
}
public EmblazonRenderer<IXamarinFormsElementHandler> Renderer { get; }
public EmblazonRenderer Renderer { get; }
public XF.Element ElementControl { get; }
public object TargetElement => ElementControl;

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

@ -6,7 +6,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class EntryHandler : InputViewHandler
{
public EntryHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.Entry entryControl) : base(renderer, entryControl)
public EntryHandler(EmblazonRenderer renderer, XF.Entry entryControl) : base(renderer, entryControl)
{
EntryControl = entryControl ?? throw new System.ArgumentNullException(nameof(entryControl));
EntryControl.Completed += (s, e) =>

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

@ -5,7 +5,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class InputViewHandler : ViewHandler
{
public InputViewHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.InputView inputViewControl) : base(renderer, inputViewControl)
public InputViewHandler(EmblazonRenderer renderer, XF.InputView inputViewControl) : base(renderer, inputViewControl)
{
InputViewControl = inputViewControl ?? throw new System.ArgumentNullException(nameof(inputViewControl));
}

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

@ -5,7 +5,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class LabelHandler : ViewHandler
{
public LabelHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.Label labelControl) : base(renderer, labelControl)
public LabelHandler(EmblazonRenderer renderer, XF.Label labelControl) : base(renderer, labelControl)
{
LabelControl = labelControl ?? throw new System.ArgumentNullException(nameof(labelControl));
}

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

@ -5,7 +5,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class LayoutHandler : ViewHandler
{
public LayoutHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.Layout layoutControl) : base(renderer, layoutControl)
public LayoutHandler(EmblazonRenderer renderer, XF.Layout layoutControl) : base(renderer, layoutControl)
{
LayoutControl = layoutControl ?? throw new System.ArgumentNullException(nameof(layoutControl));
}

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

@ -5,7 +5,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class MasterDetailPageHandler : PageHandler
{
public MasterDetailPageHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.MasterDetailPage masterDetailPageControl) : base(renderer, masterDetailPageControl)
public MasterDetailPageHandler(EmblazonRenderer renderer, XF.MasterDetailPage masterDetailPageControl) : base(renderer, masterDetailPageControl)
{
MasterDetailPageControl = masterDetailPageControl ?? throw new System.ArgumentNullException(nameof(masterDetailPageControl));

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

@ -4,7 +4,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public sealed class MasterPageHandler : ContentPageHandler
{
public MasterPageHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.ContentPage masterDetailPageControl) : base(renderer, masterDetailPageControl)
public MasterPageHandler(EmblazonRenderer renderer, XF.ContentPage masterDetailPageControl) : base(renderer, masterDetailPageControl)
{
MasterDetailPageControl = masterDetailPageControl ?? throw new System.ArgumentNullException(nameof(masterDetailPageControl));

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

@ -5,7 +5,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class NavigableElementHandler : ElementHandler
{
public NavigableElementHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.NavigableElement navigableElementControl) : base(renderer, navigableElementControl)
public NavigableElementHandler(EmblazonRenderer renderer, XF.NavigableElement navigableElementControl) : base(renderer, navigableElementControl)
{
NavigableElementControl = navigableElementControl ?? throw new System.ArgumentNullException(nameof(navigableElementControl));
}

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

@ -6,7 +6,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class PageHandler : VisualElementHandler
{
public PageHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.Page pageControl) : base(renderer, pageControl)
public PageHandler(EmblazonRenderer renderer, XF.Page pageControl) : base(renderer, pageControl)
{
PageControl = pageControl ?? throw new ArgumentNullException(nameof(pageControl));
}

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

@ -5,7 +5,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class ScrollViewHandler : LayoutHandler
{
public ScrollViewHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.ScrollView scrollViewControl) : base(renderer, scrollViewControl)
public ScrollViewHandler(EmblazonRenderer renderer, XF.ScrollView scrollViewControl) : base(renderer, scrollViewControl)
{
ScrollViewControl = scrollViewControl ?? throw new System.ArgumentNullException(nameof(scrollViewControl));
}

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

@ -5,7 +5,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class StackLayoutHandler : LayoutHandler
{
public StackLayoutHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.StackLayout stackLayoutControl) : base(renderer, stackLayoutControl)
public StackLayoutHandler(EmblazonRenderer renderer, XF.StackLayout stackLayoutControl) : base(renderer, stackLayoutControl)
{
StackLayoutControl = stackLayoutControl ?? throw new System.ArgumentNullException(nameof(stackLayoutControl));
}

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

@ -6,7 +6,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class SwitchHandler : ViewHandler
{
public SwitchHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.Switch switchControl) : base(renderer, switchControl)
public SwitchHandler(EmblazonRenderer renderer, XF.Switch switchControl) : base(renderer, switchControl)
{
SwitchControl = switchControl ?? throw new System.ArgumentNullException(nameof(switchControl));
SwitchControl.Toggled += (s, e) =>

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

@ -5,7 +5,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class TabbedPageHandler : PageHandler
{
public TabbedPageHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.TabbedPage tabbedPageControl) : base(renderer, tabbedPageControl)
public TabbedPageHandler(EmblazonRenderer renderer, XF.TabbedPage tabbedPageControl) : base(renderer, tabbedPageControl)
{
TabbedPageControl = tabbedPageControl ?? throw new System.ArgumentNullException(nameof(tabbedPageControl));
}

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

@ -6,7 +6,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class TemplatedPageHandler : PageHandler
{
public TemplatedPageHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.TemplatedPage templatedPageControl) : base(renderer, templatedPageControl)
public TemplatedPageHandler(EmblazonRenderer renderer, XF.TemplatedPage templatedPageControl) : base(renderer, templatedPageControl)
{
TemplatedPageControl = templatedPageControl ?? throw new ArgumentNullException(nameof(templatedPageControl));
}

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

@ -6,7 +6,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class ViewHandler : VisualElementHandler
{
public ViewHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.View viewControl) : base(renderer, viewControl)
public ViewHandler(EmblazonRenderer renderer, XF.View viewControl) : base(renderer, viewControl)
{
ViewControl = viewControl ?? throw new ArgumentNullException(nameof(viewControl));
}

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

@ -6,7 +6,7 @@ namespace Blaxamarin.Framework.Elements.Handlers
{
public class VisualElementHandler : NavigableElementHandler
{
public VisualElementHandler(EmblazonRenderer<IXamarinFormsElementHandler> renderer, XF.VisualElement visualElementControl) : base(renderer, visualElementControl)
public VisualElementHandler(EmblazonRenderer renderer, XF.VisualElement visualElementControl) : base(renderer, visualElementControl)
{
VisualElementControl = visualElementControl ?? throw new ArgumentNullException(nameof(visualElementControl));
VisualElementControl.Focused += (s, e) =>

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

@ -9,7 +9,7 @@ namespace Blaxamarin.Framework.Elements
{
static Label()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>
ElementHandlerRegistry
.RegisterElementHandler<Label>(renderer => new LabelHandler(renderer, new XF.Label()));
}

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

@ -10,7 +10,7 @@ namespace Blaxamarin.Framework.Elements
{
static MasterDetailDetailPage()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>
ElementHandlerRegistry
.RegisterElementHandler<MasterDetailDetailPage>(renderer => new DetailPageHandler(renderer, new XF.ContentPage()));
}
}

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

@ -10,7 +10,7 @@ namespace Blaxamarin.Framework.Elements
{
static MasterDetailMasterPage()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>
ElementHandlerRegistry
.RegisterElementHandler<MasterDetailMasterPage>(renderer => new MasterPageHandler(renderer, new XF.ContentPage()));
}
}

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

@ -10,7 +10,7 @@ namespace Blaxamarin.Framework.Elements
{
static MasterDetailPage()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>
ElementHandlerRegistry
.RegisterElementHandler<MasterDetailPage>(renderer => new MasterDetailPageHandler(renderer, new XF.MasterDetailPage()));
}

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

@ -10,7 +10,7 @@ namespace Blaxamarin.Framework.Elements
{
static Page()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>.RegisterElementHandler<Page>(
ElementHandlerRegistry.RegisterElementHandler<Page>(
renderer => new PageHandler(renderer, new XF.Page()));
}

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

@ -9,7 +9,7 @@ namespace Blaxamarin.Framework.Elements
{
static ScrollView()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>.RegisterElementHandler<ScrollView>(
ElementHandlerRegistry.RegisterElementHandler<ScrollView>(
renderer => new ScrollViewHandler(renderer, new XF.ScrollView()));
}

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

@ -9,7 +9,7 @@ namespace Blaxamarin.Framework.Elements
{
static StackLayout()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>
ElementHandlerRegistry
.RegisterElementHandler<StackLayout>(renderer => new StackLayoutHandler(renderer, new XF.StackLayout()));
}

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

@ -10,7 +10,7 @@ namespace Blaxamarin.Framework.Elements
{
static Switch()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>.RegisterElementHandler<Switch>(
ElementHandlerRegistry.RegisterElementHandler<Switch>(
renderer => new SwitchHandler(renderer, new XF.Switch()));
}

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

@ -8,7 +8,7 @@ namespace Blaxamarin.Framework.Elements
{
static TabbedPage()
{
ElementHandlerRegistry<IXamarinFormsElementHandler>
ElementHandlerRegistry
.RegisterElementHandler<TabbedPage>(renderer => new TabbedPageHandler(renderer, new XF.TabbedPage()));
}
}

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

@ -5,40 +5,40 @@ namespace BlinForms.Framework
{
internal class BlinFormsElementManager : ElementManager<IWindowsFormsControlHandler>
{
public override void RemoveElement(IWindowsFormsControlHandler control)
protected override void RemoveElement(IWindowsFormsControlHandler handler)
{
control.Control.Parent.Controls.Remove(control.Control);
handler.Control.Parent.Controls.Remove(handler.Control);
}
public override void AddChildElement(IWindowsFormsControlHandler parentControl, IWindowsFormsControlHandler childControl, int physicalSiblingIndex)
protected override void AddChildElement(IWindowsFormsControlHandler parentHandler, IWindowsFormsControlHandler childHandler, int physicalSiblingIndex)
{
if (physicalSiblingIndex <= parentControl.Control.Controls.Count)
if (physicalSiblingIndex <= parentHandler.Control.Controls.Count)
{
// WinForms ControlCollection doesn't support Insert(), so add the new child at the end,
// and then re-order the collection to move the control to the correct index.
parentControl.Control.Controls.Add(childControl.Control);
parentControl.Control.Controls.SetChildIndex(childControl.Control, physicalSiblingIndex);
parentHandler.Control.Controls.Add(childHandler.Control);
parentHandler.Control.Controls.SetChildIndex(childHandler.Control, physicalSiblingIndex);
}
else
{
Debug.WriteLine($"WARNING: {nameof(AddChildElement)} called with {nameof(physicalSiblingIndex)}={physicalSiblingIndex}, but parentControl.Controls.Count={parentControl.Control.Controls.Count}");
parentControl.Control.Controls.Add(childControl.Control);
Debug.WriteLine($"WARNING: {nameof(AddChildElement)} called with {nameof(physicalSiblingIndex)}={physicalSiblingIndex}, but parentControl.Controls.Count={parentHandler.Control.Controls.Count}");
parentHandler.Control.Controls.Add(childHandler.Control);
}
}
public override int GetPhysicalSiblingIndex(IWindowsFormsControlHandler control)
protected override int GetPhysicalSiblingIndex(IWindowsFormsControlHandler handler)
{
return control.Control.Parent.Controls.GetChildIndex(control.Control);
return handler.Control.Parent.Controls.GetChildIndex(handler.Control);
}
public override bool IsParented(IWindowsFormsControlHandler nativeControl)
protected override bool IsParented(IWindowsFormsControlHandler handler)
{
return nativeControl.Control.Parent != null;
return handler.Control.Parent != null;
}
public override bool IsParentOfChild(IWindowsFormsControlHandler parentControl, IWindowsFormsControlHandler childControl)
protected override bool IsParentOfChild(IWindowsFormsControlHandler parentHandler, IWindowsFormsControlHandler childHandler)
{
return parentControl.Control.Contains(childControl.Control);
return parentHandler.Control.Contains(childHandler.Control);
}
}
}

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

@ -5,7 +5,7 @@ using System.Windows.Forms;
namespace BlinForms.Framework
{
public class BlinFormsRenderer : EmblazonRenderer<IWindowsFormsControlHandler>
public class BlinFormsRenderer : EmblazonRenderer
{
public BlinFormsRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
: base(serviceProvider, loggerFactory)
@ -17,7 +17,7 @@ namespace BlinForms.Framework
MessageBox.Show(exception?.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
protected override ElementManager<IWindowsFormsControlHandler> CreateNativeControlManager()
protected override ElementManager CreateNativeControlManager()
{
return new BlinFormsElementManager();
}

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

@ -9,7 +9,7 @@ namespace BlinForms.Framework.Controls
{
static Button()
{
ElementHandlerRegistry<IWindowsFormsControlHandler>.RegisterElementHandler<Button>(
ElementHandlerRegistry.RegisterElementHandler<Button>(
renderer => new BlazorButton(renderer));
}
@ -30,7 +30,7 @@ namespace BlinForms.Framework.Controls
private class BlazorButton : System.Windows.Forms.Button, IWindowsFormsControlHandler
{
public BlazorButton(EmblazonRenderer<IWindowsFormsControlHandler> renderer)
public BlazorButton(EmblazonRenderer renderer)
{
Click += (s, e) =>
{
@ -43,7 +43,7 @@ namespace BlinForms.Framework.Controls
}
public ulong ClickEventHandlerId { get; set; }
public EmblazonRenderer<IWindowsFormsControlHandler> Renderer { get; }
public EmblazonRenderer Renderer { get; }
public Control Control => this;
public object TargetElement => this;

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

@ -9,7 +9,7 @@ namespace BlinForms.Framework.Controls
{
static CheckBox()
{
ElementHandlerRegistry<IWindowsFormsControlHandler>.RegisterElementHandler<CheckBox>(renderer => new BlazorCheckBox(renderer));
ElementHandlerRegistry.RegisterElementHandler<CheckBox>(renderer => new BlazorCheckBox(renderer));
}
[Parameter] public string Text { get; set; }
@ -56,7 +56,7 @@ namespace BlinForms.Framework.Controls
private class BlazorCheckBox : System.Windows.Forms.CheckBox, IWindowsFormsControlHandler
{
public BlazorCheckBox(EmblazonRenderer<IWindowsFormsControlHandler> renderer)
public BlazorCheckBox(EmblazonRenderer renderer)
{
CheckedChanged += (s, e) =>
{
@ -77,7 +77,7 @@ namespace BlinForms.Framework.Controls
public ulong CheckedChangedEventHandlerId { get; set; }
public ulong CheckStateChangedEventHandlerId { get; set; }
public EmblazonRenderer<IWindowsFormsControlHandler> Renderer { get; }
public EmblazonRenderer Renderer { get; }
public Control Control => this;
public object TargetElement => this;

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

@ -8,7 +8,7 @@ namespace BlinForms.Framework.Controls
{
static Label()
{
ElementHandlerRegistry<IWindowsFormsControlHandler>.RegisterElementHandler<Label, BlazorLabel>();
ElementHandlerRegistry.RegisterElementHandler<Label, BlazorLabel>();
}
[Parameter] public string Text { get; set; }

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

@ -8,7 +8,7 @@ namespace BlinForms.Framework.Controls
{
static Panel()
{
ElementHandlerRegistry<IWindowsFormsControlHandler>.RegisterElementHandler<Panel, BlazorPanel>();
ElementHandlerRegistry.RegisterElementHandler<Panel, BlazorPanel>();
}
[Parameter] public bool? AutoScroll { get; set; }

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

@ -9,7 +9,7 @@ namespace BlinForms.Framework.Controls
{
static SplitContainer()
{
ElementHandlerRegistry<IWindowsFormsControlHandler>.RegisterElementHandler<SplitContainer, BlazorSplitContainer>();
ElementHandlerRegistry.RegisterElementHandler<SplitContainer, BlazorSplitContainer>();
}
[Parameter] public RenderFragment Panel1 { get; set; }

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

@ -8,8 +8,8 @@ namespace BlinForms.Framework.Controls
{
static SplitterPanel1()
{
ElementHandlerRegistry<IWindowsFormsControlHandler>.RegisterElementHandler<SplitterPanel1>(
(_, parentControl) => GetSplitterPanel(parentControl.Control, panelNumber: 1));
ElementHandlerRegistry.RegisterElementHandler<SplitterPanel1>(
(_, parentControl) => GetSplitterPanel(((IWindowsFormsControlHandler)parentControl).Control, panelNumber: 1));
}
}
}

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

@ -8,8 +8,8 @@ namespace BlinForms.Framework.Controls
{
static SplitterPanel2()
{
ElementHandlerRegistry<IWindowsFormsControlHandler>.RegisterElementHandler<SplitterPanel2>(
(_, parentControl) => GetSplitterPanel(parentControl.Control, panelNumber: 2));
ElementHandlerRegistry.RegisterElementHandler<SplitterPanel2>(
(_, parentControl) => GetSplitterPanel(((IWindowsFormsControlHandler)parentControl).Control, panelNumber: 2));
}
}
}

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

@ -9,7 +9,7 @@ namespace BlinForms.Framework.Controls
{
static TextBox()
{
ElementHandlerRegistry<IWindowsFormsControlHandler>.RegisterElementHandler<TextBox>(renderer => new BlazorTextBox(renderer));
ElementHandlerRegistry.RegisterElementHandler<TextBox>(renderer => new BlazorTextBox(renderer));
}
[Parameter] public string Text { get; set; }
@ -55,7 +55,7 @@ namespace BlinForms.Framework.Controls
private class BlazorTextBox : System.Windows.Forms.TextBox, IWindowsFormsControlHandler
{
public BlazorTextBox(EmblazonRenderer<IWindowsFormsControlHandler> renderer)
public BlazorTextBox(EmblazonRenderer renderer)
{
TextChanged += (s, e) =>
{
@ -68,7 +68,7 @@ namespace BlinForms.Framework.Controls
}
public ulong TextChangedEventHandlerId { get; set; }
public EmblazonRenderer<IWindowsFormsControlHandler> Renderer { get; }
public EmblazonRenderer Renderer { get; }
public Control Control => this;
public object TargetElement => this;

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

@ -2,16 +2,16 @@
namespace Emblazon
{
internal class ElementHandlerFactory<TElementHandler> where TElementHandler : class, IElementHandler
internal class ElementHandlerFactory
{
private readonly Func<EmblazonRenderer<TElementHandler>, TElementHandler, TElementHandler> _callback;
private readonly Func<EmblazonRenderer, IElementHandler, IElementHandler> _callback;
public ElementHandlerFactory(Func<EmblazonRenderer<TElementHandler>, TElementHandler, TElementHandler> callback)
public ElementHandlerFactory(Func<EmblazonRenderer, IElementHandler, IElementHandler> callback)
{
_callback = callback ?? throw new ArgumentNullException(nameof(callback));
}
public TElementHandler CreateElementHandler(ElementHandlerFactoryContext<TElementHandler> context)
public IElementHandler CreateElementHandler(ElementHandlerFactoryContext context)
{
return _callback(context.Renderer, context.ParentHandler);
}

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

@ -2,16 +2,16 @@
namespace Emblazon
{
internal class ElementHandlerFactoryContext<TElementHandler> where TElementHandler : class, IElementHandler
internal class ElementHandlerFactoryContext
{
public ElementHandlerFactoryContext(EmblazonRenderer<TElementHandler> renderer, TElementHandler parentHandler)
public ElementHandlerFactoryContext(EmblazonRenderer renderer, IElementHandler parentHandler)
{
Renderer = renderer ?? throw new ArgumentNullException(nameof(renderer));
ParentHandler = parentHandler;
}
public TElementHandler ParentHandler { get; }
public IElementHandler ParentHandler { get; }
public EmblazonRenderer<TElementHandler> Renderer { get; }
public EmblazonRenderer Renderer { get; }
}
}

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

@ -3,28 +3,26 @@ using System.Collections.Generic;
namespace Emblazon
{
#pragma warning disable CA1000 // Do not declare static members on generic types
public static class ElementHandlerRegistry<TNativeControlHandler> where TNativeControlHandler : class, IElementHandler
public static class ElementHandlerRegistry
{
internal static Dictionary<string, ElementHandlerFactory<TNativeControlHandler>> ElementHandlers { get; }
= new Dictionary<string, ElementHandlerFactory<TNativeControlHandler>>();
internal static Dictionary<string, ElementHandlerFactory> ElementHandlers { get; }
= new Dictionary<string, ElementHandlerFactory>();
public static void RegisterElementHandler<TComponent>(
Func<EmblazonRenderer<TNativeControlHandler>, TNativeControlHandler, TNativeControlHandler> factory) where TComponent : NativeControlComponentBase
Func<EmblazonRenderer, IElementHandler, IElementHandler> factory) where TComponent : NativeControlComponentBase
{
ElementHandlers.Add(typeof(TComponent).FullName, new ElementHandlerFactory<TNativeControlHandler>(factory));
ElementHandlers.Add(typeof(TComponent).FullName, new ElementHandlerFactory(factory));
}
public static void RegisterElementHandler<TComponent>(
Func<EmblazonRenderer<TNativeControlHandler>, TNativeControlHandler> factory) where TComponent : NativeControlComponentBase
Func<EmblazonRenderer, IElementHandler> factory) where TComponent : NativeControlComponentBase
{
ElementHandlers.Add(typeof(TComponent).FullName, new ElementHandlerFactory<TNativeControlHandler>((renderer, _) => factory(renderer)));
ElementHandlers.Add(typeof(TComponent).FullName, new ElementHandlerFactory((renderer, _) => factory(renderer)));
}
public static void RegisterElementHandler<TComponent, TControlHandler>() where TComponent : NativeControlComponentBase where TControlHandler : class, IElementHandler, new()
{
RegisterElementHandler<TComponent>((_, __) => new TControlHandler() as TNativeControlHandler);
RegisterElementHandler<TComponent>((_, __) => new TControlHandler());
}
}
#pragma warning restore CA1000 // Do not declare static members on generic types
}

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

@ -6,13 +6,12 @@
/// parent/child relationships, so each must implement this given the constraints
/// and requirements of their systems.
/// </summary>
/// <typeparam name="TElementHandler"></typeparam>
public abstract class ElementManager<TElementHandler> where TElementHandler : IElementHandler
public abstract class ElementManager
{
public abstract bool IsParented(TElementHandler handler);
public abstract void AddChildElement(TElementHandler parentHandler, TElementHandler childHandler, int physicalSiblingIndex);
public abstract int GetPhysicalSiblingIndex(TElementHandler handler);
public abstract void RemoveElement(TElementHandler handler);
public abstract bool IsParentOfChild(TElementHandler parentHandler, TElementHandler childHandler);
public abstract void AddChildElement(IElementHandler parentHandler, IElementHandler childHandler, int physicalSiblingIndex);
public abstract int GetPhysicalSiblingIndex(IElementHandler handler);
public abstract bool IsParented(IElementHandler handler);
public abstract bool IsParentOfChild(IElementHandler parentHandler, IElementHandler childHandler);
public abstract void RemoveElement(IElementHandler handler);
}
}

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

@ -0,0 +1,51 @@
using System;
namespace Emblazon
{
/// <summary>
/// Utility intermediate class to make it easier to strongly-type a derived <see cref="ElementManager"/>.
/// </summary>
/// <typeparam name="TElementType"></typeparam>
public abstract class ElementManager<TElementType> : ElementManager
{
private static TElementType ConvertToType(IElementHandler elementHandler, string parameterName)
{
if (!(elementHandler is TElementType))
{
throw new ArgumentException($"Expected parameter value of type '{elementHandler.GetType().FullName}' to be convertible to type '{typeof(TElementType).FullName}'.", parameterName);
}
return (TElementType)elementHandler;
}
public sealed override void AddChildElement(IElementHandler parentHandler, IElementHandler childHandler, int physicalSiblingIndex)
{
AddChildElement(ConvertToType(parentHandler, nameof(parentHandler)), ConvertToType(childHandler, nameof(childHandler)), physicalSiblingIndex);
}
public sealed override int GetPhysicalSiblingIndex(IElementHandler handler)
{
return GetPhysicalSiblingIndex(ConvertToType(handler, nameof(handler)));
}
public sealed override bool IsParented(IElementHandler handler)
{
return IsParented(ConvertToType(handler, nameof(handler)));
}
public sealed override bool IsParentOfChild(IElementHandler parentHandler, IElementHandler childHandler)
{
return IsParentOfChild(ConvertToType(parentHandler, nameof(parentHandler)), ConvertToType(childHandler, nameof(childHandler)));
}
public sealed override void RemoveElement(IElementHandler handler)
{
RemoveElement(ConvertToType(handler, nameof(handler)));
}
protected abstract void AddChildElement(TElementType elementType1, TElementType elementType2, int physicalSiblingIndex);
protected abstract int GetPhysicalSiblingIndex(TElementType elementType);
protected abstract bool IsParented(TElementType elementType);
protected abstract bool IsParentOfChild(TElementType elementType1, TElementType elementType2);
protected abstract void RemoveElement(TElementType elementType);
}
}

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

@ -10,11 +10,11 @@ namespace Emblazon
/// Represents a "shadow" item that Blazor uses to map changes into the live native control tree.
/// </summary>
[DebuggerDisplay("{DebugName}")]
internal sealed class EmblazonAdapter<TElementHandler> : IDisposable where TElementHandler : class, IElementHandler
internal sealed class EmblazonAdapter : IDisposable
{
private static volatile int DebugInstanceCounter;
public EmblazonAdapter(EmblazonRenderer<TElementHandler> renderer, TElementHandler closestPhysicalParent, TElementHandler knownTargetControl = null)
public EmblazonAdapter(EmblazonRenderer renderer, IElementHandler closestPhysicalParent, IElementHandler knownTargetControl = null)
{
Renderer = renderer ?? throw new ArgumentNullException(nameof(renderer));
_closestPhysicalParent = closestPhysicalParent;
@ -28,13 +28,13 @@ namespace Emblazon
private string DebugName => $"[#{_debugInstanceCounterValue}] {Name}";
public EmblazonAdapter<TElementHandler> Parent { get; private set; }
public List<EmblazonAdapter<TElementHandler>> Children { get; } = new List<EmblazonAdapter<TElementHandler>>();
public EmblazonAdapter Parent { get; private set; }
public List<EmblazonAdapter> Children { get; } = new List<EmblazonAdapter>();
private readonly TElementHandler _closestPhysicalParent;
private TElementHandler _possibleTargetControl;
private readonly IElementHandler _closestPhysicalParent;
private IElementHandler _possibleTargetControl;
public EmblazonRenderer<TElementHandler> Renderer { get; }
public EmblazonRenderer Renderer { get; }
/// <summary>
/// Used for debugging purposes.
@ -108,7 +108,7 @@ namespace Emblazon
{
// This adapter represents a physical control, so by removing it, we implicitly
// remove all descendants.
Renderer.NativeControlManager.RemoveElement(_possibleTargetControl);
Renderer.ElementManager.RemoveElement(_possibleTargetControl);
}
else
{
@ -202,9 +202,9 @@ namespace Emblazon
}
}
private EmblazonAdapter<TElementHandler> CreateAdapter(TElementHandler physicalParent)
private EmblazonAdapter CreateAdapter(IElementHandler physicalParent)
{
return new EmblazonAdapter<TElementHandler>(Renderer, physicalParent);
return new EmblazonAdapter(Renderer, physicalParent);
}
private void InsertElement(int siblingIndex, RenderTreeFrame[] frames, int frameIndex, int componentId, RenderBatch batch)
@ -294,14 +294,14 @@ namespace Emblazon
var matchedEarlierSibling = GetEarlierSiblingMatch(parentAdapter, childAdapter);
if (matchedEarlierSibling != null)
{
if (!Renderer.NativeControlManager.IsParentOfChild(_closestPhysicalParent, matchedEarlierSibling._possibleTargetControl))
if (!Renderer.ElementManager.IsParentOfChild(_closestPhysicalParent, matchedEarlierSibling._possibleTargetControl))
{
Debug.Fail($"Expected that the item found ({matchedEarlierSibling.DebugName}) with target control ({matchedEarlierSibling._possibleTargetControl.GetType().FullName}) should necessarily be an immediate child of the closest native parent ({_closestPhysicalParent.GetType().FullName}), but it wasn't...");
}
// If a native control was found somewhere within this sibling, the index for the new element
// will be 1 greater than its native index.
return Renderer.NativeControlManager.GetPhysicalSiblingIndex(matchedEarlierSibling._possibleTargetControl) + 1;
return Renderer.ElementManager.GetPhysicalSiblingIndex(matchedEarlierSibling._possibleTargetControl) + 1;
}
// If this level has a native control and all its relevant children have been scanned, then there's
@ -322,7 +322,7 @@ namespace Emblazon
return -1;
}
private static EmblazonAdapter<TElementHandler> GetEarlierSiblingMatch(EmblazonAdapter<TElementHandler> parentAdapter, EmblazonAdapter<TElementHandler> childAdapter)
private static EmblazonAdapter GetEarlierSiblingMatch(EmblazonAdapter parentAdapter, EmblazonAdapter childAdapter)
{
var indexOfParentsChildAdapter = parentAdapter.Children.IndexOf(childAdapter);
@ -342,7 +342,7 @@ namespace Emblazon
return null;
}
private EmblazonAdapter<TElementHandler> GetLastDescendantWithPhysicalControl()
private EmblazonAdapter GetLastDescendantWithPhysicalControl()
{
if (_possibleTargetControl != null)
{
@ -395,7 +395,7 @@ namespace Emblazon
;
}
private void AddChildAdapter(int siblingIndex, EmblazonAdapter<TElementHandler> childAdapter)
private void AddChildAdapter(int siblingIndex, EmblazonAdapter childAdapter)
{
childAdapter.Parent = this;

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

@ -8,10 +8,10 @@ using System.Threading.Tasks;
namespace Emblazon
{
public abstract class EmblazonRenderer<TElementHandler> : Renderer where TElementHandler : class, IElementHandler
public abstract class EmblazonRenderer : Renderer
{
private readonly Dictionary<int, EmblazonAdapter<TElementHandler>> _componentIdToAdapter = new Dictionary<int, EmblazonAdapter<TElementHandler>>();
private ElementManager<TElementHandler> _nativeControlManager;
private readonly Dictionary<int, EmblazonAdapter> _componentIdToAdapter = new Dictionary<int, EmblazonAdapter>();
private ElementManager _elementManager;
private readonly Dictionary<ulong, Action> _eventRegistrations = new Dictionary<ulong, Action>();
@ -20,9 +20,15 @@ namespace Emblazon
{
}
protected abstract ElementManager<TElementHandler> CreateNativeControlManager();
protected abstract ElementManager CreateNativeControlManager();
internal ElementManager<TElementHandler> NativeControlManager => _nativeControlManager ?? (_nativeControlManager = CreateNativeControlManager());
internal ElementManager ElementManager
{
get
{
return _elementManager ?? (_elementManager = CreateNativeControlManager());
}
}
public override Dispatcher Dispatcher { get; }
= Dispatcher.CreateDefault();
@ -33,7 +39,7 @@ namespace Emblazon
/// <typeparam name="TComponent"></typeparam>
/// <param name="parent"></param>
/// <returns></returns>
public async Task AddComponent<TComponent>(TElementHandler parent) where TComponent : IComponent
public async Task AddComponent<TComponent>(IElementHandler parent) where TComponent : IComponent
{
await AddComponent(typeof(TComponent), parent);
}
@ -44,12 +50,12 @@ namespace Emblazon
/// <param name="componentType"></param>
/// <param name="parent"></param>
/// <returns></returns>
public async Task AddComponent(Type componentType, TElementHandler parent)
public async Task AddComponent(Type componentType, IElementHandler parent)
{
var component = InstantiateComponent(componentType);
var componentId = AssignRootComponentId(component);
var rootAdapter = new EmblazonAdapter<TElementHandler>(this, closestPhysicalParent: parent, knownTargetControl: parent)
var rootAdapter = new EmblazonAdapter(this, closestPhysicalParent: parent, knownTargetControl: parent)
{
Name = "RootAdapter"
};
@ -111,9 +117,9 @@ namespace Emblazon
unregisterCallback();
}
internal EmblazonAdapter<TElementHandler> CreateAdapterForChildComponent(TElementHandler physicalParent, int componentId)
internal EmblazonAdapter CreateAdapterForChildComponent(IElementHandler physicalParent, int componentId)
{
var result = new EmblazonAdapter<TElementHandler>(this, physicalParent);
var result = new EmblazonAdapter(this, physicalParent);
_componentIdToAdapter[componentId] = result;
return result;
}