blib/lib/Term/Fabulous/Widget/Dropdown/List.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 13 Podpod n/a totaltotal 0.0% 0 / 213 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::Dropdown::List;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Term::Fabulous::Role::Popup;
11 use Term::Fabulous::Widget::Display;
12  
13 our $VERSION = '0.05';
14  
15 class Term::Fabulous::Widget::Dropdown::List
16 :isa(Term::Fabulous::Widget::Display)
17 :does(Term::Fabulous::Role::Popup)
18 :strict(params)
19 {
20 use Clay::UI::Enum::Result;
21 use List::Util qw(max);
22 use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_KEY_MOUSE_RELEASE TB_KEY_MOUSE_WHEEL_UP TB_KEY_MOUSE_WHEEL_DOWN);
23 use Term::Fabulous::Unicode qw(fit_columns string_columns);
24 use Term::Fabulous::Viewport qw(clamp_offset reveal_range scroll_thumb);
25 use Term::Fabulous::Widget::Scrollbar;
26  
27 field $dropdown :param :weak;
28 field $visible_rows :param :reader;
29  
30 # The first option shown.
31 field $top = 0;
32  
33 ADJUST {
34 $self->on(
35 Mouse => sub ( $self, $event ) {
36 return $self->_handle_mouse($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE;
37 },
38 owner => $self
39 );
40 }
41  
42 method top_option () {
43 return $top;
44 }
45  
46 # The list's background, border color and border style come from the
47 # dropdown (its list colors, its accent and the theme's dropdown
48 # family), read when the frame is drawn; the readers say so too.
49 method background_color :override (@new) {
50 return $self->SUPER::background_color(@new) if @new;
51 return $self->SUPER::background_color // ( defined $dropdown ? $dropdown->list_background_color : undef );
52 }
53  
54 method border_color :override (@new) {
55 return $self->SUPER::border_color(@new) if @new;
56 return $self->SUPER::border_color if $self->has_look_override('border_color') || !defined $dropdown;
57 return $dropdown->accent_color;
58 }
59  
60 method contribute_look_theme :override ($config) {
61 return unless defined $dropdown;
62 $config->{background_color} = $self->background_color;
63 my $border = $config->{border} // return;
64 $config->{border} = { %$border, color => $self->border_color };
65 return;
66 }
67  
68 # A side without a style of its own is drawn in the dropdown's
69 # list.border.style (Term::Fabulous::Role::HasBorderStyle).
70 method derived_border_style ($side) {
71 return defined $dropdown ? $dropdown->look('list.border.style') : undef;
72 }
73  
74 method _scrolls () {
75 return $dropdown->option_count > $visible_rows;
76 }
77  
78 # Scrolls the highlighted option into view.
79 method show_highlight () {
80 my $highlighted = $dropdown->highlighted_index // 0;
81 $top = reveal_range( $dropdown->option_count, $visible_rows, $top, $highlighted, $highlighted + 1 );
82 return $self->mark_changed;
83 }
84  
85 method scroll ($rows) {
86 $top = clamp_offset( $dropdown->option_count, $visible_rows, $top + $rows );
87 return $self->mark_changed;
88 }
89  
90 # The option shown at a buffer row, or undef.
91 method option_at_row ($row) {
92 my $index = $top + $row;
93 return $row >= 0 && $index < $dropdown->option_count ? $index : undef;
94 }
95  
96 method _handle_mouse ($event) {
97 my $key = $event->key;
98 if ( $key == TB_KEY_MOUSE_WHEEL_UP || $key == TB_KEY_MOUSE_WHEEL_DOWN ) {
99 my $before = $top;
100 $self->scroll( $key == TB_KEY_MOUSE_WHEEL_UP ? -1 : 1 );
101 return 0 if $top == $before; # every option shown, or at an end
102 $event->use_wheel;
103 return 1;
104 }
105 return 0 unless $key == TB_KEY_MOUSE_LEFT || $key == TB_KEY_MOUSE_RELEASE;
106  
107 my ( $column, $row ) = $self->cell_at($event);
108 my $index = defined $row && $column < $self->_text_columns ? $self->option_at_row($row) : undef;
109 return 1 unless defined $index;
110 if ( $key == TB_KEY_MOUSE_LEFT ) {
111 $dropdown->highlight($index);
112 return 1;
113 }
114 $dropdown->choose($index);
115 return 1;
116 }
117  
118 method contribute_floating :override ($config) {
119 $self->SUPER::contribute_floating($config);
120 $self->float_popup($config) if defined $dropdown && defined $config->{floating};
121 return;
122 }
123  
124 # Where the list floats in this frame (Term::Fabulous::Role::Popup):
125 # where the dropdown places it on the screen. Before the dropdown has
126 # a box, the list hangs from it as the dropdown attached it.
127 method popup_position () {
128 return defined $dropdown ? $dropdown->list_position : undef;
129 }
130  
131 method _text_columns () {
132 return $self->columns - ( $self->_scrolls ? 1 : 0 );
133 }
134  
135 # The widest label with its margins, and a column for the scrollbar
136 # (Term::Fabulous::Widget::Display); the dropdown gives the list its
137 # size when it opens it.
138 method natural_size () {
139 return ( 0, $visible_rows ) unless defined $dropdown;
140 my $widest = max( 0, map { string_columns( $dropdown->option_label($_) ) } 0 .. $dropdown->option_count - 1 );
141 return ( $widest + 2 + ( $self->_scrolls ? 1 : 0 ), $visible_rows );
142 }
143  
144 # What the shown rows paint: [ label, fg, bg ] each.
145 method _shown_rows () {
146 return map { [ $dropdown->option_label($_), $dropdown->option_attrs($_) ] } grep { defined } map { $self->option_at_row($_) } 0 .. $self->rows - 1;
147 }
148  
149 # The options and colors the list shows come from the dropdown, which
150 # does not mark the list changed.
151 method paint_key :override () {
152 return $self->SUPER::paint_key unless defined $dropdown;
153 return ( $self->SUPER::paint_key, $top, $dropdown->option_count, map { @$_ } $self->_shown_rows );
154 }
155  
156 method paint () {
157 return unless defined $dropdown;
158 my @rows = $self->_shown_rows;
159 my $width = $self->_text_columns;
160 foreach my $row ( 0 .. $#rows ) {
161 my ( $label, $fg, $bg ) = @{ $rows[$row] };
162 $self->put_attrs( $_, $row, ' ', undef, $bg ) foreach 0 .. $width - 1;
163 $self->paint_text( 1, $row, fit_columns( $label, $width - 2 ), $fg, $bg, $width - 1 ); # a column of margin on either side
164 }
165 $self->_paint_scrollbar if $self->_scrolls;
166 return;
167 }
168  
169 # In the rightmost column, in the theme's scrollbar colors.
170 method _paint_scrollbar () {
171 Term::Fabulous::Widget::Scrollbar->paint_track(
172 $self,
173 axis => 'vertical',
174 at => $self->columns - 1,
175 thumb => scroll_thumb( $dropdown->option_count, $visible_rows, $top, $self->rows ),
176 track_attr => $self->color_attr( $self->family_look( scrollbar => 'track' ) ),
177 thumb_attr => $self->color_attr( $self->family_look( scrollbar => 'thumb' ) ),
178 );
179 return;
180 }
181 }
182  
183 1;
184  
185 __END__