forked from gsmlg-dev/ansible-semaphore-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.dart
103 lines (88 loc) · 3.13 KB
/
app.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:semaphore/constants.dart';
import 'package:semaphore/database/database.dart';
import 'package:semaphore/database/schema/app_activities.dart';
import 'package:semaphore/router/router.dart';
import 'package:semaphore/state/theme.dart';
import 'package:semaphore/adaptive/app.dart';
import 'package:system_theme/system_theme.dart';
import 'package:system_tray/system_tray.dart';
class App extends ConsumerStatefulWidget {
const App({Key? key}) : super(key: key);
@override
ConsumerState<App> createState() => _AppState();
}
class _AppState extends ConsumerState<App> with WidgetsBindingObserver {
@override
initState() {
super.initState();
ref
.read(localAppThemeDataProvider.notifier)
.saveSeedColor(SystemTheme.accentColor.accent);
if (Platform.isMacOS || Platform.isWindows || Platform.isLinux) {
initSystemTray();
}
WidgetsBinding.instance.addObserver(this);
if (WidgetsBinding.instance.lifecycleState != null) {
print(WidgetsBinding.instance.lifecycleState!);
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
final appActivity = AppActivities()
..state = state
..createdAt = DateTime.now();
await Database().instance.writeTxn(() async {
await Database().instance.appActivities.put(appActivity);
});
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Future<void> initSystemTray() async {
String path =
Platform.isWindows ? 'assets/icon/icon.png' : 'assets/icon/icon.png';
final AppWindow appWindow = AppWindow();
final SystemTray systemTray = SystemTray();
// We first init the systray menu
await systemTray.initSystemTray(
toolTip: 'Ansible Semaphore Client',
iconPath: path,
);
// create context menu
final Menu menu = Menu();
await menu.buildFrom([
MenuItemLabel(label: 'Show', onClicked: (menuItem) => appWindow.show()),
MenuItemLabel(label: 'Hide', onClicked: (menuItem) => appWindow.hide()),
MenuItemLabel(label: 'Exit', onClicked: (menuItem) => appWindow.close()),
]);
// set context menu
await systemTray.setContextMenu(menu);
// handle system tray event
systemTray.registerSystemTrayEventHandler((eventName) {
debugPrint('eventName: $eventName');
if (eventName == kSystemTrayEventClick) {
Platform.isWindows ? appWindow.show() : systemTray.popUpContextMenu();
} else if (eventName == kSystemTrayEventRightClick) {
Platform.isWindows ? systemTray.popUpContextMenu() : appWindow.show();
}
});
}
@override
Widget build(BuildContext context) {
final router = ref.watch(routerProvider);
final themeMode = ref.watch(themeModeProvider);
final accentColor = SystemTheme.accentColor.accent;
return AdaptiveApp.router(
accentColor: accentColor,
debugShowCheckedModeBanner: false,
routerConfig: router,
title: Constants.appName,
themeMode: themeMode,
);
}
}