Skip to content

Commit

Permalink
[Inspector V2] Fixes an issue where the V2 Inspector wouldn't load af…
Browse files Browse the repository at this point in the history
…ter hot-restarting from legacy inspector (flutter#8491)
  • Loading branch information
elliette authored Oct 30, 2024
1 parent 9bd0125 commit 001839a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// found in the LICENSE file.

import 'package:devtools_app_shared/shared.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../shared/feature_flags.dart';
import '../../shared/globals.dart';
import '../../shared/screen.dart';
import '../../shared/utils.dart';
import '../inspector/inspector_screen_body.dart' as legacy;
import '../inspector_v2/inspector_screen_body.dart' as v2;
import 'inspector_screen_controller.dart';
Expand All @@ -33,17 +35,44 @@ class InspectorScreen extends Screen {
const InspectorScreenSwitcher();
}

class InspectorScreenSwitcher extends StatelessWidget {
class InspectorScreenSwitcher extends StatefulWidget {
const InspectorScreenSwitcher({super.key});

@override
State<InspectorScreenSwitcher> createState() =>
_InspectorScreenSwitcherState();
}

class _InspectorScreenSwitcherState extends State<InspectorScreenSwitcher>
with
AutoDisposeMixin,
ProvidedControllerMixin<InspectorScreenController,
InspectorScreenSwitcher> {
bool get shouldShowInspectorV2 =>
FeatureFlags.inspectorV2 &&
preferences.inspector.inspectorV2Enabled.value;

@override
void didChangeDependencies() {
super.didChangeDependencies();
if (!initController()) return;

addAutoDisposeListener(preferences.inspector.inspectorV2Enabled, () async {
controller.legacyInspectorController
.setVisibleToUser(!shouldShowInspectorV2);
await controller.v2InspectorController
.setVisibleToUser(shouldShowInspectorV2);
});
}

@override
Widget build(BuildContext context) {
final controller = Provider.of<InspectorScreenController>(context);

return ValueListenableBuilder(
valueListenable: preferences.inspector.inspectorV2Enabled,
builder: (context, v2Enabled, _) {
if (FeatureFlags.inspectorV2 && v2Enabled) {
if (shouldShowInspectorV2) {
return v2.InspectorScreenBody(
controller: controller.v2InspectorController,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class InspectorController extends DisposableController
serviceConnection.serviceManager.serviceExtensionManager
.hasServiceExtension(extensions.toggleSelectWidgetMode.extension);

void _onClientChange(bool added) {
Future<void> _onClientChange(bool added) async {
if (!added && _clientCount == 0) {
// Don't try to remove clients if there are none
return;
Expand All @@ -178,10 +178,10 @@ class InspectorController extends DisposableController
_clientCount += added ? 1 : -1;
assert(_clientCount >= 0);
if (_clientCount == 1) {
setVisibleToUser(true);
await setVisibleToUser(true);
setActivate(true);
} else if (_clientCount == 0) {
setVisibleToUser(false);
await setVisibleToUser(false);
}
}

Expand Down Expand Up @@ -276,13 +276,14 @@ class InspectorController extends DisposableController
return treeType;
}

void setVisibleToUser(bool visible) {
Future<void> setVisibleToUser(bool visible) async {
if (visibleToUser == visible) {
return;
}
visibleToUser = visible;

if (visibleToUser) {
await refreshInspector();
} else {
shutdownTree(false);
}
Expand Down
1 change: 1 addition & 0 deletions packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ To learn more about DevTools, check out the

- Added an option to the [new Inspector's](https://docs.flutter.dev/tools/devtools/release-notes/release-notes-2.40.1#inspector-updates)
settings to allow auto-refreshing the widget tree after a hot-reload. - [#8483](https://github.com/flutter/devtools/pull/8483)
- Fixed an [issue](https://github.com/flutter/devtools/issues/8487) where the [new Inspector](https://docs.flutter.dev/tools/devtools/release-notes/release-notes-2.40.1#inspector-updates) would not load after a hot-restart in the legacy Inspector. - [#8491](https://github.com/flutter/devtools/pull/8491)

## Performance updates

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ void main() {
await tester.pumpAndSettle(inspectorChangeSettleTime);

// Disable Inspector V2:
final disableSwitch = find.byType(DevToolsSwitch);
expect(disableSwitch, findsOneWidget);
await tester.tap(disableSwitch);
await toggleV2Inspector(tester);
await tester.pumpAndSettle(inspectorChangeSettleTime);

// Verify the legacy inspector is visible:
Expand All @@ -302,6 +300,43 @@ void main() {
},
);

// Test to verify https://github.com/flutter/devtools/issues/8487 is fixed.
testWidgetsWithWindowSize(
'revert to legacy inspector, hot-restart, and back to new inspector',
windowSize,
(WidgetTester tester) async {
await _loadInspectorUI(tester);

// Disable Inspector V2.
await toggleV2Inspector(tester);
await tester.pumpAndSettle(inspectorChangeSettleTime);

// Verify the legacy inspector is visible.
expect(find.richTextContaining('Widget Details Tree'), findsOneWidget);

// Trigger a hot restart.
await env.flutter!.hotRestart();
await tester.pumpAndSettle(inspectorChangeSettleTime);

// Enable Inspector V2.
await toggleV2Inspector(tester);
await tester.pumpAndSettle(inspectorChangeSettleTime);

// Verify the legacy inspector is not visible.
expect(find.richTextContaining('Widget Details Tree'), findsNothing);

// Wait for the widget tree to load.
final centerWidgetFinder = find.richText('Center');
final centerWidgetFinderWithRetries = await retryUntilFound(
centerWidgetFinder,
tester: tester,
retries: 10,
);
expect(centerWidgetFinderWithRetries, findsOneWidget);
},
skip: true, // https://github.com/flutter/devtools/issues/8490
);

testWidgetsWithWindowSize(
'tree nodes contain only essential information',
windowSize,
Expand Down Expand Up @@ -462,6 +497,12 @@ Finder findExpandCollapseButtonForNode({
return expandCollapseButtonFinder;
}

Future<void> toggleV2Inspector(WidgetTester tester) async {
final inspectorSwitch = find.byType(DevToolsSwitch);
expect(inspectorSwitch, findsOneWidget);
await tester.tap(inspectorSwitch);
}

void verifyPropertyIsVisible({
required String name,
required String value,
Expand Down

0 comments on commit 001839a

Please sign in to comment.