Skip to content

Commit

Permalink
add onWindowClose callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ErBWs committed Jan 31, 2025
1 parent 09f31d0 commit 60af1a0
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion lib/app_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,29 @@ class AppWidget extends StatefulWidget {
}

class _AppWidgetState extends State<AppWidget>
with TrayListener, WidgetsBindingObserver {
with TrayListener, WidgetsBindingObserver, WindowListener {
Box setting = GStorage.setting;

final TrayManager trayManager = TrayManager.instance;

@override
void initState() {
trayManager.addListener(this);
windowManager.addListener(this);
setPreventClose();
WidgetsBinding.instance.addObserver(this);
super.initState();
}

void setPreventClose() async {
await windowManager.setPreventClose(true);
setState(() {});
}

@override
void dispose() {
trayManager.removeListener(this);
windowManager.removeListener(this);
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Expand All @@ -63,6 +71,29 @@ class _AppWidgetState extends State<AppWidget>
}
}

@override
void onWindowClose() async {
bool isPreventClose = await windowManager.isPreventClose();
if (isPreventClose) {
KazumiDialog.show(builder: (context) {
return AlertDialog(
title: const Text('退出确认'),
content: const Text('您想要退出 Kazumi 吗?'),
actions: [
TextButton(onPressed: () => exit(0), child: const Text('退出 Kazumi')),
TextButton(
onPressed: () {
KazumiDialog.dismiss();
windowManager.hide();
},
child: const Text('最小化至托盘')),
const TextButton(onPressed: KazumiDialog.dismiss, child: Text('取消')),
],
);
});
}
}

/// 处理前后台变更
/// windows/linux 在程序后台或失去焦点时只会触发 inactive 不会触发 paused
/// android/ios/macos 在程序后台时会先触发 inactive 再触发 paused, 回到前台时会先触发 inactive 再触发 resumed
Expand Down

0 comments on commit 60af1a0

Please sign in to comment.