TermUI.Event.Transformation (TermUI v0.2.0)
View SourceEvent transformation utilities.
Transforms events as they route to components, including:
- Coordinate transformation (screen to component-local)
- Event metadata enrichment
- Event filtering
Usage
# Transform mouse coordinates to component-local
local_event = Transformation.to_local(event, component_bounds)
# Add metadata to event
enriched = Transformation.with_metadata(event, %{target: :button})
Summary
Functions
Creates a standard event envelope with routing metadata.
Filters a list of events based on criteria.
Gets metadata from an event.
Checks if an event matches a filter.
Transforms screen coordinates to component-local coordinates.
Transforms component-local coordinates back to screen coordinates.
Adds metadata to an event.
Functions
Creates a standard event envelope with routing metadata.
Parameters
event- The raw eventopts- Options::source- Source of the event:target- Target component id:timestamp- Override timestamp
Returns
Event with envelope metadata.
Filters a list of events based on criteria.
Parameters
events- List of eventsfilters- Filter criteria (seematches?/2)
Returns
List of events matching all filters.
Gets metadata from an event.
Parameters
event- The eventkey- Metadata key to getdefault- Default value if key not found
Returns
The metadata value or default.
Checks if an event matches a filter.
Filter Options
:type- Event type (:key, :mouse, :focus, :custom):key- Specific key (for key events):action- Specific action (for mouse/focus events):button- Specific button (for mouse events):modifiers- Required modifiers (any or all):modifiers_all- All modifiers must be present:modifiers_any- Any modifier must be present
Example
# Match Ctrl+C
matches?(event, type: :key, key: :c, modifiers_all: [:ctrl])
# Match any click
matches?(event, type: :mouse, action: :click)
@spec to_local(TermUI.Event.Mouse.t() | term(), map()) :: TermUI.Event.Mouse.t() | term()
Transforms screen coordinates to component-local coordinates.
For mouse events, subtracts the component's position from the event coordinates so the component receives coordinates relative to its own origin (0, 0).
Parameters
event- Mouse event with screen coordinatesbounds- Component bounds with x, y position
Returns
Event with transformed coordinates, or unchanged event if not a mouse event.
Example
event = %Mouse{x: 15, y: 10, ...}
bounds = %{x: 10, y: 5, width: 20, height: 10}
local = to_local(event, bounds)
# local.x = 5, local.y = 5
@spec to_screen(TermUI.Event.Mouse.t() | term(), map()) :: TermUI.Event.Mouse.t() | term()
Transforms component-local coordinates back to screen coordinates.
Inverse of to_local/2.
Parameters
event- Mouse event with local coordinatesbounds- Component bounds with x, y position
Returns
Event with screen coordinates.
Adds metadata to an event.
Creates or updates a :metadata field on the event struct.
Parameters
event- The event to enrichmetadata- Map of metadata to add
Returns
Event with metadata merged.
Example
event = with_metadata(key_event, %{target: :input, phase: :bubble})