egui_deferred_table 0.1.7

An egui table, where the number of rows/columns is deferred
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
use egui::emath::GuiRounding;
use egui::scroll_area::ScrollBarVisibility;
use egui::{
    Color32, Context, CornerRadius, Frame, Id, Margin, NumExt, Painter, PointerButton, PopupAnchor,
    Pos2, Rangef, Rect, Response, RichText, Sense, Shadow, Stroke, StrokeKind, Style, Tooltip, Ui,
    UiBuilder, UiKind, UiStackInfo, Vec2,
};
use log::{info, trace};
use std::collections::BTreeSet;
use std::marker::PhantomData;
use std::ops::{Add, Range, Sub};

mod actions;
mod cells;
mod data_source;
mod dimensions;
mod editing;
mod ordering;
mod parameters;
mod slices;
mod table_renderer;

pub use actions::*;
pub use cells::*;
pub use data_source::*;
pub use dimensions::*;
pub use editing::*;
pub use ordering::*;
pub use parameters::*;
pub use slices::*;
pub use table_renderer::*;

const SHOW_HEADER_CELL_BORDERS: bool = false;
const SHOW_CELL_BORDERS: bool = false;

const EDITOR_FRAME: Frame = Frame {
    inner_margin: Margin::ZERO,
    stroke: Stroke::NONE,
    fill: Color32::TRANSPARENT,
    corner_radius: CornerRadius::ZERO,
    outer_margin: Margin::ZERO,
    shadow: Shadow::NONE,
};

pub struct DeferredTable<'a, DataSource> {
    id: Id,
    parameters: DeferredTableParameters<'a>,
    phantom_data: PhantomData<DataSource>,
}

impl<'a, DataSource> DeferredTable<'a, DataSource> {
    pub fn new(id: Id) -> Self {
        Self {
            id,
            parameters: DeferredTableParameters::default(),
            phantom_data: PhantomData,
        }
    }

    /// this currently also controls the row/column header and corner sizes
    pub fn default_cell_size(mut self, size: Vec2) -> Self {
        self.parameters.default_cell_size = Some(size);
        self
    }

    /// default: one-based headers
    pub fn zero_based_headers(mut self) -> Self {
        self.parameters.zero_based_headers = true;
        self
    }

    /// default: enabled
    pub fn one_based_headers(mut self) -> Self {
        self.parameters.zero_based_headers = false;
        self
    }

    /// default: enabled,
    pub fn selectable_rows(mut self) -> Self {
        self.parameters.selectable_rows = true;
        self
    }

    /// default: selectable-rows enabled
    pub fn selectable_rows_disabled(mut self) -> Self {
        self.parameters.selectable_rows = false;
        self
    }

    /// default: disabled
    pub fn highlight_hovered_cell(mut self) -> Self {
        self.parameters.highlight_hovered_cell = true;
        self
    }

    /// default: 400x200
    pub fn min_size(mut self, size: Vec2) -> Self {
        self.parameters.min_size = size;
        self
    }

    pub fn column_parameters(mut self, column_parameters: &'a Vec<AxisParameters>) -> Self {
        self.parameters.column_parameters = Some(column_parameters);
        self
    }

    pub fn row_parameters(mut self, row_parameters: &'a Vec<AxisParameters>) -> Self {
        self.parameters.row_parameters = Some(row_parameters);
        self
    }

    pub fn show<Renderer>(
        self,
        ui: &mut Ui,
        data_source: &mut DataSource,
        renderer: &mut Renderer,
    ) -> (Response, Vec<Action>)
    where
        DataSource: DeferredTableDataSource,
        Renderer: DeferredTableRenderer<DataSource>,
    {
        let editor: Option<&mut NullEditor> = None;
        let edit_state: Option<&mut EditorState<(), ()>> = None;
        self.show_outer::<Renderer, NullEditor, _, _>(ui, data_source, renderer, editor, edit_state)
    }

    pub fn show_and_edit<Renderer, Editor, IS, V>(
        self,
        ui: &mut Ui,
        data_source: &mut DataSource,
        renderer: &mut Renderer,
        editor: &mut Editor,
        edit_state: &mut EditorState<IS, V>,
    ) -> (Response, Vec<Action>)
    where
        DataSource: DeferredTableDataSource,
        Renderer: DeferredTableRenderer<DataSource>,
        Editor: EditableTableRenderer<DataSource, ItemState = IS, Value = V>,
    {
        self.show_outer(ui, data_source, renderer, Some(editor), Some(edit_state))
    }

    fn show_outer<Renderer, Editor, IS, V>(
        self,
        ui: &mut Ui,
        data_source: &mut DataSource,
        renderer: &mut Renderer,
        editor: Option<&mut Editor>,
        edit_state: Option<&mut EditorState<IS, V>>,
    ) -> (Response, Vec<Action>)
    where
        DataSource: DeferredTableDataSource,
        Renderer: DeferredTableRenderer<DataSource>,
        Editor: EditableTableRenderer<DataSource, ItemState = IS, Value = V>,
    {
        data_source.prepare();
        // cache the dimensions now, to remain consistent, since the data_source could return different dimensions
        // each time it's called.

        let dimensions = data_source.get_dimensions();

        let result = if !dimensions.is_empty() {
            self.show_inner(ui, data_source, renderer, dimensions, edit_state, editor)
        } else {
            (ui.response(), vec![])
        };

        data_source.finalize();

        result
    }

    fn outer_size(cell_size: Vec2, style: &Style) -> Vec2 {
        cell_size + style.spacing.item_spacing
    }

