Drafter.Runtime.AppLoop (drafter v0.5.0)

Copy Markdown View Source

The Drafter application event loop.

Owns the receive loop and all message dispatch. Rendering is delegated to Drafter.Runtime.Renderer.

Summary

Functions

Turns the calling process into the application loop, returning when the app stops.

Set the local terminal up, then mount the app and run the loop until it stops.

Apply the focus and blur the app has asked for so far to hierarchy, in the order asked, before anything else the process has been sent is looked at.

Mount an app, run its ready hook, and enter the event loop for screen_rect.

Functions

enter_loop(app_module, app_state, screen_rect, timers \\ %{}, widget_hierarchy \\ nil, opts \\ [])

@spec enter_loop(module(), term(), map(), map(), map() | nil, keyword()) :: :ok

Turns the calling process into the application loop, returning when the app stops.

Registers the caller with Drafter.AppRegistry and applies :refresh_rate from opts, then receives and dispatches events until the application returns {:stop, reason} or the session shuts down. The message :drafter_end_session sent to the loop's process stops its app and every app beneath it, so the call returns.

Remote sessions call this directly. A local terminal goes through run/2, which sets the terminal up first.

timers maps a timer id to the :timer reference already running for it, widget_hierarchy is the hierarchy from a prior render or nil for none.

Options

Unlike start/3, this function does not mount the app, so :props is not read here.

run(app_module, opts \\ [])

@spec run(
  module(),
  keyword()
) :: :ok

Set the local terminal up, then mount the app and run the loop until it stops.

Registers with the theme, skin and screen managers, drains any stale input, clears the screen and sizes the compositor to the terminal, then hands off to start/3 with a screen rect covering the whole terminal.

Options

Takes the same options as start/3.

settle_focus(hierarchy)

@spec settle_focus(map() | nil) :: map() | nil

Apply the focus and blur the app has asked for so far to hierarchy, in the order asked, before anything else the process has been sent is looked at.

For the render after on_ready/1: what Drafter.focus/1 and Drafter.blur/1 asked for there takes effect in that render, whatever input arrived meanwhile.

start(app_module, screen_rect, opts \\ [])

@spec start(module(), map(), keyword()) :: :ok

Mount an app, run its ready hook, and enter the event loop for screen_rect.

This is everything run/2 does once the terminal has been set up; a headless run enters here directly. Returns when the loop returns.

screen_rect is a %{x: x, y: y, width: w, height: h} map. The app is rendered once before its ready hook and once after, so intervals registered during the hook are running before the first event is taken.

Options