Skip to content

Commit

Permalink
add widgetLocationIdMap extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob314 committed Nov 18, 2022
1 parent 3a3a0db commit 56352b6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
15 changes: 15 additions & 0 deletions packages/flutter/lib/src/widgets/service_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,22 @@ enum WidgetInspectorServiceExtensions {
/// extension is registered.
trackRebuildDirtyWidgets,

/// Name of service extension that, when called, returns the mapping of
/// widget locations to ids.
///
/// This service extension is only supported if
/// [WidgetInspectorService._widgetCreationTracked] is true.
///
/// See also:
///
/// * [trackRebuildDirtyWidgets], which toggles dispatching events that use
/// these ids to efficiently indicate the locations of widgets.
/// * [WidgetInspectorService.initServiceExtensions], where the service
/// extension is registered.
widgetLocationIdMap,

/// Name of service extension that, when called, determines whether
/// [WidgetInspectorService._trackRepaintWidgets], which determines whether
/// a callback is invoked for every [RenderObject] painted each frame.
///
/// See also:
Expand Down
61 changes: 54 additions & 7 deletions packages/flutter/lib/src/widgets/widget_inspector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,13 @@ mixin WidgetInspectorService {
},
);

_registerSignalServiceExtension(
name: WidgetInspectorServiceExtensions.widgetLocationIdMap.name,
callback: () {
return _locationIdMapToJson();
},
);

_registerBoolServiceExtension(
name: WidgetInspectorServiceExtensions.trackRepaintWidgets.name,
getter: () async => _trackRepaintWidgets,
Expand Down Expand Up @@ -2259,9 +2266,11 @@ mixin WidgetInspectorService {
bool? _widgetCreationTracked;

late Duration _frameStart;
late int _frameNumber;

void _onFrameStart(Duration timeStamp) {
_frameStart = timeStamp;
_frameNumber = PlatformDispatcher.instance.frameData.frameNumber;
SchedulerBinding.instance.addPostFrameCallback(_onFrameEnd);
}

Expand All @@ -2275,7 +2284,13 @@ mixin WidgetInspectorService {
}

void _postStatsEvent(String eventName, _ElementLocationStatsTracker stats) {
postEvent(eventName, stats.exportToJson(_frameStart));
postEvent(
eventName,
stats.exportToJson(
_frameStart,
frameNumber: _frameNumber,
),
);
}

/// All events dispatched by a [WidgetInspectorService] use this method
Expand Down Expand Up @@ -2466,7 +2481,7 @@ class _ElementLocationStatsTracker {

/// Exports the current counts and then resets the stats to prepare to track
/// the next frame of data.
Map<String, dynamic> exportToJson(Duration startTime) {
Map<String, dynamic> exportToJson(Duration startTime, {required int frameNumber}) {
final List<int> events = List<int>.filled(active.length * 2, 0);
int j = 0;
for (final _LocationCount stat in active) {
Expand All @@ -2476,6 +2491,7 @@ class _ElementLocationStatsTracker {

final Map<String, dynamic> json = <String, dynamic>{
'startTime': startTime.inMicroseconds,
'frameNumber': frameNumber,
'events': events,
};

Expand Down Expand Up @@ -3120,11 +3136,21 @@ class _InspectorOverlayLayer extends Layer {
final Rect targetRect = MatrixUtils.transformRect(
state.selected.transform, state.selected.rect,
);
final Offset target = Offset(targetRect.left, targetRect.center.dy);
const double offsetFromWidget = 9.0;
final double verticalOffset = (targetRect.height) / 2 + offsetFromWidget;

_paintDescription(canvas, state.tooltip, state.textDirection, target, verticalOffset, size, targetRect);
if (!targetRect.hasNaN) {
final Offset target = Offset(targetRect.left, targetRect.center.dy);
const double offsetFromWidget = 9.0;
final double verticalOffset = (targetRect.height) / 2 + offsetFromWidget;

_paintDescription(
canvas,
state.tooltip,
state.textDirection,
target,
verticalOffset,
size,
targetRect,
);
}

// TODO(jacobr): provide an option to perform a debug paint of just the
// selected widget.
Expand Down Expand Up @@ -3518,6 +3544,27 @@ int _toLocationId(_Location location) {
return id;
}

Map<String, dynamic> _locationIdMapToJson() {
final Map<String, Map<String, List<Object?>>> fileLocationsMap = <String, Map<String, List<Object?>>>{};
for (final MapEntry<_Location, int> entry in _locationToId.entries) {
final _Location location = entry.key;
final Map<String, List<Object?>> locations = fileLocationsMap.putIfAbsent(
location.file, () => <String, List<Object?>>{
'ids': <int>[],
'lines': <int>[],
'columns': <int>[],
'names': <String?>[],
},
);

locations['ids']!.add(entry.value);
locations['lines']!.add(location.line);
locations['columns']!.add(location.column);
locations['names']!.add(location.name);
}
return fileLocationsMap;
}

/// A delegate that configures how a hierarchy of [DiagnosticsNode]s are
/// serialized by the Flutter Inspector.
@visibleForTesting
Expand Down

0 comments on commit 56352b6

Please sign in to comment.