Browse Source

Add Element::Remove

Michael Ragazzon 1 month ago
parent
commit
23d14c881e
2 changed files with 14 additions and 0 deletions
  1. 4 0
      Include/RmlUi/Core/Element.h
  2. 10 0
      Source/Core/Element.cpp

+ 4 - 0
Include/RmlUi/Core/Element.h

@@ -553,6 +553,10 @@ public:
 	/// @return True if the provided element is a descendant of this element, false otherwise.
 	bool Contains(Element* element) const;
 
+	/// Removes this element from its parent, if it has one.
+	/// @return A unique pointer to the removed element, discard the result to immediately destroy.
+	ElementPtr Remove();
+
 	//@}
 
 	/**

+ 10 - 0
Source/Core/Element.cpp

@@ -1458,6 +1458,16 @@ bool Element::Contains(Element* element) const
 	return false;
 }
 
+ElementPtr Element::Remove()
+{
+	if (Node* parent = GetParentNode())
+	{
+		if (NodePtr self = parent->RemoveChild(As<Node*>(this)))
+			return As<ElementPtr>(std::move(self));
+	}
+	return nullptr;
+}
+
 EventDispatcher* Element::GetEventDispatcher() const
 {
 	return &meta->event_dispatcher;