-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2029 from acterglobal/ben-desktop-tray
Desktop backgrounding support
- Loading branch information
Showing
19 changed files
with
348 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Keep the app alive in background and close to the tray icon only (on supported platforms) |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:acter/common/utils/routes.dart'; | ||
import 'package:acter/router/router.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:tray_manager/tray_manager.dart'; | ||
import 'package:window_manager/window_manager.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
final _log = Logger('a3::desktop'); | ||
|
||
class DesktopSupport extends StatefulWidget { | ||
final Widget child; | ||
|
||
const DesktopSupport({super.key, required this.child}); | ||
|
||
@override | ||
State<DesktopSupport> createState() => _DesktopSupportState(); | ||
} | ||
|
||
class _DesktopSupportState extends State<DesktopSupport> | ||
with WindowListener, TrayListener { | ||
@override | ||
void initState() { | ||
super.initState(); | ||
trayManager.addListener(this); | ||
windowManager.addListener(this); | ||
_initDesktop(); | ||
} | ||
|
||
Future<void> _initDesktop() async { | ||
// Must add this line. | ||
await windowManager.ensureInitialized(); | ||
|
||
WindowOptions windowOptions = const WindowOptions( | ||
title: 'Acter', | ||
titleBarStyle: TitleBarStyle.normal, | ||
); | ||
windowManager.waitUntilReadyToShow(windowOptions, () async { | ||
await windowManager.show(); | ||
await windowManager.focus(); | ||
await windowManager.setPreventClose(true); | ||
}); | ||
|
||
await trayManager.setIcon( | ||
Platform.isWindows | ||
? 'assets/icon/tray_icon.ico' | ||
: 'assets/icon/tray_icon.png', | ||
); | ||
Menu menu = Menu( | ||
items: [ | ||
MenuItem( | ||
key: 'home', | ||
label: 'Home', | ||
), | ||
MenuItem( | ||
key: 'chat', | ||
label: 'Chat', | ||
), | ||
MenuItem( | ||
key: 'activities', | ||
label: 'Activities', | ||
), | ||
MenuItem.separator(), | ||
MenuItem( | ||
key: 'exit_app', | ||
label: 'Exit App', | ||
), | ||
], | ||
); | ||
if (!Platform.isMacOS) { | ||
// the menu crashes on macos if hidden for some reason. | ||
await trayManager.setContextMenu(menu); | ||
} | ||
if (!Platform.isLinux) { | ||
// not supported on linux; | ||
await trayManager.setToolTip('Acter'); | ||
} | ||
} | ||
|
||
@override | ||
void dispose() { | ||
windowManager.removeListener(this); | ||
trayManager.removeListener(this); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return widget.child; | ||
} | ||
|
||
@override | ||
void onWindowClose() async { | ||
bool isPreventClose = await windowManager.isPreventClose(); | ||
if (isPreventClose) { | ||
await windowManager.hide(); | ||
} | ||
} | ||
|
||
@override | ||
void onTrayIconMouseDown() async { | ||
// toggle visiblity | ||
if (await windowManager.isVisible()) { | ||
_log.info('hiding window on toggle'); | ||
await windowManager.hide(); | ||
} else { | ||
_log.info('showing window on toggle'); | ||
await windowManager.show(); | ||
} | ||
} | ||
|
||
@override | ||
void onTrayIconRightMouseDown() async { | ||
// do something | ||
await trayManager.popUpContextMenu(); | ||
} | ||
|
||
@override | ||
void onTrayMenuItemClick(MenuItem menuItem) async { | ||
if (menuItem.key == 'exit_app') { | ||
_log.info('exit app'); | ||
await windowManager.destroy(); | ||
return; | ||
} | ||
|
||
await windowManager.show(); | ||
WidgetsBinding.instance.addPostFrameCallback((Duration duration) async { | ||
if (menuItem.key == 'home') { | ||
_log.info('route home'); | ||
rootNavKey.currentContext!.pushNamed(Routes.main.name); | ||
} else if (menuItem.key == 'chat') { | ||
_log.info('route chat'); | ||
rootNavKey.currentContext!.pushNamed(Routes.chat.name); | ||
} else if (menuItem.key == 'activities') { | ||
_log.info('route activities'); | ||
rootNavKey.currentContext!.pushNamed(Routes.activities.name); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.