View Source Scenic.Components (Scenic v0.11.2)

about-components

About Components

Components are small scenes that are managed, by another scene. They are useful for reusing bits of UI and containing the logic that runs them.

helper-functions

Helper Functions

This module contains a set of helper functions to make it easy to add, or modify, the standard components.

In general, each helper function is of the form:

def name_of_component(graph, data, options \\ [])

Unlike primitives, components are scenes in themselves. Each component is run by a GenServer and adding a basic component does two things.

  1. A new component GenServer is started and supervised by the owning scene's dynamic scene supervisor.
  2. A reference to the new scene is added to the graph.

This doesn't happen all at once. These helper functions simply add a reference to a to-be-started component to your graph. When you push a graph, the ViewPort then manages the life cycle of the components.

When adding components to a graph, each helper function accepts a graph as the first parameter and returns the transformed graph. This makes is very easy to build a complex graph by piping helper functions together.

@graph Graph.build()
|> button("Press Me", id: :sample_button)

When modifying a graph, you can again use the helpers by passing in the component to be modified. The transformed component will be returned.

Graph.modify(graph, :sample_button, fn(p) ->
  button(p, "Continue")
end)

# or, more compactly...

Graph.modify(graph, :sample_button, &button(&1, "Continue"))

In each case, the second parameter is a data term that is specific to the component being acted on. See the documentation below. If you pass in invalid data for the second parameter an error will be thrown along with some explanation of what it expected.

The third parameter is a keyword list of options that are to be applied to the component. This includes setting the id, styles, transforms and such.

@graph Graph.build()
|> button("Press Me", id: :sample_button, rotate: 0.4)

Link to this section Summary

Functions

Generate an uninstantiated button spec, parallel to the concept of primitive specs. This allows buttons to be treated as data.

Generate an uninstantiated checkbox spec, parallel to the concept of primitive specs. See Components.checkbox for data and options values.

Generate an uninstantiated dropdown spec, parallel to the concept of primitive specs. See Components.dropdown for data and options values.

Generate an uninstantiated radio_group spec, parallel to the concept of primitive specs. See Components.radio_group for data and options values.

Generate an uninstantiated slider spec, parallel to the concept of primitive specs. See Components.slider for data and options values.

Generate an uninstantiated text_field spec, parallel to the concept of primitive specs. See Components.text_field for data and options values.

Generate an uninstantiated toggle spec, parallel to the concept of primitive specs. See Components.toggle for data and options values.

Link to this section Functions

Link to this function

button(graph, title, options \\ [])

View Source
@spec button(
  source :: Scenic.Graph.t() | Scenic.Primitive.t(),
  title :: String.t(),
  options :: list()
) :: Scenic.Graph.t() | Scenic.Primitive.t()

Add a Button to a graph

A button is a small scene that is pretty much just some text drawn over a rounded rectangle. The button scene contains logic to detect when the button is pressed, tracks it as the pointer moves around, and when it is released.

data

Data

title

  • title - a bitstring describing the text to show in the button

messages

Messages

If a button press is successful, it sends an event message to the host scene in the form of:

{:click, id}

styles

Styles

Buttons honor the following standard styles

  • :hidden - If false the component is rendered. If true, it is skipped. The default is false.
  • :theme - The color set used to draw. See below. The default is :primary

additional-styles

Additional Styles

Buttons honor the following list of additional styles.

  • :width - pass in a number to set the width of the button.
  • :height - pass in a number to set the height of the button.
  • :radius - pass in a number to set the radius of the button's rounded rectangle.
  • :alignment - set the alignment of the text inside the button. Can be one of :left, :right, :center. The default is :center.
  • :button_font_size - the size of the font in the button

Buttons do not use the inherited :font_size style as they should look consistent regardless of what size the surrounding text is.

theme

Theme

Buttons work well with the following predefined themes: :primary, :secondary, :success, :danger, :warning, :info, :text, :light, :dark

To pass in a custom theme, supply a map with at least the following entries:

  • :text - the color of the text in the button
  • :background - the normal background of the button
  • :active - the background while the button is pressed

examples

Examples

The following example creates a simple button and positions it on the screen.

graph
|> button("Example", id: :button_id, translate: {20, 20})

The next example makes the same button as before, but colors it as a warning button. See the options list above for more details.

graph
|> button("Example", id: :button_id, translate: {20, 20}, theme: :warning)
Link to this function

button_spec(data, options)

View Source

