tray.dart 5.1 KB

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