TermUI.SpatialIndex (TermUI v0.2.0)
View SourceSpatial index for fast component lookup by screen position.
The spatial index enables efficient routing of mouse events to the correct component based on cursor coordinates. It maintains a mapping of screen regions to component references.
Usage
# Register a component's bounds
SpatialIndex.update(:my_button, pid, %{x: 10, y: 5, width: 20, height: 3})
# Find component at position
{:ok, {:my_button, pid}} = SpatialIndex.find_at(15, 6)
# Remove when unmounted
SpatialIndex.remove(:my_button)Z-Order
When components overlap, the one with the highest z-index receives mouse events. Default z-index is 0. Modals typically use higher values.
Summary
Functions
Returns a specification to start this module under a supervisor.
Clears all entries from the index.
Returns the number of indexed components.
Finds all components at the given coordinates.
Finds the component at the given coordinates.
Gets the bounds for a component.
Removes a component from the index.
Starts the spatial index.
Updates a component's bounds in the index.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear() :: :ok
Clears all entries from the index.
@spec count() :: non_neg_integer()
Returns the number of indexed components.
Finds all components at the given coordinates.
Returns all overlapping components sorted by z-index (highest first).
Finds the component at the given coordinates.
Returns the topmost component (highest z-index) at the position.
Returns
{:ok, {id, pid}}- Component found{:error, :not_found}- No component at position
Gets the bounds for a component.
@spec remove(term()) :: :ok
Removes a component from the index.
@spec start_link(keyword()) :: GenServer.on_start()
Starts the spatial index.
Updates a component's bounds in the index.
Parameters
id- Component identifierpid- Component processbounds- Map with x, y, width, heightopts- Options including:z_index
Examples
SpatialIndex.update(:button, pid, %{x: 0, y: 0, width: 10, height: 1})
SpatialIndex.update(:modal, pid, bounds, z_index: 100)