class UI::NativeView

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:

Lifecycle States

Created -> Attached -> Detached -> TornDown
               |                      ^
               +----------------------+
                 (teardown! from Attached)

Deterministic Cleanup

#teardown! performs post-order recursive cleanup:

  1. Tear down all children (depth-first, children before parent)
  2. Unregister all callbacks from CallbackRegistry
  3. Release the NativeHandle
  4. Transition to TornDown state

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.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(handle : NativeHandle, children : Array(NativeView) = [] of NativeView) #

[View source]

Instance Method Detail

def add_child(child : NativeView) : Nil #

Add a child to this view's children array.

Raises if this view has been torn down.


[View source]
def attach! : Nil #

Transition this view to the Attached state.

Raises if the view is already torn down.


[View source]
def children : Array(NativeView) #

Ordered list of child native views.


[View source]
def detach! : Nil #

Transition this view to the Detached state.

Raises if the view is already torn down.


[View source]
def finalize #

GC safety net. If the view was never explicitly torn down, attempt to clean up. Always prefer calling #teardown! explicitly.


[View source]
def handle : NativeHandle #

The native platform object handle.


[View source]
def register_callback(callback : Proc(Nil)) : UInt64 #

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.


[View source]
def register_callback(&block : -> Nil) : UInt64 #

Register a callback block and track its ID for cleanup during #teardown!.


[View source]
def remove_all_children : Nil #

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.


[View source]
def remove_child(child : NativeView) : Bool #

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.


[View source]
def state : State #

Current lifecycle state.


[View source]
def teardown! : Nil #

Perform post-order recursive cleanup of this view and all descendants.

  1. Recursively tears down all children (depth-first)
  2. Clears the children array
  3. Unregisters all tracked callbacks from CallbackRegistry
  4. Releases the NativeHandle
  5. Transitions to TornDown state

This method is idempotent: calling it on an already-torn-down view is a safe no-op.


[View source]
def torn_down? : Bool #

Returns true if the view has been torn down.


[View source]
def track_callback_id(id : UInt64) : UInt64 #

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.


[View source]