Skip to content

Commit 9bd57f3

Browse files
authored
refactor: raw create/destroy methods are now hidden + renamed WindowContext to WindowControllerContext and made it provide the controller (#22)
1 parent df92dd1 commit 9bd57f3

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

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

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ abstract class WindowController with ChangeNotifier {
3333
WindowController._({
3434
VoidCallback? onDestroyed,
3535
void Function(String)? onError,
36-
required Future<WindowCreationResult> future,
36+
required Future<_WindowCreationResult> future,
3737
}) : _future = future {
3838
_future
39-
.then((WindowCreationResult metadata) async {
39+
.then((_WindowCreationResult metadata) async {
4040
_view = metadata.view;
4141
_state = metadata.state;
4242
_size = metadata.size;
@@ -70,7 +70,7 @@ abstract class WindowController with ChangeNotifier {
7070
/// created and is ready to be used. Otherwise, returns false.
7171
bool get isReady => _view != null;
7272

73-
final Future<WindowCreationResult> _future;
73+
final Future<_WindowCreationResult> _future;
7474

7575
late _WindowListener _listener;
7676

@@ -98,7 +98,7 @@ abstract class WindowController with ChangeNotifier {
9898
}
9999

100100
_isPendingDestroy = true;
101-
return destroyWindow(view.viewId);
101+
return _destroyWindow(view.viewId);
102102
}
103103
}
104104

@@ -126,7 +126,7 @@ class RegularWindowController extends WindowController {
126126
}) : super._(
127127
onDestroyed: onDestroyed,
128128
onError: onError,
129-
future: createRegular(
129+
future: _createRegular(
130130
size: size,
131131
sizeConstraints: sizeConstraints,
132132
title: title,
@@ -165,55 +165,56 @@ class _RegularWindowState extends State<RegularWindow> {
165165
Future<void> dispose() async {
166166
super.dispose();
167167

168-
// In the event that we're being disposed before we've been destroyed
169-
// we need to destroy the window on our way out.
170-
if (widget.controller.isReady) {
168+
if (widget.controller.isReady && !widget.controller._isPendingDestroy) {
171169
await widget.controller.destroy();
172170
}
173171
}
174172

175173
@override
176174
Widget build(BuildContext context) {
177-
return FutureBuilder<WindowCreationResult>(
175+
return FutureBuilder<_WindowCreationResult>(
178176
key: widget.key,
179177
future: widget.controller._future,
180-
builder: (BuildContext context, AsyncSnapshot<WindowCreationResult> metadata) {
178+
builder: (BuildContext context, AsyncSnapshot<_WindowCreationResult> metadata) {
181179
if (!metadata.hasData) {
182180
return const ViewCollection(views: <Widget>[]);
183181
}
184182

185183
return View(
186184
view: metadata.data!.view,
187-
child: WindowContext(viewId: metadata.data!.view.viewId, child: widget.child),
185+
child: WindowControllerContext(controller: widget.controller, child: widget.child),
188186
);
189187
},
190188
);
191189
}
192190
}
193191

194-
/// Provides descendents with access to the [Window] in which they are rendered
195-
class WindowContext extends InheritedWidget {
196-
/// [window] the [Window]
197-
const WindowContext({super.key, required this.viewId, required super.child});
192+
/// Provides descendents with access to the [WindowController] in which
193+
/// they are being rendered
194+
class WindowControllerContext extends InheritedWidget {
195+
/// Creates a new [WindowControllerContext]
196+
/// [controller] the controller associated with this window
197+
/// [child] the child widget
198+
const WindowControllerContext({super.key, required this.controller, required super.child});
198199

199-
/// The view ID in this context
200-
final int viewId;
200+
/// The controller associated with this window.
201+
final WindowController controller;
201202

202203
/// Returns the [WindowContext] if any
203-
static WindowContext? of(BuildContext context) {
204-
return context.dependOnInheritedWidgetOfExactType<WindowContext>();
204+
static WindowControllerContext? of(BuildContext context) {
205+
return context.dependOnInheritedWidgetOfExactType<WindowControllerContext>();
205206
}
206207

207208
@override
208-
bool updateShouldNotify(WindowContext oldWidget) {
209-
return viewId != oldWidget.viewId;
209+
bool updateShouldNotify(WindowControllerContext oldWidget) {
210+
return controller != oldWidget.controller;
210211
}
211212
}
212213

213214
/// The raw data returned as a result of creating a window.
214-
class WindowCreationResult {
215+
class _WindowCreationResult {
215216
/// Creates a new window.
216-
WindowCreationResult({
217+
_WindowCreationResult({
217218
required this.view,
218219
required this.archetype,
219220
required this.size,
@@ -238,15 +239,7 @@ class WindowCreationResult {
238239
final int? parent;
239240
}
240241

241-
/// Creates a regular window for the platform and returns the metadata associated
242-
/// with the new window. Users should prefer using the [RegularWindow]
243-
/// widget instead of this method.
244-
///
245-
/// [size] the size of the new [Window] in pixels.
246-
/// [sizeConstraints] the size constraints of the new [Window].
247-
/// [title] the window title
248-
/// [state] the initial window state
249-
Future<WindowCreationResult> createRegular({
242+
Future<_WindowCreationResult> _createRegular({
250243
required Size size,
251244
BoxConstraints? sizeConstraints,
252245
String? title,
@@ -273,7 +266,7 @@ Future<WindowCreationResult> createRegular({
273266
);
274267
}
275268

276-
Future<WindowCreationResult> _createWindow({
269+
Future<_WindowCreationResult> _createWindow({
277270
required WindowArchetype archetype,
278271
required Future<Map<Object?, Object?>> Function(MethodChannel channel) viewBuilder,
279272
}) async {
@@ -298,18 +291,15 @@ Future<WindowCreationResult> _createWindow({
298291
},
299292
);
300293

301-
return WindowCreationResult(
294+
return _WindowCreationResult(
302295
view: flView,
303296
archetype: archetype,
304297
size: Size(size[0]! as double, size[1]! as double),
305298
state: state,
306299
);
307300
}
308301

309-
/// Destroys the window associated with the provided view ID.
310-
///
311-
/// [viewId] the view id of the window that should be destroyed
312-
Future<void> destroyWindow(int viewId) async {
302+
Future<void> _destroyWindow(int viewId) async {
313303
try {
314304
await SystemChannels.windowing.invokeMethod('destroyWindow', <String, dynamic>{
315305
'viewId': viewId,

0 commit comments

Comments
 (0)