forked from flutter/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_controller.dart
More file actions
441 lines (374 loc) · 13 KB
/
table_controller.dart
File metadata and controls
441 lines (374 loc) · 13 KB
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
// Copyright 2025 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
/// @docImport 'table.dart';
library;
import 'dart:collection';
import 'dart:math';
import 'package:collection/collection.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import '../primitives/trees.dart';
import '../primitives/utils.dart';
import 'column_widths.dart';
import 'table_data.dart';
/// Represents the various pinning modes for [FlatTable]:
///
/// - [FlatTablePinBehavior.none] disables item pinning
/// - [FlatTablePinBehavior.pinOriginalToTop] moves the original item from
/// the list of unpinned items to the list of pinned items.
/// - [FlatTablePinBehavior.pinCopyToTop] creates a copy of the original item
/// and inserts it into the list of pinned items, leaving the original item
/// in the list of unpinned items.
enum FlatTablePinBehavior { none, pinOriginalToTop, pinCopyToTop }
class FlatTableController<T> extends TableControllerBase<T> {
FlatTableController({
required super.columns,
required super.defaultSortColumn,
required super.defaultSortDirection,
super.secondarySortColumn,
super.columnGroups,
super.includeColumnGroupHeaders,
this.pinBehavior = FlatTablePinBehavior.none,
this.sizeColumnsToFit = true,
this.sortOriginalData = false,
this.onDataSorted,
});
/// Determines how elements that request to be pinned are displayed.
///
/// Defaults to [FlatTablePinBehavior.none], which disables pinning.
FlatTablePinBehavior pinBehavior;
/// Whether the columns for this table should be sized so that the entire
/// table fits in view (e.g. so that there is no horizontal scrolling).
final bool sizeColumnsToFit;
// TODO(kenz): should we enable this behavior by default? Does it ever matter
// to preserve the order of the original data passed to a flat table?
/// Whether the table controller should sort the original data list instead of
/// creating a copy.
final bool sortOriginalData;
/// Callback that will be called after each table sort operation.
final VoidCallback? onDataSorted;
/// The unmodified, original data for the active data set `_tableData.value`.
///
/// This is reset each time [setData] is called, when [sortOriginalData] is
/// false.
late UnmodifiableListView<T> _unmodifiableOriginalData;
/// The modifiable, original data for the active data set `_tableData.value`.
///
/// This is reset each time [setData] is called, when [sortOriginalData] is
/// true.
late List<T> _modifiableOriginalData;
@override
void setData(List<T> data, String key) {
if (sortOriginalData) {
_modifiableOriginalData = data;
} else {
_unmodifiableOriginalData = UnmodifiableListView(List<T>.of(data));
}
// Look up the UI state for [key], and sort accordingly.
final uiState = _tableUiStateForKey(key);
sortDataAndNotify(
columns[uiState.sortColumnIndex],
uiState.sortDirection,
secondarySortColumn: secondarySortColumn,
dataKey: key,
);
}
@override
void sortDataAndNotify(
ColumnData<T> column,
SortDirection direction, {
ColumnData<T>? secondarySortColumn,
String? dataKey,
}) {
List<T> data;
if (sortOriginalData) {
data = _modifiableOriginalData;
} else {
// TODO(kenz): copying the list for every sort could cause performance
// issues. We should only create a copy when sort is being called from
// [setData]. At all other times, the data has not been modified so there
// is no need to make a copy.
data = List<T>.of(_unmodifiableOriginalData);
}
pinnedData = <T>[];
data.sort(
(T a, T b) => _compareData<T>(
a,
b,
column,
direction,
secondarySortColumn: secondarySortColumn,
),
);
if (pinBehavior != FlatTablePinBehavior.none) {
// Collect the list of pinned entries. We don't need to sort again since
// we've already sorted the original data.
final dataCopy = <T>[];
for (final entry in data) {
final pinnableEntry = entry as PinnableListEntry;
if (pinnableEntry.pinToTop) {
pinnedData.add(entry);
}
if (!pinnableEntry.pinToTop ||
pinBehavior == FlatTablePinBehavior.pinCopyToTop) {
dataCopy.add(entry);
}
}
data = dataCopy;
}
if (!sizeColumnsToFit) {
columnWidths = computeColumnWidthsSizeToContent();
}
_tableData.value = TableData<T>(
data: data,
key: dataKey ?? _tableData.value.key,
);
setTableUiState(sortColumn: column, sortDirection: direction);
onDataSorted?.call();
}
}
class TreeTableController<T extends TreeNode<T>>
extends TableControllerBase<T> {
TreeTableController({
required super.columns,
required super.defaultSortColumn,
required super.defaultSortDirection,
super.secondarySortColumn,
super.columnGroups,
required this.treeColumn,
this.autoExpandRoots = false,
}) : assert(columns.contains(treeColumn)),
assert(columns.contains(defaultSortColumn));
/// The column of the table to treat as expandable.
final TreeColumnData<T> treeColumn;
final bool autoExpandRoots;
late List<T> dataRoots;
late int maxTableDepth;
@override
void setData(List<T> data, String key) {
dataRoots = data;
for (final root in dataRoots) {
if (autoExpandRoots && !root.isExpanded) {
root.expand();
}
}
// TODO(kenz): instead of using the maximum tree depth, consider using the
// maximum depth of expanded nodes.
maxTableDepth = dataRoots
.map((root) => root.depth)
.fold(
0,
(currentMaxDepth, nextDepth) => max(currentMaxDepth, nextDepth),
);
// Look up the UI state for [key], and sort accordingly.
final uiState = _tableUiStateForKey(key);
sortDataAndNotify(
columns[uiState.sortColumnIndex],
uiState.sortDirection,
secondarySortColumn: secondarySortColumn,
dataKey: key,
);
}
@override
void sortDataAndNotify(
ColumnData<T> column,
SortDirection direction, {
ColumnData<T>? secondarySortColumn,
String? dataKey,
}) {
pinnedData = <T>[];
int sortFunction(T a, T b) => _compareData<T>(
a,
b,
column,
direction,
secondarySortColumn: secondarySortColumn,
);
void sort(T dataObject) {
dataObject.children
..sort(sortFunction)
..forEach(sort);
}
dataRoots
..sort(sortFunction)
..forEach(sort);
setDataAndNotify(dataKey: dataKey);
setTableUiState(sortColumn: column, sortDirection: direction);
}
void setDataAndNotify({String? dataKey}) {
final dataFlatList = buildFlatList(dataRoots);
columnWidths = computeColumnWidths(maxTableDepth);
_tableData.value = TableData<T>(
data: dataFlatList,
key: dataKey ?? _tableData.value.key,
);
}
}
abstract class TableControllerBase<T> extends DisposableController {
TableControllerBase({
required this.columns,
required this.columnGroups,
required this.defaultSortColumn,
required this.defaultSortDirection,
this.secondarySortColumn,
this.includeColumnGroupHeaders = true,
});
final List<ColumnData<T>> columns;
final List<ColumnGroup>? columnGroups;
List<double>? columnWidths;
/// Determines if the headers for column groups should be rendered.
///
/// If set to false and `columnGroups` is non-null and non-empty, only
/// dividing lines will be drawn for each column group boundary.
final bool includeColumnGroupHeaders;
/// The default sort column for tables using this TableController.
///
/// The currently active sort column will be stored as part of the
/// [TableUiState] for the current data.
final ColumnData<T> defaultSortColumn;
/// The default [SortDirection] for tables using this TableController.
///
/// The currently active [SortDirection] will be stored as part of the
/// [TableUiState] for the current data.
final SortDirection defaultSortDirection;
/// The column to be used by the table sorting algorithm to break a tie for
/// two data rows that are "the same" when sorted by the primary sort column.
final ColumnData<T>? secondarySortColumn;
ScrollController? verticalScrollController;
void initScrollController([double initialScrollOffset = 0.0]) {
verticalScrollController = ScrollController(
initialScrollOffset: initialScrollOffset,
);
}
void storeScrollPosition() {
final scrollController = verticalScrollController;
if (scrollController != null && scrollController.hasClients) {
setTableUiState(scrollOffset: scrollController.offset);
}
}
/// The key for the current table data.
///
/// The value assigned to [TableData.key] will only be used if
/// `persistUiStates` has been set to true. Otherwise, all data sets will be
/// assigned to and looked up from the key [TableData.defaultDataKey].
String get _currentDataKey => _tableData.value.key;
ValueListenable<TableData<T>> get tableData => _tableData;
final _tableData = ValueNotifier<TableData<T>>(TableData<T>.empty());
/// The pinned data for the active data set `_tableData.value`.
///
/// This value is reset each time [sortDataAndNotify] is called.
late List<T> pinnedData;
/// Returns the [TableUiState] for the current data `_tableData.value`.
TableUiState get tableUiState => _tableUiStateForKey(_currentDataKey);
/// This method should be overridden by all subclasses.
void setData(List<T> data, String key);
void sortDataAndNotify(
ColumnData<T> column,
SortDirection direction, {
ColumnData<T>? secondarySortColumn,
});
TableUiState _tableUiStateForKey(String key) {
var state = TableUiStateStore.lookup(key);
if (state == null) {
TableUiStateStore.add(
key,
TableUiState(
sortColumnIndex: columns.indexOf(defaultSortColumn),
sortDirection: defaultSortDirection,
// Ignore this lint to make it clear what the default values are.
// ignore: avoid_redundant_argument_values
scrollOffset: 0.0,
),
);
state = TableUiStateStore.lookup(key)!;
}
return state;
}
void setTableUiState({
ColumnData<T>? sortColumn,
SortDirection? sortDirection,
double? scrollOffset,
}) {
final uiState = tableUiState;
if (sortColumn != null) {
uiState.sortColumnIndex = columns.indexOf(sortColumn);
}
if (sortDirection != null) {
uiState.sortDirection = sortDirection;
}
if (scrollOffset != null) {
uiState.scrollOffset = scrollOffset;
}
}
@override
void dispose() {
verticalScrollController?.dispose();
verticalScrollController = null;
_tableData.dispose();
super.dispose();
}
}
class TableData<T> {
const TableData({required this.data, String? key})
: key = key ?? defaultDataKey;
factory TableData.empty() => TableData<T>(data: const []);
static const defaultDataKey = 'defaultDataKey';
final List<T> data;
final String key;
}
class TableUiState {
TableUiState({
required this.sortColumnIndex,
required this.sortDirection,
this.scrollOffset = 0.0,
});
SortDirection sortDirection;
int sortColumnIndex;
double scrollOffset;
@override
String toString() {
return '_TableUiState($sortColumnIndex - $sortDirection - $scrollOffset)';
}
}
// Ignoring the 'avoid_classes_with_only_static_members' lint because the static
// members here allow us to add asserts that guarantee unique keys for tables
// across DevTools.
// ignore: avoid_classes_with_only_static_members
/// Stores the [TableUiState] for each table, keyed on a unique [String].
///
/// This store will remain alive for the entire life of the DevTools instance.
/// This allows us to cache the [TableUiState] for tables without having to
/// keep table [State] classes or table controller classes alive.
@visibleForTesting
abstract class TableUiStateStore<T> {
static final _tableUiStateStore = <String, TableUiState>{};
static void add(String key, TableUiState value) {
assert(
!_tableUiStateStore.containsKey(key),
'_TableUiState already exists for key: $key',
);
_tableUiStateStore[key] = value;
}
static TableUiState? lookup(String key) {
return _tableUiStateStore[key];
}
@visibleForTesting
static void clear() => _tableUiStateStore.clear();
}
int _compareFactor(SortDirection direction) =>
direction == SortDirection.ascending ? 1 : -1;
int _compareData<T>(
T a,
T b,
ColumnData<T> column,
SortDirection direction, {
ColumnData<T>? secondarySortColumn,
}) {
final compare = column.compare(a, b) * _compareFactor(direction);
if (compare != 0 || secondarySortColumn == null) return compare;
return secondarySortColumn.compare(a, b) * _compareFactor(direction);
}
/// Callback for when a specific item in a table is selected.
typedef ItemSelectedCallback<T> = void Function(T item);