Skip to content

Commit

Permalink
Add tray icon support
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed Aug 11, 2024
1 parent 2311a09 commit 7b39de5
Showing 1 changed file with 85 additions and 5 deletions.
90 changes: 85 additions & 5 deletions app/lib/config/desktop.dart
Original file line number Diff line number Diff line change
@@ -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<void> initDesktop() async {
// Must add this line.
await windowManager.ensureInitialized();

WindowOptions windowOptions = WindowOptions(
WindowOptions windowOptions = const WindowOptions(
title: 'Acter',
titleBarStyle: TitleBarStyle.normal,
);
Expand All @@ -15,6 +25,39 @@ Future<void> 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 {
Expand All @@ -26,10 +69,12 @@ class DesktopSupport extends StatefulWidget {
State<DesktopSupport> createState() => _DesktopSupportState();
}

class _DesktopSupportState extends State<DesktopSupport> with WindowListener {
class _DesktopSupportState extends State<DesktopSupport>
with WindowListener, TrayListener {
@override
void initState() {
super.initState();
trayManager.addListener(this);
windowManager.addListener(this);
}

Expand All @@ -46,9 +91,44 @@ class _DesktopSupportState extends State<DesktopSupport> 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);
}
}
}

0 comments on commit 7b39de5

Please sign in to comment.