feat: add action options for closing the application
This commit is contained in:
69
lib/features/window/widget/window_closing_dialog.dart
Normal file
69
lib/features/window/widget/window_closing_dialog.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hiddify/core/localization/translations.dart';
|
||||
import 'package:hiddify/core/preferences/actions_at_closing.dart';
|
||||
import 'package:hiddify/core/preferences/general_preferences.dart';
|
||||
import 'package:hiddify/features/window/notifier/window_notifier.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
class WindowClosingDialog extends ConsumerStatefulWidget {
|
||||
const WindowClosingDialog({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<WindowClosingDialog> createState() => _WindowClosingDialogState();
|
||||
}
|
||||
|
||||
class _WindowClosingDialogState extends ConsumerState<WindowClosingDialog> {
|
||||
bool remember = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final t = ref.watch(translationsProvider);
|
||||
|
||||
return AlertDialog(
|
||||
title: Text(t.window.alertMessage),
|
||||
content: GestureDetector(
|
||||
onTap: () => setState(() {
|
||||
remember = !remember;
|
||||
}),
|
||||
behavior: HitTestBehavior.translucent,
|
||||
child: Row(
|
||||
children: [
|
||||
Checkbox(
|
||||
value: remember,
|
||||
onChanged: (v) {
|
||||
remember = v ?? remember;
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Text(
|
||||
t.window.remember,
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
if (remember) {
|
||||
ref.read(Preferences.actionAtClose.notifier).update(ActionsAtClosing.exit);
|
||||
}
|
||||
ref.read(windowNotifierProvider.notifier).quit();
|
||||
},
|
||||
child: Text(t.window.close),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () async {
|
||||
if (remember) {
|
||||
ref.read(Preferences.actionAtClose.notifier).update(ActionsAtClosing.hide);
|
||||
}
|
||||
Navigator.of(context).maybePop(false);
|
||||
await ref.read(windowNotifierProvider.notifier).close();
|
||||
},
|
||||
child: Text(t.window.hide),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hiddify/core/localization/translations.dart';
|
||||
import 'package:hiddify/core/preferences/actions_at_closing.dart';
|
||||
import 'package:hiddify/core/preferences/general_preferences.dart';
|
||||
import 'package:hiddify/features/common/adaptive_root_scaffold.dart';
|
||||
import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
|
||||
import 'package:hiddify/features/window/notifier/window_notifier.dart';
|
||||
import 'package:hiddify/features/window/widget/window_closing_dialog.dart';
|
||||
import 'package:hiddify/utils/custom_loggers.dart';
|
||||
import 'package:hiddify/utils/platform_utils.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
@@ -22,6 +23,8 @@ class WindowWrapper extends StatefulHookConsumerWidget {
|
||||
class _WindowWrapperState extends ConsumerState<WindowWrapper> with WindowListener, AppLogger {
|
||||
late AlertDialog closeDialog;
|
||||
|
||||
bool isWindowClosingDialogOpened = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
ref.watch(windowNotifierProvider);
|
||||
@@ -52,27 +55,23 @@ class _WindowWrapperState extends ConsumerState<WindowWrapper> with WindowListen
|
||||
await ref.read(windowNotifierProvider.notifier).close();
|
||||
return;
|
||||
}
|
||||
final t = ref.watch(translationsProvider);
|
||||
|
||||
await showDialog(
|
||||
context: RootScaffold.stateKey.currentContext!,
|
||||
builder: (BuildContext context) => AlertDialog(
|
||||
title: Text(t.window.alertMessage),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () async => await ref.read(windowNotifierProvider.notifier).quit(),
|
||||
child: Text(t.window.close.toUpperCase()),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
Navigator.of(context).maybePop(false);
|
||||
await ref.read(windowNotifierProvider.notifier).close();
|
||||
},
|
||||
child: Text(t.window.hide.toUpperCase()),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
switch (ref.read(Preferences.actionAtClose)) {
|
||||
case ActionsAtClosing.ask:
|
||||
if (isWindowClosingDialogOpened) return;
|
||||
isWindowClosingDialogOpened = true;
|
||||
await showDialog(
|
||||
context: RootScaffold.stateKey.currentContext!,
|
||||
builder: (BuildContext context) => const WindowClosingDialog(),
|
||||
);
|
||||
isWindowClosingDialogOpened = false;
|
||||
|
||||
case ActionsAtClosing.hide:
|
||||
await ref.read(windowNotifierProvider.notifier).close();
|
||||
|
||||
case ActionsAtClosing.exit:
|
||||
await ref.read(windowNotifierProvider.notifier).quit();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user