forked from flutter/devtools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhome_screen.dart
More file actions
342 lines (299 loc) · 10.1 KB
/
home_screen.dart
File metadata and controls
342 lines (299 loc) · 10.1 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
// Copyright 2019 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:devtools_app_shared/utils.dart';
import 'package:devtools_shared/devtools_shared.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../service/connected_app/connection_info.dart';
import '../shared/analytics/analytics.dart' as ga;
import '../shared/analytics/constants.dart' as gac;
import '../shared/config_specific/import_export/import_export.dart';
import '../shared/framework/routing.dart';
import '../shared/framework/screen.dart';
import '../shared/globals.dart';
import '../shared/primitives/blocking_action_mixin.dart';
import '../shared/primitives/utils.dart';
import '../shared/title.dart';
import '../shared/ui/vm_flag_widgets.dart';
import '../shared/utils/utils.dart';
import 'framework_core.dart';
class HomeScreen extends Screen {
HomeScreen({this.sampleData = const []})
: super.fromMetaData(
ScreenMetaData.home,
titleGenerator: () => devToolsTitle.value,
);
static final id = ScreenMetaData.home.id;
final List<DevToolsJsonFile> sampleData;
@override
Widget buildScreenBody(BuildContext context) {
return HomeScreenBody(sampleData: sampleData);
}
}
class HomeScreenBody extends StatefulWidget {
const HomeScreenBody({super.key, this.sampleData = const []});
final List<DevToolsJsonFile> sampleData;
@override
State<HomeScreenBody> createState() => _HomeScreenBodyState();
}
class _HomeScreenBodyState extends State<HomeScreenBody> with AutoDisposeMixin {
@override
void initState() {
super.initState();
ga.screen(gac.home);
addAutoDisposeListener(serviceConnection.serviceManager.connectedState);
}
@override
Widget build(BuildContext context) {
final connected =
serviceConnection.serviceManager.connectedState.value.connected &&
serviceConnection.serviceManager.connectedAppInitialized;
return Scrollbar(
child: ListView(
children: [
ConnectionSection(connected: connected),
if (widget.sampleData.isNotEmpty && !kReleaseMode && !connected) ...[
SampleDataDropDownButton(sampleData: widget.sampleData),
const SizedBox(height: defaultSpacing),
],
],
),
);
}
}
class ConnectionSection extends StatelessWidget {
const ConnectionSection({super.key, required this.connected});
static const _primaryMinScreenWidthForTextBeforeScaling = 480.0;
static const _secondaryMinScreenWidthForTextBeforeScaling = 600.0;
final bool connected;
@override
Widget build(BuildContext context) {
if (connected) {
return LandingScreenSection(
title: 'Connected app',
actions: [
ViewVmFlagsButton(
gaScreen: gac.home,
minScreenWidthForTextBeforeScaling:
_secondaryMinScreenWidthForTextBeforeScaling,
),
const SizedBox(width: defaultSpacing),
ConnectToNewAppButton(
gaScreen: gac.home,
minScreenWidthForTextBeforeScaling:
_primaryMinScreenWidthForTextBeforeScaling,
routerDelegate: DevToolsRouterDelegate.of(context),
onPressed:
() => Navigator.of(context, rootNavigator: true).pop('dialog'),
),
],
child: const ConnectedAppSummary(narrowView: false),
);
}
return const ConnectInput();
}
}
class LandingScreenSection extends StatelessWidget {
const LandingScreenSection({
super.key,
required this.title,
required this.child,
this.actions = const [],
});
final String title;
final Widget child;
final List<Widget> actions;
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(child: Text(title, style: textTheme.headlineMedium)),
...actions,
],
),
const PaddedDivider(),
child,
PaddedDivider.vertical(padding: 10.0),
],
);
}
}
class ConnectInput extends StatefulWidget {
const ConnectInput({super.key});
@override
State<ConnectInput> createState() => _ConnectInputState();
}
class _ConnectInputState extends State<ConnectInput> with BlockingActionMixin {
late final TextEditingController connectDialogController;
/// The key for the VM Service URI we cache in storage for the purpose of
/// speeding up the DevTools development cycle.
static const _debugVmServiceUriKey = 'debug_vmServiceUri';
@override
void initState() {
super.initState();
connectDialogController = TextEditingController();
assert(() {
_debugInitVmServiceCache();
return true;
}());
}
void _debugInitVmServiceCache() async {
// We only do this in debug mode as it speeds iteration for DevTools
// developers who tend to repeatedly restart DevTools to debug the same
// test application.
final uri = await storage.getValue(_debugVmServiceUriKey);
if (uri != null) {
setState(() {
connectDialogController.text = uri;
});
}
}
@override
void dispose() {
connectDialogController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
final connectorInput = Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
SizedBox(
height: defaultTextFieldHeight,
width: scaleByFontFactor(350.0),
child: DevToolsClearableTextField(
labelText: 'URL',
onSubmitted:
actionInProgress ? null : (str) => unawaited(_connect()),
autofocus: true,
controller: connectDialogController,
),
),
const SizedBox(width: defaultSpacing),
DevToolsButton(
onPressed: actionInProgress ? null : () => unawaited(_connect()),
elevated: true,
label: 'Connect',
),
],
),
Padding(
padding: const EdgeInsets.symmetric(vertical: densePadding),
child: Text(
'(e.g., http://127.0.0.1:12345/auth_code=... or ws://...)',
textAlign: TextAlign.start,
style: Theme.of(context).textTheme.bodySmall,
),
),
],
);
return LandingScreenSection(
title: 'Connect',
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Connect to a Running App', style: textTheme.titleMedium),
const SizedBox(height: denseRowSpacing),
Text('Enter a Dart VM Service URL', style: textTheme.bodySmall),
const SizedBox(height: denseSpacing),
connectorInput,
],
),
);
}
Future<void> _connect() async {
assert(!actionInProgress);
await blockWhileInProgress(_connectHelper);
}
Future<void> _connectHelper() async {
ga.select(gac.home, gac.HomeScreenEvents.connectToApp.name);
final uri = connectDialogController.text;
if (uri.isEmpty) {
notificationService.push('Please enter a VM Service URL.');
return;
}
assert(() {
safeUnawaited(storage.setValue(_debugVmServiceUriKey, uri));
return true;
}());
// Cache the routerDelegate and notifications providers before the async
// gap as the landing screen may not be displayed by the time the async gap
// is complete but we still want to show notifications and change the route.
// TODO(jacobr): better understand why this is the case. It is bit counter
// intuitive that we don't want to just cancel the route change or
// notification if we are already on a different screen.
final routerDelegate = DevToolsRouterDelegate.of(context);
final connected = await FrameworkCore.initVmService(
serviceUriAsString: uri,
);
if (connected) {
final connectedUri = Uri.parse(
serviceConnection.serviceManager.serviceUri!,
);
await routerDelegate.updateArgsIfChanged({'uri': '$connectedUri'});
final shortUri = connectedUri.replace(path: '');
notificationService.push('Successfully connected to $shortUri.');
} else if (normalizeVmServiceUri(uri) == null) {
notificationService.push(
'Failed to connect to the VM Service at "${connectDialogController.text}".\n'
'The link was not valid.',
);
}
}
}
class SampleDataDropDownButton extends StatefulWidget {
const SampleDataDropDownButton({super.key, this.sampleData = const []});
final List<DevToolsJsonFile> sampleData;
@override
State<SampleDataDropDownButton> createState() =>
_SampleDataDropDownButtonState();
}
class _SampleDataDropDownButtonState extends State<SampleDataDropDownButton> {
DevToolsJsonFile? value;
@override
Widget build(BuildContext context) {
return Row(
children: [
RoundedDropDownButton<DevToolsJsonFile>(
value: value,
items: [for (final data in widget.sampleData) _buildMenuItem(data)],
onChanged:
(file) => setState(() {
value = file;
}),
),
const SizedBox(width: defaultSpacing),
ElevatedButton(
onPressed:
value == null
? null
: () => Provider.of<ImportController>(
context,
listen: false,
).importData(value!),
child: const MaterialIconLabel(
label: 'Load sample data',
iconData: Icons.file_upload,
),
),
],
);
}
DropdownMenuItem<DevToolsJsonFile> _buildMenuItem(DevToolsJsonFile file) {
return DropdownMenuItem<DevToolsJsonFile>(
value: file,
child: Text(file.path),
);
}
}