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

[Inspector V2] Persist tab when a new widget is selected #8404

Merged
merged 2 commits into from
Oct 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math';

import 'package:devtools_app_shared/ui.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -45,6 +47,23 @@ class _DetailsTableState extends State<DetailsTable> {
LayoutProperties? get layoutProperties =>
widget.controller.selectedNodeProperties.value.layoutProperties;

final _widgetPropertiesTab = DevToolsTab.create(
tabName: 'Widget properties',
gaPrefix: DetailsTable.gaPrefix,
);

final _renderObjectTab = DevToolsTab.create(
tabName: 'Render object',
gaPrefix: DetailsTable.gaPrefix,
);

final _flexExplorerTab = DevToolsTab.create(
tabName: 'Flex explorer',
gaPrefix: DetailsTable.gaPrefix,
);

DevToolsTab? _lastSelectedTab;

@override
void initState() {
super.initState();
Expand All @@ -67,45 +86,89 @@ class _DetailsTableState extends State<DetailsTable> {
final widgetProperties = properties.widgetProperties;
final renderProperties = properties.renderProperties;
final layoutProperties = properties.layoutProperties;

final renderTabExists = renderProperties.isNotEmpty;
final flexExplorerTabExists = selectedNode?.isFlexLayout ?? false;

return AnalyticsTabbedView(
gaScreen: gac.inspector,
onTabChanged: (int tabIndex) {
_lastSelectedTab = _getTabForIndex(
tabIndex,
renderTabExists: renderTabExists,
flexExplorerTabExists: flexExplorerTabExists,
);
},
initialSelectedIndex: _getIndexForTab(
_lastSelectedTab ?? _widgetPropertiesTab,
renderTabExists: renderTabExists,
flexExplorerTabExists: flexExplorerTabExists,
),
tabs: [
(
tab: DevToolsTab.create(
tabName: 'Widget properties',
gaPrefix: DetailsTable.gaPrefix,
),
tab: _widgetPropertiesTab,
tabView: PropertiesView(
properties: widgetProperties,
layoutProperties: layoutProperties,
controller: widget.controller,
scrollController: _widgetPropertiesScrollController,
),
),
if (renderProperties.isNotEmpty)
if (renderTabExists)
(
tab: DevToolsTab.create(
tabName: 'Render object',
gaPrefix: DetailsTable.gaPrefix,
),
tab: _renderObjectTab,
tabView: PropertiesTable(
properties: renderProperties,
scrollController: _renderPropertiesScrollController,
),
),
if (selectedNode?.isFlexLayout ?? false)
if (flexExplorerTabExists)
(
tab: DevToolsTab.create(
tabName: 'Flex explorer',
gaPrefix: DetailsTable.gaPrefix,
),
tab: _flexExplorerTab,
tabView: FlexLayoutExplorerWidget(widget.controller),
),
],
);
},
);
}

DevToolsTab _getTabForIndex(
int index, {
required bool renderTabExists,
required bool flexExplorerTabExists,
}) {
final tabs = _getTabsInOrder(
renderTabExists: renderTabExists,
flexExplorerTabExists: flexExplorerTabExists,
);

return tabs.safeGet(index) ?? _widgetPropertiesTab;
}

int _getIndexForTab(
DevToolsTab tab, {
required bool renderTabExists,
required bool flexExplorerTabExists,
}) {
final tabs = _getTabsInOrder(
renderTabExists: renderTabExists,
flexExplorerTabExists: flexExplorerTabExists,
);

// If tab is not found, return the first tab (at index 0):
return max(tabs.indexOf(tab), 0);
}

List<DevToolsTab> _getTabsInOrder({
required bool renderTabExists,
required bool flexExplorerTabExists,
}) =>
[
_widgetPropertiesTab,
if (renderTabExists) _renderObjectTab,
if (flexExplorerTabExists) _flexExplorerTab,
];
}

/// Displays a widget's properties, including the layout properties and a
Expand Down