116 lines
2.7 KiB
Dart
116 lines
2.7 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:nativeapi/nativeapi.dart' as nativeapi;
|
|
import 'package:window_manager/window_manager.dart';
|
|
|
|
import 'tray_service.dart';
|
|
|
|
class LinuxTrayService implements TrayService {
|
|
LinuxTrayService();
|
|
|
|
nativeapi.TrayIcon? _trayIcon;
|
|
nativeapi.Image? _icon;
|
|
nativeapi.Menu? _menu;
|
|
bool _initialized = false;
|
|
|
|
@override
|
|
bool get isAvailable => Platform.isLinux;
|
|
|
|
@override
|
|
Future<void> init() async {
|
|
if (!isAvailable || _initialized) {
|
|
return;
|
|
}
|
|
if (!nativeapi.TrayManager.instance.isSupported) {
|
|
return;
|
|
}
|
|
|
|
_icon = nativeapi.Image.fromAsset('assets/tray.png');
|
|
final trayIcon = nativeapi.TrayIcon();
|
|
trayIcon.title = 'Peya Wallet';
|
|
trayIcon.tooltip = 'Peya Wallet';
|
|
trayIcon.icon = _icon;
|
|
trayIcon.contextMenuTrigger = nativeapi.ContextMenuTrigger.rightClicked;
|
|
trayIcon.on<nativeapi.TrayIconClickedEvent>((_) async {
|
|
await showWindow();
|
|
});
|
|
trayIcon.on<nativeapi.TrayIconDoubleClickedEvent>((_) async {
|
|
await showWindow();
|
|
});
|
|
trayIcon.isVisible = true;
|
|
_trayIcon = trayIcon;
|
|
_initialized = true;
|
|
}
|
|
|
|
@override
|
|
Future<void> updateMenu({
|
|
required bool isWindowVisible,
|
|
required String showLabel,
|
|
required String hideLabel,
|
|
required String syncLabel,
|
|
required String quitLabel,
|
|
required void Function() onToggleVisibility,
|
|
required void Function() onSyncNow,
|
|
required void Function() onQuit,
|
|
}) async {
|
|
if (!_initialized || _trayIcon == null) {
|
|
return;
|
|
}
|
|
|
|
_menu?.dispose();
|
|
|
|
final menu = nativeapi.Menu();
|
|
final toggleItem = nativeapi.MenuItem(
|
|
isWindowVisible ? hideLabel : showLabel,
|
|
);
|
|
toggleItem.on<nativeapi.MenuItemClickedEvent>((_) {
|
|
onToggleVisibility();
|
|
});
|
|
menu.addItem(toggleItem);
|
|
|
|
final syncItem = nativeapi.MenuItem(syncLabel);
|
|
syncItem.on<nativeapi.MenuItemClickedEvent>((_) {
|
|
onSyncNow();
|
|
});
|
|
menu.addItem(syncItem);
|
|
|
|
menu.addSeparator();
|
|
|
|
final quitItem = nativeapi.MenuItem(quitLabel);
|
|
quitItem.on<nativeapi.MenuItemClickedEvent>((_) {
|
|
onQuit();
|
|
});
|
|
menu.addItem(quitItem);
|
|
|
|
_trayIcon!.contextMenu = menu;
|
|
_menu = menu;
|
|
}
|
|
|
|
@override
|
|
Future<void> showWindow() async {
|
|
await windowManager.setSkipTaskbar(false);
|
|
if (await windowManager.isMinimized()) {
|
|
await windowManager.restore();
|
|
}
|
|
await windowManager.show();
|
|
await windowManager.focus();
|
|
}
|
|
|
|
@override
|
|
Future<void> hideWindow() async {
|
|
await windowManager.setSkipTaskbar(true);
|
|
await windowManager.hide();
|
|
}
|
|
|
|
@override
|
|
Future<void> dispose() async {
|
|
_menu?.dispose();
|
|
_trayIcon?.dispose();
|
|
_icon?.dispose();
|
|
_menu = null;
|
|
_trayIcon = null;
|
|
_icon = null;
|
|
_initialized = false;
|
|
}
|
|
}
|