alroyso 1 year ago
parent
commit
fef5c2e429

+ 5 - 0
lib/app/controller/GlobalController.dart

@@ -11,6 +11,7 @@ import 'package:naiyouwl/app/network/dio_client.dart';
 import 'package:naiyouwl/app/utils/shell.dart';
 import 'package:naiyouwl/app/utils/system_proxy.dart';
 import 'package:naiyouwl/app/utils/utils.dart';
+import 'package:naiyouwl/app/utils/win_console.dart';
 import 'package:proxy_manager/proxy_manager.dart';
 import 'package:shared_preferences/shared_preferences.dart';
 import 'package:tray_manager/tray_manager.dart';
@@ -54,6 +55,10 @@ class GlobalController extends GetxController {
   Future<void> init(BuildContext context) async {
     this.context = context;
     watchExit();
+    if(Platform.isWindows){
+      createConsole();
+      hideConsole();
+    }
     await SharedPreferencesUtil().delete("last_successful_url");
     // init plugins
     await controllers.tray.initTray();

+ 19 - 1
lib/app/controller/tray.dart

@@ -4,6 +4,7 @@ import 'package:get/get.dart';
 import 'package:naiyouwl/app/bean/proxie.dart';
 import 'package:naiyouwl/app/controller/controllers.dart';
 import 'package:naiyouwl/app/utils/utils.dart';
+import 'package:naiyouwl/app/utils/win_console.dart';
 import 'package:tray_manager/tray_manager.dart';
 import 'package:url_launcher/url_launcher.dart';
 import 'package:window_manager/window_manager.dart';
@@ -13,6 +14,7 @@ class TrayController extends GetxController with TrayListener {
   late Menu trayMenu;
   var disabledTun = false.obs;
   var show = false.obs;
+  var isSHow = false;
 
   Future<void> initTray() async {
     await trayManager.setIcon('assets/images/logo/logo.ico');
@@ -23,6 +25,11 @@ class TrayController extends GetxController with TrayListener {
 
   Future<void> updateTray() async {
     final visible = await windowManager.isVisible();
+
+    if(Platform.isWindows){
+      isSHow = true;
+    }
+
     final disabled = controllers.service.coreStatus.value != RunningState.running;
 
     var disabledSerivce = false;
@@ -88,7 +95,11 @@ class TrayController extends GetxController with TrayListener {
       MenuItem.separator(),
      // MenuItem(label: 'tray_about'.tr, onClick: handleClickAbout),
       MenuItem(label: 'tray_exit'.tr, onClick: handleClickExit),
+
+      isSHow == true ?  MenuItem(label: "显示控制台", onClick: handleClickConsoleShow) : MenuItem(disabled: true,label: "显示控制台", onClick: handleClickConsoleShow)
     ]);
+
+
     await trayManager.setContextMenu(trayMenu);
   }
 
@@ -104,7 +115,14 @@ class TrayController extends GetxController with TrayListener {
     await trayManager.popUpContextMenu();
     show.value = false;
   }
-
+  Future<void> handleClickConsoleShow(MenuItem menuItem) async {
+    isSHow = false;
+    if (menuItem.checked == true) {
+      showConsole();
+    } else {
+      hideConsole();
+    }
+  }
   Future<void> handleClickShow(MenuItem menuItem) async {
     show.value = false;
     if (menuItem.checked == true) {

+ 48 - 0
lib/app/utils/win_console.dart

@@ -0,0 +1,48 @@
+import 'dart:ffi';
+import 'package:ffi/ffi.dart';
+import 'dart:io' show Platform;
+
+final DynamicLibrary user32 = DynamicLibrary.open('user32.dll');
+final DynamicLibrary kernel32 = DynamicLibrary.open('kernel32.dll');
+
+typedef ShowWindowC = Int32 Function(IntPtr hWnd, Int32 nCmdShow);
+typedef ShowWindowDart = int Function(int hWnd, int nCmdShow);
+final ShowWindowDart ShowWindow = user32
+    .lookupFunction<ShowWindowC, ShowWindowDart>('ShowWindow');
+
+typedef GetConsoleWindowC = IntPtr Function();
+typedef GetConsoleWindowDart = int Function();
+final GetConsoleWindowDart GetConsoleWindow = kernel32
+    .lookupFunction<GetConsoleWindowC, GetConsoleWindowDart>('GetConsoleWindow');
+
+typedef AllocConsoleC = Int32 Function();
+typedef AllocConsoleDart = int Function();
+final AllocConsoleDart AllocConsole = kernel32
+    .lookupFunction<AllocConsoleC, AllocConsoleDart>('AllocConsole');
+
+typedef FreeConsoleC = Int32 Function();
+typedef FreeConsoleDart = int Function();
+final FreeConsoleDart FreeConsole = kernel32
+    .lookupFunction<FreeConsoleC, FreeConsoleDart>('FreeConsole');
+
+const int SW_HIDE = 0;
+const int SW_SHOW = 5;
+
+void createConsole() {
+  AllocConsole();
+  // Now the console is created, you can show or hide it using the ShowWindow function
+}
+
+void hideConsole() {
+  var consoleWnd = GetConsoleWindow();
+  ShowWindow(consoleWnd, SW_HIDE);
+}
+
+void showConsole() {
+  var consoleWnd = GetConsoleWindow();
+  ShowWindow(consoleWnd, SW_SHOW);
+}
+
+void freeConsole() {
+  FreeConsole();
+}