    /// Safety: only call if the dimensions are non-empty
    fn show_inner<Renderer, Editor, IS, V>(
        mut self,
        ui: &mut Ui,
        data_source: &mut DataSource,
        renderer: &mut Renderer,
        dimensions: TableDimensions,
        mut edit_state: Option<&mut EditorState<IS, V>>,
        mut editor: Option<&mut Editor>,
    ) -> (Response, Vec<Action>)
    where
        DataSource: DeferredTableDataSource,
        Renderer: DeferredTableRenderer<DataSource>,
        Editor: EditableTableRenderer<DataSource, ItemState = IS, Value = V>,
    {
        let ctx = ui.ctx().clone();
        let style = ui.style();
        let pixels_per_point = ctx.pixels_per_point();

        // We need an OPAQUE 'color' so that when it's used to fill the background of the editor popup window the
        // contents behind the window are obscured.
        let opaque_faint_bg_color = if style.visuals.faint_bg_color.is_opaque() {
            style.visuals.faint_bg_color
        } else {
            style.visuals.panel_fill.add(style.visuals.faint_bg_color)
        };

        let opaque_faint_selected_bg_color = style.visuals.selection.bg_fill.gamma_multiply(0.8);

        // we need to use `any_down`, since DRAGGING doesn't count as a click in `Response::clicked_elsewhere()`
        let (pointer_interact_pos, any_down) = ctx.input(|i| {
            (
                i.pointer.latest_pos().unwrap_or_default(),
                i.pointer.any_down(),
            )
        });

        let mut actions = vec![];

        let inner_cell_size: Vec2 = self.parameters.default_cell_size.unwrap_or(Vec2::new(
            style.spacing.interact_size.x * 1.5,
            style.spacing.interact_size.y,
        ));

        // XXX - remove this temporary hard-coded value
        // let inner_cell_size: Vec2 = (50.0, 25.0).into();

        let outer_cell_size = Self::outer_size(inner_cell_size, style);

        // FIXME if the column/row is too narrow/short then the hover/drag isn't detected, even though it's visible.
        //       to replicate, set 3 columns/rows to their minimum width/heights and then try resizing the middle one.
        //       as a workaround we clamp the minimum column/row width/heights to this.
        let minimum_resize_size = (style.interaction.resize_grab_radius_side * 2.0) + 2.0;

        let mut clear_drag_state = false;
        let mut request_row_selection_changed_action = false;

        // TODO allow these to be overridden
        let default_column_parameters = AxisParameters::default();
        let default_row_parameters = AxisParameters::default();

        enum DragAction {
            SetWidth(usize, f32),
            SetHeight(usize, f32),
        }
        let mut drag_action = None;

        let pointer_pos = ui.ctx().pointer_latest_pos();

        let temp_state_id = self.id.with("temp_state");
        let mut temp_state = DeferredTableTempState::load_or_default(&ctx, temp_state_id);

        let persistent_state_id = self.id.with("persistent_state");
        let mut state = DeferredTablePersistentState::load_or_default(&ctx, persistent_state_id);

        trace!("dimensions: {:?}", dimensions);

        let dimensions_changed = temp_state
            .dimensions
            .map_or(true, |previous_frame_dimensions| {
                previous_frame_dimensions != dimensions
            });

        if dimensions_changed {
            temp_state.dimensions = Some(dimensions);

            // remove non-visible selections
            temp_state.row_selections.retain(|&mapped_row_id| {
                let visible = mapped_row_id < dimensions.row_count;

                if !visible {
                    request_row_selection_changed_action = true;
                }
                visible
            });
        }

        let parent_max_rect = ui.max_rect();
        let parent_clip_rect = ui.clip_rect();
        let ui_layer_id = ui.layer_id();

        // the x/y of this can have negative values if the OUTER scroll area is scrolled right or down, respectively.
        // i.e. if the outer scroll area scrolled down, the y will be negative, above the visible area.
        let outer_next_widget_position = ui.next_widget_position();
        trace!(
            "outer_next_widget_position: {:?}",
            outer_next_widget_position
        );

        // CRITICAL - we *must* round to pixels, otherwise we get out-by-one pixel errors when rendering lines

        // if there is content above the table, we use this min rect so we to define an area starting at the right place.
        let outer_min_rect =
            Rect::from_min_size(outer_next_widget_position, self.parameters.min_size.clone())
                .round_to_pixels(pixels_per_point);
        // FIXME if the parent_max_rect is too small, min_size is not respected, but using
        //       ... `parent_max_rect.size().at_least(self.parameters.min_size)` causes rendering errors
        let outer_max_rect =
            Rect::from_min_size(outer_next_widget_position, parent_max_rect.size())
                .round_to_pixels(pixels_per_point);

        trace!(
            "outer_min_rect: {:?}, outer_max_rect: {:?}",
            outer_min_rect, outer_max_rect
        );

        if false {
            ui.painter()
                .debug_rect(outer_min_rect, Color32::GREEN, "omnr");
            ui.painter()
                .debug_rect(outer_max_rect, Color32::RED, "omxr");
        }

        ui.scope_builder(UiBuilder::new().max_rect(outer_max_rect), |ui|{

            ui.style_mut().spacing.scroll = egui::style::ScrollStyle::solid();

            let inner_max_rect = ui.max_rect();
            // FUTURE since these are the same, we can clean-up one or the other...
            debug_assert_eq!(inner_max_rect, outer_max_rect);

            let previous_cell_origin = temp_state.cell_origin;
            trace!("previous_cell_origin: {:?}", previous_cell_origin);

            // ensure there is a column width for each possible column
            if state.column_widths.len() < dimensions.column_count {
                // Note: We do not truncate the column widths, so that if a data source has `n` columns, then later `< n` columns
                //       then later again `>= n` columns, the previously used columns widths still apply.
                state.column_widths.resize(dimensions.column_count, inner_cell_size.x);

                // apply default widths
                if let Some(column_parameters) = self.parameters.column_parameters {
                    column_parameters.iter().enumerate().for_each(|(index, column)| {
                        if let Some(default_width) = column.default_dimension {
                            let sanitized_width = if column.resizable {
                                column.dimension_range.clamp(default_width)
                            } else {
                                default_width
                            };
                            state.column_widths[index] = sanitized_width;
                        }
                    });
                }
            }

            // ensure there is a row height for each possible row
            if state.row_heights.len() < dimensions.row_count {
                // Note: We do not truncate the row heights, so that if a data source has `n` rows, then later `< n` rows
                //       then later again `>= n` rows, the previously used rows heights still apply.
                state.row_heights.resize(dimensions.row_count, inner_cell_size.y);

                // apply default heights
                if let Some(row_parameters) = self.parameters.row_parameters {
                    row_parameters.iter().enumerate().for_each(|(index, row)| {
                        if let Some(default_height) = row.default_dimension {
                            let sanitized_width = if row.resizable {
                                row.dimension_range.clamp(default_height)
                            } else {
                                default_height
                            };
                            state.row_heights[index] = sanitized_width;
                        }
                    });
                }
            }

            // XXX - remove this temporary hard-coded value
            // //state.column_widths[10] = 25.0;
            // state.column_widths[1] = 25.0;
            // state.column_widths[2] = 200.0;
            // state.column_widths[3] = 25.0;
            // state.column_widths[6] = 200.0;
            // state.column_widths[12] = 200.0;
            // // state.row_heights[10] = 10.0;
            // state.row_heights[1] = 10.0;
            // state.row_heights[2] = 100.0;
            // state.row_heights[3] = 10.0;
            // state.row_heights[6] = 100.0;
            // state.row_heights[12] = 100.0;

            let scroll_style = ui.spacing().scroll;

            #[derive(Debug, Copy, Clone, Hash)]
            enum CellId {
                Corner,
                MappedColumn(usize),
                MappedRow(usize),
                Cell(CellIndex),
            }

            //
            // container for the table and the scroll bars.
            //

            let column_ordering = renderer.column_ordering().unwrap_or_default();
            let row_ordering = renderer.row_ordering().unwrap_or_default();

            let outer_inner_difference = outer_cell_size - inner_cell_size;
            // pre-calculate to avoid doing the divide for every cell.
            let outer_inner_half_difference = outer_inner_difference / 2.0;

            // add the width/height of the column/row headers to the sum of the column widths/row heights, respectively,
            // while ignoring widths/heights that don't apply to the current dimensions.
            let total_content_width = state.column_widths
                .iter()
                .take(dimensions.column_count)
                .sum::<f32>() + ((outer_inner_difference.x + 1.0) * dimensions.column_count as f32) + outer_cell_size.x;
            let total_content_height = state.row_heights
                .iter()
                .take(dimensions.row_count)
                .sum::<f32>() + ((outer_inner_difference.y + 1.0) * dimensions.row_count as f32) + outer_cell_size.y;

            let columns_to_filter = renderer.columns_to_filter();
            let filtered_content_width = columns_to_filter.map_or(0.0,|columns|{
                columns.iter().take(dimensions.column_count).map(|index| {
                    let mapped_index = Self::map_index(dimensions.column_count, column_ordering, *index);
                    state.column_widths.get(mapped_index).map(|it|it + outer_inner_difference.x + 1.0).unwrap_or(0.0)
                }).sum::<f32>()
            });

            let rows_to_filter = renderer.rows_to_filter();
            let filtered_content_height = rows_to_filter.map_or(0.0,|rows|{
                rows.iter().take(dimensions.row_count).map(|index| {
                    let mapped_index = Self::map_index(dimensions.column_count, column_ordering, *index);
                    state.row_heights.get(mapped_index).map(|it|it + outer_inner_difference.y + 1.0).unwrap_or(0.0)
                }).sum::<f32>()
            });

            let mut total_content_size = Vec2::new(
                total_content_width - filtered_content_width,
                total_content_height - filtered_content_height,
            );
            trace!("total_content_size: {:?}, filtered_content_width: {}, filtered_content_height: {}", total_content_size, filtered_content_width, filtered_content_height);

            ui.scope_builder(UiBuilder::new().max_rect(inner_max_rect), |ui|{

                // table_max_rect is the rect INSIDE any OUTER scroll area, e.g. when *this* table is rendered inside a scrollarea
                // as the outer scroll area is scrolled,
                let table_max_rect = Rect::from_min_size(
                    inner_max_rect.min,
                    (
                        inner_max_rect.size().x - scroll_style.bar_width,
                        inner_max_rect.size().y - scroll_style.bar_width,
                    ).into()
                );
                //ui.ctx().debug_painter().debug_rect(table_max_rect, Color32::MAGENTA, "tmr");
                trace!("table_max_rect: {:?}", table_max_rect);

                if false {
                    ui.painter().debug_rect(inner_max_rect, Color32::PURPLE, "imr");
                    ui.painter().debug_rect(table_max_rect, Color32::MAGENTA, "tmr");
                }

                let available_space = ui.max_rect();
                let available_width = available_space.width() - (scroll_style.bar_width + scroll_style.bar_outer_margin + scroll_style.bar_inner_margin);

                // when laying out columns, we can add a portion of this to any expandable columns.
                let additional_width = if total_content_size.x < available_width {
                    available_width - total_content_size.x
                } else {
                    0.0
                };

                total_content_size.x += additional_width;

                egui::ScrollArea::both()
                    .id_salt("table_scroll_area")
                    .scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
                    .show_viewport(ui, |ui, viewport_rect| {
                        let viewport_changed = temp_state.last_viewport_rect.map_or(false, |last_viewport_rect| {
                            last_viewport_rect != viewport_rect
                        });

                        temp_state.last_viewport_rect = Some(viewport_rect);

                        trace!("max_rect: {:?}, viewport_rect: {:?}", ui.max_rect(), viewport_rect);
                        //ui.painter().debug_rect(ui.max_rect(), Color32::RED, "mr");
                        let translated_viewport_rect = viewport_rect.translate(ui.max_rect().min.to_vec2());
                        let cells_viewport_rect = Rect::from_min_max(viewport_rect.min, viewport_rect.max - outer_cell_size);
                        if false {
                            ui.ctx().debug_painter().debug_rect(translated_viewport_rect, Color32::GREEN, "vr");
                            ui.ctx().debug_painter().debug_rect(cells_viewport_rect.translate(ui.max_rect().min.to_vec2()).translate(outer_cell_size), Color32::RED, "tvr");
                        }

                        ui.set_height(total_content_size.y);
                        ui.set_width(total_content_size.x);

                        //ui.ctx().debug_painter().debug_rect(ui.max_rect(), Color32::RED, "mr");

                        fn range_and_index_for_offset(offset: f32, values: &[f32], map: &[usize], filter: &Option<&[usize]>, sizing: f32) -> Result<(Range<f32>, usize, usize, usize), ()> {
                            let mut visible_index = 0;
                            let mut min = 0.0;
                            let mut max = 0.0;
                            let mut filtered = 0;
                            let mut index ;
                            let values_len = values.len();
                            loop {
                                index = *map.get(visible_index).unwrap_or(&visible_index);
                                if index >= values_len {
                                    // handle out-of-range mapping values
                                    index = visible_index;
                                }

                                let Some(value) = values.get(index) else {
                                    if visible_index == 0 {
                                        // no values at all
                                        return Err(())
                                    }
                                    // no more values, use previous loop iteration values
                                    break
                                };

                                // filter applies AFTER mapping
                                if let Some(filter) = filter {
                                    if filter.contains(&index) {
                                        visible_index += 1;
                                        filtered += 1;
                                        continue;
                                    }
                                }

                                let size = value + sizing;
                                max += size;

                                if offset >= min && offset < max {
                                    break
                                }

                                min += size;
                                visible_index += 1;
                            }

                            Ok((min..max, index, visible_index, filtered))
                        }

                        // use the cells_viewport_rect for upper left and origin calculation
                        let (first_column, first_column_index, first_column_visible_index, first_column_filtered_count) = range_and_index_for_offset(cells_viewport_rect.min.x, &state.column_widths, &column_ordering, &columns_to_filter, outer_inner_difference.x + 1.0).unwrap();
                        let (first_row, first_row_index, first_row_visible_index, first_row_filtered_count) = range_and_index_for_offset(cells_viewport_rect.min.y, &state.row_heights, &row_ordering, &rows_to_filter, outer_inner_difference.y + 1.0).unwrap();

                        // use the total viewport (including header area) to find the last column and row
                        let (last_column, _last_column_index, last_column_visible_index, last_column_filtered_count) = range_and_index_for_offset(viewport_rect.max.x, &state.column_widths, &column_ordering, &columns_to_filter, outer_inner_difference.x + 1.0).unwrap();
                        let (last_row, _last_row_index, last_row_visible_index, last_row_filtered_count) = range_and_index_for_offset(viewport_rect.max.y, &state.row_heights, &row_ordering, &rows_to_filter, outer_inner_difference.y + 1.0).unwrap();

                        // note, if the scroll area doesn't line up exactly with the viewport, then we may have to render additional rows/columns that
                        // are outside of this rect
                        let rect = Rect::from_min_max((first_column.start, first_row.start).into(), (last_column.end, last_row.end).into())
                            .translate(ui.max_rect().min.to_vec2())
                            .round_to_pixels(pixels_per_point);

                        trace!("rect: {:?}", rect);
                        if false {
                            ui.ctx().debug_painter().debug_rect(rect, Color32::CYAN, "rect");
                        }

                        trace!("first_column_index: {}, first_column_index: {}, first_column_visible_index: {}", first_column_index, first_column_index, first_column_visible_index);
                        trace!("first_row_index: {}, first_row_index: {}, first_row_visible_index: {}", first_row_index, first_row_index, first_row_visible_index);

                        let cell_origin = CellIndex {
                            row: first_row_visible_index,
                            column: first_column_visible_index,
                        };
                        trace!("cell_origin: {:?}", cell_origin);
                        temp_state.cell_origin = cell_origin;

                        let visible_row_count = last_row_visible_index - first_row_visible_index + 1 + last_row_filtered_count;
                        let visible_column_count = last_column_visible_index - first_column_visible_index + 1 + last_column_filtered_count;
                        trace!("visible_row_count: {}, visible_column_count: {}", visible_row_count, visible_column_count);
                        trace!("first_column_filtered_count: {}, last_column_filtered_count: {}", first_column_filtered_count, last_column_filtered_count);
                        trace!("first_row_filtered_count: {}, last_row_filtered_count: {}", first_row_filtered_count, last_row_filtered_count);

                        let mut table_width = 0.0;
                        let mut table_height = 0.0;

                        let mut row_counter = cell_origin.row - first_row_filtered_count;

                        trace!("headers");
                        let header_row_bg_color = ui.style().visuals.widgets.inactive.bg_fill.gamma_multiply(0.5);
                        let mut accumulated_row_heights = 0.0;
                        for grid_row_index in 0..=visible_row_count {
                            if grid_row_index + cell_origin.row > dimensions.row_count {
                                trace!("break 1");
                                break
                            }

                            let visible_row_index = cell_origin.row + (grid_row_index.saturating_sub(1));
                            let mapped_row_index = Self::map_index(dimensions.row_count, row_ordering, visible_row_index);

                            let row_kind = Self::build_row_kind(grid_row_index);

                            if matches!(row_kind, RowKind::ValuesRow) {
                                if let Some(rows_to_filter) = &rows_to_filter {
                                    if rows_to_filter.contains(&(mapped_row_index)) {
                                        trace!("filtered row");
                                        continue;
                                    }
                                }
                            }
                            row_counter += 1;

                            let row_was_selected = if matches!(row_kind, RowKind::ValuesRow) && self.parameters.selectable_rows {
                                temp_state.row_selections.contains(&mapped_row_index)
                            } else {
                                false
                            };

                            let row_bg_color = Self::pick_row_bg_color(opaque_faint_bg_color, opaque_faint_selected_bg_color, ui, row_counter, row_was_selected);

                            let inner_row_height = match row_kind {
                                RowKind::ValuesRow => *state.row_heights.get(mapped_row_index).unwrap_or(&inner_cell_size.y),
                                RowKind::HeaderRow => inner_cell_size.y,
                            };
                            let outer_row_height = inner_row_height + outer_inner_difference.y;

                            let mut accumulated_column_widths = 0.0;

                            // Prevent applying expansion twice.
                            let mut expansion_applied = false;

                            for grid_column_index in 0..=visible_column_count {
                                if grid_column_index + cell_origin.column > dimensions.column_count {
                                    break
                                }

                                let cell_kind = Self::build_cell_kind(grid_row_index, grid_column_index);

                                if matches!(cell_kind, CellKind::Value) {
                                    // no cell rendering during header rendering
                                    // we're just rendering the top and left headers
                                    break
                                }

                                let visible_column_index = cell_origin.column + (grid_column_index.saturating_sub(1));
                                let mapped_column_index = Self::map_index(dimensions.column_count, column_ordering, visible_column_index);

                                if matches!(cell_kind, CellKind::ColumnHeader) {
                                    if let Some(columns_to_filter) = &columns_to_filter {
                                        if columns_to_filter.contains(&mapped_column_index) {
                                            trace!("filtered column");
                                            continue;
                                        }
                                    }
                                }

                                let start_pos = match cell_kind {
                                    // for smooth scrolling, we position the cell using rect.min, then later we clip the left/top of the partial cell
                                    CellKind::ColumnHeader | CellKind::RowHeader => rect.min,
                                    // for the corner we fix the cell use the top/left
                                    CellKind::Corner => table_max_rect.min,
                                    _ => unreachable!()
                                };

                                let inner_column_width = if matches!(cell_kind, CellKind::ColumnHeader) {
                                    let mut width = state.column_widths[mapped_column_index];
                                    if !expansion_applied {
                                        if let Some(column_parameters) = self.parameters.column_parameters {
                                            match column_parameters.get(mapped_column_index) {
                                                Some(params) if params.expandable => {
                                                    expansion_applied = true;
                                                    width += additional_width
                                                }
                                                _ => {}
                                            }
                                        }
                                    }
                                    width
                                } else {
                                    inner_cell_size.x
                                };

                                let outer_column_width = inner_column_width + outer_inner_difference.x;

                                let mut y = start_pos.y + accumulated_row_heights;
                                let mut x = start_pos.x + accumulated_column_widths;
                                accumulated_column_widths += outer_column_width + 1.0;

                                if matches!(cell_kind, CellKind::Corner | CellKind::ColumnHeader) {
                                    y = table_max_rect.min.y;
                                }
                                if matches!(cell_kind, CellKind::Corner | CellKind::RowHeader) {
                                    x = table_max_rect.min.x;
                                }

                                let cell_rect = Rect::from_min_size(Pos2::new(x, y), (outer_column_width, outer_row_height).into());

                                let mut cell_clip_rect = cell_rect.intersect(translated_viewport_rect);

                                if grid_row_index == 1 {
                                    cell_clip_rect.min.y = table_max_rect.min.y + outer_cell_size.y + 1.0;
                                }
                                if grid_column_index == 1 {
                                    cell_clip_rect.min.x = table_max_rect.min.x + outer_cell_size.x + 1.0;
                                }
                                let cell_clip_rect = cell_clip_rect.intersect(parent_clip_rect);

                                let cell_inner_rect = cell_rect.shrink2(outer_inner_half_difference);
                                let cell_inner_clip_rect = cell_inner_rect.intersect(cell_clip_rect);

                                if false {
                                    ui.painter().debug_rect(cell_clip_rect, Color32::ORANGE, "ccr");
                                }

                                let cell_clip_rect_size = cell_clip_rect.size();
                                let skip = cell_clip_rect_size.x < 0.0 || cell_clip_rect_size.y < 0.0;

                                trace!("grid: i=[{},{}] v=[{},{}], m=[{},{}], cell_rect: {:?}, cell_clip_rect: {:?}, pos: {:?}, size: {:?}, skip: {}",
                                    grid_row_index, grid_column_index,
                                    visible_row_index, visible_column_index,
                                    mapped_row_index, mapped_column_index,
                                    cell_rect, cell_clip_rect, cell_clip_rect.min, cell_clip_rect_size, skip);

                                if skip {
                                    continue;
                                }

                                let bg_color = if grid_row_index == 0 {
                                    header_row_bg_color
                                } else {
                                    row_bg_color
                                };

                                let cell_painter = ui.painter()
                                    .with_clip_rect(cell_clip_rect);

                                cell_painter
                                    .rect_filled(cell_rect, 0.0, bg_color);

                                if SHOW_HEADER_CELL_BORDERS {
                                    cell_painter
                                        .rect_stroke(cell_rect, CornerRadius::ZERO, ui.style().visuals.widgets.noninteractive.bg_stroke, StrokeKind::Inside);
                                }

                                let resize_painter = ui.painter()
                                    .with_clip_rect(parent_clip_rect);

                                let mut drag_tooltip_message = None;

                                if matches!(cell_kind, CellKind::ColumnHeader) {
                                    let column_parameters = self.parameters.column_parameters
                                        .map(|it|it.get(mapped_column_index))
                                        .flatten()
                                        .unwrap_or_else(|| {
                                        &default_column_parameters
                                    });

                                    let column_resize_id = ui.id().with("resize_column").with(mapped_column_index);

                                    let resize_line_points = [cell_rect.right_top(), cell_rect.right_bottom()];
                                    let resize_interact_rect = Rect::from(resize_line_points)
                                        .expand2(Vec2::new(ui.style().interaction.resize_grab_radius_side, 0.0));

                                    if false {
                                        ui.painter().debug_rect(resize_interact_rect, Color32::MAGENTA, "r");
                                    }

                                    let resize_response =
                                        ui.interact(resize_interact_rect, column_resize_id, egui::Sense::click_and_drag());

                                    let mut drag_handle_state = if resize_response.hovered() {
                                        // if !column_parameters.resizable || column_parameters.expandable {
                                        if !column_parameters.resizable {
                                            DragHandleState::Disabled
                                        } else {
                                            DragHandleState::Hovered
                                        }
                                    } else {
                                        DragHandleState::Inactive
                                    };

                                    // if column_parameters.resizable && !column_parameters.expandable {
                                    if column_parameters.resizable {
                                        if resize_response.drag_started_by(PointerButton::Primary) && temp_state.drag_state.is_none() {
                                            temp_state.drag_state = pointer_pos.map(|start_pos| DragState { index: mapped_column_index, start_pos, cell_kind: cell_kind, initial_size: outer_column_width });
                                        }

                                        if resize_response.drag_stopped() {
                                            clear_drag_state = true;
                                        }

                                        match temp_state.drag_state {
                                            Some(DragState { index, start_pos, cell_kind: drag_cell_kind, initial_size }) if index == mapped_column_index && drag_cell_kind == cell_kind => {
                                                // dragging this column
                                                let drag_delta = pointer_pos.map_or(Vec2::ZERO, |current_pos| current_pos - start_pos);
                                                let new_outer_column_width = initial_size + drag_delta.x;
                                                let new_inner_column_width = new_outer_column_width - outer_inner_difference.x;

                                                let sanitized_column_width = column_parameters.dimension_range.clamp(new_inner_column_width);

                                                let new_column_width = sanitized_column_width.at_least(minimum_resize_size);

                                                if new_column_width != inner_column_width {
                                                    // change at the end of the frame to avoid cells being the old size.
                                                    drag_action = Some(DragAction::SetWidth(mapped_column_index, new_column_width));
                                                }
                                                drag_tooltip_message = Some(format!("{}", new_column_width));

                                                drag_handle_state = DragHandleState::Dragged;
                                            }
                                            _ => {}
                                        };
                                    }

                                    Self::paint_resize_handle(ui, resize_line_points, drag_handle_state, &resize_painter, cell_kind);
                                }

                                if matches!(cell_kind, CellKind::RowHeader) {
                                    let row_resize_id = ui.id().with("resize_row").with(grid_row_index);

                                    let resize_line_points = [cell_rect.left_bottom(), cell_rect.right_bottom()];
                                    let resize_interact_rect = Rect::from(resize_line_points)
                                        .expand2(Vec2::new(0.0, ui.style().interaction.resize_grab_radius_side));

                                    let resize_response =
                                        ui.interact(resize_interact_rect, row_resize_id, egui::Sense::click_and_drag());

                                    let mut drag_handle_state = if resize_response.hovered() {
                                        DragHandleState::Hovered
                                    } else {
                                        DragHandleState::Inactive
                                    };

                                    if resize_response.drag_started_by(PointerButton::Primary) && temp_state.drag_state.is_none() {
                                        temp_state.drag_state = pointer_pos.map(|start_pos|DragState { index: mapped_row_index, start_pos, cell_kind: cell_kind, initial_size: outer_row_height });
                                    }

                                    if resize_response.drag_stopped() {
                                        clear_drag_state = true;
                                    }

                                    match temp_state.drag_state {
                                        Some(DragState { index, start_pos, cell_kind: drag_cell_kind, initial_size }) if index == mapped_row_index && drag_cell_kind == cell_kind => {
                                            // dragging this row
                                            let drag_delta = pointer_pos.map_or(Vec2::ZERO, |current_pos| current_pos - start_pos);
                                            let new_outer_row_height = initial_size + drag_delta.y;
                                            let new_inner_row_height = new_outer_row_height - outer_inner_difference.y;
                                            let new_row_height = Rangef::new(minimum_resize_size, f32::INFINITY).clamp(new_inner_row_height);

                                            if new_row_height != inner_row_height {
                                                // change at the end of the frame to avoid cells being the old size.
                                                drag_action = Some(DragAction::SetHeight(mapped_row_index, new_row_height));
                                            }
                                            drag_tooltip_message = Some(format!("{}", new_row_height));

                                            drag_handle_state = DragHandleState::Dragged;
                                        }
                                        _ => { }
                                    }

                                    Self::paint_resize_handle(ui, resize_line_points, drag_handle_state, &resize_painter, cell_kind);
                                }

                                if let Some(message) = drag_tooltip_message {
                                    Tooltip::always_open(ctx.clone(), ui_layer_id, "_egui_deferred_table_resize_".into(), PopupAnchor::Pointer)
                                        .gap(12.0)
                                        .show(|ui|{
                                            ui.horizontal(|ui|{
                                                ui.label(message);
                                            });
                                        });
                                }

                                let response = ui.allocate_rect(cell_clip_rect, Sense::click_and_drag());

                                let cell_id = match cell_kind {
                                    CellKind::Corner => CellId::Corner,
                                    CellKind::ColumnHeader => CellId::MappedColumn(mapped_column_index),
                                    CellKind::RowHeader => CellId::MappedRow(mapped_row_index),
                                    CellKind::Value => unreachable!(),
                                };

                                if matches!(cell_kind, CellKind::ColumnHeader | CellKind::RowHeader) {
                                    response.dnd_set_drag_payload(cell_id);
                                }

                                let mut cell_ui = ui.new_child(UiBuilder::new()
                                    .id_salt(cell_id)
                                    .max_rect(cell_inner_rect));
                                cell_ui.set_clip_rect(cell_inner_clip_rect);
                                let style = cell_ui.style_mut();
                                style.wrap_mode = Some(egui::TextWrapMode::Extend);

                                let mut monospace = false;
                                let label = match cell_kind {
                                    CellKind::Corner => {
                                        Some(format!("{}*{} ({},{})", dimensions.column_count, dimensions.row_count, cell_origin.column, cell_origin.row))
                                    }
                                    CellKind::ColumnHeader => {
                                        monospace = default_column_parameters.monospace;

                                        if let Some(column_parameters) = self.parameters
                                            .column_parameters
                                            .map(|it| it.get(mapped_column_index))
                                            .flatten()
                                        {
                                            monospace = column_parameters.monospace;
                                            column_parameters.name.clone()
                                        } else if self.parameters.zero_based_headers {
                                            Some(mapped_column_index.to_string())
                                        } else {
                                            let mapped_column_number = mapped_column_index + 1;
                                            Some(mapped_column_number.to_string())
                                        }
                                    }
                                    CellKind::RowHeader => {
                                        monospace = default_row_parameters.monospace;
                                        if let Some(row_parameters) = self.parameters
                                            .row_parameters
                                            .map(|it| it.get(mapped_row_index))
                                            .flatten()
                                        {
                                            monospace = row_parameters.monospace;
                                            row_parameters.name.clone()
                                        } else if self.parameters.zero_based_headers {
                                            Some(mapped_row_index.to_string())
                                        } else {
                                            let mapped_row_number = mapped_row_index + 1;
                                            Some(mapped_row_number.to_string())
                                        }
                                    },
                                    CellKind::Value => {
                                        // already filtered out
                                        unreachable!()
                                    }
                                };

                                if let Some(label) = &label {
                                    //cell_ui.label(format!("{:?}", cell_ui.id()));
                                    let mut text = RichText::new(label);

                                    if monospace {
                                        text = text.monospace();
                                    }

                                    cell_ui.add({
                                        egui::Label::new(text).selectable(false)
                                    });
                                }

                                if response.clicked() {
                                    match cell_kind {
                                        CellKind::RowHeader => {
                                            if self.parameters.selectable_rows {
                                                match row_was_selected {
                                                    true => { temp_state.row_selections.remove(&mapped_row_index); },
                                                    false => { temp_state.row_selections.insert(mapped_row_index); },
                                                }
                                                request_row_selection_changed_action = true;
                                            }
                                        }
                                        // TODO selectable columns?
                                        _ => {}
                                    }
                                }

                                if !matches!(cell_kind, CellKind::Corner) {
                                    if let Some(label) = label {
                                        if response.dragged() {
                                            Tooltip::always_open(ctx.clone(), ui_layer_id, "_egui_deferred_table_dnd_".into(), PopupAnchor::Pointer)
                                                .gap(12.0)
                                                .show(|ui| {
                                                    ui.horizontal(|ui| {
                                                        ui.label(label);
                                                    });
                                                });
                                        }
                                    }

                                    // Highlight drop target
                                    if response.dnd_hover_payload::<CellId>().is_some() {
                                        ui.painter().rect_filled(
                                            cell_clip_rect,
                                            CornerRadius::ZERO,
                                            ui.style().visuals.selection.bg_fill.gamma_multiply(0.25),
                                        );
                                    }

                                    // handle dnd release
                                    if let Some(payload) = response.dnd_release_payload::<CellId>() {
                                        match (*payload, cell_id) {
                                            // currently only dragging like onto like is supported.
                                            (CellId::MappedColumn(payload_index), CellId::MappedColumn(current_index)) => if payload_index != current_index {
                                                info!("dnd release: column {} -> column {}", payload_index, current_index);
                                                actions.push(Action::ColumnReorder{ from: payload_index, to: mapped_column_index })
                                            }
                                            (CellId::MappedRow(payload_index), CellId::MappedRow(current_index)) => if payload_index != current_index {
                                                info!("dnd release: row {} -> row {}", payload_index, current_index);
                                                actions.push(Action::RowReorder{ from: payload_index, to: current_index })
                                            }
                                            _ => ()
                                        }
                                    }
                                }

                                if grid_row_index == 0 {
                                    table_width += cell_clip_rect.size().x + 1.0;
                                }
                                if grid_column_index == 0 {
                                    table_height += cell_clip_rect.size().y + 1.0;
                                }
                            }
                            accumulated_row_heights += outer_row_height + 1.0;
                        }

                        trace!("cells");

                        let cells_clip_rect = Rect::from_min_max((table_max_rect.min + outer_cell_size) + Vec2::splat(1.0), translated_viewport_rect.max).intersect(parent_clip_rect);
                        if false {
                            ui.painter().debug_rect(cells_clip_rect, Color32::CYAN, "cr");
                        }

                        ui.scope_builder(UiBuilder::new().max_rect(rect), |ui| {
                            ui.set_clip_rect(translated_viewport_rect);

                            let table_max_rect = ui.max_rect();

                            //
                            // display the table
                            //

                            let start_pos = table_max_rect.min;

                            // reset the visual row index for the cells, skipping the header row.
                            row_counter = cell_origin.row + 1 - first_row_filtered_count;

                            // start with an offset equal to header height, which is currently using the cell_size
                            let mut accumulated_row_heights = outer_cell_size.y + 1.0;
                            for grid_row_index in 1..=visible_row_count {
                                if grid_row_index + cell_origin.row > dimensions.row_count {
                                    break
                                }

                                let visible_row_index = cell_origin.row + (grid_row_index.saturating_sub(1));
                                let mapped_row_index = Self::map_index(dimensions.row_count, row_ordering, visible_row_index);

                                if let Some(rows_to_filter) = &rows_to_filter {
                                    if rows_to_filter.contains(&mapped_row_index) {
                                        trace!("filtered row");
                                        continue;
                                    }
                                }
                                row_counter += 1;

                                let inner_row_height = state.row_heights[mapped_row_index];
                                let outer_row_height = inner_row_height + outer_inner_difference.y;

                                let row_was_selected = if self.parameters.selectable_rows {
                                    temp_state.row_selections.contains(&mapped_row_index)
                                } else {
                                    false
                                };

                                let row_bg_color = Self::pick_row_bg_color(opaque_faint_bg_color, opaque_faint_selected_bg_color, ui, row_counter, row_was_selected);

                                let y = start_pos.y + accumulated_row_heights;

                                // start with an offset equal to header width, which is currently using the cell_size
                                let mut accumulated_column_widths = outer_cell_size.x + 1.0;

                                for grid_column_index in 1..=visible_column_count {
                                    if grid_column_index + cell_origin.column > dimensions.column_count {
                                        break
                                    }

                                    let visible_column_index = cell_origin.column + (grid_column_index - 1);
                                    let mapped_column_index = Self::map_index(dimensions.column_count, column_ordering, visible_column_index);

                                    if let Some(columns_to_filter) = &columns_to_filter {
                                        if columns_to_filter.contains(&mapped_column_index) {
                                            trace!("filtered column");
                                            continue;
                                        }
                                    }

                                    let inner_column_width = {
                                        let mut width = state.column_widths[mapped_column_index];
                                        if let Some(column_parameters) = self.parameters.column_parameters {
                                            match column_parameters.get(mapped_column_index) {
                                                Some(params) if params.expandable => {
                                                    width += additional_width
                                                }
                                                _ => {}
                                            }
                                        }
                                        width
                                    };
                                    let outer_column_width = inner_column_width + outer_inner_difference.x;

                                    let cell_index = CellIndex {
                                        row: mapped_row_index,
                                        column: mapped_column_index,
                                    };

                                    let x = start_pos.x + accumulated_column_widths;
                                    accumulated_column_widths += outer_column_width + 1.0;

                                    let cell_rect = Rect::from_min_size(Pos2::new(x, y), (outer_column_width, outer_row_height).into());
                                    let cell_clip_rect = cell_rect.intersect(cells_clip_rect);
                                    let cell_clip_rect_size = cell_clip_rect.size();

                                    let cell_inner_rect = cell_rect.shrink2(outer_inner_half_difference);
                                    let cell_inner_clip_rect = cell_inner_rect.intersect(cell_clip_rect);

                                    let skip = cell_clip_rect_size.x < 0.0 || cell_clip_rect_size.y < 0.0;

                                    trace!("grid: r={}, c={}, rect: {:?}, pos: {:?}, size: {:?}, skip: {}", grid_row_index, grid_column_index, cell_clip_rect, cell_clip_rect.min, cell_clip_rect_size, skip);

                                    if skip {
                                        continue;
                                    }

                                    let response = ui.allocate_rect(cell_clip_rect, Sense::click());

                                    let bg_color = if self.parameters.highlight_hovered_cell && response.contains_pointer() {
                                        ui.style().visuals.widgets.hovered.weak_bg_fill
                                    } else {
                                        row_bg_color
                                    };

                                    ui.painter()
                                        .with_clip_rect(cell_clip_rect)
                                        .rect_filled(cell_rect, 0.0, bg_color);

                                    // note: cannot use 'response.clicked()' here as the the cell 'swallows' the click if the contents are interactive.
                                    if response.contains_pointer() && ui.ctx().input(|i| i.pointer.primary_released()) {
                                        // FIXME this doesn't track if the click location is in the same cell, that is, this will
                                        //       be triggered if you click somewhere, then release in this cell.
                                        //       which is not the intention.

                                        actions.push(Action::CellClicked(cell_index));

                                        if let (Some(editor), Some(edit_state)) = (editor.as_mut(), edit_state.as_mut()) {
                                            self.handle_editable_cell_click(data_source, cell_index, *editor, *edit_state);
                                        }
                                    }

                                    // TODO track double clicks

                                    if SHOW_CELL_BORDERS {
                                        ui.painter()
                                            .with_clip_rect(cell_clip_rect)
                                            .rect_stroke(cell_rect, CornerRadius::ZERO, ui.style().visuals.widgets.noninteractive.bg_stroke, StrokeKind::Inside);
                                    }

                                    let cell_id = CellId::Cell(cell_index);

                                    let mut cell_ui = ui.new_child(UiBuilder::new()
                                        .id_salt(cell_id)
                                        .ui_stack_info(UiStackInfo::new(UiKind::TableCell))
                                        .max_rect(cell_inner_rect));
                                    cell_ui.set_clip_rect(cell_inner_clip_rect);
                                    cell_ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);

                                    //cell_ui.label(format!("{:?}", cell_ui.id()));

                                    let mut handled = false;

                                    editor.as_mut().zip(edit_state.as_mut()).map(|(editor, edit_state)| {
                                        match &mut edit_state.state {
                                            Some(CellEditState::Editing(editing_cell_index, item_state, value)) if cell_index.eq(editing_cell_index) => {

                                                let mut editor_frame: Frame = EDITOR_FRAME;
                                                editor_frame.fill = row_bg_color;
                                                editor_frame.stroke = Stroke::new(1.0, ui.style().visuals.window_stroke.color);
                                                editor_frame.inner_margin = Margin::same(1);

                                                egui::Window::new("")
                                                    .title_bar(false)
                                                    .id(cell_ui.id().with("cell_editor"))
                                                    .frame(editor_frame)
                                                    .constrain_to(cells_clip_rect)
                                                    .fixed_pos(cell_rect.min.sub(Vec2::splat(2.0)))
                                                    .auto_sized()
                                                    .default_rect(cell_rect)
                                                    .min_size(cell_rect.size())
                                                    .max_width(cell_rect.width())
                                                    .show(&ctx, |ui|{
                                                        //ui.set_min_width(cell_rect.width());
                                                        ui.set_min_size(cell_rect.size());
                                                        editor.render_cell_editor(ui, &cell_index, item_state, value, data_source);
                                                    })
                                                    .map(|window|
                                                    {
                                                        let window_response = window.response;

                                                        // cannot use `resp.contains_pointer()` here, since it doesn't take into account popups generated by clicking on a drop-down which appear
                                                        // above the window and may appear outside of it. (e.g. small cell and a larger drop-down)
                                                        let window_contains_pointer = window_response.contains_pointer();
                                                        // cannot use `resp.clicked_elsewhere()` here, since it doesn't it doesn't take into account a DRAG started or stopped outside the reponse area.
                                                        // (e.g. try creating a text editor outside of table and selecting text in it while an editor is visible), this will be false.
                                                        let window_clicked_elsewhere = window_response.clicked_elsewhere();

                                                        let cells_clip_rect_contains_pointer = cells_clip_rect.contains(pointer_interact_pos);

                                                        trace!("clicked_elsewhere: {}, response_rect_contains_pointer: {}, any_down: {}, contains_pointer: {}, viewport_changed: {}", window_clicked_elsewhere, cells_clip_rect_contains_pointer, any_down, window_contains_pointer, viewport_changed);

                                                        // given the above issues and scenarious, we use `any_down` (see above) and since the editor window is constrained to the cells_clip_rect can see if the pointer
                                                        // is still within the table area too.

                                                        let apply_edit = if any_down && !cells_clip_rect_contains_pointer {
                                                            trace!("applying edit due a button down and cells_clip_rect does not contain pointer");
                                                            true
                                                        } else if viewport_changed {
                                                            trace!("applying edit due to viewport change");
                                                            true
                                                        } else {
                                                            false
                                                        };

                                                        if apply_edit {
                                                            Self::apply_edit(data_source, cell_index, *editor, edit_state);
                                                        }
                                                    });

                                                handled = true;
                                            }
                                            _ => {}
                                        }
                                    });

                                    if !handled {
                                        renderer.render_cell(&mut cell_ui, cell_index, data_source);
                                    }
                                }
                                accumulated_row_heights += outer_row_height + 1.0;
                            }
                        });

                        let line_stroke = ui.style().visuals.window_stroke;
                        ui.painter()
                            .with_clip_rect(inner_max_rect)
                            .hline(table_max_rect.min.x..=table_max_rect.min.x + table_width, table_max_rect.min.y + outer_cell_size.y, line_stroke);

                        ui.painter()
                            .with_clip_rect(inner_max_rect)
                            .vline(table_max_rect.min.x + outer_cell_size.x, table_max_rect.min.y..=table_max_rect.min.y + table_height, line_stroke);

                        ui.response()
                    });
            });
        });

