123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import 'dart:async';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:speed_safe/app/controllers/config.dart';
- import 'package:speed_safe/app/controllers/controllers.dart';
- import 'package:speed_safe/app/util/log.dart';
- import 'package:speed_safe/app/util/system.dart';
- import 'package:window_manager/window_manager.dart';
- import 'app/routes/app_pages.dart';
- Future<void> getAppPath() async {
- if (Platform.isLinux && Platform.environment.containsKey('APPIMAGE')) {
- execPath = Platform.environment['APPIMAGE']!;
- } else {
- execPath = Platform.resolvedExecutable;
- }
- if (const bool.fromEnvironment('dart.vm.product')) {
- //如果是发布
- if (Platform.isLinux) {
- final linuxAppPath = (await getApplicationSupportDirectory()).path;
- final int firstIndex = linuxAppPath.indexOf('/root/');
- if (firstIndex != -1) {
- appPath = linuxAppPath.replaceFirst('/root/',
- '/home/${Platform.environment['SUDO_USER']}/', firstIndex);
- } else {
- appPath = linuxAppPath;
- }
- } else if (Platform.isMacOS) {
- final macAppPath = (await getApplicationSupportDirectory()).path;
- final int firstIndex = macAppPath.indexOf('/var/root/');
- if (firstIndex != -1) {
- appPath = macAppPath.replaceFirst('/var/root/',
- '/Users/${Platform.environment['SUDO_USER']}/', firstIndex);
- } else {
- appPath = macAppPath;
- }
- } else {
- appPath =
- execPath.substring(0, execPath.lastIndexOf(Platform.pathSeparator));
- }
- } else {
- if (Platform.isMacOS) {
- //如果是调试
- // For debug
- appPath = execPath.substring(0, execPath.lastIndexOf('/speed_safe.app'));
- } else {
- appPath =
- execPath.substring(0, execPath.lastIndexOf(Platform.pathSeparator));
- }
- }
- }
- Future<void> initConfig() async {
- await controllers.config.init();
- }
- Future<void> initApp() async {
- // Get app path
- await getAppPath();
- // init path
- SystemUtil.initPaths();
- // Init logger
- if (const bool.fromEnvironment('dart.vm.product')) {
- SLog.initLogger(true, 2, 2);
- } else {
- SLog.initLogger(false, 5, 5);
- }
- // Check dir exists
- SystemUtil.createDirectory(binPath);
- SystemUtil.createDirectory(configPath);
- SystemUtil.createDirectory(logPath);
- SystemUtil.createDirectory(tempPath);
- // Init SystemUtil
- SystemUtil.init();
- final speedInfo = '''
- SpeedSafe - a Proxy Handling Intuitive Application
- Full version: $speedFullVersion
- Last commit hash: $speedLastCommitHash
- OS: ${SystemUtil.os.name}
- Architecture: ${SystemUtil.architecture.name}
- App Path: $appPath
- Exec Path: $execPath
- Bin path: $binPath
- Config path: $configPath
- Log path: $logPath
- Temp path: $tempPath''';
- logger.i(speedInfo);
- await initConfig();
- WidgetsFlutterBinding.ensureInitialized();
- // 必须加上这一行。
- await windowManager.ensureInitialized();
- WindowOptions windowOptions = const WindowOptions(
- size: Size(800, 600),
- center: true,
- backgroundColor: Colors.transparent,
- skipTaskbar: false,
- //titleBarStyle: TitleBarStyle.hidden,
- );
- windowManager.waitUntilReadyToShow(windowOptions, () async {
- await windowManager.show();
- await windowManager.focus();
- });
- return;
- }
- void main() async {
- Get.put(ConfigController());
- controllers.init();
- await initApp();
- runApp(
- const MyApp(),
- );
- }
- class MyApp extends StatefulWidget {
- const MyApp({Key? key}) : super(key: key);
- @override
- State<MyApp> createState() => _MyAppState();
- }
- class _MyAppState extends State<MyApp> {
- late StreamSubscription<bool> listenShow;
- @override
- void initState() {
-
- //controllers.global.init(context);
- // listenShow = controllers.window.isVisible.stream.listen((event) async {
- // if (!event) return;
- // await Future.delayed(const Duration(milliseconds: 100));
- // await Get.forceAppUpdate();
- // });
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- // const Set<PointerDeviceKind> kTouchLikeDeviceTypes = <PointerDeviceKind>{
- // PointerDeviceKind.touch,
- // PointerDeviceKind.mouse,
- // PointerDeviceKind.stylus,
- // PointerDeviceKind.invertedStylus,
- // PointerDeviceKind.unknown
- // };
- return GetMaterialApp(
- debugShowCheckedModeBanner: false,
- title: "Application",
- initialRoute: AppPages.INITIAL,
- getPages: AppPages.routes,
- );
- }
- @override
- void dispose() {
- //controllers.global.dispose();
- listenShow.cancel();
- super.dispose();
- }
- }
|