@@ -28,6 +28,7 @@ English | [简体中文](./README-ZH.md)
2828 - [ Quit on close] ( #quit-on-close )
2929 - [ macOS] ( #macos )
3030 - [ Windows] ( #windows )
31+ - [ Confirm before closing] ( #confirm-before-closing )
3132 - [ Hidden at launch] ( #hidden-at-launch )
3233 - [ macOS] ( #macos-1 )
3334 - [ Windows] ( #windows-1 )
@@ -36,9 +37,11 @@ English | [简体中文](./README-ZH.md)
3637 - [ WindowManager] ( #windowmanager )
3738 - [ Methods] ( #methods )
3839 - [ close] ( #close )
40+ - [ isPreventClose] ( #ispreventclose )
41+ - [ setPreventClose] ( #setpreventclose )
3942 - [ focus] ( #focus )
4043 - [ blur ` macos ` ` windows ` ] ( #blur--macos--windows )
41- - [ isFocused ` windows ` ] ( #isfocused--windows )
44+ - [ isFocused ` macos ` ` windows ` ] ( #isfocused--macos --windows )
4245 - [ show] ( #show )
4346 - [ hide] ( #hide )
4447 - [ isVisible] ( #isvisible )
@@ -84,6 +87,7 @@ English | [简体中文](./README-ZH.md)
8487 - [ startDragging] ( #startdragging )
8588 - [ WindowListener] ( #windowlistener )
8689 - [ Methods] ( #methods-1 )
90+ - [ onWindowClose] ( #onwindowclose )
8791 - [ onWindowFocus] ( #onwindowfocus )
8892 - [ onWindowBlur] ( #onwindowblur )
8993 - [ onWindowMaximize] ( #onwindowmaximize )
@@ -113,7 +117,7 @@ Add this to your package's `pubspec.yaml` file:
113117
114118``` yaml
115119dependencies :
116- window_manager : ^0.1.5
120+ window_manager : ^0.1.6
117121` ` `
118122
119123Or
@@ -188,6 +192,11 @@ class _HomePageState extends State<HomePage> with WindowListener {
188192 print('[WindowManager] onWindowEvent: $eventName');
189193 }
190194
195+ @override
196+ void onWindowClose() {
197+ // do something
198+ }
199+
191200 @override
192201 void onWindowFocus() {
193202 // do something
@@ -279,6 +288,72 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
279288}
280289```
281290
291+ ### Confirm before closing
292+
293+ ``` dart
294+ import 'package:flutter/cupertino.dart';
295+ import 'package:window_manager/window_manager.dart';
296+
297+ class HomePage extends StatefulWidget {
298+ @override
299+ _HomePageState createState() => _HomePageState();
300+ }
301+
302+ class _HomePageState extends State<HomePage> with WindowListener {
303+ @override
304+ void initState() {
305+ windowManager.addListener(this);
306+ super.initState();
307+ }
308+
309+ void _init() async {
310+ await windowManager.setPreventClose(true);
311+ setState(() {});
312+ }
313+
314+ @override
315+ void dispose() {
316+ windowManager.removeListener(this);
317+ super.dispose();
318+ }
319+
320+ @override
321+ Widget build(BuildContext context) {
322+ // ...
323+ }
324+
325+ @override
326+ void onWindowClose() async {
327+ bool _isPreventClose = await windowManager.isPreventClose();
328+ if (_isPreventClose) {
329+ showDialog(
330+ context: context,
331+ builder: (_) {
332+ return AlertDialog(
333+ title: Text('Are you sure you want to close this window?'),
334+ actions: [
335+ TextButton(
336+ child: Text('No'),
337+ onPressed: () {
338+ Navigator.of(context).pop();
339+ },
340+ ),
341+ TextButton(
342+ child: Text('Yes'),
343+ onPressed: () {
344+ Navigator.of(context).pop();
345+ exit(0);
346+ },
347+ ),
348+ ],
349+ );
350+ },
351+ );
352+ }
353+ }
354+ }
355+ ```
356+
282357#### Hidden at launch
283358
284359##### macOS
@@ -386,6 +461,15 @@ class _HomePageState extends State<HomePage> with WindowListener {
386461
387462Try to close the window.
388463
464+ ##### isPreventClose
465+
466+ Check if is intercepting the native close signal.
467+
468+ ##### setPreventClose
469+
470+ Set if intercept the native close signal. May useful when combine with the onclose event listener.
471+ This will also prevent the manually triggered close event.
472+
389473##### focus
390474
391475Focuses on the window.
@@ -395,7 +479,7 @@ Focuses on the window.
395479Removes focus from the window.
396480
397481
398- ##### isFocused ` windows `
482+ ##### isFocused ` macos ` ` windows `
399483
400484Returns ` bool ` - Whether window is focused.
401485
@@ -589,6 +673,10 @@ Starts a window drag based on the specified mouse-down event.
589673
590674#### Methods
591675
676+ ##### onWindowClose
677+
678+ Emitted when the window is going to be closed.
679+
592680##### onWindowFocus
593681
594682Emitted when the window gains focus.
0 commit comments