import 'dart:io'; import 'package:get/get.dart'; import 'package:naiyouwl/app/bean/proxie.dart'; import 'package:naiyouwl/app/controller/controllers.dart'; import 'package:naiyouwl/app/data/model/NodeMode.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 disabledTun = false.obs; var show = false.obs; var isSHow = false; var isShowConsole = 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(); if(Platform.isWindows){ isSHow = true; } final disabled = controllers.service.coreStatus.value != RunningState.running; var disabledSerivce = false; if(Platform.isWindows){ disabledSerivce = true; } 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.global.nodeModes.map((it) => // MenuItem.checkbox( // label: it.name, // checked: it.id == nodeId.value, // onClicked: () { // handleClickProxieItem(it) // }, // ) // ).toList(), // ), // ), MenuItem.submenu( label: 'proxie_group_title'.tr, disabled: disabled, submenu: Menu( items: controllers.global.nodeModes.map((it) => MenuItem.checkbox( label: it.name, checked: it.id == controllers.global.nodeId.value, onClick: (m) => handleClickProxieItem(it, m), ) ).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: 'route_tun_title'.tr, checked: disabledTun.value, disabled: false, onClick: handleClickSetAsTunProxy, ), MenuItem.checkbox( label: 'setting_service_open'.tr, checked: controllers.service.serviceMode.value, disabled: disabledSerivce == false ? !controllers.service.isCanOperationService : disabledSerivce, 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: '服务端口:${controllers.config.servicePort.value}', disabled: true,), isSHow == true ? MenuItem(label: "显示控制台", onClick: handleClickConsoleShow,checked: isShowConsole.value) : MenuItem(disabled: true,label: "显示控制台", onClick: handleClickConsoleShow), 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 handleClickConsoleShow(MenuItem menuItem) async { isSHow = true; if (menuItem.checked == true) { isShowConsole.value = false; controllers.global.hideConsole(); } else { isShowConsole.value = true; controllers.global.showConsole(); } } Future handleClickShow(MenuItem menuItem) async { show.value = false; if (menuItem.checked == true) { await controllers.window.hideWindow(); } else { await controllers.window.showWindow(); } } Future handleClickProxieItem(NodeMode proxie, MenuItem menuItem) async { controllers.global.selectNode(proxie); await controllers.global.swift(proxie.name ?? ""); } // 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 handleClickSetAsTunProxy(MenuItem menuItem) async { disabledTun.value = menuItem.checked != true; await controllers.global.TunProxySwitch(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 { controllers.global.allowStatusUpdate = true; await controllers.service.reloadClashCore(); } Future handleClickServiceModeSwitch(MenuItem menuItem) async { await controllers.service.serviceModeSwitch(menuItem.checked != true); } @override void dispose() { trayManager.removeListener(this); super.dispose(); } }