tray.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. 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. MenuItem(label: 'tray_exit'.tr, onClick: handleClickExit),
  88. isSHow == true ? MenuItem(label: "显示控制台", onClick: handleClickConsoleShow) : MenuItem(disabled: true,label: "显示控制台", onClick: handleClickConsoleShow)
  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. showConsole();
  107. } else {
  108. hideConsole();
  109. }
  110. }
  111. Future<void> handleClickShow(MenuItem menuItem) async {
  112. show.value = false;
  113. if (menuItem.checked == true) {
  114. await controllers.window.hideWindow();
  115. } else {
  116. await controllers.window.showWindow();
  117. }
  118. }
  119. Future<void> handleClickProxieItem(ProxieProxiesItem proxie, MenuItem menuItem) async {
  120. await controllers.global.handleSetProxieGroup(proxie, menuItem.label!);
  121. }
  122. Future<void> handleClickSetAsSystemProxy(MenuItem menuItem) async {
  123. await controllers.global.systemProxySwitch(menuItem.checked != true);
  124. }
  125. Future<void> handleClickSetAsTunProxy(MenuItem menuItem) async {
  126. disabledTun.value = menuItem.checked != true;
  127. await controllers.global.TunProxySwitch(menuItem.checked != true);
  128. }
  129. Future<void> handleClickCopyCommandLineProxy(MenuItem menuItem) async {
  130. final title = menuItem.label!;
  131. final proxyConfig = controllers.core.proxyConfig;
  132. await copyCommandLineProxy(title, http: proxyConfig.http, https: proxyConfig.https);
  133. }
  134. Future<void> handleClickAbout(MenuItem menuItem) async {
  135. //await launchUrl(Uri.parse('https://github.com/csj8520/clash_for_flutter'));
  136. }
  137. Future<void> handleClickExit(MenuItem menuItem) async {
  138. await controllers.global.handleExit();
  139. }
  140. Future<void> handleClickRestartClashCore(MenuItem menuItem) async {
  141. controllers.global.allowStatusUpdate = true;
  142. await controllers.service.reloadClashCore();
  143. }
  144. Future<void> handleClickServiceModeSwitch(MenuItem menuItem) async {
  145. await controllers.service.serviceModeSwitch(menuItem.checked != true);
  146. }
  147. @override
  148. void dispose() {
  149. trayManager.removeListener(this);
  150. super.dispose();
  151. }
  152. }