From 7b39de54483196422732009a5cb50e2943317c51 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Sun, 4 Aug 2024 18:09:05 +0100 Subject: [PATCH] Add tray icon support --- app/lib/config/desktop.dart | 90 ++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/app/lib/config/desktop.dart b/app/lib/config/desktop.dart index 797216283a0f..ca677a60f275 100644 --- a/app/lib/config/desktop.dart +++ b/app/lib/config/desktop.dart @@ -1,12 +1,22 @@ +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:flutter/material.dart' hide MenuItem; +import 'package:flutter/services.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'); Future initDesktop() async { // Must add this line. await windowManager.ensureInitialized(); - WindowOptions windowOptions = WindowOptions( + WindowOptions windowOptions = const WindowOptions( title: 'Acter', titleBarStyle: TitleBarStyle.normal, ); @@ -15,6 +25,39 @@ Future initDesktop() async { await windowManager.focus(); await windowManager.setPreventClose(true); }); + + await trayManager.setIcon( + Platform.isWindows ? 'images/tray_icon.ico' : 'images/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', + onClick: (item) { + windowManager.destroy(); + },), + ], + ); + await trayManager.setContextMenu(menu); + try { + trayManager.setToolTip('Acter'); + } on MissingPluginException { + _log.warning('Setting Tray tooltip not supported on this platform'); + } } class DesktopSupport extends StatefulWidget { @@ -26,10 +69,12 @@ class DesktopSupport extends StatefulWidget { State createState() => _DesktopSupportState(); } -class _DesktopSupportState extends State with WindowListener { +class _DesktopSupportState extends State + with WindowListener, TrayListener { @override void initState() { super.initState(); + trayManager.addListener(this); windowManager.addListener(this); } @@ -46,9 +91,44 @@ class _DesktopSupportState extends State with WindowListener { @override void onWindowClose() async { - bool _isPreventClose = await windowManager.isPreventClose(); - if (_isPreventClose) { + bool isPreventClose = await windowManager.isPreventClose(); + if (isPreventClose) { windowManager.hide(); } } + + // @override + // void onTrayIconMouseUp() async { + // // toggle visiblity + // print("toggled"); + // if (await windowManager.isVisible()) { + // windowManager.hide(); + // } else { + // windowManager.show(); + // } + // } + + // @override + // void onTrayIconRightMouseDown() { + // // do something + // trayManager.popUpContextMenu(); + // } + + @override + void onTrayMenuItemClick(MenuItem menuItem) { + if (menuItem.key == 'exit_app') { + windowManager.destroy(); + return; + } + + // we want to show the window on all other clicks + windowManager.show(); + if (menuItem.key == 'home') { + rootNavKey.currentContext!.pushNamed(Routes.main.name); + } else if (menuItem.key == 'chat') { + rootNavKey.currentContext!.pushNamed(Routes.chat.name); + } else if (menuItem.key == 'activities') { + rootNavKey.currentContext!.pushNamed(Routes.activities.name); + } + } }