| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2013-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807 ':experimental(inherit_field)'; | ||
| 9 | |||
| 10 | package Tickit::Widget::LinearSplit 0.42; | ||
| 11 | class Tickit::Widget::LinearSplit :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::ContainerWidget; | ||
| 14 | |||
| 15 | use Tickit::Window 0.32; # needs drag_start | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | |||
| 19 | field $_split_fraction = 0.5; | ||
| 20 | field $_A_child :reader; | ||
| 21 | field $_B_child :reader; | ||
| 22 | |||
| 23 | field $_split_at :inheritable; | ||
| 24 | field $_split_len :inheritable; | ||
| 25 | |||
| 26 | method set_A_child | ||
| 27 | { | ||
| 28 | my ( $child ) = @_; | ||
| 29 | $self->remove( $_A_child ) if $_A_child; | ||
| 30 | $self->add( $_A_child = $child ); | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | method set_B_child | ||
| 35 | { | ||
| 36 | my ( $child ) = @_; | ||
| 37 | $self->remove( $_B_child ) if $_B_child; | ||
| 38 | $self->add( $_B_child = $child ); | ||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | method children | ||
| 43 | { | ||
| 44 | return grep { defined } $_A_child, $_B_child; | ||
| 45 | } | ||
| 46 | |||
| 47 | method child_resized | ||
| 48 | { | ||
| 49 | # TODO: should handle minimums at least | ||
| 50 | } | ||
| 51 | |||
| 52 | ## This should come from ContainerWidget | ||
| 53 | method children_changed | ||
| 54 | { | ||
| 55 | $self->reshape if $self->window; | ||
| 56 | $self->resized; | ||
| 57 | } | ||
| 58 | ## | ||
| 59 | |||
| 60 | method reshape | ||
| 61 | { | ||
| 62 | my $win = $self->window or return; | ||
| 63 | |||
| 64 | my $spacing = $self->get_style_values( "spacing" ); | ||
| 65 | |||
| 66 | my $method = $self->VALUE_METHOD; | ||
| 67 | |||
| 68 | my $quota = $win->$method - $spacing; | ||
| 69 | my $want_split_at = int( $quota * $_split_fraction + 0.5 ); # round to nearest | ||
| 70 | |||
| 71 | # Enforce child minimum sizes | ||
| 72 | if( my $child = $_B_child ) { | ||
| 73 | my $max = $quota - $child->$method; | ||
| 74 | $want_split_at = $max if $want_split_at > $max; | ||
| 75 | } | ||
| 76 | if( my $child = $_A_child ) { | ||
| 77 | my $min = $child->$method; | ||
| 78 | $want_split_at = $min if $want_split_at < $min; | ||
| 79 | } | ||
| 80 | |||
| 81 | my $A_value = $want_split_at; | ||
| 82 | my $B_value = $quota - $want_split_at; | ||
| 83 | |||
| 84 | my @A_geom = $self->_make_child_geom( 0, $A_value ); | ||
| 85 | my @B_geom = $self->_make_child_geom( $A_value + $spacing, $B_value ); | ||
| 86 | |||
| 87 | if( my $child = $_A_child ) { | ||
| 88 | if( $A_value > 0 ) { | ||
| 89 | if( my $childwin = $child->window ) { | ||
| 90 | $childwin->change_geometry( @A_geom ); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $child->set_window( $win->make_sub( @A_geom ) ); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | $child->set_window( undef ); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | if( my $child = $_B_child ) { | ||
| 102 | if( $B_value > 0 ) { | ||
| 103 | if( my $childwin = $child->window ) { | ||
| 104 | $childwin->change_geometry( @B_geom ); | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $child->set_window( $win->make_sub( @B_geom ) ); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | $child->set_window( undef ); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | $_split_at = $A_value; | ||
| 116 | $_split_len = $spacing; | ||
| 117 | } | ||
| 118 | |||
| 119 | field $_drag_mouse_offset; | ||
| 120 | |||
| 121 | method _on_mouse | ||
| 122 | { | ||
| 123 | my ( $ev, $val ) = @_; | ||
| 124 | |||
| 125 | my $val0 = $val - $_split_at; | ||
| 126 | my $in_split = $val0 >= 0 && $val0 < $_split_len; | ||
| 127 | |||
| 128 | if( $ev eq "press" ) { | ||
| 129 | $self->set_style_tag( active => 1 ) if $in_split; | ||
| 130 | } | ||
| 131 | elsif( $ev eq "drag_start" ) { | ||
| 132 | return unless $in_split; | ||
| 133 | $_drag_mouse_offset = $val0; | ||
| 134 | return 1; | ||
| 135 | } | ||
| 136 | elsif( $ev eq "drag" ) { | ||
| 137 | return unless defined $_drag_mouse_offset; | ||
| 138 | |||
| 139 | my $method = $self->VALUE_METHOD; | ||
| 140 | |||
| 141 | my $quota = $self->window->$method - $_split_len; | ||
| 142 | |||
| 143 | my $want_split_at = $val - $_drag_mouse_offset; | ||
| 144 | $_split_fraction = $want_split_at / $quota; | ||
| 145 | |||
| 146 | $self->reshape; | ||
| 147 | $self->redraw; | ||
| 148 | } | ||
| 149 | elsif( $ev eq "drag_drop" ) { | ||
| 150 | undef $_drag_mouse_offset; | ||
| 151 | } | ||
| 152 | elsif( $ev eq "release" ) { | ||
| 153 | $self->set_style_tag( active => 0 ) if $in_split; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | 0x55AA; |