diff --git a/Dashboard/Controls/UIBase.cs b/Dashboard/Controls/UIBase.cs index 2c9901b..d411718 100644 --- a/Dashboard/Controls/UIBase.cs +++ b/Dashboard/Controls/UIBase.cs @@ -1,5 +1,6 @@ using System; +using System.Collections.Generic; using Dashboard.ImmediateDraw; using OpenTK.Mathematics; @@ -8,7 +9,7 @@ namespace Dashboard.Controls /// /// Bases for all UI elements. /// - 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 Userdata { get; } = new Dictionary(); + 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 diff --git a/Dashboard/IDbUserdata.cs b/Dashboard/IDbUserdata.cs new file mode 100644 index 0000000..d27f8a7 --- /dev/null +++ b/Dashboard/IDbUserdata.cs @@ -0,0 +1,21 @@ + +using System; +using System.Collections.Generic; + +namespace Dashboard +{ + /// + /// Common interface for Dashboard objects that accept userdata. + /// + /// + /// 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. + /// + /// Userdata dictionary. + /// + public Dictionary Userdata { get; } + } +} \ No newline at end of file