« blib/lib/Term/Fabulous/Widget/StatusBar.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Table/Cell.pm »
| 1 | package Term::Fabulous::Widget::Table; | ||
| 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 | use Clay::UI::Role::Core::Preparable; | ||
| 13 | use Clay::UI::Role::Interaction::Focusable; | ||
| 14 | use Clay::UI::Role::Interaction::Hoverable; | ||
| 15 | use Term::Fabulous::Widget::Box; | ||
| 16 | use Term::Fabulous::Widget::ScrollBox; | ||
| 17 | use Term::Fabulous::Widget::Text; | ||
| 18 | use Term::Fabulous::Widget::TextField; | ||
| 19 | use Term::Fabulous::Widget::Table::Cell; | ||
| 20 | use Term::Fabulous::Widget::Table::ColumnChooser; | ||
| 21 | use Term::Fabulous::Widget::Table::Grid; | ||
| 22 | use Term::Fabulous::Widget::Table::EdgeView; | ||
| 23 | use Term::Fabulous::Role::Searchable qw(searchables_in nested_search_texts nested_search_revision nested_highlight_text nested_clear_text_highlights nested_reveal_text); | ||
| 24 | use Term::Fabulous::Widget::Table::Model; | ||
| 25 | use Term::Fabulous::Widget::Table::Pager; | ||
| 26 | use Term::Fabulous::Widget::Scrollbar; | ||
| 27 | use Term::Fabulous::Widget::Table::Toggle; | ||
| 28 | |||
| 29 | class Term::Fabulous::Widget::Table | ||
| 30 | :isa(Term::Fabulous::Widget::Box) | ||
| 31 | :does(Clay::UI::Role::Interaction::Focusable) | ||
| 32 | :does(Clay::UI::Role::Interaction::Hoverable) | ||
| 33 | :does(Clay::UI::Role::Core::Preparable) | ||
| 34 | :does(Term::Fabulous::Role::Searchable) | ||
| 35 | :strict(params) | ||
| 36 | { | ||
| 37 | use Clay::UI::Enum::Result; | ||
| 38 | use Clay::UI::Role::Layout::HasParent qw(is_widget); | ||
| 39 | use Feature::Compat::Try; | ||
| 40 | use Clay::XS qw( | ||
| 41 | sizing_fit sizing_fixed sizing_grow sizing_percent CLAY_LEFT_TO_RIGHT CLAY_TOP_TO_BOTTOM | ||
| 42 | CLAY__SIZING_TYPE_GROW CLAY__SIZING_TYPE_PERCENT | ||
| 43 | CLAY_ALIGN_X_LEFT CLAY_ALIGN_X_CENTER CLAY_ALIGN_X_RIGHT CLAY_ALIGN_Y_TOP | ||
| 44 | CLAY_TEXT_WRAP_WORDS CLAY_TEXT_WRAP_NEWLINES CLAY_TEXT_WRAP_NONE | ||
| 45 | CLAY_TEXT_ALIGN_LEFT CLAY_TEXT_ALIGN_CENTER CLAY_TEXT_ALIGN_RIGHT | ||
| 46 | ); | ||
| 47 | use List::Util qw(all any first max min none); | ||
| 48 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 49 | use Term::Fabulous::Check qw(boolean border_style describe non_negative_integer one_of optional string); | ||
| 50 | use Term::Fabulous::Enum::BorderStyle; | ||
| 51 | use Term::Fabulous::Event::CursorMove; | ||
| 52 | use Term::Fabulous::Event::Collapse; | ||
| 53 | use Term::Fabulous::Event::ColumnsChange; | ||
| 54 | use Term::Fabulous::Event::Expand; | ||
| 55 | use Term::Fabulous::Event::FilterChange; | ||
| 56 | use Term::Fabulous::Event::PageChange; | ||
| 57 | use Term::Fabulous::Event::RowActivate; | ||
| 58 | use Term::Fabulous::Event::SelectionChange; | ||
| 59 | use Term::Fabulous::Event::SortChange; | ||
| 60 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_KEY_MOUSE_RIGHT TB_MOD_MOTION TB_MOD_SHIFT TB_MOD_CTRL TB_MOD_ALT); | ||
| 61 | use Term::Fabulous::Text::Marks; | ||
| 62 | use Term::Fabulous::Widget::Table::Borders qw(resolve_borders); | ||
| 63 | use Term::Fabulous::Widget::Table::Filter; | ||
| 64 | use Term::Fabulous::Widget::Table::Style qw(style_hash merge_styles); | ||
| 65 | |||
| 66 | use constant { | ||
| 67 | SELECT_COLUMN => '', # the column key of the selection column | ||
| 68 | EMPTY_LINE => "e\0", # the key of the line an empty view shows | ||
| 69 | MARK_CHECKED => '[x]', | ||
| 70 | MARK_UNCHECKED => '[ ]', | ||
| 71 | MARK_SOME => '[-]', | ||
| 72 | OPEN => "\x{25BE} ", | ||
| 73 | CLOSED => "\x{25B8} ", | ||
| 74 | LEAF => ' ', | ||
| 75 | SORT_UP => "\x{25B4}", | ||
| 76 | SORT_DOWN => "\x{25BE}", | ||
| 77 | INDENT => 2, | ||
| 78 | DEFAULT_PAGE_STEP => 10, | ||
| 79 | FALLBACK_BACKGROUND => [ 22, 25, 31, 255 ], # cells on nothing (see _base_background) | ||
| 80 | }; | ||
| 81 | |||
| 82 | my $CONTINUE = Clay::UI::Enum::Result->CONTINUE; | ||
| 83 | my $HANDLED = Clay::UI::Enum::Result->HANDLED; | ||
| 84 | |||
| 85 | my @SELECTIONS = qw(none single multiple); | ||
| 86 | my %ALIGN_X = ( left => CLAY_ALIGN_X_LEFT, center => CLAY_ALIGN_X_CENTER, right => CLAY_ALIGN_X_RIGHT ); | ||
| 87 | my %TEXT_ALIGN = ( left => CLAY_TEXT_ALIGN_LEFT, center => CLAY_TEXT_ALIGN_CENTER, right => CLAY_TEXT_ALIGN_RIGHT ); | ||
| 88 | my %WRAP = ( words => CLAY_TEXT_WRAP_WORDS, newlines => CLAY_TEXT_WRAP_NEWLINES, none => CLAY_TEXT_WRAP_NONE ); | ||
| 89 | my @LINE_PARAMS = qw(border_top border_right border_bottom border_left column_lines row_lines header_line); | ||
| 90 | my @NONE_IS_HIDDEN = ( none => Term::Fabulous::Enum::BorderStyle->Hidden ); # 'none' draws no line | ||
| 91 | my %NO_BOX_BORDER = map { $_ => 0 } qw(top right bottom left); | ||
| 92 | my @COLOR_PARAMS = qw( | ||
| 93 | text_color header_text_color header_background_color group_text_color group_background_color | ||
| 94 | cursor_color selected_color hover_color filter_background_color error_color muted_color line_color | ||
| 95 | ); | ||
| 96 | my %COLOR_SLOT = ( | ||
| 97 | text_color => 'text', | ||
| 98 | header_text_color => 'header.text', | ||
| 99 | header_background_color => 'header.background', | ||
| 100 | group_text_color => 'group.text', | ||
| 101 | group_background_color => 'group.background', | ||
| 102 | cursor_color => 'cursor', | ||
| 103 | selected_color => 'selected', | ||
| 104 | hover_color => 'hover', | ||
| 105 | filter_background_color => 'filter.background', | ||
| 106 | error_color => 'error', | ||
| 107 | muted_color => 'muted', | ||
| 108 | line_color => 'line.color', | ||
| 109 | ); | ||
| 110 | |||
| 111 | # --------------------------------------------------------------------- | ||
| 112 | # Parameters | ||
| 113 | # --------------------------------------------------------------------- | ||
| 114 | |||
| 115 | # What the model is made of; handed over in ADJUST and dropped there. | ||
| 116 | field $initial_columns :param(columns) = []; | ||
| 117 | field $initial_rows :param(rows) = []; | ||
| 118 | field $initial_sort :param(sort) = []; | ||
| 119 | field $initial_group :param(group_by) = undef; | ||
| 120 | field $row_id :param = undef; | ||
| 121 | field $children_key :param = undef; | ||
| 122 | field $tree_expanded :param = 0; | ||
| 123 | field $initial_page_size :param(page_size) = 0; | ||
| 124 | |||
| 125 | field $selection :param = 'none'; | ||
| 126 | field $selection_column :param = undef; | ||
| 127 | field $_explicit_selection_column; # selection_column as the user set it, else undef | ||
| 128 | field $show_header :param(header) = 1; | ||
| 129 | field $filter_row :param = 0; | ||
| 130 | field $page_sizes :param = [ 10, 25, 50, 100 ]; | ||
| 131 | field $show_pager :param(pager) = undef; | ||
| 132 | field $scrollbar :param = 1; | ||
| 133 | field $hover :param = 1; | ||
| 134 | field $tree_column :param = undef; | ||
| 135 | field $group_label :param = undef; | ||
| 136 | field $row_style :param = undef; | ||
| 137 | field $header_style :param = undef; | ||
| 138 | field $group_style :param = undef; | ||
| 139 | field $empty_text :param = 'No rows'; | ||
| 140 | field $no_match_text :param = 'No rows match'; | ||
| 141 | field $cell_padding :param = 1; | ||
| 142 | |||
| 143 | field $border :param = 'Round'; | ||
| 144 | field $border_top :param = undef; | ||
| 145 | field $border_right :param = undef; | ||
| 146 | field $border_bottom :param = undef; | ||
| 147 | field $border_left :param = undef; | ||
| 148 | field $column_lines :param = 'Solid'; | ||
| 149 | field $row_lines :param = undef; | ||
| 150 | field $header_line :param = 'Solid'; | ||
| 151 | |||
| 152 | # --------------------------------------------------------------------- | ||
| 153 | # State | ||
| 154 | # --------------------------------------------------------------------- | ||
| 155 | |||
| 156 | field $model :reader; | ||
| 157 | field %_line_style; # table-wide lines: border_* , column_lines, row_lines, header_line | ||
| 158 | |||
| 159 | # The widgets of the table: three rows, each a view of a grid (the | ||
| 160 | # header, the scrolling body, the footer) and, while the rows scroll, | ||
| 161 | # a strip of the scrollbar column beside it, which draws the frame's | ||
| 162 | # right side there. The strips of the header and the footer hold a | ||
| 163 | # blank column, the body's the scrollbar. | ||
| 164 | field $_header_row; | ||
| 165 | field $_header_view; | ||
| 166 | field $_header_grid; | ||
| 167 | field $_header_strip; | ||
| 168 | field $_body_row; | ||
| 169 | field $_body; | ||
| 170 | field $_body_grid; | ||
| 171 | field $_body_strip; | ||
| 172 | field $_scrollbar; | ||
| 173 | field $_footer_row; | ||
| 174 | field $_footer_view; | ||
| 175 | field $_footer_grid; | ||
| 176 | field $_footer_strip; | ||
| 177 | field $_pager; | ||
| 178 | field $_chooser; | ||
| 179 | |||
| 180 | # The header as built: a signature of what it was built from, and per | ||
| 181 | # grid column { cell, title, marker, mark } and { cell, field }; or, | ||
| 182 | # without header rows, the cells of the ledge that draws the frame's | ||
| 183 | # top. The footer is the ledge that draws its bottom. | ||
| 184 | field $_header_signature = ''; | ||
| 185 | field @_header_cells; | ||
| 186 | field @_filter_cells; | ||
| 187 | field @_top_ledge_cells; | ||
| 188 | field %_filter_text; | ||
| 189 | field %_filter_error; | ||
| 190 | field $_footer_signature = ''; | ||
| 191 | field @_footer_cells; | ||
| 192 | |||
| 193 | # Whether the scrollbar column is shown: wanted, and the rows did not | ||
| 194 | # fit in the last frame. The strips as dressed, by part, and the | ||
| 195 | # column inside the header strip that fills the header rows' | ||
| 196 | # backgrounds, with what it was filled from. | ||
| 197 | field $_gutter_shown = 0; | ||
| 198 | field %_strip_dressed; | ||
| 199 | field $_header_fills; | ||
| 200 | field $_header_fills_signature = ''; | ||
| 201 | |||
| 202 | # The body as built: the signature of its columns, the lines by key | ||
| 203 | # (see _build_line) and the order of the grid's rows. | ||
| 204 | field $_body_signature = ''; | ||
| 205 | field %_built; | ||
| 206 | field @_grid_keys; | ||
| 207 | |||
| 208 | # What a search marked, by "id\0column": for a text cell the ranges | ||
| 209 | # and the row revision they were set at, for a custom cell the | ||
| 210 | # highlights of the widgets inside it, [ index, key, ranges ] by the | ||
| 211 | # index and key. | ||
| 212 | field %_text_highlights; | ||
| 213 | field %_cell_highlights; | ||
| 214 | |||
| 215 | # The grid lines as last worked out (per line, and the strips of the | ||
| 216 | # scrollbar column), what they were worked out from, and a count that | ||
| 217 | # tells the lines' looks they changed. Changes of styles count in | ||
| 218 | # _style_revision. | ||
| 219 | field $_borders_signature = ''; | ||
| 220 | field $_borders = []; | ||
| 221 | field $_strip_borders = undef; | ||
| 222 | field $_borders_generation = 0; | ||
| 223 | field $_style_revision = 0; | ||
| 224 | |||
| 225 | field %_row_style_of; # row id => style hash | ||
| 226 | field %_cell_style_of; # "id\0key" => style hash | ||
| 227 | field $_hover_key; | ||
| 228 | field $_header_cursor; # grid column index while the header has the keyboard, else undef | ||
| 229 | field $_last_press_key; # the line of the last single left press, which a double click must repeat | ||
| 230 | field $_shown_page = 0; | ||
| 231 | |||
| 232 | method theme_family :common () { | ||
| 233 | return 'table'; | ||
| 234 | } | ||
| 235 | |||
| 236 | # The colors come from the theme's table family unless given. The | ||
| 237 | # table draws its frame itself, so it takes no bordered and has no | ||
| 238 | # border.enabled from the theme. | ||
| 239 | method themed_params :common () { | ||
| 240 | my %box = $class->SUPER::themed_params; | ||
| 241 | delete $box{bordered}; | ||
| 242 | return ( | ||
| 243 | %box, | ||
| 244 | stripe_color => [ 'stripe', 'normal', 'optional_color' ], | ||
| 245 | map { $_ => [ $COLOR_SLOT{$_}, 'normal', 'color' ] } @COLOR_PARAMS, | ||
| 246 | ); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub BUILDARGS ( $class, %params ) { | ||
| 250 | _no_bordered($class) if exists $params{bordered}; | ||
| 251 | return $class->SUPER::BUILDARGS(%params); | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _no_bordered ($owner) { | ||
| 255 | die( ( ref $owner || $owner ) | ||
| 256 | . ": a table takes no bordered; it draws its frame itself, in the style of border (or border_top, border_right, border_bottom and border_left), and 'none' leaves a side out" ); | ||
| 257 | } | ||
| 258 | |||
| 259 | method bordered :override (@new) { | ||
| 260 | _no_bordered($self) if @new; | ||
| 261 | return $self->SUPER::bordered; | ||
| 262 | } | ||
| 263 | |||
| 264 | # No Box border around the table's own frame. | ||
| 265 | method _bordered_sides :override () { | ||
| 266 | return \%NO_BOX_BORDER; | ||
| 267 | } | ||
| 268 | |||
| 269 | # The cells, the pager and the scrollbar copy the colors | ||
| 270 | # (Term::Fabulous::Role::Themed). | ||
| 271 | method looks_changed (@names) { | ||
| 272 | $self->_colors_changed(@names); | ||
| 273 | return; | ||
| 274 | } | ||
| 275 | |||
| 276 | ADJUST { | ||
| 277 | my $id = $self->id; | ||
| 278 | die "Term::Fabulous::Widget::Table: a table needs an id (its body keeps its scroll position by it)" unless defined $id; | ||
| 279 | |||
| 280 | $self->_check_parameters; | ||
| 281 | $model = Term::Fabulous::Widget::Table::Model->new( | ||
| 282 | row_id => $row_id, | ||
| 283 | children_key => $children_key, | ||
| 284 | page_size => $initial_page_size, | ||
| 285 | expand_new => $tree_expanded, | ||
| 286 | ); | ||
| 287 | $model->add_column($_) foreach @$initial_columns; | ||
| 288 | $model->set_rows($initial_rows); | ||
| 289 | $model->set_sort( _sort_spec($initial_sort) ); | ||
| 290 | $model->group_by( ref $initial_group eq 'ARRAY' ? @$initial_group : $initial_group ) if defined $initial_group; | ||
| 291 | ( $initial_columns, $initial_rows, $initial_sort, $initial_group ) = (); | ||
| 292 | |||
| 293 | $self->_build_frame; | ||
| 294 | $self->_listen; | ||
| 295 | $self->_watch_gutter; | ||
| 296 | $self->request_prepare; | ||
| 297 | } | ||
| 298 | |||
| 299 | method _check_parameters () { | ||
| 300 | one_of( $self, selection => $selection, @SELECTIONS ); | ||
| 301 | $_explicit_selection_column = boolean( $self, selection_column => $selection_column ) if defined $selection_column; | ||
| 302 | $selection_column = $_explicit_selection_column // ( $selection eq 'multiple' ? 1 : 0 ); | ||
| 303 | $show_header = boolean( $self, header => $show_header ); | ||
| 304 | $filter_row = boolean( $self, filter_row => $filter_row ); | ||
| 305 | $scrollbar = boolean( $self, scrollbar => $scrollbar ); | ||
| 306 | $hover = boolean( $self, hover => $hover ); | ||
| 307 | $tree_expanded = boolean( $self, tree_expanded => $tree_expanded ); | ||
| 308 | $show_pager = boolean( $self, pager => $show_pager ) if defined $show_pager; | ||
| 309 | die "Term::Fabulous::Widget::Table: page_sizes must be an array reference of positive integers, got " . describe($page_sizes) | ||
| 310 | unless ref $page_sizes eq 'ARRAY' && @$page_sizes && !grep { !defined || ref || !/\A[1-9][0-9]*\z/ } @$page_sizes; | ||
| 311 | $page_sizes = [ map { $_ + 0 } @$page_sizes ]; | ||
| 312 | |||
| 313 | foreach my $code ( [ group_label => $group_label ], [ row_style => $row_style ] ) { | ||
| 314 | die "Term::Fabulous::Widget::Table: $code->[0] must be a code reference, got " . describe( $code->[1] ) | ||
| 315 | if defined $code->[1] && ref $code->[1] ne 'CODE'; | ||
| 316 | } | ||
| 317 | $header_style = style_hash( $self, 'header_style', row => $header_style ); | ||
| 318 | $group_style = style_hash( $self, 'group_style', row => $group_style ); | ||
| 319 | $empty_text = string( $self, empty_text => $empty_text ); | ||
| 320 | $no_match_text = string( $self, no_match_text => $no_match_text ); | ||
| 321 | $cell_padding = $self->_padding($cell_padding); | ||
| 322 | |||
| 323 | my $outer = optional( \&border_style, $self, border => $border, @NONE_IS_HIDDEN ); | ||
| 324 | my %line = ( | ||
| 325 | border_top => $border_top, | ||
| 326 | border_right => $border_right, | ||
| 327 | border_bottom => $border_bottom, | ||
| 328 | border_left => $border_left, | ||
| 329 | column_lines => $column_lines, | ||
| 330 | row_lines => $row_lines, | ||
| 331 | header_line => $header_line, | ||
| 332 | ); | ||
| 333 | foreach my $name (@LINE_PARAMS) { | ||
| 334 | my $value = $line{$name} // ( $name =~ /\Aborder_/ ? $outer : undef ); | ||
| 335 | $_line_style{$name} = optional( \&border_style, $self, $name => $value, @NONE_IS_HIDDEN ); | ||
| 336 | } | ||
| 337 | return; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub _sort_spec ($spec) { | ||
| 341 | die "Term::Fabulous::Widget::Table: sort must be an array reference of column keys or [ key, 'asc' or 'desc' ] pairs, got " . describe($spec) | ||
| 342 | unless ref $spec eq 'ARRAY'; | ||
| 343 | return @$spec; | ||
| 344 | } | ||
| 345 | |||
| 346 | method _padding ($padding) { | ||
| 347 | return { left => $padding, right => $padding, top => 0, bottom => 0 } if !ref $padding && defined $padding && $padding =~ /\A[0-9]+\z/; | ||
| 348 | die "Term::Fabulous::Widget::Table: cell_padding must be a number of cells or a hash reference of left, right, top and bottom, got " . describe($padding) | ||
| 349 | unless ref $padding eq 'HASH' && !grep { !/\A(?:left|right|top|bottom)\z/ } keys %$padding; | ||
| 350 | return { map { $_ => non_negative_integer( $self, "cell_padding $_", $padding->{$_} // 0 ) } qw(left right top bottom) }; | ||
| 351 | } | ||
| 352 | |||
| 353 | # --------------------------------------------------------------------- | ||
| 354 | # The frame of widgets: header, body with scrollbar, pager | ||
| 355 | # --------------------------------------------------------------------- | ||
| 356 | |||
| 357 | method _build_frame () { | ||
| 358 | my $layout = $self->layout; | ||
| 359 | $self->layout( | ||
| 360 | { | ||
| 361 | %$layout, | ||
| 362 | layout_direction => CLAY_TOP_TO_BOTTOM, | ||
| 363 | sizing => { width => sizing_fit(), height => sizing_fit(), %{ $layout->{sizing} // {} } }, | ||
| 364 | } | ||
| 365 | ); | ||
| 366 | |||
| 367 | my $id = $self->id; | ||
| 368 | $_body = Term::Fabulous::Widget::ScrollBox->new( | ||
| 369 | id => "$id/body", | ||
| 370 | vertical => 1, | ||
| 371 | horizontal => 1, | ||
| 372 | scrollbar => 0, | ||
| 373 | layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow(), height => sizing_fit() } }, | ||
| 374 | ); | ||
| 375 | $_header_grid = Term::Fabulous::Widget::Table::Grid->new( layout => { sizing => { width => sizing_grow(), height => sizing_fit() } } ); | ||
| 376 | $_body_grid = Term::Fabulous::Widget::Table::Grid->new( layout => { sizing => { width => sizing_grow(), height => sizing_fit() } }, share_columns_with => $_header_grid ); | ||
| 377 | $_body->add_child($_body_grid); | ||
| 378 | |||
| 379 | $_footer_grid = Term::Fabulous::Widget::Table::Grid->new( layout => { sizing => { width => sizing_grow(), height => sizing_fit() } }, share_columns_with => $_header_grid ); | ||
| 380 | ( $_header_view, $_footer_view ) = map { Term::Fabulous::Widget::Table::EdgeView->new( follows => $_body, layout => { sizing => { width => sizing_grow(), height => sizing_fit() } } ) } 1 .. 2; | ||
| 381 | $_header_view->add_child($_header_grid); | ||
| 382 | $_footer_view->add_child($_footer_grid); | ||
| 383 | |||
| 384 | $_scrollbar = Term::Fabulous::Widget::Scrollbar->new( | ||
| 385 | follows => $_body, | ||
| 386 | thumb_color => $self->text_color, | ||
| 387 | track_color => $self->line_color, | ||
| 388 | layout => { sizing => { width => sizing_fixed(1), height => sizing_grow() } }, | ||
| 389 | ); | ||
| 390 | $_header_fills = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_fixed(1), height => sizing_grow() } } ); | ||
| 391 | $_header_strip = _new_strip($_header_fills); | ||
| 392 | $_body_strip = _new_strip($_scrollbar); | ||
| 393 | $_footer_strip = _new_strip( Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed(1), height => sizing_fit() } } ) ); | ||
| 394 | |||
| 395 | ( $_header_row, $_body_row, $_footer_row ) = map { _new_row($_) } $_header_view, $_body, $_footer_view; | ||
| 396 | $_pager = $self->_new_pager; | ||
| 397 | $self->_wire_pager; | ||
| 398 | $self->add_child( $_header_row, $_body_row, $_footer_row ); | ||
| 399 | return; | ||
| 400 | } | ||
| 401 | |||
| 402 | sub _new_row ($view) { | ||
| 403 | my $row = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_LEFT_TO_RIGHT, sizing => { width => sizing_grow(), height => sizing_fit() } } ); | ||
| 404 | $row->add_child($view); | ||
| 405 | return $row; | ||
| 406 | } | ||
| 407 | |||
| 408 | # A strip of the scrollbar column: its content (a blank column or the | ||
| 409 | # scrollbar) and, once dressed, the frame's right side. | ||
| 410 | sub _new_strip ($content) { | ||
| 411 | my $strip = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fit(), height => sizing_grow() } } ); | ||
| 412 | $strip->add_child($content); | ||
| 413 | return $strip; | ||
| 414 | } | ||
| 415 | |||
| 416 | method _new_pager () { | ||
| 417 | return Term::Fabulous::Widget::Table::Pager->new( | ||
| 418 | page_sizes => $page_sizes, | ||
| 419 | text_color => $self->text_color, | ||
| 420 | muted_color => $self->muted_color, | ||
| 421 | button_color => $self->look('pager.button'), | ||
| 422 | ); | ||
| 423 | } | ||
| 424 | |||
| 425 | # A new pager, for new page sizes or colors; prepare_layout shows it. | ||
| 426 | method _rebuild_pager () { | ||
| 427 | my $old = $_pager; | ||
| 428 | $_pager = $self->_new_pager; | ||
| 429 | $self->_wire_pager; | ||
| 430 | $self->remove_child($old); | ||
| 431 | return; | ||
| 432 | } | ||
| 433 | |||
| 434 | method _wire_pager () { | ||
| 435 | my %step = ( first => sub ($self) { 1 }, previous => sub ($self) { $self->page - 1 }, next => sub ($self) { $self->page + 1 }, last => sub ($self) { $self->page_count } ); | ||
| 436 | foreach my $name ( sort keys %step ) { | ||
| 437 | $_pager->button($name)->on( Activate => sub ( $self, $event ) { $self->_turn_page( $step{$name}->($self) ); return }, owner => $self ); | ||
| 438 | } | ||
| 439 | $_pager->size_list->on( Change => sub ( $self, $event ) { $self->_change_page_size( $event->value ); return }, owner => $self ); | ||
| 440 | return; | ||
| 441 | } | ||
| 442 | |||
| 443 | # The grid columns: the selection column, then the visible columns. | ||
| 444 | method _grid_columns () { | ||
| 445 | my @columns = map { { key => $_->key, column => $_ } } $model->visible_columns; | ||
| 446 | unshift @columns, { key => SELECT_COLUMN, column => undef } if $selection_column && $selection ne 'none'; | ||
| 447 | return @columns; | ||
| 448 | } | ||
| 449 | |||
| 450 | # The column that shows the tree: tree_column, or the first visible one. | ||
| 451 | method _tree_column_key () { | ||
| 452 | return undef unless defined $model->children_key; | ||
| 453 | return $tree_column if defined $tree_column && $model->has_column($tree_column) && $model->is_column_visible($tree_column); | ||
| 454 | my ($first) = $model->visible_columns; | ||
| 455 | return defined $first ? $first->key : undef; | ||
| 456 | } | ||
| 457 | |||
| 458 | # --------------------------------------------------------------------- | ||
| 459 | # Preparing the widgets for a frame | ||
| 460 | # --------------------------------------------------------------------- | ||
| 461 | |||
| 462 | method _changed () { | ||
| 463 | $self->request_prepare; | ||
| 464 | return; | ||
| 465 | } | ||
| 466 | |||
| 467 | # A change of looks or lines every cell may show. | ||
| 468 | method _restyle () { | ||
| 469 | $_style_revision++; | ||
| 470 | $self->request_prepare; | ||
| 471 | return; | ||
| 472 | } | ||
| 473 | |||
| 474 | # A focused widget in a cell that is built again or removed hands the | ||
| 475 | # focus to its focus heir, the table. | ||
| 476 | method prepare_layout () { | ||
| 477 | my @grid_columns = $self->_grid_columns; | ||
| 478 | my @lines = $self->_body_lines; | ||
| 479 | $self->_prepare_width( \@grid_columns ); | ||
| 480 | $self->_prepare_gutter; | ||
| 481 | $self->_prepare_header( \@grid_columns ); | ||
| 482 | $self->_prepare_footer( \@grid_columns ); | ||
| 483 | $self->_prepare_body( \@grid_columns, \@lines ); | ||
| 484 | $self->_dress( \@grid_columns, \@lines ); | ||
| 485 | $self->_prepare_pager; | ||
| 486 | $self->_prepare_scrolling; | ||
| 487 | return; | ||
| 488 | } | ||
| 489 | |||
| 490 | # The grids are as wide as their columns, unless a column grows (or is | ||
| 491 | # a share of the width): then they take the width of the table, and the | ||
| 492 | # body no longer scrolls sideways, since Clay squeezes the columns (and | ||
| 493 | # wraps their text) only inside a box that does not clip them. | ||
| 494 | method _prepare_width ($grid_columns) { | ||
| 495 | my $stretch = any { | ||
| 496 | my $type = defined $_->{column} ? $_->{column}->width->{type} : -1; | ||
| 497 | $type == CLAY__SIZING_TYPE_GROW || $type == CLAY__SIZING_TYPE_PERCENT; | ||
| 498 | } @$grid_columns; | ||
| 499 | my $width = $stretch ? sizing_grow() : sizing_fit(); | ||
| 500 | foreach my $widget ( $_header_row, $_header_view, $_header_grid, $_body_row, $_body, $_body_grid, $_footer_row, $_footer_view, $_footer_grid ) { | ||
| 501 | my $layout = $widget->layout; | ||
| 502 | next if _same( $layout->{sizing}{width}, $width ); | ||
| 503 | $widget->layout( { %$layout, sizing => { %{ $layout->{sizing} // {} }, width => $width } } ); | ||
| 504 | } | ||
| 505 | my $sideways = $stretch ? 0 : 1; | ||
| 506 | $_body->horizontal($sideways) if $_body->horizontal != $sideways; | ||
| 507 | return; | ||
| 508 | } | ||
| 509 | |||
| 510 | # The lines of the page, or the one line of an empty view. | ||
| 511 | method _body_lines () { | ||
| 512 | my @lines = $model->page_lines; | ||
| 513 | return @lines if @lines; | ||
| 514 | return { kind => 'empty', key => EMPTY_LINE }; | ||
| 515 | } | ||
| 516 | |||
| 517 | method _prepare_header ($grid_columns) { | ||
| 518 | my $signature = join "\0", $model->columns_revision, $show_header, $filter_row, $selection, map { $_->{key} } @$grid_columns; | ||
| 519 | return if $signature eq $_header_signature; | ||
| 520 | $_header_signature = $signature; | ||
| 521 | $_header_grid->clear_rows; | ||
| 522 | @_header_cells = @_filter_cells = (); | ||
| 523 | if ($show_header) { | ||
| 524 | @_header_cells = map { $self->_header_cell($_) } @$grid_columns; | ||
| 525 | $_header_grid->append_row( [ map { $_->{cell} } @_header_cells ] ) if @_header_cells; | ||
| 526 | } | ||
| 527 | if ($filter_row) { | ||
| 528 | @_filter_cells = map { $self->_filter_cell($_) } @$grid_columns; | ||
| 529 | $_header_grid->append_row( [ map { $_->{cell} } @_filter_cells ] ) if @_filter_cells; | ||
| 530 | } | ||
| 531 | |||
| 532 | # Without header rows, the frame's top is a ledge, which also keeps | ||
| 533 | # the header grid, and with it the body, as wide as the columns. | ||
| 534 | @_top_ledge_cells = @_header_cells || @_filter_cells ? () : map { { cell => $self->_ledge_cell($_) } } @$grid_columns; | ||
| 535 | $_header_grid->append_row( [ map { $_->{cell} } @_top_ledge_cells ] ) if @_top_ledge_cells; | ||
| 536 | return; | ||
| 537 | } | ||
| 538 | |||
| 539 | # The footer: the ledge that draws the frame's bottom. | ||
| 540 | method _prepare_footer ($grid_columns) { | ||
| 541 | my $signature = join "\0", $model->columns_revision, $selection, map { $_->{key} } @$grid_columns; | ||
| 542 | return if $signature eq $_footer_signature; | ||
| 543 | $_footer_signature = $signature; | ||
| 544 | $_footer_grid->clear_rows; | ||
| 545 | @_footer_cells = map { { cell => $self->_ledge_cell($_) } } @$grid_columns; | ||
| 546 | $_footer_grid->append_row( [ map { $_->{cell} } @_footer_cells ] ); | ||
| 547 | return; | ||
| 548 | } | ||
| 549 | |||
| 550 | # A cell of a ledge: a child of no height inside (Clay gives a childless | ||
| 551 | # box no height, border or not), so it is as high as the one frame side | ||
| 552 | # it draws. | ||
| 553 | method _ledge_cell ($grid_column) { | ||
| 554 | my $column = $grid_column->{column}; | ||
| 555 | my $cell = Term::Fabulous::Widget::Table::Cell->new( | ||
| 556 | part => 'ledge', | ||
| 557 | column_key => $grid_column->{key}, | ||
| 558 | layout => { sizing => { width => defined $column ? $column->width : sizing_fit(), height => sizing_fit() } }, | ||
| 559 | ); | ||
| 560 | $cell->add_child( Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow(), height => sizing_fixed(0) } } ) ); | ||
| 561 | return $cell; | ||
| 562 | } | ||
| 563 | |||
| 564 | method _new_cell ( $part, $line_key, $column_key, $column ) { | ||
| 565 | my $align = defined $column ? ( $part eq 'header' ? $column->header_align : $column->align ) : 'left'; | ||
| 566 | return Term::Fabulous::Widget::Table::Cell->new( | ||
| 567 | part => $part, | ||
| 568 | line_key => $line_key, | ||
| 569 | column_key => $column_key, | ||
| 570 | layout => { | ||
| 571 | sizing => { width => defined $column ? $column->width : sizing_fit(), height => sizing_fit() }, | ||
| 572 | padding => {%$cell_padding}, | ||
| 573 | child_alignment => { x => $ALIGN_X{$align}, y => CLAY_ALIGN_Y_TOP }, | ||
| 574 | }, | ||
| 575 | ); | ||
| 576 | } | ||
| 577 | |||
| 578 | method _header_cell ($grid_column) { | ||
| 579 | my $column = $grid_column->{column}; | ||
| 580 | my $cell = $self->_new_cell( header => undef, $grid_column->{key}, $column ); | ||
| 581 | unless ( defined $column ) { | ||
| 582 | my $mark = Term::Fabulous::Widget::Text->new( text => MARK_UNCHECKED ); | ||
| 583 | $cell->add_child($mark); | ||
| 584 | return { cell => $cell, mark => $mark }; | ||
| 585 | } | ||
| 586 | my $marker = Term::Fabulous::Widget::Text->new( text => '', wrap_mode => CLAY_TEXT_WRAP_NONE ); | ||
| 587 | my $title; | ||
| 588 | my $content; | ||
| 589 | if ( defined $column->header ) { | ||
| 590 | $content = _widget_from( "column '" . $column->key . "' header", $column->header->($column) ); | ||
| 591 | } | ||
| 592 | else { | ||
| 593 | $content = $title = Term::Fabulous::Widget::Text->new( text => $column->title, wrap_mode => $WRAP{ $column->wrap }, text_alignment => $TEXT_ALIGN{ $column->header_align } ); | ||
| 594 | } | ||
| 595 | my $row = Term::Fabulous::Widget::Box->new; | ||
| 596 | $row->add_child( $content, $marker ); | ||
| 597 | $cell->add_child($row); | ||
| 598 | return { cell => $cell, title => $title, marker => $marker }; | ||
| 599 | } | ||
| 600 | |||
| 601 | method _filter_cell ($grid_column) { | ||
| 602 | my $column = $grid_column->{column}; | ||
| 603 | my $cell = $self->_new_cell( filter => undef, $grid_column->{key}, $column ); | ||
| 604 | return { cell => $cell } unless defined $column && $column->filterable; | ||
| 605 | my $key = $column->key; | ||
| 606 | my $field = Term::Fabulous::Widget::TextField->new( | ||
| 607 | value => $_filter_text{$key} // '', | ||
| 608 | placeholder => "\x{2026}", | ||
| 609 | text_color => $self->text_color, | ||
| 610 | focus_background_color => $self->cursor_color, | ||
| 611 | layout => { sizing => { width => sizing_grow(0) } }, # one row high, as wide as the column | ||
| 612 | ); | ||
| 613 | $field->on( Change => sub ( $self, $event ) { $self->_filter_typed( $key, $event->value ); return }, owner => $self ); | ||
| 614 | $field->on( Submit => sub ( $self, $event ) { $self->_focus_self; return }, owner => $self ); | ||
| 615 | $cell->add_child($field); | ||
| 616 | return { cell => $cell, field => $field }; | ||
| 617 | } | ||
| 618 | |||
| 619 | sub _widget_from ( $what, $widget ) { | ||
| 620 | die "Term::Fabulous::Widget::Table: $what must return a widget, got " . describe($widget) | ||
| 621 | unless is_widget($widget); | ||
| 622 | return $widget; | ||
| 623 | } | ||
| 624 | |||
| 625 | # --------------------------------------------------------------------- | ||
| 626 | # The body: one grid row per line of the page | ||
| 627 | # --------------------------------------------------------------------- | ||
| 628 | |||
| 629 | method _prepare_body ( $grid_columns, $lines ) { | ||
| 630 | my $signature = join "\0", $model->columns_revision, $selection, map { $_->{key} } @$grid_columns; | ||
| 631 | if ( $signature ne $_body_signature ) { | ||
| 632 | $_body_signature = $signature; | ||
| 633 | $_body_grid->clear_rows; | ||
| 634 | %_built = (); | ||
| 635 | @_grid_keys = (); | ||
| 636 | } | ||
| 637 | |||
| 638 | my ( %wanted, %rebuilt ); | ||
| 639 | foreach my $line (@$lines) { | ||
| 640 | my $key = $line->{key}; | ||
| 641 | my $built = $_built{$key}; | ||
| 642 | $wanted{$key} = 1; | ||
| 643 | if ( !defined $built || !$self->_still_fits( $built, $line ) ) { | ||
| 644 | $_built{$key} = $self->_build_line( $line, $grid_columns ); | ||
| 645 | $rebuilt{$key} = 1 if defined $built; | ||
| 646 | next; | ||
| 647 | } | ||
| 648 | $built->{line} = $line; | ||
| 649 | $self->_refresh_row( $built, $grid_columns ) if $line->{kind} eq 'row' && $built->{revision} != $model->row_revision( $line->{id} ); | ||
| 650 | } | ||
| 651 | |||
| 652 | my @gone = grep { !$wanted{ $_grid_keys[$_] } || $rebuilt{ $_grid_keys[$_] } } 0 .. $#_grid_keys; | ||
| 653 | $_body_grid->remove_row($_) foreach reverse @gone; | ||
| 654 | my %gone_index = map { $_ => 1 } @gone; | ||
| 655 | @_grid_keys = map { $_grid_keys[$_] } grep { !$gone_index{$_} } 0 .. $#_grid_keys; | ||
| 656 | delete @_built{ grep { !$wanted{$_} } keys %_built }; | ||
| 657 | |||
| 658 | my %placed = map { $_ => 1 } @_grid_keys; | ||
| 659 | foreach my $line ( grep { !$placed{ $_->{key} } } @$lines ) { | ||
| 660 | my $built = $_built{ $line->{key} }; | ||
| 661 | $built->{spanning} ? $_body_grid->append_spanning_row( $built->{cells}[0] ) : $_body_grid->append_row( $built->{cells} ); | ||
| 662 | push @_grid_keys, $line->{key}; | ||
| 663 | } | ||
| 664 | |||
| 665 | my %index_of = map { $_grid_keys[$_] => $_ } 0 .. $#_grid_keys; | ||
| 666 | my @order = map { $index_of{ $_->{key} } } @$lines; | ||
| 667 | $_body_grid->reorder_rows( \@order ) if join( ',', @order ) ne join( ',', 0 .. $#order ); | ||
| 668 | @_grid_keys = map { $_->{key} } @$lines; | ||
| 669 | return; | ||
| 670 | } | ||
| 671 | |||
| 672 | # Whether a line built earlier can show the line now (its contents are | ||
| 673 | # refreshed separately). | ||
| 674 | method _still_fits ( $built, $line ) { | ||
| 675 | return 0 if $built->{kind} ne $line->{kind}; | ||
| 676 | return 1 unless $line->{kind} eq 'row'; | ||
| 677 | return $built->{depth} == $line->{depth} ? 1 : 0; | ||
| 678 | } | ||
| 679 | |||
| 680 | method _build_line ( $line, $grid_columns ) { | ||
| 681 | return $self->_build_row( $line, $grid_columns ) if $line->{kind} eq 'row'; | ||
| 682 | return $self->_build_group($line) if $line->{kind} eq 'group'; | ||
| 683 | my $cell = $self->_new_cell( body => $line->{key}, undef, undef ); | ||
| 684 | my $text = Term::Fabulous::Widget::Text->new( text => $model->is_filtered && $model->row_count ? $no_match_text : $empty_text ); | ||
| 685 | $cell->add_child($text); | ||
| 686 | $cell->layout( { %{ $cell->layout }, sizing => { width => sizing_percent(1), height => sizing_fit() } } ); | ||
| 687 | return { kind => 'empty', line => $line, spanning => 1, cells => [$cell], label => $text }; | ||
| 688 | } | ||
| 689 | |||
| 690 | # The hash a column's cell, update_cell and cell_style get. | ||
| 691 | method _cell_context ( $id, $column ) { | ||
| 692 | my $key = $column->key; | ||
| 693 | return { | ||
| 694 | value => $model->value_of( $id, $key ), | ||
| 695 | display => $model->display_of( $id, $key ), | ||
| 696 | row => $model->data_of($id), | ||
| 697 | id => $id, | ||
| 698 | column => $column, | ||
| 699 | table => $self, | ||
| 700 | }; | ||
| 701 | } | ||
| 702 | |||
| 703 | method _build_row ( $line, $grid_columns ) { | ||
| 704 | my $id = $line->{id}; | ||
| 705 | my $tree_key = $self->_tree_column_key; | ||
| 706 | my %built = ( kind => 'row', line => $line, id => $id, depth => $line->{depth}, revision => $model->row_revision($id), cells => [], content => {}, text => {}, holder => {} ); | ||
| 707 | foreach my $grid_column (@$grid_columns) { | ||
| 708 | my ( $key, $column ) = @{$grid_column}{qw(key column)}; | ||
| 709 | my $cell = $self->_new_cell( body => $line->{key}, $key, $column ); | ||
| 710 | push @{ $built{cells} }, $cell; | ||
| 711 | unless ( defined $column ) { | ||
| 712 | $built{mark} = Term::Fabulous::Widget::Text->new( text => MARK_UNCHECKED ); | ||
| 713 | $cell->add_child( $built{mark} ); | ||
| 714 | next; | ||
| 715 | } | ||
| 716 | my $content = $self->_cell_content( \%built, $column ); | ||
| 717 | my $holder = $cell; | ||
| 718 | if ( defined $tree_key && $key eq $tree_key ) { | ||
| 719 | $holder = Term::Fabulous::Widget::Box->new( layout => { padding => { left => INDENT * $line->{depth} } } ); | ||
| 720 | $built{toggle_box} = Term::Fabulous::Widget::Table::Toggle->new( line_key => $line->{key} ); | ||
| 721 | $built{toggle} = Term::Fabulous::Widget::Text->new( text => LEAF, wrap_mode => CLAY_TEXT_WRAP_NONE ); | ||
| 722 | $built{toggle_box}->add_child( $built{toggle} ); | ||
| 723 | $holder->add_child( $built{toggle_box} ); | ||
| 724 | $cell->add_child($holder); | ||
| 725 | } | ||
| 726 | $holder->add_child($content); | ||
| 727 | $built{holder}{$key} = $holder; | ||
| 728 | } | ||
| 729 | return \%built; | ||
| 730 | } | ||
| 731 | |||
| 732 | # The widget showing a cell: the column's cell widget, or a Text. The | ||
| 733 | # highlights a search left for the cell go onto the new widget. | ||
| 734 | method _cell_content ( $built, $column ) { | ||
| 735 | my $key = $column->key; | ||
| 736 | my $slot = "$built->{id}\0$key"; | ||
| 737 | if ( defined $column->cell ) { | ||
| 738 | my $widget = _widget_from( "the cell of column '$key'", $column->cell->( $self->_cell_context( $built->{id}, $column ) ) ); | ||
| 739 | delete $built->{text}{$key}; | ||
| 740 | my @searchables = searchables_in($widget); | ||
| 741 | nested_highlight_text( \@searchables, [ $_->[0], $_->[1] ], $_->[2] ) foreach values %{ $_cell_highlights{$slot} // {} }; | ||
| 742 | return $built->{content}{$key} = $widget; | ||
| 743 | } | ||
| 744 | my $text = Term::Fabulous::Widget::Text->new( | ||
| 745 | text => $model->display_of( $built->{id}, $key ), | ||
| 746 | wrap_mode => $WRAP{ $column->wrap }, | ||
| 747 | text_alignment => $TEXT_ALIGN{ $column->align }, | ||
| 748 | ); | ||
| 749 | my $kept = $_text_highlights{$slot}; | ||
| 750 | $text->highlights( $kept->{ranges} ) if defined $kept && $kept->{revision} == $model->row_revision( $built->{id} ); | ||
| 751 | $built->{text}{$key} = $text; | ||
| 752 | return $built->{content}{$key} = $text; | ||
| 753 | } | ||
| 754 | |||
| 755 | # A row whose data changed: texts are updated, cell widgets brought up | ||
| 756 | # to date by update_cell or built again. What a search marked in the | ||
| 757 | # row pointed into the old data and goes. | ||
| 758 | method _refresh_row ( $built, $grid_columns ) { | ||
| 759 | $built->{revision} = $model->row_revision( $built->{id} ); | ||
| 760 | $self->_drop_row_highlights( $built->{id} ); | ||
| 761 | foreach my $column ( map { $_->{column} } grep { defined $_->{column} } @$grid_columns ) { | ||
| 762 | my $key = $column->key; | ||
| 763 | if ( my $text = $built->{text}{$key} ) { | ||
| 764 | _set( $text, text => $model->display_of( $built->{id}, $key ) ); | ||
| 765 | next; | ||
| 766 | } | ||
| 767 | if ( defined $column->update_cell ) { | ||
| 768 | $column->update_cell->( $built->{content}{$key}, $self->_cell_context( $built->{id}, $column ) ); | ||
| 769 | next; | ||
| 770 | } | ||
| 771 | my $holder = $built->{holder}{$key}; | ||
| 772 | my $old = $built->{content}{$key}; | ||
| 773 | $holder->remove_child($old); | ||
| 774 | $holder->add_child( $self->_cell_content( $built, $column ) ); | ||
| 775 | } | ||
| 776 | return; | ||
| 777 | } | ||
| 778 | |||
| 779 | method _build_group ($line) { | ||
| 780 | my $cell = $self->_new_cell( body => $line->{key}, undef, undef ); | ||
| 781 | $cell->layout( { %{ $cell->layout }, sizing => { width => sizing_percent(1), height => sizing_fit() } } ); | ||
| 782 | my $holder = Term::Fabulous::Widget::Box->new( layout => { padding => { left => INDENT * $line->{depth} } } ); | ||
| 783 | my $toggle = Term::Fabulous::Widget::Table::Toggle->new( line_key => $line->{key} ); | ||
| 784 | my $glyph = Term::Fabulous::Widget::Text->new( text => OPEN, wrap_mode => CLAY_TEXT_WRAP_NONE ); | ||
| 785 | $toggle->add_child($glyph); | ||
| 786 | $holder->add_child($toggle); | ||
| 787 | $cell->add_child($holder); | ||
| 788 | my %built = ( kind => 'group', line => $line, spanning => 1, cells => [$cell], holder => $holder, toggle => $glyph, toggle_box => $toggle, label_signature => '' ); | ||
| 789 | $self->_refresh_group_label( \%built ); | ||
| 790 | return \%built; | ||
| 791 | } | ||
| 792 | |||
| 793 | # The label of a group header: group_label's text or widget, or | ||
| 794 | # "Title: value (count)". | ||
| 795 | method _refresh_group_label ($built) { | ||
| 796 | my $line = $built->{line}; | ||
| 797 | my $signature = join "\0", $line->{display}, $line->{count}; | ||
| 798 | return if $signature eq $built->{label_signature}; | ||
| 799 | $built->{label_signature} = $signature; | ||
| 800 | my $label = defined $group_label ? $group_label->( $self->_group_context($line) ) : $self->_default_group_label($line); | ||
| 801 | if ( !ref $label && defined $built->{label} && $built->{label}->isa('Term::Fabulous::Widget::Text') ) { | ||
| 802 | _set( $built->{label}, text => $label ); | ||
| 803 | return; | ||
| 804 | } | ||
| 805 | my $widget = ref $label ? _widget_from( 'group_label', $label ) : Term::Fabulous::Widget::Text->new( text => $label // '', bold => 1 ); | ||
| 806 | $built->{holder}->remove_child( $built->{label} ) if defined $built->{label}; | ||
| 807 | $built->{holder}->add_child($widget); | ||
| 808 | $built->{label} = $widget; | ||
| 809 | return; | ||
| 810 | } | ||
| 811 | |||
| 812 | method _group_context ($line) { | ||
| 813 | return { | ||
| 814 | column => $model->column( $line->{column} ), | ||
| 815 | value => $line->{value}, | ||
| 816 | display => $line->{display}, | ||
| 817 | count => $line->{count}, | ||
| 818 | path => [ @{ $line->{path} } ], | ||
| 819 | depth => $line->{depth}, | ||
| 820 | ids => [ @{ $line->{ids} } ], | ||
| 821 | table => $self, | ||
| 822 | }; | ||
| 823 | } | ||
| 824 | |||
| 825 | method _default_group_label ($line) { | ||
| 826 | my $title = $model->column( $line->{column} )->title; | ||
| 827 | my $shown = length $line->{display} ? $line->{display} : '(empty)'; | ||
| 828 | return "$title: $shown ($line->{count})"; | ||
| 829 | } | ||
| 830 | |||
| 831 | # --------------------------------------------------------------------- | ||
| 832 | # Dressing: colors, lines and markers of every cell | ||
| 833 | # --------------------------------------------------------------------- | ||
| 834 | |||
| 835 | # Sets a property only when it differs, so preparing a table that did | ||
| 836 | # not change makes no further frame due. | ||
| 837 | sub _set ( $widget, $property, $value ) { | ||
| 838 | my $current = $widget->$property; | ||
| 839 | return if _same( $current, $value ); | ||
| 840 | $widget->$property($value); | ||
| 841 | return; | ||
| 842 | } | ||
| 843 | |||
| 844 | sub _same ( $left, $right ) { | ||
| 845 | return !defined $right unless defined $left; | ||
| 846 | return 0 unless defined $right; | ||
| 847 | return _flat($left) eq _flat($right); | ||
| 848 | } | ||
| 849 | |||
| 850 | sub _flat ($value) { | ||
| 851 | return "$value" unless ref $value; | ||
| 852 | return 'S' . refaddr($value) if blessed $value; | ||
| 853 | return 'A' . join( "\x{1F}", map { _flat($_) } @$value ) if ref $value eq 'ARRAY'; | ||
| 854 | return 'H' . join( "\x{1F}", map { "$_=" . _flat( $value->{$_} ) } sort keys %$value ); | ||
| 855 | } | ||
| 856 | |||
| 857 | # Whether the keyboard is in the table: the table or a widget of its | ||
| 858 | # body has the focus, not the header, the column chooser or the pager. | ||
| 859 | method _has_focus_within () { | ||
| 860 | my $ui = $self->ui // return 0; | ||
| 861 | my $interaction = $ui->interaction; | ||
| 862 | return 0 unless $interaction->has_focus_within($self); | ||
| 863 | return ( none { $interaction->has_focus_within($_) } grep { defined } $_header_view, $_chooser, $_pager ) ? 1 : 0; | ||
| 864 | } | ||
| 865 | |||
| 866 | method _row_style_for ($id) { | ||
| 867 | my $dynamic = defined $row_style ? style_hash( $self, 'row_style result', row => $row_style->( $model->data_of($id), $id ) ) : {}; | ||
| 868 | return merge_styles( $_row_style_of{$id}, $dynamic ); | ||
| 869 | } | ||
| 870 | |||
| 871 | method _cell_style_for ( $id, $column ) { | ||
| 872 | my $static = $_cell_style_of{ $id . "\0" . $column->key }; | ||
| 873 | my $dynamic = defined $column->cell_style ? style_hash( $self, "column '" . $column->key . "' cell_style result", cell => $column->cell_style->( $self->_cell_context( $id, $column ) ) ) : {}; | ||
| 874 | return merge_styles( $static, $dynamic ); | ||
| 875 | } | ||
| 876 | |||
| 877 | # Whether styles depend on the data of rows (row_style, a column's | ||
| 878 | # cell_style): then a change of a row may change its lines and looks. | ||
| 879 | method _styles_follow_data ($grid_columns) { | ||
| 880 | return 1 if defined $row_style; | ||
| 881 | return ( any { defined $_->{column} && defined $_->{column}->cell_style } @$grid_columns ) ? 1 : 0; | ||
| 882 | } | ||
| 883 | |||
| 884 | # Colors, lines and marks of every cell. The grid lines are worked out | ||
| 885 | # again only when something they depend on changed; a body line is | ||
| 886 | # dressed again only when its look, the backgrounds of the lines next | ||
| 887 | # to it (see _line_backdrops) or the lines changed, so moving the | ||
| 888 | # cursor dresses the two lines and their neighbours. | ||
| 889 | method _dress ( $grid_columns, $lines ) { | ||
| 890 | my $follows_data = $self->_styles_follow_data($grid_columns); | ||
| 891 | my @header_rows = $self->_header_rows; | ||
| 892 | my $signature = join "\0", $model->columns_revision, $_style_revision, $_gutter_shown, scalar @header_rows, scalar @$grid_columns, | ||
| 893 | map { $_->{key} . ( $follows_data && $_->{kind} eq 'row' ? ':' . $model->row_revision( $_->{id} ) : '' ) } @$lines; | ||
| 894 | if ( $signature ne $_borders_signature ) { | ||
| 895 | $_borders_signature = $signature; | ||
| 896 | my $resolved = $self->_resolve_lines( $grid_columns, \@header_rows, $lines ); | ||
| 897 | ( $_borders, $_strip_borders ) = @{$resolved}{qw(lines gutter)}; | ||
| 898 | $_borders_generation++; | ||
| 899 | } | ||
| 900 | |||
| 901 | my $column_count = scalar @$grid_columns; | ||
| 902 | my $ledge_look = $self->_ledge_look; | ||
| 903 | my @header_looks = map { | ||
| 904 | my ( $cells, $part ) = @$_; | ||
| 905 | [ map { $part eq 'header' ? $self->_header_look( $_, $grid_columns->[$_] ) : $part eq 'filter' ? $self->_filter_look( $cells->[$_], $grid_columns->[$_] ) : $ledge_look } 0 .. $#$cells ] | ||
| 906 | } @header_rows; | ||
| 907 | my @footer_looks = ($ledge_look) x $column_count; | ||
| 908 | |||
| 909 | my $focused = $self->_has_focus_within; | ||
| 910 | my $cursor = $model->cursor; | ||
| 911 | my $data_index = 0; | ||
| 912 | foreach my $line (@$lines) { | ||
| 913 | my $built = $_built{ $line->{key} }; | ||
| 914 | my $state | ||
| 915 | = $focused && !defined $_header_cursor && defined $cursor && $cursor eq $line->{key} ? 'cursor' | ||
| 916 | : $line->{kind} eq 'row' && $model->is_selected( $line->{id} ) ? 'selected' | ||
| 917 | : $hover && defined $_hover_key && $_hover_key eq $line->{key} ? 'hover' | ||
| 918 | : ''; | ||
| 919 | my $stripe = $line->{kind} eq 'row' && defined $self->stripe_color && $data_index++ % 2 ? 1 : 0; | ||
| 920 | my $look_key = join "\0", $_borders_generation, $state, $stripe, $self->_line_look_key($line); | ||
| 921 | next if defined $built->{look_key} && $built->{look_key} eq $look_key; | ||
| 922 | $built->{look_key} = $look_key; | ||
| 923 | $built->{looks} = $self->_line_looks( $line, $grid_columns, $state, $stripe ); | ||
| 924 | $built->{backgrounds} = _backgrounds( $built->{looks}, $column_count ); | ||
| 925 | } | ||
| 926 | |||
| 927 | my @backgrounds = ( ( map { _backgrounds( $_, $column_count ) } @header_looks ), ( map { $_built{ $_->{key} }{backgrounds} } @$lines ), _backgrounds( \@footer_looks, $column_count ) ); | ||
| 928 | my $base = $self->_base_background; | ||
| 929 | my $backdrops_of = sub ($at) { | ||
| 930 | my $borders = $_borders->[$at]; | ||
| 931 | my @spans = @$borders == $column_count ? map { [ $_, $_ ] } 0 .. $column_count - 1 : ( [ 0, $column_count - 1 ] ); | ||
| 932 | return [ map { _line_backdrops( \@backgrounds, scalar @header_rows, $at, @{ $spans[$_] }, $borders->[$_], $base ) } 0 .. $#$borders ]; | ||
| 933 | }; | ||
| 934 | |||
| 935 | foreach my $index ( 0 .. $#header_rows ) { | ||
| 936 | my $cells = $header_rows[$index][0]; | ||
| 937 | my $backdrops = $backdrops_of->($index); | ||
| 938 | foreach my $column ( 0 .. $#$cells ) { | ||
| 939 | $self->_dress_cell( $cells->[$column]{cell}, $header_looks[$index][$column], $_borders->[$index][$column], $backdrops->[$column], \$cells->[$column]{dressed} ); | ||
| 940 | } | ||
| 941 | } | ||
| 942 | $self->_mark_header($grid_columns) if @_header_cells; | ||
| 943 | |||
| 944 | my $footer_at = @header_rows + @$lines; | ||
| 945 | my $footer_backdrops = $backdrops_of->($footer_at); | ||
| 946 | $self->_dress_cell( $_footer_cells[$_]{cell}, $footer_looks[$_], $_borders->[$footer_at][$_], $footer_backdrops->[$_], \$_footer_cells[$_]{dressed} ) foreach 0 .. $#_footer_cells; | ||
| 947 | $self->_dress_strips( \@header_rows, $base ) if $_gutter_shown; | ||
| 948 | |||
| 949 | my @background_keys = map { join "\x{1F}", @$_ } @backgrounds; | ||
| 950 | my $base_key = _flat($base); | ||
| 951 | foreach my $index ( 0 .. $#$lines ) { | ||
| 952 | my ( $line, $at ) = ( $lines->[$index], @header_rows + $index ); | ||
| 953 | my $built = $_built{ $line->{key} }; | ||
| 954 | my $dress_key = join "\0", $built->{look_key}, $base_key, map { $background_keys[$_] // '' } grep { $_ >= 0 } $at - 1, $at + 1; | ||
| 955 | next if defined $built->{dress_key} && $built->{dress_key} eq $dress_key; | ||
| 956 | $built->{dress_key} = $dress_key; | ||
| 957 | $self->_dress_line( $built, $line, $grid_columns, $built->{looks}, $_borders->[$at], $backdrops_of->($at) ); | ||
| 958 | } | ||
| 959 | return; | ||
| 960 | } | ||
| 961 | |||
| 962 | # The rows of the header grid as built, [ cells, part ] each: the | ||
| 963 | # column titles and the filter row, or the ledge instead of both. | ||
| 964 | method _header_rows () { | ||
| 965 | return ( ( @_header_cells ? [ \@_header_cells, 'header' ] : () ), ( @_filter_cells ? [ \@_filter_cells, 'filter' ] : () ), ( @_top_ledge_cells ? [ \@_top_ledge_cells, 'ledge' ] : () ) ); | ||
| 966 | } | ||
| 967 | |||
| 968 | # The strips of the scrollbar column: the frame's right side and the | ||
| 969 | # lines that run on beside the header and the footer, on the table's | ||
| 970 | # background. Inside the header strip, a blank column in the background | ||
| 971 | # of each header row, so that the rows reach the frame. | ||
| 972 | method _dress_strips ( $header_rows, $base ) { | ||
| 973 | my $look = { background_color => $base, border_color => $self->line_color }; | ||
| 974 | my %strip = ( header => $_header_strip, body => $_body_strip, footer => $_footer_strip ); | ||
| 975 | $self->_dress_cell( $strip{$_}, $look, $_strip_borders->{$_}, undef, \$_strip_dressed{$_} ) foreach sort keys %strip; | ||
| 976 | |||
| 977 | my $filter_height = 1 + $cell_padding->{top} + $cell_padding->{bottom}; | ||
| 978 | my @fills = map { | ||
| 979 | my $part = $_->[1]; | ||
| 980 | $part eq 'header' ? [ $header_style->{background_color} // $self->header_background_color, sizing_grow() ] | ||
| 981 | : $part eq 'filter' ? [ $self->filter_background_color, sizing_fixed($filter_height) ] | ||
| 982 | : () | ||
| 983 | } @$header_rows; | ||
| 984 | my $signature = join "\0", map { _flat($_) } @fills; | ||
| 985 | return if $signature eq $_header_fills_signature; | ||
| 986 | $_header_fills_signature = $signature; | ||
| 987 | $_header_fills->clear_children; | ||
| 988 | $_header_fills->add_child( Term::Fabulous::Widget::Box->new( background_color => $_->[0], layout => { sizing => { width => sizing_grow(), height => $_->[1] } } ) ) foreach @fills; | ||
| 989 | return; | ||
| 990 | } | ||
| 991 | |||
| 992 | # The background of every grid column of a line, as comparable keys: a | ||
| 993 | # line across the table has one look for all columns. | ||
| 994 | sub _backgrounds ( $looks, $column_count ) { | ||
| 995 | my @keys = map { _flat( $_->{background_color} ) } @$looks; | ||
| 996 | return @keys == 1 ? [ ( $keys[0] ) x $column_count ] : \@keys; | ||
| 997 | } | ||
| 998 | |||
| 999 | # A line between two cells of different backgrounds takes neither: the | ||
| 1000 | # drawn sides and corners of a cell inside the frame whose cells around | ||
| 1001 | # them do not all share one background get the table's background | ||
| 1002 | # ($base). The line below the header rows always does, as the body | ||
| 1003 | # scrolls under it. Takes the background keys of every line (header | ||
| 1004 | # rows first) per grid column, the number of header rows, the cell (its | ||
| 1005 | # line and first and last grid column) and its lines (from | ||
| 1006 | # resolve_borders); returns a hash for border_background_colors, or | ||
| 1007 | # undef when every line keeps the cell's own background. | ||
| 1008 | sub _line_backdrops ( $backgrounds, $header_lines, $line, $first, $last, $border, $base ) { | ||
| 1009 | my $sides = $border->{sides}; | ||
| 1010 | my %inside = ( top => $line > 0, bottom => $line < $#$backgrounds, left => $first > 0, right => $last < $#{ $backgrounds->[$line] } ); | ||
| 1011 | my %lines_of = ( top => [ $line - 1, $line ], bottom => [ $line, $line + 1 ] ); | ||
| 1012 | my %columns_of = ( left => [ $first - 1, $first ], right => [ $last, $last + 1 ] ); | ||
| 1013 | my %backdrops; | ||
| 1014 | foreach my $name (qw(top bottom left right top_left top_right bottom_left bottom_right)) { | ||
| 1015 | my @parts = split /_/, $name; | ||
| 1016 | next unless all { $sides->{$_} && $inside{$_} } @parts; | ||
| 1017 | my ($across) = grep { $lines_of{$_} } @parts; | ||
| 1018 | my ($along) = grep { $columns_of{$_} } @parts; | ||
| 1019 | my @lines = defined $across ? @{ $lines_of{$across} } : ($line); | ||
| 1020 | my @columns = defined $along ? @{ $columns_of{$along} } : ( $first .. $last ); | ||
| 1021 | my @around = map { @{ $backgrounds->[$_] }[@columns] } @lines; | ||
| 1022 | my $scrolls = defined $across && $lines[0] == $header_lines - 1; | ||
| 1023 | next if !$scrolls && all { $_ eq $around[0] } @around; | ||
| 1024 | $backdrops{$name} = $base; | ||
| 1025 | } | ||
| 1026 | return %backdrops ? \%backdrops : undef; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | # What else a line's look depends on. | ||
| 1030 | method _line_look_key ($line) { | ||
| 1031 | return join "\0", $model->row_revision( $line->{id} ), $line->{has_children}, $line->{expanded}, $model->is_selected( $line->{id} ) if $line->{kind} eq 'row'; | ||
| 1032 | return join "\0", $line->{expanded}, $line->{count}, $line->{display} if $line->{kind} eq 'group'; | ||
| 1033 | return $model->is_filtered && $model->row_count ? 'no match' : 'empty'; | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | method _state_color ($state) { | ||
| 1037 | return $state eq 'cursor' ? $self->cursor_color : $state eq 'selected' ? $self->selected_color : $state eq 'hover' ? $self->hover_color : undef; | ||
| 1038 | } | ||
| 1039 | |||
| 1040 | # The grid lines of the header rows, the lines and the footer, and the | ||
| 1041 | # strips of the scrollbar column while it is shown. | ||
| 1042 | method _resolve_lines ( $grid_columns, $header_rows, $lines ) { | ||
| 1043 | my @no_styles = map { {} } @$grid_columns; | ||
| 1044 | my @border_lines = map { $_->[1] eq 'ledge' ? { ledge => 1, cells => [@no_styles] } : { style => $header_style, cells => [@no_styles] } } @$header_rows; | ||
| 1045 | foreach my $line (@$lines) { | ||
| 1046 | if ( $line->{kind} eq 'row' ) { | ||
| 1047 | my $id = $line->{id}; | ||
| 1048 | push @border_lines, { | ||
| 1049 | style => $self->_row_style_for($id), | ||
| 1050 | cells => [ map { defined $_->{column} ? $self->_cell_style_for( $id, $_->{column} ) : {} } @$grid_columns ], | ||
| 1051 | }; | ||
| 1052 | next; | ||
| 1053 | } | ||
| 1054 | push @border_lines, { spanning => 1, style => $line->{kind} eq 'group' ? $group_style : {}, cells => [ {} ] }; | ||
| 1055 | } | ||
| 1056 | push @border_lines, { ledge => 1, cells => [@no_styles] }; | ||
| 1057 | return resolve_borders( | ||
| 1058 | columns => scalar @$grid_columns, | ||
| 1059 | header => scalar @$header_rows, | ||
| 1060 | gutter => $_gutter_shown, | ||
| 1061 | table => \%_line_style, | ||
| 1062 | column_styles => [ map { defined $_->{column} ? $_->{column}->style : {} } @$grid_columns ], | ||
| 1063 | lines => \@border_lines, | ||
| 1064 | ); | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | # The look of every cell of a line: one per grid column for a data | ||
| 1068 | # row, one for a line across the table. | ||
| 1069 | method _line_looks ( $line, $grid_columns, $state, $stripe ) { | ||
| 1070 | my $state_color = $self->_state_color($state); | ||
| 1071 | return [ $line->{kind} eq 'group' ? $self->_group_look($state_color) : $self->_empty_look ] unless $line->{kind} eq 'row'; | ||
| 1072 | my $id = $line->{id}; | ||
| 1073 | my $row = $self->_row_style_for($id); | ||
| 1074 | return [ | ||
| 1075 | map { | ||
| 1076 | my $cell = defined $grid_columns->[$_]{column} ? $self->_cell_style_for( $id, $grid_columns->[$_]{column} ) : {}; | ||
| 1077 | $self->_body_look( $state_color // ( $stripe ? $self->stripe_color : undef ), $row, $cell, $grid_columns->[$_]{column}, defined $state_color ); | ||
| 1078 | } 0 .. $#$grid_columns | ||
| 1079 | ]; | ||
| 1080 | } | ||
| 1081 | |||
| 1082 | method _dress_line ( $built, $line, $grid_columns, $looks, $borders, $backdrops ) { | ||
| 1083 | if ( $line->{kind} eq 'row' ) { | ||
| 1084 | $built->{dressed} //= []; | ||
| 1085 | $self->_dress_cell( $built->{cells}[$_], $looks->[$_], $borders->[$_], $backdrops->[$_], \$built->{dressed}[$_] ) foreach 0 .. $#$looks; | ||
| 1086 | $self->_mark_row( $built, $line, $grid_columns, $looks ); | ||
| 1087 | return; | ||
| 1088 | } | ||
| 1089 | my $look = $looks->[0]; | ||
| 1090 | $self->_dress_cell( $built->{cells}[0], $look, $borders->[0], $backdrops->[0], \$built->{dressed} ); | ||
| 1091 | if ( $line->{kind} eq 'group' ) { | ||
| 1092 | $self->_refresh_group_label($built); | ||
| 1093 | _set( $built->{toggle}, text => $line->{expanded} ? OPEN : CLOSED ); | ||
| 1094 | $self->_dress_text( $built->{toggle}, $look ); | ||
| 1095 | _set( $built->{toggle_box}, background_color => $look->{background_color} ); | ||
| 1096 | } | ||
| 1097 | else { | ||
| 1098 | _set( $built->{label}, text => $model->is_filtered && $model->row_count ? $no_match_text : $empty_text ); | ||
| 1099 | } | ||
| 1100 | $self->_dress_text( $built->{label}, $look ) if defined $built->{label} && $built->{label}->isa('Term::Fabulous::Widget::Text'); | ||
| 1101 | return; | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | method _header_look ( $index, $grid_column ) { | ||
| 1105 | my $column = $grid_column->{column}; | ||
| 1106 | my $style = merge_styles( defined $column ? $column->header_style : {}, $header_style ); | ||
| 1107 | my $active = defined $_header_cursor && $_header_cursor == $index && $self->_has_focus_within; | ||
| 1108 | return { | ||
| 1109 | background_color => $active ? $self->cursor_color : $style->{background_color} // $self->header_background_color, | ||
| 1110 | text_color => $style->{text_color} // $self->header_text_color, | ||
| 1111 | bold => $style->{bold} // 1, | ||
| 1112 | italic => $style->{italic} // 0, | ||
| 1113 | underline => $style->{underline} // 0, | ||
| 1114 | border_color => $style->{border_color} // $self->line_color, | ||
| 1115 | }; | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | method _filter_look ( $cell, $grid_column ) { | ||
| 1119 | my $column = $grid_column->{column}; | ||
| 1120 | if ( defined $cell->{field} ) { | ||
| 1121 | my $error = defined $_filter_error{ $column->key }; | ||
| 1122 | _set( $cell->{field}, text_color => $error ? $self->error_color : $self->text_color ); | ||
| 1123 | } | ||
| 1124 | return { background_color => $self->filter_background_color, border_color => $self->line_color }; | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | method _group_look ($state_color) { | ||
| 1128 | my $style = $group_style; | ||
| 1129 | return { | ||
| 1130 | background_color => $state_color // $style->{background_color} // $self->group_background_color, | ||
| 1131 | text_color => $style->{text_color} // $self->group_text_color, | ||
| 1132 | bold => $style->{bold} // 1, | ||
| 1133 | italic => $style->{italic} // 0, | ||
| 1134 | underline => $style->{underline} // 0, | ||
| 1135 | border_color => $style->{border_color} // $self->line_color, | ||
| 1136 | }; | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | method _body_look ( $base_color, $row, $cell, $column, $state_wins ) { | ||
| 1140 | my $column_style = defined $column ? $column->style : {}; | ||
| 1141 | my $look = merge_styles( $cell, $row, $column_style ); | ||
| 1142 | my $background = ( $state_wins ? $base_color : $look->{background_color} // $base_color ) // $self->_base_background; | ||
| 1143 | return { | ||
| 1144 | background_color => $background, | ||
| 1145 | text_color => $look->{text_color} // $self->text_color, | ||
| 1146 | bold => $look->{bold} // 0, | ||
| 1147 | italic => $look->{italic} // 0, | ||
| 1148 | underline => $look->{underline} // 0, | ||
| 1149 | border_color => $look->{border_color} // $self->line_color, | ||
| 1150 | }; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | # Gives a cell its look and lines, and the background of the lines it | ||
| 1154 | # draws between itself and cells of other backgrounds ($backdrops, see | ||
| 1155 | # _line_backdrops); $memo holds what it was given last time, so an | ||
| 1156 | # unchanged cell costs one string comparison. | ||
| 1157 | method _dress_cell ( $cell, $look, $border, $backdrops, $memo ) { | ||
| 1158 | my $sides = $border->{sides}; | ||
| 1159 | my %properties = ( | ||
| 1160 | background_color => $look->{background_color}, | ||
| 1161 | border_background_colors => $backdrops, | ||
| 1162 | border_color => $look->{border_color}, | ||
| 1163 | bordered => { map { $_ => $sides->{$_} ? 1 : 0 } qw(top right bottom left) }, | ||
| 1164 | border_corners => %{ $border->{corners} } ? $border->{corners} : undef, | ||
| 1165 | outer_border_sides => $border->{outer}, | ||
| 1166 | map { ( "border_style_$_" => $sides->{$_} ) } qw(top right bottom left), | ||
| 1167 | ); | ||
| 1168 | my $signature = join "\x{1E}", map { "$_=" . ( defined $properties{$_} ? _flat( $properties{$_} ) : '' ) } sort keys %properties; | ||
| 1169 | return if defined $$memo && $$memo eq $signature; | ||
| 1170 | $$memo = $signature; | ||
| 1171 | $cell->$_( $properties{$_} ) foreach sort keys %properties; | ||
| 1172 | return; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | method _dress_text ( $text, $look ) { | ||
| 1176 | _set( $text, text_color => $look->{text_color} ); | ||
| 1177 | _set( $text, $_ => $look->{$_} // 0 ) foreach qw(bold italic underline); | ||
| 1178 | return; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | # The marks of a data row - the selection mark, the tree marker - and | ||
| 1182 | # the look of its texts. | ||
| 1183 | method _mark_row ( $built, $line, $grid_columns, $looks ) { | ||
| 1184 | my $tree_key = $self->_tree_column_key // "\0"; | ||
| 1185 | foreach my $index ( 0 .. $#$grid_columns ) { | ||
| 1186 | my ( $key, $look ) = ( $grid_columns->[$index]{key}, $looks->[$index] ); | ||
| 1187 | if ( $key eq SELECT_COLUMN ) { | ||
| 1188 | _set( $built->{mark}, text => $model->is_selected( $line->{id} ) ? MARK_CHECKED : MARK_UNCHECKED ); | ||
| 1189 | $self->_dress_text( $built->{mark}, $look ); | ||
| 1190 | next; | ||
| 1191 | } | ||
| 1192 | $self->_dress_text( $built->{text}{$key}, $look ) if defined $built->{text}{$key}; | ||
| 1193 | next unless $key eq $tree_key && defined $built->{toggle}; | ||
| 1194 | _set( $built->{toggle}, text => !$line->{has_children} ? LEAF : $line->{expanded} ? OPEN : CLOSED ); | ||
| 1195 | $self->_dress_text( $built->{toggle}, $look ); | ||
| 1196 | _set( $built->{toggle_box}, background_color => $look->{background_color} ); | ||
| 1197 | } | ||
| 1198 | return; | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | method _ledge_look () { | ||
| 1202 | return { background_color => $self->_base_background, border_color => $self->line_color }; | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | method _empty_look () { | ||
| 1206 | return { background_color => $self->_base_background, text_color => $self->muted_color, border_color => $self->line_color, italic => 1 }; | ||
| 1207 | } | ||
| 1208 | |||
| 1209 | # The color of cells that have none of their own: what the table lies | ||
| 1210 | # on (background_below), so the cells blend in; a fixed dark color only | ||
| 1211 | # where there is none (Term::Fabulous::Static, no UI). Cells are always | ||
| 1212 | # painted: only painted cells receive the mouse. | ||
| 1213 | method _base_background () { | ||
| 1214 | return $self->background_below // FALLBACK_BACKGROUND; | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | method _mark_header ($grid_columns) { | ||
| 1218 | my @sort = @{ $model->sort_spec }; | ||
| 1219 | foreach my $index ( 0 .. $#_header_cells ) { | ||
| 1220 | my $cell = $_header_cells[$index]; | ||
| 1221 | my $look = $self->_header_look( $index, $grid_columns->[$index] ); | ||
| 1222 | if ( defined $cell->{mark} ) { | ||
| 1223 | _set( $cell->{mark}, text => $self->_select_all_mark ); | ||
| 1224 | $self->_dress_text( $cell->{mark}, $look ); | ||
| 1225 | next; | ||
| 1226 | } | ||
| 1227 | my $key = $grid_columns->[$index]{key}; | ||
| 1228 | my ($place) = grep { $sort[$_][0] eq $key } 0 .. $#sort; | ||
| 1229 | my $marker = !defined $place ? '' : ' ' . ( $sort[$place][1] eq 'asc' ? SORT_UP : SORT_DOWN ) . ( @sort > 1 ? $place + 1 : '' ); | ||
| 1230 | _set( $cell->{marker}, text => $marker ); | ||
| 1231 | $self->_dress_text( $cell->{marker}, $look ); | ||
| 1232 | $self->_dress_text( $cell->{title}, $look ) if defined $cell->{title}; | ||
| 1233 | } | ||
| 1234 | return; | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | method _select_all_mark () { | ||
| 1238 | my @ids = $model->filtered_row_ids; | ||
| 1239 | my $selected = grep { $model->is_selected($_) } @ids; | ||
| 1240 | return !@ids || !$selected ? MARK_UNCHECKED : $selected == @ids ? MARK_CHECKED : MARK_SOME; | ||
| 1241 | } | ||
| 1242 | |||
| 1243 | # --------------------------------------------------------------------- | ||
| 1244 | # Scrollbar, pager, scrolling | ||
| 1245 | # --------------------------------------------------------------------- | ||
| 1246 | |||
| 1247 | # The scrollbar column, while the rows did not fit in the last frame: | ||
| 1248 | # the strips beside the header, the body and the footer. | ||
| 1249 | method _prepare_gutter () { | ||
| 1250 | $_gutter_shown = $self->_gutter_wanted; | ||
| 1251 | _show_child( $_header_row, $_header_strip, $_gutter_shown ); | ||
| 1252 | _show_child( $_body_row, $_body_strip, $_gutter_shown ); | ||
| 1253 | _show_child( $_footer_row, $_footer_strip, $_gutter_shown ); | ||
| 1254 | return; | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | method _gutter_wanted () { | ||
| 1258 | return 0 unless $scrollbar; | ||
| 1259 | my $ui = $self->ui // return 0; | ||
| 1260 | my $state = $ui->scroll_state($_body) // return 0; | ||
| 1261 | return $state->{content}{height} > $state->{viewport}{height} ? 1 : 0; | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | # Whether the rows fit is known after a frame; one that changes it | ||
| 1265 | # prepares the next. | ||
| 1266 | method _watch_gutter () { | ||
| 1267 | $self->each_frame( | ||
| 1268 | sub ($table) { | ||
| 1269 | $table->request_prepare if $table->_gutter_wanted != $_gutter_shown; | ||
| 1270 | return 0; | ||
| 1271 | }, | ||
| 1272 | name => 'gutter' | ||
| 1273 | ); | ||
| 1274 | return; | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | sub _show_child ( $parent, $child, $show ) { | ||
| 1278 | $parent->remove_child($child) unless $show; | ||
| 1279 | $parent->add_child($child) if $show && !$parent->has_child($child); | ||
| 1280 | return; | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | method _pager_shown () { | ||
| 1284 | return $show_pager // ( $model->page_size > 0 ? 1 : 0 ); | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | method _prepare_pager () { | ||
| 1288 | if ( !$self->_pager_shown ) { | ||
| 1289 | $self->remove_child($_pager); | ||
| 1290 | return; | ||
| 1291 | } | ||
| 1292 | $self->add_child($_pager) unless $self->has_child($_pager); | ||
| 1293 | my $size = $model->page_size; | ||
| 1294 | my $total = $model->line_count; | ||
| 1295 | my $first = $size ? ( $model->page - 1 ) * $size + 1 : 1; | ||
| 1296 | my $last = $size ? min( $total, $first + $size - 1 ) : $total; | ||
| 1297 | $_pager->show( page => $model->page, page_count => $model->page_count, page_size => $size || $total || 1, first => $first, last => $last, total => $total ); | ||
| 1298 | return; | ||
| 1299 | } | ||
| 1300 | |||
| 1301 | # A new page starts at the top. | ||
| 1302 | method _prepare_scrolling () { | ||
| 1303 | my $ui = $self->ui // return; | ||
| 1304 | return if $model->page == $_shown_page; | ||
| 1305 | $_shown_page = $model->page; | ||
| 1306 | $ui->scroll_to( $_body, { x => 0, y => 0 } ); | ||
| 1307 | return; | ||
| 1308 | } | ||
| 1309 | |||
| 1310 | # A cursor that moved is scrolled into view once the frame that lays | ||
| 1311 | # its line out is drawn: the line's cells may be new, or the last | ||
| 1312 | # frame put them elsewhere (a sort, a new page). Before the table is | ||
| 1313 | # in a UI, that is the first frame. | ||
| 1314 | method _reveal_cursor_after_frame () { | ||
| 1315 | $self->each_frame( sub ($table) { $table->_reveal_cursor_line; return 1 }, name => 'cursor' ); | ||
| 1316 | return; | ||
| 1317 | } | ||
| 1318 | |||
| 1319 | # The body reveals the first cell of the cursor's line. | ||
| 1320 | method _reveal_cursor_line () { | ||
| 1321 | my $built = $_built{ $model->cursor // '' } // return; | ||
| 1322 | $_body->reveal( $built->{cells}[0] ); | ||
| 1323 | return; | ||
| 1324 | } | ||
| 1325 | |||
| 1326 | # How many lines of the page fit into the body's viewport, for PageUp | ||
| 1327 | # and PageDown. | ||
| 1328 | method _page_step () { | ||
| 1329 | my $ui = $self->ui // return DEFAULT_PAGE_STEP; | ||
| 1330 | my $state = $ui->scroll_state($_body) // return DEFAULT_PAGE_STEP; | ||
| 1331 | my @heights = grep { defined } map { my $built = $_built{$_}; defined $built ? ( $ui->bounding_box( $built->{cells}[0] ) // {} )->{height} : undef } @_grid_keys; | ||
| 1332 | return DEFAULT_PAGE_STEP unless @heights; | ||
| 1333 | my $average = ( List::Util::sum(@heights) / @heights ) || 1; | ||
| 1334 | return max( 1, int( $state->{viewport}{height} / $average ) - 1 ); | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | # --------------------------------------------------------------------- | ||
| 1338 | # Input | ||
| 1339 | # --------------------------------------------------------------------- | ||
| 1340 | |||
| 1341 | method _listen () { | ||
| 1342 | $self->on( KeyPress => sub ( $self, $event ) { return $self->_on_key($event) }, owner => $self ); | ||
| 1343 | $self->on( Mouse => sub ( $self, $event ) { return $self->_on_mouse($event) }, owner => $self ); | ||
| 1344 | $self->on( MouseMove => sub ( $self, $event ) { $self->_on_mouse_move($event); return $CONTINUE }, owner => $self ); | ||
| 1345 | foreach my $name (qw(OnFocus OnBlur)) { | ||
| 1346 | $self->on( $name => sub ( $self, $event ) { $self->_changed; return $CONTINUE }, owner => $self ); | ||
| 1347 | } | ||
| 1348 | $self->on( OnHoverStopped => sub ( $self, $event ) { $self->_hover_line(undef) if refaddr( $event->target ) == refaddr($self); return $CONTINUE }, owner => $self ); | ||
| 1349 | return; | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | # Which part of the table a widget belongs to. | ||
| 1353 | method _region_of ($widget) { | ||
| 1354 | my %region = ( refaddr($_body_row) => 'body', refaddr($_header_view) => 'header', refaddr($_pager) => 'pager', refaddr($self) => 'table' ); | ||
| 1355 | $region{ refaddr $_chooser } = 'chooser' if defined $_chooser; | ||
| 1356 | my $part = $widget->closest( sub ($node) { exists $region{ refaddr $node } } ) // return 'table'; | ||
| 1357 | return $region{ refaddr $part }; | ||
| 1358 | } | ||
| 1359 | |||
| 1360 | # Whether a widget between $widget and the table takes input of its own | ||
| 1361 | # (a button or a field in a cell). | ||
| 1362 | # A widget without a background of its own is not hit by the mouse: a | ||
| 1363 | # click on such a button makes its cell the target, so the widgets of | ||
| 1364 | # the cell under the pointer count, too. | ||
| 1365 | method _inside_input ($event) { | ||
| 1366 | my $target = $event->target; | ||
| 1367 | foreach my $node ( $target, $target->ancestors ) { | ||
| 1368 | last if refaddr($node) == refaddr($self); | ||
| 1369 | return 1 if _takes_input($node); | ||
| 1370 | return $self->_input_under_pointer( $node, $event->x, $event->y ) if $node->isa('Term::Fabulous::Widget::Table::Cell'); | ||
| 1371 | } | ||
| 1372 | return 0; | ||
| 1373 | } | ||
| 1374 | |||
| 1375 | sub _takes_input ($widget) { | ||
| 1376 | return $widget->DOES('Clay::UI::Role::Interaction::Pressable') || ( $widget->DOES('Clay::UI::Role::Interaction::Focusable') && $widget->can_focus ) ? 1 : 0; | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | method _input_under_pointer ( $widget, $x, $y ) { | ||
| 1380 | my $ui = $self->ui // return 0; | ||
| 1381 | return 0 unless $widget->can('children'); | ||
| 1382 | foreach my $child ( @{ $widget->children } ) { | ||
| 1383 | my $box = $ui->bounding_box($child) // next; | ||
| 1384 | next unless $x >= $box->{x} && $x < $box->{x} + $box->{width} && $y >= $box->{y} && $y < $box->{y} + $box->{height}; | ||
| 1385 | return 1 if _takes_input($child) || $self->_input_under_pointer( $child, $x, $y ); | ||
| 1386 | } | ||
| 1387 | return 0; | ||
| 1388 | } | ||
| 1389 | |||
| 1390 | method _on_key ($event) { | ||
| 1391 | my $name = $event->key_name // return $CONTINUE; | ||
| 1392 | my $region = $self->_region_of( $event->target ); | ||
| 1393 | if ( $region eq 'header' ) { | ||
| 1394 | return $self->_on_filter_key( $event, $name ); | ||
| 1395 | } | ||
| 1396 | return $CONTINUE unless $region eq 'table' || $region eq 'body'; | ||
| 1397 | return $self->_on_header_key($name) if defined $_header_cursor; | ||
| 1398 | my $from_cell = $region eq 'body' && refaddr( $event->target ) != refaddr($self); | ||
| 1399 | return $CONTINUE unless $self->_on_body_key( $name, $from_cell ); | ||
| 1400 | $self->_focus_self if $from_cell; # the keyboard follows the cursor out of the cell | ||
| 1401 | return $HANDLED; | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | method _on_filter_key ( $event, $name ) { | ||
| 1405 | if ( $name eq 'Down' ) { | ||
| 1406 | $self->_focus_self; | ||
| 1407 | return $HANDLED; | ||
| 1408 | } | ||
| 1409 | if ( $name eq 'Escape' ) { | ||
| 1410 | my ($cell) = grep { defined $_->{field} && refaddr( $_->{field} ) == refaddr( $event->target ) } @_filter_cells; | ||
| 1411 | return $CONTINUE unless defined $cell && length $cell->{field}->value; | ||
| 1412 | $cell->{field}->value(''); | ||
| 1413 | $self->_filter_typed( $cell->{cell}->column_key, '' ); | ||
| 1414 | return $HANDLED; | ||
| 1415 | } | ||
| 1416 | return $CONTINUE; | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | method _focus_self () { | ||
| 1420 | my $ui = $self->ui // return; | ||
| 1421 | $ui->interaction->set_focused_widget($self) if $self->can_focus; | ||
| 1422 | return; | ||
| 1423 | } | ||
| 1424 | |||
| 1425 | # Keys on the rows. Returns whether the key was used. $from_cell: the | ||
| 1426 | # key comes from a widget inside a cell, which has used the keys it | ||
| 1427 | # wants; Space and Enter are left to it. | ||
| 1428 | method _on_body_key ( $name, $from_cell ) { | ||
| 1429 | my $cursor = $model->cursor; | ||
| 1430 | my $line = defined $cursor ? $model->line($cursor) : undef; | ||
| 1431 | my %move = ( | ||
| 1432 | Up => -1, | ||
| 1433 | Down => 1, | ||
| 1434 | PageUp => -$self->_page_step, | ||
| 1435 | PageDown => $self->_page_step, | ||
| 1436 | ); | ||
| 1437 | my $base = $name =~ s/\AShift\+//r; | ||
| 1438 | my $extend = $base ne $name && $selection eq 'multiple'; | ||
| 1439 | if ( exists $move{$base} && ( $base eq $name || $extend ) ) { | ||
| 1440 | return $self->_enter_header if $base eq 'Up' && defined $line && $self->_is_first_on_page($cursor) && !$extend && $self->_header_usable; | ||
| 1441 | $self->_move_within_page( $move{$base}, $extend ); | ||
| 1442 | return 1; | ||
| 1443 | } | ||
| 1444 | if ( ( $base eq 'Home' || $base eq 'End' ) && ( $base eq $name || $extend ) ) { | ||
| 1445 | my @lines = $model->page_lines; | ||
| 1446 | return 1 unless @lines; | ||
| 1447 | $self->_move_cursor( ( $base eq 'Home' ? $lines[0] : $lines[-1] )->{key}, extend => $extend ); | ||
| 1448 | return 1; | ||
| 1449 | } | ||
| 1450 | my %jump = ( | ||
| 1451 | 'Ctrl+Home' => sub { $self->_move_cursor( $model->first_line_key ) }, | ||
| 1452 | 'Ctrl+End' => sub { $self->_move_cursor( $model->last_line_key ) }, | ||
| 1453 | 'Ctrl+PageUp' => sub { $self->_turn_page( $model->page - 1 ) }, | ||
| 1454 | 'Ctrl+PageDown' => sub { $self->_turn_page( $model->page + 1 ) }, | ||
| 1455 | ); | ||
| 1456 | if ( exists $jump{$name} ) { | ||
| 1457 | $jump{$name}->(); | ||
| 1458 | return 1; | ||
| 1459 | } | ||
| 1460 | return $self->_select_all_by_user if $name eq 'Ctrl+A' && $selection eq 'multiple'; | ||
| 1461 | return 0 unless defined $line; | ||
| 1462 | |||
| 1463 | if ( $line->{kind} eq 'group' ) { | ||
| 1464 | return $self->_toggle_line( $line, !$line->{expanded} ) if $name eq 'Enter' || $name eq 'Space'; | ||
| 1465 | return $self->_toggle_line( $line, 1 ) if $name eq '+' || ( $name eq 'Right' && !$line->{expanded} ); | ||
| 1466 | return $self->_toggle_line( $line, 0 ) if $name eq '-' || ( $name eq 'Left' && $line->{expanded} ); | ||
| 1467 | if ( $name eq 'Right' ) { | ||
| 1468 | $self->_move_cursor( $self->_line_after($cursor) ); | ||
| 1469 | return 1; | ||
| 1470 | } | ||
| 1471 | return $self->_move_to_parent_group($line) if $name eq 'Left'; | ||
| 1472 | return 0; | ||
| 1473 | } | ||
| 1474 | return 0 unless $line->{kind} eq 'row'; | ||
| 1475 | if ( $name eq 'Enter' && !$from_cell ) { | ||
| 1476 | $self->fire_event( Term::Fabulous::Event::RowActivate->new( row_id => $line->{id}, row => $model->row( $line->{id} ) ) ); | ||
| 1477 | return 1; | ||
| 1478 | } | ||
| 1479 | if ( $name eq 'Space' && !$from_cell ) { | ||
| 1480 | return $self->_toggle_selection_by_user( $line->{id} ) if $selection eq 'multiple'; | ||
| 1481 | return $self->_select_by_user( $line->{id} ) if $selection eq 'single'; | ||
| 1482 | return 0; | ||
| 1483 | } | ||
| 1484 | return $self->_toggle_line( $line, 1 ) if ( $name eq '+' || $name eq 'Right' ) && $line->{has_children} && !$line->{expanded}; | ||
| 1485 | return $self->_toggle_line( $line, 0 ) if ( $name eq '-' || $name eq 'Left' ) && $line->{has_children} && $line->{expanded}; | ||
| 1486 | if ( $name eq 'Right' && $line->{has_children} ) { | ||
| 1487 | $self->_move_cursor( $self->_line_after($cursor) ); | ||
| 1488 | return 1; | ||
| 1489 | } | ||
| 1490 | return $self->_move_to_parent($line) if $name eq 'Left' && ( defined $model->parent_of( $line->{id} ) || $model->group_by ); | ||
| 1491 | return 0; | ||
| 1492 | } | ||
| 1493 | |||
| 1494 | method _header_usable () { | ||
| 1495 | return $show_header && @_header_cells ? 1 : 0; | ||
| 1496 | } | ||
| 1497 | |||
| 1498 | method _is_first_on_page ($key) { | ||
| 1499 | my ($first) = $model->page_lines; | ||
| 1500 | return defined $first && $first->{key} eq $key ? 1 : 0; | ||
| 1501 | } | ||
| 1502 | |||
| 1503 | method _line_after ($key) { | ||
| 1504 | my @lines = $model->page_lines; | ||
| 1505 | my $index = first { $lines[$_]{key} eq $key } 0 .. $#lines; | ||
| 1506 | return defined $index && $index < $#lines ? $lines[ $index + 1 ]{key} : $key; | ||
| 1507 | } | ||
| 1508 | |||
| 1509 | method _move_within_page ( $steps, $extend ) { | ||
| 1510 | my @lines = $model->page_lines; | ||
| 1511 | return unless @lines; | ||
| 1512 | my $cursor = $model->cursor; | ||
| 1513 | my $index = first { $lines[$_]{key} eq $cursor } 0 .. $#lines; | ||
| 1514 | $index = max( 0, min( $#lines, ( $index // 0 ) + $steps ) ); | ||
| 1515 | $self->_move_cursor( $lines[$index]{key}, extend => $extend ); | ||
| 1516 | return; | ||
| 1517 | } | ||
| 1518 | |||
| 1519 | method _move_to_parent ($line) { | ||
| 1520 | my $parent = $model->parent_of( $line->{id} ); | ||
| 1521 | my $key = defined $parent ? Term::Fabulous::Widget::Table::Model::row_key($parent) : undef; | ||
| 1522 | if ( defined $key && defined $model->line_index($key) ) { | ||
| 1523 | $self->_move_cursor($key); | ||
| 1524 | return 1; | ||
| 1525 | } | ||
| 1526 | return $self->_move_to_parent_group($line); | ||
| 1527 | } | ||
| 1528 | |||
| 1529 | # The nearest group header above the line, of a lower level for a group. | ||
| 1530 | method _move_to_parent_group ($line) { | ||
| 1531 | my @lines = $model->lines; | ||
| 1532 | my $index = $model->line_index( $line->{key} ) // return 1; | ||
| 1533 | my $limit = $line->{kind} eq 'group' ? $line->{depth} : 9**9; | ||
| 1534 | for ( my $at = $index - 1; $at >= 0; $at-- ) { | ||
| 1535 | next unless $lines[$at]{kind} eq 'group' && $lines[$at]{depth} < $limit; | ||
| 1536 | $self->_move_cursor( $lines[$at]{key} ); | ||
| 1537 | return 1; | ||
| 1538 | } | ||
| 1539 | return 1; | ||
| 1540 | } | ||
| 1541 | |||
| 1542 | # Moves the cursor as the user does: fires CursorMove, selects in | ||
| 1543 | # single mode, extends the selection with extend, turns the page. | ||
| 1544 | method _move_cursor ( $key, %options ) { | ||
| 1545 | return 0 unless defined $key; | ||
| 1546 | my $page_before = $model->page; | ||
| 1547 | my $anchor = $model->anchor // $model->cursor; | ||
| 1548 | my $moved = $model->set_cursor($key); | ||
| 1549 | $self->_after_cursor_moved( $moved, $page_before ); | ||
| 1550 | my $id = Term::Fabulous::Widget::Table::Model::id_of_key($key); | ||
| 1551 | if ( $options{extend} && defined $anchor && defined $model->line_index($anchor) ) { | ||
| 1552 | $model->set_anchor($anchor); | ||
| 1553 | my @ids = grep { defined } map { Term::Fabulous::Widget::Table::Model::id_of_key($_) } $model->line_keys_between( $anchor, $key ); | ||
| 1554 | $self->_select_by_user( $options{add} ? ( $model->selected_ids, @ids ) : @ids ); | ||
| 1555 | } | ||
| 1556 | else { | ||
| 1557 | $model->set_anchor($key); | ||
| 1558 | $self->_select_by_user($id) if $selection eq 'single' && defined $id && $moved; | ||
| 1559 | } | ||
| 1560 | return $moved; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | method _after_cursor_moved ( $moved, $page_before ) { | ||
| 1564 | return unless $moved || $model->page != $page_before; | ||
| 1565 | $self->_reveal_cursor_after_frame; | ||
| 1566 | $self->_changed; | ||
| 1567 | if ($moved) { | ||
| 1568 | my $line = $model->line( $model->cursor ); | ||
| 1569 | $self->fire_event( | ||
| 1570 | Term::Fabulous::Event::CursorMove->new( | ||
| 1571 | row_id => $line->{kind} eq 'row' ? $line->{id} : undef, | ||
| 1572 | group_path => $line->{kind} eq 'group' ? $line->{path} : undef, | ||
| 1573 | ) | ||
| 1574 | ); | ||
| 1575 | } | ||
| 1576 | $self->_fire_page_change if $model->page != $page_before; | ||
| 1577 | return; | ||
| 1578 | } | ||
| 1579 | |||
| 1580 | method _fire_page_change () { | ||
| 1581 | $self->fire_event( Term::Fabulous::Event::PageChange->new( page => $model->page, page_size => $model->page_size ) ); | ||
| 1582 | return; | ||
| 1583 | } | ||
| 1584 | |||
| 1585 | # Turns the page as the user does. The cursor goes to the page's first | ||
| 1586 | # line, as for any other move by the user: CursorMove and PageChange | ||
| 1587 | # fire, a range starts there, and in single mode the row is selected. | ||
| 1588 | method _turn_page ($page) { | ||
| 1589 | $page = max( 1, min( $page, $model->page_count ) ); | ||
| 1590 | my ( $page_before, $cursor_before ) = ( $model->page, $model->cursor ); | ||
| 1591 | return 0 unless $model->set_page($page); | ||
| 1592 | my $key = $model->cursor; | ||
| 1593 | my $moved = _same( $cursor_before, $key ) ? 0 : 1; | ||
| 1594 | $self->_after_cursor_moved( $moved, $page_before ); | ||
| 1595 | $model->set_anchor($key); | ||
| 1596 | my $id = Term::Fabulous::Widget::Table::Model::id_of_key($key); | ||
| 1597 | $self->_select_by_user($id) if $selection eq 'single' && defined $id && $moved; | ||
| 1598 | return 1; | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | method _change_page_size ($size) { | ||
| 1602 | return unless $model->set_page_size($size); | ||
| 1603 | $self->_reveal_cursor_after_frame; | ||
| 1604 | $self->_changed; | ||
| 1605 | $self->_fire_page_change; | ||
| 1606 | return; | ||
| 1607 | } | ||
| 1608 | |||
| 1609 | # Selection changes the user makes fire SelectionChange. | ||
| 1610 | method _select_by_user (@ids) { | ||
| 1611 | return $self->_selection_event( $model->set_selection(@ids) ); | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | method _toggle_selection_by_user ($id) { | ||
| 1615 | return $self->_selection_event( $model->toggle_selected($id) ); | ||
| 1616 | } | ||
| 1617 | |||
| 1618 | method _select_all_by_user () { | ||
| 1619 | my @ids = $model->filtered_row_ids; | ||
| 1620 | my $all = @ids && !grep { !$model->is_selected($_) } @ids; | ||
| 1621 | return $self->_selection_event( $all ? $model->deselect(@ids) : $model->select(@ids) ); | ||
| 1622 | } | ||
| 1623 | |||
| 1624 | method _selection_event ( $added, $removed ) { | ||
| 1625 | return 1 unless @$added || @$removed; | ||
| 1626 | $self->_changed; | ||
| 1627 | $self->fire_event( Term::Fabulous::Event::SelectionChange->new( selected_ids => [ $model->selected_ids ], added_ids => $added, removed_ids => $removed ) ); | ||
| 1628 | return 1; | ||
| 1629 | } | ||
| 1630 | |||
| 1631 | # Opens or closes a group or tree row as the user does. | ||
| 1632 | method _toggle_line ( $line, $open ) { | ||
| 1633 | my $changed | ||
| 1634 | = $line->{kind} eq 'group' | ||
| 1635 | ? $model->set_group_collapsed( $line->{path}, $open ? 0 : 1 ) | ||
| 1636 | : $model->set_expanded( $line->{id}, $open ); | ||
| 1637 | return 1 unless $changed; | ||
| 1638 | $self->_changed; | ||
| 1639 | my $class = $open ? 'Term::Fabulous::Event::Expand' : 'Term::Fabulous::Event::Collapse'; | ||
| 1640 | $self->fire_event( | ||
| 1641 | $class->new( | ||
| 1642 | row_id => $line->{kind} eq 'row' ? $line->{id} : undef, | ||
| 1643 | group_path => $line->{kind} eq 'group' ? $line->{path} : undef, | ||
| 1644 | ) | ||
| 1645 | ); | ||
| 1646 | return 1; | ||
| 1647 | } | ||
| 1648 | |||
| 1649 | # The header has the keyboard: the header cursor picks a column. | ||
| 1650 | method _enter_header () { | ||
| 1651 | $_header_cursor = first { defined $_header_cells[$_]{marker} || defined $_header_cells[$_]{mark} } 0 .. $#_header_cells; | ||
| 1652 | $_header_cursor //= 0; | ||
| 1653 | $self->_changed; | ||
| 1654 | return 1; | ||
| 1655 | } | ||
| 1656 | |||
| 1657 | method _leave_header () { | ||
| 1658 | $_header_cursor = undef; | ||
| 1659 | $self->_reveal_cursor_after_frame; | ||
| 1660 | $self->_changed; | ||
| 1661 | return $HANDLED; | ||
| 1662 | } | ||
| 1663 | |||
| 1664 | method _on_header_key ($name) { | ||
| 1665 | my $last = $#_header_cells; | ||
| 1666 | return $self->_leave_header if $name eq 'Down' || $name eq 'Escape' || $last < 0; | ||
| 1667 | my %target = ( Left => $_header_cursor - 1, Right => $_header_cursor + 1, Home => 0, End => $last ); | ||
| 1668 | if ( exists $target{$name} ) { | ||
| 1669 | $_header_cursor = max( 0, min( $last, $target{$name} ) ); | ||
| 1670 | $self->_changed; | ||
| 1671 | return $HANDLED; | ||
| 1672 | } | ||
| 1673 | my @grid_columns = $self->_grid_columns; | ||
| 1674 | my $key = $grid_columns[$_header_cursor]{key} // return $CONTINUE; | ||
| 1675 | if ( $name eq 'Enter' || $name eq 'Space' ) { | ||
| 1676 | if ( $key eq SELECT_COLUMN ) { | ||
| 1677 | $self->_select_all_by_user; | ||
| 1678 | return $HANDLED; | ||
| 1679 | } | ||
| 1680 | $self->_sort_by_user( $key, $name eq 'Space' ); | ||
| 1681 | return $HANDLED; | ||
| 1682 | } | ||
| 1683 | if ( $name eq 'c' ) { | ||
| 1684 | $self->open_column_chooser; | ||
| 1685 | return $HANDLED; | ||
| 1686 | } | ||
| 1687 | return $CONTINUE; | ||
| 1688 | } | ||
| 1689 | |||
| 1690 | method _sort_by_user ( $key, $add ) { | ||
| 1691 | return unless $model->column($key)->sortable; | ||
| 1692 | return unless $model->cycle_sort( $key, $add ); | ||
| 1693 | $self->_reveal_cursor_after_frame; | ||
| 1694 | $self->_changed; | ||
| 1695 | $self->fire_event( Term::Fabulous::Event::SortChange->new( sort => $model->sort_spec ) ); | ||
| 1696 | return; | ||
| 1697 | } | ||
| 1698 | |||
| 1699 | # --------------------------------------------------------------------- | ||
| 1700 | # Mouse | ||
| 1701 | # --------------------------------------------------------------------- | ||
| 1702 | |||
| 1703 | # The table widget a mouse event is about: a toggle or a cell, and the | ||
| 1704 | # widget it hit. The scrollbar handles its own clicks. | ||
| 1705 | method _hit ($target) { | ||
| 1706 | foreach my $node ( $target, $target->ancestors ) { | ||
| 1707 | last if refaddr($node) == refaddr($self); | ||
| 1708 | return ( toggle => $node ) if $node->isa('Term::Fabulous::Widget::Table::Toggle'); | ||
| 1709 | return ( cell => $node ) if $node->isa('Term::Fabulous::Widget::Table::Cell'); | ||
| 1710 | } | ||
| 1711 | return (); | ||
| 1712 | } | ||
| 1713 | |||
| 1714 | method _on_mouse ($event) { | ||
| 1715 | my ( $kind, $widget ) = $self->_hit( $event->target ); | ||
| 1716 | return $CONTINUE unless defined $kind; | ||
| 1717 | my $key = $event->key; | ||
| 1718 | my $motion = $event->modifiers & TB_MOD_MOTION; | ||
| 1719 | return $CONTINUE if $motion; | ||
| 1720 | if ( $key == TB_KEY_MOUSE_RIGHT ) { | ||
| 1721 | return $CONTINUE unless $kind eq 'cell' && $widget->part eq 'header'; | ||
| 1722 | $self->open_column_chooser; | ||
| 1723 | return $HANDLED; | ||
| 1724 | } | ||
| 1725 | return $CONTINUE unless $key == TB_KEY_MOUSE_LEFT; | ||
| 1726 | |||
| 1727 | if ( $kind eq 'toggle' ) { | ||
| 1728 | my $line = $model->line( $widget->line_key ) // return $HANDLED; | ||
| 1729 | $self->_move_cursor( $line->{key} ); | ||
| 1730 | $self->_toggle_line( $line, !$line->{expanded} ) if $line->{kind} eq 'group' || $line->{has_children}; | ||
| 1731 | return $HANDLED; | ||
| 1732 | } | ||
| 1733 | return $self->_click_header( $widget, $event ) if $widget->part eq 'header'; | ||
| 1734 | return $CONTINUE unless $widget->part eq 'body'; | ||
| 1735 | return $self->_click_line( $widget, $event ); | ||
| 1736 | } | ||
| 1737 | |||
| 1738 | method _click_header ( $cell, $event ) { | ||
| 1739 | $_header_cursor = undef; | ||
| 1740 | my $key = $cell->column_key // return $HANDLED; | ||
| 1741 | if ( $key eq SELECT_COLUMN ) { | ||
| 1742 | $self->_select_all_by_user; | ||
| 1743 | return $HANDLED; | ||
| 1744 | } | ||
| 1745 | my $add = $event->modifiers & ( TB_MOD_SHIFT | TB_MOD_CTRL | TB_MOD_ALT ) ? 1 : 0; | ||
| 1746 | $self->_sort_by_user( $key, $add ); | ||
| 1747 | return $HANDLED; | ||
| 1748 | } | ||
| 1749 | |||
| 1750 | method _click_line ( $cell, $event ) { | ||
| 1751 | my $line = $model->line( $cell->line_key // '' ) // return $HANDLED; | ||
| 1752 | my $double = $event->click_count >= 2 && defined $_last_press_key && $_last_press_key eq $line->{key}; | ||
| 1753 | $_last_press_key = $double ? undef : $line->{key}; | ||
| 1754 | $_header_cursor = undef; | ||
| 1755 | |||
| 1756 | if ( $line->{kind} eq 'group' ) { | ||
| 1757 | $self->_move_cursor( $line->{key} ); | ||
| 1758 | $self->_toggle_line( $line, !$line->{expanded} ); | ||
| 1759 | return $HANDLED; | ||
| 1760 | } | ||
| 1761 | my $in_input = $self->_inside_input($event); | ||
| 1762 | my $mods = $event->modifiers; | ||
| 1763 | my $id = $line->{id}; | ||
| 1764 | if ( $in_input || $selection ne 'multiple' ) { | ||
| 1765 | $self->_move_cursor( $line->{key} ); | ||
| 1766 | } | ||
| 1767 | elsif ( defined $cell->column_key && $cell->column_key eq SELECT_COLUMN || $mods & ( TB_MOD_CTRL | TB_MOD_ALT ) ) { | ||
| 1768 | $self->_move_cursor( $line->{key} ); | ||
| 1769 | $self->_toggle_selection_by_user($id); | ||
| 1770 | } | ||
| 1771 | elsif ( $mods & TB_MOD_SHIFT ) { | ||
| 1772 | $self->_move_cursor( $line->{key}, extend => 1 ); | ||
| 1773 | } | ||
| 1774 | else { | ||
| 1775 | $self->_move_cursor( $line->{key} ); | ||
| 1776 | $self->_select_by_user($id); | ||
| 1777 | } | ||
| 1778 | $self->_select_by_user($id) if $selection eq 'single' && !$in_input; | ||
| 1779 | $self->fire_event( Term::Fabulous::Event::RowActivate->new( row_id => $id, row => $model->row($id) ) ) if $double && !$in_input; | ||
| 1780 | return $HANDLED; | ||
| 1781 | } | ||
| 1782 | |||
| 1783 | method _on_mouse_move ($event) { | ||
| 1784 | return unless $hover; | ||
| 1785 | my ( $kind, $widget ) = $self->_hit( $event->target ); | ||
| 1786 | my $key = defined $kind && $kind eq 'cell' && $widget->part eq 'body' ? $widget->line_key : undef; | ||
| 1787 | $self->_hover_line($key); | ||
| 1788 | return; | ||
| 1789 | } | ||
| 1790 | |||
| 1791 | method _hover_line ($key) { | ||
| 1792 | return if _same( $key, $_hover_key ); | ||
| 1793 | $_hover_key = $key; | ||
| 1794 | $self->_changed; | ||
| 1795 | return; | ||
| 1796 | } | ||
| 1797 | |||
| 1798 | # --------------------------------------------------------------------- | ||
| 1799 | # The filter row | ||
| 1800 | # --------------------------------------------------------------------- | ||
| 1801 | |||
| 1802 | method _filter_typed ( $key, $text ) { | ||
| 1803 | my $error = $self->_apply_filter_text( $key, $text ); | ||
| 1804 | $self->fire_event( Term::Fabulous::Event::FilterChange->new( column => $key, text => $text, error => $error ) ); | ||
| 1805 | return; | ||
| 1806 | } | ||
| 1807 | |||
| 1808 | # Applies the expression of a filter field; returns the error message, | ||
| 1809 | # or undef. | ||
| 1810 | method _apply_filter_text ( $key, $text ) { | ||
| 1811 | my $column = $model->column($key); | ||
| 1812 | $_filter_text{$key} = $text; | ||
| 1813 | delete $_filter_error{$key}; | ||
| 1814 | my $filter; | ||
| 1815 | try { | ||
| 1816 | $filter = Term::Fabulous::Widget::Table::Filter->parse( $text, column => $key, type => $column->type, on => $column->filter_on ); | ||
| 1817 | } | ||
| 1818 | catch ($error) { | ||
| 1819 | $_filter_error{$key} = $error =~ s/\ATerm::Fabulous::Widget::Table::Filter: //r =~ s/\s+\z//r; | ||
| 1820 | } | ||
| 1821 | $model->set_filter( "column:$key" => $filter ); | ||
| 1822 | $model->expand_to_matches; | ||
| 1823 | $self->_reveal_cursor_after_frame; | ||
| 1824 | $self->_changed; | ||
| 1825 | return $_filter_error{$key}; | ||
| 1826 | } | ||
| 1827 | |||
| 1828 | # ===================================================================== | ||
| 1829 | # Public methods | ||
| 1830 | # ===================================================================== | ||
| 1831 | |||
| 1832 | # --- Rows ------------------------------------------------------------ | ||
| 1833 | |||
| 1834 | method rows (@new) { | ||
| 1835 | return $model->rows unless @new; | ||
| 1836 | $model->set_rows( $new[0] ); | ||
| 1837 | $self->_rows_changed; | ||
| 1838 | return $self; | ||
| 1839 | } | ||
| 1840 | |||
| 1841 | method _rows_changed () { | ||
| 1842 | %_row_style_of = map { $_ => $_row_style_of{$_} } grep { $model->has_row($_) } keys %_row_style_of; | ||
| 1843 | %_cell_style_of = map { $_ => $_cell_style_of{$_} } grep { $model->has_row( ( split /\0/, $_, 2 )[0] ) } keys %_cell_style_of; | ||
| 1844 | $model->expand_to_matches; | ||
| 1845 | $self->_changed; | ||
| 1846 | return; | ||
| 1847 | } | ||
| 1848 | |||
| 1849 | method add_row ( $row, %options ) { | ||
| 1850 | my ($id) = $model->add_rows( [$row], %options ); | ||
| 1851 | $self->_rows_changed; | ||
| 1852 | return $id; | ||
| 1853 | } | ||
| 1854 | |||
| 1855 | method add_rows ( $rows, %options ) { | ||
| 1856 | my @ids = $model->add_rows( $rows, %options ); | ||
| 1857 | $self->_rows_changed; | ||
| 1858 | return @ids; | ||
| 1859 | } | ||
| 1860 | |||
| 1861 | method update_row ( $id, $changes ) { | ||
| 1862 | $model->update_row( $id, $changes ); | ||
| 1863 | $self->_data_changed; | ||
| 1864 | return $self; | ||
| 1865 | } | ||
| 1866 | |||
| 1867 | method replace_row ( $id, $row ) { | ||
| 1868 | $model->replace_row( $id, $row ); | ||
| 1869 | $self->_data_changed; | ||
| 1870 | return $self; | ||
| 1871 | } | ||
| 1872 | |||
| 1873 | method set_value ( $id, $key, $value ) { | ||
| 1874 | $model->column($key); | ||
| 1875 | $model->set_value( $id, $key, $value ); | ||
| 1876 | $self->_data_changed; | ||
| 1877 | return $self; | ||
| 1878 | } | ||
| 1879 | |||
| 1880 | # A row's data changed: in a filtered tree, a row that matches now is | ||
| 1881 | # shown, like after any change of the filters. | ||
| 1882 | method _data_changed () { | ||
| 1883 | $model->expand_to_matches; | ||
| 1884 | $self->_changed; | ||
| 1885 | return; | ||
| 1886 | } | ||
| 1887 | |||
| 1888 | method value ( $id, $key ) { | ||
| 1889 | return $model->value_of( $self->_check_row($id), $key ); | ||
| 1890 | } | ||
| 1891 | |||
| 1892 | method display_value ( $id, $key ) { | ||
| 1893 | return $model->display_of( $self->_check_row($id), $key ); | ||
| 1894 | } | ||
| 1895 | |||
| 1896 | method _check_row ($id) { | ||
| 1897 | die "Term::Fabulous::Widget::Table: there is no row with the id " . describe($id) unless $model->has_row($id); | ||
| 1898 | return $id; | ||
| 1899 | } | ||
| 1900 | |||
| 1901 | method remove_rows (@ids) { | ||
| 1902 | $model->remove_rows(@ids); | ||
| 1903 | $self->_rows_changed; | ||
| 1904 | return $self; | ||
| 1905 | } | ||
| 1906 | |||
| 1907 | method remove_row ($id) { | ||
| 1908 | return $self->remove_rows($id); | ||
| 1909 | } | ||
| 1910 | |||
| 1911 | method clear_rows () { | ||
| 1912 | $model->clear_rows; | ||
| 1913 | $self->_rows_changed; | ||
| 1914 | return $self; | ||
| 1915 | } | ||
| 1916 | |||
| 1917 | method row ($id) { return $model->row($id) } | ||
| 1918 | method has_row ($id) { return $model->has_row($id) } | ||
| 1919 | method row_ids () { return $model->row_ids } | ||
| 1920 | method row_count () { return $model->row_count } | ||
| 1921 | method parent_of ($id) { return $model->parent_of($id) } | ||
| 1922 | method children_of ($id) { return $model->children_of($id) } | ||
| 1923 | method filtered_row_ids () { return $model->filtered_row_ids } | ||
| 1924 | |||
| 1925 | method page_row_ids () { | ||
| 1926 | return map { $_->{id} } grep { $_->{kind} eq 'row' } $model->page_lines; | ||
| 1927 | } | ||
| 1928 | |||
| 1929 | # --- Columns --------------------------------------------------------- | ||
| 1930 | |||
| 1931 | method columns () { return $model->columns } | ||
| 1932 | method column ($key) { return $model->column($key) } | ||
| 1933 | method column_keys () { return $model->column_keys } | ||
| 1934 | |||
| 1935 | method add_column ( $spec, %options ) { | ||
| 1936 | my @unknown = grep { $_ ne 'index' } sort keys %options; | ||
| 1937 | die "Term::Fabulous::Widget::Table: add_column does not accept @unknown (known: index)" if @unknown; | ||
| 1938 | my $column = $model->add_column( $spec, $options{index} ); | ||
| 1939 | $self->_changed; | ||
| 1940 | return $column; | ||
| 1941 | } | ||
| 1942 | |||
| 1943 | method remove_column ($key) { | ||
| 1944 | delete $_filter_text{$key}; | ||
| 1945 | delete $_filter_error{$key}; | ||
| 1946 | delete $_cell_style_of{$_} foreach grep { ( split /\0/, $_, 2 )[1] eq $key } keys %_cell_style_of; | ||
| 1947 | $model->remove_column($key); | ||
| 1948 | $self->_changed; | ||
| 1949 | return $self; | ||
| 1950 | } | ||
| 1951 | |||
| 1952 | method move_column ( $key, $index ) { | ||
| 1953 | $model->move_column( $key, $index ); | ||
| 1954 | $self->_changed; | ||
| 1955 | return $self; | ||
| 1956 | } | ||
| 1957 | |||
| 1958 | method update_column ( $key, %changes ) { | ||
| 1959 | die "Term::Fabulous::Widget::Table: update_column cannot change the key of a column" if exists $changes{key}; | ||
| 1960 | my $column = $model->replace_column( $model->column($key)->with(%changes) ); | ||
| 1961 | $self->_changed; | ||
| 1962 | return $column; | ||
| 1963 | } | ||
| 1964 | |||
| 1965 | method visible_columns () { | ||
| 1966 | return map { $_->key } $model->visible_columns; | ||
| 1967 | } | ||
| 1968 | |||
| 1969 | method is_column_visible ($key) { | ||
| 1970 | return $model->is_column_visible($key); | ||
| 1971 | } | ||
| 1972 | |||
| 1973 | method show_columns (@keys) { | ||
| 1974 | $model->set_column_visible( $_, 1 ) foreach @keys; | ||
| 1975 | $self->_changed; | ||
| 1976 | return $self; | ||
| 1977 | } | ||
| 1978 | |||
| 1979 | method hide_columns (@keys) { | ||
| 1980 | $model->set_column_visible( $_, 0 ) foreach @keys; | ||
| 1981 | $self->_changed; | ||
| 1982 | return $self; | ||
| 1983 | } | ||
| 1984 | |||
| 1985 | method set_visible_columns (@keys) { | ||
| 1986 | $model->set_visible_columns(@keys); | ||
| 1987 | $self->_changed; | ||
| 1988 | return $self; | ||
| 1989 | } | ||
| 1990 | |||
| 1991 | method open_column_chooser () { | ||
| 1992 | return $self if defined $_chooser; | ||
| 1993 | weaken( my $weak = $self ); | ||
| 1994 | $_chooser = Term::Fabulous::Widget::Table::ColumnChooser->new( | ||
| 1995 | columns => [ map { [ $_->key, length $_->title ? $_->title : $_->key, $model->is_column_visible( $_->key ) ] } $model->columns ], | ||
| 1996 | text_color => $self->text_color, | ||
| 1997 | muted_color => $self->muted_color, | ||
| 1998 | background_color => $self->group_background_color, | ||
| 1999 | bordered => 1, | ||
| 2000 | border_color => $self->text_color, | ||
| 2001 | border_style => Term::Fabulous::Enum::BorderStyle->Round, | ||
| 2002 | on_toggle => sub ( $key, $visible ) { $weak->_chooser_toggled( $key, $visible ) if $weak }, | ||
| 2003 | on_close => sub () { $weak->close_column_chooser if $weak }, | ||
| 2004 | ); | ||
| 2005 | $self->add_child($_chooser); | ||
| 2006 | $_chooser->focus_first; | ||
| 2007 | return $self; | ||
| 2008 | } | ||
| 2009 | |||
| 2010 | method close_column_chooser () { | ||
| 2011 | return $self unless defined $_chooser; | ||
| 2012 | my $closing = $_chooser; | ||
| 2013 | undef $_chooser; | ||
| 2014 | $self->remove_child($closing); # a focus inside goes to its heir, the table | ||
| 2015 | return $self; | ||
| 2016 | } | ||
| 2017 | |||
| 2018 | method is_column_chooser_open () { | ||
| 2019 | return defined $_chooser ? 1 : 0; | ||
| 2020 | } | ||
| 2021 | |||
| 2022 | method _chooser_toggled ( $key, $visible ) { | ||
| 2023 | $model->set_column_visible( $key, $visible ); | ||
| 2024 | $self->_changed; | ||
| 2025 | $self->fire_event( Term::Fabulous::Event::ColumnsChange->new( visible => [ $self->visible_columns ] ) ); | ||
| 2026 | return; | ||
| 2027 | } | ||
| 2028 | |||
| 2029 | # --- Sorting --------------------------------------------------------- | ||
| 2030 | |||
| 2031 | method sort_spec () { | ||
| 2032 | return $model->sort_spec; | ||
| 2033 | } | ||
| 2034 | |||
| 2035 | method sort_by (@spec) { | ||
| 2036 | $self->_reveal_cursor_after_frame if $model->set_sort(@spec); | ||
| 2037 | $self->_changed; | ||
| 2038 | return $self; | ||
| 2039 | } | ||
| 2040 | |||
| 2041 | method clear_sort () { | ||
| 2042 | return $self->sort_by; | ||
| 2043 | } | ||
| 2044 | |||
| 2045 | # --- Filtering ------------------------------------------------------- | ||
| 2046 | |||
| 2047 | method filter ( $name, $filter ) { | ||
| 2048 | die "Term::Fabulous::Widget::Table: filter names starting with 'column:' belong to the filter row; use filter_text" | ||
| 2049 | if defined $name && !ref $name && $name =~ /\Acolumn:/; | ||
| 2050 | $model->set_filter( $name, $filter ); | ||
| 2051 | $model->expand_to_matches; | ||
| 2052 | $self->_reveal_cursor_after_frame; | ||
| 2053 | $self->_changed; | ||
| 2054 | return $self; | ||
| 2055 | } | ||
| 2056 | |||
| 2057 | method remove_filter ($name) { | ||
| 2058 | $model->remove_filter($name); | ||
| 2059 | $model->expand_to_matches; | ||
| 2060 | $self->_changed; | ||
| 2061 | return $self; | ||
| 2062 | } | ||
| 2063 | |||
| 2064 | method filter_names () { | ||
| 2065 | return grep { !/\Acolumn:/ } $model->filter_names; | ||
| 2066 | } | ||
| 2067 | |||
| 2068 | method clear_filters () { | ||
| 2069 | %_filter_text = %_filter_error = (); | ||
| 2070 | _set( $_->{field}, value => '' ) foreach grep { defined $_->{field} } @_filter_cells; | ||
| 2071 | $model->clear_filters; | ||
| 2072 | $self->_changed; | ||
| 2073 | return $self; | ||
| 2074 | } | ||
| 2075 | |||
| 2076 | method search (@new) { | ||
| 2077 | return $model->search unless @new; | ||
| 2078 | $model->search( $new[0] ); | ||
| 2079 | $model->expand_to_matches; | ||
| 2080 | $self->_reveal_cursor_after_frame; | ||
| 2081 | $self->_changed; | ||
| 2082 | return $self; | ||
| 2083 | } | ||
| 2084 | |||
| 2085 | method filter_text ( $key, @new ) { | ||
| 2086 | $model->column($key); | ||
| 2087 | return $_filter_text{$key} // '' unless @new; | ||
| 2088 | my $text = string( $self, filter_text => $new[0] // '' ); | ||
| 2089 | my ($cell) = grep { defined $_->{field} && $_->{cell}->column_key eq $key } @_filter_cells; | ||
| 2090 | _set( $cell->{field}, value => $text ) if defined $cell; | ||
| 2091 | $self->_apply_filter_text( $key, $text ); | ||
| 2092 | return $self; | ||
| 2093 | } | ||
| 2094 | |||
| 2095 | method filter_error ($key) { | ||
| 2096 | $model->column($key); | ||
| 2097 | return $_filter_error{$key}; | ||
| 2098 | } | ||
| 2099 | |||
| 2100 | method filter_row (@new) { | ||
| 2101 | return $filter_row unless @new; | ||
| 2102 | $filter_row = boolean( $self, filter_row => $new[0] ); | ||
| 2103 | $self->_changed; | ||
| 2104 | return $filter_row; | ||
| 2105 | } | ||
| 2106 | |||
| 2107 | # --- Grouping -------------------------------------------------------- | ||
| 2108 | |||
| 2109 | method group_by (@keys) { | ||
| 2110 | return $model->group_by unless @keys; | ||
| 2111 | $model->group_by(@keys); | ||
| 2112 | $self->_changed; | ||
| 2113 | return $self; | ||
| 2114 | } | ||
| 2115 | |||
| 2116 | method ungroup () { | ||
| 2117 | $model->group_by(undef); | ||
| 2118 | $self->_changed; | ||
| 2119 | return $self; | ||
| 2120 | } | ||
| 2121 | |||
| 2122 | method is_group_expanded (@path) { | ||
| 2123 | return $model->is_group_collapsed(@path) ? 0 : 1; | ||
| 2124 | } | ||
| 2125 | |||
| 2126 | method expand_group (@path) { | ||
| 2127 | $model->set_group_collapsed( \@path, 0 ); | ||
| 2128 | $self->_changed; | ||
| 2129 | return $self; | ||
| 2130 | } | ||
| 2131 | |||
| 2132 | method collapse_group (@path) { | ||
| 2133 | $model->set_group_collapsed( \@path, 1 ); | ||
| 2134 | $self->_changed; | ||
| 2135 | return $self; | ||
| 2136 | } | ||
| 2137 | |||
| 2138 | method expand_all_groups () { | ||
| 2139 | $model->set_all_groups_collapsed(0); | ||
| 2140 | $self->_changed; | ||
| 2141 | return $self; | ||
| 2142 | } | ||
| 2143 | |||
| 2144 | method collapse_all_groups () { | ||
| 2145 | $model->set_all_groups_collapsed(1); | ||
| 2146 | $self->_changed; | ||
| 2147 | return $self; | ||
| 2148 | } | ||
| 2149 | |||
| 2150 | # --- Tree ------------------------------------------------------------ | ||
| 2151 | |||
| 2152 | method is_expanded ($id) { return $model->is_expanded($id) } | ||
| 2153 | |||
| 2154 | method expand (@ids) { | ||
| 2155 | $model->set_expanded( $_, 1 ) foreach @ids; | ||
| 2156 | $self->_changed; | ||
| 2157 | return $self; | ||
| 2158 | } | ||
| 2159 | |||
| 2160 | method collapse (@ids) { | ||
| 2161 | $model->set_expanded( $_, 0 ) foreach @ids; | ||
| 2162 | $self->_changed; | ||
| 2163 | return $self; | ||
| 2164 | } | ||
| 2165 | |||
| 2166 | method expand_all () { | ||
| 2167 | $model->set_all_expanded(1); | ||
| 2168 | $self->_changed; | ||
| 2169 | return $self; | ||
| 2170 | } | ||
| 2171 | |||
| 2172 | method collapse_all () { | ||
| 2173 | $model->set_all_expanded(0); | ||
| 2174 | $self->_changed; | ||
| 2175 | return $self; | ||
| 2176 | } | ||
| 2177 | |||
| 2178 | # --- Pages ----------------------------------------------------------- | ||
| 2179 | |||
| 2180 | method page (@new) { | ||
| 2181 | return $model->page unless @new; | ||
| 2182 | $model->set_page( $new[0] ); | ||
| 2183 | $self->_changed; | ||
| 2184 | return $self; | ||
| 2185 | } | ||
| 2186 | |||
| 2187 | method page_count () { return $model->page_count } | ||
| 2188 | |||
| 2189 | # The rows the table takes before it is laid out: the header rows, the | ||
| 2190 | # lines of the current page (or the one line that says there are | ||
| 2191 | # none), the lines of the frame around them (the top, the line under | ||
| 2192 | # the header rows and the bottom ledge, each when its style draws a | ||
| 2193 | # line) and the pager when it shows. The lines between rows are not | ||
| 2194 | # counted. | ||
| 2195 | method estimated_content_rows :override ($columns) { | ||
| 2196 | my $header_rows = ( $show_header ? 1 : 0 ) + ( $filter_row ? 1 : 0 ); | ||
| 2197 | my $rows = $header_rows + max( 1, scalar $model->page_lines ); | ||
| 2198 | $rows++ if _draws_line( $_line_style{border_top} ); | ||
| 2199 | $rows++ if $header_rows && _draws_line( $_line_style{header_line} // $_line_style{row_lines} ); | ||
| 2200 | $rows++ if _draws_line( $_line_style{border_bottom} ); | ||
| 2201 | $rows += $_pager->estimated_rows($columns) if $self->_pager_shown; | ||
| 2202 | return $rows; | ||
| 2203 | } | ||
| 2204 | |||
| 2205 | sub _draws_line ($style) { | ||
| 2206 | return defined $style && $style->name ne 'Hidden' ? 1 : 0; | ||
| 2207 | } | ||
| 2208 | |||
| 2209 | method page_size (@new) { | ||
| 2210 | return $model->page_size unless @new; | ||
| 2211 | $model->set_page_size( $new[0] ); | ||
| 2212 | $self->_changed; | ||
| 2213 | return $self; | ||
| 2214 | } | ||
| 2215 | |||
| 2216 | method next_page () { return $self->page( min( $model->page + 1, $model->page_count ) ) } | ||
| 2217 | method previous_page () { return $self->page( max( $model->page - 1, 1 ) ) } | ||
| 2218 | |||
| 2219 | # --- Selection ------------------------------------------------------- | ||
| 2220 | |||
| 2221 | # Changing the mode keeps what the new mode allows of the selection: | ||
| 2222 | # none of it for 'none', the first selected row for 'single'. | ||
| 2223 | method selection (@new) { | ||
| 2224 | return $selection unless @new; | ||
| 2225 | my $mode = one_of( $self, selection => $new[0], @SELECTIONS ); | ||
| 2226 | $selection = $mode; | ||
| 2227 | my @kept = $mode eq 'none' ? () : $model->selected_ids; | ||
| 2228 | @kept = @kept[ 0 .. 0 ] if $mode eq 'single' && @kept > 1; | ||
| 2229 | $model->set_selection(@kept); | ||
| 2230 | $selection_column = $_explicit_selection_column // ( $mode eq 'multiple' ? 1 : 0 ); | ||
| 2231 | $self->_changed; | ||
| 2232 | return $selection; | ||
| 2233 | } | ||
| 2234 | |||
| 2235 | method selection_column (@new) { | ||
| 2236 | return $selection_column unless @new; | ||
| 2237 | $_explicit_selection_column = $selection_column = boolean( $self, selection_column => $new[0] ); | ||
| 2238 | $self->_changed; | ||
| 2239 | return $selection_column; | ||
| 2240 | } | ||
| 2241 | |||
| 2242 | method row_id (@new) { | ||
| 2243 | return $model->row_id unless @new; | ||
| 2244 | return $model->set_row_id( $new[0] ); | ||
| 2245 | } | ||
| 2246 | |||
| 2247 | method children_key (@new) { | ||
| 2248 | return $model->children_key unless @new; | ||
| 2249 | $model->set_children_key( $new[0] ); | ||
| 2250 | $_body_signature = ''; | ||
| 2251 | $self->_changed; | ||
| 2252 | return $model->children_key; | ||
| 2253 | } | ||
| 2254 | |||
| 2255 | method tree_expanded (@new) { | ||
| 2256 | return $model->expand_new unless @new; | ||
| 2257 | return $model->expand_new( boolean( $self, tree_expanded => $new[0] ) ); | ||
| 2258 | } | ||
| 2259 | |||
| 2260 | method _selectable (@ids) { | ||
| 2261 | die "Term::Fabulous::Widget::Table: the table has selection => 'none'; nothing can be selected" if @ids && $selection eq 'none'; | ||
| 2262 | die "Term::Fabulous::Widget::Table: a table with selection => 'single' selects one row at most" if @ids > 1 && $selection eq 'single'; | ||
| 2263 | $self->_check_row($_) foreach @ids; | ||
| 2264 | return @ids; | ||
| 2265 | } | ||
| 2266 | |||
| 2267 | method selected_ids () { return $model->selected_ids } | ||
| 2268 | |||
| 2269 | method selected_rows () { | ||
| 2270 | return map { $model->row($_) } $model->selected_ids; | ||
| 2271 | } | ||
| 2272 | method is_selected ($id) { return $model->is_selected($id) } | ||
| 2273 | |||
| 2274 | method set_selection (@ids) { | ||
| 2275 | $model->set_selection( $self->_selectable(@ids) ); | ||
| 2276 | $self->_changed; | ||
| 2277 | return $self; | ||
| 2278 | } | ||
| 2279 | |||
| 2280 | method select (@ids) { | ||
| 2281 | return $self->set_selection(@ids) if $selection eq 'single'; | ||
| 2282 | $model->select( $self->_selectable(@ids) ); | ||
| 2283 | $self->_changed; | ||
| 2284 | return $self; | ||
| 2285 | } | ||
| 2286 | |||
| 2287 | method deselect (@ids) { | ||
| 2288 | $model->deselect( map { $self->_check_row($_) } @ids ); | ||
| 2289 | $self->_changed; | ||
| 2290 | return $self; | ||
| 2291 | } | ||
| 2292 | |||
| 2293 | method select_all () { | ||
| 2294 | die "Term::Fabulous::Widget::Table: select_all needs selection => 'multiple'" unless $selection eq 'multiple'; | ||
| 2295 | $model->select( $model->filtered_row_ids ); | ||
| 2296 | $self->_changed; | ||
| 2297 | return $self; | ||
| 2298 | } | ||
| 2299 | |||
| 2300 | method clear_selection () { | ||
| 2301 | $model->set_selection; | ||
| 2302 | $self->_changed; | ||
| 2303 | return $self; | ||
| 2304 | } | ||
| 2305 | |||
| 2306 | # --- Cursor ---------------------------------------------------------- | ||
| 2307 | |||
| 2308 | method cursor (@new) { | ||
| 2309 | unless (@new) { | ||
| 2310 | return Term::Fabulous::Widget::Table::Model::id_of_key( $model->cursor ); | ||
| 2311 | } | ||
| 2312 | my $key = Term::Fabulous::Widget::Table::Model::row_key( $self->_check_row( $new[0] ) ); | ||
| 2313 | die "Term::Fabulous::Widget::Table: row '$new[0]' is not shown (filtered out, or below a collapsed row or group)" unless defined $model->line_index($key); | ||
| 2314 | $model->set_cursor($key); | ||
| 2315 | $model->set_anchor($key); | ||
| 2316 | $self->_reveal_cursor_after_frame; | ||
| 2317 | $self->_changed; | ||
| 2318 | return $self; | ||
| 2319 | } | ||
| 2320 | |||
| 2321 | method cursor_group () { | ||
| 2322 | my $line = $model->line( $model->cursor // return undef ) // return undef; | ||
| 2323 | return $line->{kind} eq 'group' ? [ @{ $line->{path} } ] : undef; | ||
| 2324 | } | ||
| 2325 | |||
| 2326 | method scroll_to_row ($id) { | ||
| 2327 | return $self->cursor($id); | ||
| 2328 | } | ||
| 2329 | |||
| 2330 | # --- Styles ---------------------------------------------------------- | ||
| 2331 | |||
| 2332 | method set_row_style ( $id, $style ) { | ||
| 2333 | $self->_check_row($id); | ||
| 2334 | my $checked = style_hash( $self, 'row style', row => $style ); | ||
| 2335 | %$checked ? ( $_row_style_of{$id} = $checked ) : delete $_row_style_of{$id}; | ||
| 2336 | $self->_restyle; | ||
| 2337 | return $self; | ||
| 2338 | } | ||
| 2339 | |||
| 2340 | method row_style_of ($id) { | ||
| 2341 | return { %{ $_row_style_of{ $self->_check_row($id) } // {} } }; | ||
| 2342 | } | ||
| 2343 | |||
| 2344 | method set_cell_style ( $id, $key, $style ) { | ||
| 2345 | $self->_check_row($id); | ||
| 2346 | $model->column($key); | ||
| 2347 | my $checked = style_hash( $self, 'cell style', cell => $style ); | ||
| 2348 | %$checked ? ( $_cell_style_of{ $id . "\0" . $key } = $checked ) : delete $_cell_style_of{ $id . "\0" . $key }; | ||
| 2349 | $self->_restyle; | ||
| 2350 | return $self; | ||
| 2351 | } | ||
| 2352 | |||
| 2353 | method cell_style_of ( $id, $key ) { | ||
| 2354 | $self->_check_row($id); | ||
| 2355 | $model->column($key); | ||
| 2356 | return { %{ $_cell_style_of{ $id . "\0" . $key } // {} } }; | ||
| 2357 | } | ||
| 2358 | |||
| 2359 | # --- Appearance ------------------------------------------------------ | ||
| 2360 | |||
| 2361 | method _color_property ( $name, @new ) { | ||
| 2362 | return @new ? $self->set_look( $name => $new[0] ) : $self->look_value($name); | ||
| 2363 | } | ||
| 2364 | |||
| 2365 | # The parts that copy the table's colors take them again. | ||
| 2366 | method _colors_changed (@names) { | ||
| 2367 | my %changed = map { $_ => 1 } @names; | ||
| 2368 | $_scrollbar->thumb_color( $self->text_color ) if $changed{text_color}; | ||
| 2369 | $_scrollbar->track_color( $self->line_color ) if $changed{line_color}; | ||
| 2370 | $self->_rebuild_pager if $changed{text_color} || $changed{muted_color}; | ||
| 2371 | $_header_signature = '' if $changed{text_color} || $changed{cursor_color}; # the filter fields take them when built | ||
| 2372 | $self->_restyle; | ||
| 2373 | return; | ||
| 2374 | } | ||
| 2375 | |||
| 2376 | method text_color (@new) { return $self->_color_property( text_color => @new ) } | ||
| 2377 | method header_text_color (@new) { return $self->_color_property( header_text_color => @new ) } | ||
| 2378 | method header_background_color (@new) { return $self->_color_property( header_background_color => @new ) } | ||
| 2379 | method group_text_color (@new) { return $self->_color_property( group_text_color => @new ) } | ||
| 2380 | method group_background_color (@new) { return $self->_color_property( group_background_color => @new ) } | ||
| 2381 | method cursor_color (@new) { return $self->_color_property( cursor_color => @new ) } | ||
| 2382 | method selected_color (@new) { return $self->_color_property( selected_color => @new ) } | ||
| 2383 | method hover_color (@new) { return $self->_color_property( hover_color => @new ) } | ||
| 2384 | method filter_background_color (@new) { return $self->_color_property( filter_background_color => @new ) } | ||
| 2385 | method error_color (@new) { return $self->_color_property( error_color => @new ) } | ||
| 2386 | method muted_color (@new) { return $self->_color_property( muted_color => @new ) } | ||
| 2387 | method line_color (@new) { return $self->_color_property( line_color => @new ) } | ||
| 2388 | |||
| 2389 | method stripe_color (@new) { | ||
| 2390 | return @new ? $self->set_look( stripe_color => $new[0] ) : $self->look_value('stripe_color'); | ||
| 2391 | } | ||
| 2392 | |||
| 2393 | # The table's own background is the color of its cells (see | ||
| 2394 | # _base_background). | ||
| 2395 | method background_color :override (@new) { | ||
| 2396 | my $color = $self->SUPER::background_color(@new); | ||
| 2397 | $self->_restyle if @new; | ||
| 2398 | return $color; | ||
| 2399 | } | ||
| 2400 | |||
| 2401 | method _line_property ( $name, @new ) { | ||
| 2402 | return $_line_style{$name} unless @new; | ||
| 2403 | $_line_style{$name} = optional( \&border_style, $self, $name => $new[0], @NONE_IS_HIDDEN ); | ||
| 2404 | $self->_restyle; | ||
| 2405 | return $_line_style{$name}; | ||
| 2406 | } | ||
| 2407 | |||
| 2408 | method border_top (@new) { return $self->_line_property( border_top => @new ) } | ||
| 2409 | method border_right (@new) { return $self->_line_property( border_right => @new ) } | ||
| 2410 | method border_bottom (@new) { return $self->_line_property( border_bottom => @new ) } | ||
| 2411 | method border_left (@new) { return $self->_line_property( border_left => @new ) } | ||
| 2412 | method column_lines (@new) { return $self->_line_property( column_lines => @new ) } | ||
| 2413 | method row_lines (@new) { return $self->_line_property( row_lines => @new ) } | ||
| 2414 | method header_line (@new) { return $self->_line_property( header_line => @new ) } | ||
| 2415 | |||
| 2416 | # Sets the four sides of the frame; reads the style they share, or | ||
| 2417 | # undef when they differ. | ||
| 2418 | method border (@new) { | ||
| 2419 | if (@new) { | ||
| 2420 | $self->_line_property( $_ => $new[0] ) foreach qw(border_top border_right border_bottom border_left); | ||
| 2421 | return $_line_style{border_top}; | ||
| 2422 | } | ||
| 2423 | my %names = map { ( defined $_ ? $_->name : '' ) => 1 } @_line_style{qw(border_top border_right border_bottom border_left)}; | ||
| 2424 | return keys %names == 1 ? $_line_style{border_top} : undef; | ||
| 2425 | } | ||
| 2426 | |||
| 2427 | method header (@new) { | ||
| 2428 | return $show_header unless @new; | ||
| 2429 | $show_header = boolean( $self, header => $new[0] ); | ||
| 2430 | $self->_changed; | ||
| 2431 | return $show_header; | ||
| 2432 | } | ||
| 2433 | |||
| 2434 | method scrollbar (@new) { | ||
| 2435 | return $scrollbar unless @new; | ||
| 2436 | $scrollbar = boolean( $self, scrollbar => $new[0] ); | ||
| 2437 | $self->_changed; | ||
| 2438 | return $scrollbar; | ||
| 2439 | } | ||
| 2440 | |||
| 2441 | method hover (@new) { | ||
| 2442 | return $hover unless @new; | ||
| 2443 | $hover = boolean( $self, hover => $new[0] ); | ||
| 2444 | $_hover_key = undef unless $hover; | ||
| 2445 | $self->_restyle; | ||
| 2446 | return $hover; | ||
| 2447 | } | ||
| 2448 | |||
| 2449 | method pager (@new) { | ||
| 2450 | return $show_pager unless @new; | ||
| 2451 | $show_pager = defined $new[0] ? boolean( $self, pager => $new[0] ) : undef; | ||
| 2452 | $self->_changed; | ||
| 2453 | return $show_pager; | ||
| 2454 | } | ||
| 2455 | |||
| 2456 | method empty_text (@new) { | ||
| 2457 | return $empty_text unless @new; | ||
| 2458 | $empty_text = string( $self, empty_text => $new[0] ); | ||
| 2459 | $self->_restyle; | ||
| 2460 | return $empty_text; | ||
| 2461 | } | ||
| 2462 | |||
| 2463 | method no_match_text (@new) { | ||
| 2464 | return $no_match_text unless @new; | ||
| 2465 | $no_match_text = string( $self, no_match_text => $new[0] ); | ||
| 2466 | $self->_restyle; | ||
| 2467 | return $no_match_text; | ||
| 2468 | } | ||
| 2469 | |||
| 2470 | method _callback_property ( $name, $field_ref, @new ) { | ||
| 2471 | return $$field_ref unless @new; | ||
| 2472 | die "Term::Fabulous::Widget::Table: $name must be a code reference or undef, got " . describe( $new[0] ) | ||
| 2473 | if defined $new[0] && ref $new[0] ne 'CODE'; | ||
| 2474 | $$field_ref = $new[0]; | ||
| 2475 | $_->{label_signature} = '' foreach grep { $_->{kind} eq 'group' } values %_built; # labels are made again | ||
| 2476 | $self->_restyle; | ||
| 2477 | return $$field_ref; | ||
| 2478 | } | ||
| 2479 | |||
| 2480 | method row_style (@new) { return $self->_callback_property( row_style => \$row_style, @new ) } | ||
| 2481 | method group_label (@new) { return $self->_callback_property( group_label => \$group_label, @new ) } | ||
| 2482 | |||
| 2483 | method header_style (@new) { | ||
| 2484 | return {%$header_style} unless @new; | ||
| 2485 | $header_style = style_hash( $self, 'header_style', row => $new[0] ); | ||
| 2486 | $self->_restyle; | ||
| 2487 | return {%$header_style}; | ||
| 2488 | } | ||
| 2489 | |||
| 2490 | method group_style (@new) { | ||
| 2491 | return {%$group_style} unless @new; | ||
| 2492 | $group_style = style_hash( $self, 'group_style', row => $new[0] ); | ||
| 2493 | $self->_restyle; | ||
| 2494 | return {%$group_style}; | ||
| 2495 | } | ||
| 2496 | |||
| 2497 | method cell_padding (@new) { | ||
| 2498 | return {%$cell_padding} unless @new; | ||
| 2499 | $cell_padding = $self->_padding( $new[0] ); | ||
| 2500 | $_header_signature = $_body_signature = ''; # every cell is built again | ||
| 2501 | $self->_changed; | ||
| 2502 | return {%$cell_padding}; | ||
| 2503 | } | ||
| 2504 | |||
| 2505 | method tree_column (@new) { | ||
| 2506 | return $tree_column unless @new; | ||
| 2507 | die "Term::Fabulous::Widget::Table: tree_column must be a column key or undef, got " . describe( $new[0] ) if ref $new[0]; | ||
| 2508 | $model->column( $new[0] ) if defined $new[0]; | ||
| 2509 | $tree_column = $new[0]; | ||
| 2510 | $_body_signature = ''; | ||
| 2511 | $self->_changed; | ||
| 2512 | return $tree_column; | ||
| 2513 | } | ||
| 2514 | |||
| 2515 | method page_sizes (@new) { | ||
| 2516 | return [@$page_sizes] unless @new; | ||
| 2517 | die "Term::Fabulous::Widget::Table: page_sizes must be an array reference of positive integers, got " . describe( $new[0] ) | ||
| 2518 | unless ref $new[0] eq 'ARRAY' && @{ $new[0] } && !grep { !defined || ref || !/\A[1-9][0-9]*\z/ } @{ $new[0] }; | ||
| 2519 | $page_sizes = [ map { $_ + 0 } @{ $new[0] } ]; | ||
| 2520 | $self->_rebuild_pager; | ||
| 2521 | $self->_changed; | ||
| 2522 | return [@$page_sizes]; | ||
| 2523 | } | ||
| 2524 | |||
| 2525 | # --- KDL ------------------------------------------------------------- | ||
| 2526 | |||
| 2527 | method layout_properties :common () { | ||
| 2528 | return ( | ||
| 2529 | $class->SUPER::layout_properties, | ||
| 2530 | selection => 'scalar', | ||
| 2531 | selection_column => 'boolean', | ||
| 2532 | page_size => 'scalar', | ||
| 2533 | pager => 'boolean', | ||
| 2534 | filter_row => 'boolean', | ||
| 2535 | header => 'boolean', | ||
| 2536 | scrollbar => 'boolean', | ||
| 2537 | hover => 'boolean', | ||
| 2538 | row_id => 'scalar', | ||
| 2539 | children_key => 'scalar', | ||
| 2540 | tree_expanded => 'boolean', | ||
| 2541 | tree_column => 'scalar', | ||
| 2542 | empty_text => 'scalar', | ||
| 2543 | no_match_text => 'scalar', | ||
| 2544 | column => \&_parse_column, | ||
| 2545 | sort => \&_parse_sort, | ||
| 2546 | group_by => \&_parse_group_by, | ||
| 2547 | lines => \&_parse_lines, | ||
| 2548 | cell_padding => \&_parse_cell_padding, | ||
| 2549 | page_sizes => \&_parse_page_sizes, | ||
| 2550 | ); | ||
| 2551 | } | ||
| 2552 | |||
| 2553 | # A table's cells come from its columns, not from child widgets. | ||
| 2554 | method apply_layout_node :override ($node) { | ||
| 2555 | my @widgets = grep { Term::Fabulous::Role::CanParseLayout::is_widget_node_name( $_->name ) } $node->children->@*; | ||
| 2556 | die "Term::Fabulous::Widget::Table: a table takes no child widgets (found '" . $widgets[0]->name . "'); its cells come from its columns and rows" if @widgets; | ||
| 2557 | return $self->SUPER::apply_layout_node($node); | ||
| 2558 | } | ||
| 2559 | |||
| 2560 | # How rows are read comes first, then the columns, so every other | ||
| 2561 | # property can name them. | ||
| 2562 | method apply_layout_settings :override (@settings) { | ||
| 2563 | my %rank = ( row_id => 0, children_key => 0, column => 1, sort => 3, group_by => 3 ); | ||
| 2564 | my @ordered = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ $rank{ $_->[0] } // 2, $_ ] } @settings; | ||
| 2565 | return $self->SUPER::apply_layout_settings(@ordered); | ||
| 2566 | } | ||
| 2567 | |||
| 2568 | my @COLUMN_PROPERTIES = qw(title type width align header_align wrap sortable filterable filter_on compare visible); | ||
| 2569 | my @STYLE_PROPERTIES = qw(text_color background_color bold italic underline border_color border_left border_right row_lines); | ||
| 2570 | |||
| 2571 | method _parse_column ($kid) { | ||
| 2572 | my @args = map { $_->as_perl } $kid->args->@*; | ||
| 2573 | die "Term::Fabulous::Widget::Table: layout property 'column' needs the column key as its one argument" unless @args == 1 && defined $args[0] && !ref $args[0]; | ||
| 2574 | my %props = map { $_->[0] => $_->[1]->as_perl } $kid->props->@*; | ||
| 2575 | my %allowed = map { $_ => 1 } @COLUMN_PROPERTIES; | ||
| 2576 | my @unknown = grep { !$allowed{$_} } sort keys %props; | ||
| 2577 | die "Term::Fabulous::Widget::Table: layout property 'column' does not accept @unknown (allowed: @COLUMN_PROPERTIES)" if @unknown; | ||
| 2578 | my %styles; | ||
| 2579 | foreach my $child ( $kid->children->@* ) { | ||
| 2580 | my $name = $child->name; | ||
| 2581 | die "Term::Fabulous::Widget::Table: a column node holds only 'style' and 'header_style' nodes, got '$name'" | ||
| 2582 | unless $name eq 'style' || $name eq 'header_style'; | ||
| 2583 | $styles{$name} = $self->kdl_properties( $child, @STYLE_PROPERTIES ); | ||
| 2584 | } | ||
| 2585 | $self->add_column( { key => $args[0], %props, %styles } ); | ||
| 2586 | return; | ||
| 2587 | } | ||
| 2588 | |||
| 2589 | method _parse_sort ($kid) { | ||
| 2590 | my @args = map { $_->as_perl } $kid->args->@*; | ||
| 2591 | die "Term::Fabulous::Widget::Table: layout property 'sort' takes a column key and optionally asc or desc" | ||
| 2592 | unless ( @args == 1 || @args == 2 ) && !$kid->props->@* && !$kid->children->@*; | ||
| 2593 | $self->sort_by( @{ $model->sort_spec }, [ $args[0], $args[1] // 'asc' ] ); | ||
| 2594 | return; | ||
| 2595 | } | ||
| 2596 | |||
| 2597 | method _parse_group_by ($kid) { | ||
| 2598 | my @keys = map { $_->as_perl } $kid->args->@*; | ||
| 2599 | die "Term::Fabulous::Widget::Table: layout property 'group_by' takes one or more column keys" unless @keys && !$kid->props->@* && !$kid->children->@*; | ||
| 2600 | $self->group_by(@keys); | ||
| 2601 | return; | ||
| 2602 | } | ||
| 2603 | |||
| 2604 | method _parse_lines ($kid) { | ||
| 2605 | my %line_of = ( top => 'border_top', right => 'border_right', bottom => 'border_bottom', left => 'border_left', columns => 'column_lines', rows => 'row_lines', header => 'header_line' ); | ||
| 2606 | my $props = $self->kdl_properties( $kid, 'frame', ( sort keys %line_of ), 'color' ); | ||
| 2607 | $self->border( $props->{frame} ) if exists $props->{frame}; | ||
| 2608 | $self->_line_property( $line_of{$_} => $props->{$_} ) foreach grep { exists $props->{$_} } sort keys %line_of; | ||
| 2609 | $self->line_color( $props->{color} ) if exists $props->{color}; | ||
| 2610 | return; | ||
| 2611 | } | ||
| 2612 | |||
| 2613 | method _parse_cell_padding ($kid) { | ||
| 2614 | my @args = $kid->args->@*; | ||
| 2615 | $self->cell_padding( @args ? $self->kdl_argument($kid)->as_perl : $self->kdl_properties( $kid, qw(left right top bottom) ) ); | ||
| 2616 | return; | ||
| 2617 | } | ||
| 2618 | |||
| 2619 | method _parse_page_sizes ($kid) { | ||
| 2620 | my @sizes = map { $_->as_perl } $kid->args->@*; | ||
| 2621 | die "Term::Fabulous::Widget::Table: layout property 'page_sizes' takes one or more numbers" unless @sizes && !$kid->props->@* && !$kid->children->@*; | ||
| 2622 | $self->page_sizes( \@sizes ); | ||
| 2623 | return; | ||
| 2624 | } | ||
| 2625 | |||
| 2626 | # --- Widgets --------------------------------------------------------- | ||
| 2627 | |||
| 2628 | # --------------------------------------------------------------------- | ||
| 2629 | # Searching (Term::Fabulous::Role::Searchable): every filtered row, | ||
| 2630 | # on any page, shown or collapsed | ||
| 2631 | # --------------------------------------------------------------------- | ||
| 2632 | |||
| 2633 | # The texts of the plain columns come from the model, so a search | ||
| 2634 | # reaches rows the table never built; the widgets of a custom cell | ||
| 2635 | # answer only while the row is built. | ||
| 2636 | method search_texts () { | ||
| 2637 | my @columns = $model->visible_columns; | ||
| 2638 | my @entries; | ||
| 2639 | foreach my $id ( $model->filtered_row_ids ) { | ||
| 2640 | my $built = $_built{ Term::Fabulous::Widget::Table::Model::row_key($id) }; | ||
| 2641 | foreach my $column (@columns) { | ||
| 2642 | my $key = $column->key; | ||
| 2643 | if ( !defined $column->cell ) { | ||
| 2644 | push @entries, [ [ $id, $key ], $model->display_of( $id, $key ) ]; | ||
| 2645 | next; | ||
| 2646 | } | ||
| 2647 | my $content = defined $built ? $built->{content}{$key} : undef; | ||
| 2648 | next unless defined $content; | ||
| 2649 | push @entries, map { [ [ $id, $key, @{ $_->[0] } ], $_->[1] ] } nested_search_texts( [ searchables_in($content) ] ); | ||
| 2650 | } | ||
| 2651 | } | ||
| 2652 | return @entries; | ||
| 2653 | } | ||
| 2654 | |||
| 2655 | # A key: [ row id, column key ] for a text cell, with the index and the | ||
| 2656 | # key of the widget inside for a custom cell. | ||
| 2657 | method _search_target ( $what, $key ) { | ||
| 2658 | die ref($self) . ": $what needs a key of [ row id, column key ] (and the index and key of a widget in a custom cell), got " . describe($key) | ||
| 2659 | unless ref $key eq 'ARRAY' && ( @$key == 2 || @$key == 4 ) && defined $key->[0] && !ref $key->[0] && defined $key->[1] && !ref $key->[1]; | ||
| 2660 | my ( $id, $column, @nested ) = @$key; | ||
| 2661 | return ( $id, $column, @nested ? [@nested] : undef ); | ||
| 2662 | } | ||
| 2663 | |||
| 2664 | method search_revision ($key) { | ||
| 2665 | my ( $id, $column, $nested ) = $self->_search_target( search_revision => $key ); | ||
| 2666 | return 'gone' unless $model->has_row($id) && $model->has_column($column); | ||
| 2667 | my $revision = join ':', $model->row_revision($id), $model->columns_revision; | ||
| 2668 | return $revision unless defined $nested; | ||
| 2669 | my $content = $self->cell_widget( $id, $column ) // return 'gone'; | ||
| 2670 | return join ':', $revision, nested_search_revision( [ searchables_in($content) ], $nested ); | ||
| 2671 | } | ||
| 2672 | |||
| 2673 | method highlight_text ( $key, $ranges ) { | ||
| 2674 | my ( $id, $column, $nested ) = $self->_search_target( highlight_text => $key ); | ||
| 2675 | return unless $model->has_row($id); | ||
| 2676 | my $slot = "$id\0$column"; | ||
| 2677 | my $built = $_built{ Term::Fabulous::Widget::Table::Model::row_key($id) }; | ||
| 2678 | if ( defined $nested ) { | ||
| 2679 | $_cell_highlights{$slot}{ join "\0", map { $_ // '' } @$nested } = [ @$nested, $ranges ]; | ||
| 2680 | my $content = defined $built ? $built->{content}{$column} : undef; | ||
| 2681 | nested_highlight_text( [ searchables_in($content) ], $nested, $ranges ) if defined $content; | ||
| 2682 | return; | ||
| 2683 | } | ||
| 2684 | |||
| 2685 | # The ranges are checked against the cell's text now, whether or | ||
| 2686 | # not the row is built: the text the cell shows is the model's. | ||
| 2687 | Term::Fabulous::Text::Marks->new( owner => ref($self), text => $model->display_of( $id, $column ) )->check_spans( highlight => $ranges ); | ||
| 2688 | $_text_highlights{$slot} = { ranges => $ranges, revision => $model->row_revision($id) }; | ||
| 2689 | my $text = defined $built ? $built->{text}{$column} : undef; | ||
| 2690 | $text->highlights($ranges) if defined $text; | ||
| 2691 | return; | ||
| 2692 | } | ||
| 2693 | |||
| 2694 | method clear_text_highlights () { | ||
| 2695 | %_text_highlights = (); | ||
| 2696 | %_cell_highlights = (); | ||
| 2697 | foreach my $built ( grep { $_->{kind} eq 'row' } values %_built ) { | ||
| 2698 | $_->clear_highlights foreach values %{ $built->{text} }; | ||
| 2699 | nested_clear_text_highlights( [ map { searchables_in($_) } grep { !$_->isa('Term::Fabulous::Widget::Text') } values %{ $built->{content} } ] ); | ||
| 2700 | } | ||
| 2701 | return; | ||
| 2702 | } | ||
| 2703 | |||
| 2704 | method _drop_row_highlights ($id) { | ||
| 2705 | delete @_text_highlights{ grep { index( $_, "$id\0" ) == 0 } keys %_text_highlights }; | ||
| 2706 | delete @_cell_highlights{ grep { index( $_, "$id\0" ) == 0 } keys %_cell_highlights }; | ||
| 2707 | return; | ||
| 2708 | } | ||
| 2709 | |||
| 2710 | # Shows the row: the rows and groups above it are expanded, the cursor | ||
| 2711 | # goes to it, which turns to its page and scrolls the body. The cell's | ||
| 2712 | # widget is there once the table was prepared again. | ||
| 2713 | method reveal_text ( $key, $offset ) { | ||
| 2714 | my ( $id, $column, $nested ) = $self->_search_target( reveal_text => $key ); | ||
| 2715 | return [ undef, undef ] unless $model->has_row($id); | ||
| 2716 | for ( my $parent = $model->parent_of($id); defined $parent; $parent = $model->parent_of($parent) ) { | ||
| 2717 | $model->set_expanded( $parent, 1 ); | ||
| 2718 | } | ||
| 2719 | my @path; | ||
| 2720 | foreach my $group_key ( $model->group_by ) { | ||
| 2721 | push @path, $model->value_of( $id, $group_key ); | ||
| 2722 | $model->set_group_collapsed( [@path], 0 ); | ||
| 2723 | } | ||
| 2724 | return [ undef, undef ] unless defined $model->line_index( Term::Fabulous::Widget::Table::Model::row_key($id) ); | ||
| 2725 | $self->cursor($id); | ||
| 2726 | my $built = $_built{ Term::Fabulous::Widget::Table::Model::row_key($id) } // return [ undef, undef ]; | ||
| 2727 | return [ $built->{text}{$column}, undef ] unless defined $nested; | ||
| 2728 | my $content = $built->{content}{$column} // return [ undef, undef ]; | ||
| 2729 | return nested_reveal_text( [ searchables_in($content) ], $nested, $offset ); | ||
| 2730 | } | ||
| 2731 | |||
| 2732 | # The widget a cell shows (its column's cell widget or a Text), once | ||
| 2733 | # the row is on the page and the table was prepared. | ||
| 2734 | method cell_widget ( $id, $key ) { | ||
| 2735 | my $built = $_built{ Term::Fabulous::Widget::Table::Model::row_key($id) } // return undef; | ||
| 2736 | return $built->{content}{$key}; | ||
| 2737 | } | ||
| 2738 | |||
| 2739 | method body () { return $_body } | ||
| 2740 | method pager_widget () { return $_pager } | ||
| 2741 | } | ||
| 2742 | |||
| 2743 | 1; | ||
| 2744 | |||
| 2745 | __END__ |