Generate an uninstantiated button spec, parallel to the concept of primitive specs. This allows buttons to be treated as data.

Link to this function

checkbox(graph, data, options \\ [])

View Source
@spec checkbox(
  source :: Scenic.Graph.t() | Scenic.Primitive.t(),
  data :: {String.t(), boolean()},
  options :: list()
) :: Scenic.Graph.t() | Scenic.Primitive.t()

Add a Checkbox to a graph

data

Data

{text, checked?}

  • text - must be a bitstring
  • checked? - must be a boolean and indicates if the checkbox is set.

messages

Messages

When the state of the checkbox changes, it sends an event message to the parent scene in the form of:

{:value_changed, id, checked?}

styles

Styles

Buttons honor the following standard styles

  • :hidden - If false the component is rendered. If true, it is skipped. The default is false.
  • :theme - The color set used to draw. See below. The default is :dark

theme

Theme

Checkboxes work well with the following predefined themes: :light, :dark

To pass in a custom theme, supply a map with at least the following entries:

  • :text - the color of the text in the button
  • :background - the background of the box
  • :border - the border of the box
  • :active - the border of the box while the button is pressed
  • :thumb - the color of the check mark itself

examples

Examples

The following example creates a checkbox and positions it on the screen.

graph
|> checkbox({"Example", true}, id: :checkbox_id, translate: {20, 20})
Link to this function

checkbox_spec(data, options)

View Source

Generate an uninstantiated checkbox spec, parallel to the concept of primitive specs. See Components.checkbox for data and options values.

Link to this function

radio_group(graph, data, options \\ [])

View Source
@spec radio_group(
  source :: Scenic.Graph.t() | Scenic.Primitive.t(),
  data :: [{String.t(), any()} | {String.t(), any(), boolean()}],
  options :: list()
) :: Scenic.Graph.t() | Scenic.Primitive.t()

Add a RadioGroup to a graph

data

Data

{radio_buttons, selected_id}

  • radio_buttons must be a list of radio button data. See below.

Radio button data:

{text, radio_id}

  • text - must be a bitstring
  • radio_id - can be any term you want. It will be passed back to you as the group's value.

messages

Messages

When the state of the radio group changes, it sends an event message to the host scene in the form of:

{:value_changed, id, radio_id}

options

Options

Radio Buttons honor the following list of options.

  • :theme - This sets the color scheme of the button. This can be one of pre-defined button schemes :light, :dark, or it can be a completely custom scheme like this: {text_color, box_background, border_color, pressed_color, checkmark_color}.

styles

Styles

Radio Buttons honor the following styles

  • :hidden - If false the component is rendered. If true, it is skipped. The default is false.
  • :theme - The color set used to draw. See below. The default is :dark

theme

Theme

Radio buttons work well with the following predefined themes: :light, :dark

To pass in a custom theme, supply a map with at least the following entries:

  • :text - the color of the text
  • :background - the background of the component
  • :border - the border of the component
  • :active - the background of the circle while the button is pressed
  • :thumb - the color of inner selected-mark

examples

Examples

The following example creates a radio group and positions it on the screen.

graph
|> radio_group([{
    {"Radio A", :radio_a},
    {"Radio B", :radio_b},
    {"Radio C", :radio_c},
  ], :radio_b}, id: :radio_group, translate: {20, 20})
Link to this function

radio_group_spec(data, options)

View Source

Generate an uninstantiated radio_group spec, parallel to the concept of primitive specs. See Components.radio_group for data and options values.

Link to this function

slider(graph, data, options \\ [])

View Source
@spec slider(
  source :: Scenic.Graph.t() | Scenic.Primitive.t(),
  data :: {{number(), number()}, number()} | list(),
  options :: list()
) :: Scenic.Graph.t() | Scenic.Primitive.t()

Add a Slider to a graph

data

Data

{ extents, initial_value}

  • extents gives the range of values. It can take several forms...
    • {min, max} If min and max are integers, then the slider value will be an integer.
    • {min, max} If min and max are floats, then the slider value will be an float.
    • [a, b, c] A list of terms. The value will be one of the terms
  • initial_value Sets the initial value (and position) of the slider. It must make sense with the extents you passed in.

messages

Messages

When the state of the slider changes, it sends an event message to the host scene in the form of:

{:value_changed, id, value}

options

Options

Sliders honor the following list of options.

styles

Styles

Sliders honor the following styles

  • :hidden - If false the component is rendered. If true, it is skipped. The default is false.
  • :theme - The color set used to draw. See below. The default is :dark

