Toddy.Iced.StyleMap (Toddy v0.3.0)

Copy Markdown View Source

Per-instance widget styling that crosses the IPC boundary as a plain map.

A StyleMap lets you override visual properties (background, text color, border, shadow) on individual widget instances without defining a named style. Status overrides (hovered, pressed, disabled, focused) apply partial style changes for those interaction states.

The background field accepts either a solid color (hex string or named atom) or a Toddy.Iced.Gradient for gradient fills.

Wire format

%{
  "background" => "#ff0000",
  "text_color" => "#ffffff",
  "border" => %{"color" => "#000000", "width" => 1, "radius" => 4},
  "hovered" => %{"background" => "#cc0000"},
  "pressed" => %{"background" => "#990000"}
}

Example

style = Toddy.Iced.StyleMap.new()
        |> Toddy.Iced.StyleMap.background("#3366ff")
        |> Toddy.Iced.StyleMap.text_color(:white)
        |> Toddy.Iced.StyleMap.hovered(%{background: "#5588ff"})

Button.new("btn", "Click me", style: style)

Gradient backgrounds work the same way:

gradient = Toddy.Iced.Gradient.linear(90, [{0.0, "#ff0000"}, {1.0, "#0000ff"}])

style = Toddy.Iced.StyleMap.new()
        |> Toddy.Iced.StyleMap.background(gradient)

Container.new("ctr", [], style: style)

Summary

Types

Partial style override for an interaction state.

t()

Functions

Sets the background. Accepts a color (any form Color.cast/1 supports) or a Gradient.

Sets the base preset to extend from instead of starting from the widget default.

Sets the border specification.

Sets the disabled status override. Accepts a map or keyword list.

Sets the focused status override. Accepts a map or keyword list.

Sets the hovered status override. Accepts a map or keyword list.

Creates an empty style map.

Sets the pressed status override. Accepts a map or keyword list.

Sets the shadow specification.

Sets the text color. Accepts a hex string or named color atom.

Types

status_override()

@type status_override() :: %{
  optional(:background) => Toddy.Iced.Color.t() | Toddy.Iced.Gradient.t(),
  optional(:text_color) => Toddy.Iced.Color.t(),
  optional(:border) => Toddy.Iced.Border.t(),
  optional(:shadow) => Toddy.Iced.Shadow.t()
}

Partial style override for an interaction state.

t()

@type t() :: %Toddy.Iced.StyleMap{
  background: Toddy.Iced.Color.t() | Toddy.Iced.Gradient.t() | nil,
  base: atom() | nil,
  border: Toddy.Iced.Border.t() | nil,
  disabled: status_override() | nil,
  focused: status_override() | nil,
  hovered: status_override() | nil,
  pressed: status_override() | nil,
  shadow: Toddy.Iced.Shadow.t() | nil,
  text_color: Toddy.Iced.Color.t() | nil
}

Functions

background(style_map, gradient)

@spec background(
  style_map :: t(),
  background :: Toddy.Iced.Color.input() | Toddy.Iced.Gradient.t()
) ::
  t()

Sets the background. Accepts a color (any form Color.cast/1 supports) or a Gradient.

base(style_map, preset)

@spec base(style_map :: t(), preset :: atom()) :: t()

Sets the base preset to extend from instead of starting from the widget default.

border(style_map, border)

@spec border(style_map :: t(), border :: Toddy.Iced.Border.t()) :: t()

Sets the border specification.

disabled(style_map, disabled)

@spec disabled(style_map :: t(), disabled :: status_override() | keyword()) :: t()

Sets the disabled status override. Accepts a map or keyword list.

focused(style_map, focused)

@spec focused(style_map :: t(), focused :: status_override() | keyword()) :: t()

Sets the focused status override. Accepts a map or keyword list.

hovered(style_map, hovered)

@spec hovered(style_map :: t(), hovered :: status_override() | keyword()) :: t()

Sets the hovered status override. Accepts a map or keyword list.

new()

@spec new() :: t()

Creates an empty style map.

pressed(style_map, pressed)

@spec pressed(style_map :: t(), pressed :: status_override() | keyword()) :: t()

Sets the pressed status override. Accepts a map or keyword list.

shadow(style_map, shadow)

@spec shadow(style_map :: t(), shadow :: Toddy.Iced.Shadow.t()) :: t()

Sets the shadow specification.

text_color(style_map, text_color)

@spec text_color(style_map :: t(), text_color :: Toddy.Iced.Color.input()) :: t()

Sets the text color. Accepts a hex string or named color atom.