tray.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import 'dart:io';
  2. import 'package:get/get.dart';
  3. import 'package:naiyouwl/app/bean/proxie.dart';
  4. import 'package:naiyouwl/app/controller/controllers.dart';
  5. import 'package:naiyouwl/app/utils/utils.dart';
  6. import 'package:tray_manager/tray_manager.dart';
  7. import 'package:url_launcher/url_launcher.dart';
  8. import 'package:window_manager/window_manager.dart';
  9. class TrayController extends GetxController with TrayListener {
  10. late Menu trayMenu;
  11. var disabledTun = false.obs;
  12. var show = false.obs;
  13. var isSHow = false;
  14. var isShowConsole = false.obs;
  15. Future<void> initTray() async {
  16. await trayManager.setIcon('assets/images/logo/logo.ico');
  17. // await trayManager.setTitle('Clash For Flutter');
  18. updateTray();
  19. trayManager.addListener(this);
  20. }
  21. Future<void> updateTray() async {
  22. final visible = await windowManager.isVisible();
  23. if(Platform.isWindows){
  24. isSHow = true;
  25. }
  26. final disabled = controllers.service.coreStatus.value != RunningState.running;
  27. var disabledSerivce = false;
  28. if(Platform.isWindows){
  29. disabledSerivce = true;
  30. }
  31. trayMenu = Menu(items: [
  32. MenuItem.checkbox(label: 'tray_show'.tr, checked: visible, onClick: handleClickShow),
  33. MenuItem.separator(),
  34. MenuItem.submenu(
  35. label: 'proxie_group_title'.tr,
  36. disabled: disabled,
  37. submenu: Menu(
  38. items: controllers.global.proxieGroups
  39. .map((it) => MenuItem.submenu(
  40. label: it.name,
  41. submenu: Menu(
  42. items: (it.all ?? [])
  43. .map((t) => MenuItem.checkbox(
  44. label: t,
  45. checked: t == it.now,
  46. disabled: it.type != 'Selector',
  47. onClick: (m) => handleClickProxieItem(it, m),
  48. ))
  49. .toList(),
  50. ),
  51. ))
  52. .toList()),
  53. ),
  54. // MenuItem(
  55. // label: 'tray_restart_clash_core'.tr,
  56. // disabled: !controllers.service.isCanOperationCore,
  57. // onClick: handleClickRestartClashCore,
  58. // ),
  59. MenuItem.checkbox(
  60. label: 'setting_set_as_system_proxy'.tr,
  61. checked: controllers.config.config.value.setSystemProxy,
  62. disabled: disabled || controllers.global.systemProxySwitchIng.value,
  63. onClick: handleClickSetAsSystemProxy,
  64. ),
  65. MenuItem.checkbox(
  66. label: 'route_tun_title'.tr,
  67. checked: disabledTun.value,
  68. disabled: false,
  69. onClick: handleClickSetAsTunProxy,
  70. ),
  71. MenuItem.checkbox(
  72. label: 'setting_service_open'.tr,
  73. checked: controllers.service.serviceMode.value,
  74. disabled: disabledSerivce == false ? !controllers.service.isCanOperationService : disabledSerivce,
  75. onClick: handleClickServiceModeSwitch,
  76. ),
  77. MenuItem.submenu(
  78. label: 'tray_copy_command_line_proxy'.tr,
  79. disabled: disabled,
  80. submenu: Menu(items: [
  81. MenuItem(label: 'bash', onClick: handleClickCopyCommandLineProxy),
  82. MenuItem(label: 'cmd', onClick: handleClickCopyCommandLineProxy),
  83. MenuItem(label: 'powershell', onClick: handleClickCopyCommandLineProxy),
  84. ])),
  85. MenuItem.separator(),
  86. // MenuItem(label: 'tray_about'.tr, onClick: handleClickAbout),
  87. //isSHow == true ? MenuItem(label: "显示控制台", onClick: handleClickConsoleShow,checked: isShowConsole.value) : MenuItem(disabled: true,label: "显示控制台", onClick: handleClickConsoleShow),
  88. MenuItem(label: 'tray_exit'.tr, onClick: handleClickExit),
  89. ]);
  90. await trayManager.setContextMenu(trayMenu);
  91. }
  92. @override
  93. void onTrayIconMouseDown() async {
  94. await controllers.window.showWindow();
  95. }
  96. @override
  97. void onTrayIconRightMouseDown() async {
  98. show.value = true;
  99. await updateTray();
  100. await trayManager.popUpContextMenu();
  101. show.value = false;
  102. }
  103. Future<void> handleClickConsoleShow(MenuItem menuItem) async {
  104. isSHow = false;
  105. if (menuItem.checked == true) {
  106. isShowConsole.value = true;
  107. //showConsole();
  108. } else {
  109. isShowConsole.value = false;
  110. // hideConsole();
  111. }
  112. }
  113. Future<void> handleClickShow(MenuItem menuItem) async {
  114. show.value = false;
  115. if (menuItem.checked == true) {
  116. await controllers.window.hideWindow();
  117. } else {
  118. await controllers.window.showWindow();
  119. }
  120. }
  121. Future<void> handleClickProxieItem(ProxieProxiesItem proxie, MenuItem menuItem) async {
  122. await controllers.global.handleSetProxieGroup(proxie, menuItem.label!);
  123. }
  124. Future<void> handleClickSetAsSystemProxy(MenuItem menuItem) async {
  125. await controllers.global.systemProxySwitch(menuItem.checked != true);
  126. }
  127. Future<void> handleClickSetAsTunProxy(MenuItem menuItem) async {
  128. disabledTun.value = menuItem.checked != true;
  129. await controllers.global.TunProxySwitch(menuItem.checked != true);
  130. }
  131. Future<void> handleClickCopyCommandLineProxy(MenuItem menuItem) async {
  132. final title = menuItem.label!;
  133. final proxyConfig = controllers.core.proxyConfig;
  134. await copyCommandLineProxy(title, http: proxyConfig.http, https: proxyConfig.https);
  135. }
  136. Future<void> handleClickAbout(MenuItem menuItem) async {
  137. //await launchUrl(Uri.parse('https://github.com/csj8520/clash_for_flutter'));
  138. }
  139. Future<void> handleClickExit(MenuItem menuItem) async {
  140. await controllers.global.handleExit();
  141. }
  142. Future<void> handleClickRestartClashCore(MenuItem menuItem) async {
  143. controllers.global.allowStatusUpdate = true;
  144. await controllers.service.reloadClashCore();
  145. }
  146. Future<void> handleClickServiceModeSwitch(MenuItem menuItem) async {
  147. await controllers.service.serviceModeSwitch(menuItem.checked != true);
  148. }
  149. @override
  150. void dispose() {
  151. trayManager.removeListener(this);
  152. super.dispose();
  153. }
  154. }