Skip to content

Commit 1ac2e82

Browse files
authored
Add BaseWindowController.isDestroyed flag (#189061)
## What's new? - Added `BaseWindowController.isDestroyed` - Added `BaseWindowController.isDestroyedOf` - Added `BaseWindowController.maybeIsDestroyedOf` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
1 parent 947adf9 commit 1ac2e82

8 files changed

Lines changed: 621 additions & 27 deletions

File tree

‎dev/integration_tests/windowing_test/lib/main.dart‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,28 @@ import 'dart:convert';
1111
import 'package:flutter/foundation.dart';
1212
import 'package:flutter/material.dart';
1313
import 'package:flutter/src/widgets/_window.dart';
14+
import 'package:flutter/src/widgets/_window_positioner.dart';
1415
import 'package:flutter_driver/driver_extension.dart';
1516

1617
late final RegularWindowController controller;
1718
final ValueNotifier<DialogWindowController?> dialogController = ValueNotifier(null);
1819

20+
/// A generic "secondary" window used by the `isDestroyed` end-to-end tests.
21+
///
22+
/// Constructing a controller creates the native window immediately, so these
23+
/// tests exercise the [BaseWindowController.isDestroyed] lifecycle at the
24+
/// controller level without mounting the window's widget. The reference is kept
25+
/// even after the window is destroyed so that its `isDestroyed` flag can still
26+
/// be queried.
27+
BaseWindowController? secondaryController;
28+
29+
/// Positioner used when creating child secondary windows (tooltip, popup,
30+
/// satellite).
31+
const WindowPositioner _kSecondaryPositioner = WindowPositioner(
32+
parentAnchor: WindowPositionerAnchor.right,
33+
childAnchor: WindowPositionerAnchor.left,
34+
);
35+
1936
void main() {
2037
final Completer<void> windowCreated = Completer();
2138
enableFlutterDriverExtension(
@@ -153,6 +170,55 @@ void main() {
153170
} else if (jsonMap['type'] == 'close_dialog') {
154171
dialogController.value?.destroy();
155172
return jsonEncode({'result': true});
173+
} else if (jsonMap['type'] == 'create_window') {
174+
// Destroy any lingering live secondary window so tests are isolated.
175+
if (secondaryController?.isDestroyed == false) {
176+
secondaryController!.destroy();
177+
}
178+
final windowType = jsonMap['window_type']! as String;
179+
180+
try {
181+
switch (windowType) {
182+
case 'regular':
183+
secondaryController = RegularWindowController(
184+
size: const Size(200, 200),
185+
title: 'Secondary',
186+
);
187+
case 'dialog':
188+
secondaryController = DialogWindowController(
189+
size: const Size(200, 200),
190+
parent: controller,
191+
);
192+
case 'tooltip':
193+
secondaryController = TooltipWindowController(
194+
parent: controller,
195+
anchorRect: const Rect.fromLTWH(0, 0, 100, 100),
196+
positioner: _kSecondaryPositioner,
197+
);
198+
case 'popup':
199+
secondaryController = PopupWindowController(
200+
parent: controller,
201+
anchorRect: const Rect.fromLTWH(0, 0, 100, 100),
202+
positioner: _kSecondaryPositioner,
203+
);
204+
case 'satellite':
205+
secondaryController = SatelliteWindowController(
206+
parent: controller,
207+
initialPositioner: _kSecondaryPositioner,
208+
size: const Size(200, 200),
209+
);
210+
default:
211+
throw ArgumentError('Unknown window_type: $windowType');
212+
}
213+
} on UnsupportedError {
214+
return jsonEncode({'result': 'unsupported'});
215+
}
216+
return jsonEncode({'result': true});
217+
} else if (jsonMap['type'] == 'is_window_destroyed') {
218+
return jsonEncode({'isDestroyed': secondaryController?.isDestroyed});
219+
} else if (jsonMap['type'] == 'destroy_window') {
220+
secondaryController?.destroy();
221+
return jsonEncode({'result': true});
156222
} else {
157223
throw ArgumentError('Unknown message type: ${jsonMap['type']}');
158224
}

‎dev/integration_tests/windowing_test/test_driver/main_test.dart‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ Future<String> _requestDataWithRetry(
3838
throw StateError('Should not reach here');
3939
}
4040

41+
/// Creates a secondary window of [windowType], asserts its controller reports
42+
/// `isDestroyed == false`, destroys it, then asserts `isDestroyed == true`.
43+
///
44+
/// If the platform reports the window type is unsupported, the test is skipped.
45+
Future<void> _expectIsDestroyedTransition(FlutterDriver driver, String windowType) async {
46+
final String createResponse = await _requestDataWithRetry(
47+
driver,
48+
jsonEncode({'type': 'create_window', 'window_type': windowType}),
49+
);
50+
if ((jsonDecode(createResponse) as Map<String, Object?>)['result'] == 'unsupported') {
51+
markTestSkipped('$windowType windows are not supported on this platform.');
52+
return;
53+
}
54+
55+
String response = await _requestDataWithRetry(
56+
driver,
57+
jsonEncode({'type': 'is_window_destroyed'}),
58+
);
59+
expect((jsonDecode(response) as Map<String, Object?>)['isDestroyed'], false);
60+
61+
await _requestDataWithRetry(driver, jsonEncode({'type': 'destroy_window'}));
62+
63+
response = await _requestDataWithRetry(driver, jsonEncode({'type': 'is_window_destroyed'}));
64+
expect((jsonDecode(response) as Map<String, Object?>)['isDestroyed'], true);
65+
}
66+
4167
void main() {
4268
group('end-to-end test', () {
4369
late final FlutterDriver driver;
@@ -202,5 +228,25 @@ void main() {
202228
timeout: Timeout.none,
203229
testOn: 'linux',
204230
);
231+
232+
test('Regular window is not destroyed until closed', () async {
233+
await _expectIsDestroyedTransition(driver, 'regular');
234+
}, timeout: Timeout.none);
235+
236+
test('Dialog window is not destroyed until closed', () async {
237+
await _expectIsDestroyedTransition(driver, 'dialog');
238+
}, timeout: Timeout.none);
239+
240+
test('Tooltip window is not destroyed until closed', () async {
241+
await _expectIsDestroyedTransition(driver, 'tooltip');
242+
}, timeout: Timeout.none);
243+
244+
test('Popup window is not destroyed until closed', () async {
245+
await _expectIsDestroyedTransition(driver, 'popup');
246+
}, timeout: Timeout.none);
247+
248+
test('Satellite window is not destroyed until closed', () async {
249+
await _expectIsDestroyedTransition(driver, 'satellite');
250+
}, timeout: Timeout.none);
205251
});
206252
}

‎packages/flutter/lib/src/widgets/_window.dart‎

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ sealed class BaseWindowController extends ChangeNotifier {
9797
set rootView(FlutterView view) {
9898
_view = view;
9999
}
100+
101+
/// Whether or not the underlying native window is destroyed.
102+
///
103+
/// {@macro flutter.widgets.windowing.experimental}
104+
@internal
105+
bool get isDestroyed;
100106
}
101107

102108
/// Delegate class for regular window controller.
@@ -1860,7 +1866,15 @@ class SatelliteWindow extends StatelessWidget {
18601866
}
18611867
}
18621868

1863-
enum _WindowControllerAspect { contentSize, title, activated, maximized, minimized, fullscreen }
1869+
enum _WindowControllerAspect {
1870+
contentSize,
1871+
title,
1872+
activated,
1873+
maximized,
1874+
minimized,
1875+
fullscreen,
1876+
destroyed,
1877+
}
18641878

18651879
/// Provides descendants with access to the [BaseWindowController] associated with
18661880
/// the window that is being rendered.
@@ -1890,12 +1904,17 @@ class WindowScope extends InheritedModel<_WindowControllerAspect> {
18901904
/// {@macro flutter.widgets.windowing.experimental}
18911905
@internal
18921906
WindowScope({super.key, required this.controller, required super.child})
1893-
: _contentSize = controller.contentSize,
1894-
_title = _titleValue(controller),
1895-
_isActivated = _isActivatedValue(controller),
1896-
_isMaximized = _isMaximizedValue(controller),
1897-
_isMinimized = _isMinimizedValue(controller),
1898-
_isFullscreen = _isFullscreenValue(controller) {
1907+
: _isDestroyed = controller.isDestroyed,
1908+
// A destroyed controller throws from its other getters (e.g.
1909+
// [BaseWindowController.contentSize]), so only the destroyed flag is read
1910+
// once the window is gone. The remaining aspects are moot at that point
1911+
// and fall back to defaults.
1912+
_contentSize = controller.isDestroyed ? Size.zero : controller.contentSize,
1913+
_title = controller.isDestroyed ? '' : _titleValue(controller),
1914+
_isActivated = !controller.isDestroyed && _isActivatedValue(controller),
1915+
_isMaximized = !controller.isDestroyed && _isMaximizedValue(controller),
1916+
_isMinimized = !controller.isDestroyed && _isMinimizedValue(controller),
1917+
_isFullscreen = !controller.isDestroyed && _isFullscreenValue(controller) {
18991918
if (!isWindowingEnabled) {
19001919
throw UnsupportedError(_kWindowingDisabledErrorMessage);
19011920
}
@@ -1917,6 +1936,7 @@ class WindowScope extends InheritedModel<_WindowControllerAspect> {
19171936
final bool _isMaximized;
19181937
final bool _isMinimized;
19191938
final bool _isFullscreen;
1939+
final bool _isDestroyed;
19201940

19211941
/// The controller associated with this window.
19221942
///
@@ -2179,6 +2199,37 @@ class WindowScope extends InheritedModel<_WindowControllerAspect> {
21792199
return _isFullscreenValue(controller);
21802200
}
21812201

2202+
/// Returns whether the nearest [WindowScope]'s window is destroyed.
2203+
///
2204+
/// {@macro flutter.widgets.windowing.windowScope.of}
2205+
///
2206+
/// {@macro flutter.widgets.windowing.experimental}
2207+
///
2208+
/// See also:
2209+
///
2210+
/// * [BaseWindowController.isDestroyed], which returns whether the underlying
2211+
/// native window is destroyed.
2212+
/// * [of], which returns the [BaseWindowController] associated with the window.
2213+
@internal
2214+
static bool isDestroyedOf(BuildContext context) {
2215+
return _of(context, _WindowControllerAspect.destroyed).isDestroyed;
2216+
}
2217+
2218+
/// Returns whether the nearest [WindowScope]'s window is destroyed,
2219+
/// or null if not found.
2220+
///
2221+
/// {@macro flutter.widgets.windowing.experimental}
2222+
///
2223+
/// See also:
2224+
///
2225+
/// * [BaseWindowController.isDestroyed], which returns whether the underlying
2226+
/// native window is destroyed.
2227+
/// * [maybeOf], which returns the [BaseWindowController] associated with the window, or null if not found.
2228+
@internal
2229+
static bool? maybeIsDestroyedOf(BuildContext context) {
2230+
return _maybeOf(context, _WindowControllerAspect.destroyed)?.isDestroyed;
2231+
}
2232+
21822233
/// Computes the value of the [_WindowControllerAspect.title] aspect for the
21832234
/// given [controller]. Controllers that do not support titles report an empty
21842235
/// string.
@@ -2283,7 +2334,8 @@ class WindowScope extends InheritedModel<_WindowControllerAspect> {
22832334
_isActivated != oldWidget._isActivated ||
22842335
_isMaximized != oldWidget._isMaximized ||
22852336
_isMinimized != oldWidget._isMinimized ||
2286-
_isFullscreen != oldWidget._isFullscreen;
2337+
_isFullscreen != oldWidget._isFullscreen ||
2338+
_isDestroyed != oldWidget._isDestroyed;
22872339
}
22882340

22892341
/// {@macro flutter.widgets.windowing.experimental}
@@ -2300,6 +2352,7 @@ class WindowScope extends InheritedModel<_WindowControllerAspect> {
23002352
_WindowControllerAspect.maximized => _isMaximized != oldWidget._isMaximized,
23012353
_WindowControllerAspect.minimized => _isMinimized != oldWidget._isMinimized,
23022354
_WindowControllerAspect.fullscreen => _isFullscreen != oldWidget._isFullscreen,
2355+
_WindowControllerAspect.destroyed => _isDestroyed != oldWidget._isDestroyed,
23032356
},
23042357
);
23052358
}

‎packages/flutter/lib/src/widgets/_window_linux.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ class RegularWindowControllerLinux extends RegularWindowController
377377
late final _FlWindowMonitor _windowMonitor;
378378
bool _destroyed = false;
379379

380+
@override
381+
@internal
382+
bool get isDestroyed => _destroyed;
383+
380384
@override
381385
@internal
382386
Size get contentSize => _window.getSize();
@@ -393,6 +397,7 @@ class RegularWindowControllerLinux extends RegularWindowController
393397
_windowMonitor.unref();
394398
_destroyed = true;
395399
_owner.registrar.unregister(rootView.viewId);
400+
notifyListeners();
396401
}
397402

398403
@override
@@ -591,6 +596,10 @@ class DialogWindowControllerLinux extends DialogWindowController implements Wind
591596
late final _FlWindowMonitor _windowMonitor;
592597
bool _destroyed = false;
593598

599+
@override
600+
@internal
601+
bool get isDestroyed => _destroyed;
602+
594603
@override
595604
@internal
596605
Size get contentSize => _window.getSize();
@@ -607,6 +616,7 @@ class DialogWindowControllerLinux extends DialogWindowController implements Wind
607616
_windowMonitor.unref();
608617
_destroyed = true;
609618
_owner.registrar.unregister(rootView.viewId);
619+
notifyListeners();
610620
}
611621

612622
@override
@@ -763,6 +773,10 @@ class TooltipWindowControllerLinux extends TooltipWindowController
763773
late final _FlWindowMonitor _windowMonitor;
764774
bool _destroyed = false;
765775

776+
@override
777+
@internal
778+
bool get isDestroyed => _destroyed;
779+
766780
@override
767781
@internal
768782
Size get contentSize => _window.getSize();
@@ -779,6 +793,7 @@ class TooltipWindowControllerLinux extends TooltipWindowController
779793
_windowMonitor.unref();
780794
_destroyed = true;
781795
_owner.registrar.unregister(rootView.viewId);
796+
notifyListeners();
782797
}
783798

784799
@override
@@ -955,6 +970,10 @@ class PopupWindowControllerLinux extends PopupWindowController implements Window
955970
Offset? _offsetFromParent;
956971
bool _destroyed = false;
957972

973+
@override
974+
@internal
975+
bool get isDestroyed => _destroyed;
976+
958977
@override
959978
@internal
960979
Size get contentSize => _window.getSize();
@@ -971,6 +990,7 @@ class PopupWindowControllerLinux extends PopupWindowController implements Window
971990
_windowMonitor.unref();
972991
_destroyed = true;
973992
_owner.registrar.unregister(rootView.viewId);
993+
notifyListeners();
974994
}
975995

