feat: add flutter tray and hide logic of main window

This commit is contained in:
Kingtous
2022-11-02 10:22:41 +08:00
parent 3f8c7fd4f4
commit 4f301bd0b5
5 changed files with 36 additions and 17 deletions

View File

@@ -13,6 +13,7 @@ import 'package:flutter_hbb/desktop/widgets/scroll_wrapper.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:flutter_hbb/models/server_model.dart';
import 'package:flutter_hbb/utils/multi_window_manager.dart';
import 'package:flutter_hbb/utils/tray_manager.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tray_manager/tray_manager.dart';
@@ -395,14 +396,21 @@ class _DesktopHomePageState extends State<DesktopHomePage>
);
}
@override
void onTrayIconMouseDown() {
windowManager.show();
windowManager.focus();
}
@override
void onTrayMenuItemClick(MenuItem menuItem) {
debugPrint('click ${menuItem.key}');
switch (menuItem.key) {
case "quit":
exit(0);
case "show":
// windowManager.show();
case kTrayItemQuitKey:
windowManager.close();
break;
case kTrayItemShowKey:
windowManager.show();
windowManager.focus();
break;
default:
break;
@@ -416,6 +424,7 @@ class _DesktopHomePageState extends State<DesktopHomePage>
updateUrl = await bind.mainGetSoftwareUpdateUrl();
if (updateUrl.isNotEmpty) setState(() {});
});
initTray();
trayManager.addListener(this);
windowManager.addListener(this);
rustDeskWinManager.setMethodHandler((call, fromWindowId) async {
@@ -456,6 +465,7 @@ class _DesktopHomePageState extends State<DesktopHomePage>
@override
void dispose() {
destoryTray();
trayManager.removeListener(this);
windowManager.removeListener(this);
_uniLinksSubscription?.cancel();

View File

@@ -509,14 +509,15 @@ class WindowActionPanelState extends State<WindowActionPanel>
onTap: () async {
final res = await widget.onClose?.call() ?? true;
if (res) {
if (widget.isMainWindow) {
windowManager.close();
} else {
// only hide for multi window, not close
Future.delayed(Duration.zero, () {
// hide for all window
// note: the main window can be restored by tray icon
Future.delayed(Duration.zero, () {
if (widget.isMainWindow) {
windowManager.hide();
} else {
WindowController.fromWindowId(windowId!).hide();
});
}
}
});
}
},
isClose: true,

View File

@@ -101,8 +101,6 @@ void runMainApp(bool startService) async {
await bind.mainCheckConnectStatus();
if (startService) {
// await windowManager.ensureInitialized();
// disable tray
// initTray();
gFFI.serverModel.startService();
}
runApp(App());

View File

@@ -4,11 +4,14 @@ import 'package:tray_manager/tray_manager.dart';
import '../common.dart';
const kTrayItemShowKey = "show";
const kTrayItemQuitKey = "quit";
Future<void> initTray({List<MenuItem>? extra_item}) async {
List<MenuItem> items = [
MenuItem(key: "show", label: translate("show rustdesk")),
MenuItem(key: kTrayItemShowKey, label: translate("Show RustDesk")),
MenuItem.separator(),
MenuItem(key: "quit", label: translate("quit rustdesk")),
MenuItem(key: kTrayItemQuitKey, label: translate("Quit")),
];
if (extra_item != null) {
items.insertAll(0, extra_item);
@@ -17,7 +20,13 @@ Future<void> initTray({List<MenuItem>? extra_item}) async {
trayManager
.setIcon(Platform.isWindows ? "assets/logo.ico" : "assets/logo.png"),
trayManager.setContextMenu(Menu(items: items)),
trayManager.setToolTip("rustdesk"),
trayManager.setTitle("rustdesk")
]);
if (Platform.isMacOS || Platform.isWindows) {
await trayManager.setToolTip("rustdesk");
}
}
Future<void> destoryTray() async {
return trayManager.destroy();
}