Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to enable and disable an extension from the extension screen #6140

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/devtools_app/lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ class DevToolsApp extends StatefulWidget {
///
/// This manages the route generation, and marshals URL query parameters into
/// flutter route parameters.
// TODO(https://github.com/flutter/devtools/issues/1146): Introduce tests that
// navigate the full app.
class DevToolsAppState extends State<DevToolsApp> with AutoDisposeMixin {
List<Screen> get _screens => [
..._originalScreens,
Expand All @@ -100,7 +98,7 @@ class DevToolsAppState extends State<DevToolsApp> with AutoDisposeMixin {
/// [extensionService.availableExtensions] and verify tabs are added / removed
/// propertly based on the enabled state of extensions.
Iterable<Screen> get _extensionScreens =>
extensionService.availableExtensions.value.map(
extensionService.visibleExtensions.value.map(
(e) => DevToolsScreen<void>(ExtensionScreen(e)).screen,
);

Expand Down Expand Up @@ -136,6 +134,11 @@ class DevToolsAppState extends State<DevToolsApp> with AutoDisposeMixin {
_clearCachedRoutes();
});
});
addAutoDisposeListener(extensionService.visibleExtensions, () {
setState(() {
_clearCachedRoutes();
});
});
}

addAutoDisposeListener(serviceManager.isolateManager.mainIsolate, () {
Expand Down Expand Up @@ -256,6 +259,7 @@ class DevToolsAppState extends State<DevToolsApp> with AutoDisposeMixin {
listenables: [
preferences.vmDeveloperModeEnabled,
extensionService.availableExtensions,
extensionService.visibleExtensions,
],
builder: (_, __, child) {
final screens = _visibleScreens()
Expand Down
68 changes: 18 additions & 50 deletions packages/devtools_app/lib/src/extensions/extension_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import 'package:devtools_shared/devtools_extensions.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../shared/analytics/constants.dart' as gac;
import '../shared/common_widgets.dart';
import '../shared/globals.dart';
import '../shared/primitives/listenable.dart';
import '../shared/primitives/utils.dart';
import '../shared/screen.dart';
import '../shared/theme.dart';
import 'embedded/controller.dart';
import 'embedded/view.dart';
import 'extension_screen_controls.dart';

class ExtensionScreen extends Screen {
ExtensionScreen(this.extensionConfig)
: super.conditional(
// TODO(kenz): we may need to ensure this is a unique id.
id: '${extensionConfig.name}-ext',
id: '${extensionConfig.name}_ext',
title: extensionConfig.name.toSentenceCase(),
icon: extensionConfig.icon,
// TODO(kenz): support static DevTools extensions.
Expand Down Expand Up @@ -100,10 +100,22 @@ class ExtensionView extends StatelessWidget {
children: [
EmbeddedExtensionHeader(extension: extension),
Expanded(
child: KeepAliveWrapper(
child: Center(
child: EmbeddedExtensionView(controller: controller),
child: ValueListenableBuilder<ExtensionEnabledState>(
valueListenable: extensionService.enabledStateListenable(
extension.name,
),
builder: (context, activationState, _) {
if (activationState == ExtensionEnabledState.enabled) {
return KeepAliveWrapper(
child: Center(
child: EmbeddedExtensionView(controller: controller),
),
);
}
return EnableExtensionPrompt(
extension: controller.extensionConfig,
);
},
),
),
],
Expand All @@ -112,50 +124,6 @@ class ExtensionView extends StatelessWidget {
}
}

// TODO(kenz): add button to deactivate extension once activate / deactivate
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to extension_screen_controls.dart

// logic is hooked up.
class EmbeddedExtensionHeader extends StatelessWidget {
const EmbeddedExtensionHeader({super.key, required this.extension});

final DevToolsExtensionConfig extension;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final extensionName = extension.name.toLowerCase();
return AreaPaneHeader(
title: RichText(
text: TextSpan(
text: 'package:$extensionName extension',
style: theme.regularTextStyle.copyWith(fontWeight: FontWeight.bold),
children: [
TextSpan(
text: ' (v${extension.version})',
style: theme.subtleTextStyle,
),
],
),
),
includeTopBorder: false,
roundedTopBorder: false,
rightPadding: defaultSpacing,
actions: [
RichText(
text: LinkTextSpan(
link: Link(
display: 'Report an issue',
url: extension.issueTrackerLink,
gaScreenName: gac.extensionScreenId,
gaSelectedItemDescription: gac.extensionFeedback(extensionName),
),
context: context,
),
),
],
);
}
}

extension ExtensionConfigExtension on DevToolsExtensionConfig {
IconData get icon => IconData(
materialIconCodePoint,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
// Copyright 2023 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:devtools_shared/devtools_extensions.dart';
import 'package:flutter/material.dart';

import '../shared/analytics/constants.dart' as gac;
import '../shared/common_widgets.dart';
import '../shared/dialogs.dart';
import '../shared/globals.dart';
import '../shared/routing.dart';
import '../shared/theme.dart';

class EmbeddedExtensionHeader extends StatelessWidget {
const EmbeddedExtensionHeader({super.key, required this.extension});

final DevToolsExtensionConfig extension;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final extensionName = extension.displayName;
return AreaPaneHeader(
title: RichText(
text: TextSpan(
text: 'package:$extensionName extension',
style: theme.regularTextStyle.copyWith(fontWeight: FontWeight.bold),
children: [
TextSpan(
text: ' (v${extension.version})',
style: theme.subtleTextStyle,
),
],
),
),
includeTopBorder: false,
roundedTopBorder: false,
rightPadding: defaultSpacing,
actions: [
RichText(
text: LinkTextSpan(
link: Link(
display: 'Report an issue',
url: extension.issueTrackerLink,
gaScreenName: gac.extensionScreenId,
gaSelectedItemDescription: gac.extensionFeedback(extensionName),
),
context: context,
),
),
ValueListenableBuilder<ExtensionEnabledState>(
valueListenable:
extensionService.enabledStateListenable(extension.displayName),
builder: (context, activationState, _) {
if (activationState == ExtensionEnabledState.enabled) {
return Padding(
padding: const EdgeInsets.only(left: denseSpacing),
child: DisableExtensionButton(extension: extension),
);
}
return const SizedBox.shrink();
},
),
],
);
}
}

@visibleForTesting
class DisableExtensionButton extends StatelessWidget {
const DisableExtensionButton({super.key, required this.extension});

final DevToolsExtensionConfig extension;

@override
Widget build(BuildContext context) {
return DevToolsButton.iconOnly(
icon: Icons.extension_off_outlined,
outlined: false,
tooltip: 'Disable extension',
gaScreen: gac.extensionScreenId,
gaSelection: gac.extensionDisable(extension.displayName),
onPressed: () => unawaited(
showDialog(
context: context,
builder: (_) => DisableExtensionDialog(extension: extension),
),
),
);
}
}

@visibleForTesting
class DisableExtensionDialog extends StatelessWidget {
const DisableExtensionDialog({super.key, required this.extension});

final DevToolsExtensionConfig extension;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return DevToolsDialog(
title: const DialogTitleText('Disable extension?'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
RichText(
text: TextSpan(
text: 'Are you sure you want to disable the ',
style: theme.regularTextStyle,
children: [
TextSpan(
text: extension.displayName,
style: theme.fixedFontStyle,
),
const TextSpan(text: ' extension?'),
],
),
),
const SizedBox(height: denseSpacing),
RichText(
text: TextSpan(
text: 'You can always re-enable this extension later from the ',
style: theme.regularTextStyle,
children: [
TextSpan(
text: 'DevTools Extensions ',
style: theme.boldTextStyle,
),
WidgetSpan(
child: Icon(
Icons.extension_rounded,
size: defaultIconSize,
),
),
const TextSpan(text: ' menu.'),
],
),
),
],
),
actions: [
DialogTextButton(
onPressed: () {
unawaited(
extensionService.setExtensionEnabledState(
extension,
enable: false,
),
);
Navigator.of(context).pop(dialogDefaultContext);
DevToolsRouterDelegate.of(context)
.navigateHome(clearScreenParam: true);
},
child: const Text('YES, DISABLE'),
),
const DialogCancelButton(),
],
);
}
}

class EnableExtensionPrompt extends StatelessWidget {
const EnableExtensionPrompt({
super.key,
required this.extension,
});

final DevToolsExtensionConfig extension;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final extensionName = extension.displayName;
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RichText(
text: TextSpan(
text: 'The ',
style: theme.regularTextStyle,
children: [
TextSpan(
text: extension.name,
style: theme.fixedFontStyle,
),
const TextSpan(
text: ' extension has not been enabled. Do you want to enable'
' this extension?',
),
],
),
),
const SizedBox(height: defaultSpacing),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DevToolsButton(
label: 'Enable',
gaScreen: gac.extensionScreenId,
gaSelection: gac.extensionEnable(extensionName),
elevatedButton: true,
onPressed: () {
unawaited(
extensionService.setExtensionEnabledState(
extension,
enable: true,
),
);
},
),
const SizedBox(width: defaultSpacing),
DevToolsButton(
label: 'No, hide this screen',
gaScreen: gac.extensionScreenId,
gaSelection: gac.extensionDisable(extensionName),
onPressed: () {
unawaited(
extensionService.setExtensionEnabledState(
extension,
enable: false,
),
);
DevToolsRouterDelegate.of(context)
.navigateHome(clearScreenParam: true);
},
),
],
),
],
),
);
}
}
Loading