80 lines
1.8 KiB
Dart
80 lines
1.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/services.dart';
|
|
import 'package:tray_manager/tray_manager.dart';
|
|
|
|
typedef TrayActionCallback = Future<void> Function();
|
|
|
|
final class TrayService with TrayListener {
|
|
TrayService({
|
|
required TrayActionCallback onShow,
|
|
required TrayActionCallback onHide,
|
|
required TrayActionCallback onQuit,
|
|
}) : _onShow = onShow,
|
|
_onHide = onHide,
|
|
_onQuit = onQuit;
|
|
|
|
final TrayActionCallback _onShow;
|
|
final TrayActionCallback _onHide;
|
|
final TrayActionCallback _onQuit;
|
|
|
|
bool _isInitialized = false;
|
|
|
|
Future<void> init() async {
|
|
if (_isInitialized) {
|
|
return;
|
|
}
|
|
|
|
trayManager.addListener(this);
|
|
await trayManager.setIcon('assets/logo.png');
|
|
try {
|
|
await trayManager.setToolTip('Quasar Jet VPN');
|
|
} on MissingPluginException {
|
|
// Linux backend does not implement this method.
|
|
}
|
|
await trayManager.setContextMenu(
|
|
Menu(
|
|
items: <MenuItem>[
|
|
MenuItem(key: 'show', label: 'Show'),
|
|
MenuItem(key: 'hide', label: 'Hide'),
|
|
MenuItem.separator(),
|
|
MenuItem(key: 'quit', label: 'Quit'),
|
|
],
|
|
),
|
|
);
|
|
|
|
_isInitialized = true;
|
|
}
|
|
|
|
@override
|
|
void onTrayIconMouseDown() {
|
|
unawaited(trayManager.popUpContextMenu());
|
|
}
|
|
|
|
@override
|
|
void onTrayMenuItemClick(MenuItem menuItem) {
|
|
final String key = menuItem.key ?? '';
|
|
if (key == 'show') {
|
|
unawaited(_onShow());
|
|
return;
|
|
}
|
|
if (key == 'hide') {
|
|
unawaited(_onHide());
|
|
return;
|
|
}
|
|
if (key == 'quit') {
|
|
unawaited(_onQuit());
|
|
}
|
|
}
|
|
|
|
Future<void> dispose() async {
|
|
if (!_isInitialized) {
|
|
return;
|
|
}
|
|
|
|
trayManager.removeListener(this);
|
|
await trayManager.destroy();
|
|
_isInitialized = false;
|
|
}
|
|
}
|