theme

Theme

Sliders work well with the following predefined themes: :light, :dark

To pass in a custom theme, supply a map with at least the following entries:

  • :border - the color of the slider line
  • :thumb - the color of slider thumb

examples

Examples

The following example creates a numeric slider and positions it on the screen.

graph
|> slider({{0,100}, 0}, id: :num_slider, translate: {20,20})

The following example creates a list slider and positions it on the screen.

graph
|> slider({[
    :white,
    :cornflower_blue,
    :green,
    :chartreuse
  ], :cornflower_blue}, id: :slider_id, translate: {20,20})
Link to this function

slider_spec(data, options)

View Source

Generate an uninstantiated slider spec, parallel to the concept of primitive specs. See Components.slider for data and options values.

Link to this function

text_field(graph, data, options \\ [])

View Source
@spec text_field(
  source :: Scenic.Graph.t() | Scenic.Primitive.t(),
  data :: String.t(),
  options :: list()
) :: Scenic.Graph.t() | Scenic.Primitive.t()

Add a TextField input to a graph

data

Data

initial_value

  • initial_value - is the string that will be the starting value

messages

Messages

When the text in the field changes, it sends an event message to the host scene in the form of:

{:value_changed, id, value}

styles

Styles

Text fields honor the following styles

  • :hidden - If false the component is rendered. If true, it is skipped. The default is false.
  • :theme - The color set used to draw. See below. The default is :dark

additional-styles

Additional Styles

Text fields honor the following list of additional styles.

  • :filter - Adding a filter option restricts which characters can be entered into the text_field component. The value of filter can be one of:
    • :all - Accept all characters. This is the default
    • :number - Any characters from "0123456789.,"
    • "filter_string" - Pass in a string containing all the characters you will accept
    • function/1 - Pass in an anonymous function. The single parameter will be the character to be filtered. Return true or false to keep or reject it.
  • :hint - A string that will be shown (greyed out) when the entered value of the component is empty.
  • :type - Can be one of the following options:
    • :all - Show all characters. This is the default.
    • :password - Display a string of '*' characters instead of the value.
  • :width - set the width of the control.

theme

Theme

Text fields work well with the following predefined themes: :light, :dark

To pass in a custom theme, supply a map with at least the following entries:

  • :text - the color of the text
  • :background - the background of the component
  • :border - the border of the component
  • :focus - the border while the component has focus

examples

Examples

graph
|> text_field("Sample Text", id: :text_id, translate: {20,20})

graph
|> text_field(
  "", id: :pass_id, type: :password, hint: "Enter password", translate: {20,20}
)
Link to this function

text_field_spec(data, options)

View Source

Generate an uninstantiated text_field spec, parallel to the concept of primitive specs. See Components.text_field for data and options values.

Link to this function

toggle(graph, data, options \\ [])

View Source
@spec toggle(Scenic.Graph.t() | Scenic.Primitive.t(), boolean(), Keyword.t() | nil) ::
  Scenic.Graph.t()

Add Toggle to a Scenic graph.

data

Data

on?

  • on? - true if the toggle is on, pass false if not.

styles

Styles

Toggles honor the following styles. The :light and :dark styles look nice. The other bundled themes...not so much. You can also supply your own theme.

  • :hidden - If false the toggle is rendered. If true, it is skipped. The default is false.
  • :theme - The color set used to draw. See below. The default is :dark

additional-styles

Additional Styles

Toggles also honor the following additional styles.

  • :border_width - the border width. Defaults to 2.
  • :padding - the space between the border and the thumb. Defaults to 2
  • :thumb_radius - the radius of the thumb. This determines the size of the entire toggle. Defaults to 10.

theme

Theme

To pass in a custom theme, supply a map with at least the following entries:

  • :border - the color of the border around the toggle
  • :background - the color of the track when the toggle is off.
  • :text - the color of the thumb.
  • :thumb - the color of the track when the toggle is on.

Optionally, you can supply the following entries:

  • :thumb_pressed - the color of the thumb when pressed. Defaults to :gainsboro.

examples

Examples

The following example creates a toggle.

graph
|> toggle(true, translate: {20, 20})

The next example makes a larger toggle.

graph
|> toggle(true, translate: {20, 20}, thumb_radius: 14)
Link to this function

toggle_spec(data, options)

View Source

Generate an uninstantiated toggle spec, parallel to the concept of primitive specs. See Components.toggle for data and options values.