Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/src/widgets/window_caption.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ class _WindowCaptionState extends State<WindowCaption> {
() => nativeapi.Window.isCloseSupported() && window.close(),
) ??
false);
if (!closed) {
// Window.close() returns true once the request is made; a veto arrives
// asynchronously, so false means the request could not be made. Only a
// caption that follows the current window may then quit the app: a
// caption given its window must not end the whole application, which may
// have other windows.
if (!closed && widget.window == null) {
tryNative(() => nativeapi.Application.instance.quit(0));
}
}
Expand Down
45 changes: 45 additions & 0 deletions test/window_caption_close_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:window_manager/window_manager.dart';

// A window whose close request cannot be made, as when the platform refuses
// it. No native window is created.
class _RefusingWindow extends Window {
_RefusingWindow() : super.borrowed(0);

int closes = 0;

@override
bool get isMaximized => false;

@override
bool close() {
closes++;
return false;
}
}

void main() {
testWidgets('close button of an explicit window only addresses that window', (
tester,
) async {
final window = _RefusingWindow();
await tester.pumpWidget(
MaterialApp(
home: SizedBox(
height: kWindowCaptionHeight,
child: WindowCaption(window: window),
),
),
);

await tester.tap(find.byType(WindowCaptionButton).last);
await tester.pump();

// The request went to the given window and nothing else was attempted:
// with an explicit window the caption no longer falls back to quitting
// the application when the window does not close.
expect(window.closes, Window.isCloseSupported() ? 1 : 0);
expect(tester.takeException(), isNull);
});
}