11import 'dart:async' ;
22import 'dart:io' ;
3+ import 'package:fl_clash/models/models.dart' hide Process;
34import 'package:launch_at_startup/launch_at_startup.dart' ;
45
56import 'constant.dart' ;
67import 'system.dart' ;
8+ import 'windows.dart' ;
79
810class AutoLaunch {
911 static AutoLaunch ? _instance;
@@ -34,6 +36,9 @@ class AutoLaunch {
3436 }
3537
3638 Future <bool > enable () async {
39+ if (Platform .isWindows) {
40+ await windowsDisable ();
41+ }
3742 return await launchAtStartup.enable ();
3843 }
3944
@@ -51,45 +56,47 @@ class AutoLaunch {
5156 return res.exitCode == 0 ;
5257 }
5358
54- windowsEnable () async {
55- await Process .run (
56- 'schtasks' ,
57- [
58- '/Create' ,
59- '/SC' ,
60- 'ONLOGON' ,
61- '/TN' ,
62- appName,
63- '/TR' ,
64- Platform .resolvedExecutable,
65- '/RL' ,
66- 'HIGHEST' ,
67- '/F'
68- ],
69- runInShell: true ,
70- );
59+ Future <bool > windowsEnable () async {
60+ await disable ();
61+ return windows? .runas (
62+ 'schtasks' ,
63+ [
64+ '/Create' ,
65+ '/SC' ,
66+ 'ONLOGON' ,
67+ '/TN' ,
68+ appName,
69+ '/TR' ,
70+ Platform .resolvedExecutable,
71+ '/RL' ,
72+ 'HIGHEST' ,
73+ '/F'
74+ ].join (" " ),
75+ ) ??
76+ false ;
7177 }
7278
7379 Future <bool > disable () async {
7480 return await launchAtStartup.disable ();
7581 }
7682
77- updateStatus (bool value ) async {
78- final currentEnable =
79- Platform .isWindows ? await windowsIsEnable : await isEnable ;
80- if (value == currentEnable ) {
81- return ;
82- }
83- if ( Platform .isWindows) {
84- if (value ) {
85- enable ();
86- windowsEnable ();
83+ updateStatus (AutoLaunchState state ) async {
84+ final isOpenTun = state.isOpenTun;
85+ final isAutoLaunch = state.isAutoLaunch ;
86+ if (Platform .isWindows && isOpenTun ) {
87+ if ( await windowsIsEnable == isAutoLaunch) return ;
88+ if (isAutoLaunch) {
89+ final isEnable = await windowsEnable ();
90+ if (! isEnable ) {
91+ enable ();
92+ }
8793 } else {
8894 windowsDisable ();
8995 }
9096 return ;
9197 }
92- if (value == true ) {
98+ if (await isEnable == isAutoLaunch) return ;
99+ if (isAutoLaunch == true ) {
93100 enable ();
94101 } else {
95102 disable ();
0 commit comments