tray.dart 5.3 KB

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