diff --git a/CHANGELOG.md b/CHANGELOG.md
index d8dda3e..8d505d6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,7 @@ Fixes
Additions
- Added some extension methods for querying Anchor types
- Added Element.AutoSizeAddedAbsolute to allow for more granular control of auto-sizing
+- Added Element.OnAddedToUi and Element.OnRemovedFromUi
Improvements
- Allow elements to auto-adjust their size even when their children are aligned oddly
diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs
index ccf3748..4e168f0 100644
--- a/MLEM.Ui/Elements/Element.cs
+++ b/MLEM.Ui/Elements/Element.cs
@@ -407,14 +407,22 @@ namespace MLEM.Ui.Elements {
///
public GamepadNextElementCallback GetGamepadNextElement;
///
- /// Event that is called when a child is added to this element using
+ /// Event that is called when a child or any level of grandchild is added to this element using
///
public OtherElementCallback OnChildAdded;
///
- /// Event that is called when a child is removed from this element using
+ /// Event that is called when a child or any level of grandchild is removed from this element using
///
public OtherElementCallback OnChildRemoved;
///
+ /// Event that is called when this element is added to a , that is, when this element's is set to a non- value.
+ ///
+ public GenericCallback OnAddedToUi;
+ ///
+ /// Event that is called when this element is removed from a , that is, when this element's is set to .
+ ///
+ public GenericCallback OnRemovedFromUi;
+ ///
/// Event that is called when this element's method is called, which also happens in .
/// This event is useful for unregistering global event handlers when this object should be destroyed.
///
@@ -497,6 +505,7 @@ namespace MLEM.Ui.Elements {
element.AndChildren(e => {
e.Root = this.Root;
e.System = this.System;
+ e.OnAddedToUi?.Invoke(e);
this.Root?.InvokeOnElementAdded(e);
this.OnChildAdded?.Invoke(this, e);
});
@@ -520,6 +529,7 @@ namespace MLEM.Ui.Elements {
element.AndChildren(e => {
e.Root = null;
e.System = null;
+ e.OnRemovedFromUi?.Invoke(e);
this.Root?.InvokeOnElementRemoved(e);
this.OnChildRemoved?.Invoke(this, e);
});
diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs
index 2f4fd32..b9d78e7 100644
--- a/MLEM.Ui/UiSystem.cs
+++ b/MLEM.Ui/UiSystem.cs
@@ -347,6 +347,7 @@ namespace MLEM.Ui {
root.Element.AndChildren(e => {
e.Root = root;
e.System = this;
+ e.OnAddedToUi?.Invoke(e);
root.InvokeOnElementAdded(e);
e.SetAreaDirty();
});
@@ -369,6 +370,7 @@ namespace MLEM.Ui {
root.Element.AndChildren(e => {
e.Root = null;
e.System = null;
+ e.OnRemovedFromUi?.Invoke(e);
root.InvokeOnElementRemoved(e);
e.SetAreaDirty();
});