blib/lib/Term/Fabulous/Widget/Toast.pm untested

filter: Statementstmt 0.0% 0 / 243 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 411 CC 1 SCAR 6.9
CC
1
Cov
0%
BUILDARGS
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::Toast;
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::Widget::Box;
11 use Term::Fabulous::Widget::Button;
12 use Term::Fabulous::Widget::Text;
13 use Term::Fabulous::Widget::Toast::Stack;
14  
15 our $VERSION = '0.05';
16  
17 class Term::Fabulous::Widget::Toast
18 :isa(Term::Fabulous::Widget::Box)
19 :strict(params)
20 {
21 use Clay::XS qw(sizing_fit sizing_grow CLAY_TOP_TO_BOTTOM CLAY_TEXT_WRAP_WORDS);
22 use IO::Async::Loop;
23 use IO::Async::Timer::Countdown;
24 use List::Util qw(any);
25 use Scalar::Util qw(blessed refaddr weaken);
26 use Term::Fabulous::Check qw(boolean integer non_negative_integer number one_of optional string);
27 use Term::Fabulous::Event::Close;
28  
29 use constant STACK_CLASS => 'Term::Fabulous::Widget::Toast::Stack';
30 use constant CLOSE_MARK => "\x{2715}";
31  
32 # The default icon of each kind; its color is the theme's toast slot of
33 # the kind's name.
34 my %KIND = (
35 info => { icon => 'i' },
36 success => { icon => "\x{2713}" },
37 warning => { icon => '!' },
38 danger => { icon => "\x{2717}" },
39 );
40  
41 my %DEFAULT_SIZING = ( width => sizing_fit( 20, 44 ) );
42  
43 field $kind :param = 'info';
44 field $title :param = '';
45 field $message :param = '';
46 field $icon :param = undef;
47 field $closable :param = 1;
48 field $timeout :param = 5;
49 field $important :param = 0;
50 field $position :param = 'top_right';
51 field $z_index :param = 2000;
52 field $margin :param = 1;
53 field $color :param = undef;
54  
55 # The parts of the toast, and the stack it is shown in.
56 field $_icon_text;
57 field $_title_text;
58 field $_message_text;
59 field $_body;
60 field $_close_button;
61 field $_timer;
62 field $_built = 0;
63  
64 # The background behind a toast that is not important: the one given
65 # to new or written later, kept while an important toast is filled.
66 field $_panel_color;
67 field $_styling = 0;
68  
69 # A toast looks like a toast unless told otherwise: a panel in the
70 # theme's toast colors and border (toast.border.enabled).
71 sub BUILDARGS ( $class, %params ) {
72 $params{layout} = { padding => { left => 1, right => 1 }, child_gap => 1, %{ $params{layout} // {} } };
73 $params{layout}{sizing} = { %DEFAULT_SIZING, %{ $params{layout}{sizing} // {} } };
74 return $class->SUPER::BUILDARGS(%params);
75 }
76  
77 ADJUST {
78 $kind = one_of( $self, kind => $kind, keys %KIND );
79 $title = string( $self, title => $title );
80 $message = string( $self, message => $message );
81 $icon = optional( \&string, $self, icon => $icon );
82 $closable = boolean( $self, closable => $closable );
83 $timeout = $self->_checked_timeout($timeout);
84 $important = boolean( $self, important => $important );
85 $position = one_of( $self, position => $position, STACK_CLASS->positions );
86 $z_index = integer( $self, z_index => $z_index );
87 $margin = non_negative_integer( $self, margin => $margin );
88 $color = optional( \&Term::Fabulous::Check::color, $self, color => $color );
89  
90 $_icon_text = Term::Fabulous::Widget::Text->new( text => '' );
91 $_title_text = Term::Fabulous::Widget::Text->new( text => '', bold => 1 );
92 $_message_text = Term::Fabulous::Widget::Text->new( text => '', wrap_mode => CLAY_TEXT_WRAP_WORDS );
93 $_body = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow() } } );
94 $_close_button = Term::Fabulous::Widget::Button->new( can_focus => 0, pressed_background_color => undef );
95 $_close_button->add_child( Term::Fabulous::Widget::Text->new( text => CLOSE_MARK ) );
96 $_close_button->on( Activate => sub ( $self, $event ) { $self->hide; return }, owner => $self );
97 $_panel_color = $self->background_color;
98 $self->SUPER::add_child( $_icon_text, $_body, $_close_button );
99 $_built = 1;
100 $self->_refresh_look;
101 }
102  
103 method background_color :override (@new) {
104 my $result = $self->SUPER::background_color(@new);
105 $_panel_color = $result if @new && !$_styling;
106 return $result;
107 }
108  
109 method theme_family :common () {
110 return 'toast';
111 }
112  
113 # The text color comes from the theme's toast family unless given, and
114 # so do the colors of the kinds.
115 method themed_params :common () {
116 return ( $class->SUPER::themed_params, text_color => [ 'text', 'normal', 'color' ] );
117 }
118  
119 # The parts copy the looks (Term::Fabulous::Role::Themed).
120 method looks_changed (@names) {
121 $self->_refresh_look;
122 return;
123 }
124  
125 method _checked_timeout ($seconds) {
126 return undef unless defined $seconds;
127 $seconds = number( $self, timeout => $seconds );
128 die ref($self) . ": timeout must be positive, or undef for no timeout, got $seconds" unless $seconds > 0;
129 return $seconds;
130 }
131  
132 # ---------------------------------------------------------------------
133 # The parts: children of the program go into the body, below the
134 # message
135 # ---------------------------------------------------------------------
136  
137 method add_child :override (@kids) {
138 return $self->SUPER::add_child(@kids) unless $_built;
139 $_body->add_child(@kids);
140 return $self;
141 }
142  
143 # The title and the message stay: they are the toast's own parts.
144 method remove_child :override (@kids) {
145 $_body->remove_child( grep { !$self->_is_message_part($_) } @kids );
146 return $self;
147 }
148  
149 method remove_child_with_id :override ($target_id) {
150 $_body->remove_child_with_id($target_id);
151 return $self;
152 }
153  
154 method remove_children_with :override ($predicate) {
155 $_body->remove_children_with( sub ($child) { !$self->_is_message_part($child) && $predicate->($child) } );
156 return $self;
157 }
158  
159 # Anything but a widget is no part; the body's remove_child rejects it.
160 method _is_message_part ($widget) {
161 my $address = refaddr($widget) // return 0;
162 return any { $address == refaddr($_) } $_title_text, $_message_text;
163 }
164  
165 method clear_children :override () {
166 $self->remove_children_with( sub { 1 } );
167 return $self;
168 }
169  
170 method body () { return $_body }
171  
172 # ---------------------------------------------------------------------
173 # Look
174 # ---------------------------------------------------------------------
175  
176 method kind_color () {
177 return $color // $self->look($kind);
178 }
179  
180 method _refresh_look () {
181 my $accent = $self->kind_color;
182 my $dark = $self->look('important_text');
183 my $text_color = $self->text_color;
184 $_styling = 1;
185 $self->border_color($accent);
186 $self->background_color( $important ? $accent : $_panel_color );
187 $_styling = 0;
188  
189 $_icon_text->text( $icon // $KIND{$kind}{icon} );
190 $_icon_text->text_color( $important ? $dark : $accent );
191 $_title_text->text($title);
192 $_title_text->text_color( $important ? $dark : $accent );
193 $_message_text->text($message);
194 $_message_text->text_color( $important ? $dark : $text_color );
195 $_close_button->children->[0]->text_color( $important ? $dark : $text_color );
196  
197 $self->_order_body( ( length $title ? $_title_text : () ), ( length $message ? $_message_text : () ) );
198 $self->SUPER::remove_child($_close_button);
199 $self->SUPER::add_child($_close_button) if $closable;
200 return;
201 }
202  
203 # The title and the message that have a text come first in the body,
204 # then whatever the program added.
205 method _order_body (@texts) {
206 my %is_text = map { refaddr($_) => 1 } $_title_text, $_message_text;
207 my @others = grep { !$is_text{ refaddr($_) } } $_body->children->@*;
208 $_body->clear_children;
209 $_body->add_child( @texts, @others );
210 return;
211 }
212  
213 # ---------------------------------------------------------------------
214 # Accessors
215 # ---------------------------------------------------------------------
216  
217 method _set ( $field_ref, $value ) {
218 $$field_ref = $value;
219 $self->_refresh_look;
220 return $$field_ref;
221 }
222  
223 method kind (@new) { return @new ? $self->_set( \$kind, one_of( $self, kind => $new[0], keys %KIND ) ) : $kind }
224 method title (@new) { return @new ? $self->_set( \$title, string( $self, title => $new[0] ) ) : $title }
225 method message (@new) { return @new ? $self->_set( \$message, string( $self, message => $new[0] ) ) : $message }
226 method icon (@new) { return @new ? $self->_set( \$icon, optional( \&string, $self, icon => $new[0] ) ) : $icon }
227 method closable (@new) { return @new ? $self->_set( \$closable, boolean( $self, closable => $new[0] ) ) : $closable }
228 method important (@new) { return @new ? $self->_set( \$important, boolean( $self, important => $new[0] ) ) : $important }
229 method color (@new) { return @new ? $self->_set( \$color, optional( \&Term::Fabulous::Check::color, $self, color => $new[0] ) ) : $color }
230 method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') }
231  
232 method timeout (@new) {
233 return $timeout unless @new;
234 $timeout = $self->_checked_timeout( $new[0] );
235 $self->_restart_timer if $self->is_shown;
236 return $timeout;
237 }
238  
239 method position (@new) {
240 return $position unless @new;
241 my $wanted = one_of( $self, position => $new[0], STACK_CLASS->positions );
242 die ref($self) . ": position cannot change while the toast is shown; hide it first" if $self->is_shown && $wanted ne $position;
243 return $position = $wanted;
244 }
245  
246 method z_index (@new) {
247 return $z_index unless @new;
248 $z_index = integer( $self, z_index => $new[0] );
249 my $stack = $self->stack;
250 $stack->floating( { %{ $stack->floating }, z_index => $z_index } ) if defined $stack;
251 return $z_index;
252 }
253  
254 method margin (@new) {
255 return $margin unless @new;
256 return $margin = non_negative_integer( $self, margin => $new[0] );
257 }
258  
259 method layout_properties :common () {
260 return (
261 $class->SUPER::layout_properties,
262 ( map { $_ => 'scalar' } qw(kind title message icon timeout position z_index margin color) ),
263 ( map { $_ => 'boolean' } qw(closable important) ),
264 );
265 }
266  
267 # ---------------------------------------------------------------------
268 # Showing and hiding
269 # ---------------------------------------------------------------------
270  
271 # The stack the toast is shown in, or undef.
272 method stack () {
273 my $parent = $self->parent;
274 return blessed $parent && $parent->isa(STACK_CLASS) ? $parent : undef;
275 }
276  
277 method is_shown () {
278 return defined $self->stack ? 1 : 0;
279 }
280  
281 # The stack of the toast's position on the root of a UI, created when
282 # there is none yet.
283 method _stack_on ($root) {
284 my ($stack) = $root->get_children_with( sub { blessed $_ && $_->isa(STACK_CLASS) && $_->position eq $position } );
285 return $stack if defined $stack;
286 $stack = STACK_CLASS->new( position => $position, z_index => $z_index, margin => $margin );
287 $root->add_child($stack);
288 return $stack;
289 }
290  
291 method show ($ui) {
292 die ref($self) . ": show needs the Term::Fabulous object, got " . ( ref $ui || $ui // 'undef' ) unless blessed $ui && $ui->isa('Clay::UI');
293 if ( $self->is_shown ) {
294 $self->_restart_timer;
295 return $self;
296 }
297 die ref($self) . ": a toast that is a child of another widget cannot be shown" if defined $self->parent;
298 $self->_stack_on( $ui->root )->add_child($self);
299 $self->_restart_timer;
300 return $self;
301 }
302  
303 # Takes the toast off the screen, and the stack with it when it was the
304 # last toast there; fires Close. Does nothing for a toast that is not
305 # shown.
306 method hide () {
307 my $stack = $self->stack // return $self;
308 $self->_stop_timer;
309 $stack->remove_child($self);
310 my $holder = $stack->parent;
311 $holder->remove_child($stack) if defined $holder && !$stack->children->@*;
312 $self->fire_event( Term::Fabulous::Event::Close->new );
313 return $self;
314 }
315  
316 # The timeout runs on the process-wide IO::Async loop, which run
317 # drives; without a running loop (step in tests) the toast stays.
318 method _restart_timer () {
319 $self->_stop_timer;
320 return unless defined $timeout;
321 weaken( my $weak_self = $self );
322 $_timer = IO::Async::Timer::Countdown->new( delay => $timeout, on_expire => sub { $weak_self->hide if $weak_self; return } );
323 IO::Async::Loop->new->add($_timer);
324 $_timer->start;
325 return;
326 }
327  
328 method _stop_timer () {
329 return unless defined $_timer;
330 $_timer->stop if $_timer->is_running;
331 $_timer->remove_from_parent if defined $_timer->loop;
332 $_timer = undef;
333 return;
334 }
335  
336 # Lets the timeout run out now, as the loop would: for tests.
337 method expire () {
338 return $self unless defined $_timer;
339 $self->_stop_timer;
340 $self->hide;
341 return $self;
342 }
343 }
344  
345 1;
346  
347 __END__