-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathsystem_tray.dart
72 lines (64 loc) · 1.64 KB
/
system_tray.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';
import '../../constants.dart';
import '../../l10n/l10n.dart';
const _exitAppMenuKey = 'exit_app';
const _showHideWindowMenuKey = 'show_hide_window';
Future<void> initTray() async {
await trayManager.setIcon(_icon());
Menu menu = Menu(
items: [
MenuItem(
key: _showHideWindowMenuKey,
label: 'Show Window',
),
MenuItem.separator(),
MenuItem(
key: _exitAppMenuKey,
label: 'Exit App',
),
],
);
await trayManager.setContextMenu(menu);
}
String _icon() {
if (Platform.isLinux) {
return kAppId;
} else if (Platform.isWindows) {
return 'assets/images/tray_icon.ico';
} else {
return 'assets/images/tray_icon.png';
}
}
Future<void> updateTrayItems(BuildContext context) async {
bool isVisible = await windowManager.isVisible();
if (!context.mounted) return;
final trayMenuItems = [
MenuItem(
key: _showHideWindowMenuKey,
label: isVisible ? context.l10n.hideToTray : context.l10n.closeApp,
),
MenuItem.separator(),
MenuItem(
key: _exitAppMenuKey,
label: context.l10n.closeApp,
),
];
await trayManager.setContextMenu(Menu(items: trayMenuItems));
}
void reactToTray(MenuItem menuItem) {
switch (menuItem.key) {
case _showHideWindowMenuKey:
windowManager.isVisible().then((value) {
if (value) {
windowManager.hide();
} else {
windowManager.show();
}
});
case _exitAppMenuKey:
windowManager.close();
}
}