tray.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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: 'setting_service_open'.tr,
  56. checked: controllers.service.serviceMode.value,
  57. disabled: !controllers.service.isCanOperationService,
  58. onClick: handleClickServiceModeSwitch,
  59. ),
  60. MenuItem.submenu(
  61. label: 'tray_copy_command_line_proxy'.tr,
  62. disabled: disabled,
  63. submenu: Menu(items: [
  64. MenuItem(label: 'bash', onClick: handleClickCopyCommandLineProxy),
  65. MenuItem(label: 'cmd', onClick: handleClickCopyCommandLineProxy),
  66. MenuItem(label: 'powershell', onClick: handleClickCopyCommandLineProxy),
  67. ])),
  68. MenuItem.separator(),
  69. MenuItem(label: 'tray_about'.tr, onClick: handleClickAbout),
  70. MenuItem(label: 'tray_exit'.tr, onClick: handleClickExit),
  71. ]);
  72. await trayManager.setContextMenu(trayMenu);
  73. }
  74. @override
  75. void onTrayIconMouseDown() async {
  76. await controllers.window.showWindow();
  77. }
  78. @override
  79. void onTrayIconRightMouseDown() async {
  80. show.value = true;
  81. await updateTray();
  82. await trayManager.popUpContextMenu();
  83. show.value = false;
  84. }
  85. Future<void> handleClickShow(MenuItem menuItem) async {
  86. show.value = false;
  87. if (menuItem.checked == true) {
  88. await controllers.window.hideWindow();
  89. } else {
  90. await controllers.window.showWindow();
  91. }
  92. }
  93. Future<void> handleClickProxieItem(ProxieProxiesItem proxie, MenuItem menuItem) async {
  94. await controllers.global.handleSetProxieGroup(proxie, menuItem.label!);
  95. }
  96. Future<void> handleClickSetAsSystemProxy(MenuItem menuItem) async {
  97. await controllers.global.systemProxySwitch(menuItem.checked != true);
  98. }
  99. Future<void> handleClickCopyCommandLineProxy(MenuItem menuItem) async {
  100. final title = menuItem.label!;
  101. final proxyConfig = controllers.core.proxyConfig;
  102. await copyCommandLineProxy(title, http: proxyConfig.http, https: proxyConfig.https);
  103. }
  104. Future<void> handleClickAbout(MenuItem menuItem) async {
  105. await launchUrl(Uri.parse('https://github.com/csj8520/clash_for_flutter'));
  106. }
  107. Future<void> handleClickExit(MenuItem menuItem) async {
  108. await controllers.global.handleExit();
  109. }
  110. Future<void> handleClickRestartClashCore(MenuItem menuItem) async {
  111. controllers.global.allowStatusUpdate = true;
  112. await controllers.service.reloadClashCore();
  113. }
  114. Future<void> handleClickServiceModeSwitch(MenuItem menuItem) async {
  115. await controllers.service.serviceModeSwitch(menuItem.checked != true);
  116. }
  117. @override
  118. void dispose() {
  119. trayManager.removeListener(this);
  120. super.dispose();
  121. }
  122. }