Drafter.Style.Computed (drafter v0.3.1)

Copy Markdown View Source

Resolves a widget's final style: stylesheet cascade, then inline overrides, then theme colour lookup.

for_widget/3 and for_part/4 each build a match context from the widget's type and state, run it through a stylesheet, merge the caller's inline style on top, and resolve the colour-valued keys against the current theme. to_segment_style/1 then converts the result into the shape Drafter.Draw.Segment expects.

The match context

The context is built from the widget's state map, reading :focused, :hovered, :active, :disabled, :checked and :selected, each defaulting to false when the state does not carry it, plus :expanded. A state that is not a map contributes false for every one of them. :widget_type comes from the first argument and :id and :classes from the options.

Colour resolution

:color, :background and :border_color are passed through Drafter.Style.resolve_color/2 with the theme from Drafter.ThemeManager.get_current_theme/0, so a theme token such as :primary becomes an RGB triple. No other key is resolved, and in particular the :fg and :bg keys to_segment_style/1 produces are never theme-resolved.

Because the theme is read from the session's theme manager, for_widget/3 and for_part/4 need a running session; only to_segment_style/1 is usable on its own.

Summary

Functions

The resolved style for the part sub-region of a widget of widget_type.

The resolved style for a widget of widget_type in state.

Convert a computed style into a Drafter.Draw.Segment.style/0 map.

Types

context()

@type context() :: map()

The map handed to Drafter.Style.Selector.matches?/2.

Functions

for_part(widget_type, state, part, opts \\ [])

@spec for_part(atom(), map() | struct() | term(), atom(), keyword()) ::
  Drafter.Style.t()

The resolved style for the part sub-region of a widget of widget_type.

As for_widget/3, with :part set to part in the match context so that ::part selectors apply, and with one more layer merged on top.

Options are those of for_widget/3, plus:

  • :part_styles — map of %{part => style} whose entry for part is merged last, over both the stylesheet result and :style. Default %{}, and a part with no entry contributes nothing.

    Drafter.Style.Computed.for_part(:text_input, %{focused: true}, :border)

for_widget(widget_type, state, opts \\ [])

@spec for_widget(atom(), map() | struct() | term(), keyword()) :: Drafter.Style.t()

The resolved style for a widget of widget_type in state.

Options:

Passing :stylesheet makes :app_module unused.

Drafter.Style.Computed.for_widget(:button, %{focused: true}, classes: [:primary])

to_segment_style(computed_style)

@spec to_segment_style(Drafter.Style.t() | map()) :: Drafter.Draw.Segment.style()

Convert a computed style into a Drafter.Draw.Segment.style/0 map.

:fg is taken from the style's :fg, or from :color when there is no :fg; :bg from :bg, or from :background. :bold, :dim, :italic, :underline and :reverse are copied across under the same names. Every other key is dropped, and a key whose value is nil is left out rather than set to nil. A key set to false is kept.

Examples

iex> Drafter.Style.Computed.to_segment_style(%{color: {1, 2, 3}, bold: true, padding: 2})
%{bold: true, fg: {1, 2, 3}}

iex> Drafter.Style.Computed.to_segment_style(%{fg: {1, 2, 3}, color: {9, 9, 9}})
%{fg: {1, 2, 3}}

iex> Drafter.Style.Computed.to_segment_style(%{})
%{}