add comment

This commit is contained in:
SoLongAndThanksForAllThePizza
2022-05-28 03:56:42 +08:00
parent 7eb3a43f33
commit 9dd6e40003
7 changed files with 363 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ import 'remote_page.dart';
import 'settings_page.dart';
import 'scan_page.dart';
/// Connection page for connecting to a remote peer.
class ConnectionPage extends StatefulWidget implements PageShape {
ConnectionPage({Key? key}) : super(key: key);
@@ -26,8 +27,12 @@ class ConnectionPage extends StatefulWidget implements PageShape {
_ConnectionPageState createState() => _ConnectionPageState();
}
/// State for the connection page.
class _ConnectionPageState extends State<ConnectionPage> {
/// Controller for the id input bar.
final _idController = TextEditingController();
/// Update url. If it's not null, means an update is available.
var _updateUrl = '';
var _menuPos;
@@ -60,11 +65,15 @@ class _ConnectionPageState extends State<ConnectionPage> {
);
}
/// Callback for the connect button.
/// Connects to the selected peer.
void onConnect() {
var id = _idController.text.trim();
connect(id);
}
/// Connect to a peer with [id].
/// If [isFileTransfer], starts a session only for file transfer.
void connect(String id, {bool isFileTransfer = false}) async {
if (id == '') return;
id = id.replaceAll(' ', '');
@@ -94,6 +103,8 @@ class _ConnectionPageState extends State<ConnectionPage> {
}
}
/// UI for software update.
/// If [_updateUrl] is not empty, shows a button to update the software.
Widget getUpdateUI() {
return _updateUrl.isEmpty
? SizedBox(height: 0)
@@ -114,6 +125,8 @@ class _ConnectionPageState extends State<ConnectionPage> {
color: Colors.white, fontWeight: FontWeight.bold))));
}
/// UI for the search bar.
/// Search for a peer and connect to it if the id exists.
Widget getSearchBarUI() {
var w = Padding(
padding: const EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 0.0),
@@ -187,6 +200,7 @@ class _ConnectionPageState extends State<ConnectionPage> {
super.dispose();
}
/// Get the image for the current [platform].
Widget getPlatformImage(String platform) {
platform = platform.toLowerCase();
if (platform == 'mac os')
@@ -195,6 +209,7 @@ class _ConnectionPageState extends State<ConnectionPage> {
return Image.asset('assets/$platform.png', width: 24, height: 24);
}
/// Get all the saved peers.
Widget getPeers() {
final size = MediaQuery.of(context).size;
final space = 8.0;
@@ -244,6 +259,8 @@ class _ConnectionPageState extends State<ConnectionPage> {
return Wrap(children: cards, spacing: space, runSpacing: space);
}
/// Show the peer menu and handle user's choice.
/// User might remove the peer or send a file to the peer.
void showPeerMenu(BuildContext context, String id) async {
var value = await showMenu(
context: context,

View File

@@ -119,6 +119,7 @@ class FfiModel with ChangeNotifier {
_permissions.clear();
}
/// Bind the event listener to receive events from the Rust core.
void updateEventListener(String peerId) {
final void Function(Map<String, dynamic>) cb = (evt) {
var name = evt['name'];
@@ -179,6 +180,7 @@ class FfiModel with ChangeNotifier {
notifyListeners();
}
/// Handle the message box event based on [evt] and [id].
void handleMsgBox(Map<String, dynamic> evt, String id) {
var type = evt['type'];
var title = evt['title'];
@@ -193,6 +195,7 @@ class FfiModel with ChangeNotifier {
}
}
/// Show a message box with [type], [title] and [text].
void showMsgBox(String type, String title, String text, bool hasRetry) {
msgBox(type, title, text);
_timer?.cancel();
@@ -207,6 +210,7 @@ class FfiModel with ChangeNotifier {
}
}
/// Handle the peer info event based on [evt].
void handlePeerInfo(Map<String, dynamic> evt) {
SmartDialog.dismiss();
_pi.version = evt['version'];
@@ -649,6 +653,7 @@ class CursorModel with ChangeNotifier {
}
}
/// Mouse button enum.
enum MouseButtons { left, right, wheel }
extension ToString on MouseButtons {
@@ -664,6 +669,7 @@ extension ToString on MouseButtons {
}
}
/// FFI class for communicating with the Rust core.
class FFI {
static var id = "";
static var shift = false;
@@ -679,29 +685,35 @@ class FFI {
static final chatModel = ChatModel();
static final fileModel = FileModel();
/// Get the remote id for current client.
static String getId() {
return getByName('remote_id');
}
/// Send a mouse tap event(down and up).
static void tap(MouseButtons button) {
sendMouse('down', button);
sendMouse('up', button);
}
/// Send scroll event with scroll distance [y].
static void scroll(int y) {
setByName('send_mouse',
json.encode(modify({'type': 'wheel', 'y': y.toString()})));
}
/// Reconnect to the remote peer.
static void reconnect() {
setByName('reconnect');
FFI.ffiModel.clearPermissions();
}
/// Reset key modifiers to false, including [shift], [ctrl], [alt] and [command].
static void resetModifiers() {
shift = ctrl = alt = command = false;
}
/// Modify the given modifier map [evt] based on current modifier key status.
static Map<String, String> modify(Map<String, String> evt) {
if (ctrl) evt['ctrl'] = 'true';
if (shift) evt['shift'] = 'true';
@@ -710,12 +722,16 @@ class FFI {
return evt;
}
/// Send mouse press event.
static void sendMouse(String type, MouseButtons button) {
if (!ffiModel.keyboard()) return;
setByName('send_mouse',
json.encode(modify({'type': type, 'buttons': button.value})));
}
/// Send key stroke event.
/// [down] indicates the key's state(down or up).
/// [press] indicates a click event(down and up).
static void inputKey(String name, {bool? down, bool? press}) {
if (!ffiModel.keyboard()) return;
setByName(
@@ -727,6 +743,7 @@ class FFI {
})));
}
/// Send mouse movement event with distance in [x] and [y].
static void moveMouse(double x, double y) {
if (!ffiModel.keyboard()) return;
var x2 = x.toInt();
@@ -734,6 +751,7 @@ class FFI {
setByName('send_mouse', json.encode(modify({'x': '$x2', 'y': '$y2'})));
}
/// List the saved peers.
static List<Peer> peers() {
try {
var str = getByName('peers');
@@ -750,6 +768,7 @@ class FFI {
return [];
}
/// Connect with the given [id]. Only transfer file if [isFileTransfer].
static void connect(String id, {bool isFileTransfer = false}) {
if (isFileTransfer) {
setByName('connect_file_transfer', id);
@@ -772,6 +791,7 @@ class FFI {
return null;
}
/// Login with [password], choose if the client should [remember] it.
static void login(String password, bool remember) {
setByName(
'login',
@@ -781,6 +801,7 @@ class FFI {
}));
}
/// Close the remote session.
static void close() {
chatModel.close();
if (FFI.imageModel.image != null && !isWebDesktop) {
@@ -796,10 +817,13 @@ class FFI {
resetModifiers();
}
/// Send **get** command to the Rust core based on [name] and [arg].
/// Return the result as a string.
static String getByName(String name, [String arg = '']) {
return PlatformFFI.getByName(name, arg);
}
/// Send **set** command to the Rust core based on [name] and [value].
static void setByName(String name, [String value = '']) {
PlatformFFI.setByName(name, value);
}
@@ -953,6 +977,7 @@ void initializeCursorAndCanvas() async {
FFI.canvasModel.update(xCanvas, yCanvas, scale);
}
/// Translate text based on the pre-defined dictionary.
String translate(String name) {
if (name.startsWith('Failed to') && name.contains(': ')) {
return name.split(': ').map((x) => translate(x)).join(': ');

View File

@@ -22,6 +22,8 @@ class RgbaFrame extends Struct {
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
/// FFI wrapper around the native Rust core.
/// Hides the platform differences.
class PlatformFFI {
static Pointer<RgbaFrame>? _lastRgbaFrame;
static String _dir = '';
@@ -36,6 +38,8 @@ class PlatformFFI {
return packageInfo.version;
}
/// Send **get** command to the Rust core based on [name] and [arg].
/// Return the result as a string.
static String getByName(String name, [String arg = '']) {
if (_getByName == null) return '';
var a = name.toNativeUtf8();
@@ -49,6 +53,7 @@ class PlatformFFI {
return res;
}
/// Send **set** command to the Rust core based on [name] and [value].
static void setByName(String name, [String value = '']) {
if (_setByName == null) return;
var a = name.toNativeUtf8();
@@ -58,6 +63,7 @@ class PlatformFFI {
calloc.free(b);
}
/// Init the FFI class, loads the native Rust core library.
static Future<Null> init() async {
isIOS = Platform.isIOS;
isAndroid = Platform.isAndroid;
@@ -112,6 +118,7 @@ class PlatformFFI {
version = await getVersion();
}
/// Start listening to the Rust core's events and frames.
static void _startListenEvent(RustdeskImpl rustdeskImpl) {
() async {
await for (final message in rustdeskImpl.startEventStream()) {