class
UI::NativeView
- UI::NativeView
- Reference
- Object
Overview
A rendered native view tree node wrapping a platform-specific handle.
NativeView is the output of a platform renderer (AppKit, UIKit, Android).
It forms a tree structure mirroring the native view hierarchy, with each
node owning:
- A
NativeHandleto the platform object (NSView, UIView, Android View) - An array of child
NativeViewnodes - An array of callback IDs registered via
CallbackRegistry
Lifecycle States
Created -> Attached -> Detached -> TornDown
| ^
+----------------------+
(teardown! from Attached)
Created: The view has been constructed but not yet added to a parent.Attached: The view is part of an active native view hierarchy.Detached: The view was removed from its parent but not yet torn down. Resources are still held; the view could potentially be re-attached.TornDown: All resources have been released. The view is dead and must not be used.
Deterministic Cleanup
#teardown! performs post-order recursive cleanup:
- Tear down all children (depth-first, children before parent)
- Unregister all callbacks from
CallbackRegistry - Release the
NativeHandle - Transition to
TornDownstate
This ensures native child views are removed before their parent, which is required by both AppKit and Android view hierarchies.
Defined in:
ui/native/native_view.crConstructors
Instance Method Summary
-
#add_child(child : NativeView) : Nil
Add a child to this view's children array.
-
#attach! : Nil
Transition this view to the
Attachedstate. -
#children : Array(NativeView)
Ordered list of child native views.
-
#detach! : Nil
Transition this view to the
Detachedstate. -
#finalize
GC safety net.
-
#handle : NativeHandle
The native platform object handle.
-
#register_callback(callback : Proc(Nil)) : UInt64
Register a callback
Procin theCallbackRegistryand track its ID for cleanup during#teardown!. -
#register_callback(&block : -> Nil) : UInt64
Register a callback block and track its ID for cleanup during
#teardown!. -
#remove_all_children : Nil
Remove all children from this view's children array.
-
#remove_child(child : NativeView) : Bool
Remove a specific child from this view's children array.
-
#state : State
Current lifecycle state.
-
#teardown! : Nil
Perform post-order recursive cleanup of this view and all descendants.
-
#torn_down? : Bool
Returns
trueif the view has been torn down. -
#track_callback_id(id : UInt64) : UInt64
Track a callback ID that was registered directly against CallbackRegistry.
Constructor Detail
Instance Method Detail
Add a child to this view's children array.
Raises if this view has been torn down.
Transition this view to the Attached state.
Raises if the view is already torn down.
Transition this view to the Detached state.
Raises if the view is already torn down.
GC safety net. If the view was never explicitly torn down, attempt
to clean up. Always prefer calling #teardown! explicitly.
Register a callback Proc in the CallbackRegistry and track its
ID for cleanup during #teardown!.
Returns the callback ID for passing to the native side.
Register a callback block and track its ID for cleanup during #teardown!.
Remove all children from this view's children array.
Does NOT tear down the removed children -- the caller is responsible for that if the children are no longer needed.
Remove a specific child from this view's children array.
Returns true if the child was found and removed, false otherwise.
Does NOT tear down the removed child -- the caller is responsible for
that if the child is no longer needed.
Perform post-order recursive cleanup of this view and all descendants.
- Recursively tears down all children (depth-first)
- Clears the children array
- Unregisters all tracked callbacks from
CallbackRegistry - Releases the
NativeHandle - Transitions to
TornDownstate
This method is idempotent: calling it on an already-torn-down view is a safe no-op.
Track a callback ID that was registered directly against CallbackRegistry.
This is useful for typed callback registries that return IDs but do not
route through #register_callback.