| 1 | #!/usr/bin/perl | ||
| 2 | # made by: KorG | ||
| 3 | # vim: cc=119 et sw=4 ts=4 : | ||
| 4 | package X11::korgwm::Layout; | ||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use POSIX qw( ceil floor round ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Storable qw( dclone ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use X11::korgwm::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use X11::korgwm::Window; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | The idea behind this module lays in arranging the windows in a proper order. | ||
| 17 | There is a kind of "default" layout for any tag. | ||
| 18 | Each time user starts using a tag (i.e. creates first window on it), the layout | ||
| 19 | for this tag is being copied from the default one to a "working" one. | ||
| 20 | Then user can either close all windows on a tag resulting in this working | ||
| 21 | layout disposal, or change the layout sizes in several ways. | ||
| 22 | |||
| 23 | Working layout depends on the number of tiled windows in it: layout for a | ||
| 24 | single window differs with layout for many of them. | ||
| 25 | |||
| 26 | Based on number of windows, we need to get a "layout object", which could be | ||
| 27 | either altered in edge sizes, or handle a list of windows, configuring them one | ||
| 28 | by one to match the selected scheme. | ||
| 29 | |||
| 30 | Layout relies on grid division of the screen. Firstly it divides the screen | ||
| 31 | into number of rows, then into columns. "default" layout for 5 windows looks | ||
| 32 | like: | ||
| 33 | |||
| 34 | { | ||
| 35 | 'cols' => [ | ||
| 36 | '0.5', | ||
| 37 | '0.5' | ||
| 38 | ], | ||
| 39 | 'ncols' => 2, | ||
| 40 | 'nrows' => 3, | ||
| 41 | 'rows' => [ | ||
| 42 | '0.333333333333333', | ||
| 43 | '0.333333333333333', | ||
| 44 | '0.333333333333333' | ||
| 45 | ] | ||
| 46 | }; | ||
| 47 | |||
| 48 | Then it's being translated into a grid, first element of each array is column's | ||
| 49 | weight. Other elements -- rows weights inside this column. For 5 windows the | ||
| 50 | grid will look like: | ||
| 51 | |||
| 52 | [ | ||
| 53 | [ | ||
| 54 | '0.5', <-- weight (column ratio, 0..1) | ||
| 55 | '0.5', <-- vertical (row) ratio; 0.5 to split screen evenly | ||
| 56 | '0.5' <-- vertical (row) ratio for second window | ||
| 57 | ], | ||
| 58 | [ | ||
| 59 | '0.5', <-- this column has the same width as the first one | ||
| 60 | '0.333333333333333', <-- all three windows have the same row ratio | ||
| 61 | '0.333333333333333', | ||
| 62 | '0.333333333333333' | ||
| 63 | ] | ||
| 64 | ]; | ||
| 65 | |||
| 66 | User *maybe* will be able to change weights in their local copy of grid and | ||
| 67 | everything will work in the same way. | ||
| 68 | |||
| 69 | Layout also defines display order for tiled windows. Default order depends | ||
| 70 | on the layout type: reverse order for grid, forward order for columns. | ||
| 71 | See API on how to change it dynamically. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub _ncols($windows) { | ||
| 76 | return 0 if $windows <= 0; | ||
| 77 | return 2 if $windows == 5; # the only reasonable correction. | ||
| 78 | ceil(sqrt($windows)); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _nrows($windows) { | ||
| 82 | return 0 if $windows <= 0; | ||
| 83 | ceil($windows / _ncols($windows)); | ||
| 84 | } | ||
| 85 | |||
| 86 | # Implement dynamic grid | ||
| 87 | # +-----+-----+ | ||
| 88 | # | | | | ||
| 89 | # +-----+-----+ | ||
| 90 | # | | | | ||
| 91 | # +-----+-----+ | ||
| 92 | sub _new_grid_layout($windows) { | ||
| 93 | croak "Cannot create a layout for imaginary windows" if $windows <= 0; | ||
| 94 | my $nrows = _nrows($windows); | ||
| 95 | my $ncols = _ncols($windows); | ||
| 96 | my $layout = { | ||
| 97 | nrows => $nrows, | ||
| 98 | ncols => $ncols, | ||
| 99 | rows => [ map { 1 / $nrows } 1..$nrows ], | ||
| 100 | cols => [ map { 1 / $ncols } 1..$ncols ], | ||
| 101 | }; | ||
| 102 | my $grid = [ map { [ $_, @{ $layout->{rows} } ] } @{ $layout->{cols} } ]; | ||
| 103 | return $grid if $ncols == 1; | ||
| 104 | |||
| 105 | # Compact first elements of the grid, firstly get extra elements: | ||
| 106 | my $extra = $nrows * $ncols - $windows; | ||
| 107 | |||
| 108 | # ... they're always in leftmost column: | ||
| 109 | pop @{ $grid->[0] } for 1..$extra; | ||
| 110 | |||
| 111 | # Maybe we should rebalance two first columns | ||
| 112 | push @{ $grid->[0] }, pop @{ $grid->[1] } if @{ $grid->[1] } - @{ $grid->[0] } > 1; | ||
| 113 | |||
| 114 | # Normalize elements in first two columns | ||
| 115 | for my $arr (@{ $grid }[0, 1]) { | ||
| 116 | @{ $arr } = (shift @{ $arr }, map { 1 / @$arr } 1..@{ $arr }); | ||
| 117 | } | ||
| 118 | |||
| 119 | return $grid; | ||
| 120 | } | ||
| 121 | |||
| 122 | # Implement simple columns layout | ||
| 123 | # +--+--+--+--+ | ||
| 124 | # | | | | | | ||
| 125 | # | | | | | | ||
| 126 | # | | | | | | ||
| 127 | # +--+--+--+--+ | ||
| 128 | sub _new_columns_layout($windows) { | ||
| 129 | # I don't want to make any algorithm to calculate these values, so just hardcode them | ||
| 130 | return [ [0.34, 1], [0.33, 1 ], [0.33, 1 ] ] if $windows == 3; | ||
| 131 | return [ [0.25, 1], [0.25, 1 ], [0.25, 1 ], [0.25, 1 ] ] if $windows == 4; | ||
| 132 | return [ [0.25, 1], [0.25, 1 ], [0.25, 1 ], [0.25, 0.5, 0.5] ] if $windows == 5; | ||
| 133 | return [ [0.25, 1], [0.25, 1 ], [0.25, 0.5, 0.5], [0.25, 0.5, 0.5] ] if $windows == 6; | ||
| 134 | return [ [0.25, 1], [0.25, 0.5, 0.5], [0.25, 0.5, 0.5], [0.25, 0.5, 0.5] ] if $windows == 7; | ||
| 135 | |||
| 136 | # Fallback to a grid layout | ||
| 137 | return _new_grid_layout($windows); | ||
| 138 | } | ||
| 139 | |||
| 140 | # Implement very simple narrow layout for 1 or 2 windows | ||
| 141 | # +--+-----+--+ | ||
| 142 | # |--| |--| | ||
| 143 | # |--+-----+--| | ||
| 144 | # |--| |--| | ||
| 145 | # +--+-----+--+ | ||
| 146 | sub _new_narrow_layout($windows) { | ||
| 147 | my $padding = 0.20; | ||
| 148 | return [ [$padding], [1 - (2 * $padding), 1 ], [$padding] ] if $windows == 1; | ||
| 149 | return [ [$padding], [1 - (2 * $padding), 0.5, 0.5], [$padding] ] if $windows == 2; | ||
| 150 | |||
| 151 | # Fallback to a grid layout | ||
| 152 | return _new_grid_layout($windows); | ||
| 153 | } | ||
| 154 | |||
| 155 | our %layouts = ( | ||
| 156 | grid => { func => \&_new_grid_layout, reverse_windows => 1 }, | ||
| 157 | columns => { func => \&_new_columns_layout, reverse_windows => 0 }, | ||
| 158 | narrow => { func => \&_new_narrow_layout, reverse_windows => 0 }, | ||
| 159 | ); | ||
| 160 | |||
| 161 | sub arrange_windows($self, $windows, $dpy_width, $dpy_height, $x_offset=0, $y_offset=0) { | ||
| 162 | # Validate parameters | ||
| 163 | croak "Cannot arrange non-windows" unless ref $windows eq "ARRAY"; | ||
| 164 | return if @{ $windows } < 1; | ||
| 165 | croak "Trying to use non-initialized layout" unless defined $self->{grid}; | ||
| 166 | |||
| 167 | # dpy_* means display_ | ||
| 168 | my $nwindows = @{ $windows }; | ||
| 169 | my ($dpy_width_orig, $dpy_height_orig) = ($dpy_width, $dpy_height); | ||
| 170 | |||
| 171 | # Create layout if needed | ||
| 172 | my $grid = dclone($self->{grid}->[$nwindows - 1] //= $self->{func}->($nwindows)); | ||
| 173 | |||
| 174 | # Prepare windows and grid to zip them | ||
| 175 | my @cols = reverse @{ $grid }; | ||
| 176 | my @windows = $self->{reverse_windows} ? reverse @{ $windows } : @{ $windows }; | ||
| 177 | my $hide_border = (1 == @windows and 1 == @screens); | ||
| 178 | |||
| 179 | # Prepare $i, $j to save actual position | ||
| 180 | my ($i, $j) = 0 + @cols; | ||
| 181 | for my $col (@cols) { | ||
| 182 | $i--; | ||
| 183 | $j = @{ $col } - 1; | ||
| 184 | my $col_w = shift @{ $col }; | ||
| 185 | my $width = floor($dpy_width_orig * $col_w); | ||
| 186 | my $x = $dpy_width - $width; | ||
| 187 | $x--, $width++ if $x == 1; | ||
| 188 | |||
| 189 | for my $row_w (reverse @{ $col }) { | ||
| 190 | $j--; | ||
| 191 | my $height = floor($dpy_height_orig * $row_w); | ||
| 192 | my $y = $dpy_height - $height; | ||
| 193 | $y--, $height++ if $y == 1; | ||
| 194 | |||
| 195 | # Extract next window | ||
| 196 | my $win = shift @windows; | ||
| 197 | croak "Window cannot be undef" unless defined $win; | ||
| 198 | $win->resize_and_move($x + $x_offset, $y + $y_offset, $width, $height, $hide_border ? 0 : ()); | ||
| 199 | |||
| 200 | # Save real layout position in the window | ||
| 201 | $win->{real_i} = $i; | ||
| 202 | $win->{real_j} = $j; | ||
| 203 | |||
| 204 | $dpy_height = $y; | ||
| 205 | } | ||
| 206 | |||
| 207 | $dpy_height = $dpy_height_orig; | ||
| 208 | $dpy_width = $x; | ||
| 209 | } | ||
| 210 | $X->flush(); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub resize($self, $nwindows, $i, $j, $delta_x, $delta_y) { | ||
| 214 | return if $i < 0 or $j < 0 or $nwindows < 0; | ||
| 215 | |||
| 216 | my $grid = $self->{grid}->[$nwindows - 1]; | ||
| 217 | return unless defined $grid and @{ $grid } > $i + ($delta_x ? 1 : 0); | ||
| 218 | |||
| 219 | my $col = $grid->[$i]; | ||
| 220 | return unless defined $col and @{ $col } > $j + ($delta_y ? 2 : 1); | ||
| 221 | |||
| 222 | # left/right: just change col's weight | ||
| 223 | if ($delta_x) { | ||
| 224 | # Ignore change if columns are already too small | ||
| 225 | next unless ($delta_x > 0 ? $grid->[$i + 1] : $grid->[$i])->[0] >= 0.2; | ||
| 226 | |||
| 227 | $grid->[$i]->[0] += $delta_x; | ||
| 228 | $grid->[$i + 1]->[0] -= $delta_x; | ||
| 229 | } | ||
| 230 | |||
| 231 | # up/down: change weight for all similar cols | ||
| 232 | if ($delta_y) { | ||
| 233 | my $col_size = @{ $col }; | ||
| 234 | |||
| 235 | for my $col (grep { $col_size == @{ $_ } } @{ $grid }) { | ||
| 236 | next unless ($delta_y > 0 ? $col->[$j + 2] : $col->[$j + 1]) >= 0.2; | ||
| 237 | |||
| 238 | $col->[$j + 1] += $delta_y; | ||
| 239 | $col->[$j + 2] -= $delta_y; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | sub new($self, %params) { | ||
| 245 | my $layout = $layouts{ $params{func} // "grid" } or croak "Unknown layout: $params{func}"; | ||
| 246 | |||
| 247 | my $reverse_windows = $params{reverse_windows} // $layout->{reverse_windows}; | ||
| 248 | |||
| 249 | bless { grid => [], func => $layout->{func}, reverse_windows => $reverse_windows }, $self; | ||
| 250 | } | ||
| 251 | |||
| 252 | 1; | ||