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: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)
  88. : 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 = true;
  106. if (menuItem.checked == true) {
  107. isShowConsole.value = false;
  108. controllers.global.hideConsole();
  109. } else {
  110. isShowConsole.value = true;
  111. controllers.global.showConsole();
  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. }