Clay-UI

 view release on metacpan or  search on metacpan

lib/Clay/XS/Structs.pod  view on Meta::CPAN


A "declaration" is the hashref passed to C<Clay__ConfigureOpenElement>;
it is a C<Clay_ElementDeclaration>. A "render command" is one hash in
the arrayref C<Clay_EndLayout> returns (see L<Clay::XS/RENDER COMMANDS>).

=head2 Writing structs in Perl

=over 4

=item *

A struct is a hash reference whose keys are the exact C field names
from F<clay.h>, in camelCase: C<backgroundColor>, C<layoutDirection>,
C<childGap>. Nested structs are nested hashes:
C<< { layout => { padding => { left => 8 } } } >>.

=item *

C<Clay_Color>, C<Clay_Vector2> and C<Clay_Dimensions> also accept an
array reference with the fields in order: C<[r, g, b, a]>, C<[x, y]>,
C<[width, height]>. Missing array elements are 0, so C<[255, 0, 0]> is a
colour with alpha 0.

=item *

C<cornerRadius> and C<aspectRatio> also accept a plain number:
C<< cornerRadius => 8 >> sets all four corners, C<< aspectRatio => 1.5 >>
means C<< aspectRatio => { aspectRatio => 1.5 } >>.

=item *

A missing key, or a key whose value is undef, keeps the zero value: 0
for numbers, false for booleans, the first constant (value 0) for enums.
This is what a C designated initialiser does, and Clay's defaults are
themselves zero, so C<{}> is a valid declaration: a box with C<FIT>
sizing on both axes, laid out left to right, drawing nothing. A nested
struct that is present starts from zero too: C<< backgroundColor => { r
=> 255 } >> has C<g>, C<b> and C<a> 0.

=item *

When a struct is passed to Clay (C<Clay__ConfigureOpenElement>,
C<Clay__OpenTextElement> and the other functions), unknown keys and extra
array elements are ignored. C<check_struct> (see L<Clay::XS/CHECKING
STRUCTS>) is strict: it croaks for unknown keys, for arrays of the wrong
length and for references used as booleans. Use it to catch a misspelt
key such as C<childgap>.

=back

=head2 Value types and ranges

Every value is checked when it crosses into Clay, at any nesting depth.
The field descriptions below use these types:

=over 4

=item float

Any number that is finite as a C C<float> (absolute value up to about
3.4e38). NaN, infinities and non-numeric strings croak. Unless a key's
entry gives a range, signs are not checked: Clay accepts negative values
and computes with them. The one exception to finiteness is the C<max> of
a sizing axis, which may also be C<+Inf> (see L</sizing>).

=item U16

An integer in C<0 .. 65535>. Values outside C<0 .. 65535> and
non-integers (C<1.5>) croak; C<1.0> is accepted. Padding, C<childGap>,
C<lineGap>, border widths, C<fontId>, C<fontSize>, C<letterSpacing> and
C<lineHeight> are U16 and range-checked this way.

Colour channels, corner radii and C<percent> are floats with a range:
channels lie in C<0 .. 255>, radii are not negative and C<percent> lies
in C<0 .. 1>, so C<< { r => 300 } >>, a negative radius or a C<percent>
of 2 croak (C<expected a number in 0..255>, C<< expected a number >= 0 >>).

=item U32

An integer in C<0 .. 4294967295>.

=item int16

An integer in C<-32768 .. 32767>.

=item enum

An integer in C<0 .. N>, where N is the value of the field's last
constant. Use the exported constants (C<CLAY_TOP_TO_BOTTOM>, ...); every
field lists its constants with their values.

=item boolean

Any Perl value, read as true or false. C<check_struct> rejects references
(they are always true, so almost always a mistake).

=item opaque integer

C<userData>, C<imageData> and C<customData>: an unsigned integer that fits
a pointer (C<0 .. 2**64 - 1> on a 64-bit perl). Clay treats it as an
opaque pointer and hands it back unchanged in render commands. A
C<refaddr> value fits. Integers above 2**53 are exact only as Perl
integers or decimal strings, not as floats. Clay::XS takes no reference
to anything, so keep the object an integer stands for alive yourself.

=back

A value that does not fit croaks a C<Clay::XS::StructError> object (see
L<Clay::XS/STRUCT ERRORS>). It names the path to the field:

    Clay_ElementDeclaration.layout.padding.left: expected an integer in 0..65535, got '-8'
    Clay_ElementDeclaration.floating.zIndex: expected an integer in -32768..32767, got '32768'
    Clay_ElementDeclaration.layout: expected a hash reference, got 'x'

=head2 Clay::UI spelling

L<Clay::UI> widgets take the same structs, with two differences:

=over 4

=item *



( run in 1.554 second using v1.01-cache-2.11-cpan-60884a8df76 )