        if clear_drag_state {
            temp_state.drag_state = None;
        }

        if request_row_selection_changed_action {
            actions.push(Action::RowSelectionChanged {
                selection: temp_state.row_selections.clone(),
            });
        }

        let repaint = match drag_action.take() {
            None => false,
            Some(DragAction::SetWidth(index, new_width)) => {
                state.column_widths[index] = new_width;
                true
            }
            Some(DragAction::SetHeight(index, new_height)) => {
                state.row_heights[index] = new_height;
                true
            }
        };

        if repaint {
            ui.ctx().request_repaint();
        }

        DeferredTablePersistentState::store(ui.ctx(), persistent_state_id, state);
        DeferredTableTempState::store(ui.ctx(), temp_state_id, temp_state);

        (ui.response(), actions)
    }

    fn pick_row_bg_color(
        opaque_faint_bg_color: Color32,
        opaque_faint_selected_bg_color: Color32,
        ui: &mut Ui,
        row_counter: usize,
        row_was_selected: bool,
    ) -> Color32 {
        if row_was_selected {
            striped_row_color(row_counter, opaque_faint_selected_bg_color)
                .unwrap_or(ui.style().visuals.selection.bg_fill)
        } else {
            striped_row_color(row_counter, opaque_faint_bg_color)
                .unwrap_or(ui.style().visuals.widgets.noninteractive.weak_bg_fill)
        }
    }

    fn paint_resize_handle(
        ui: &mut Ui,
        points: [Pos2; 2],
        state: DragHandleState,
        cell_painter: &Painter,
        cell_kind: CellKind,
    ) {
        let stroke = match state {
            DragHandleState::Disabled => ui.visuals().widgets.noninteractive.bg_stroke,
            DragHandleState::Inactive => ui.visuals().widgets.open.bg_stroke,
            DragHandleState::Hovered => ui.style().visuals.widgets.hovered.bg_stroke,
            DragHandleState::Dragged => ui.style().visuals.widgets.active.bg_stroke,
        };

        cell_painter.line_segment(points, stroke);

        match state {
            DragHandleState::Disabled => {
                ui.ctx().set_cursor_icon(egui::CursorIcon::NotAllowed);
            }
            DragHandleState::Inactive => {}
            DragHandleState::Dragged | DragHandleState::Hovered => match cell_kind {
                CellKind::ColumnHeader => {
                    ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeColumn);
                }
                CellKind::RowHeader => {
                    ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeRow);
                }
                _ => unreachable!(),
            },
        }
    }

    fn build_cell_kind(grid_row_index: usize, grid_column_index: usize) -> CellKind {
        if grid_row_index == 0 && grid_column_index == 0 {
            CellKind::Corner
        } else if grid_row_index == 0 {
            CellKind::ColumnHeader
        } else if grid_column_index == 0 {
            CellKind::RowHeader
        } else {
            CellKind::Value
        }
    }

    fn build_row_kind(grid_row_index: usize) -> RowKind {
        if grid_row_index == 0 {
            RowKind::HeaderRow
        } else {
            RowKind::ValuesRow
        }
    }

    fn map_index(count: usize, row_ordering: &[usize], visible_row_index: usize) -> usize {
        let mut mapped_row_index = *row_ordering
            .get(visible_row_index)
            .unwrap_or(&visible_row_index);
        if mapped_row_index >= count {
            // handle out-of-range mapping values
            mapped_row_index = visible_row_index;
        }
        mapped_row_index
    }

    /// call this function from a cell action handler
    pub fn handle_editable_cell_click<IS, V>(
        &mut self,
        source: &mut DataSource,
        cell_index: CellIndex,
        editor: &mut dyn EditableTableRenderer<DataSource, ItemState = IS, Value = V>,
        edit_state: &mut EditorState<IS, V>,
    ) {
        match &edit_state.state {
            None => {
                // change selection
                edit_state.state.replace(CellEditState::Pivot(cell_index));
            }
            Some(CellEditState::Pivot(pivot_cell_index)) if *pivot_cell_index == cell_index => {
                trace!("clicked in selected cell");

                // change mode to edit
                let item_state = editor.build_item_state(cell_index, source);
                if let Some((edit, original_item)) = item_state {
                    edit_state.state.replace(CellEditState::Editing(
                        cell_index,
                        edit,
                        original_item,
                    ));
                }
            }
            Some(CellEditState::Pivot(_)) => {
                trace!("clicked in different cell");

                // change selection
                edit_state.state.replace(CellEditState::Pivot(cell_index));
            }
            Some(CellEditState::Editing(editing_cell_index, _cell_edit_state, _original_item))
                if *editing_cell_index == cell_index =>
            {
                trace!("clicked in cell while editing");

                // nothing to do
            }
            Some(CellEditState::Editing(_editing_cell_index, _cell_edit_state, _original_item)) => {
                trace!("clicked in a different cell while editing");

                Self::apply_edit(source, cell_index, editor, edit_state);
            }
        }
    }

    fn apply_edit<IS, V>(
        source: &mut DataSource,
        cell_index: CellIndex,
        editor: &mut dyn EditableTableRenderer<DataSource, Value = V, ItemState = IS>,
        edit_state: &mut EditorState<IS, V>,
    ) {
        trace!("applying edit");
        // apply edited value
        let Some(CellEditState::Editing(index, state, original_item)) = edit_state.state.take()
        else {
            unreachable!();
        };
        editor.on_edit_complete(index, state, original_item, source);

        // change selection
        edit_state.state.replace(CellEditState::Pivot(cell_index));
    }
}

