-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathdisplay_provider.dart
More file actions
367 lines (334 loc) · 11 KB
/
display_provider.dart
File metadata and controls
367 lines (334 loc) · 11 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
// Copyright 2020 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.
import 'dart:async';
import 'package:devtools_app_shared/ui.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart' hide Stack;
import 'package:flutter/services.dart';
import 'package:vm_service/vm_service.dart';
import '../../diagnostics/dap_object_node.dart';
import '../../diagnostics/dart_object_node.dart';
import '../../framework/routing.dart';
import '../../framework/screen.dart';
import '../../globals.dart';
import '../../primitives/utils.dart';
import '../../ui/colors.dart';
import 'description.dart';
/// The display provider for variables fetched via the VM service protocol.
class DisplayProvider extends StatefulWidget {
const DisplayProvider({
super.key,
required this.variable,
required this.onTap,
this.onCopy,
});
final DartObjectNode variable;
final VoidCallback onTap;
final void Function(DartObjectNode)? onCopy;
@override
State<DisplayProvider> createState() => _DisplayProviderState();
}
class _DisplayProviderState extends State<DisplayProvider> {
bool isHovered = false;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
if (widget.variable.text != null) {
return InteractivityWrapper(
onTap: widget.onTap,
menuButtons: _getMenuButtons(context),
child: DevToolsTooltip(
message: widget.variable.text,
child: Text.rich(
maxLines: 1,
softWrap: false,
overflow: TextOverflow.ellipsis,
TextSpan(
children: textSpansFromAnsi(
widget.variable.text!,
theme.subtleFixedFontStyle,
),
),
),
),
);
}
final diagnostic = widget.variable.ref?.diagnostic;
if (diagnostic != null) {
return DiagnosticsNodeDescription(
diagnostic,
multiline: true,
style: theme.fixedFontStyle,
);
}
// Only enable hover behaviour when copy behaviour is provided.
final isHoverEnabled = widget.onCopy != null;
final hasName = widget.variable.name?.isNotEmpty ?? false;
// The tooltip can be hovered over in order to see the original text.
final originalDisplayValue = widget.variable.displayValue.toString();
// Only 1 line of text is permitted in the tree, since we only get 1 row.
// So replace all newlines with \\n so that the user can still see that
// there are new lines in the value.
final displayValue = originalDisplayValue.replaceAll('\n', '\\n');
final contents = InteractivityWrapper(
onTap: widget.onTap,
menuButtons: _getMenuButtons(context),
child: DevToolsTooltip(
message: originalDisplayValue,
child: Container(
color: isHovered ? Theme.of(context).highlightColor : null,
child: Row(
children: [
Expanded(
child: Text.rich(
maxLines: 1,
softWrap: false,
overflow: TextOverflow.ellipsis,
TextSpan(
text: hasName ? widget.variable.name : null,
style: widget.variable.artificialName
? theme.subtleFixedFontStyle
: theme.fixedFontStyle.apply(
color: theme.colorScheme.controlFlowSyntaxColor,
),
children: [
if (hasName)
TextSpan(text: ': ', style: theme.fixedFontStyle),
TextSpan(
text: displayValue,
style: widget.variable.artificialValue
? theme.subtleFixedFontStyle
: _variableDisplayStyle(theme, widget.variable),
),
],
),
),
),
if (isHovered && widget.onCopy != null)
DevToolsButton(
icon: Icons.copy_outlined,
outlined: false,
onPressed: () => widget.onCopy!.call(widget.variable),
),
],
),
),
),
);
if (isHoverEnabled) {
return SelectionContainer.disabled(
child: MouseRegion(
onEnter: (_) {
setState(() {
isHovered = true;
});
},
onExit: (event) {
setState(() {
isHovered = false;
});
},
child: contents,
),
);
}
return contents;
}
List<ContextMenuButtonItem> _getMenuButtons(BuildContext context) {
return [
if (widget.variable.isRerootable)
ContextMenuButtonItem(
onPressed: () {
ContextMenuController.removeAny();
final ref = widget.variable.ref;
unawaited(
serviceConnection.consoleService.appendBrowsableInstance(
instanceRef: widget.variable.value as InstanceRef?,
isolateRef: ref?.isolateRef,
heapSelection: ref?.heapSelection,
),
);
},
label: 'Reroot',
),
if (serviceConnection.inspectorService != null && widget.variable.isRoot)
ContextMenuButtonItem(
onPressed: () {
ContextMenuController.removeAny();
_handleInspect(context);
},
label: 'Inspect',
),
];
}
void _handleInspect(BuildContext context) async {
final router = DevToolsRouterDelegate.of(context);
final inspectorService = serviceConnection.inspectorService;
if (await widget.variable.inspectWidget()) {
router.navigateIfNotCurrent(ScreenMetaData.inspector.id);
} else {
if (inspectorService!.isDisposed) return;
final isInspectable = await widget.variable.isInspectable;
if (inspectorService.isDisposed) return;
if (isInspectable) {
notificationService.push(
'Widget is already the current inspector selection.',
);
} else {
notificationService.push(
'Only Elements and RenderObjects can currently be inspected',
);
}
}
}
TextStyle _variableDisplayStyle(ThemeData theme, DartObjectNode variable) {
final style = theme.subtleFixedFontStyle;
String? kind = variable.ref?.instanceRef?.kind;
// Handle nodes with primative values.
if (kind == null) {
final value = variable.ref?.value;
if (value != null) {
switch (value.runtimeType) {
case const (String):
kind = InstanceKind.kString;
break;
case const (num):
kind = InstanceKind.kInt;
break;
case const (bool):
kind = InstanceKind.kBool;
break;
}
}
kind ??= InstanceKind.kNull;
}
switch (kind) {
case InstanceKind.kString:
return style.apply(color: theme.colorScheme.stringSyntaxColor);
case InstanceKind.kInt:
case InstanceKind.kDouble:
return style.apply(color: theme.colorScheme.numericConstantSyntaxColor);
case InstanceKind.kBool:
case InstanceKind.kNull:
return style.apply(color: theme.colorScheme.modifierSyntaxColor);
default:
return style;
}
}
}
/// The display provider for variables fetched via the Debug Adapter Protocol.
class DapDisplayProvider extends StatelessWidget {
const DapDisplayProvider({
super.key,
required this.node,
required this.onTap,
});
final DapObjectNode node;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final variable = node.variable;
final name = variable.name;
final value = variable.value;
// TODO(https://github.com/flutter/devtools/issues/6056): Wrap in
// interactivity wrapper to provide inspect and re-root functionality. Add
// tooltip on hover to provide type information.
return DevToolsTooltip(
message: value,
child: Text.rich(
maxLines: 1,
softWrap: false,
overflow: TextOverflow.ellipsis,
TextSpan(
text: name,
style: theme.fixedFontStyle.apply(
color: theme.colorScheme.controlFlowSyntaxColor,
),
children: [
TextSpan(text: ': ', style: theme.fixedFontStyle),
// TODO(https://github.com/flutter/devtools/issues/6056): Change text
// style based on variable type.
TextSpan(
text: value.replaceAll('\n', '\\n'),
style: theme.subtleFixedFontStyle,
),
],
),
),
);
}
}
/// A wrapper that allows the user to interact with the variable.
///
/// Responds to primary and secondary click events.
class InteractivityWrapper extends StatefulWidget {
const InteractivityWrapper({
super.key,
required this.child,
this.menuButtons,
this.onTap,
});
/// Button items shown in a context menu on secondary click.
///
/// If none are provided, then no action will happen on secondary click.
final List<ContextMenuButtonItem>? menuButtons;
/// Optional callback when tapped.
final void Function()? onTap;
/// The child widget that will be listened to for gestures.
final Widget child;
@override
State<InteractivityWrapper> createState() => _InteractivityWrapperState();
}
class _InteractivityWrapperState extends State<InteractivityWrapper> {
final _contextMenuController = ContextMenuController();
void _onTap() {
ContextMenuController.removeAny();
final onTap = widget.onTap;
if (onTap != null) {
onTap();
}
}
void _onSecondaryTapUp(TapUpDetails details) {
_maybeShowMenu(details.globalPosition);
}
void _maybeShowMenu(Offset offset) {
// TODO(https://github.com/flutter/devtools/issues/6018): Once
// https://github.com/flutter/flutter/issues/129692 is fixed, disable the
// browser's native context menu on secondary-click, and enable this menu.
if (kIsWeb && BrowserContextMenu.enabled) {
return;
}
if (_contextMenuController.isShown) {
return;
}
_contextMenuController.show(
context: context,
contextMenuBuilder: (context) {
return AdaptiveTextSelectionToolbar.buttonItems(
anchors: TextSelectionToolbarAnchors(primaryAnchor: offset),
buttonItems: widget.menuButtons,
);
},
);
}
@override
void dispose() {
_contextMenuController.remove();
super.dispose();
}
@override
Widget build(BuildContext context) {
final contextMenuOnSecondaryTapEnabled =
widget.menuButtons != null && widget.menuButtons!.isNotEmpty;
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: _onTap,
onSecondaryTapUp: contextMenuOnSecondaryTapEnabled
? _onSecondaryTapUp
: null,
child: widget.child,
);
}
}