From eec26e5c70f3b5115e4b606bab9d18ce7d77ea90 Mon Sep 17 00:00:00 2001 From: csf Date: Sat, 30 Jul 2022 22:13:38 +0800 Subject: [PATCH] add mobile restart remote device --- flutter/lib/models/model.dart | 9 ++++++--- flutter/lib/pages/remote_page.dart | 30 ++++++++++++++++++++++++++++++ src/mobile.rs | 19 +++++++++++++++++-- src/ui/remote.rs | 5 +++-- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/flutter/lib/models/model.dart b/flutter/lib/models/model.dart index 7255c6baa..4e1c76e4a 100644 --- a/flutter/lib/models/model.dart +++ b/flutter/lib/models/model.dart @@ -68,7 +68,7 @@ class FfiModel with ChangeNotifier { void updatePermission(Map evt) { evt.forEach((k, v) { - if (k == 'name') return; + if (k == 'name' || k.isEmpty) return; _permissions[k] = v == 'true'; }); print('$_permissions'); @@ -195,14 +195,17 @@ class FfiModel with ChangeNotifier { wrongPasswordDialog(id); } else if (type == 'input-password') { enterPasswordDialog(id); + } else if (type == 'restarting') { + showMsgBox(type, title, text, false, hasCancel: false); } else { var hasRetry = evt['hasRetry'] == 'true'; showMsgBox(type, title, text, hasRetry); } } - void showMsgBox(String type, String title, String text, bool hasRetry) { - msgBox(type, title, text); + void showMsgBox(String type, String title, String text, bool hasRetry, + {bool? hasCancel}) { + msgBox(type, title, text, hasCancel: hasCancel); _timer?.cancel(); if (hasRetry) { _timer = Timer(Duration(seconds: _reconnects), () { diff --git a/flutter/lib/pages/remote_page.dart b/flutter/lib/pages/remote_page.dart index bb196f0cf..81349ef3b 100644 --- a/flutter/lib/pages/remote_page.dart +++ b/flutter/lib/pages/remote_page.dart @@ -694,6 +694,13 @@ class _RemotePageState extends State { value: 'block-input')); } } + if (FFI.ffiModel.permissions["restart"] != false && + (pi.platform == "Linux" || + pi.platform == "Windows" || + pi.platform == "Mac OS")) { + more.add(PopupMenuItem( + child: Text(translate('Restart Remote Device')), value: 'restart')); + } () async { var value = await showMenu( context: context, @@ -727,6 +734,8 @@ class _RemotePageState extends State { } } else if (value == 'reset_canvas') { FFI.cursorModel.reset(); + } else if (value == 'restart') { + showRestartRemoteDevice(pi, widget.id); } }(); } @@ -1103,6 +1112,27 @@ void showOptions() { }, clickMaskDismiss: true, backDismiss: true); } +void showRestartRemoteDevice(PeerInfo pi, String id) async { + final res = + await DialogManager.show((setState, close) => CustomAlertDialog( + title: Row(children: [ + Icon(Icons.warning_amber_sharp, + color: Colors.redAccent, size: 28), + SizedBox(width: 10), + Text(translate("Restart Remote Device")), + ]), + content: Text( + "${translate('Are you sure you want to restart')} \n${pi.username}@${pi.hostname}($id) ?"), + actions: [ + TextButton( + onPressed: () => close(), child: Text(translate("Cancel"))), + ElevatedButton( + onPressed: () => close(true), child: Text(translate("OK"))), + ], + )); + if (res == true) FFI.setByName('restart_remote_device'); +} + void showSetOSPassword(bool login) { final controller = TextEditingController(); var password = FFI.getByName('peer_option', "os-password"); diff --git a/src/mobile.rs b/src/mobile.rs index a8777cf39..dec9d49c8 100644 --- a/src/mobile.rs +++ b/src/mobile.rs @@ -88,6 +88,15 @@ impl Session { } } + pub fn restart_remote_device() { + if let Some(session) = SESSION.write().unwrap().as_ref() { + let mut lc = session.lc.write().unwrap(); + lc.restarting_remote_device = true; + let msg = lc.restart_remote_device(); + session.send(Data::Message(msg)); + } + } + fn send(data: Data) { if let Some(session) = SESSION.read().unwrap().as_ref() { session.send(data); @@ -605,8 +614,13 @@ impl Connection { } } } else { - log::info!("Reset by the peer"); - session.msgbox("error", "Connection Error", "Reset by the peer"); + if session.lc.read().unwrap().restarting_remote_device { + log::info!("Restart remote device"); + session.msgbox("restarting", "Restarting Remote Device", "remote_restarting_tip"); + } else { + log::info!("Reset by the peer"); + session.msgbox("error", "Connection Error", "Reset by the peer"); + } break; } } @@ -876,6 +890,7 @@ impl Connection { Permission::Keyboard => "keyboard", Permission::Clipboard => "clipboard", Permission::Audio => "audio", + Permission::Restart => "restart", _ => "", }, &p.enabled.to_string(), diff --git a/src/ui/remote.rs b/src/ui/remote.rs index f1b2df46b..5a44b498e 100644 --- a/src/ui/remote.rs +++ b/src/ui/remote.rs @@ -629,8 +629,9 @@ impl Handler { } fn restart_remote_device(&mut self) { - self.lc.write().unwrap().restarting_remote_device = true; - let msg = self.lc.write().unwrap().restart_remote_device(); + let mut lc = self.lc.write().unwrap(); + lc.restarting_remote_device = true; + let msg = lc.restart_remote_device(); self.send(Data::Message(msg)); }