fn striped_row_color(row: usize, striped_color: Color32) -> Option<Color32> {
    if row % 2 == 1 {
        Some(striped_color)
    } else {
        None
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum DragHandleState {
    Disabled,
    Inactive,
    Hovered,
    Dragged,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum CellKind {
    Corner,
    ColumnHeader,
    RowHeader,
    Value,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum RowKind {
    HeaderRow,
    ValuesRow,
}

/// State that could be stored between application restarts
#[derive(Default, Clone)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
struct DeferredTablePersistentState {
    // FUTURE We *could* add row/column ordering/filtering here too
    column_widths: Vec<f32>,
    row_heights: Vec<f32>,
}

impl DeferredTablePersistentState {
    pub fn load_or_default(ctx: &Context, id: Id) -> Self {
        ctx.data_mut(|d| {
            d.get_persisted::<DeferredTablePersistentState>(id)
                .unwrap_or(DeferredTablePersistentState::default())
        })
    }

    pub fn store(ctx: &Context, id: Id, instance: Self) {
        ctx.data_mut(|d| d.insert_persisted(id, instance));
    }
}

/// State that should not be persisted between application restarts
#[derive(Default, Clone)]
struct DeferredTableTempState {
    /// holds the index of the top-left cell
    cell_origin: CellIndex,

    drag_state: Option<DragState>,
    last_viewport_rect: Option<Rect>,

    // the collection here needs to a have a fast lookup, slow insertion/removal is fine.
    // this is because we render frames often and insert/remove infrequently.
    row_selections: BTreeSet<usize>,
    /// holds the dimensions used on the last render.
    dimensions: Option<TableDimensions>,
}

#[derive(Clone, Copy)]
struct DragState {
    index: usize,
    start_pos: Pos2,
    cell_kind: CellKind,
    initial_size: f32,
}

impl DeferredTableTempState {
    pub fn load_or_default(ctx: &Context, id: Id) -> Self {
        ctx.data_mut(|d| {
            d.get_temp::<DeferredTableTempState>(id)
                .unwrap_or(DeferredTableTempState::default())
        })
    }

    pub fn store(ctx: &Context, id: Id, instance: Self) {
        ctx.data_mut(|d| d.insert_temp(id, instance));
    }
}