2023-09-02 22:42:01 +03:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
2023-12-01 12:56:24 +03:30
|
|
|
import 'package:hiddify/core/localization/translations.dart';
|
|
|
|
|
import 'package:hiddify/core/model/constants.dart';
|
2023-11-15 18:48:48 +03:30
|
|
|
import 'package:hiddify/core/router/router.dart';
|
2023-07-15 18:00:44 +03:30
|
|
|
import 'package:hiddify/features/common/window/window_controller.dart';
|
2023-12-01 12:56:24 +03:30
|
|
|
import 'package:hiddify/features/config_option/model/config_option_patch.dart';
|
|
|
|
|
import 'package:hiddify/features/config_option/notifier/config_option_notifier.dart';
|
|
|
|
|
import 'package:hiddify/features/connection/model/connection_status.dart';
|
|
|
|
|
import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/gen/assets.gen.dart';
|
2023-12-01 12:56:24 +03:30
|
|
|
import 'package:hiddify/singbox/model/singbox_config_enum.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/utils/utils.dart';
|
|
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
|
import 'package:tray_manager/tray_manager.dart';
|
|
|
|
|
|
|
|
|
|
part 'system_tray_controller.g.dart';
|
|
|
|
|
|
|
|
|
|
@Riverpod(keepAlive: true)
|
|
|
|
|
class SystemTrayController extends _$SystemTrayController
|
|
|
|
|
with TrayListener, AppLogger {
|
|
|
|
|
@override
|
|
|
|
|
Future<void> build() async {
|
2023-07-15 18:00:44 +03:30
|
|
|
if (!_initialized) {
|
|
|
|
|
loggy.debug('initializing');
|
2023-09-02 22:42:01 +03:30
|
|
|
await trayManager.setIcon(
|
|
|
|
|
_trayIconPath,
|
|
|
|
|
isTemplate: Platform.isMacOS,
|
|
|
|
|
);
|
2023-09-14 15:20:48 +03:30
|
|
|
if (!Platform.isLinux) await trayManager.setToolTip(Constants.appName);
|
2023-07-15 18:00:44 +03:30
|
|
|
trayManager.addListener(this);
|
|
|
|
|
_initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 12:56:24 +03:30
|
|
|
final connection = switch (ref.watch(connectionNotifierProvider)) {
|
2023-11-12 21:55:17 +03:30
|
|
|
AsyncData(:final value) => value,
|
|
|
|
|
_ => const Disconnected(),
|
|
|
|
|
};
|
2023-12-01 12:56:24 +03:30
|
|
|
final serviceMode = await ref
|
|
|
|
|
.watch(configOptionNotifierProvider.future)
|
|
|
|
|
.then((value) => value.serviceMode);
|
2023-07-15 18:00:44 +03:30
|
|
|
|
2023-11-01 22:02:13 +03:30
|
|
|
final t = ref.watch(translationsProvider);
|
2023-11-15 18:48:48 +03:30
|
|
|
final destinations = <(String label, String location)>[
|
|
|
|
|
(t.home.pageTitle, const HomeRoute().location),
|
|
|
|
|
(t.proxies.pageTitle, const ProxiesRoute().location),
|
2023-11-28 18:24:31 +03:30
|
|
|
(t.logs.pageTitle, const LogsOverviewRoute().location),
|
2023-11-15 18:48:48 +03:30
|
|
|
(t.settings.pageTitle, const SettingsRoute().location),
|
|
|
|
|
(t.about.pageTitle, const AboutRoute().location),
|
|
|
|
|
];
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-11-01 22:02:13 +03:30
|
|
|
loggy.debug('updating system tray');
|
2023-09-02 22:42:01 +03:30
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
final trayMenu = Menu(
|
|
|
|
|
items: [
|
2023-07-12 23:20:06 +03:30
|
|
|
MenuItem(
|
2023-07-06 17:18:41 +03:30
|
|
|
label: t.tray.dashboard,
|
|
|
|
|
onClick: handleClickShowApp,
|
|
|
|
|
),
|
|
|
|
|
MenuItem.separator(),
|
|
|
|
|
MenuItem.checkbox(
|
2023-11-01 22:02:13 +03:30
|
|
|
label: switch (connection) {
|
|
|
|
|
Disconnected() => t.tray.status.connect,
|
|
|
|
|
Connecting() => t.tray.status.connecting,
|
|
|
|
|
Connected() => t.tray.status.disconnect,
|
|
|
|
|
Disconnecting() => t.tray.status.disconnecting,
|
|
|
|
|
},
|
2023-07-06 17:18:41 +03:30
|
|
|
checked: connection.isConnected,
|
|
|
|
|
disabled: connection.isSwitching,
|
|
|
|
|
onClick: handleClickSetAsSystemProxy,
|
|
|
|
|
),
|
2023-11-01 22:02:13 +03:30
|
|
|
MenuItem.submenu(
|
2023-11-11 23:10:58 +03:30
|
|
|
label: t.settings.config.serviceMode,
|
2023-11-01 22:02:13 +03:30
|
|
|
submenu: Menu(
|
|
|
|
|
items: [
|
2023-11-11 23:10:58 +03:30
|
|
|
...ServiceMode.values.map(
|
2023-11-01 22:02:13 +03:30
|
|
|
(e) => MenuItem.checkbox(
|
2023-11-11 23:10:58 +03:30
|
|
|
checked: e == serviceMode,
|
2023-11-01 22:02:13 +03:30
|
|
|
key: e.name,
|
|
|
|
|
label: e.present(t),
|
|
|
|
|
onClick: (menuItem) async {
|
2023-11-11 23:10:58 +03:30
|
|
|
final newMode = ServiceMode.values.byName(menuItem.key!);
|
|
|
|
|
loggy.debug("switching service mode: [$newMode]");
|
2023-11-01 22:02:13 +03:30
|
|
|
await ref
|
2023-12-01 12:56:24 +03:30
|
|
|
.read(configOptionNotifierProvider.notifier)
|
|
|
|
|
.updateOption(ConfigOptionPatch(serviceMode: newMode));
|
2023-11-01 22:02:13 +03:30
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
2023-11-15 18:48:48 +03:30
|
|
|
MenuItem.submenu(
|
|
|
|
|
label: t.tray.open,
|
|
|
|
|
submenu: Menu(
|
|
|
|
|
items: [
|
|
|
|
|
...destinations.map(
|
|
|
|
|
(e) => MenuItem(
|
|
|
|
|
label: e.$1,
|
|
|
|
|
onClick: (_) async {
|
|
|
|
|
await ref.read(windowControllerProvider.notifier).show();
|
|
|
|
|
ref.read(routerProvider).go(e.$2);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
2023-07-06 17:18:41 +03:30
|
|
|
MenuItem.separator(),
|
|
|
|
|
MenuItem(
|
|
|
|
|
label: t.tray.quit,
|
|
|
|
|
onClick: handleClickExitApp,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
await trayManager.setContextMenu(trayMenu);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 22:02:13 +03:30
|
|
|
bool _initialized = false;
|
|
|
|
|
|
|
|
|
|
String get _trayIconPath {
|
|
|
|
|
if (Platform.isWindows) return Assets.images.trayIconIco;
|
|
|
|
|
return Assets.images.trayIconPng.path;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 23:20:06 +03:30
|
|
|
@override
|
|
|
|
|
Future<void> onTrayIconMouseDown() async {
|
2023-12-09 15:32:52 +03:30
|
|
|
if (Platform.isMacOS) {
|
|
|
|
|
await trayManager.popUpContextMenu();
|
|
|
|
|
} else {
|
|
|
|
|
await ref.read(windowControllerProvider.notifier).show();
|
|
|
|
|
}
|
2023-07-12 23:20:06 +03:30
|
|
|
}
|
|
|
|
|
|
2023-07-06 17:18:41 +03:30
|
|
|
@override
|
|
|
|
|
Future<void> onTrayIconRightMouseDown() async {
|
|
|
|
|
super.onTrayIconRightMouseDown();
|
|
|
|
|
await trayManager.popUpContextMenu();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> handleClickShowApp(MenuItem menuItem) async {
|
2023-07-15 18:00:44 +03:30
|
|
|
await ref.read(windowControllerProvider.notifier).show();
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> handleClickSetAsSystemProxy(MenuItem menuItem) async {
|
2023-12-01 12:56:24 +03:30
|
|
|
return ref.read(connectionNotifierProvider.notifier).toggleConnection();
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> handleClickExitApp(MenuItem menuItem) async {
|
2023-12-01 12:56:24 +03:30
|
|
|
await ref.read(connectionNotifierProvider.notifier).abortConnection();
|
2023-08-19 22:27:23 +03:30
|
|
|
await trayManager.destroy();
|
|
|
|
|
return ref.read(windowControllerProvider.notifier).quit();
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|
|
|
|
|
}
|