From 97f25a40b25b81c1fd0901be4508b3693e5805bc Mon Sep 17 00:00:00 2001 From: Kenzie Davisson <43759233+kenzieschmoll@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:40:11 -0700 Subject: [PATCH] Deprecate the `DevToolsExtension.requiresRunningApplication` field (#7611) --- .../release_notes/NEXT_RELEASE_NOTES.md | 3 + packages/devtools_extensions/CHANGELOG.md | 1 + .../test/simulated_environment_test.dart | 5 +- .../_simulated_devtools_environment.dart | 11 ---- .../lib/src/template/devtools_extension.dart | 56 +++++-------------- .../lib/src/template/extension_manager.dart | 26 ++++----- 6 files changed, 32 insertions(+), 70 deletions(-) diff --git a/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md b/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md index 6a6b6acd3a3..ad0900f618b 100644 --- a/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md +++ b/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md @@ -59,6 +59,9 @@ session. - [#7598](https://github.com/flutter/devtools/pull/7598) ## DevTools Extension updates +* Deprecate the `DevToolsExtension.requiresRunningApplication` field in favor of the +new optional `requiresConnection` field that can be added to an extension's `config.yaml` +file. - [#7611](https://github.com/flutter/devtools/pull/7611), [#7602](https://github.com/flutter/devtools/pull/7602) * Detect extensions for all types of run targets in a package. - [#7533](https://github.com/flutter/devtools/pull/7533), [#7535](https://github.com/flutter/devtools/pull/7535) diff --git a/packages/devtools_extensions/CHANGELOG.md b/packages/devtools_extensions/CHANGELOG.md index cf585ff4138..2217f158757 100644 --- a/packages/devtools_extensions/CHANGELOG.md +++ b/packages/devtools_extensions/CHANGELOG.md @@ -1,4 +1,5 @@ ## 0.2.0-dev.0 +* Deprecate the `DevToolsExtension.requiresRunningApplication` field. * Update `extension_config_spec.md` to include an optional field `requiresConnection`. * Bump `devtools_shared` dependency to `^10.0.0`. * Fix file locations in the `dart_foo` extension example. diff --git a/packages/devtools_extensions/integration_test/test/simulated_environment_test.dart b/packages/devtools_extensions/integration_test/test/simulated_environment_test.dart index c48b67c0134..61317bf2e9c 100644 --- a/packages/devtools_extensions/integration_test/test/simulated_environment_test.dart +++ b/packages/devtools_extensions/integration_test/test/simulated_environment_test.dart @@ -22,10 +22,7 @@ void main() { testWidgets('end to end simulated environment', (tester) async { runApp( - const DevToolsExtension( - requiresRunningApplication: false, - child: TestDevToolsExtension(), - ), + const DevToolsExtension(child: TestDevToolsExtension()), ); await tester.pump(safePumpDuration); expect(find.byType(DevToolsExtension), findsOneWidget); diff --git a/packages/devtools_extensions/lib/src/template/_simulated_devtools_environment/_simulated_devtools_environment.dart b/packages/devtools_extensions/lib/src/template/_simulated_devtools_environment/_simulated_devtools_environment.dart index 40873235859..b1b75d95b3c 100644 --- a/packages/devtools_extensions/lib/src/template/_simulated_devtools_environment/_simulated_devtools_environment.dart +++ b/packages/devtools_extensions/lib/src/template/_simulated_devtools_environment/_simulated_devtools_environment.dart @@ -35,14 +35,11 @@ class SimulatedDevToolsWrapper extends StatefulWidget { const SimulatedDevToolsWrapper({ super.key, required this.child, - required this.requiresRunningApplication, required this.onDtdConnectionChange, }); final Widget child; - final bool requiresRunningApplication; - final Future Function(String?) onDtdConnectionChange; @override @@ -156,8 +153,6 @@ class SimulatedDevToolsWrapperState extends State ), _SimulatedApi( simController: simController, - requiresRunningApplication: - widget.requiresRunningApplication, connectedToApplication: vmServiceConnected, ), const PaddedDivider(), @@ -183,21 +178,15 @@ class SimulatedDevToolsWrapperState extends State class _SimulatedApi extends StatelessWidget { const _SimulatedApi({ required this.simController, - required this.requiresRunningApplication, required this.connectedToApplication, }); final SimulatedDevToolsController simController; - final bool requiresRunningApplication; - final bool connectedToApplication; @override Widget build(BuildContext context) { - if (requiresRunningApplication && !connectedToApplication) { - return const SizedBox.shrink(); - } return Padding( padding: const EdgeInsets.symmetric(vertical: denseSpacing), child: Column( diff --git a/packages/devtools_extensions/lib/src/template/devtools_extension.dart b/packages/devtools_extensions/lib/src/template/devtools_extension.dart index 3c31aed9e00..1d4361af5ff 100644 --- a/packages/devtools_extensions/lib/src/template/devtools_extension.dart +++ b/packages/devtools_extensions/lib/src/template/devtools_extension.dart @@ -100,9 +100,10 @@ T _accessGlobalOrThrow({required String globalName}) { /// extension should be defined by [child]. /// /// This wrapper: -/// * initializes the [extensionManager] and [serviceManager] globals. -/// * initializes the [extensionManager] with the VM service connection from -/// DevTools when[requiresRunningApplication] is true. +/// * initializes the [extensionManager], [serviceManager], and [dtdManager] +/// globals. +/// * initializes the [extensionManager] with the VM service and Dart Tooling +/// Daemon connections when available. /// * establishes a connection with DevTools for this extension to interact /// over. /// @@ -114,6 +115,10 @@ class DevToolsExtension extends StatefulWidget { super.key, required this.child, this.eventHandlers = const {}, + @Deprecated( + 'Set the requiresConnection field in the extension\'s config.yaml ' + 'file instead.', + ) this.requiresRunningApplication = true, }); @@ -126,6 +131,10 @@ class DevToolsExtension extends StatefulWidget { final Map eventHandlers; /// Whether this extension requires a running application to use. + @Deprecated( + 'Set the requiresConnection field in the extension\'s config.yaml ' + 'file instead.', + ) final bool requiresRunningApplication; @override @@ -138,11 +147,7 @@ class _DevToolsExtensionState extends State void initState() { super.initState(); _initGlobals(); - unawaited( - extensionManager._init( - connectToVmService: widget.requiresRunningApplication, - ), - ); + unawaited(extensionManager._init()); for (final handler in widget.eventHandlers.entries) { extensionManager.registerEventHandler(handler.key, handler.value); } @@ -177,10 +182,6 @@ class _DevToolsExtensionState extends State @override Widget build(BuildContext context) { - final child = _ConnectionAwareWrapper( - requiresRunningApplication: widget.requiresRunningApplication, - child: widget.child, - ); return MaterialApp( themeMode: extensionManager.darkThemeEnabled.value ? ThemeMode.dark @@ -198,38 +199,11 @@ class _DevToolsExtensionState extends State home: Scaffold( body: _useSimulatedEnvironment ? SimulatedDevToolsWrapper( - requiresRunningApplication: widget.requiresRunningApplication, onDtdConnectionChange: extensionManager._connectToDtd, - child: child, + child: widget.child, ) - : child, + : widget.child, ), ); } } - -class _ConnectionAwareWrapper extends StatelessWidget { - const _ConnectionAwareWrapper({ - required this.child, - required this.requiresRunningApplication, - }); - - final bool requiresRunningApplication; - - final Widget child; - - @override - Widget build(BuildContext context) { - return ValueListenableBuilder( - valueListenable: serviceManager.connectedState, - builder: (context, connectedState, _) { - if (requiresRunningApplication && !connectedState.connected) { - return const Center( - child: Text('Please connect an app to use this DevTools Extension'), - ); - } - return child; - }, - ); - } -} diff --git a/packages/devtools_extensions/lib/src/template/extension_manager.dart b/packages/devtools_extensions/lib/src/template/extension_manager.dart index ba98f53e485..32c020764dd 100644 --- a/packages/devtools_extensions/lib/src/template/extension_manager.dart +++ b/packages/devtools_extensions/lib/src/template/extension_manager.dart @@ -45,7 +45,7 @@ class ExtensionManager { EventListener? _handleMessageListener; // ignore: unused_element, false positive due to part files - Future _init({required bool connectToVmService}) async { + Future _init() async { window.addEventListener( 'message', _handleMessageListener = _handleMessage.toJS, @@ -62,19 +62,17 @@ class ExtensionManager { } final vmServiceUri = queryParams[_vmServiceQueryParameter]; - if (connectToVmService) { - if (vmServiceUri == null) { - // Request the vm service uri for the connected app. DevTools will - // respond with a [DevToolsPluginEventType.connectedVmService] event - // containing the currently connected app's vm service URI. - postMessageToDevTools( - DevToolsExtensionEvent( - DevToolsExtensionEventType.vmServiceConnection, - ), - ); - } else { - unawaited(_connectToVmService(vmServiceUri)); - } + if (vmServiceUri == null && !_useSimulatedEnvironment) { + // Request the vm service uri for the connected app. DevTools will + // respond with a [DevToolsPluginEventType.connectedVmService] event + // containing the currently connected app's vm service URI. + postMessageToDevTools( + DevToolsExtensionEvent( + DevToolsExtensionEventType.vmServiceConnection, + ), + ); + } else { + unawaited(_connectToVmService(vmServiceUri)); } }