| 1 | package Term::Fabulous::Widget::Box; | ||
| 2 | |||
| 3 | 4 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 4 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 4 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | our $VERSION = '0.05'; | ||
| 11 | |||
| 12 | class Term::Fabulous::Widget::Box | ||
| 13 | :isa(Term::Fabulous::Widget) | ||
| 14 | :does(Term::Fabulous::Role::CanParseLayout) | ||
| 15 | :strict(params) | ||
| 16 | 0 | ▶ | { |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | use Clay::XS qw( | ||
| 18 | Clay_GetElementId CLAY_LEFT_TO_RIGHT CLAY_TOP_TO_BOTTOM | ||
| 19 | CLAY_LEFT_TO_RIGHT_WRAP CLAY_BACK_TO_FRONT CLAY_LINE_SIZING_GROW CLAY_LINE_SIZING_FIT | ||
| 20 | CLAY_ALIGN_X_LEFT CLAY_ALIGN_X_CENTER CLAY_ALIGN_X_RIGHT CLAY_ALIGN_Y_TOP CLAY_ALIGN_Y_CENTER CLAY_ALIGN_Y_BOTTOM | ||
| 21 | CLAY_ATTACH_TO_PARENT CLAY_ATTACH_TO_ROOT CLAY_ATTACH_TO_ELEMENT_WITH_ID | ||
| 22 | CLAY_ATTACH_POINT_LEFT_TOP CLAY_ATTACH_POINT_LEFT_CENTER CLAY_ATTACH_POINT_LEFT_BOTTOM | ||
| 23 | CLAY_ATTACH_POINT_CENTER_TOP CLAY_ATTACH_POINT_CENTER_CENTER CLAY_ATTACH_POINT_CENTER_BOTTOM | ||
| 24 | CLAY_ATTACH_POINT_RIGHT_TOP CLAY_ATTACH_POINT_RIGHT_CENTER CLAY_ATTACH_POINT_RIGHT_BOTTOM | ||
| 25 | CLAY_POINTER_CAPTURE_MODE_CAPTURE CLAY_POINTER_CAPTURE_MODE_PASSTHROUGH | ||
| 26 | CLAY_CLIP_TO_NONE CLAY_CLIP_TO_ATTACHED_PARENT | ||
| 27 | CLAY__SIZING_TYPE_FIXED | ||
| 28 | ); | ||
| 29 | use List::Util qw(max sum0); | ||
| 30 | use Scalar::Util qw(refaddr); | ||
| 31 | use Term::Fabulous::Check qw(border_style describe integer non_negative_integer one_of sizing); | ||
| 32 | |||
| 33 | my %DIRECTION_BY_NAME = ( | ||
| 34 | top_to_bottom => CLAY_TOP_TO_BOTTOM, | ||
| 35 | ttb => CLAY_TOP_TO_BOTTOM, | ||
| 36 | down => CLAY_TOP_TO_BOTTOM, | ||
| 37 | left_to_right => CLAY_LEFT_TO_RIGHT, | ||
| 38 | ltr => CLAY_LEFT_TO_RIGHT, | ||
| 39 | right => CLAY_LEFT_TO_RIGHT, | ||
| 40 | wrap => CLAY_LEFT_TO_RIGHT_WRAP, | ||
| 41 | ltr_wrap => CLAY_LEFT_TO_RIGHT_WRAP, | ||
| 42 | left_to_right_wrap => CLAY_LEFT_TO_RIGHT_WRAP, | ||
| 43 | stack => CLAY_BACK_TO_FRONT, | ||
| 44 | back_to_front => CLAY_BACK_TO_FRONT, | ||
| 45 | btf => CLAY_BACK_TO_FRONT, | ||
| 46 | ); | ||
| 47 | |||
| 48 | my %LINE_SIZING_BY_NAME = ( grow => CLAY_LINE_SIZING_GROW, fit => CLAY_LINE_SIZING_FIT ); | ||
| 49 | |||
| 50 | my %ALIGN_X_BY_NAME = ( left => CLAY_ALIGN_X_LEFT, center => CLAY_ALIGN_X_CENTER, right => CLAY_ALIGN_X_RIGHT ); | ||
| 51 | my %ALIGN_Y_BY_NAME = ( top => CLAY_ALIGN_Y_TOP, center => CLAY_ALIGN_Y_CENTER, bottom => CLAY_ALIGN_Y_BOTTOM ); | ||
| 52 | |||
| 53 | my %ATTACH_TO_BY_NAME = ( | ||
| 54 | parent => CLAY_ATTACH_TO_PARENT, | ||
| 55 | root => CLAY_ATTACH_TO_ROOT, | ||
| 56 | element => CLAY_ATTACH_TO_ELEMENT_WITH_ID, | ||
| 57 | ); | ||
| 58 | |||
| 59 | my %ATTACH_POINT_BY_NAME = ( | ||
| 60 | left_top => CLAY_ATTACH_POINT_LEFT_TOP, | ||
| 61 | left_center => CLAY_ATTACH_POINT_LEFT_CENTER, | ||
| 62 | left_bottom => CLAY_ATTACH_POINT_LEFT_BOTTOM, | ||
| 63 | center_top => CLAY_ATTACH_POINT_CENTER_TOP, | ||
| 64 | center_center => CLAY_ATTACH_POINT_CENTER_CENTER, | ||
| 65 | center_bottom => CLAY_ATTACH_POINT_CENTER_BOTTOM, | ||
| 66 | right_top => CLAY_ATTACH_POINT_RIGHT_TOP, | ||
| 67 | right_center => CLAY_ATTACH_POINT_RIGHT_CENTER, | ||
| 68 | right_bottom => CLAY_ATTACH_POINT_RIGHT_BOTTOM, | ||
| 69 | ); | ||
| 70 | |||
| 71 | my %POINTER_CAPTURE_BY_NAME = ( capture => CLAY_POINTER_CAPTURE_MODE_CAPTURE, passthrough => CLAY_POINTER_CAPTURE_MODE_PASSTHROUGH ); | ||
| 72 | my %CLIP_TO_BY_NAME = ( none => CLAY_CLIP_TO_NONE, attached_parent => CLAY_CLIP_TO_ATTACHED_PARENT ); | ||
| 73 | |||
| 74 | # Each key of a floating node: where its value goes in the | ||
| 75 | # Clay_FloatingElementConfig hash (a key, or a key and a subkey), and | ||
| 76 | # how the KDL value is parsed. | ||
| 77 | my %FLOATING_KEY = ( | ||
| 78 | attach_to => [ ['attach_to'], sub ( $box, $value ) { _named( 'floating attach_to', \%ATTACH_TO_BY_NAME, $value ) } ], | ||
| 79 | parent_id => [ ['parent_id'], sub ( $box, $value ) { _element_id( 'floating parent_id', $value ) } ], | ||
| 80 | element => [ [ attach_points => 'element' ], sub ( $box, $value ) { _named( 'floating element', \%ATTACH_POINT_BY_NAME, $value ) } ], | ||
| 81 | parent => [ [ attach_points => 'parent' ], sub ( $box, $value ) { _named( 'floating parent', \%ATTACH_POINT_BY_NAME, $value ) } ], | ||
| 82 | offset_x => [ [ offset => 'x' ], sub ( $box, $value ) { integer( $box, 'floating offset_x', $value ) } ], | ||
| 83 | offset_y => [ [ offset => 'y' ], sub ( $box, $value ) { integer( $box, 'floating offset_y', $value ) } ], | ||
| 84 | z_index => [ ['z_index'], sub ( $box, $value ) { integer( $box, 'floating z_index', $value ) } ], | ||
| 85 | pointer_capture => [ ['pointer_capture_mode'], sub ( $box, $value ) { _named( 'floating pointer_capture', \%POINTER_CAPTURE_BY_NAME, $value ) } ], | ||
| 86 | clip_to => [ ['clip_to'], sub ( $box, $value ) { _named( 'floating clip_to', \%CLIP_TO_BY_NAME, $value ) } ], | ||
| 87 | ); | ||
| 88 | |||
| 89 | # The value a name stands for in %$value_by_name; an unknown name dies | ||
| 90 | # with the known ones. | ||
| 91 | sub _named ( $what, $value_by_name, $name ) { | ||
| 92 | return $value_by_name->{ one_of( 'Term::Fabulous::Widget::Box', $what, $name, keys %$value_by_name ) }; | ||
| 93 | } | ||
| 94 | |||
| 95 | # The Clay element id number of the widget with the id $id, hashed the | ||
| 96 | # way Clay::UI hashes widget ids. | ||
| 97 | sub _element_id ( $what, $id ) { | ||
| 98 | die "Term::Fabulous::Widget::Box: $what must be a widget id, got " . describe($id) unless defined $id && length $id; | ||
| 99 | return Clay_GetElementId($id)->{id}; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _sizing ( $axis, $spec ) { | ||
| 103 | return sizing( 'Term::Fabulous::Widget::Box', "sizing $axis", $spec // '' ); | ||
| 104 | } | ||
| 105 | |||
| 106 | # --------------------------------------------------------------------- | ||
| 107 | # Height before layout | ||
| 108 | # --------------------------------------------------------------------- | ||
| 109 | |||
| 110 | # The rows the children take: one under the other, their rows and the | ||
| 111 | # gaps between them add up; side by side, each child gets its fixed | ||
| 112 | # width or an equal share of the rest, and the tallest decides. Other | ||
| 113 | # directions take the tallest child too. A child without an estimate | ||
| 114 | # counts one row. | ||
| 115 | method estimated_content_rows :override ($columns) { | ||
| 116 | my @children = @{ $self->layout_children }; | ||
| 117 | return 0 unless @children; | ||
| 118 | my $layout = $self->layout; | ||
| 119 | my $direction = $layout->{layout_direction} // CLAY_LEFT_TO_RIGHT; | ||
| 120 | my $gap = $layout->{child_gap} // 0; | ||
| 121 | return sum0( map { _estimated_rows_of( $_, $columns ) } @children ) + $gap * ( @children - 1 ) if $direction == CLAY_TOP_TO_BOTTOM; | ||
| 122 | return max( map { _estimated_rows_of( $_, $columns ) } @children ) unless $direction == CLAY_LEFT_TO_RIGHT; | ||
| 123 | |||
| 124 | my %fixed = map { refaddr($_) => _fixed_columns_of($_) } @children; | ||
| 125 | my @flexible = grep { !defined $fixed{ refaddr $_ } } @children; | ||
| 126 | my $remaining = max( 1, $columns - sum0( grep { defined } values %fixed ) - $gap * ( @children - 1 ) ); | ||
| 127 | my $share = max( 1, int( $remaining / max( 1, scalar @flexible ) ) ); | ||
| 128 | return max( map { _estimated_rows_of( $_, $fixed{ refaddr $_ } // $share ) } @children ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _estimated_rows_of ( $child, $columns ) { | ||
| 132 | return $child->can('estimated_rows') ? $child->estimated_rows($columns) : 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | # The width a child was given as a fixed sizing, or undef. | ||
| 136 | sub _fixed_columns_of ($child) { | ||
| 137 | return undef unless $child->can('layout'); | ||
| 138 | my $width = $child->layout->{sizing}{width}; | ||
| 139 | return defined $width && $width->{type} == CLAY__SIZING_TYPE_FIXED ? $width->{min} : undef; | ||
| 140 | } | ||
| 141 | |||
| 142 | method layout_properties :common () { | ||
| 143 | return ( | ||
| 144 | background_color => 'color', | ||
| 145 | classes => \&_parse_classes, | ||
| 146 | glyphs_show_through => 'boolean', | ||
| 147 | border_color => 'color', | ||
| 148 | width_group => 'scalar', | ||
| 149 | height_group => 'scalar', | ||
| 150 | layout => \&_parse_layout, | ||
| 151 | border => \&_parse_border, | ||
| 152 | sizing => \&_parse_sizing, | ||
| 153 | padding => \&_parse_padding, | ||
| 154 | child_alignment => \&_parse_child_alignment, | ||
| 155 | floating => \&_parse_floating, | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | |||
| 159 | method _parse_classes ($kid) { | ||
| 160 | $self->classes( $self->kdl_strings($kid) ); | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | |||
| 164 | method _parse_layout ($kid) { | ||
| 165 | my $props = $self->kdl_properties( $kid, qw(direction child_gap gap line_gap line_sizing) ); | ||
| 166 | die "Term::Fabulous::Widget::Box: layout accepts 'child_gap' or its alias 'gap', not both" | ||
| 167 | if exists $props->{child_gap} && exists $props->{gap}; | ||
| 168 | |||
| 169 | my %layout = %{ $self->layout }; | ||
| 170 | if ( exists $props->{direction} ) { | ||
| 171 | $layout{layout_direction} = _named( 'layout direction', \%DIRECTION_BY_NAME, $props->{direction} ); | ||
| 172 | } | ||
| 173 | foreach my $gap_name ( grep { exists $props->{$_} } qw(child_gap gap) ) { | ||
| 174 | $layout{child_gap} = non_negative_integer( $self, "layout $gap_name", $props->{$gap_name} ); | ||
| 175 | } | ||
| 176 | $layout{line_gap} = non_negative_integer( $self, 'layout line_gap', $props->{line_gap} ) if exists $props->{line_gap}; | ||
| 177 | $layout{line_sizing} = _named( 'layout line_sizing', \%LINE_SIZING_BY_NAME, $props->{line_sizing} ) if exists $props->{line_sizing}; | ||
| 178 | $self->layout( \%layout ); | ||
| 179 | return; | ||
| 180 | } | ||
| 181 | |||
| 182 | method _parse_border ($kid) { | ||
| 183 | my @sides = qw(top right bottom left); | ||
| 184 | my $props = $self->kdl_properties( $kid, 'style', ( map { "style-$_" } @sides ), 'color' ); | ||
| 185 | |||
| 186 | foreach my $side (@sides) { | ||
| 187 | my ($style_key) = grep { exists $props->{$_} } ( "style-$side", 'style' ); # the side key wins | ||
| 188 | next unless defined $style_key; | ||
| 189 | my $accessor = "border_style_$side"; | ||
| 190 | $self->$accessor( border_style( $self, "border $style_key", $props->{$style_key} ) ); | ||
| 191 | } | ||
| 192 | $self->border_color( $props->{color} ) if exists $props->{color}; | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | method _parse_sizing ($kid) { | ||
| 197 | my $props = $self->kdl_properties( $kid, qw(width height) ); | ||
| 198 | my %layout = %{ $self->layout }; | ||
| 199 | $layout{sizing} = { %{ $layout{sizing} // {} }, map { $_ => _sizing( $_, $props->{$_} ) } keys %$props }; | ||
| 200 | $self->layout( \%layout ); | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | method _parse_padding ($kid) { | ||
| 205 | my $props = $self->kdl_properties( $kid, qw(left right top bottom) ); | ||
| 206 | my %layout = %{ $self->layout }; | ||
| 207 | $layout{padding} = { %{ $layout{padding} // {} }, map { $_ => non_negative_integer( $self, "padding $_", $props->{$_} ) } keys %$props }; | ||
| 208 | $self->layout( \%layout ); | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | |||
| 212 | method _parse_child_alignment ($kid) { | ||
| 213 | my $props = $self->kdl_properties( $kid, qw(x y) ); | ||
| 214 | my %alignment_by_key = ( x => \%ALIGN_X_BY_NAME, y => \%ALIGN_Y_BY_NAME ); | ||
| 215 | my %layout = %{ $self->layout }; | ||
| 216 | $layout{child_alignment} = { %{ $layout{child_alignment} // {} }, map { $_ => _named( "child_alignment $_", $alignment_by_key{$_}, $props->{$_} ) } keys %$props }; | ||
| 217 | $self->layout( \%layout ); | ||
| 218 | return; | ||
| 219 | } | ||
| 220 | |||
| 221 | method _parse_floating ($kid) { | ||
| 222 | my $props = $self->kdl_properties( $kid, qw(attach_to parent_id element parent offset_x offset_y z_index pointer_capture clip_to) ); | ||
| 223 | my %floating = %{ $self->floating // {} }; | ||
| 224 | foreach my $key ( sort keys %$props ) { | ||
| 225 | my ( $slot, $parse ) = $FLOATING_KEY{$key}->@*; | ||
| 226 | my ( $field, $subfield ) = @$slot; | ||
| 227 | my $value = $parse->( $self, $props->{$key} ); | ||
| 228 | $floating{$field} = defined $subfield ? { %{ $floating{$field} // {} }, $subfield => $value } : $value; | ||
| 229 | } | ||
| 230 | $floating{attach_to} //= CLAY_ATTACH_TO_PARENT; # a floating node means "float" | ||
| 231 | die "Term::Fabulous::Widget::Box: floating attach_to=element needs parent_id" | ||
| 232 | if ( $floating{attach_to} // -1 ) == CLAY_ATTACH_TO_ELEMENT_WITH_ID && !defined $floating{parent_id}; | ||
| 233 | $self->floating( \%floating ); | ||
| 234 | return; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | 1; | ||
| 239 | |||
| 240 | __END__ | ||