optimize closeConfirmDialog by using async onWindowCloseButton

This commit is contained in:
csf 2022-09-09 19:29:19 +08:00
parent d939a5ebc6
commit 7fce018eea
4 changed files with 63 additions and 41 deletions

View File

@ -75,9 +75,7 @@ class _FileManagerTabPageState extends State<FileManagerTabPage> {
backgroundColor: MyTheme.color(context).bg,
body: DesktopTab(
controller: tabController,
onWindowCloseButton: () {
tabController.clear();
},
onWindowCloseButton: handleWindowCloseButton,
tail: const AddButton().paddingOnly(left: 10),
)),
),
@ -103,4 +101,21 @@ class _FileManagerTabPageState extends State<FileManagerTabPage> {
tabController.closeBy(peerId);
}
}
Future<bool> handleWindowCloseButton() async {
final connLength = tabController.state.value.tabs.length;
if (connLength < 1) {
return true;
} else if (connLength == 1) {
final currentConn = tabController.state.value.tabs[0];
handleTabCloseButton(currentConn.key);
return false;
} else {
final res = await closeConfirmDialog();
if (res) {
tabController.clear();
}
return res;
}
}
}

View File

@ -78,8 +78,9 @@ class _PortForwardTabPageState extends State<PortForwardTabPage> {
backgroundColor: MyTheme.color(context).bg,
body: DesktopTab(
controller: tabController,
onWindowCloseButton: () {
onWindowCloseButton: () async {
tabController.clear();
return true;
},
tail: AddButton().paddingOnly(left: 10),
)),

View File

@ -97,9 +97,7 @@ class _ConnectionTabPageState extends State<ConnectionTabPage> {
body: DesktopTab(
controller: tabController,
showTabBar: fullscreen.isFalse,
onWindowCloseButton: () {
tabController.clear();
},
onWindowCloseButton: handleWindowCloseButton,
tail: AddButton().paddingOnly(left: 10),
pageViewBuilder: (pageView) {
WindowController.fromWindowId(windowId())
@ -167,4 +165,21 @@ class _ConnectionTabPageState extends State<ConnectionTabPage> {
tabController.closeBy(peerId);
}
}
Future<bool> handleWindowCloseButton() async {
final connLength = tabController.state.value.tabs.length;
if (connLength < 1) {
return true;
} else if (connLength == 1) {
final currentConn = tabController.state.value.tabs[0];
handleTabCloseButton(currentConn.key);
return false;
} else {
final res = await closeConfirmDialog();
if (res) {
tabController.clear();
}
return res;
}
}
}

View File

@ -183,7 +183,7 @@ class DesktopTab extends StatelessWidget {
final bool showClose;
final Widget Function(Widget pageView)? pageViewBuilder;
final Widget? tail;
final VoidCallback? onWindowCloseButton;
final Future<bool> Function()? onWindowCloseButton;
final TabBuilder? tabBuilder;
final LabelGetter? labelGetter;
@ -356,7 +356,7 @@ class WindowActionPanel extends StatelessWidget {
final bool showMinimize;
final bool showMaximize;
final bool showClose;
final VoidCallback? onClose;
final Future<bool> Function()? onClose;
const WindowActionPanel(
{Key? key,
@ -434,7 +434,8 @@ class WindowActionPanel extends StatelessWidget {
message: 'Close',
icon: IconFont.close,
onTap: () async {
action() {
final res = await onClose?.call() ?? true;
if (res) {
if (mainTab) {
windowManager.close();
} else {
@ -443,14 +444,6 @@ class WindowActionPanel extends StatelessWidget {
WindowController.fromWindowId(windowId!).hide();
});
}
onClose?.call();
}
if (tabType != DesktopTabType.main &&
state.value.tabs.length > 1) {
closeConfirmDialog(action);
} else {
action();
}
},
isClose: true,
@ -458,30 +451,28 @@ class WindowActionPanel extends StatelessWidget {
],
);
}
}
closeConfirmDialog(Function() callback) async {
final res = await gFFI.dialogManager.show<bool>((setState, close) {
submit() => close(true);
return CustomAlertDialog(
title: Row(children: [
const Icon(Icons.warning_amber_sharp,
color: Colors.redAccent, size: 28),
const SizedBox(width: 10),
Text(translate("Warning")),
]),
content: Text(translate("Disconnect all devices?")),
actions: [
TextButton(onPressed: close, child: Text(translate("Cancel"))),
ElevatedButton(onPressed: submit, child: Text(translate("OK"))),
],
onSubmit: submit,
onCancel: close,
);
});
if (res == true) {
callback();
}
}
Future<bool> closeConfirmDialog() async {
final res = await gFFI.dialogManager.show<bool>((setState, close) {
submit() => close(true);
return CustomAlertDialog(
title: Row(children: [
const Icon(Icons.warning_amber_sharp,
color: Colors.redAccent, size: 28),
const SizedBox(width: 10),
Text(translate("Warning")),
]),
content: Text(translate("Disconnect all devices?")),
actions: [
TextButton(onPressed: close, child: Text(translate("Cancel"))),
ElevatedButton(onPressed: submit, child: Text(translate("OK"))),
],
onSubmit: submit,
onCancel: close,
);
});
return res == true;
}
// ignore: must_be_immutable