Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat(ui): redesign exit confirmation

Predidit665784a5b18a
diff --git a/lib/app_widget.dart b/lib/app_widget.dart
index d1a63047..3ff3c390 100644
--- a/lib/app_widget.dart
+++ b/lib/app_widget.dart
@@ -9,6 +9,7 @@ import 'package:tray_manager/tray_manager.dart';
import 'package:kazumi/services/logging/logger.dart';
import 'package:window_manager/window_manager.dart';
import 'package:kazumi/bean/dialog/dialog_helper.dart';
+import 'package:kazumi/bean/dialog/exit_confirmation_dialog.dart';
import 'package:kazumi/bean/settings/theme_provider.dart';
import 'package:kazumi/navigation.dart';
import 'package:kazumi/utils/constants.dart';
@@ -25,7 +26,7 @@ class AppWidget extends StatefulWidget {
class _AppWidgetState extends State<AppWidget>
with TrayListener, WidgetsBindingObserver, WindowListener {
final TrayManager trayManager = TrayManager.instance;
- bool showingExitDialog = false;
+ bool _isHandlingWindowClose = false;
bool _didApplyStoredThemeSettings = false;
Brightness? _lastTitleBarBrightness;
@@ -184,81 +185,47 @@ class _AppWidgetState extends State<AppWidget>
}
}
- /// 处理窗口关闭事件,
- /// 需要使用 `windowManager.close()` 来触发,`exit(0)` 会直接退出程序
+ // windowManager.close() triggers this handler; exit() bypasses confirmation.
@override
- void onWindowClose() {
- final exitBehavior = GStorage.getSetting(SettingsKeys.exitBehavior);
-
- switch (exitBehavior) {
- case 0:
- exit(0);
- case 1:
- KazumiDialog.dismiss();
- windowManager.hide();
- break;
- default:
- if (showingExitDialog) return;
- showingExitDialog = true;
- KazumiDialog.show(onDismiss: () {
- showingExitDialog = false;
- }, builder: (context) {
- bool saveExitBehavior = false; // 下次不再询问?
-
- return AlertDialog(
- title: const Text('退出确认'),
- content: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- const Text('您想要退出 Kazumi 吗?'),
- const SizedBox(height: 24),
- StatefulBuilder(builder: (context, setState) {
- onChanged(value) {
- saveExitBehavior = value ?? false;
- setState(() {});
- }
-
- return Wrap(
- crossAxisAlignment: WrapCrossAlignment.center,
- spacing: 8,
- children: [
- Checkbox(value: saveExitBehavior, onChanged: onChanged),
- const Text('下次不再询问'),
- ],
- );
- }),
- ],
- ),
- actions: [
- TextButton(
- onPressed: () async {
- if (saveExitBehavior) {
- await GStorage.putSetting(SettingsKeys.exitBehavior, 0);
- }
- exit(0);
- },
- child: const Text('退出 Kazumi')),
- TextButton(
- onPressed: () async {
- if (saveExitBehavior) {
- await GStorage.putSetting(SettingsKeys.exitBehavior, 1);
- }
- KazumiDialog.dismiss();
- windowManager.hide();
- },
- child: const Text('最小化至托盘')),
- const TextButton(
- onPressed: KazumiDialog.dismiss, child: Text('取消')),
- ],
+ Future<void> onWindowClose() async {
+ if (_isHandlingWindowClose || !mounted) return;
+ _isHandlingWindowClose = true;
+ try {
+ var action = switch (GStorage.getSetting(SettingsKeys.exitBehavior)) {
+ 0 => ExitDialogAction.exit,
+ 1 => ExitDialogAction.minimizeToTray,
+ _ => null,
+ };
+ if (action == null) {
+ final result = await KazumiDialog.show<ExitDialogResult>(
+ builder: (_) => const ExitConfirmationDialog(),
+ );
+ if (result == null || !mounted) return;
+
+ action = result.action;
+ if (result.rememberChoice) {
+ await GStorage.putSetting(
+ SettingsKeys.exitBehavior,
+ switch (action) {
+ ExitDialogAction.exit => 0,
+ ExitDialogAction.minimizeToTray => 1,
+ },
);
- });
+ }
+ }
+
+ if (!mounted) return;
+ switch (action) {
+ case ExitDialogAction.exit:
+ exit(0);
+ case ExitDialogAction.minimizeToTray:
+ await windowManager.hide();
+ }
+ } finally {
+ _isHandlingWindowClose = false;
}
}
- /// 处理前后台变更
- /// windows/linux 在程序后台或失去焦点时只会触发 inactive 不会触发 paused
- /// android/ios/macos 在程序后台时会先触发 inactive 再触发 paused, 回到前台时会先触发 inactive 再触发 resumed
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
super.didChangeAppLifecycleState(state);
diff --git a/lib/bean/dialog/exit_confirmation_dialog.dart b/lib/bean/dialog/exit_confirmation_dialog.dart
new file mode 100644
index 00000000..c97a5705
--- /dev/null
+++ b/lib/bean/dialog/exit_confirmation_dialog.dart
@@ -0,0 +1,216 @@
+import 'package:flutter/material.dart';
+
+enum ExitDialogAction { exit, minimizeToTray }
+
+class ExitDialogResult {
+ const ExitDialogResult({
+ required this.action,
+ required this.rememberChoice,
+ });
+
+ final ExitDialogAction action;
+ final bool rememberChoice;
+}
+
+class ExitConfirmationDialog extends StatefulWidget {
+ const ExitConfirmationDialog({super.key});
+
+ @override
+ State<ExitConfirmationDialog> createState() => _ExitConfirmationDialogState();
+}
+
+class _ExitConfirmationDialogState extends State<ExitConfirmationDialog> {
+ ExitDialogAction _action = ExitDialogAction.minimizeToTray;
+ bool _rememberChoice = false;
+
+ Duration get _animationDuration => MediaQuery.disableAnimationsOf(context)
+ ? Duration.zero
+ : const Duration(milliseconds: 200);
+
+ @override
+ Widget build(BuildContext context) {
+ final theme = Theme.of(context);
+ final colors = theme.colorScheme;
+ final buttonStyle = ButtonStyle(
+ minimumSize: const WidgetStatePropertyAll(Size(64, 48)),
+ padding: const WidgetStatePropertyAll(
+ EdgeInsets.symmetric(horizontal: 24, vertical: 12),
+ ),
+ animationDuration: _animationDuration,
+ shape: WidgetStateProperty.resolveWith((states) {
+ return RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(
+ states.contains(WidgetState.pressed) ? 12 : 24,
+ ),
+ );
+ }),
+ );
+
+ return AlertDialog(
+ constraints: const BoxConstraints(minWidth: 280, maxWidth: 440),
+ insetPadding: const EdgeInsets.all(24),
+ backgroundColor: colors.surfaceContainerHigh,
+ surfaceTintColor: Colors.transparent,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
+ clipBehavior: Clip.antiAlias,
+ scrollable: true,
+ iconPadding: const EdgeInsets.fromLTRB(24, 24, 24, 0),
+ icon: Center(
+ child: Container(
+ width: 56,
+ height: 56,
+ decoration: BoxDecoration(
+ color: colors.primaryContainer,
+ borderRadius: BorderRadius.circular(20),
+ ),
+ child: Icon(
+ Icons.exit_to_app_rounded,
+ color: colors.onPrimaryContainer,
+ size: 28,
+ ),
+ ),
+ ),
+ titlePadding: const EdgeInsets.fromLTRB(24, 16, 24, 0),
+ title: const Text('关闭 Kazumi?', textAlign: TextAlign.center),
+ titleTextStyle: theme.textTheme.headlineSmall?.copyWith(
+ color: colors.onSurface,
+ fontWeight: FontWeight.w600,
+ ),
+ contentPadding: const EdgeInsets.fromLTRB(24, 16, 24, 16),
+ content: SizedBox(
+ width: 392,
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: [
+ Text(
+ '选择关闭窗口后的操作',
+ textAlign: TextAlign.center,
+ style: theme.textTheme.bodyMedium?.copyWith(
+ color: colors.onSurfaceVariant,
+ ),
+ ),
+ const SizedBox(height: 24),
+ RadioGroup<ExitDialogAction>(
+ groupValue: _action,
+ onChanged: (value) {
+ if (value != null) setState(() => _action = value);
+ },
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ _buildOption(
+ ExitDialogAction.minimizeToTray,
+ borderRadius: const BorderRadius.vertical(
+ top: Radius.circular(20),
+ bottom: Radius.circular(4),
+ ),
+ ),
+ const SizedBox(height: 4),
+ _buildOption(
+ ExitDialogAction.exit,
+ borderRadius: const BorderRadius.vertical(
+ top: Radius.circular(4),
+ bottom: Radius.circular(20),
+ ),
+ ),
+ ],
+ ),
+ ),
+ const SizedBox(height: 16),
+ CheckboxListTile(
+ value: _rememberChoice,
+ onChanged: (value) {
+ setState(() => _rememberChoice = value ?? false);
+ },
+ controlAffinity: ListTileControlAffinity.leading,
+ contentPadding: const EdgeInsets.symmetric(horizontal: 4),
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(12),
+ ),
+ title: Text('记住我的选择', style: theme.textTheme.bodyMedium),
+ subtitle: Text(
+ '下次关闭窗口时不再询问',
+ style: theme.textTheme.bodySmall?.copyWith(
+ color: colors.onSurfaceVariant,
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ actionsPadding: const EdgeInsets.fromLTRB(24, 0, 24, 24),
+ actionsOverflowButtonSpacing: 8,
+ actions: [
+ TextButton(
+ style: buttonStyle,
+ onPressed: () => Navigator.of(context).pop(),
+ child: const Text('取消'),
+ ),
+ FilledButton(
+ style: buttonStyle,
+ onPressed: () => Navigator.of(context).pop(
+ ExitDialogResult(action: _action, rememberChoice: _rememberChoice),
+ ),
+ child: Text(
+ switch (_action) {
+ ExitDialogAction.minimizeToTray => '最小化至托盘',
+ ExitDialogAction.exit => '退出',
+ },
+ ),
+ ),
+ ],
+ );
+ }
+
+ Widget _buildOption(
+ ExitDialogAction action, {
+ required BorderRadius borderRadius,
+ }) {
+ final theme = Theme.of(context);
+ final colors = theme.colorScheme;
+ final (icon, title, description) = switch (action) {
+ ExitDialogAction.minimizeToTray => (
+ Icons.minimize_rounded,
+ '最小化至托盘',
+ '隐藏窗口,继续在后台运行',
+ ),
+ ExitDialogAction.exit => (
+ Icons.power_settings_new_rounded,
+ '退出 Kazumi',
+ '结束运行并关闭应用',
+ ),
+ };
+ final selected = _action == action;
+ final foreground =
+ selected ? colors.onSecondaryContainer : colors.onSurface;
+
+ return Material(
+ color: selected ? colors.secondaryContainer : colors.surfaceContainerLow,
+ borderRadius: borderRadius,
+ clipBehavior: Clip.antiAlias,
+ animationDuration: _animationDuration,
+ child: RadioListTile<ExitDialogAction>(
+ value: action,
+ selected: selected,
+ activeColor: colors.onSecondaryContainer,
+ controlAffinity: ListTileControlAffinity.trailing,
+ contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
+ shape: RoundedRectangleBorder(borderRadius: borderRadius),
+ secondary: Icon(icon, color: foreground),
+ title: Text(
+ title,
+ style: theme.textTheme.titleSmall?.copyWith(color: foreground),
+ ),
+ subtitle: Text(
+ description,
+ style: theme.textTheme.bodySmall?.copyWith(
+ color: selected
+ ? colors.onSecondaryContainer
+ : colors.onSurfaceVariant,
+ ),
+ ),
+ ),
+ );
+ }
+}