Hi.WinForm/Disp/RenderingForm.cs
2025-04-14 17:03:37 +08:00

104 lines
3.9 KiB
C#

using Hi.Disp;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
namespace Hi.WinForm.Disp
{
/// <summary>
/// A <see cref="Form"/> contains <see cref="Disp.RenderingCanvas"/>.
/// This class is usually used for debug due to its simplicity.
/// </summary>
public partial class RenderingForm : Form, IGetDispEngine
{
static readonly Form seedForm = new Form();
private static readonly ConcurrentDictionary<string, RenderingForm> displayerMap
= new ConcurrentDictionary<string, RenderingForm>(4, 4);
/// <summary>
/// See <see cref="Call(string, IDisplayee[])"/> to get the information.
/// </summary>
public static ConcurrentDictionary<string, RenderingForm> DisplayerMap { get => displayerMap; }
/// <summary>
/// Ctor.
/// </summary>
/// <param name="displayees">displayees</param>
internal RenderingForm(params IDisplayee[] displayees)
{
InitializeComponent();
RenderingCanvas = new RenderingCanvas(displayees);
this.Controls.Add(RenderingCanvas);
//Displayer.DispEngine.Start();
}
/// <summary>
/// The contained <see cref="Disp.RenderingCanvas"/>.
/// </summary>
public RenderingCanvas RenderingCanvas { get; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IDisplayee Displayee
{
get => GetDispEngine().Displayee;
set
{
var preDisplayee = GetDispEngine().Displayee;
GetDispEngine().Displayee = value;
if (preDisplayee == null)
RenderingCanvas.DispEngine.SetViewToHomeView();
}
}
/// <summary>
/// Create and obtain a <see cref="RenderingForm"/> if the key has not existed; Otherwise, the old one is obtained.
/// <paramref name="displayees"/> are set to the obtained <see cref="RenderingForm"/>.
/// The dictionary of this function is <see cref="DisplayerMap"/>.
/// </summary>
/// <param name="key">key</param>
/// <param name="displayees">The displayees set to the obtained <see cref="RenderingForm"/>.</param>
/// <returns>A <see cref="RenderingForm"/> obtained by the key.</returns>
public static RenderingForm Call(string key, params IDisplayee[] displayees)
{
if (displayerMap.TryGetValue(key, out RenderingForm f))
{
f.RenderingCanvas.DispEngine.Displayee = new DispList(displayees);
return f;
}
else
{
_ = seedForm.Handle;
seedForm.Invoke(new Action(() =>
{
RenderingForm ff = new RenderingForm(displayees)
{
Text = key,
Visible = true
};
displayerMap.TryAdd(key, ff);
}));
RenderingForm fff = null;
while (!displayerMap.TryGetValue(key, out fff))
Thread.Sleep(1);
return fff;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
RenderingCanvas.Dispose();
components.Dispose();
}
base.Dispose(disposing);
}
/// <inheritdoc/>
public DispEngine GetDispEngine()
{
return RenderingCanvas.DispEngine;
}
}
}