Skip to content

Commit fe49f82

Browse files
committed
feat: keep instance on menuBar when pressing close in macOS
1 parent 31fd59e commit fe49f82

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

lib/main.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,29 @@ void main() async {
2525
runApp(const App());
2626
}
2727

28+
class MacWindowListener extends WindowListener {
29+
@override
30+
void onWindowClose() async {
31+
// On macOS, hide the window instead of closing it
32+
if (Platform.isMacOS) {
33+
await windowManager.hide();
34+
}
35+
}
36+
}
37+
2838
Future<void> initWindowManager() async {
2939
await windowManager.ensureInitialized();
3040
WindowOptions windowOptions = const WindowOptions(
3141
size: Size(1000, 780),
3242
);
3343
windowManager.waitUntilReadyToShow(
3444
windowOptions,
35-
() {
36-
windowManager.setTitle("IRNet: freedom does not have a price");
45+
() async {
46+
await windowManager.setTitle("IRNet: freedom does not have a price");
47+
// On macOS, add window listener to handle close button
48+
if (Platform.isMacOS) {
49+
windowManager.addListener(MacWindowListener());
50+
}
3751
},
3852
);
3953
}

macos/Runner/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import FlutterMacOS
44
@main
55
class AppDelegate: FlutterAppDelegate {
66
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
7-
return true
7+
return false
88
}
99

1010
override func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {

0 commit comments

Comments
 (0)