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

Open WidgetInspectorService APIs to use by package/devtools extensions authors #8519

Open
tbuczkowski opened this issue Oct 30, 2024 · 1 comment
Labels
devtools extensions Issues related to DevTools extensions P2 important to work on, but not at the top of the work list. screen: inspector

Comments

@tbuczkowski
Copy link

tbuczkowski commented Oct 30, 2024

Use case

My team and I are building a new package and devtools extension, that would highly benefit from the ability to tell which widget from the local project corresponds to a specific coordinate on screen - exactly like the WidgetInspector does with the "Select Widget" feature.

My first thought was to learn how it's implemented in WidgetInspector and try to tap into the same APIs, but I quickly learned that those APIs clearly were not meant to be called from the "outside", and while I was able to work my way around their limitations, it's very hacky and definitely not the intended way to do it.

The issue I'm facing is that most of the WidgetInspectorService methods are either private or protected. The class is designed in such a way, that a lot of methods that return some useful data are private and have their protected counterpart that converts their return value into JSON for transfer to devtools. For example:

  String getChildrenSummaryTree(String diagnosticsNodeId, String groupName) {
    return _safeJsonEncode(_getChildrenSummaryTree(diagnosticsNodeId, groupName));
  }

and

List<Object> _getChildrenSummaryTree(String? diagnosticableId, String groupName) {
    final DiagnosticsNode? node = _idToDiagnosticsNode(diagnosticableId);
    if (node == null) {
      return <Object>[];
    }

    final InspectorSerializationDelegate delegate = InspectorSerializationDelegate(groupName: groupName, summaryTree: true, service: this);
    return _nodesToJson(_getChildrenFiltered(node, delegate), delegate, parent: node);
  }

And if we assume that the devtools is the only consumer of that data, then it's perfectly understandable why it was designed that way. But since it's all protected or private, there's no "right" way for external packages to check whether a specific widget was created by local project.

I think it basically comes down to a single small method within WidgetInspectorService, that allows for checking the object creation location:

_Location? _getObjectCreationLocation(Object object) {
  return object is _HasCreationLocation ? object._location : null;
}

And since the _HasCreationLocation class doesn't seem to be explicitly used anywhere else I can only assume that it is somehow dynamically added to objects by the engine in runtime.

Proposal

My proposal is to either open up the WidgetInspectorService APIs to allow package authors to tap into the methods it provides, or even just allow the _HasCreationLocation class to be used outside of the WidgetInspector environment by making it public.

I'm aware this is a rather niche feature request that wouldn't be used by many developers, but if there's no significant arguments against it, I'd be happy to prepare a PR with a proposal of how the updated API could look like.

@chunhtai
Copy link
Contributor

chunhtai commented Nov 5, 2024

I think the framework team doesn't own the WidgetInspector, the devtool team does. Assigning to tool team for now, please reassign accordingly.

cc @kenzieschmoll

@bkonyi bkonyi transferred this issue from flutter/flutter Nov 12, 2024
@kenzieschmoll kenzieschmoll added screen: inspector devtools extensions Issues related to DevTools extensions P2 important to work on, but not at the top of the work list. labels Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devtools extensions Issues related to DevTools extensions P2 important to work on, but not at the top of the work list. screen: inspector
Projects
None yet
Development

No branches or pull requests

3 participants