Add container resize logic so controls show up

- Also add Timer component
This commit is contained in:
Eilon Lipton 2019-08-06 08:27:36 -07:00
Родитель 9b64beb69c
Коммит 03a07eb7b1
6 изменённых файлов: 170 добавлений и 10 удалений

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

@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -11,7 +12,7 @@ namespace BlinForms.Framework
{
public class BlinFormsRenderer : Renderer
{
private Dictionary<int, Blontrol> _componentIdToControl = new Dictionary<int, Blontrol>();
private Dictionary<int, Blontrol> _componentIdToControl = new Dictionary<int, Blontrol>(); // TODO: Map to Control
public BlinFormsRenderer(IServiceProvider serviceProvider)
: base(serviceProvider, new LoggerFactory())
@ -27,7 +28,11 @@ namespace BlinForms.Framework
{
var component = InstantiateComponent(typeof(T));
var componentId = AssignRootComponentId(component);
var control = _componentIdToControl[componentId] = new Blontrol(this);
var control = _componentIdToControl[componentId] =
new Blontrol(this)
{
Size = new Size(500, 500),
};
RootForm.Controls.Add(control);
return RenderRootComponentAsync(componentId);
}

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

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using BlinForms.Framework.Controls;
using BlinForms.Framework.Elements;
@ -18,8 +19,6 @@ namespace BlinForms.Framework
public Blontrol(BlinFormsRenderer renderer)
{
_renderer = renderer;
Width = 500;
Height = 500;
}
internal void ApplyEdits(ArrayBuilderSegment<RenderTreeEdit> edits, ArrayRange<RenderTreeFrame> referenceFrames)
@ -34,12 +33,20 @@ namespace BlinForms.Framework
case RenderTreeEditType.SetAttribute:
ApplySetAttribute(edit.SiblingIndex, ref referenceFrames.Array[edit.ReferenceFrameIndex]);
break;
case RenderTreeEditType.RemoveFrame:
ApplyRemoveFrame(edit.SiblingIndex);
break;
default:
throw new NotImplementedException($"Not supported edit type: {edit.Type}");
}
}
}
private void ApplyRemoveFrame(int siblingIndex)
{
//Controls.RemoveAt(siblingIndex);
}
private void ApplySetAttribute(int siblingIndex, ref RenderTreeFrame attributeFrame)
{
var target = (IBlazorNativeControl)Controls[siblingIndex];
@ -51,11 +58,21 @@ namespace BlinForms.Framework
ref var frame = ref frames[frameIndex];
switch (frame.FrameType)
{
case RenderTreeFrameType.Element:
var elementControl = (Control)CreateNativeControlForElement(frames, frameIndex);
AddChildControl(siblingIndex, elementControl);
case RenderTreeFrameType.Element: // ... then this gets called to create the native component
var element = CreateNativeControlForElement(frames, frameIndex);
// Ignoring non-controls, such as Timer Component
if (element is Control elementControl)
{
AddChildControl(siblingIndex, elementControl);
}
else
{
Debug.WriteLine("Ignoring non-control child: " + element.GetType().FullName);
}
break;
case RenderTreeFrameType.Component:
case RenderTreeFrameType.Component: // ... first a bunch of these get created for each "thing"
{
var childControl = _renderer.CreateControlForChildComponent(frame.ComponentId);
AddChildControl(siblingIndex, childControl);
@ -67,6 +84,12 @@ namespace BlinForms.Framework
throw new NotImplementedException("Nonempty markup: " + frame.MarkupContent);
}
break;
case RenderTreeFrameType.Text:
if (!string.IsNullOrWhiteSpace(frame.TextContent))
{
throw new NotImplementedException("Nonempty text: " + frame.TextContent);
}
break;
default:
throw new NotImplementedException($"Not supported frame type: {frame.FrameType}");
}
@ -75,6 +98,8 @@ namespace BlinForms.Framework
private void AddChildControl(int siblingIndex, Control childControl)
{
Controls.Add(childControl);
// TODO: _childControls is never read from... only added to...
if (siblingIndex < _childControls.Count)
{
_childControls.Insert(siblingIndex, childControl);

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

@ -1,4 +1,5 @@
using System;
using System.Drawing;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.RenderTree;
@ -35,6 +36,30 @@ namespace BlinForms.Framework.Controls
};
}
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
if (Parent != null)
{
Parent.Location = Location;
Location = Point.Empty;
Parent.Size = Size;
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (Parent != null)
{
Parent.Location = Location;
Location = Point.Empty;
Parent.Size = Size;
}
}
public void ApplyAttribute(ref RenderTreeFrame attribute)
{
switch (attribute.AttributeName)

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

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.RenderTree;
using System;
using System.Drawing;
namespace BlinForms.Framework.Controls
{
@ -24,6 +25,30 @@ namespace BlinForms.Framework.Controls
{
}
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
if (Parent != null)
{
Parent.Location = Location;
Location = Point.Empty;
Parent.Size = Size;
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (Parent != null)
{
Parent.Location = Location;
Location = Point.Empty;
Parent.Size = Size;
}
}
public void ApplyAttribute(ref RenderTreeFrame attribute)
{
switch (attribute.AttributeName)

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

@ -0,0 +1,52 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.RenderTree;
using System.Windows.Forms;
namespace BlinForms.Framework.Controls
{
public class Timer : FormsComponentBase
{
static Timer()
{
Blontrol.KnownElements.Add(typeof(Timer).FullName, renderer => new BlazorTimer(renderer));
}
[Parameter] public EventCallback OnTick { get; set; }
protected override void RenderAttributes(RenderTreeBuilder builder)
{
builder.AddAttribute(1, "ontick", OnTick);
}
class BlazorTimer : System.Windows.Forms.Timer, IBlazorNativeControl
{
public ulong TickEventHandlerId { get; set; }
public BlazorTimer(BlinFormsRenderer renderer)
{
Interval = 1000;
Tick += (s, e) =>
{
if (TickEventHandlerId != default)
{
renderer.DispatchEventAsync(TickEventHandlerId, null, new UIEventArgs());
}
};
Enabled = true;
}
public void ApplyAttribute(ref RenderTreeFrame attribute)
{
switch (attribute.AttributeName)
{
case "ontick":
TickEventHandlerId = attribute.AttributeEventHandlerId;
break;
default:
//FormsComponentBase.ApplyAttribute(this, ref attribute);
break;
}
}
}
}
}

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

@ -1,11 +1,39 @@
@using BlinForms.Framework.Controls
<Button Top="50" Text="@("Click " + count)" OnClick="@IncrementCount" />
<Button Top="50" Width="300" Text="@("Increment: " + count)" OnClick="@IncrementCount" />
<Label Top="100" Width="200" Text="@("I'm a label: " + labelCount)"></Label>
<Timer OnTick="@OnTimerTick" />
<Button Top="200" Width="400" Text="@($"Toggle visible (visible={isDetailsVisible})")" OnClick="@ToggleVisible" />
@if (isDetailsVisible)
{
<Label Top="250" Width="200" Text="I am visible!"></Label>
}
else
{
<Label Top="300" Width="200" Text="Not visible..."></Label>
}
@code {
int count = 0;
int labelCount = 0;
bool isDetailsVisible;
void IncrementCount()
{
count++;
}
}
void ToggleVisible()
{
isDetailsVisible = !isDetailsVisible;
}
void OnTimerTick()
{
labelCount += 10;
}
}