tray.dart 5.9 KB

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