Add userdata fields to all control hierarchy.

This commit is contained in:
H. Utku Maden 2024-07-30 21:54:18 +03:00
parent 50552914c1
commit 9b6a15c6ec
2 changed files with 48 additions and 1 deletions

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Dashboard.ImmediateDraw;
using OpenTK.Mathematics;
@ -8,7 +9,7 @@ namespace Dashboard.Controls
/// <summary>
/// Bases for all UI elements.
/// </summary>
public abstract class UIBase
public abstract class UIBase : IDbUserdata
{
private Vector2 size;
@ -59,6 +60,8 @@ namespace Dashboard.Controls
public bool IsMaximumSizeSet => MaximumSize != new Vector2(-1, -1);
public bool IsMinimumSizeSet => MinimumSize != new Vector2(-1, -1);
public Dictionary<string, object> Userdata { get; } = new Dictionary<string, object>();
public virtual void NotifyEvent(object? sender, EventArgs args)
{
}
@ -87,6 +90,29 @@ namespace Dashboard.Controls
{
Resized?.Invoke(sender, ea);
}
public bool IsDisposed { get; private set; } = false;
protected virtual void Dispose(bool disposing)
{
foreach (object userdata in Userdata.Values)
{
if (userdata is IDisposable disposable)
disposable.Dispose();
}
}
protected void DisposeInvoker(bool disposing)
{
if (IsDisposed)
return;
IsDisposed = true;
Dispose(disposing);
}
public void Dispose() => DisposeInvoker(true);
}
public class ResizedEventArgs : EventArgs

21
Dashboard/IDbUserdata.cs Normal file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
namespace Dashboard
{
/// <summary>
/// Common interface for Dashboard objects that accept userdata.
/// </summary>
/// <remarks>
/// Dashboard will call dispose on any and all objects which implement IDisposable.
/// If this is an issue, please guard your object against this using a wrapper.
/// </remarks
public interface IDbUserdata : IDisposable
{
/// <summary>
/// Userdata dictionary.
/// </summary>
public Dictionary<string, object> Userdata { get; }
}
}