tray.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import 'dart:io';
  2. import 'package:get/get.dart';
  3. import 'package:naiyouwl/app/bean/proxie.dart';
  4. import 'package:naiyouwl/app/common/SharedPreferencesUtil.dart';
  5. import 'package:naiyouwl/app/controller/controllers.dart';
  6. import 'package:naiyouwl/app/data/model/NodeMode.dart';
  7. import 'package:naiyouwl/app/utils/utils.dart';
  8. import 'package:tray_manager/tray_manager.dart';
  9. import 'package:url_launcher/url_launcher.dart';
  10. import 'package:window_manager/window_manager.dart';
  11. class TrayController extends GetxController with TrayListener {
  12. late Menu trayMenu;
  13. var disabledTun = false.obs;
  14. var show = false.obs;
  15. var isSHow = false;
  16. var isShowConsole = false.obs;
  17. Future<void> initTray() async {
  18. await trayManager.setIcon('assets/images/logo/logo.ico');
  19. // await trayManager.setTitle('jsq');
  20. updateTray();
  21. trayManager.addListener(this);
  22. }
  23. Future<void> updateTray() async {
  24. final visible = await windowManager.isVisible();
  25. if(Platform.isWindows){
  26. isSHow = true;
  27. }
  28. var tun = await SharedPreferencesUtil().getString("tun");
  29. if(tun == null){
  30. disabledTun.value == false;
  31. } else {
  32. disabledTun.value = tun == "true" ? true : false;
  33. }
  34. final disabled = controllers.global.connectStatus.value == false;
  35. var disabledSerivce = controllers.global.connectStatus.value;
  36. if(Platform.isWindows){
  37. //disabledTun.value = true;
  38. }
  39. var serivceName = controllers.cc_service.installStatus.value == true? 'setting_service_uninstall'.tr : 'setting_service_install'.tr,
  40. trayMenu = Menu(items: [
  41. MenuItem.checkbox(label: 'tray_show'.tr, checked: visible, onClick: handleClickShow),
  42. MenuItem.separator(),
  43. // MenuItem.submenu(
  44. // label: 'proxie_group_title'.tr,
  45. // disabled: disabled,
  46. // submenu: Menu(
  47. // items: controllers.global.nodeModes.map((it) =>
  48. // MenuItem.checkbox(
  49. // label: it.name,
  50. // checked: it.id == nodeId.value,
  51. // onClicked: () {
  52. // handleClickProxieItem(it)
  53. // },
  54. // )
  55. // ).toList(),
  56. // ),
  57. // ),
  58. MenuItem.submenu(
  59. label: 'proxie_group_title'.tr,
  60. disabled: false,
  61. submenu: Menu(
  62. items: controllers.global.nodeModes.map((it) =>
  63. MenuItem.checkbox(
  64. label: it.name,
  65. checked: it.id == controllers.global.nodeId.value,
  66. onClick: (m) => handleClickProxieItem(it, m),
  67. )
  68. ).toList(),
  69. ),
  70. ),
  71. // MenuItem(
  72. // label: 'tray_restart_clash_core'.tr,
  73. // disabled: !controllers.service.isCanOperationCore,
  74. // onClick: handleClickRestartClashCore,
  75. // ),
  76. MenuItem.checkbox(
  77. label: 'setting_set_as_system_proxy'.tr,
  78. checked: controllers.global.systemProxy.value,
  79. disabled: false, //disabled || controllers.global.systemProxySwitchIng.value
  80. onClick: handleClickSetAsSystemProxy,
  81. ),
  82. MenuItem.checkbox(
  83. label: 'route_tun_title'.tr,
  84. checked: disabledTun.value,
  85. disabled: Platform.isWindows ? false : disabledSerivce,
  86. onClick: handleClickSetAsTunProxy,
  87. ),
  88. MenuItem.checkbox(
  89. label: serivceName,
  90. checked: controllers.cc_service.installStatus.value,
  91. disabled: disabledSerivce,
  92. onClick: handleClickServiceModeSwitch,
  93. ),
  94. MenuItem.submenu(
  95. label: 'tray_copy_command_line_proxy'.tr,
  96. disabled: disabled,
  97. submenu: Menu(items: [
  98. MenuItem(label: 'bash', onClick: handleClickCopyCommandLineProxy),
  99. MenuItem(label: 'cmd', onClick: handleClickCopyCommandLineProxy),
  100. MenuItem(label: 'powershell', onClick: handleClickCopyCommandLineProxy),
  101. ])),
  102. MenuItem.separator(),
  103. MenuItem(label: '服务端口:${controllers.config.servicePort.value}', disabled: true,),
  104. isSHow == true ? MenuItem(label: "显示控制台", onClick: handleClickConsoleShow,checked: isShowConsole.value)
  105. : MenuItem(disabled: true,label: "显示控制台", onClick: handleClickConsoleShow),
  106. MenuItem(label: 'tray_exit'.tr, onClick: handleClickExit),
  107. ]);
  108. await trayManager.setContextMenu(trayMenu);
  109. }
  110. @override
  111. void onTrayIconMouseDown() async {
  112. await controllers.window.showWindow();
  113. }
  114. @override
  115. void onTrayIconRightMouseDown() async {
  116. show.value = true;
  117. await updateTray();
  118. await trayManager.popUpContextMenu();
  119. show.value = false;
  120. }
  121. Future<void> handleClickConsoleShow(MenuItem menuItem) async {
  122. isSHow = true;
  123. if (menuItem.checked == true) {
  124. isShowConsole.value = false;
  125. controllers.global.hideConsole();
  126. } else {
  127. isShowConsole.value = true;
  128. controllers.global.showConsole();
  129. }
  130. }
  131. Future<void> handleClickShow(MenuItem menuItem) async {
  132. show.value = false;
  133. if (menuItem.checked == true) {
  134. await controllers.window.hideWindow();
  135. } else {
  136. await controllers.window.showWindow();
  137. }
  138. }
  139. Future<void> handleClickProxieItem(NodeMode proxie, MenuItem menuItem) async {
  140. controllers.global.selectNode(proxie);
  141. await controllers.global.swift(proxie.name ?? "");
  142. }
  143. // Future<void> handleClickProxieItem(ProxieProxiesItem proxie, MenuItem menuItem) async {
  144. // await controllers.global.handleSetProxieGroup(proxie, menuItem.label!);
  145. // }
  146. Future<void> handleClickSetAsSystemProxy(MenuItem menuItem) async {
  147. await controllers.global.systemProxySwitch(menuItem.checked != true);
  148. }
  149. Future<void> handleClickSetAsTunProxy(MenuItem menuItem) async {
  150. disabledTun.value = menuItem.checked != true;
  151. await SharedPreferencesUtil().setString("tun", disabledTun.value == true ? "true": "false");
  152. await controllers.global.TunProxySwitch(menuItem.checked != true);
  153. }
  154. Future<void> handleClickCopyCommandLineProxy(MenuItem menuItem) async {
  155. final title = menuItem.label!;
  156. final proxyConfig = controllers.core.proxyConfig;
  157. await copyCommandLineProxy(title, http: proxyConfig.http, https: proxyConfig.https);
  158. }
  159. Future<void> handleClickAbout(MenuItem menuItem) async {
  160. //await launchUrl(Uri.parse('https://github.com/csj8520/clash_for_flutter'));
  161. }
  162. Future<void> handleClickExit(MenuItem menuItem) async {
  163. await controllers.global.handleExit();
  164. }
  165. Future<void> handleClickRestartClashCore(MenuItem menuItem) async {
  166. controllers.global.allowStatusUpdate = true;
  167. await controllers.service.reloadClashCore();
  168. }
  169. Future<void> handleClickServiceModeSwitch(MenuItem menuItem) async {
  170. await controllers.cc_service.serviceModeSwitch(menuItem.checked != true);
  171. }
  172. @override
  173. void dispose() {
  174. trayManager.removeListener(this);
  175. super.dispose();
  176. }
  177. }