976996
@override

‎packages/flutter/lib/src/widgets/_window_macos.dart‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ abstract interface class WindowControllerMacOS {
198198
/// {@macro flutter.widgets.windowing.experimental}
199199
@internal
200200
Pointer<Void> get windowHandle;
201+
202+
/// Whether or not the underlying native window is destroyed.
203+
///
204+
/// {@macro flutter.widgets.windowing.experimental}
205+
@internal
206+
bool get isDestroyed;
201207
}
202208

203209
mixin _WindowControllerMixin implements WindowControllerMacOS {
@@ -273,7 +279,9 @@ mixin _WindowControllerMixin implements WindowControllerMacOS {
273279
_MacOSPlatformInterface.destroyWindow(handle);
274280
}
275281

276-
bool get destroyed => _destroyed;
282+
@override
283+
@internal
284+
bool get isDestroyed => _destroyed;
277285

278286
bool _destroyed = false;
279287

@@ -345,6 +353,7 @@ class TooltipWindowControllerMacOS extends TooltipWindowController with _WindowC
345353
@override
346354
void _handleOnWillClose() {
347355
super._handleOnWillClose();
356+
notifyListeners();
348357
_delegate.onWindowDestroyed();
349358
}
350359

@@ -447,6 +456,7 @@ class PopupWindowControllerMacOS extends PopupWindowController with _WindowContr
447456
@override
448457
void _handleOnWillClose() {
449458
super._handleOnWillClose();
459+
notifyListeners();
450460
_delegate.onWindowDestroyed();
451461
}
452462

@@ -535,6 +545,7 @@ class RegularWindowControllerMacOS extends RegularWindowController with _WindowC
535545
@override
536546
void _handleOnWillClose() {
537547
super._handleOnWillClose();
548+
notifyListeners();
538549
_delegate.onWindowDestroyed();
539550
}
540551

@@ -671,6 +682,7 @@ class DialogWindowControllerMacOS extends DialogWindowController with _WindowCon
671682
@override
672683
void _handleOnWillClose() {
673684
super._handleOnWillClose();
685+
notifyListeners();
674686
_delegate.onWindowDestroyed();
675687
}
676688

0 commit comments

Comments
 (0)