Packages

Native desktop GUI framework for Gleam, powered by Iced

Current section

Files

Jump to
plushie_gleam src plushie@node.erl
Raw

src/plushie@node.erl

-module(plushie@node).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/plushie/node.gleam").
-export([new/2, with_prop/3, with_props/2, with_children/2, add_child/2, empty_container/0]).
-export_type([prop_value/0, node_/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Tree node types for plushie's UI tree.\n"
"\n"
" `PropValue` is a JSON-like union representing wire-compatible values.\n"
" `Node` is the fundamental tree building block: widget builders produce\n"
" these, tree operations consume them, and the protocol encoder serializes\n"
" them to wire format.\n"
).
-type prop_value() :: {string_val, binary()} |
{int_val, integer()} |
{float_val, float()} |
{bool_val, boolean()} |
null_val |
{binary_val, bitstring()} |
{list_val, list(prop_value())} |
{dict_val, gleam@dict:dict(binary(), prop_value())} |
{opaque_val, gleam@dynamic:dynamic_()}.
-type node_() :: {node,
binary(),
binary(),
gleam@dict:dict(binary(), prop_value()),
list(node_()),
gleam@dict:dict(binary(), prop_value())}.
-file("src/plushie/node.gleam", 61).
?DOC(" Create a node with no props and no children.\n").
-spec new(binary(), binary()) -> node_().
new(Id, Kind) ->
{node, Id, Kind, maps:new(), [], maps:new()}.
-file("src/plushie/node.gleam", 66).
?DOC(" Set a single prop on a node.\n").
-spec with_prop(node_(), binary(), prop_value()) -> node_().
with_prop(Node, Key, Value) ->
{node,
erlang:element(2, Node),
erlang:element(3, Node),
gleam@dict:insert(erlang:element(4, Node), Key, Value),
erlang:element(5, Node),
erlang:element(6, Node)}.
-file("src/plushie/node.gleam", 71).
?DOC(" Set multiple props on a node.\n").
-spec with_props(node_(), list({binary(), prop_value()})) -> node_().
with_props(Node, Props) ->
Merged = gleam@list:fold(
Props,
erlang:element(4, Node),
fun(Acc, Pair) ->
gleam@dict:insert(
Acc,
erlang:element(1, Pair),
erlang:element(2, Pair)
)
end
),
{node,
erlang:element(2, Node),
erlang:element(3, Node),
Merged,
erlang:element(5, Node),
erlang:element(6, Node)}.
-file("src/plushie/node.gleam", 80).
?DOC(" Replace a node's children.\n").
-spec with_children(node_(), list(node_())) -> node_().
with_children(Node, Children) ->
{node,
erlang:element(2, Node),
erlang:element(3, Node),
erlang:element(4, Node),
Children,
erlang:element(6, Node)}.
-file("src/plushie/node.gleam", 85).
?DOC(" Append a child to a node.\n").
-spec add_child(node_(), node_()) -> node_().
add_child(Node, Child) ->
{node,
erlang:element(2, Node),
erlang:element(3, Node),
erlang:element(4, Node),
lists:append(erlang:element(5, Node), [Child]),
erlang:element(6, Node)}.
-file("src/plushie/node.gleam", 90).
?DOC(" An empty container node, useful as a default root.\n").
-spec empty_container() -> node_().
empty_container() ->
new(<<""/utf8>>, <<"container"/utf8>>).