refactor service start/stop

This commit is contained in:
rustdesk 2023-06-06 21:51:40 +08:00
parent d2bb866e48
commit a5d56fcd3f
3 changed files with 36 additions and 37 deletions

View File

@ -2043,3 +2043,12 @@ void onCopyFingerprint(String value) {
showToast(translate("no fingerprints")); showToast(translate("no fingerprints"));
} }
} }
Future<void> start_service(bool is_start) async {
bool checked = !bind.mainIsInstalled() ||
!Platform.isMacOS ||
await bind.mainCheckSuperUserPermission();
if (checked) {
bind.mainSetOption(key: "stop-service", value: is_start ? "" : "Y");
}
}

View File

@ -274,12 +274,7 @@ class _ConnectionPageState extends State<ConnectionPage>
offstage: !svcStopped.value, offstage: !svcStopped.value,
child: InkWell( child: InkWell(
onTap: () async { onTap: () async {
bool checked = !bind.mainIsInstalled() || await start_service(true);
await bind.mainCheckSuperUserPermission();
if (checked) {
bind.mainSetOption(key: "stop-service", value: "");
bind.mainSetOption(key: "access-mode", value: "");
}
}, },
child: Text(translate("Start Service"), child: Text(translate("Start Service"),
style: TextStyle( style: TextStyle(

View File

@ -247,6 +247,9 @@ class _General extends StatefulWidget {
} }
class _GeneralState extends State<_General> { class _GeneralState extends State<_General> {
final RxBool serviceStop = Get.find<RxBool>(tag: 'stop-service');
RxBool serviceBtnEabled = true.obs;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final scrollController = ScrollController(); final scrollController = ScrollController();
@ -256,6 +259,7 @@ class _GeneralState extends State<_General> {
physics: DraggableNeverScrollableScrollPhysics(), physics: DraggableNeverScrollableScrollPhysics(),
controller: scrollController, controller: scrollController,
children: [ children: [
service(),
theme(), theme(),
hwcodec(), hwcodec(),
audio(context), audio(context),
@ -292,6 +296,21 @@ class _GeneralState extends State<_General> {
]); ]);
} }
Widget service() {
return _Card(title: 'Service', children: [
Obx(() => _Button(serviceStop.value ? 'Start' : 'Stop', () {
() async {
serviceBtnEabled.value = false;
await start_service(serviceStop.value);
// enable the button after 1 second
Future.delayed(const Duration(seconds: 1), () {
serviceBtnEabled.value = true;
});
}();
}, enabled: serviceBtnEabled.value))
]);
}
Widget other() { Widget other() {
return _Card(title: 'Other', children: [ return _Card(title: 'Other', children: [
_OptionCheckBox(context, 'Confirm before closing multiple tabs', _OptionCheckBox(context, 'Confirm before closing multiple tabs',
@ -457,7 +476,6 @@ enum _AccessMode {
custom, custom,
full, full,
view, view,
deny,
} }
class _Safety extends StatefulWidget { class _Safety extends StatefulWidget {
@ -510,15 +528,12 @@ class _SafetyState extends State<_Safety> with AutomaticKeepAliveClientMixin {
} }
Widget _permissions(context, bool stopService) { Widget _permissions(context, bool stopService) {
bool enabled = !locked; bool enabled = !locked && stopService;
return futureBuilder(future: () async { return futureBuilder(future: () async {
return await bind.mainGetOption(key: 'access-mode'); return await bind.mainGetOption(key: 'access-mode');
}(), hasData: (data) { }(), hasData: (data) {
String accessMode = data! as String; String accessMode = data! as String;
_AccessMode mode; _AccessMode mode;
if (stopService) {
mode = _AccessMode.deny;
} else {
if (accessMode == 'full') { if (accessMode == 'full') {
mode = _AccessMode.full; mode = _AccessMode.full;
} else if (accessMode == 'view') { } else if (accessMode == 'view') {
@ -526,7 +541,6 @@ class _SafetyState extends State<_Safety> with AutomaticKeepAliveClientMixin {
} else { } else {
mode = _AccessMode.custom; mode = _AccessMode.custom;
} }
}
String initialKey; String initialKey;
bool? fakeValue; bool? fakeValue;
switch (mode) { switch (mode) {
@ -542,10 +556,6 @@ class _SafetyState extends State<_Safety> with AutomaticKeepAliveClientMixin {
initialKey = 'view'; initialKey = 'view';
fakeValue = false; fakeValue = false;
break; break;
case _AccessMode.deny:
initialKey = 'deny';
fakeValue = false;
break;
} }
return _Card(title: 'Permissions', children: [ return _Card(title: 'Permissions', children: [
@ -554,34 +564,19 @@ class _SafetyState extends State<_Safety> with AutomaticKeepAliveClientMixin {
'', '',
'full', 'full',
'view', 'view',
'deny'
], ],
values: [ values: [
translate('Custom'), translate('Custom'),
translate('Full Access'), translate('Full Access'),
translate('Screen Share'), translate('Screen Share'),
translate('Deny remote access'),
], ],
enabled: enabled, enabled: enabled,
initialKey: initialKey, initialKey: initialKey,
onChanged: (mode) async { onChanged: (mode) async {
String modeValue; await bind.mainSetOption(key: 'access-mode', value: mode);
bool stopService;
if (mode == 'deny') {
modeValue = '';
stopService = true;
} else {
modeValue = mode;
stopService = false;
}
await bind.mainSetOption(key: 'access-mode', value: modeValue);
await bind.mainSetOption(
key: 'stop-service',
value: bool2option('stop-service', stopService));
setState(() {}); setState(() {});
}).marginOnly(left: _kContentHMargin), }).marginOnly(left: _kContentHMargin),
Offstage( Offstage(
offstage: mode == _AccessMode.deny,
child: Column( child: Column(
children: [ children: [
_OptionCheckBox( _OptionCheckBox(