2022-05-29 17:19:50 +08:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
2022-07-14 12:32:01 +08:00
|
|
|
import 'dart:io';
|
2022-05-29 17:19:50 +08:00
|
|
|
import 'dart:math';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'dart:ui' as ui;
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2020-11-28 13:22:19 +08:00
|
|
|
import 'package:flutter/services.dart';
|
2022-05-31 14:44:06 +08:00
|
|
|
import 'package:flutter_hbb/generated_bridge.dart';
|
2022-07-25 16:23:45 +08:00
|
|
|
import 'package:flutter_hbb/models/ab_model.dart';
|
2022-03-03 14:58:57 +08:00
|
|
|
import 'package:flutter_hbb/models/chat_model.dart';
|
2022-03-07 22:54:34 +08:00
|
|
|
import 'package:flutter_hbb/models/file_model.dart';
|
2022-03-19 23:28:29 +08:00
|
|
|
import 'package:flutter_hbb/models/server_model.dart';
|
2022-07-27 14:29:47 +08:00
|
|
|
import 'package:flutter_hbb/models/user_model.dart';
|
2022-04-19 13:07:45 +08:00
|
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
2020-12-21 18:28:28 +08:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2020-11-23 23:18:42 +08:00
|
|
|
import 'package:tuple/tuple.dart';
|
2022-05-29 17:19:50 +08:00
|
|
|
|
2022-02-28 18:29:25 +08:00
|
|
|
import '../common.dart';
|
2022-05-24 23:33:00 +08:00
|
|
|
import '../mobile/widgets/dialog.dart';
|
|
|
|
import '../mobile/widgets/overlay.dart';
|
2022-01-26 12:48:16 +08:00
|
|
|
import 'native_model.dart' if (dart.library.html) 'web_model.dart';
|
2022-07-27 22:56:28 +08:00
|
|
|
import 'peer_model.dart';
|
2020-11-19 00:32:46 +08:00
|
|
|
|
2022-03-19 23:28:29 +08:00
|
|
|
typedef HandleMsgBox = void Function(Map<String, dynamic> evt, String id);
|
2022-05-19 23:45:44 +08:00
|
|
|
bool _waitForImage = false;
|
2022-03-19 23:28:29 +08:00
|
|
|
|
2020-11-19 00:32:46 +08:00
|
|
|
class FfiModel with ChangeNotifier {
|
2022-02-17 15:22:14 +08:00
|
|
|
PeerInfo _pi = PeerInfo();
|
|
|
|
Display _display = Display();
|
2022-06-13 21:07:26 +08:00
|
|
|
PlatformFFI _platformFFI = PlatformFFI();
|
|
|
|
|
2022-02-02 00:46:21 +08:00
|
|
|
var _inputBlocked = false;
|
2020-11-22 21:08:19 +08:00
|
|
|
final _permissions = Map<String, bool>();
|
2022-02-17 15:22:14 +08:00
|
|
|
bool? _secure;
|
|
|
|
bool? _direct;
|
2022-04-28 22:44:54 +08:00
|
|
|
bool _touchMode = false;
|
2022-03-28 17:24:52 +08:00
|
|
|
Timer? _timer;
|
|
|
|
var _reconnects = 1;
|
2022-06-13 21:07:26 +08:00
|
|
|
WeakReference<FFI> parent;
|
2020-11-22 21:08:19 +08:00
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
Map<String, bool> get permissions => _permissions;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
Display get display => _display;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
PlatformFFI get platformFFI => _platformFFI;
|
|
|
|
|
|
|
|
set platformFFI(PlatformFFI value) {
|
|
|
|
_platformFFI = value;
|
|
|
|
}
|
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
bool? get secure => _secure;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
bool? get direct => _direct;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
PeerInfo get pi => _pi;
|
2022-02-17 15:22:14 +08:00
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
bool get inputBlocked => _inputBlocked;
|
|
|
|
|
|
|
|
bool get touchMode => _touchMode;
|
|
|
|
|
|
|
|
bool get isPeerAndroid => _pi.platform == "Android";
|
2022-02-02 00:46:21 +08:00
|
|
|
|
|
|
|
set inputBlocked(v) {
|
|
|
|
_inputBlocked = v;
|
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
FfiModel(this.parent) {
|
2021-08-02 20:54:56 +08:00
|
|
|
Translator.call = translate;
|
2020-11-22 21:08:19 +08:00
|
|
|
clear();
|
2022-04-17 00:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> init() async {
|
2022-06-13 21:07:26 +08:00
|
|
|
await _platformFFI.init();
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
void toggleTouchMode() {
|
|
|
|
if (!isPeerAndroid) {
|
|
|
|
_touchMode = !_touchMode;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 21:08:19 +08:00
|
|
|
void updatePermission(Map<String, dynamic> evt) {
|
|
|
|
evt.forEach((k, v) {
|
2022-08-04 17:24:02 +08:00
|
|
|
if (k == 'name' || k.isEmpty) return;
|
2020-11-22 21:08:19 +08:00
|
|
|
_permissions[k] = v == 'true';
|
|
|
|
});
|
2020-11-23 17:45:38 +08:00
|
|
|
print('$_permissions');
|
2022-02-06 16:29:56 +08:00
|
|
|
notifyListeners();
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 15:44:45 +08:00
|
|
|
void updateUser() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-11-24 23:36:46 +08:00
|
|
|
bool keyboard() => _permissions['keyboard'] != false;
|
2020-11-23 23:18:42 +08:00
|
|
|
|
2020-11-19 00:32:46 +08:00
|
|
|
void clear() {
|
2020-11-22 21:08:19 +08:00
|
|
|
_pi = PeerInfo();
|
|
|
|
_display = Display();
|
|
|
|
_waitForImage = false;
|
2020-11-29 00:13:55 +08:00
|
|
|
_secure = null;
|
|
|
|
_direct = null;
|
2022-02-02 00:46:21 +08:00
|
|
|
_inputBlocked = false;
|
2022-03-28 17:24:52 +08:00
|
|
|
_timer?.cancel();
|
|
|
|
_timer = null;
|
2020-11-28 13:22:19 +08:00
|
|
|
clearPermissions();
|
|
|
|
}
|
|
|
|
|
2020-11-29 00:13:55 +08:00
|
|
|
void setConnectionType(bool secure, bool direct) {
|
|
|
|
_secure = secure;
|
|
|
|
_direct = direct;
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:22:14 +08:00
|
|
|
Image? getConnectionImage() {
|
|
|
|
String? icon;
|
2020-11-29 00:13:55 +08:00
|
|
|
if (secure == true && direct == true) {
|
|
|
|
icon = 'secure';
|
|
|
|
} else if (secure == false && direct == true) {
|
|
|
|
icon = 'insecure';
|
|
|
|
} else if (secure == false && direct == false) {
|
|
|
|
icon = 'insecure_relay';
|
|
|
|
} else if (secure == true && direct == false) {
|
|
|
|
icon = 'secure_relay';
|
|
|
|
}
|
|
|
|
return icon == null
|
|
|
|
? null
|
|
|
|
: Image.asset('assets/$icon.png', width: 48, height: 48);
|
|
|
|
}
|
|
|
|
|
2020-11-28 13:22:19 +08:00
|
|
|
void clearPermissions() {
|
2022-02-02 00:46:21 +08:00
|
|
|
_inputBlocked = false;
|
2020-11-22 21:08:19 +08:00
|
|
|
_permissions.clear();
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
2022-03-07 22:54:34 +08:00
|
|
|
|
2022-05-31 17:36:36 +08:00
|
|
|
void Function(Map<String, dynamic>) startEventListener(String peerId) {
|
|
|
|
return (evt) {
|
|
|
|
var name = evt['name'];
|
|
|
|
if (name == 'msgbox') {
|
|
|
|
handleMsgBox(evt, peerId);
|
|
|
|
} else if (name == 'peer_info') {
|
2022-05-31 22:09:36 +08:00
|
|
|
handlePeerInfo(evt, peerId);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'connection_ready') {
|
2022-06-13 21:07:26 +08:00
|
|
|
setConnectionType(evt['secure'] == 'true', evt['direct'] == 'true');
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'switch_display') {
|
|
|
|
handleSwitchDisplay(evt);
|
|
|
|
} else if (name == 'cursor_data') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.updateCursorData(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'cursor_id') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.updateCursorId(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'cursor_position') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.updateCursorPosition(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'clipboard') {
|
|
|
|
Clipboard.setData(ClipboardData(text: evt['content']));
|
|
|
|
} else if (name == 'permission') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.ffiModel.updatePermission(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'chat_client_mode') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.chatModel
|
|
|
|
.receive(ChatModel.clientModeID, evt['text'] ?? "");
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'chat_server_mode') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.chatModel
|
2022-05-31 17:36:36 +08:00
|
|
|
.receive(int.parse(evt['id'] as String), evt['text'] ?? "");
|
|
|
|
} else if (name == 'file_dir') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.receiveFileDir(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'job_progress') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.tryUpdateJobProgress(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'job_done') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.jobDone(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'job_error') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.jobError(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'override_file_confirm') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.overrideFileConfirm(evt);
|
2022-07-11 18:23:58 +08:00
|
|
|
} else if (name == 'load_last_job') {
|
|
|
|
parent.target?.fileModel.loadLastJob(evt);
|
2022-07-11 10:30:45 +08:00
|
|
|
} else if (name == 'update_folder_files') {
|
|
|
|
parent.target?.fileModel.updateFolderFiles(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'try_start_without_auth') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.serverModel.loginRequest(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'on_client_authorized') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.serverModel.onClientAuthorized(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'on_client_remove') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.serverModel.onClientRemove(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Bind the event listener to receive events from the Rust core.
|
2022-05-17 19:59:37 +08:00
|
|
|
void updateEventListener(String peerId) {
|
|
|
|
final void Function(Map<String, dynamic>) cb = (evt) {
|
2020-11-19 00:32:46 +08:00
|
|
|
var name = evt['name'];
|
|
|
|
if (name == 'msgbox') {
|
2022-03-19 23:28:29 +08:00
|
|
|
handleMsgBox(evt, peerId);
|
2020-11-19 00:32:46 +08:00
|
|
|
} else if (name == 'peer_info') {
|
2022-05-31 22:09:36 +08:00
|
|
|
handlePeerInfo(evt, peerId);
|
2020-11-29 00:13:55 +08:00
|
|
|
} else if (name == 'connection_ready') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.ffiModel.setConnectionType(
|
2020-11-29 00:13:55 +08:00
|
|
|
evt['secure'] == 'true', evt['direct'] == 'true');
|
2020-11-19 00:32:46 +08:00
|
|
|
} else if (name == 'switch_display') {
|
|
|
|
handleSwitchDisplay(evt);
|
|
|
|
} else if (name == 'cursor_data') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.updateCursorData(evt);
|
2020-11-19 00:32:46 +08:00
|
|
|
} else if (name == 'cursor_id') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.updateCursorId(evt);
|
2020-11-19 00:32:46 +08:00
|
|
|
} else if (name == 'cursor_position') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.updateCursorPosition(evt);
|
2020-11-28 13:22:19 +08:00
|
|
|
} else if (name == 'clipboard') {
|
|
|
|
Clipboard.setData(ClipboardData(text: evt['content']));
|
2020-11-22 21:08:19 +08:00
|
|
|
} else if (name == 'permission') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.ffiModel.updatePermission(evt);
|
2022-03-22 16:40:23 +08:00
|
|
|
} else if (name == 'chat_client_mode') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.chatModel
|
|
|
|
.receive(ChatModel.clientModeID, evt['text'] ?? "");
|
2022-03-22 16:40:23 +08:00
|
|
|
} else if (name == 'chat_server_mode') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.chatModel
|
2022-03-28 15:44:45 +08:00
|
|
|
.receive(int.parse(evt['id'] as String), evt['text'] ?? "");
|
2022-03-07 22:54:34 +08:00
|
|
|
} else if (name == 'file_dir') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.receiveFileDir(evt);
|
2022-03-19 23:28:29 +08:00
|
|
|
} else if (name == 'job_progress') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.tryUpdateJobProgress(evt);
|
2022-03-19 23:28:29 +08:00
|
|
|
} else if (name == 'job_done') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.jobDone(evt);
|
2022-03-19 23:28:29 +08:00
|
|
|
} else if (name == 'job_error') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.jobError(evt);
|
2022-05-17 20:56:36 +08:00
|
|
|
} else if (name == 'override_file_confirm') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.overrideFileConfirm(evt);
|
2022-07-11 18:23:58 +08:00
|
|
|
} else if (name == 'load_last_job') {
|
|
|
|
parent.target?.fileModel.loadLastJob(evt);
|
2022-07-11 10:30:45 +08:00
|
|
|
} else if (name == 'update_folder_files') {
|
|
|
|
parent.target?.fileModel.updateFolderFiles(evt);
|
2022-03-19 23:28:29 +08:00
|
|
|
} else if (name == 'try_start_without_auth') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.serverModel.loginRequest(evt);
|
2022-03-22 21:47:42 +08:00
|
|
|
} else if (name == 'on_client_authorized') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.serverModel.onClientAuthorized(evt);
|
2022-03-19 23:28:29 +08:00
|
|
|
} else if (name == 'on_client_remove') {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.serverModel.onClientRemove(evt);
|
2022-08-04 17:24:02 +08:00
|
|
|
} else if (name == 'update_quality_status') {
|
|
|
|
parent.target?.qualityMonitorModel.updateQualityStatus(evt);
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
2022-05-17 19:59:37 +08:00
|
|
|
};
|
2022-06-13 21:07:26 +08:00
|
|
|
platformFFI.setEventCallback(cb);
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleSwitchDisplay(Map<String, dynamic> evt) {
|
2022-06-02 17:16:23 +08:00
|
|
|
final oldOrientation = _display.width > _display.height;
|
2020-12-24 10:44:44 +08:00
|
|
|
var old = _pi.currentDisplay;
|
2020-11-19 00:32:46 +08:00
|
|
|
_pi.currentDisplay = int.parse(evt['display']);
|
|
|
|
_display.x = double.parse(evt['x']);
|
|
|
|
_display.y = double.parse(evt['y']);
|
|
|
|
_display.width = int.parse(evt['width']);
|
|
|
|
_display.height = int.parse(evt['height']);
|
2020-12-24 10:44:44 +08:00
|
|
|
if (old != _pi.currentDisplay)
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.updateDisplayOrigin(_display.x, _display.y);
|
2022-06-02 17:16:23 +08:00
|
|
|
|
|
|
|
// remote is mobile, and orientation changed
|
|
|
|
if ((_display.width > _display.height) != oldOrientation) {
|
2022-06-27 11:18:53 +08:00
|
|
|
gFFI.canvasModel.updateViewStyle();
|
2022-06-02 17:16:23 +08:00
|
|
|
}
|
2020-11-25 23:52:58 +08:00
|
|
|
notifyListeners();
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Handle the message box event based on [evt] and [id].
|
2022-03-28 17:24:52 +08:00
|
|
|
void handleMsgBox(Map<String, dynamic> evt, String id) {
|
|
|
|
var type = evt['type'];
|
|
|
|
var title = evt['title'];
|
|
|
|
var text = evt['text'];
|
|
|
|
if (type == 're-input-password') {
|
|
|
|
wrongPasswordDialog(id);
|
|
|
|
} else if (type == 'input-password') {
|
|
|
|
enterPasswordDialog(id);
|
2022-08-04 17:24:02 +08:00
|
|
|
} else if (type == 'restarting') {
|
|
|
|
showMsgBox(id, type, title, text, false, hasCancel: false);
|
2022-03-28 17:24:52 +08:00
|
|
|
} else {
|
|
|
|
var hasRetry = evt['hasRetry'] == 'true';
|
2022-05-31 22:09:36 +08:00
|
|
|
showMsgBox(id, type, title, text, hasRetry);
|
2022-03-28 17:24:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Show a message box with [type], [title] and [text].
|
2022-05-31 22:09:36 +08:00
|
|
|
void showMsgBox(
|
2022-08-04 17:24:02 +08:00
|
|
|
String id, String type, String title, String text, bool hasRetry,
|
|
|
|
{bool? hasCancel}) {
|
|
|
|
msgBox(type, title, text, hasCancel: hasCancel);
|
2022-05-16 00:01:27 +08:00
|
|
|
_timer?.cancel();
|
2022-03-28 17:24:52 +08:00
|
|
|
if (hasRetry) {
|
|
|
|
_timer = Timer(Duration(seconds: _reconnects), () {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.bind.sessionReconnect(id: id);
|
2022-05-31 22:09:36 +08:00
|
|
|
clearPermissions();
|
2022-03-28 17:24:52 +08:00
|
|
|
showLoading(translate('Connecting...'));
|
|
|
|
});
|
|
|
|
_reconnects *= 2;
|
|
|
|
} else {
|
|
|
|
_reconnects = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Handle the peer info event based on [evt].
|
2022-05-31 22:09:36 +08:00
|
|
|
void handlePeerInfo(Map<String, dynamic> evt, String peerId) async {
|
2022-04-19 13:07:45 +08:00
|
|
|
SmartDialog.dismiss();
|
2020-11-27 17:34:09 +08:00
|
|
|
_pi.version = evt['version'];
|
2020-11-19 00:32:46 +08:00
|
|
|
_pi.username = evt['username'];
|
|
|
|
_pi.hostname = evt['hostname'];
|
|
|
|
_pi.platform = evt['platform'];
|
|
|
|
_pi.sasEnabled = evt['sas_enabled'] == "true";
|
|
|
|
_pi.currentDisplay = int.parse(evt['current_display']);
|
2022-03-16 15:33:00 +08:00
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
if (isPeerAndroid) {
|
|
|
|
_touchMode = true;
|
2022-06-13 21:07:26 +08:00
|
|
|
if (parent.target?.ffiModel.permissions['keyboard'] != false) {
|
2022-05-16 00:01:27 +08:00
|
|
|
Timer(Duration(milliseconds: 100), showMobileActionsOverlay);
|
2022-04-28 22:44:54 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-13 21:07:26 +08:00
|
|
|
_touchMode = await parent.target?.bind
|
|
|
|
.getSessionOption(id: peerId, arg: "touch-mode") !=
|
|
|
|
'';
|
2022-04-28 22:44:54 +08:00
|
|
|
}
|
|
|
|
|
2022-03-19 23:28:29 +08:00
|
|
|
if (evt['is_file_transfer'] == "true") {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.fileModel.onReady();
|
2022-03-19 23:28:29 +08:00
|
|
|
} else {
|
2022-03-16 15:33:00 +08:00
|
|
|
_pi.displays = [];
|
|
|
|
List<dynamic> displays = json.decode(evt['displays']);
|
|
|
|
for (int i = 0; i < displays.length; ++i) {
|
|
|
|
Map<String, dynamic> d0 = displays[i];
|
|
|
|
var d = Display();
|
|
|
|
d.x = d0['x'].toDouble();
|
|
|
|
d.y = d0['y'].toDouble();
|
|
|
|
d.width = d0['width'];
|
|
|
|
d.height = d0['height'];
|
|
|
|
_pi.displays.add(d);
|
|
|
|
}
|
|
|
|
if (_pi.currentDisplay < _pi.displays.length) {
|
|
|
|
_display = _pi.displays[_pi.currentDisplay];
|
|
|
|
}
|
|
|
|
if (displays.length > 0) {
|
|
|
|
showLoading(translate('Connected, waiting for image...'));
|
|
|
|
_waitForImage = true;
|
2022-04-26 21:21:08 +08:00
|
|
|
_reconnects = 1;
|
2022-03-16 15:33:00 +08:00
|
|
|
}
|
2020-11-19 00:53:10 +08:00
|
|
|
}
|
2020-11-29 14:28:07 +08:00
|
|
|
notifyListeners();
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImageModel with ChangeNotifier {
|
2022-02-17 15:22:14 +08:00
|
|
|
ui.Image? _image;
|
2020-11-19 00:32:46 +08:00
|
|
|
|
2022-02-17 15:22:14 +08:00
|
|
|
ui.Image? get image => _image;
|
2020-11-19 00:32:46 +08:00
|
|
|
|
2022-06-01 15:09:48 +08:00
|
|
|
String _id = "";
|
2022-05-31 22:09:36 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
WeakReference<FFI> parent;
|
|
|
|
|
|
|
|
ImageModel(this.parent);
|
|
|
|
|
2022-08-03 15:31:19 +08:00
|
|
|
void onRgba(Uint8List rgba, double tabBarHeight) {
|
2022-05-31 22:09:36 +08:00
|
|
|
if (_waitForImage) {
|
|
|
|
_waitForImage = false;
|
|
|
|
SmartDialog.dismiss();
|
|
|
|
}
|
2022-06-13 21:07:26 +08:00
|
|
|
final pid = parent.target?.id;
|
2022-05-31 22:09:36 +08:00
|
|
|
ui.decodeImageFromPixels(
|
|
|
|
rgba,
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.ffiModel.display.width ?? 0,
|
|
|
|
parent.target?.ffiModel.display.height ?? 0,
|
2022-05-31 22:09:36 +08:00
|
|
|
isWeb ? ui.PixelFormat.rgba8888 : ui.PixelFormat.bgra8888, (image) {
|
2022-06-13 21:07:26 +08:00
|
|
|
if (parent.target?.id != pid) return;
|
2022-05-31 22:09:36 +08:00
|
|
|
try {
|
|
|
|
// my throw exception, because the listener maybe already dispose
|
2022-08-03 15:31:19 +08:00
|
|
|
update(image, tabBarHeight);
|
2022-05-31 22:09:36 +08:00
|
|
|
} catch (e) {
|
|
|
|
print('update image: $e');
|
2022-05-19 23:45:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-03 15:31:19 +08:00
|
|
|
void update(ui.Image? image, double tabBarHeight) {
|
2020-11-24 22:03:04 +08:00
|
|
|
if (_image == null && image != null) {
|
2022-05-23 16:02:37 +08:00
|
|
|
if (isWebDesktop) {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.canvasModel.updateViewStyle();
|
2022-02-03 00:53:59 +08:00
|
|
|
} else {
|
2022-03-07 22:54:34 +08:00
|
|
|
final size = MediaQueryData.fromWindow(ui.window).size;
|
2022-08-03 15:31:19 +08:00
|
|
|
final canvasWidth = size.width;
|
|
|
|
final canvasHeight = size.height - tabBarHeight;
|
|
|
|
final xscale = canvasWidth / image.width;
|
|
|
|
final yscale = canvasHeight / image.height;
|
|
|
|
parent.target?.canvasModel.scale = min(xscale, yscale);
|
2022-06-13 21:07:26 +08:00
|
|
|
}
|
|
|
|
if (parent.target != null) {
|
|
|
|
initializeCursorAndCanvas(parent.target!);
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
2022-05-10 14:44:47 +08:00
|
|
|
Future.delayed(Duration(milliseconds: 1), () {
|
2022-06-13 21:07:26 +08:00
|
|
|
if (parent.target?.ffiModel.isPeerAndroid ?? false) {
|
|
|
|
parent.target?.bind
|
2022-06-01 15:09:48 +08:00
|
|
|
.sessionPeerOption(id: _id, name: "view-style", value: "shrink");
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.canvasModel.updateViewStyle();
|
2022-05-10 14:44:47 +08:00
|
|
|
}
|
|
|
|
});
|
2020-11-24 22:03:04 +08:00
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
_image = image;
|
2020-11-19 18:22:06 +08:00
|
|
|
if (image != null) notifyListeners();
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
2020-11-24 12:11:55 +08:00
|
|
|
|
2022-08-03 15:31:19 +08:00
|
|
|
// mobile only
|
|
|
|
// for desktop, height should minus tabbar height
|
2020-11-24 12:11:55 +08:00
|
|
|
double get maxScale {
|
2022-07-02 21:24:49 +08:00
|
|
|
if (_image == null) return 1.5;
|
2022-03-07 22:54:34 +08:00
|
|
|
final size = MediaQueryData.fromWindow(ui.window).size;
|
2022-02-17 15:22:14 +08:00
|
|
|
final xscale = size.width / _image!.width;
|
|
|
|
final yscale = size.height / _image!.height;
|
2022-07-02 21:24:49 +08:00
|
|
|
return max(1.5, max(xscale, yscale));
|
2020-11-24 12:11:55 +08:00
|
|
|
}
|
|
|
|
|
2022-08-03 15:31:19 +08:00
|
|
|
// mobile only
|
|
|
|
// for desktop, height should minus tabbar height
|
2020-11-24 12:11:55 +08:00
|
|
|
double get minScale {
|
2022-07-02 21:24:49 +08:00
|
|
|
if (_image == null) return 1.5;
|
2022-03-07 22:54:34 +08:00
|
|
|
final size = MediaQueryData.fromWindow(ui.window).size;
|
2022-02-17 15:22:14 +08:00
|
|
|
final xscale = size.width / _image!.width;
|
|
|
|
final yscale = size.height / _image!.height;
|
2022-07-02 21:24:49 +08:00
|
|
|
return min(xscale, yscale) / 1.5;
|
2020-11-24 12:11:55 +08:00
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2020-11-23 23:18:42 +08:00
|
|
|
class CanvasModel with ChangeNotifier {
|
2022-02-17 15:22:14 +08:00
|
|
|
double _x = 0;
|
|
|
|
double _y = 0;
|
|
|
|
double _scale = 1.0;
|
2022-08-03 15:31:19 +08:00
|
|
|
double _tabBarHeight = 0.0;
|
2022-05-31 22:09:36 +08:00
|
|
|
String id = ""; // TODO multi canvas model
|
2020-11-23 23:18:42 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
WeakReference<FFI> parent;
|
|
|
|
|
|
|
|
CanvasModel(this.parent);
|
2020-11-23 23:18:42 +08:00
|
|
|
|
|
|
|
double get x => _x;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2020-11-23 23:18:42 +08:00
|
|
|
double get y => _y;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2020-11-23 23:18:42 +08:00
|
|
|
double get scale => _scale;
|
|
|
|
|
2022-08-03 15:31:19 +08:00
|
|
|
set tabBarHeight(double h) => _tabBarHeight = h;
|
|
|
|
double get tabBarHeight => _tabBarHeight;
|
|
|
|
|
2022-05-31 22:09:36 +08:00
|
|
|
void updateViewStyle() async {
|
2022-06-13 21:07:26 +08:00
|
|
|
final s =
|
|
|
|
await parent.target?.bind.getSessionOption(id: id, arg: 'view-style');
|
2022-05-31 22:09:36 +08:00
|
|
|
if (s == null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-07 22:54:34 +08:00
|
|
|
final size = MediaQueryData.fromWindow(ui.window).size;
|
2022-08-03 15:31:19 +08:00
|
|
|
final canvasWidth = size.width;
|
|
|
|
final canvasHeight = size.height - _tabBarHeight;
|
|
|
|
final s1 = canvasWidth / (parent.target?.ffiModel.display.width ?? 720);
|
|
|
|
final s2 = canvasHeight / (parent.target?.ffiModel.display.height ?? 1280);
|
2022-06-27 16:50:02 +08:00
|
|
|
// Closure to perform shrink operation.
|
|
|
|
final shrinkOp = () {
|
2022-02-03 00:53:59 +08:00
|
|
|
final s = s1 < s2 ? s1 : s2;
|
|
|
|
if (s < 1) {
|
|
|
|
_scale = s;
|
|
|
|
}
|
2022-06-27 16:50:02 +08:00
|
|
|
};
|
|
|
|
// Closure to perform stretch operation.
|
|
|
|
final stretchOp = () {
|
2022-02-03 00:53:59 +08:00
|
|
|
final s = s1 > s2 ? s1 : s2;
|
|
|
|
if (s > 1) {
|
|
|
|
_scale = s;
|
|
|
|
}
|
2022-06-27 16:50:02 +08:00
|
|
|
};
|
|
|
|
// Closure to perform default operation(set the scale to 1.0).
|
|
|
|
final defaultOp = () {
|
|
|
|
_scale = 1.0;
|
|
|
|
};
|
|
|
|
if (s == 'shrink') {
|
|
|
|
shrinkOp();
|
|
|
|
} else if (s == 'stretch') {
|
|
|
|
stretchOp();
|
2022-02-03 00:53:59 +08:00
|
|
|
} else {
|
2022-06-27 16:50:02 +08:00
|
|
|
// On desktop, shrink is the default behavior.
|
|
|
|
if (isDesktop) {
|
|
|
|
shrinkOp();
|
|
|
|
} else {
|
|
|
|
defaultOp();
|
|
|
|
}
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
2022-08-03 15:31:19 +08:00
|
|
|
_x = (canvasWidth - getDisplayWidth() * _scale) / 2;
|
|
|
|
_y = (canvasHeight - getDisplayHeight() * _scale) / 2;
|
2022-02-03 00:53:59 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-12-21 18:28:28 +08:00
|
|
|
void update(double x, double y, double scale) {
|
|
|
|
_x = x;
|
|
|
|
_y = y;
|
|
|
|
_scale = scale;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
int getDisplayWidth() {
|
|
|
|
return parent.target?.ffiModel.display.width ?? 1080;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getDisplayHeight() {
|
|
|
|
return parent.target?.ffiModel.display.height ?? 720;
|
|
|
|
}
|
|
|
|
|
2022-02-06 16:29:56 +08:00
|
|
|
void moveDesktopMouse(double x, double y) {
|
2022-06-27 16:50:02 +08:00
|
|
|
// On mobile platforms, move the canvas with the cursor.
|
|
|
|
if (!isDesktop) {
|
|
|
|
final size = MediaQueryData.fromWindow(ui.window).size;
|
2022-08-03 15:31:19 +08:00
|
|
|
final canvasWidth = size.width;
|
|
|
|
final canvasHeight = size.height - _tabBarHeight;
|
2022-06-27 16:50:02 +08:00
|
|
|
final dw = getDisplayWidth() * _scale;
|
|
|
|
final dh = getDisplayHeight() * _scale;
|
|
|
|
var dxOffset = 0;
|
|
|
|
var dyOffset = 0;
|
2022-08-03 15:31:19 +08:00
|
|
|
if (dw > canvasWidth) {
|
|
|
|
dxOffset = (x - dw * (x / canvasWidth) - _x).toInt();
|
2022-06-27 16:50:02 +08:00
|
|
|
}
|
2022-08-03 15:31:19 +08:00
|
|
|
if (dh > canvasHeight) {
|
|
|
|
dyOffset = (y - dh * (y / canvasHeight) - _y).toInt();
|
2022-06-27 16:50:02 +08:00
|
|
|
}
|
|
|
|
_x += dxOffset;
|
|
|
|
_y += dyOffset;
|
|
|
|
if (dxOffset != 0 || dyOffset != 0) {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2022-02-06 16:29:56 +08:00
|
|
|
}
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.moveLocal(x, y);
|
2022-02-06 16:29:56 +08:00
|
|
|
}
|
|
|
|
|
2020-11-24 12:11:55 +08:00
|
|
|
set scale(v) {
|
|
|
|
_scale = v;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-11-24 22:03:04 +08:00
|
|
|
void panX(double dx) {
|
|
|
|
_x += dx;
|
|
|
|
notifyListeners();
|
2020-11-23 23:18:42 +08:00
|
|
|
}
|
|
|
|
|
2020-11-27 17:59:42 +08:00
|
|
|
void resetOffset() {
|
2022-05-23 16:02:37 +08:00
|
|
|
if (isWebDesktop) {
|
2022-02-03 00:53:59 +08:00
|
|
|
updateViewStyle();
|
|
|
|
} else {
|
2022-08-03 15:31:19 +08:00
|
|
|
final size = MediaQueryData.fromWindow(ui.window).size;
|
|
|
|
final canvasWidth = size.width;
|
|
|
|
final canvasHeight = size.height - _tabBarHeight;
|
|
|
|
_x = (canvasWidth - getDisplayWidth() * _scale) / 2;
|
|
|
|
_y = (canvasHeight - getDisplayHeight() * _scale) / 2;
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
2020-11-27 17:59:42 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-11-24 22:03:04 +08:00
|
|
|
void panY(double dy) {
|
2020-11-23 23:18:42 +08:00
|
|
|
_y += dy;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateScale(double v) {
|
2022-06-13 21:07:26 +08:00
|
|
|
if (parent.target?.imageModel.image == null) return;
|
|
|
|
final offset = parent.target?.cursorModel.offset ?? Offset(0, 0);
|
|
|
|
var r = parent.target?.cursorModel.getVisibleRect() ?? Rect.zero;
|
2020-11-25 14:41:57 +08:00
|
|
|
final px0 = (offset.dx - r.left) * _scale;
|
|
|
|
final py0 = (offset.dy - r.top) * _scale;
|
2020-11-23 23:18:42 +08:00
|
|
|
_scale *= v;
|
2022-06-13 21:07:26 +08:00
|
|
|
final maxs = parent.target?.imageModel.maxScale ?? 1;
|
|
|
|
final mins = parent.target?.imageModel.minScale ?? 1;
|
2020-11-24 12:11:55 +08:00
|
|
|
if (_scale > maxs) _scale = maxs;
|
|
|
|
if (_scale < mins) _scale = mins;
|
2022-06-13 21:07:26 +08:00
|
|
|
r = parent.target?.cursorModel.getVisibleRect() ?? Rect.zero;
|
2020-11-25 14:41:57 +08:00
|
|
|
final px1 = (offset.dx - r.left) * _scale;
|
|
|
|
final py1 = (offset.dy - r.top) * _scale;
|
|
|
|
_x -= px1 - px0;
|
|
|
|
_y -= py1 - py0;
|
2020-11-23 23:18:42 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-02-02 17:25:56 +08:00
|
|
|
void clear([bool notify = false]) {
|
2020-11-23 23:18:42 +08:00
|
|
|
_x = 0;
|
|
|
|
_y = 0;
|
|
|
|
_scale = 1.0;
|
2021-08-22 07:50:12 +08:00
|
|
|
if (notify) notifyListeners();
|
2020-11-23 23:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:32:46 +08:00
|
|
|
class CursorModel with ChangeNotifier {
|
2022-02-17 15:22:14 +08:00
|
|
|
ui.Image? _image;
|
2020-11-23 23:18:42 +08:00
|
|
|
final _images = Map<int, Tuple3<ui.Image, double, double>>();
|
2020-11-22 18:29:04 +08:00
|
|
|
double _x = -10000;
|
|
|
|
double _y = -10000;
|
2020-11-19 00:32:46 +08:00
|
|
|
double _hotx = 0;
|
|
|
|
double _hoty = 0;
|
|
|
|
double _displayOriginX = 0;
|
|
|
|
double _displayOriginY = 0;
|
2022-05-31 22:09:36 +08:00
|
|
|
String id = ""; // TODO multi cursor model
|
2022-06-13 21:07:26 +08:00
|
|
|
WeakReference<FFI> parent;
|
2020-11-19 00:32:46 +08:00
|
|
|
|
2022-02-17 15:22:14 +08:00
|
|
|
ui.Image? get image => _image;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2020-11-23 23:18:42 +08:00
|
|
|
double get x => _x - _displayOriginX;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2020-11-23 23:18:42 +08:00
|
|
|
double get y => _y - _displayOriginY;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2020-11-25 14:41:57 +08:00
|
|
|
Offset get offset => Offset(_x, _y);
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2020-11-23 23:18:42 +08:00
|
|
|
double get hotx => _hotx;
|
2022-02-02 17:25:56 +08:00
|
|
|
|
2020-11-23 23:18:42 +08:00
|
|
|
double get hoty => _hoty;
|
2020-11-19 00:32:46 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
CursorModel(this.parent);
|
|
|
|
|
2020-11-25 14:41:57 +08:00
|
|
|
// remote physical display coordinate
|
2020-11-24 22:03:04 +08:00
|
|
|
Rect getVisibleRect() {
|
2022-03-07 22:54:34 +08:00
|
|
|
final size = MediaQueryData.fromWindow(ui.window).size;
|
2022-06-13 21:07:26 +08:00
|
|
|
final xoffset = parent.target?.canvasModel.x ?? 0;
|
|
|
|
final yoffset = parent.target?.canvasModel.y ?? 0;
|
|
|
|
final scale = parent.target?.canvasModel.scale ?? 1;
|
2020-11-24 22:03:04 +08:00
|
|
|
final x0 = _displayOriginX - xoffset / scale;
|
|
|
|
final y0 = _displayOriginY - yoffset / scale;
|
|
|
|
return Rect.fromLTWH(x0, y0, size.width / scale, size.height / scale);
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:20:40 +08:00
|
|
|
double adjustForKeyboard() {
|
2020-12-21 17:26:23 +08:00
|
|
|
final m = MediaQueryData.fromWindow(ui.window);
|
|
|
|
var keyboardHeight = m.viewInsets.bottom;
|
|
|
|
final size = m.size;
|
2020-11-25 11:20:40 +08:00
|
|
|
if (keyboardHeight < 100) return 0;
|
2022-06-13 21:07:26 +08:00
|
|
|
final s = parent.target?.canvasModel.scale ?? 1.0;
|
2020-12-21 17:26:23 +08:00
|
|
|
final thresh = (size.height - keyboardHeight) / 2;
|
2020-11-25 14:41:57 +08:00
|
|
|
var h = (_y - getVisibleRect().top) * s; // local physical display height
|
2020-11-27 12:05:23 +08:00
|
|
|
return h - thresh;
|
2020-11-25 11:20:40 +08:00
|
|
|
}
|
|
|
|
|
2022-02-17 18:00:44 +08:00
|
|
|
void touch(double x, double y, MouseButtons button) {
|
|
|
|
moveLocal(x, y);
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.moveMouse(_x, _y);
|
|
|
|
parent.target?.tap(button);
|
2022-02-06 16:29:56 +08:00
|
|
|
}
|
|
|
|
|
2022-03-07 22:54:34 +08:00
|
|
|
void move(double x, double y) {
|
2022-02-17 18:00:44 +08:00
|
|
|
moveLocal(x, y);
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.moveMouse(_x, _y);
|
2022-02-17 18:00:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void moveLocal(double x, double y) {
|
2022-06-13 21:07:26 +08:00
|
|
|
final scale = parent.target?.canvasModel.scale ?? 1.0;
|
|
|
|
final xoffset = parent.target?.canvasModel.x ?? 0;
|
|
|
|
final yoffset = parent.target?.canvasModel.y ?? 0;
|
2021-08-21 17:18:14 +08:00
|
|
|
_x = (x - xoffset) / scale + _displayOriginX;
|
|
|
|
_y = (y - yoffset) / scale + _displayOriginY;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
_x = _displayOriginX;
|
|
|
|
_y = _displayOriginY;
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.moveMouse(_x, _y);
|
|
|
|
parent.target?.canvasModel.clear(true);
|
2021-08-21 17:18:14 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:54:34 +08:00
|
|
|
void updatePan(double dx, double dy, bool touchMode) {
|
2022-06-13 21:07:26 +08:00
|
|
|
if (parent.target?.imageModel.image == null) return;
|
2021-08-21 17:18:14 +08:00
|
|
|
if (touchMode) {
|
2022-06-13 21:07:26 +08:00
|
|
|
final scale = parent.target?.canvasModel.scale ?? 1.0;
|
2022-04-19 13:07:45 +08:00
|
|
|
_x += dx / scale;
|
|
|
|
_y += dy / scale;
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.moveMouse(_x, _y);
|
2022-04-19 13:07:45 +08:00
|
|
|
notifyListeners();
|
2021-08-21 17:18:14 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-06-13 21:07:26 +08:00
|
|
|
final scale = parent.target?.canvasModel.scale ?? 1.0;
|
2020-11-25 00:13:23 +08:00
|
|
|
dx /= scale;
|
|
|
|
dy /= scale;
|
2020-11-24 22:03:04 +08:00
|
|
|
final r = getVisibleRect();
|
|
|
|
var cx = r.center.dx;
|
|
|
|
var cy = r.center.dy;
|
|
|
|
var tryMoveCanvasX = false;
|
|
|
|
if (dx > 0) {
|
2022-05-11 22:34:41 +08:00
|
|
|
final maxCanvasCanMove = _displayOriginX +
|
2022-06-13 21:07:26 +08:00
|
|
|
(parent.target?.imageModel.image!.width ?? 1280) -
|
2022-05-11 22:34:41 +08:00
|
|
|
r.right.roundToDouble();
|
2020-11-24 22:03:04 +08:00
|
|
|
tryMoveCanvasX = _x + dx > cx && maxCanvasCanMove > 0;
|
|
|
|
if (tryMoveCanvasX) {
|
|
|
|
dx = min(dx, maxCanvasCanMove);
|
|
|
|
} else {
|
|
|
|
final maxCursorCanMove = r.right - _x;
|
|
|
|
dx = min(dx, maxCursorCanMove);
|
|
|
|
}
|
|
|
|
} else if (dx < 0) {
|
2022-05-11 22:34:41 +08:00
|
|
|
final maxCanvasCanMove = _displayOriginX - r.left.roundToDouble();
|
2020-11-24 22:03:04 +08:00
|
|
|
tryMoveCanvasX = _x + dx < cx && maxCanvasCanMove < 0;
|
|
|
|
if (tryMoveCanvasX) {
|
|
|
|
dx = max(dx, maxCanvasCanMove);
|
|
|
|
} else {
|
|
|
|
final maxCursorCanMove = r.left - _x;
|
|
|
|
dx = max(dx, maxCursorCanMove);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var tryMoveCanvasY = false;
|
|
|
|
if (dy > 0) {
|
2022-05-11 22:34:41 +08:00
|
|
|
final mayCanvasCanMove = _displayOriginY +
|
2022-06-13 21:07:26 +08:00
|
|
|
(parent.target?.imageModel.image!.height ?? 720) -
|
2022-05-11 22:34:41 +08:00
|
|
|
r.bottom.roundToDouble();
|
2020-11-24 22:03:04 +08:00
|
|
|
tryMoveCanvasY = _y + dy > cy && mayCanvasCanMove > 0;
|
|
|
|
if (tryMoveCanvasY) {
|
|
|
|
dy = min(dy, mayCanvasCanMove);
|
|
|
|
} else {
|
2020-11-24 23:36:46 +08:00
|
|
|
final mayCursorCanMove = r.bottom - _y;
|
2020-11-24 22:03:04 +08:00
|
|
|
dy = min(dy, mayCursorCanMove);
|
|
|
|
}
|
|
|
|
} else if (dy < 0) {
|
2022-05-11 22:34:41 +08:00
|
|
|
final mayCanvasCanMove = _displayOriginY - r.top.roundToDouble();
|
2020-11-24 22:03:04 +08:00
|
|
|
tryMoveCanvasY = _y + dy < cy && mayCanvasCanMove < 0;
|
|
|
|
if (tryMoveCanvasY) {
|
|
|
|
dy = max(dy, mayCanvasCanMove);
|
|
|
|
} else {
|
2020-11-24 23:36:46 +08:00
|
|
|
final mayCursorCanMove = r.top - _y;
|
2020-11-24 22:03:04 +08:00
|
|
|
dy = max(dy, mayCursorCanMove);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dx == 0 && dy == 0) return;
|
|
|
|
_x += dx;
|
|
|
|
_y += dy;
|
|
|
|
if (tryMoveCanvasX && dx != 0) {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.canvasModel.panX(-dx);
|
2020-11-24 22:03:04 +08:00
|
|
|
}
|
|
|
|
if (tryMoveCanvasY && dy != 0) {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.canvasModel.panY(-dy);
|
2020-11-24 22:03:04 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.moveMouse(_x, _y);
|
2020-11-24 22:03:04 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:32:46 +08:00
|
|
|
void updateCursorData(Map<String, dynamic> evt) {
|
|
|
|
var id = int.parse(evt['id']);
|
|
|
|
_hotx = double.parse(evt['hotx']);
|
|
|
|
_hoty = double.parse(evt['hoty']);
|
|
|
|
var width = int.parse(evt['width']);
|
|
|
|
var height = int.parse(evt['height']);
|
|
|
|
List<dynamic> colors = json.decode(evt['colors']);
|
|
|
|
final rgba = Uint8List.fromList(colors.map((s) => s as int).toList());
|
2022-06-13 21:07:26 +08:00
|
|
|
var pid = parent.target?.id;
|
2020-11-19 00:32:46 +08:00
|
|
|
ui.decodeImageFromPixels(rgba, width, height, ui.PixelFormat.rgba8888,
|
2022-05-31 14:44:06 +08:00
|
|
|
(image) {
|
2022-06-13 21:07:26 +08:00
|
|
|
if (parent.target?.id != pid) return;
|
2022-05-31 14:44:06 +08:00
|
|
|
_image = image;
|
|
|
|
_images[id] = Tuple3(image, _hotx, _hoty);
|
|
|
|
try {
|
|
|
|
// my throw exception, because the listener maybe already dispose
|
|
|
|
notifyListeners();
|
|
|
|
} catch (e) {
|
|
|
|
print('notify cursor: $e');
|
|
|
|
}
|
|
|
|
});
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void updateCursorId(Map<String, dynamic> evt) {
|
|
|
|
final tmp = _images[int.parse(evt['id'])];
|
|
|
|
if (tmp != null) {
|
2020-11-23 23:18:42 +08:00
|
|
|
_image = tmp.item1;
|
|
|
|
_hotx = tmp.item2;
|
|
|
|
_hoty = tmp.item3;
|
2020-11-19 00:32:46 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 16:50:02 +08:00
|
|
|
/// Update the cursor position.
|
2020-11-19 00:32:46 +08:00
|
|
|
void updateCursorPosition(Map<String, dynamic> evt) {
|
|
|
|
_x = double.parse(evt['x']);
|
|
|
|
_y = double.parse(evt['y']);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateDisplayOrigin(double x, double y) {
|
|
|
|
_displayOriginX = x;
|
|
|
|
_displayOriginY = y;
|
2020-12-21 21:52:20 +08:00
|
|
|
_x = x + 1;
|
|
|
|
_y = y + 1;
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.moveMouse(x, y);
|
|
|
|
parent.target?.canvasModel.resetOffset();
|
2020-11-19 00:32:46 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-05-31 14:44:06 +08:00
|
|
|
void updateDisplayOriginWithCursor(
|
|
|
|
double x, double y, double xCursor, double yCursor) {
|
2020-12-21 18:28:28 +08:00
|
|
|
_displayOriginX = x;
|
|
|
|
_displayOriginY = y;
|
|
|
|
_x = xCursor;
|
|
|
|
_y = yCursor;
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.moveMouse(x, y);
|
2020-12-21 18:28:28 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:32:46 +08:00
|
|
|
void clear() {
|
2020-11-22 18:29:04 +08:00
|
|
|
_x = -10000;
|
|
|
|
_x = -10000;
|
2020-11-19 00:32:46 +08:00
|
|
|
_image = null;
|
|
|
|
_images.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 17:24:02 +08:00
|
|
|
class QualityMonitorData {
|
|
|
|
String? speed;
|
|
|
|
String? fps;
|
|
|
|
String? delay;
|
|
|
|
String? targetBitrate;
|
|
|
|
String? codecFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
class QualityMonitorModel with ChangeNotifier {
|
|
|
|
WeakReference<FFI> parent;
|
|
|
|
|
|
|
|
QualityMonitorModel(this.parent);
|
|
|
|
var _show = false;
|
|
|
|
final _data = QualityMonitorData();
|
|
|
|
|
|
|
|
bool get show => _show;
|
|
|
|
QualityMonitorData get data => _data;
|
|
|
|
|
|
|
|
checkShowQualityMonitor() {
|
|
|
|
final show =
|
|
|
|
gFFI.getByName('toggle_option', 'show-quality-monitor') == 'true';
|
|
|
|
if (_show != show) {
|
|
|
|
_show = show;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateQualityStatus(Map<String, dynamic> evt) {
|
|
|
|
try {
|
|
|
|
if ((evt["speed"] as String).isNotEmpty) _data.speed = evt["speed"];
|
|
|
|
if ((evt["fps"] as String).isNotEmpty) _data.fps = evt["fps"];
|
|
|
|
if ((evt["delay"] as String).isNotEmpty) _data.delay = evt["delay"];
|
|
|
|
if ((evt["target_bitrate"] as String).isNotEmpty)
|
|
|
|
_data.targetBitrate = evt["target_bitrate"];
|
|
|
|
if ((evt["codec_format"] as String).isNotEmpty)
|
|
|
|
_data.codecFormat = evt["codec_format"];
|
|
|
|
notifyListeners();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Mouse button enum.
|
2022-03-07 22:54:34 +08:00
|
|
|
enum MouseButtons { left, right, wheel }
|
2022-02-17 18:00:44 +08:00
|
|
|
|
2022-03-07 22:54:34 +08:00
|
|
|
extension ToString on MouseButtons {
|
2022-02-17 18:00:44 +08:00
|
|
|
String get value {
|
|
|
|
switch (this) {
|
|
|
|
case MouseButtons.left:
|
|
|
|
return "left";
|
|
|
|
case MouseButtons.right:
|
|
|
|
return "right";
|
|
|
|
case MouseButtons.wheel:
|
|
|
|
return "wheel";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// FFI class for communicating with the Rust core.
|
2020-11-19 00:32:46 +08:00
|
|
|
class FFI {
|
2022-06-13 21:07:26 +08:00
|
|
|
var id = "";
|
|
|
|
var shift = false;
|
|
|
|
var ctrl = false;
|
|
|
|
var alt = false;
|
|
|
|
var command = false;
|
|
|
|
var version = "";
|
|
|
|
late final ImageModel imageModel;
|
|
|
|
late final FfiModel ffiModel;
|
|
|
|
late final CursorModel cursorModel;
|
|
|
|
late final CanvasModel canvasModel;
|
|
|
|
late final ServerModel serverModel;
|
|
|
|
late final ChatModel chatModel;
|
|
|
|
late final FileModel fileModel;
|
2022-07-25 16:23:45 +08:00
|
|
|
late final AbModel abModel;
|
2022-07-27 14:29:47 +08:00
|
|
|
late final UserModel userModel;
|
2022-08-04 17:24:02 +08:00
|
|
|
late final QualityMonitorModel qualityMonitorModel;
|
2022-06-13 21:07:26 +08:00
|
|
|
|
|
|
|
FFI() {
|
|
|
|
this.imageModel = ImageModel(WeakReference(this));
|
|
|
|
this.ffiModel = FfiModel(WeakReference(this));
|
|
|
|
this.cursorModel = CursorModel(WeakReference(this));
|
|
|
|
this.canvasModel = CanvasModel(WeakReference(this));
|
|
|
|
this.serverModel = ServerModel(WeakReference(this)); // use global FFI
|
|
|
|
this.chatModel = ChatModel(WeakReference(this));
|
|
|
|
this.fileModel = FileModel(WeakReference(this));
|
2022-07-25 16:23:45 +08:00
|
|
|
this.abModel = AbModel(WeakReference(this));
|
2022-07-27 14:29:47 +08:00
|
|
|
this.userModel = UserModel(WeakReference(this));
|
2022-08-04 17:24:02 +08:00
|
|
|
this.qualityMonitorModel = QualityMonitorModel(WeakReference(this));
|
2022-06-13 21:07:26 +08:00
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
|
2022-06-21 17:58:27 +08:00
|
|
|
static FFI newFFI() {
|
|
|
|
final ffi = FFI();
|
|
|
|
// keep platformFFI only once
|
|
|
|
ffi.ffiModel.platformFFI = gFFI.ffiModel.platformFFI;
|
|
|
|
return ffi;
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Get the remote id for current client.
|
2022-06-13 21:07:26 +08:00
|
|
|
String getId() {
|
2022-05-31 22:09:36 +08:00
|
|
|
return getByName('remote_id'); // TODO
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send a mouse tap event(down and up).
|
2022-06-13 21:07:26 +08:00
|
|
|
void tap(MouseButtons button) {
|
2022-02-17 18:00:44 +08:00
|
|
|
sendMouse('down', button);
|
|
|
|
sendMouse('up', button);
|
2020-11-25 17:02:27 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send scroll event with scroll distance [y].
|
2022-06-13 21:07:26 +08:00
|
|
|
void scroll(int y) {
|
2020-11-25 17:02:27 +08:00
|
|
|
setByName('send_mouse',
|
2022-05-31 22:09:36 +08:00
|
|
|
json.encode(modify({'id': id, 'type': 'wheel', 'y': y.toString()})));
|
2020-11-25 16:28:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Reconnect to the remote peer.
|
2022-05-31 22:09:36 +08:00
|
|
|
// static void reconnect() {
|
|
|
|
// setByName('reconnect');
|
2022-06-13 21:07:26 +08:00
|
|
|
// parent.target?.ffiModel.clearPermissions();
|
2022-05-31 22:09:36 +08:00
|
|
|
// }
|
2020-11-28 13:22:19 +08:00
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Reset key modifiers to false, including [shift], [ctrl], [alt] and [command].
|
2022-06-13 21:07:26 +08:00
|
|
|
void resetModifiers() {
|
2020-11-25 16:28:46 +08:00
|
|
|
shift = ctrl = alt = command = false;
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Modify the given modifier map [evt] based on current modifier key status.
|
2022-06-13 21:07:26 +08:00
|
|
|
Map<String, String> modify(Map<String, String> evt) {
|
2020-11-25 16:28:46 +08:00
|
|
|
if (ctrl) evt['ctrl'] = 'true';
|
|
|
|
if (shift) evt['shift'] = 'true';
|
|
|
|
if (alt) evt['alt'] = 'true';
|
|
|
|
if (command) evt['command'] = 'true';
|
|
|
|
return evt;
|
2020-11-24 12:11:55 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send mouse press event.
|
2022-06-13 21:07:26 +08:00
|
|
|
void sendMouse(String type, MouseButtons button) {
|
2020-11-25 16:28:46 +08:00
|
|
|
if (!ffiModel.keyboard()) return;
|
2022-03-07 22:54:34 +08:00
|
|
|
setByName('send_mouse',
|
2022-05-31 22:09:36 +08:00
|
|
|
json.encode(modify({'id': id, 'type': type, 'buttons': button.value})));
|
2020-11-25 16:28:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send key stroke event.
|
|
|
|
/// [down] indicates the key's state(down or up).
|
|
|
|
/// [press] indicates a click event(down and up).
|
2022-06-13 21:07:26 +08:00
|
|
|
void inputKey(String name, {bool? down, bool? press}) {
|
2020-11-25 16:28:46 +08:00
|
|
|
if (!ffiModel.keyboard()) return;
|
2022-05-31 22:09:36 +08:00
|
|
|
// final Map<String, String> out = Map();
|
|
|
|
// out['name'] = name;
|
|
|
|
// // default: down = false
|
|
|
|
// if (down == true) {
|
|
|
|
// out['down'] = "true";
|
|
|
|
// }
|
|
|
|
// // default: press = true
|
|
|
|
// if (press != false) {
|
|
|
|
// out['press'] = "true";
|
|
|
|
// }
|
|
|
|
// setByName('input_key', json.encode(modify(out)));
|
|
|
|
// TODO id
|
2022-06-13 21:07:26 +08:00
|
|
|
bind.sessionInputKey(
|
2022-05-31 22:09:36 +08:00
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
down: down ?? false,
|
|
|
|
press: press ?? true,
|
|
|
|
alt: alt,
|
|
|
|
ctrl: ctrl,
|
|
|
|
shift: shift,
|
|
|
|
command: command);
|
2020-11-24 12:11:55 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send mouse movement event with distance in [x] and [y].
|
2022-06-13 21:07:26 +08:00
|
|
|
void moveMouse(double x, double y) {
|
2020-11-25 16:28:46 +08:00
|
|
|
if (!ffiModel.keyboard()) return;
|
2020-11-24 23:36:46 +08:00
|
|
|
var x2 = x.toInt();
|
|
|
|
var y2 = y.toInt();
|
2022-05-31 22:09:36 +08:00
|
|
|
setByName(
|
|
|
|
'send_mouse', json.encode(modify({'id': id, 'x': '$x2', 'y': '$y2'})));
|
2020-11-24 23:36:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// List the saved peers.
|
2022-06-13 21:07:26 +08:00
|
|
|
List<Peer> peers() {
|
2020-11-19 00:32:46 +08:00
|
|
|
try {
|
2022-05-31 22:09:36 +08:00
|
|
|
var str = getByName('peers'); // TODO
|
2022-04-14 11:00:24 +08:00
|
|
|
if (str == "") return [];
|
|
|
|
List<dynamic> peers = json.decode(str);
|
2020-11-19 00:32:46 +08:00
|
|
|
return peers
|
|
|
|
.map((s) => s as List<dynamic>)
|
|
|
|
.map((s) =>
|
2022-05-31 14:44:06 +08:00
|
|
|
Peer.fromJson(s[0] as String, s[1] as Map<String, dynamic>))
|
2020-11-19 00:32:46 +08:00
|
|
|
.toList();
|
|
|
|
} catch (e) {
|
2020-11-26 20:28:37 +08:00
|
|
|
print('peers(): $e');
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Connect with the given [id]. Only transfer file if [isFileTransfer].
|
2022-08-03 15:31:19 +08:00
|
|
|
void connect(String id,
|
|
|
|
{bool isFileTransfer = false, double tabBarHeight = 0.0}) {
|
2022-06-27 09:48:35 +08:00
|
|
|
if (!isFileTransfer) {
|
2022-06-13 21:07:26 +08:00
|
|
|
chatModel.resetClientMode();
|
|
|
|
canvasModel.id = id;
|
|
|
|
imageModel._id = id;
|
|
|
|
cursorModel.id = id;
|
2022-06-21 17:58:27 +08:00
|
|
|
}
|
2022-06-28 22:15:00 +08:00
|
|
|
id = isFileTransfer ? 'ft_${id}' : id;
|
2022-06-21 17:58:27 +08:00
|
|
|
final stream = bind.sessionConnect(id: id, isFileTransfer: isFileTransfer);
|
|
|
|
final cb = ffiModel.startEventListener(id);
|
|
|
|
() async {
|
|
|
|
await for (final message in stream) {
|
|
|
|
if (message is Event) {
|
|
|
|
try {
|
|
|
|
debugPrint("event:${message.field0}");
|
|
|
|
Map<String, dynamic> event = json.decode(message.field0);
|
|
|
|
cb(event);
|
|
|
|
} catch (e) {
|
|
|
|
print('json.decode fail(): $e');
|
2022-05-31 17:36:36 +08:00
|
|
|
}
|
2022-06-21 17:58:27 +08:00
|
|
|
} else if (message is Rgba) {
|
2022-08-03 15:31:19 +08:00
|
|
|
imageModel.onRgba(message.field0, tabBarHeight);
|
2022-05-31 17:36:36 +08:00
|
|
|
}
|
2022-06-21 17:58:27 +08:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
// every instance will bind a stream
|
2022-06-17 22:21:49 +08:00
|
|
|
this.id = id;
|
2022-06-21 17:58:27 +08:00
|
|
|
if (isFileTransfer) {
|
|
|
|
this.fileModel.initFileFetcher();
|
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Login with [password], choose if the client should [remember] it.
|
2022-06-13 21:07:26 +08:00
|
|
|
void login(String id, String password, bool remember) {
|
|
|
|
bind.sessionLogin(id: id, password: password, remember: remember);
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Close the remote session.
|
2022-06-28 22:04:10 +08:00
|
|
|
Future<void> close() async {
|
2022-03-25 16:34:27 +08:00
|
|
|
chatModel.close();
|
2022-06-13 21:07:26 +08:00
|
|
|
if (imageModel.image != null && !isWebDesktop) {
|
2022-06-28 22:04:10 +08:00
|
|
|
await savePreference(id, cursorModel.x, cursorModel.y, canvasModel.x,
|
2022-03-07 22:54:34 +08:00
|
|
|
canvasModel.y, canvasModel.scale, ffiModel.pi.currentDisplay);
|
2022-02-03 17:19:25 +08:00
|
|
|
}
|
2022-06-13 21:07:26 +08:00
|
|
|
bind.sessionClose(id: id);
|
2020-11-28 17:42:29 +08:00
|
|
|
id = "";
|
2022-08-03 15:31:19 +08:00
|
|
|
imageModel.update(null, 0.0);
|
2020-11-25 16:28:46 +08:00
|
|
|
cursorModel.clear();
|
|
|
|
ffiModel.clear();
|
|
|
|
canvasModel.clear();
|
|
|
|
resetModifiers();
|
2022-06-17 22:21:49 +08:00
|
|
|
print("model closed");
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send **get** command to the Rust core based on [name] and [arg].
|
|
|
|
/// Return the result as a string.
|
2022-06-13 21:07:26 +08:00
|
|
|
String getByName(String name, [String arg = '']) {
|
|
|
|
return ffiModel.platformFFI.getByName(name, arg);
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send **set** command to the Rust core based on [name] and [value].
|
2022-06-13 21:07:26 +08:00
|
|
|
void setByName(String name, [String value = '']) {
|
|
|
|
ffiModel.platformFFI.setByName(name, value);
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-14 12:32:01 +08:00
|
|
|
String getOption(String name) {
|
|
|
|
return ffiModel.platformFFI.getByName("option", name);
|
|
|
|
}
|
|
|
|
|
2022-07-25 16:23:45 +08:00
|
|
|
Future<String> getLocalOption(String name) {
|
|
|
|
return bind.mainGetLocalOption(key: name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setLocalOption(String key, String value) {
|
|
|
|
return bind.mainSetLocalOption(key: key, value: value);
|
|
|
|
}
|
|
|
|
|
2022-07-29 12:03:24 +08:00
|
|
|
Future<String> getPeerOption(String id, String key) {
|
|
|
|
return bind.mainGetPeerOption(id: id, key: key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setPeerOption(String id, String key, String value) {
|
|
|
|
return bind.mainSetPeerOption(id: id, key: key, value: value);
|
|
|
|
}
|
|
|
|
|
2022-07-14 12:32:01 +08:00
|
|
|
void setOption(String name, String value) {
|
|
|
|
Map<String, String> res = Map()
|
2022-07-25 16:23:45 +08:00
|
|
|
..["name"] = name
|
2022-07-14 12:32:01 +08:00
|
|
|
..["value"] = value;
|
|
|
|
return ffiModel.platformFFI.setByName('option', jsonEncode(res));
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
RustdeskImpl get bind => ffiModel.platformFFI.ffiBind;
|
2022-05-31 14:44:06 +08:00
|
|
|
|
2022-08-03 15:31:19 +08:00
|
|
|
handleMouse(Map<String, dynamic> evt, {double tabBarHeight = 0.0}) {
|
2022-02-03 17:19:25 +08:00
|
|
|
var type = '';
|
2022-02-06 16:29:56 +08:00
|
|
|
var isMove = false;
|
2022-02-03 17:19:25 +08:00
|
|
|
switch (evt['type']) {
|
|
|
|
case 'mousedown':
|
|
|
|
type = 'down';
|
|
|
|
break;
|
|
|
|
case 'mouseup':
|
|
|
|
type = 'up';
|
|
|
|
break;
|
|
|
|
case 'mousemove':
|
2022-02-06 16:29:56 +08:00
|
|
|
isMove = true;
|
2022-02-03 17:19:25 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
evt['type'] = type;
|
|
|
|
var x = evt['x'];
|
2022-08-03 15:31:19 +08:00
|
|
|
var y = max(0.0, (evt['y'] as double) - tabBarHeight);
|
2022-02-06 16:29:56 +08:00
|
|
|
if (isMove) {
|
2022-06-13 21:07:26 +08:00
|
|
|
canvasModel.moveDesktopMouse(x, y);
|
2022-02-06 16:29:56 +08:00
|
|
|
}
|
2022-06-13 21:07:26 +08:00
|
|
|
final d = ffiModel.display;
|
|
|
|
x -= canvasModel.x;
|
|
|
|
y -= canvasModel.y;
|
2022-02-06 16:29:56 +08:00
|
|
|
if (!isMove && (x < 0 || x > d.width || y < 0 || y > d.height)) {
|
2022-02-03 17:19:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-06-13 21:07:26 +08:00
|
|
|
x /= canvasModel.scale;
|
|
|
|
y /= canvasModel.scale;
|
2022-02-03 17:19:25 +08:00
|
|
|
x += d.x;
|
|
|
|
y += d.y;
|
|
|
|
if (type != '') {
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
}
|
2022-04-25 18:25:25 +08:00
|
|
|
evt['x'] = '${x.round()}';
|
|
|
|
evt['y'] = '${y.round()}';
|
2022-02-03 17:19:25 +08:00
|
|
|
var buttons = '';
|
|
|
|
switch (evt['buttons']) {
|
|
|
|
case 1:
|
|
|
|
buttons = 'left';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
buttons = 'right';
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
buttons = 'wheel';
|
|
|
|
break;
|
2021-08-05 01:38:20 +08:00
|
|
|
}
|
2022-02-03 17:19:25 +08:00
|
|
|
evt['buttons'] = buttons;
|
2022-05-31 22:09:36 +08:00
|
|
|
evt['id'] = id;
|
2022-02-03 17:19:25 +08:00
|
|
|
setByName('send_mouse', json.encode(evt));
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
listenToMouse(bool yesOrNo) {
|
2022-02-03 00:53:59 +08:00
|
|
|
if (yesOrNo) {
|
2022-06-13 21:07:26 +08:00
|
|
|
ffiModel.platformFFI.startDesktopWebListener();
|
2022-02-03 00:53:59 +08:00
|
|
|
} else {
|
2022-06-13 21:07:26 +08:00
|
|
|
ffiModel.platformFFI.stopDesktopWebListener();
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 02:07:53 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
void setMethodCallHandler(FMethod callback) {
|
|
|
|
ffiModel.platformFFI.setMethodCallHandler(callback);
|
2022-02-10 02:07:53 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
Future<bool> invokeMethod(String method, [dynamic arguments]) async {
|
|
|
|
return await ffiModel.platformFFI.invokeMethod(method, arguments);
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
2022-07-14 12:32:01 +08:00
|
|
|
|
|
|
|
Future<List<String>> getAudioInputs() async {
|
|
|
|
return await bind.mainGetSoundInputs();
|
|
|
|
}
|
2022-07-27 22:56:28 +08:00
|
|
|
|
2022-07-14 12:32:01 +08:00
|
|
|
String getDefaultAudioInput() {
|
|
|
|
final input = getOption('audio-input');
|
|
|
|
if (input.isEmpty && Platform.isWindows) {
|
|
|
|
return "System Sound";
|
|
|
|
}
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2022-07-25 16:23:45 +08:00
|
|
|
void setDefaultAudioInput(String input) {
|
2022-07-14 12:32:01 +08:00
|
|
|
setOption('audio-input', input);
|
|
|
|
}
|
2022-07-25 16:23:45 +08:00
|
|
|
|
|
|
|
Future<Map<String, String>> getHttpHeaders() async {
|
|
|
|
return {"Authorization": "Bearer " + await getLocalOption("access_token")};
|
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class Display {
|
|
|
|
double x = 0;
|
|
|
|
double y = 0;
|
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class PeerInfo {
|
2022-02-17 15:22:14 +08:00
|
|
|
String version = "";
|
|
|
|
String username = "";
|
|
|
|
String hostname = "";
|
|
|
|
String platform = "";
|
|
|
|
bool sasEnabled = false;
|
|
|
|
int currentDisplay = 0;
|
|
|
|
List<Display> displays = [];
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
2020-12-21 18:28:28 +08:00
|
|
|
|
2022-06-28 22:04:10 +08:00
|
|
|
Future<void> savePreference(String id, double xCursor, double yCursor,
|
|
|
|
double xCanvas, double yCanvas, double scale, int currentDisplay) async {
|
2020-12-21 18:28:28 +08:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
final p = Map<String, dynamic>();
|
|
|
|
p['xCursor'] = xCursor;
|
|
|
|
p['yCursor'] = yCursor;
|
|
|
|
p['xCanvas'] = xCanvas;
|
|
|
|
p['yCanvas'] = yCanvas;
|
|
|
|
p['scale'] = scale;
|
|
|
|
p['currentDisplay'] = currentDisplay;
|
|
|
|
prefs.setString('peer' + id, json.encode(p));
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:22:14 +08:00
|
|
|
Future<Map<String, dynamic>?> getPreference(String id) async {
|
2022-05-23 16:02:37 +08:00
|
|
|
if (!isWebDesktop) return null;
|
2020-12-21 18:28:28 +08:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
var p = prefs.getString('peer' + id);
|
|
|
|
if (p == null) return null;
|
|
|
|
Map<String, dynamic> m = json.decode(p);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:18:06 +08:00
|
|
|
void removePreference(String id) async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
prefs.remove('peer' + id);
|
2020-12-21 19:05:31 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
void initializeCursorAndCanvas(FFI ffi) async {
|
|
|
|
var p = await getPreference(ffi.id);
|
2020-12-21 18:28:28 +08:00
|
|
|
int currentDisplay = 0;
|
|
|
|
if (p != null) {
|
|
|
|
currentDisplay = p['currentDisplay'];
|
|
|
|
}
|
2022-06-13 21:07:26 +08:00
|
|
|
if (p == null || currentDisplay != ffi.ffiModel.pi.currentDisplay) {
|
|
|
|
ffi.cursorModel
|
|
|
|
.updateDisplayOrigin(ffi.ffiModel.display.x, ffi.ffiModel.display.y);
|
2020-12-21 18:28:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
double xCursor = p['xCursor'];
|
|
|
|
double yCursor = p['yCursor'];
|
|
|
|
double xCanvas = p['xCanvas'];
|
|
|
|
double yCanvas = p['yCanvas'];
|
|
|
|
double scale = p['scale'];
|
2022-06-13 21:07:26 +08:00
|
|
|
ffi.cursorModel.updateDisplayOriginWithCursor(
|
|
|
|
ffi.ffiModel.display.x, ffi.ffiModel.display.y, xCursor, yCursor);
|
|
|
|
ffi.canvasModel.update(xCanvas, yCanvas, scale);
|
2020-12-21 18:28:28 +08:00
|
|
|
}
|
2021-08-02 20:54:56 +08:00
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Translate text based on the pre-defined dictionary.
|
2022-06-13 21:07:26 +08:00
|
|
|
/// note: params [FFI?] can be used to replace global FFI implementation
|
|
|
|
/// for example: during global initialization, gFFI not exists yet.
|
|
|
|
String translate(String name, {FFI? ffi}) {
|
2022-01-27 01:28:32 +08:00
|
|
|
if (name.startsWith('Failed to') && name.contains(': ')) {
|
2021-08-02 22:21:23 +08:00
|
|
|
return name.split(': ').map((x) => translate(x)).join(': ');
|
|
|
|
}
|
2022-01-26 19:00:23 +08:00
|
|
|
var a = 'translate';
|
|
|
|
var b = '{"locale": "$localeName", "text": "$name"}';
|
2022-06-13 21:07:26 +08:00
|
|
|
return (ffi ?? gFFI).getByName(a, b);
|
2021-08-02 20:54:56 +08:00
|
|
|
}
|