Skip to content

Commit

Permalink
Show closing confirmation when window isn't closed from button
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Sep 30, 2023
1 parent 0700d7b commit 0f8fc3d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
47 changes: 30 additions & 17 deletions lib/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DashboardPage extends StatefulWidget {
State<DashboardPage> createState() => _DashboardPageState();
}

class _DashboardPageState extends State<DashboardPage> {
class _DashboardPageState extends State<DashboardPage> with WindowListener {
late final SharedPreferences _preferences;
late final UpdateChecker updateChecker;

Expand All @@ -60,6 +60,9 @@ class _DashboardPageState extends State<DashboardPage> {
_preferences = widget.preferences;
updateChecker = UpdateChecker(currentVersion: widget.version);

windowManager.addListener(this);
Future(() async => await windowManager.setPreventClose(true));

loadLayout();

if (tabData.isEmpty) {
Expand Down Expand Up @@ -154,6 +157,29 @@ class _DashboardPageState extends State<DashboardPage> {
Future(() => checkForUpdates(notifyIfLatest: false));
}

@override
void onWindowClose() async {
Map<String, dynamic> savedJson =
jsonDecode(_preferences.getString(PrefKeys.layout) ?? '{}');
Map<String, dynamic> currentJson = toJson();

bool showConfirmation =
!const DeepCollectionEquality().equals(savedJson, currentJson);

if (showConfirmation) {
showCloseConfirmation(context);
await windowManager.focus();
} else {
await windowManager.destroy();
}
}

@override
void dispose() async {
windowManager.removeListener(this);
super.dispose();
}

Map<String, dynamic> toJson() {
List<Map<String, dynamic>> gridData = [];

Expand Down Expand Up @@ -558,14 +584,14 @@ class _DashboardPageState extends State<DashboardPage> {

Future.delayed(
const Duration(milliseconds: 250),
() async => await windowManager.close(),
() async => await windowManager.destroy(),
);
},
child: const Text('Save'),
),
TextButton(
onPressed: () async {
await windowManager.close();
await windowManager.destroy();
},
child: const Text('Discard'),
),
Expand Down Expand Up @@ -720,20 +746,7 @@ class _DashboardPageState extends State<DashboardPage> {

return Scaffold(
appBar: CustomAppBar(
onWindowClose: () async {
Map<String, dynamic> savedJson =
jsonDecode(_preferences.getString(PrefKeys.layout) ?? '{}');
Map<String, dynamic> currentJson = toJson();

bool showConfirmation =
!const DeepCollectionEquality().equals(savedJson, currentJson);

if (showConfirmation) {
showCloseConfirmation(context);
} else {
await windowManager.close();
}
},
onWindowClose: onWindowClose,
menuBar: menuBar,
),
body: CallbackShortcuts(
Expand Down
20 changes: 15 additions & 5 deletions test/pages/dashboard_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@ void main() {

expect(minimizeButton, findsOneWidget);

await widgetTester.tap(minimizeButton);
final minimizeButtonWidget =
minimizeButton.evaluate().first.widget as DecoratedMinimizeButton;

minimizeButtonWidget.onPressed?.call();
});

testWidgets('Maximizing/unmaximizing window', (widgetTester) async {
Expand Down Expand Up @@ -516,9 +519,10 @@ void main() {

final maximizeButton = find.byType(DecoratedMaximizeButton);

expect(maximizeButton, findsOneWidget);
final maximizeButtonWidget =
maximizeButton.evaluate().first.widget as DecoratedMaximizeButton;

await widgetTester.tap(maximizeButton);
maximizeButtonWidget.onPressed?.call();
});

testWidgets('Closing window (All changes saved)', (widgetTester) async {
Expand Down Expand Up @@ -553,7 +557,10 @@ void main() {

expect(closeButton, findsOneWidget);

await widgetTester.tap(closeButton);
final closeButtonWidget =
closeButton.evaluate().first.widget as DecoratedCloseButton;

closeButtonWidget.onPressed?.call();
await widgetTester.pumpAndSettle();

expect(find.widgetWithText(AlertDialog, 'Unsaved Changes'), findsNothing);
Expand Down Expand Up @@ -588,7 +595,10 @@ void main() {

expect(closeButton, findsOneWidget);

await widgetTester.tap(closeButton);
final closeButtonWidget =
closeButton.evaluate().first.widget as DecoratedCloseButton;

closeButtonWidget.onPressed?.call();
await widgetTester.pumpAndSettle();

expect(find.widgetWithText(AlertDialog, 'Unsaved Changes'), findsOneWidget);
Expand Down

0 comments on commit 0f8fc3d

Please sign in to comment.