import 'package:get/get.dart'; import 'package:naiyouwl/app/bean/proxie.dart'; import 'package:naiyouwl/app/controller/controllers.dart'; import 'package:naiyouwl/app/utils/utils.dart'; import 'package:tray_manager/tray_manager.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:window_manager/window_manager.dart'; class TrayController extends GetxController with TrayListener { late Menu trayMenu; var show = false.obs; Future initTray() async { await trayManager.setIcon('assets/images/logo/logo.ico'); // await trayManager.setTitle('Clash For Flutter'); updateTray(); trayManager.addListener(this); } Future updateTray() async { final visible = await windowManager.isVisible(); final disabled = !controllers.service.isRunning; trayMenu = Menu(items: [ MenuItem.checkbox(label: 'tray_show'.tr, checked: visible, onClick: handleClickShow), MenuItem.separator(), // MenuItem.submenu( // label: 'proxie_group_title'.tr, // disabled: disabled, // submenu: Menu( // items: controllers.pageProxie.proxieGroups // .map((it) => MenuItem.submenu( // label: it.name, // submenu: Menu( // items: (it.all ?? []) // .map((t) => MenuItem.checkbox( // label: t, // checked: t == it.now, // disabled: it.type != 'Selector', // onClick: (m) => handleClickProxieItem(it, m), // )) // .toList(), // ), // )) // .toList()), // ), MenuItem( label: 'tray_restart_clash_core'.tr, disabled: !controllers.service.isCanOperationCore, onClick: handleClickRestartClashCore, ), MenuItem.checkbox( label: 'setting_set_as_system_proxy'.tr, checked: controllers.config.config.value.setSystemProxy, disabled: disabled || controllers.global.systemProxySwitchIng.value, onClick: handleClickSetAsSystemProxy, ), MenuItem.checkbox( label: 'setting_service_open'.tr, checked: controllers.service.serviceMode.value, disabled: !controllers.service.isCanOperationService, onClick: handleClickServiceModeSwitch, ), MenuItem.submenu( label: 'tray_copy_command_line_proxy'.tr, disabled: disabled, submenu: Menu(items: [ MenuItem(label: 'bash', onClick: handleClickCopyCommandLineProxy), MenuItem(label: 'cmd', onClick: handleClickCopyCommandLineProxy), MenuItem(label: 'powershell', onClick: handleClickCopyCommandLineProxy), ])), MenuItem.separator(), MenuItem(label: 'tray_about'.tr, onClick: handleClickAbout), MenuItem(label: 'tray_exit'.tr, onClick: handleClickExit), ]); await trayManager.setContextMenu(trayMenu); } @override void onTrayIconMouseDown() async { await controllers.window.showWindow(); } @override void onTrayIconRightMouseDown() async { show.value = true; await updateTray(); await trayManager.popUpContextMenu(); show.value = false; } Future handleClickShow(MenuItem menuItem) async { show.value = false; if (menuItem.checked == true) { await controllers.window.hideWindow(); } else { await controllers.window.showWindow(); } } Future handleClickProxieItem(ProxieProxiesItem proxie, MenuItem menuItem) async { await controllers.global.handleSetProxieGroup(proxie, menuItem.label!); } Future handleClickSetAsSystemProxy(MenuItem menuItem) async { await controllers.global.systemProxySwitch(menuItem.checked != true); } Future handleClickCopyCommandLineProxy(MenuItem menuItem) async { final title = menuItem.label!; final proxyConfig = controllers.core.proxyConfig; await copyCommandLineProxy(title, http: proxyConfig.http, https: proxyConfig.https); } Future handleClickAbout(MenuItem menuItem) async { await launchUrl(Uri.parse('https://github.com/csj8520/clash_for_flutter')); } Future handleClickExit(MenuItem menuItem) async { await controllers.global.handleExit(); } Future handleClickRestartClashCore(MenuItem menuItem) async { await controllers.service.reloadClashCore(); } Future handleClickServiceModeSwitch(MenuItem menuItem) async { await controllers.service.serviceModeSwitch(menuItem.checked != true); } @override void dispose() { trayManager.removeListener(this); super.dispose(); } }