Skip to content

Commit 529b9ea

Browse files
committed
wip: able to create windows in the reference application again
1 parent a4f99b7 commit 529b9ea

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

examples/multi_window_ref_app/lib/app/main_window.dart

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:multi_window_ref_app/app/window_metadata_content.dart';
33

4-
import 'regular_window_content.dart';
54
import 'window_settings.dart';
65
import 'window_settings_dialog.dart';
76

@@ -74,7 +73,8 @@ class _MainWindowState extends State<MainWindow> {
7473
listenable: _windowManagerModel,
7574
builder: (BuildContext context, Widget? child) {
7675
return _WindowCreatorCard(
77-
selectedWindow: _windowManagerModel.selected);
76+
selectedWindow: _windowManagerModel.selected,
77+
windowManagerModel: _windowManagerModel);
7878
})
7979
],
8080
),
@@ -83,12 +83,29 @@ class _MainWindowState extends State<MainWindow> {
8383
),
8484
);
8585

86+
return ViewAnchor(
87+
view: ListenableBuilder(
88+
listenable: _windowManagerModel,
89+
builder: (BuildContext context, Widget? widget) {
90+
return _ViewCollection(windowManagerModel: _windowManagerModel);
91+
}),
92+
child: widget);
93+
}
94+
}
95+
96+
class _ViewCollection extends StatelessWidget {
97+
_ViewCollection({required this.windowManagerModel});
98+
99+
_WindowManagerModel windowManagerModel;
100+
101+
@override
102+
Widget build(BuildContext context) {
86103
final List<Widget> childViews = <Widget>[];
87-
for (final WindowMetadata childWindow in _windowManagerModel.windows) {
104+
for (final WindowMetadata childWindow in windowManagerModel.windows) {
88105
childViews.add(WindowMetadataContent(childWindow: childWindow));
89106
}
90107

91-
return ViewAnchor(view: ViewCollection(views: childViews), child: widget);
108+
return ViewCollection(views: childViews);
92109
}
93110
}
94111

@@ -181,9 +198,11 @@ class _ActiveWindowsTable extends StatelessWidget {
181198
}
182199

183200
class _WindowCreatorCard extends StatefulWidget {
184-
const _WindowCreatorCard({required this.selectedWindow});
201+
const _WindowCreatorCard(
202+
{required this.selectedWindow, required this.windowManagerModel});
185203

186204
final WindowMetadata? selectedWindow;
205+
final _WindowManagerModel windowManagerModel;
187206

188207
@override
189208
State<StatefulWidget> createState() => _WindowCreatorCardState();
@@ -216,7 +235,9 @@ class _WindowCreatorCardState extends State<_WindowCreatorCard> {
216235
children: [
217236
OutlinedButton(
218237
onPressed: () async {
219-
await createRegular(size: _settings.regularSize);
238+
final WindowMetadata metadata =
239+
await createRegular(size: _settings.regularSize);
240+
widget.windowManagerModel.add(metadata);
220241
},
221242
child: const Text('Regular'),
222243
),

packages/flutter/lib/src/widgets/window.dart

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:ui' show FlutterView;
66

77
import 'package:flutter/material.dart';
8+
import 'package:flutter/scheduler.dart';
89
import 'package:flutter/services.dart';
910

1011
/// Defines the type of a [Window]
@@ -81,27 +82,34 @@ class _RegularWindowState extends State<RegularWindow> {
8182
@override
8283
void initState() {
8384
super.initState();
84-
final _WindowingAppContext? windowingAppContext =
85-
_WindowingAppContext.of(context);
8685
widget._future.then((RegularWindowMetadata metadata) async {
87-
assert(windowingAppContext != null);
88-
_listener = _WindowListener(
89-
viewId: metadata.view.viewId,
90-
onChanged: (_WindowChangeProperties properties) {
91-
if (widget.controller == null) {
92-
return;
93-
}
94-
95-
if (properties.size != null) {
96-
widget.controller!._size = properties.size!;
97-
}
98-
99-
if (properties.parentViewId != null) {
100-
widget.controller!._parentViewId = properties.parentViewId;
101-
}
102-
},
103-
onDestroyed: widget.onDestroyed);
104-
windowingAppContext!.windowingApp._registerListener(_listener!);
86+
if (widget.controller != null) {
87+
widget.controller!._parentViewId = metadata.parentViewId;
88+
widget.controller!._size = metadata.size;
89+
}
90+
91+
SchedulerBinding.instance.addPostFrameCallback((_) async {
92+
final _WindowingAppContext? windowingAppContext =
93+
_WindowingAppContext.of(context);
94+
assert(windowingAppContext != null);
95+
_listener = _WindowListener(
96+
viewId: metadata.view.viewId,
97+
onChanged: (_WindowChangeProperties properties) {
98+
if (widget.controller == null) {
99+
return;
100+
}
101+
102+
if (properties.size != null) {
103+
widget.controller!._size = properties.size!;
104+
}
105+
106+
if (properties.parentViewId != null) {
107+
widget.controller!._parentViewId = properties.parentViewId;
108+
}
109+
},
110+
onDestroyed: widget.onDestroyed);
111+
windowingAppContext!.windowingApp._registerListener(_listener!);
112+
});
105113
});
106114
}
107115

@@ -123,7 +131,8 @@ class _RegularWindowState extends State<RegularWindow> {
123131
builder: (BuildContext context,
124132
AsyncSnapshot<RegularWindowMetadata> metadata) {
125133
if (!metadata.hasData) {
126-
return Container();
134+
final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized();
135+
return binding.wrapWithDefaultView(Container());
127136
}
128137

129138
return View(
@@ -202,6 +211,7 @@ Future<RegularWindowMetadata> createRegular({required Size size}) async {
202211
Future<_WindowMetadata> _createWindow(
203212
{required Future<Map<Object?, Object?>> Function(MethodChannel channel)
204213
viewBuilder}) async {
214+
WidgetsFlutterBinding.ensureInitialized();
205215
final Map<Object?, Object?> creationData =
206216
await viewBuilder(SystemChannels.windowing);
207217
final int viewId = creationData['viewId']! as int;
@@ -260,6 +270,7 @@ class _WindowListener {
260270
/// The current [Window] can be looked up with [WindowContext.of].
261271
class WindowingApp extends StatelessWidget {
262272
WindowingApp({super.key, required this.children}) {
273+
WidgetsFlutterBinding.ensureInitialized();
263274
SystemChannels.windowing.setMethodCallHandler(_methodCallHandler);
264275
}
265276

0 commit comments

Comments
 (0)