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

filter: Statementstmt 0.0% 0 / 245 Branchbran 0.0% 0 / 82 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 / 415 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.02';
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 weaken( my $weak_self = $self );
91 $_icon_text = Term::Fabulous::Widget::Text->new( text => '' );
92 $_title_text = Term::Fabulous::Widget::Text->new( text => '', bold => 1 );
93 $_message_text = Term::Fabulous::Widget::Text->new( text => '', wrap_mode => CLAY_TEXT_WRAP_WORDS );
94 $_body = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow() } } );
95 $_close_button = Term::Fabulous::Widget::Button->new( can_focus => 0, pressed_background_color => undef );
96 $_close_button->add_child( Term::Fabulous::Widget::Text->new( text => CLOSE_MARK ) );
97 $_close_button->on( Activate => sub ($event) { $weak_self->hide if $weak_self; return } );
98 $_panel_color = $self->background_color;
99 $self->SUPER::add_child( $_icon_text, $_body, $_close_button );
100 $_built = 1;
101 $self->_refresh_look;
102 }
103  
104 method background_color :override (@new) {
105 my $result = $self->SUPER::background_color(@new);
106 $_panel_color = $result if @new && !$_styling;
107 return $result;
108 }
109  
110 method theme_family :common () {
111 return 'toast';
112 }
113  
114 # The text color comes from the theme's toast family unless given, and
115 # so do the colors of the kinds.
116 method themed_params :common () {
117 return ( $class->SUPER::themed_params, text_color => [ 'text', 'normal', 'color' ] );
118 }
119  
120 # The parts copy the looks (Term::Fabulous::Role::Themed).
121 method looks_changed (@names) {
122 $self->_refresh_look;
123 return;
124 }
125  
126 method _checked_timeout ($seconds) {
127 return undef unless defined $seconds;
128 $seconds = number( $self, timeout => $seconds );
129 die ref($self) . ": timeout must be positive, or undef for no timeout, got $seconds" unless $seconds > 0;
130 return $seconds;
131 }
132  
133 # ---------------------------------------------------------------------
134 # The parts: children of the program go into the body, below the
135 # message
136 # ---------------------------------------------------------------------
137  
138 method add_child :override (@kids) {
139 return $self->SUPER::add_child(@kids) unless $_built;
140 $_body->add_child(@kids);
141 return $self;
142 }
143  
144 # The title and the message stay: they are the toast's own parts.
145 method remove_child :override (@kids) {
146 $_body->remove_child( grep { !$self->_is_message_part($_) } @kids );
147 return $self;
148 }
149  
150 method remove_child_with_id :override ($target_id) {
151 $_body->remove_child_with_id($target_id);
152 return $self;
153 }
154  
155 method remove_children_with :override ($predicate) {
156 $_body->remove_children_with( sub ($child) { !$self->_is_message_part($child) && $predicate->($child) } );
157 return $self;
158 }
159  
160 # Anything but a widget is no part; the body's remove_child rejects it.
161 method _is_message_part ($widget) {
162 my $address = refaddr($widget) // return 0;
163 return any { $address == refaddr($_) } $_title_text, $_message_text;
164 }
165  
166 method clear_children :override () {
167 $self->remove_children_with( sub { 1 } );
168 return $self;
169 }
170  
171 method body () { return $_body }
172  
173 # ---------------------------------------------------------------------
174 # Look
175 # ---------------------------------------------------------------------
176  
177 method kind_color () {
178 return $color // $self->look($kind);
179 }
180  
181 method _refresh_look () {
182 my $accent = $self->kind_color;
183 my $dark = $self->look('important_text');
184 my $text_color = $self->text_color;
185 $_styling = 1;
186 $self->border_color($accent);
187 $self->background_color( $important ? $accent : $_panel_color );
188 $_styling = 0;
189  
190 $_icon_text->text( $icon // $KIND{$kind}{icon} );
191 $_icon_text->text_color( $important ? $dark : $accent );
192 $_title_text->text($title);
193 $_title_text->text_color( $important ? $dark : $accent );
194 $_message_text->text($message);
195 $_message_text->text_color( $important ? $dark : $text_color );
196 $_close_button->children->[0]->text_color( $important ? $dark : $text_color );
197  
198 $self->_order_body( ( length $title ? $_title_text : () ), ( length $message ? $_message_text : () ) );
199 $self->SUPER::remove_child($_close_button);
200 $self->SUPER::add_child($_close_button) if $closable;
201 return;
202 }
203  
204 # The title and the message that have a text come first in the body,
205 # then whatever the program added.
206 method _order_body (@texts) {
207 my %is_text = map { refaddr($_) => 1 } $_title_text, $_message_text;
208 my @others = grep { !$is_text{ refaddr($_) } } $_body->children->@*;
209 $_body->clear_children;
210 $_body->add_child( @texts, @others );
211 return;
212 }
213  
214 # ---------------------------------------------------------------------
215 # Accessors
216 # ---------------------------------------------------------------------
217  
218 method _set ( $field_ref, $value ) {
219 $$field_ref = $value;
220 $self->_refresh_look;
221 return $$field_ref;
222 }
223  
224 method kind (@new) { return @new ? $self->_set( \$kind, one_of( $self, kind => $new[0], keys %KIND ) ) : $kind }
225 method title (@new) { return @new ? $self->_set( \$title, string( $self, title => $new[0] ) ) : $title }
226 method message (@new) { return @new ? $self->_set( \$message, string( $self, message => $new[0] ) ) : $message }
227 method icon (@new) { return @new ? $self->_set( \$icon, optional( \&string, $self, icon => $new[0] ) ) : $icon }
228 method closable (@new) { return @new ? $self->_set( \$closable, boolean( $self, closable => $new[0] ) ) : $closable }
229 method important (@new) { return @new ? $self->_set( \$important, boolean( $self, important => $new[0] ) ) : $important }
230 method color (@new) { return @new ? $self->_set( \$color, optional( \&Term::Fabulous::Check::color, $self, color => $new[0] ) ) : $color }
231 method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') }
232  
233 method timeout (@new) {
234 return $timeout unless @new;
235 $timeout = $self->_checked_timeout( $new[0] );
236 $self->_restart_timer if $self->is_shown;
237 return $timeout;
238 }
239  
240 method position (@new) {
241 return $position unless @new;
242 my $wanted = one_of( $self, position => $new[0], STACK_CLASS->positions );
243 die ref($self) . ": position cannot change while the toast is shown; hide it first" if $self->is_shown && $wanted ne $position;
244 return $position = $wanted;
245 }
246  
247 method z_index (@new) {
248 return $z_index unless @new;
249 $z_index = integer( $self, z_index => $new[0] );
250 my $stack = $self->stack;
251 $stack->floating( { %{ $stack->floating }, z_index => $z_index } ) if defined $stack;
252 return $z_index;
253 }
254  
255 method margin (@new) {
256 return $margin unless @new;
257 return $margin = non_negative_integer( $self, margin => $new[0] );
258 }
259  
260 method layout_properties :common () {
261 return (
262 $class->SUPER::layout_properties,
263 ( map { $_ => 'scalar' } qw(kind title message icon timeout position z_index margin color) ),
264 ( map { $_ => 'boolean' } qw(closable important) ),
265 );
266 }
267  
268 # ---------------------------------------------------------------------
269 # Showing and hiding
270 # ---------------------------------------------------------------------
271  
272 # The stack the toast is shown in, or undef.
273 method stack () {
274 my $parent = $self->parent;
275 return blessed $parent && $parent->isa(STACK_CLASS) ? $parent : undef;
276 }
277  
278 method is_shown () {
279 return defined $self->stack ? 1 : 0;
280 }
281  
282 # The stack of the toast's position on the root of a UI, created when
283 # there is none yet.
284 method _stack_on ($root) {
285 my ($stack) = $root->get_children_with( sub { blessed $_ && $_->isa(STACK_CLASS) && $_->position eq $position } );
286 return $stack if defined $stack;
287 $stack = STACK_CLASS->new( position => $position, z_index => $z_index, margin => $margin );
288 $root->add_child($stack);
289 return $stack;
290 }
291  
292 method show ($ui) {
293 die ref($self) . ": show needs the Term::Fabulous object, got " . ( ref $ui || $ui // 'undef' ) unless blessed $ui && $ui->isa('Clay::UI');
294 if ( $self->is_shown ) {
295 $self->_restart_timer;
296 return $self;
297 }
298 die ref($self) . ": a toast that is a child of another widget cannot be shown" if defined $self->parent;
299 $self->_stack_on( $ui->root )->add_child($self);
300 $self->_restart_timer;
301 return $self;
302 }
303  
304 # Takes the toast off the screen, and the stack with it when it was the
305 # last toast there; fires Close. Does nothing for a toast that is not
306 # shown.
307 method hide () {
308 my $stack = $self->stack // return $self;
309 $self->_stop_timer;
310 $stack->remove_child($self);
311 my $holder = $stack->parent;
312 $holder->remove_child($stack) if defined $holder && !$stack->children->@*;
313 $self->fire_event( Term::Fabulous::Event::Close->new );
314 return $self;
315 }
316  
317 # The timeout runs on the process-wide IO::Async loop, which run
318 # drives; without a running loop (step in tests) the toast stays.
319 method _restart_timer () {
320 $self->_stop_timer;
321 return unless defined $timeout;
322 weaken( my $weak_self = $self );
323 $_timer = IO::Async::Timer::Countdown->new( delay => $timeout, on_expire => sub { $weak_self->hide if $weak_self; return } );
324 IO::Async::Loop->new->add($_timer);
325 $_timer->start;
326 return;
327 }
328  
329 method _stop_timer () {
330 return unless defined $_timer;
331 $_timer->stop if $_timer->is_running;
332 $_timer->remove_from_parent if defined $_timer->loop;
333 $_timer = undef;
334 return;
335 }
336  
337 # Lets the timeout run out now, as the loop would: for tests.
338 method expire () {
339 return $self unless defined $_timer;
340 $self->_stop_timer;
341 $self->hide;
342 return $self;
343 }
344 }
345  
346 1;
347  
348 __END__