main.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:speed_safe/app/controllers/config.dart';
  7. import 'package:speed_safe/app/controllers/controllers.dart';
  8. import 'package:speed_safe/app/util/log.dart';
  9. import 'package:speed_safe/app/util/system.dart';
  10. import 'package:window_manager/window_manager.dart';
  11. import 'app/routes/app_pages.dart';
  12. Future<void> getAppPath() async {
  13. if (Platform.isLinux && Platform.environment.containsKey('APPIMAGE')) {
  14. execPath = Platform.environment['APPIMAGE']!;
  15. } else {
  16. execPath = Platform.resolvedExecutable;
  17. }
  18. if (const bool.fromEnvironment('dart.vm.product')) {
  19. //如果是发布
  20. if (Platform.isLinux) {
  21. final linuxAppPath = (await getApplicationSupportDirectory()).path;
  22. final int firstIndex = linuxAppPath.indexOf('/root/');
  23. if (firstIndex != -1) {
  24. appPath = linuxAppPath.replaceFirst('/root/',
  25. '/home/${Platform.environment['SUDO_USER']}/', firstIndex);
  26. } else {
  27. appPath = linuxAppPath;
  28. }
  29. } else if (Platform.isMacOS) {
  30. final macAppPath = (await getApplicationSupportDirectory()).path;
  31. final int firstIndex = macAppPath.indexOf('/var/root/');
  32. if (firstIndex != -1) {
  33. appPath = macAppPath.replaceFirst('/var/root/',
  34. '/Users/${Platform.environment['SUDO_USER']}/', firstIndex);
  35. } else {
  36. appPath = macAppPath;
  37. }
  38. } else {
  39. appPath =
  40. execPath.substring(0, execPath.lastIndexOf(Platform.pathSeparator));
  41. }
  42. } else {
  43. if (Platform.isMacOS) {
  44. //如果是调试
  45. // For debug
  46. appPath = execPath.substring(0, execPath.lastIndexOf('/speed_safe.app'));
  47. } else {
  48. appPath =
  49. execPath.substring(0, execPath.lastIndexOf(Platform.pathSeparator));
  50. }
  51. }
  52. }
  53. Future<void> initConfig() async {
  54. await controllers.config.init();
  55. }
  56. Future<void> initApp() async {
  57. // Get app path
  58. await getAppPath();
  59. // init path
  60. SystemUtil.initPaths();
  61. // Init logger
  62. if (const bool.fromEnvironment('dart.vm.product')) {
  63. SLog.initLogger(true, 2, 2);
  64. } else {
  65. SLog.initLogger(false, 5, 5);
  66. }
  67. // Check dir exists
  68. SystemUtil.createDirectory(binPath);
  69. SystemUtil.createDirectory(configPath);
  70. SystemUtil.createDirectory(logPath);
  71. SystemUtil.createDirectory(tempPath);
  72. // Init SystemUtil
  73. SystemUtil.init();
  74. final speedInfo = '''
  75. SpeedSafe - a Proxy Handling Intuitive Application
  76. Full version: $speedFullVersion
  77. Last commit hash: $speedLastCommitHash
  78. OS: ${SystemUtil.os.name}
  79. Architecture: ${SystemUtil.architecture.name}
  80. App Path: $appPath
  81. Exec Path: $execPath
  82. Bin path: $binPath
  83. Config path: $configPath
  84. Log path: $logPath
  85. Temp path: $tempPath''';
  86. logger.i(speedInfo);
  87. await initConfig();
  88. WidgetsFlutterBinding.ensureInitialized();
  89. // 必须加上这一行。
  90. await windowManager.ensureInitialized();
  91. WindowOptions windowOptions = const WindowOptions(
  92. size: Size(800, 600),
  93. center: true,
  94. backgroundColor: Colors.transparent,
  95. skipTaskbar: false,
  96. //titleBarStyle: TitleBarStyle.hidden,
  97. );
  98. windowManager.waitUntilReadyToShow(windowOptions, () async {
  99. await windowManager.show();
  100. await windowManager.focus();
  101. });
  102. return;
  103. }
  104. void main() async {
  105. Get.put(ConfigController());
  106. controllers.init();
  107. await initApp();
  108. runApp(
  109. const MyApp(),
  110. );
  111. }
  112. class MyApp extends StatefulWidget {
  113. const MyApp({Key? key}) : super(key: key);
  114. @override
  115. State<MyApp> createState() => _MyAppState();
  116. }
  117. class _MyAppState extends State<MyApp> {
  118. late StreamSubscription<bool> listenShow;
  119. @override
  120. void initState() {
  121. //controllers.global.init(context);
  122. // listenShow = controllers.window.isVisible.stream.listen((event) async {
  123. // if (!event) return;
  124. // await Future.delayed(const Duration(milliseconds: 100));
  125. // await Get.forceAppUpdate();
  126. // });
  127. super.initState();
  128. }
  129. @override
  130. Widget build(BuildContext context) {
  131. // const Set<PointerDeviceKind> kTouchLikeDeviceTypes = <PointerDeviceKind>{
  132. // PointerDeviceKind.touch,
  133. // PointerDeviceKind.mouse,
  134. // PointerDeviceKind.stylus,
  135. // PointerDeviceKind.invertedStylus,
  136. // PointerDeviceKind.unknown
  137. // };
  138. return GetMaterialApp(
  139. debugShowCheckedModeBanner: false,
  140. title: "Application",
  141. initialRoute: AppPages.INITIAL,
  142. getPages: AppPages.routes,
  143. );
  144. }
  145. @override
  146. void dispose() {
  147. //controllers.global.dispose();
  148. listenShow.cancel();
  149. super.dispose();
  150. }
  151. }