« blib/lib/Term/Fabulous/Widget/ColorPicker.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Dialog/Backdrop.pm »
| 1 | package Term::Fabulous::Widget::Dialog; | ||
| 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::Dialog::Backdrop; | ||
| 12 | |||
| 13 | our $VERSION = '0.05'; | ||
| 14 | |||
| 15 | class Term::Fabulous::Widget::Dialog | ||
| 16 | :isa(Term::Fabulous::Widget::Box) | ||
| 17 | :strict(params) | ||
| 18 | { | ||
| 19 | use Clay::XS qw(sizing_fit sizing_grow CLAY_TOP_TO_BOTTOM CLAY_ATTACH_TO_ROOT CLAY_ATTACH_POINT_RIGHT_CENTER); | ||
| 20 | use List::Util qw(first min); | ||
| 21 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 22 | use Term::Fabulous::Check qw(boolean describe integer one_of string); | ||
| 23 | use Term::Fabulous::Event::Answer; | ||
| 24 | use Term::Fabulous::Event::Close; | ||
| 25 | use Term::Fabulous::Placement qw(TOPMOST_Z_INDEX); | ||
| 26 | use Term::Fabulous::Widget::Button; | ||
| 27 | use Term::Fabulous::Widget::Text; | ||
| 28 | |||
| 29 | use constant DISMISSED => 'cancel'; # the button a dismissed dialog answers with | ||
| 30 | use constant RIGHT_EDGE => -1; # a dialog on the right keeps a column free at the edge | ||
| 31 | use constant LOWEST_Z_INDEX => -32768; # Clay's z_index is a 16-bit integer, up to TOPMOST_Z_INDEX | ||
| 32 | |||
| 33 | my %DEFAULT_LAYOUT = ( | ||
| 34 | layout_direction => CLAY_TOP_TO_BOTTOM, | ||
| 35 | padding => { left => 1, right => 1, top => 1, bottom => 1 }, | ||
| 36 | child_gap => 1, | ||
| 37 | ); | ||
| 38 | |||
| 39 | field $z_index :param = 1000; | ||
| 40 | field $close_on_escape :param = 1; | ||
| 41 | field $title :param = ''; | ||
| 42 | field $notice_line :param = 'shown'; | ||
| 43 | field $placement :param = 'center'; | ||
| 44 | |||
| 45 | field @_buttons; # { id, label, primary, widget }, in order | ||
| 46 | field $_title_text; | ||
| 47 | field $_notice_line; # a box around the notice text, above the buttons | ||
| 48 | field $_notice_text; | ||
| 49 | field $_button_row; | ||
| 50 | |||
| 51 | # The Term::Fabulous::Widget::Dialog::Backdrop while open. The tree owns | ||
| 52 | # it (it holds the dialog as its child), so the reference is weak: a | ||
| 53 | # dialog left open in a dropped UI is freed with it. | ||
| 54 | field $_backdrop; | ||
| 55 | field $_is_open = 0; | ||
| 56 | |||
| 57 | # A dialog looks like a dialog unless told otherwise: a panel in the | ||
| 58 | # theme's dialog colors and border (dialog.border.enabled), laid out | ||
| 59 | # top to bottom with a cell of padding. | ||
| 60 | sub BUILDARGS ( $class, %params ) { | ||
| 61 | $params{layout} = { %DEFAULT_LAYOUT, %{ $params{layout} // {} } }; | ||
| 62 | return $class->SUPER::BUILDARGS(%params); | ||
| 63 | } | ||
| 64 | |||
| 65 | ADJUST :params ( :$buttons = [] ) { | ||
| 66 | $z_index = $self->_checked_z_index($z_index); | ||
| 67 | $close_on_escape = boolean( $self, close_on_escape => $close_on_escape ); | ||
| 68 | $title = string( $self, title => $title ); | ||
| 69 | $notice_line = one_of( $self, notice_line => $notice_line, qw(shown kept row) ); | ||
| 70 | $placement = one_of( $self, placement => $placement, qw(center right) ); | ||
| 71 | @_buttons = $self->_parsed_buttons($buttons); | ||
| 72 | |||
| 73 | $_title_text = Term::Fabulous::Widget::Text->new( text => $title, bold => 1 ); | ||
| 74 | $_notice_text = Term::Fabulous::Widget::Text->new( markup => '' ); | ||
| 75 | $_notice_line = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow(), height => sizing_fit( $notice_line eq 'row' ? 1 : 0 ) } } ); | ||
| 76 | $_notice_line->add_child($_notice_text); | ||
| 77 | $_button_row = $self->_button_row if @_buttons; | ||
| 78 | $self->add_child($_title_text) if length $title; | ||
| 79 | $self->_float_on_the_right if $placement eq 'right'; | ||
| 80 | } | ||
| 81 | |||
| 82 | method theme_family :common () { | ||
| 83 | return 'dialog'; | ||
| 84 | } | ||
| 85 | |||
| 86 | method themed_params :common () { | ||
| 87 | return ( $class->SUPER::themed_params, backdrop_color => [ 'backdrop', 'normal', 'color' ] ); | ||
| 88 | } | ||
| 89 | |||
| 90 | method layout_properties :common () { | ||
| 91 | return ( | ||
| 92 | $class->SUPER::layout_properties, | ||
| 93 | z_index => 'scalar', | ||
| 94 | close_on_escape => 'boolean', | ||
| 95 | title => 'scalar', | ||
| 96 | notice_line => 'scalar', | ||
| 97 | placement => 'scalar', | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | |||
| 101 | # --------------------------------------------------------------------- | ||
| 102 | # Parameters | ||
| 103 | # --------------------------------------------------------------------- | ||
| 104 | |||
| 105 | method _parsed_buttons ($buttons) { | ||
| 106 | die ref($self) . ": buttons must be an array reference, got " . describe($buttons) unless ref $buttons eq 'ARRAY'; | ||
| 107 | my ( @parsed, %seen ); | ||
| 108 | foreach my $button (@$buttons) { | ||
| 109 | die ref($self) . ": a button must be a hash reference with an id and a label, got " . describe($button) unless ref $button eq 'HASH'; | ||
| 110 | my %rest = %$button; | ||
| 111 | my $id = string( $self, 'the id of a button' => delete $rest{id} ); | ||
| 112 | my $label = string( $self, "the label of the button '$id'" => delete $rest{label} ); | ||
| 113 | my $primary = boolean( $self, "the primary flag of the button '$id'" => delete $rest{primary} ); | ||
| 114 | die ref($self) . ": the button '$id' has unknown keys: " . join( ', ', sort keys %rest ) if %rest; | ||
| 115 | die ref($self) . ": two buttons have the id '$id'" if $seen{$id}++; | ||
| 116 | push @parsed, { id => $id, label => $label, primary => $primary }; | ||
| 117 | } | ||
| 118 | return @parsed; | ||
| 119 | } | ||
| 120 | |||
| 121 | # A dialog on the right floats at the right edge of the screen, over | ||
| 122 | # its backdrop. | ||
| 123 | method _float_on_the_right () { | ||
| 124 | $self->floating( | ||
| 125 | { | ||
| 126 | attach_to => CLAY_ATTACH_TO_ROOT, | ||
| 127 | attach_points => { element => CLAY_ATTACH_POINT_RIGHT_CENTER, parent => CLAY_ATTACH_POINT_RIGHT_CENTER }, | ||
| 128 | offset => { x => RIGHT_EDGE, y => 0 }, | ||
| 129 | z_index => min( $z_index + 1, TOPMOST_Z_INDEX ), | ||
| 130 | } | ||
| 131 | ); | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | # --------------------------------------------------------------------- | ||
| 136 | # The parts: title, buttons and the notice line | ||
| 137 | # --------------------------------------------------------------------- | ||
| 138 | |||
| 139 | method _button_row () { | ||
| 140 | my $row = Term::Fabulous::Widget::Box->new( layout => { child_gap => 2 } ); | ||
| 141 | foreach my $button (@_buttons) { | ||
| 142 | my $id = $button->{id}; | ||
| 143 | my $widget = Term::Fabulous::Widget::Button->new( bordered => 1, classes => $button->{primary} ? ['primary'] : [], layout => { padding => { left => 1, right => 1 } } ); | ||
| 144 | $widget->add_child( Term::Fabulous::Widget::Text->new( text => $button->{label} ) ); | ||
| 145 | $widget->on( Activate => sub ( $self, $event ) { $self->press_button($id); return }, owner => $self ); | ||
| 146 | $button->{widget} = $widget; | ||
| 147 | $row->add_child($widget); | ||
| 148 | } | ||
| 149 | return $row; | ||
| 150 | } | ||
| 151 | |||
| 152 | # The title first; the notice line, when it shows, and the buttons | ||
| 153 | # last, unless a subclass put the buttons elsewhere. | ||
| 154 | method place_parts () { | ||
| 155 | $self->_show_title; | ||
| 156 | $self->remove_child( grep { defined } $_notice_line, $_button_row ); # a row elsewhere stays there | ||
| 157 | $self->add_child($_notice_line) if $self->_shows_notice_line; | ||
| 158 | $self->add_child($_button_row) if defined $_button_row && !defined $_button_row->parent; | ||
| 159 | return $self; | ||
| 160 | } | ||
| 161 | |||
| 162 | method _holds ($part) { | ||
| 163 | my $parent = defined $part ? $part->parent : undef; | ||
| 164 | return defined $parent && refaddr($parent) == refaddr($self) ? 1 : 0; | ||
| 165 | } | ||
| 166 | |||
| 167 | method _show_title () { | ||
| 168 | return $self->remove_child($_title_text) unless length $title; | ||
| 169 | $self->insert_children( 0, $_title_text ) unless $self->_holds($_title_text); | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | method _shows_notice_line () { | ||
| 174 | return $notice_line ne 'shown' || length $_notice_text->text; | ||
| 175 | } | ||
| 176 | |||
| 177 | # The notice line comes and goes in its place, above the buttons. | ||
| 178 | method _show_notice_line () { | ||
| 179 | return $self->remove_child($_notice_line) unless $self->_shows_notice_line; | ||
| 180 | return if $self->_holds($_notice_line); | ||
| 181 | my $children = $self->children; | ||
| 182 | my $row = first { refaddr( $children->[$_] ) == refaddr( $_button_row // $self ) } 0 .. $#$children; | ||
| 183 | $self->insert_children( $row // scalar @$children, $_notice_line ); | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | method title (@new) { | ||
| 188 | return $title unless @new; | ||
| 189 | $title = string( $self, title => $new[0] ); | ||
| 190 | $_title_text->text($title); | ||
| 191 | $self->_show_title; | ||
| 192 | return $title; | ||
| 193 | } | ||
| 194 | |||
| 195 | # What the line above the buttons says, in rich markup: an error in | ||
| 196 | # the color of an invalid input, anything else in the text color. | ||
| 197 | method notice (@new) { | ||
| 198 | return length $_notice_text->text ? $_notice_text->markup : undef unless @new; | ||
| 199 | my ( $markup, $kind ) = @new; | ||
| 200 | $markup = string( $self, notice => $markup ); | ||
| 201 | $kind = one_of( $self, 'the kind of a notice' => $kind // 'info', qw(info error) ); | ||
| 202 | $_notice_text->markup($markup); | ||
| 203 | $kind eq 'error' ? $_notice_text->text_color( $self->family_look( input => 'text', 'invalid' ) ) : $_notice_text->reset_look('text_color'); | ||
| 204 | $self->_show_notice_line; | ||
| 205 | return $self; | ||
| 206 | } | ||
| 207 | |||
| 208 | method button ($id) { | ||
| 209 | return $self->_button($id)->{widget}; | ||
| 210 | } | ||
| 211 | |||
| 212 | method is_primary ($id) { | ||
| 213 | return $self->_button($id)->{primary}; | ||
| 214 | } | ||
| 215 | |||
| 216 | method _button ($id) { | ||
| 217 | my $button = first { $_->{id} eq ( $id // '' ) } @_buttons; | ||
| 218 | die ref($self) . ": the dialog has no button with the id " . describe($id) unless defined $button; | ||
| 219 | return $button; | ||
| 220 | } | ||
| 221 | |||
| 222 | # The notice as it reads, without its markup. | ||
| 223 | method notice_text () { | ||
| 224 | my $text = $_notice_text->text; | ||
| 225 | return length $text ? $text : undef; | ||
| 226 | } | ||
| 227 | |||
| 228 | method button_ids () { | ||
| 229 | return map { $_->{id} } @_buttons; | ||
| 230 | } | ||
| 231 | |||
| 232 | method button_row () { | ||
| 233 | return $_button_row; | ||
| 234 | } | ||
| 235 | |||
| 236 | # The button Enter in a field presses where a dialog wants it to: the | ||
| 237 | # first primary one, or the first one. | ||
| 238 | method default_button () { | ||
| 239 | return undef unless @_buttons; | ||
| 240 | my $primary = first { $_->{primary} } @_buttons; | ||
| 241 | return ( $primary // $_buttons[0] )->{id}; | ||
| 242 | } | ||
| 243 | |||
| 244 | # --------------------------------------------------------------------- | ||
| 245 | # Accessors | ||
| 246 | # --------------------------------------------------------------------- | ||
| 247 | |||
| 248 | # The backdrop reads the color when a frame is drawn. | ||
| 249 | method backdrop_color (@new) { | ||
| 250 | return $self->look_value('backdrop_color') unless @new; | ||
| 251 | return $self->set_look( backdrop_color => $new[0] ); | ||
| 252 | } | ||
| 253 | |||
| 254 | method _checked_z_index ($value) { | ||
| 255 | $value = integer( $self, z_index => $value ); | ||
| 256 | die ref($self) . ": z_index must be an integer from " . LOWEST_Z_INDEX . " to " . TOPMOST_Z_INDEX . ", got $value" if $value < LOWEST_Z_INDEX || $value > TOPMOST_Z_INDEX; | ||
| 257 | return $value; | ||
| 258 | } | ||
| 259 | |||
| 260 | method z_index (@new) { | ||
| 261 | return $z_index unless @new; | ||
| 262 | $z_index = $self->_checked_z_index( $new[0] ); | ||
| 263 | $_backdrop->floating( { %{ $_backdrop->floating }, z_index => $z_index } ) if defined $_backdrop; | ||
| 264 | $self->_float_on_the_right if $placement eq 'right'; | ||
| 265 | return $z_index; | ||
| 266 | } | ||
| 267 | |||
| 268 | method close_on_escape (@new) { | ||
| 269 | return $close_on_escape unless @new; | ||
| 270 | return $close_on_escape = boolean( $self, close_on_escape => $new[0] ); | ||
| 271 | } | ||
| 272 | |||
| 273 | method notice_line (@new) { | ||
| 274 | return $notice_line unless @new; | ||
| 275 | $notice_line = one_of( $self, notice_line => $new[0], qw(shown kept row) ); | ||
| 276 | $_notice_line->layout( { %{ $_notice_line->layout }, sizing => { width => sizing_grow(), height => sizing_fit( $notice_line eq 'row' ? 1 : 0 ) } } ); | ||
| 277 | $self->_show_notice_line; | ||
| 278 | return $notice_line; | ||
| 279 | } | ||
| 280 | |||
| 281 | # A dialog in the center sits in its backdrop's layout; one on the | ||
| 282 | # right floats. | ||
| 283 | method placement (@new) { | ||
| 284 | return $placement unless @new; | ||
| 285 | $placement = one_of( $self, placement => $new[0], qw(center right) ); | ||
| 286 | $placement eq 'right' ? $self->_float_on_the_right : $self->floating(undef); | ||
| 287 | return $placement; | ||
| 288 | } | ||
| 289 | |||
| 290 | method is_open () { | ||
| 291 | return $_is_open; | ||
| 292 | } | ||
| 293 | |||
| 294 | method backdrop () { | ||
| 295 | return $_backdrop; | ||
| 296 | } | ||
| 297 | |||
| 298 | # What an answer carries besides what answer is given: nothing for a | ||
| 299 | # plain dialog; a subclass with fields reports them. | ||
| 300 | method values () { | ||
| 301 | return {}; | ||
| 302 | } | ||
| 303 | |||
| 304 | # --------------------------------------------------------------------- | ||
| 305 | # Opening, answering and closing | ||
| 306 | # --------------------------------------------------------------------- | ||
| 307 | |||
| 308 | method open ($ui) { | ||
| 309 | die "Term::Fabulous::Widget::Dialog: open needs the Term::Fabulous object, got " . ( ref $ui || $ui // 'undef' ) | ||
| 310 | unless blessed $ui && $ui->isa('Clay::UI'); | ||
| 311 | return $self if defined $_backdrop; | ||
| 312 | die "Term::Fabulous::Widget::Dialog: a dialog that is a child of another widget cannot open" if defined $self->parent; | ||
| 313 | |||
| 314 | $self->place_parts; | ||
| 315 | my $interaction = $ui->interaction; | ||
| 316 | my $backdrop = Term::Fabulous::Widget::Dialog::Backdrop->new( | ||
| 317 | dialog => $self, | ||
| 318 | z_index => $z_index, | ||
| 319 | focus_before => $interaction->get_focused_widget, | ||
| 320 | ); | ||
| 321 | $backdrop->add_child($self); | ||
| 322 | $ui->root->add_child($backdrop); | ||
| 323 | weaken( $_backdrop = $backdrop ); | ||
| 324 | $_is_open = 1; | ||
| 325 | $interaction->set_focused_widget( $backdrop->get_next_focus ); | ||
| 326 | return $self; | ||
| 327 | } | ||
| 328 | |||
| 329 | # What pressing a button does: the dialog answers with its id. A | ||
| 330 | # subclass whose buttons do more overrides it. | ||
| 331 | method press_button ($id) { | ||
| 332 | return $self->answer($id); | ||
| 333 | } | ||
| 334 | |||
| 335 | method answer ( $id, %values ) { | ||
| 336 | $id = string( $self, 'the id of an answer' => $id ); | ||
| 337 | $self->_button($id) if @_buttons; | ||
| 338 | die ref($self) . ": answer needs an open dialog" unless $_is_open; | ||
| 339 | return $self->_finish( $id, { %{ $self->values }, %values }, 0 ); | ||
| 340 | } | ||
| 341 | |||
| 342 | # Escape, or close from the program: the dialog is dismissed and | ||
| 343 | # answers cancel with its values. | ||
| 344 | method close () { | ||
| 345 | return $self unless $_is_open; | ||
| 346 | return $self->_finish( DISMISSED, $self->values, 1 ); | ||
| 347 | } | ||
| 348 | |||
| 349 | # Removing the backdrop gives the focus back: the backdrop names the | ||
| 350 | # widget that had it as its focus heir. The program may have taken the | ||
| 351 | # backdrop out of the tree already (with clear_children on the root, | ||
| 352 | # for example): then there is nothing to remove, but Close and Answer | ||
| 353 | # still fire. | ||
| 354 | method _finish ( $button, $values, $dismissed ) { | ||
| 355 | my $backdrop = $_backdrop; | ||
| 356 | ( $_is_open, $_backdrop ) = ( 0, undef ); | ||
| 357 | if ( defined $backdrop ) { | ||
| 358 | my $holder = $backdrop->parent; | ||
| 359 | $holder->remove_child($backdrop) if defined $holder; | ||
| 360 | $backdrop->clear_children; | ||
| 361 | } | ||
| 362 | $self->fire_event( Term::Fabulous::Event::Close->new ); | ||
| 363 | $self->fire_event( Term::Fabulous::Event::Answer->new( button => $button, values => $values, dismissed => $dismissed ) ); | ||
| 364 | return $self; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | 1; | ||
| 369 | |||
| 370 | __END__ |