| 1 | package Term::Fabulous::Role::Popup; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | role Term::Fabulous::Role::Popup { | ||
| 13 | use Clay::XS qw(CLAY_ATTACH_TO_ROOT CLAY_ATTACH_POINT_LEFT_TOP); | ||
| 14 | |||
| 15 | # Where the popup goes in this frame, { x, y } on the screen, or undef | ||
| 16 | # while that is not known (an anchor that was never laid out). | ||
| 17 | method popup_position; | ||
| 18 | |||
| 19 | field $_placed_at; # the position the last layout used | ||
| 20 | |||
| 21 | # Attaches a floating config to the root at the popup's position. | ||
| 22 | # Positions come from the boxes of the frame before, which this frame | ||
| 23 | # may change (an anchor that moves or grows, a popup laid out for the | ||
| 24 | # first time), so after the frame the popup looks again and asks for | ||
| 25 | # one more frame when it belongs elsewhere. | ||
| 26 | method float_popup ($config) { | ||
| 27 | $self->each_frame( sub ($popup) { $popup->_recheck_popup_position; return 1 }, name => 'popup position' ); | ||
| 28 | my $position = $self->popup_position // return; | ||
| 29 | $_placed_at = $position; | ||
| 30 | my $corner = CLAY_ATTACH_POINT_LEFT_TOP; | ||
| 31 | $config->{floating} = { %{ $config->{floating} }, attach_to => CLAY_ATTACH_TO_ROOT, attach_points => { element => $corner, parent => $corner }, offset => {%$position} }; | ||
| 32 | return; | ||
| 33 | } | ||
| 34 | |||
| 35 | method _recheck_popup_position () { | ||
| 36 | my $position = $self->popup_position // return; | ||
| 37 | return if defined $_placed_at && $_placed_at->{x} == $position->{x} && $_placed_at->{y} == $position->{y}; | ||
| 38 | $self->mark_changed; | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | 1; | ||
| 44 | |||
| 45 | __END__ |