Move WindowManager outside of WidgetsApp#188866
Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
There was a problem hiding this comment.
Code Review
This pull request refactors the windowing system by updating WindowManager to accept a list of initialWindows instead of a single child widget, registering them during initialization, and removing the automatic wrapping of WindowManager in WidgetsApp. The example application is updated accordingly. Feedback on these changes recommends adding documentation for the new initialWindows property, asserting that the list is not empty to prevent widget tree disposal issues, and replacing the forEach loop with a for-in loop for registering windows to adhere to Dart style guidelines.
| /// {@macro flutter.widgets.windowing.experimental} | ||
| @internal | ||
| const WindowManager({super.key, required this.child}); | ||
| const WindowManager({super.key, required this.initialWindows}); |
There was a problem hiding this comment.
It is recommended to assert that initialWindows is not empty to prevent creating an empty ViewCollection, which would artificially dispose the widget tree.
| const WindowManager({super.key, required this.initialWindows}); | |
| const WindowManager({ | |
| super.key, | |
| required this.initialWindows, | |
| }) : assert(initialWindows.isNotEmpty, 'initialWindows must not be empty.'); |
mattkae
left a comment
There was a problem hiding this comment.
This seems sensible to me.
Another thing to think about: should the WindowManager just be provided by runWidget long term? That way, it is guaranteed to only show up once at the entry point of the app.
cc: @loic-sharma
| // Disable this for now. This is cursed and only works by accident because | ||
| // the WindowManager was planted in a particular place of WidgetApp hierarchy, | ||
| // which unfortunately caused a host of other issue. | ||
|
|
||
| // final WindowRegistry? windowRegistry = WindowRegistry.maybeOf(context); | ||
| // if (windowRegistry != null && isWindowingEnabled) { | ||
| // try { | ||
| // final Size? parentSize = WindowScope.maybeContentSizeOf(context); | ||
| // return navigator.push<T>( | ||
| // _DialogWindowRoute<T>( | ||
| // builder: builder, | ||
| // parentController: WindowScope.maybeOf(context), | ||
| // context: context, | ||
| // settings: routeSettings, | ||
| // preferredSize: fullscreenDialog ? parentSize : null, | ||
| // ), | ||
| // ); | ||
| // } on UnsupportedError catch (error, stacktrace) { | ||
| // // Fallback to normal dialog route if windowing is not supported. | ||
| // FlutterError.reportError( | ||
| // FlutterErrorDetails(exception: error, library: 'widgets library', stack: stacktrace), | ||
| // ); | ||
| // } | ||
| // } |
There was a problem hiding this comment.
Do we need to disable this for now? Can we just check if the user is manually providing a WindowManager and create the dialog then? I see no harm in that and then we don't need to update the core dialog code at all. It will just mostly do the non-windowy way.
There was a problem hiding this comment.
The dialog will not be sized to content when it contains entire MaterialApp because the Overlay inside navigator will always size to cosntriants.biggest. So no matter what you do the dialog will cover entire screen. And we have no way to force size using the route.
Possible but hacky solution would be to set the alwaysSizeToContent in navigator overlay if there is WindowManager present. But it's seems really hacky.
There was a problem hiding this comment.
I am tempted to keep this code since it still works as good as it did before, but we can also re-add it later if we feel that that is more appropriate 👍
There was a problem hiding this comment.
Unfortunately I have to disagree. For the route to work interchangeably with and window windowing enabled the widget would need to be planted at the same place in window hierarchy, but we don't currently have a straightforward way to do that.
bcf20cb to
4560628
Compare
There was a problem hiding this comment.
Code Review
This pull request refactors window management in Flutter by updating WindowManager to accept a list of initialWindows instead of a single child widget, registering them in initState, and removing windowing-specific dialog routing and WidgetsApp integration. Feedback on these changes suggests adding an assertion to ensure initialWindows is not empty, refactoring Iterable.forEach to a standard for-in loop in _WindowManagerState.initState for better performance and readability, and wrapping the independent widget trees in PopupButton and TooltipButton with MaterialApp or Directionality to prevent runtime errors from missing inherited contexts.
| ? View( | ||
| view: _popupWindowEntry!.controller.rootView, | ||
| child: Builder(builder: _popupWindowEntry!.builder), | ||
| ) |
There was a problem hiding this comment.
Since View bootstraps a completely independent widget tree, its child (PopupWindowContent) will not inherit any InheritedWidgets (such as Theme, MediaQuery, or Directionality) from the parent window's context. If PopupWindowContent contains widgets like Text or Material components that rely on these inherited contexts, it will throw runtime errors. Consider wrapping the child in a MaterialApp or at least Directionality and Material to ensure it has the necessary context, similar to how DialogWindowContent and RegularWindowContent were updated.
There was a problem hiding this comment.
That is completely incorrect. It's all part of single element tree.
| ? View( | ||
| view: _tooltipEntry!.controller.rootView, | ||
| child: Builder(builder: _tooltipEntry!.builder), | ||
| ) |
There was a problem hiding this comment.
Since View bootstraps a completely independent widget tree, its child (TooltipWindowContent) will not inherit any InheritedWidgets (such as Theme, MediaQuery, or Directionality) from the parent window's context. If TooltipWindowContent contains widgets like Text or Material components that rely on these inherited contexts, it will throw runtime errors. Consider wrapping the child in a MaterialApp or at least Directionality and Material to ensure it has the necessary context, similar to how DialogWindowContent and RegularWindowContent were updated.
| } | ||
| void initState() { | ||
| super.initState(); | ||
| widget.initialWindows.forEach(_registry.register); |
There was a problem hiding this comment.
In Flutter framework code, using a standard for loop is preferred over Iterable.forEach for better readability and to avoid the overhead of function closures. Consider refactoring this to a standard for-in loop.
for (final WindowEntry entry in widget.initialWindows) {
_registry.register(entry);
}References
- Effective Dart recommends avoiding
Iterable.forEachwith function literals or when a standardforloop is more readable and performant. (link)
There was a problem hiding this comment.
No. Using normal for loop actually triggers analyzer warning.
This is a prototype branch for moving
WindowManageroutside ofWidgetsApp.Our current approach where first window is "blessed" and other windows are anchored inside first window hierarchy is causing multiple issues:
In general there is no reason for one window to be special. Normally for a desktop application all windows should be equal. The closest we can get to this is to give each top level window its own Widgets/MaterialApp. While this duplicates some elements that should normally be shared, arguably it behaves better than doing what we do right now.
Ideally we'd want to split the App widget into shared and per window parts, but that will be a much bigger change.
With this branch the top level Dialog/Regular window have each their own MaterialApp widget and separate focus trees, solving the issues above.
PopupandTooltipwindow work and are anchored at proper place in window hierarchy (they should not be top level window). However we might want to consider a convenience Widget to anchoring these instead of usingViewAnchor+View.Screenshot of example app running in this mode - notice the Debug banner and widget inspector buttons in each window.