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-08-31 18:41:55 +08:00
|
|
|
import 'package:flutter_hbb/consts.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';
|
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-09-07 22:06:05 +08:00
|
|
|
import 'package:flutter_custom_cursor/flutter_custom_cursor.dart';
|
2022-05-29 17:19:50 +08:00
|
|
|
|
2022-02-28 18:29:25 +08:00
|
|
|
import '../common.dart';
|
2022-08-29 18:48:12 +08:00
|
|
|
import '../common/shared_state.dart';
|
2022-09-11 10:50:48 +08:00
|
|
|
import '../utils/image.dart' as img;
|
2022-05-24 23:33:00 +08:00
|
|
|
import '../mobile/widgets/dialog.dart';
|
2022-07-27 22:56:28 +08:00
|
|
|
import 'peer_model.dart';
|
2022-08-03 22:03:31 +08:00
|
|
|
import 'platform_model.dart';
|
2020-11-19 00:32:46 +08:00
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
typedef HandleMsgBox = 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
|
|
|
|
2022-02-02 00:46:21 +08:00
|
|
|
var _inputBlocked = false;
|
2022-09-03 10:39:33 +08:00
|
|
|
final _permissions = <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-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;
|
|
|
|
|
2022-09-07 12:20:53 +08:00
|
|
|
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) {
|
2020-11-22 21:08:19 +08:00
|
|
|
clear();
|
2022-04-17 00:44:05 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
toggleTouchMode() {
|
2022-04-28 22:44:54 +08:00
|
|
|
if (!isPeerAndroid) {
|
|
|
|
_touchMode = !_touchMode;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
updatePermission(Map<String, dynamic> evt, String id) {
|
2020-11-22 21:08:19 +08:00
|
|
|
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';
|
|
|
|
});
|
2022-09-03 10:39:33 +08:00
|
|
|
KeyboardEnabledState.find(id).value = _permissions['keyboard'] != false;
|
|
|
|
debugPrint('$_permissions');
|
2022-02-06 16:29:56 +08:00
|
|
|
notifyListeners();
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
updateUser() {
|
2022-03-28 15:44:45 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2020-11-24 23:36:46 +08:00
|
|
|
bool keyboard() => _permissions['keyboard'] != false;
|
2020-11-23 23:18:42 +08:00
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
setConnectionType(String peerId, bool secure, bool direct) {
|
2020-11-29 00:13:55 +08:00
|
|
|
_secure = secure;
|
|
|
|
_direct = direct;
|
2022-08-29 18:48:12 +08:00
|
|
|
try {
|
|
|
|
var connectionType = ConnectionTypeState.find(peerId);
|
|
|
|
connectionType.setSecure(secure);
|
|
|
|
connectionType.setDirect(direct);
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
2020-11-29 00:13:55 +08:00
|
|
|
}
|
|
|
|
|
2022-02-17 15:22:14 +08:00
|
|
|
Image? getConnectionImage() {
|
2022-08-29 18:48:12 +08:00
|
|
|
if (secure == null || direct == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
final icon =
|
2022-09-07 12:20:53 +08:00
|
|
|
'${secure == true ? 'secure' : 'insecure'}${direct == true ? '' : '_relay'}';
|
2022-08-29 18:48:12 +08:00
|
|
|
return Image.asset('assets/$icon.png', width: 48, height: 48);
|
2020-11-29 00:13:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
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-09-11 10:50:48 +08:00
|
|
|
StreamEventHandler startEventListener(String peerId) {
|
|
|
|
return (evt) async {
|
2022-05-31 17:36:36 +08:00
|
|
|
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-08-29 18:48:12 +08:00
|
|
|
setConnectionType(
|
|
|
|
peerId, 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-09-11 10:50:48 +08:00
|
|
|
await parent.target?.cursorModel.updateCursorData(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'cursor_id') {
|
2022-09-11 10:50:48 +08:00
|
|
|
await parent.target?.cursorModel.updateCursorId(evt);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'cursor_position') {
|
2022-09-11 10:50:48 +08:00
|
|
|
await parent.target?.cursorModel.updateCursorPosition(evt, peerId);
|
2022-05-31 17:36:36 +08:00
|
|
|
} else if (name == 'clipboard') {
|
|
|
|
Clipboard.setData(ClipboardData(text: evt['content']));
|
|
|
|
} else if (name == 'permission') {
|
2022-09-03 10:39:33 +08:00
|
|
|
parent.target?.ffiModel.updatePermission(evt, peerId);
|
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
|
2022-09-07 12:20:53 +08:00
|
|
|
.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-09-07 12:20:53 +08:00
|
|
|
.receive(int.parse(evt['id'] as String), evt['text'] ?? '');
|
2022-05-31 17:36:36 +08:00
|
|
|
} 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-09-05 19:41:09 +08:00
|
|
|
} else if (name == 'add_connection') {
|
|
|
|
parent.target?.serverModel.addConnection(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-08-05 20:29:43 +08:00
|
|
|
} else if (name == 'update_quality_status') {
|
|
|
|
parent.target?.qualityMonitorModel.updateQualityStatus(evt);
|
2022-08-08 22:00:01 +08:00
|
|
|
} else if (name == 'update_block_input_state') {
|
2022-08-26 23:28:08 +08:00
|
|
|
updateBlockInputState(evt, peerId);
|
2022-08-08 22:00:01 +08:00
|
|
|
} else if (name == 'update_privacy_mode') {
|
2022-08-26 23:28:08 +08:00
|
|
|
updatePrivacyMode(evt, peerId);
|
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-09-12 16:35:56 +08:00
|
|
|
updateEventListener(String peerId) {
|
2022-09-07 12:20:53 +08:00
|
|
|
platformFFI.setEventCallback(startEventListener(peerId));
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
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']);
|
2022-09-07 12:20:53 +08:00
|
|
|
if (old != _pi.currentDisplay) {
|
2022-06-13 21:07:26 +08:00
|
|
|
parent.target?.cursorModel.updateDisplayOrigin(_display.x, _display.y);
|
2022-09-07 12:20:53 +08:00
|
|
|
}
|
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-09-12 16:35:56 +08:00
|
|
|
handleMsgBox(Map<String, dynamic> evt, String id) {
|
2022-08-12 18:42:02 +08:00
|
|
|
if (parent.target == null) return;
|
|
|
|
final dialogManager = parent.target!.dialogManager;
|
2022-03-28 17:24:52 +08:00
|
|
|
var type = evt['type'];
|
|
|
|
var title = evt['title'];
|
|
|
|
var text = evt['text'];
|
|
|
|
if (type == 're-input-password') {
|
2022-08-12 18:42:02 +08:00
|
|
|
wrongPasswordDialog(id, dialogManager);
|
2022-03-28 17:24:52 +08:00
|
|
|
} else if (type == 'input-password') {
|
2022-08-12 18:42:02 +08:00
|
|
|
enterPasswordDialog(id, dialogManager);
|
2022-08-04 17:24:02 +08:00
|
|
|
} else if (type == 'restarting') {
|
2022-08-12 18:42:02 +08:00
|
|
|
showMsgBox(id, type, title, text, false, dialogManager, hasCancel: false);
|
2022-03-28 17:24:52 +08:00
|
|
|
} else {
|
|
|
|
var hasRetry = evt['hasRetry'] == 'true';
|
2022-08-12 18:42:02 +08:00
|
|
|
showMsgBox(id, type, title, text, hasRetry, dialogManager);
|
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-09-12 16:35:56 +08:00
|
|
|
showMsgBox(String id, String type, String title, String text, bool hasRetry,
|
|
|
|
OverlayDialogManager dialogManager,
|
2022-08-04 17:24:02 +08:00
|
|
|
{bool? hasCancel}) {
|
2022-08-12 18:42:02 +08:00
|
|
|
msgBox(type, title, text, dialogManager, 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-08-03 22:03:31 +08:00
|
|
|
bind.sessionReconnect(id: id);
|
2022-05-31 22:09:36 +08:00
|
|
|
clearPermissions();
|
2022-08-12 18:42:02 +08:00
|
|
|
dialogManager.showLoading(translate('Connecting...'),
|
2022-08-16 21:27:21 +08:00
|
|
|
onCancel: closeConnection);
|
2022-03-28 17:24:52 +08:00
|
|
|
});
|
|
|
|
_reconnects *= 2;
|
|
|
|
} else {
|
|
|
|
_reconnects = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Handle the peer info event based on [evt].
|
2022-09-12 16:35:56 +08:00
|
|
|
handlePeerInfo(Map<String, dynamic> evt, String peerId) async {
|
2022-09-01 21:18:29 +08:00
|
|
|
// recent peer updated by handle_peer_info(ui_session_interface.rs) --> handle_peer_info(client.rs) --> save_config(client.rs)
|
|
|
|
bind.mainLoadRecentPeers();
|
|
|
|
|
2022-08-12 18:42:02 +08:00
|
|
|
parent.target?.dialogManager.dismissAll();
|
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'];
|
2022-09-07 12:20:53 +08:00
|
|
|
_pi.sasEnabled = evt['sas_enabled'] == 'true';
|
2020-11-19 00:32:46 +08:00
|
|
|
_pi.currentDisplay = int.parse(evt['current_display']);
|
2022-03-16 15:33:00 +08:00
|
|
|
|
2022-08-26 23:28:08 +08:00
|
|
|
try {
|
|
|
|
CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2022-04-28 22:44:54 +08:00
|
|
|
if (isPeerAndroid) {
|
|
|
|
_touchMode = true;
|
2022-09-08 22:18:02 +08:00
|
|
|
if (parent.target != null &&
|
|
|
|
parent.target!.ffiModel.permissions['keyboard'] != false) {
|
|
|
|
Timer(const Duration(milliseconds: 100),
|
|
|
|
parent.target!.dialogManager.showMobileActionsOverlay);
|
2022-04-28 22:44:54 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-03 22:03:31 +08:00
|
|
|
_touchMode =
|
2022-09-07 12:20:53 +08:00
|
|
|
await bind.sessionGetOption(id: peerId, arg: 'touch-mode') != '';
|
2022-04-28 22:44:54 +08:00
|
|
|
}
|
|
|
|
|
2022-09-06 19:08:45 +08:00
|
|
|
if (parent.target != null &&
|
|
|
|
parent.target!.connType == ConnType.fileTransfer) {
|
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) {
|
2022-08-12 18:42:02 +08:00
|
|
|
parent.target?.dialogManager.showLoading(
|
|
|
|
translate('Connected, waiting for image...'),
|
2022-08-16 21:27:21 +08:00
|
|
|
onCancel: closeConnection);
|
2022-03-16 15:33:00 +08:00
|
|
|
_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
|
|
|
}
|
2022-08-08 22:00:01 +08:00
|
|
|
|
2022-08-26 23:28:08 +08:00
|
|
|
updateBlockInputState(Map<String, dynamic> evt, String peerId) {
|
2022-08-08 22:00:01 +08:00
|
|
|
_inputBlocked = evt['input_state'] == 'on';
|
|
|
|
notifyListeners();
|
2022-08-26 23:28:08 +08:00
|
|
|
try {
|
|
|
|
BlockInputState.find(peerId).value = evt['input_state'] == 'on';
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
2022-08-08 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
2022-08-26 23:28:08 +08:00
|
|
|
updatePrivacyMode(Map<String, dynamic> evt, String peerId) {
|
2022-08-08 22:00:01 +08:00
|
|
|
notifyListeners();
|
2022-08-26 23:28:08 +08:00
|
|
|
try {
|
|
|
|
PrivacyModeState.find(peerId).value =
|
|
|
|
bind.sessionGetToggleOptionSync(id: peerId, arg: 'privacy-mode');
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
2022-08-08 22:00:01 +08:00
|
|
|
}
|
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-09-07 12:20:53 +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-09-12 16:35:56 +08:00
|
|
|
onRgba(Uint8List rgba, double tabBarHeight) {
|
2022-05-31 22:09:36 +08:00
|
|
|
if (_waitForImage) {
|
|
|
|
_waitForImage = false;
|
2022-08-12 18:42:02 +08:00
|
|
|
parent.target?.dialogManager.dismissAll();
|
2022-05-31 22:09:36 +08:00
|
|
|
}
|
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) {
|
2022-09-07 22:06:05 +08:00
|
|
|
debugPrint('update image: $e');
|
2022-05-19 23:45:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
update(ui.Image? image, double tabBarHeight) async {
|
2020-11-24 22:03:04 +08:00
|
|
|
if (_image == null && image != null) {
|
2022-08-13 15:08:17 +08:00
|
|
|
if (isWebDesktop || isDesktop) {
|
2022-09-12 10:52:38 +08:00
|
|
|
await parent.target?.canvasModel.updateViewStyle();
|
|
|
|
await parent.target?.canvasModel.updateScrollStyle();
|
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) {
|
2022-09-12 10:52:38 +08:00
|
|
|
await initializeCursorAndCanvas(parent.target!);
|
|
|
|
}
|
|
|
|
if (parent.target?.ffiModel.isPeerAndroid ?? false) {
|
|
|
|
bind.sessionPeerOption(id: _id, name: 'view-style', value: 'adaptive');
|
|
|
|
parent.target?.canvasModel.updateViewStyle();
|
2022-02-03 00:53:59 +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
|
|
|
}
|
|
|
|
|
2022-08-11 00:12:47 +08:00
|
|
|
enum ScrollStyle {
|
|
|
|
scrollbar,
|
2022-08-12 20:14:53 +08:00
|
|
|
scrollauto,
|
2022-08-11 00:12:47 +08:00
|
|
|
}
|
|
|
|
|
2020-11-23 23:18:42 +08:00
|
|
|
class CanvasModel with ChangeNotifier {
|
2022-09-03 10:39:33 +08:00
|
|
|
// image offset of canvas
|
|
|
|
double _x = 0;
|
|
|
|
// image offset of canvas
|
|
|
|
double _y = 0;
|
|
|
|
// image scale
|
|
|
|
double _scale = 1.0;
|
|
|
|
// the tabbar over the image
|
|
|
|
double tabBarHeight = 0.0;
|
|
|
|
// TODO multi canvas model
|
2022-09-07 12:20:53 +08:00
|
|
|
String id = '';
|
2022-08-12 20:14:53 +08:00
|
|
|
// scroll offset x percent
|
|
|
|
double _scrollX = 0.0;
|
|
|
|
// scroll offset y percent
|
|
|
|
double _scrollY = 0.0;
|
2022-08-13 15:08:17 +08:00
|
|
|
ScrollStyle _scrollStyle = ScrollStyle.scrollauto;
|
2022-09-12 16:35:56 +08:00
|
|
|
String? _viewStyle;
|
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;
|
|
|
|
double get y => _y;
|
|
|
|
double get scale => _scale;
|
2022-08-11 00:12:47 +08:00
|
|
|
ScrollStyle get scrollStyle => _scrollStyle;
|
2020-11-23 23:18:42 +08:00
|
|
|
|
2022-08-12 20:14:53 +08:00
|
|
|
setScrollPercent(double x, double y) {
|
|
|
|
_scrollX = x;
|
|
|
|
_scrollY = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
double get scrollX => _scrollX;
|
|
|
|
double get scrollY => _scrollY;
|
|
|
|
|
2022-09-12 10:52:38 +08:00
|
|
|
updateViewStyle() async {
|
2022-08-16 15:22:57 +08:00
|
|
|
final style = await bind.sessionGetOption(id: id, arg: 'view-style');
|
2022-09-12 16:35:56 +08:00
|
|
|
if (style == null || _viewStyle == style) {
|
2022-05-31 22:09:36 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-08-13 15:08:17 +08:00
|
|
|
|
2022-08-31 16:41:05 +08:00
|
|
|
_scale = 1.0;
|
|
|
|
if (style == 'adaptive') {
|
2022-08-31 18:41:55 +08:00
|
|
|
final s1 = size.width / getDisplayWidth();
|
|
|
|
final s2 = size.height / getDisplayHeight();
|
2022-08-31 16:41:05 +08:00
|
|
|
_scale = s1 < s2 ? s1 : s2;
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
2022-09-12 16:35:56 +08:00
|
|
|
_viewStyle = style;
|
2022-08-12 20:14:53 +08:00
|
|
|
_x = (size.width - getDisplayWidth() * _scale) / 2;
|
|
|
|
_y = (size.height - getDisplayHeight() * _scale) / 2;
|
2022-02-03 00:53:59 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-08-12 20:14:53 +08:00
|
|
|
updateScrollStyle() async {
|
2022-08-16 15:22:57 +08:00
|
|
|
final style = await bind.sessionGetOption(id: id, arg: 'scroll-style');
|
2022-08-13 15:08:17 +08:00
|
|
|
if (style == 'scrollbar') {
|
2022-08-11 00:12:47 +08:00
|
|
|
_scrollStyle = ScrollStyle.scrollbar;
|
2022-08-12 20:14:53 +08:00
|
|
|
_scrollX = 0.0;
|
|
|
|
_scrollY = 0.0;
|
2022-08-13 15:08:17 +08:00
|
|
|
} else {
|
|
|
|
_scrollStyle = ScrollStyle.scrollauto;
|
2022-08-11 00:12:47 +08:00
|
|
|
}
|
2022-08-13 15:08:17 +08:00
|
|
|
notifyListeners();
|
2022-08-11 00:12:47 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 10:52:38 +08:00
|
|
|
update(double x, double y, double scale) {
|
2020-12-21 18:28:28 +08:00
|
|
|
_x = x;
|
|
|
|
_y = y;
|
|
|
|
_scale = scale;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
int getDisplayWidth() {
|
2022-08-31 18:41:55 +08:00
|
|
|
final defaultWidth = (isDesktop || isWebDesktop)
|
|
|
|
? kDesktopDefaultDisplayWidth
|
|
|
|
: kMobileDefaultDisplayWidth;
|
|
|
|
return parent.target?.ffiModel.display.width ?? defaultWidth;
|
2022-06-13 21:07:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int getDisplayHeight() {
|
2022-08-31 18:41:55 +08:00
|
|
|
final defaultHeight = (isDesktop || isWebDesktop)
|
|
|
|
? kDesktopDefaultDisplayHeight
|
|
|
|
: kMobileDefaultDisplayHeight;
|
|
|
|
return parent.target?.ffiModel.display.height ?? defaultHeight;
|
2022-06-13 21:07:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-12 20:14:53 +08:00
|
|
|
Size get size {
|
|
|
|
final size = MediaQueryData.fromWindow(ui.window).size;
|
2022-09-03 10:39:33 +08:00
|
|
|
return Size(size.width, size.height - tabBarHeight);
|
2022-08-12 20:14:53 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
moveDesktopMouse(double x, double y) {
|
2022-06-27 16:50:02 +08:00
|
|
|
// On mobile platforms, move the canvas with the cursor.
|
2022-08-12 20:14:53 +08:00
|
|
|
final dw = getDisplayWidth() * _scale;
|
|
|
|
final dh = getDisplayHeight() * _scale;
|
|
|
|
var dxOffset = 0;
|
|
|
|
var dyOffset = 0;
|
|
|
|
if (dw > size.width) {
|
|
|
|
dxOffset = (x - dw * (x / size.width) - _x).toInt();
|
2022-02-06 16:29:56 +08:00
|
|
|
}
|
2022-08-12 20:14:53 +08:00
|
|
|
if (dh > size.height) {
|
|
|
|
dyOffset = (y - dh * (y / size.height) - _y).toInt();
|
|
|
|
}
|
|
|
|
_x += dxOffset;
|
|
|
|
_y += dyOffset;
|
|
|
|
if (dxOffset != 0 || dyOffset != 0) {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2022-09-03 10:39:33 +08:00
|
|
|
|
|
|
|
// If keyboard is not permitted, do not move cursor when mouse is moving.
|
2022-09-07 12:20:53 +08:00
|
|
|
if (parent.target != null && parent.target!.ffiModel.keyboard()) {
|
|
|
|
// Draw cursor if is not desktop.
|
|
|
|
if (!isDesktop) {
|
2022-09-03 10:39:33 +08:00
|
|
|
parent.target!.cursorModel.moveLocal(x, y);
|
2022-09-07 12:20:53 +08:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
RemoteCursorMovedState.find(id).value = false;
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
2022-09-03 10:39:33 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-06 16:29:56 +08:00
|
|
|
}
|
|
|
|
|
2020-11-24 12:11:55 +08:00
|
|
|
set scale(v) {
|
|
|
|
_scale = v;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
panX(double dx) {
|
2020-11-24 22:03:04 +08:00
|
|
|
_x += dx;
|
|
|
|
notifyListeners();
|
2020-11-23 23:18:42 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
resetOffset() {
|
2022-05-23 16:02:37 +08:00
|
|
|
if (isWebDesktop) {
|
2022-02-03 00:53:59 +08:00
|
|
|
updateViewStyle();
|
|
|
|
} else {
|
2022-09-03 10:39:33 +08:00
|
|
|
_x = (size.width - getDisplayWidth() * _scale) / 2;
|
|
|
|
_y = (size.height - getDisplayHeight() * _scale) / 2;
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
2020-11-27 17:59:42 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
panY(double dy) {
|
2020-11-23 23:18:42 +08:00
|
|
|
_y += dy;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
updateScale(double v) {
|
2022-06-13 21:07:26 +08:00
|
|
|
if (parent.target?.imageModel.image == null) return;
|
2022-09-03 10:39:33 +08:00
|
|
|
final offset = parent.target?.cursorModel.offset ?? const Offset(0, 0);
|
2022-06-13 21:07:26 +08:00
|
|
|
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-09-12 16:35:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 22:06:05 +08:00
|
|
|
// data for cursor
|
|
|
|
class CursorData {
|
|
|
|
final String peerId;
|
|
|
|
final int id;
|
|
|
|
final Uint8List? data;
|
|
|
|
final double hotx;
|
|
|
|
final double hoty;
|
|
|
|
final int width;
|
|
|
|
final int height;
|
|
|
|
|
|
|
|
CursorData({
|
|
|
|
required this.peerId,
|
|
|
|
required this.id,
|
|
|
|
required this.data,
|
|
|
|
required this.hotx,
|
|
|
|
required this.hoty,
|
|
|
|
required this.width,
|
|
|
|
required this.height,
|
2022-09-08 10:52:30 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
int _doubleToInt(double v) => (v * 10e6).round().toInt();
|
|
|
|
|
|
|
|
String key(double scale) =>
|
2022-09-09 12:05:26 +08:00
|
|
|
'${peerId}_${id}_${_doubleToInt(width * scale)}_${_doubleToInt(height * scale)}';
|
2022-09-07 22:06:05 +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;
|
2022-09-07 17:14:52 +08:00
|
|
|
final _images = <int, Tuple3<ui.Image, double, double>>{};
|
2022-09-07 22:06:05 +08:00
|
|
|
CursorData? _cacheLinux;
|
|
|
|
final _cacheMapLinux = <int, CursorData>{};
|
2022-09-08 10:52:30 +08:00
|
|
|
final _cacheKeysLinux = <String>{};
|
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-09-07 12:20:53 +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-09-07 22:06:05 +08:00
|
|
|
CursorData? get cacheLinux => _cacheLinux;
|
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);
|
|
|
|
|
2022-09-08 10:52:30 +08:00
|
|
|
Set<String> get cachedKeysLinux => _cacheKeysLinux;
|
|
|
|
addKeyLinux(String key) => _cacheKeysLinux.add(key);
|
2022-09-07 22:06:05 +08:00
|
|
|
|
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-09-12 16:35:56 +08:00
|
|
|
touch(double x, double y, MouseButtons button) {
|
2022-02-17 18:00:44 +08:00
|
|
|
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-09-12 16:35:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
reset() {
|
2021-08-21 17:18:14 +08:00
|
|
|
_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-09-12 16:35:56 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-09-11 10:50:48 +08:00
|
|
|
updateCursorData(Map<String, dynamic> evt) async {
|
2020-11-19 00:32:46 +08:00
|
|
|
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;
|
2022-09-11 10:50:48 +08:00
|
|
|
final image = await img.decodeImageFromPixels(
|
|
|
|
rgba, width, height, ui.PixelFormat.rgba8888);
|
|
|
|
if (parent.target?.id != pid) return;
|
|
|
|
_image = image;
|
|
|
|
_images[id] = Tuple3(image, _hotx, _hoty);
|
|
|
|
await _updateCacheLinux(image, id, width, height);
|
|
|
|
try {
|
|
|
|
// my throw exception, because the listener maybe already dispose
|
|
|
|
notifyListeners();
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint('notify cursor: $e');
|
2022-09-07 22:06:05 +08:00
|
|
|
}
|
2022-09-11 10:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateCacheLinux(ui.Image image, int id, int w, int h) async {
|
|
|
|
final data = await image.toByteData(format: ui.ImageByteFormat.png);
|
2022-09-07 22:06:05 +08:00
|
|
|
_cacheLinux = CursorData(
|
|
|
|
peerId: this.id,
|
2022-09-11 10:50:48 +08:00
|
|
|
data: data?.buffer.asUint8List(),
|
2022-09-07 22:06:05 +08:00
|
|
|
id: id,
|
|
|
|
hotx: _hotx,
|
|
|
|
hoty: _hoty,
|
|
|
|
width: w,
|
|
|
|
height: h,
|
|
|
|
);
|
|
|
|
_cacheMapLinux[id] = _cacheLinux!;
|
|
|
|
}
|
|
|
|
|
2022-09-11 10:50:48 +08:00
|
|
|
updateCursorId(Map<String, dynamic> evt) async {
|
|
|
|
final id = int.parse(evt['id']);
|
|
|
|
_cacheLinux = _cacheMapLinux[id];
|
|
|
|
final tmp = _images[id];
|
2020-11-19 00:32:46 +08:00
|
|
|
if (tmp != null) {
|
2022-09-07 17:14:52 +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.
|
2022-09-11 10:50:48 +08:00
|
|
|
updateCursorPosition(Map<String, dynamic> evt, String id) async {
|
2020-11-19 00:32:46 +08:00
|
|
|
_x = double.parse(evt['x']);
|
|
|
|
_y = double.parse(evt['y']);
|
2022-09-07 12:20:53 +08:00
|
|
|
try {
|
2022-09-07 17:14:52 +08:00
|
|
|
RemoteCursorMovedState.find(id).value = true;
|
2022-09-07 12:20:53 +08:00
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
updateDisplayOrigin(double x, double y) {
|
2020-11-19 00:32:46 +08:00
|
|
|
_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-09-12 16:35:56 +08:00
|
|
|
updateDisplayOriginWithCursor(
|
2022-05-31 14:44:06 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
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-09-07 22:06:05 +08:00
|
|
|
|
|
|
|
_clearCacheLinux();
|
|
|
|
_cacheLinux = null;
|
|
|
|
_cacheMapLinux.clear();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
_clearCacheLinux() {
|
2022-09-08 10:52:30 +08:00
|
|
|
final cachedKeys = {...cachedKeysLinux};
|
2022-09-07 22:06:05 +08:00
|
|
|
for (var key in cachedKeys) {
|
|
|
|
customCursorController.freeCache(key);
|
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2022-08-05 20:29:43 +08:00
|
|
|
checkShowQualityMonitor(String id) async {
|
2022-08-16 15:22:57 +08:00
|
|
|
final show = await bind.sessionGetToggleOption(
|
2022-08-05 20:29:43 +08:00
|
|
|
id: id, arg: 'show-quality-monitor') ==
|
|
|
|
true;
|
2022-08-04 17:24:02 +08:00
|
|
|
if (_show != show) {
|
|
|
|
_show = show;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateQualityStatus(Map<String, dynamic> evt) {
|
|
|
|
try {
|
2022-09-07 12:20:53 +08:00
|
|
|
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'];
|
|
|
|
}
|
2022-08-04 17:24:02 +08:00
|
|
|
notifyListeners();
|
2022-09-07 22:06:05 +08:00
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
2022-08-04 17:24:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2022-09-07 12:20:53 +08:00
|
|
|
return 'left';
|
2022-02-17 18:00:44 +08:00
|
|
|
case MouseButtons.right:
|
2022-09-07 12:20:53 +08:00
|
|
|
return 'right';
|
2022-02-17 18:00:44 +08:00
|
|
|
case MouseButtons.wheel:
|
2022-09-07 12:20:53 +08:00
|
|
|
return 'wheel';
|
2022-02-17 18:00:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:08:45 +08:00
|
|
|
enum ConnType { defaultConn, fileTransfer, portForward, rdp }
|
|
|
|
|
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-09-07 12:20:53 +08:00
|
|
|
var id = '';
|
2022-06-13 21:07:26 +08:00
|
|
|
var shift = false;
|
|
|
|
var ctrl = false;
|
|
|
|
var alt = false;
|
|
|
|
var command = false;
|
2022-09-07 12:20:53 +08:00
|
|
|
var version = '';
|
2022-09-06 19:08:45 +08:00
|
|
|
var connType = ConnType.defaultConn;
|
2022-08-12 18:42:02 +08:00
|
|
|
|
|
|
|
/// dialogManager use late to ensure init after main page binding [globalKey]
|
|
|
|
late final dialogManager = OverlayDialogManager();
|
|
|
|
|
|
|
|
late final ImageModel imageModel; // session
|
|
|
|
late final FfiModel ffiModel; // session
|
|
|
|
late final CursorModel cursorModel; // session
|
|
|
|
late final CanvasModel canvasModel; // session
|
|
|
|
late final ServerModel serverModel; // global
|
|
|
|
late final ChatModel chatModel; // session
|
|
|
|
late final FileModel fileModel; // session
|
|
|
|
late final AbModel abModel; // global
|
|
|
|
late final UserModel userModel; // global
|
|
|
|
late final QualityMonitorModel qualityMonitorModel; // session
|
2022-06-13 21:07:26 +08:00
|
|
|
|
|
|
|
FFI() {
|
2022-08-31 23:02:16 +08:00
|
|
|
imageModel = ImageModel(WeakReference(this));
|
|
|
|
ffiModel = FfiModel(WeakReference(this));
|
|
|
|
cursorModel = CursorModel(WeakReference(this));
|
|
|
|
canvasModel = CanvasModel(WeakReference(this));
|
|
|
|
serverModel = ServerModel(WeakReference(this)); // use global FFI
|
|
|
|
chatModel = ChatModel(WeakReference(this));
|
|
|
|
fileModel = FileModel(WeakReference(this));
|
|
|
|
abModel = AbModel(WeakReference(this));
|
|
|
|
userModel = UserModel(WeakReference(this));
|
|
|
|
qualityMonitorModel = QualityMonitorModel(WeakReference(this));
|
2022-06-13 21:07:26 +08:00
|
|
|
}
|
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-09-12 16:35:56 +08:00
|
|
|
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-09-12 16:35:56 +08:00
|
|
|
scroll(int y) {
|
2022-08-08 22:27:27 +08:00
|
|
|
bind.sessionSendMouse(
|
|
|
|
id: id,
|
|
|
|
msg: 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-09-12 16:35:56 +08:00
|
|
|
// static reconnect() {
|
2022-05-31 22:09:36 +08:00
|
|
|
// 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-09-12 16:35:56 +08:00
|
|
|
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-09-12 16:35:56 +08:00
|
|
|
sendMouse(String type, MouseButtons button) {
|
2020-11-25 16:28:46 +08:00
|
|
|
if (!ffiModel.keyboard()) return;
|
2022-08-08 22:27:27 +08:00
|
|
|
bind.sessionSendMouse(
|
|
|
|
id: id,
|
|
|
|
msg: json.encode(modify({'type': type, 'buttons': button.value})));
|
2020-11-25 16:28:46 +08:00
|
|
|
}
|
|
|
|
|
2022-09-02 19:49:36 +08:00
|
|
|
// Raw Key
|
2022-09-12 16:35:56 +08:00
|
|
|
inputRawKey(String name, int keyCode, int scanCode, bool down) {
|
2022-09-02 19:49:36 +08:00
|
|
|
bind.sessionHandleFlutterKeyEvent(
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
keycode: keyCode,
|
|
|
|
scancode: scanCode,
|
|
|
|
downOrUp: down);
|
2022-08-27 09:29:46 +08:00
|
|
|
}
|
|
|
|
|
2022-09-06 11:39:01 +08:00
|
|
|
Future<String> getKeyboardMode() {
|
2022-09-05 22:18:29 +08:00
|
|
|
return bind.sessionGetKeyboardName(id: id);
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
enterOrLeave(bool enter) {
|
2022-09-06 11:39:01 +08:00
|
|
|
// Fix status
|
|
|
|
if (!enter) {
|
2022-09-06 16:39:24 +08:00
|
|
|
resetModifiers();
|
2022-09-06 11:39:01 +08:00
|
|
|
}
|
2022-09-05 18:19:05 +08:00
|
|
|
bind.sessionEnterOrLeave(id: id, enter: enter);
|
|
|
|
}
|
|
|
|
|
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-09-12 16:35:56 +08:00
|
|
|
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) {
|
2022-09-07 12:20:53 +08:00
|
|
|
// out['down'] = 'true';
|
2022-05-31 22:09:36 +08:00
|
|
|
// }
|
|
|
|
// // default: press = true
|
|
|
|
// if (press != false) {
|
2022-09-07 12:20:53 +08:00
|
|
|
// out['press'] = 'true';
|
2022-05-31 22:09:36 +08:00
|
|
|
// }
|
|
|
|
// 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-09-12 16:35:56 +08:00
|
|
|
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-08-08 22:27:27 +08:00
|
|
|
bind.sessionSendMouse(
|
|
|
|
id: id, msg: json.encode(modify({'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-08-08 17:53:51 +08:00
|
|
|
Future<List<Peer>> peers() async {
|
2020-11-19 00:32:46 +08:00
|
|
|
try {
|
2022-08-08 17:53:51 +08:00
|
|
|
var str = await bind.mainGetRecentPeers();
|
2022-09-07 12:20:53 +08:00
|
|
|
if (str == '') return [];
|
2022-04-14 11:00:24 +08:00
|
|
|
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) {
|
2022-09-07 17:14:52 +08:00
|
|
|
debugPrint('peers(): $e');
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-08-26 11:35:28 +08:00
|
|
|
/// Connect with the given [id]. Only transfer file if [isFileTransfer], only port forward if [isPortForward].
|
2022-09-12 16:35:56 +08:00
|
|
|
connect(String id,
|
2022-08-26 11:35:28 +08:00
|
|
|
{bool isFileTransfer = false,
|
|
|
|
bool isPortForward = false,
|
|
|
|
double tabBarHeight = 0.0}) {
|
2022-09-07 12:20:53 +08:00
|
|
|
assert(!(isFileTransfer && isPortForward), 'more than one connect type');
|
2022-08-26 11:35:28 +08:00
|
|
|
if (isFileTransfer) {
|
2022-09-06 19:08:45 +08:00
|
|
|
connType = ConnType.fileTransfer;
|
|
|
|
id = 'ft_$id';
|
2022-08-26 11:35:28 +08:00
|
|
|
} else if (isPortForward) {
|
2022-09-06 19:08:45 +08:00
|
|
|
connType = ConnType.portForward;
|
|
|
|
id = 'pf_$id';
|
2022-08-26 11:35:28 +08:00
|
|
|
} else {
|
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-08-30 21:15:35 +08:00
|
|
|
// ignore: unused_local_variable
|
|
|
|
final addRes = bind.sessionAddSync(
|
2022-08-26 11:35:28 +08:00
|
|
|
id: id, isFileTransfer: isFileTransfer, isPortForward: isPortForward);
|
2022-08-30 21:15:35 +08:00
|
|
|
final stream = bind.sessionStart(id: id);
|
2022-06-21 17:58:27 +08:00
|
|
|
final cb = ffiModel.startEventListener(id);
|
|
|
|
() async {
|
|
|
|
await for (final message in stream) {
|
|
|
|
if (message is Event) {
|
|
|
|
try {
|
|
|
|
Map<String, dynamic> event = json.decode(message.field0);
|
2022-09-12 10:52:38 +08:00
|
|
|
await cb(event);
|
2022-06-21 17:58:27 +08:00
|
|
|
} catch (e) {
|
2022-09-12 10:52:38 +08:00
|
|
|
debugPrint('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) {
|
2022-09-06 19:08:45 +08:00
|
|
|
fileModel.initFileFetcher();
|
2022-06-21 17:58:27 +08:00
|
|
|
}
|
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-09-12 16:35:56 +08:00
|
|
|
login(String id, String password, bool remember) {
|
2022-06-13 21:07:26 +08:00
|
|
|
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);
|
2022-09-07 12:20:53 +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-09-07 12:20:53 +08:00
|
|
|
debugPrint('model $id 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-08-08 22:27:27 +08:00
|
|
|
// String getByName(String name, [String arg = '']) {
|
|
|
|
// return 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-09-12 16:35:56 +08:00
|
|
|
// setByName(String name, [String value = '']) {
|
2022-08-08 22:27:27 +08:00
|
|
|
// platformFFI.setByName(name, value);
|
|
|
|
// }
|
2020-11-19 00:32:46 +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;
|
2022-08-12 20:14:53 +08:00
|
|
|
if (canvasModel.scrollStyle == ScrollStyle.scrollbar) {
|
|
|
|
final imageWidth = d.width * canvasModel.scale;
|
|
|
|
final imageHeight = d.height * canvasModel.scale;
|
|
|
|
x += imageWidth * canvasModel.scrollX;
|
|
|
|
y += imageHeight * canvasModel.scrollY;
|
2022-08-13 15:08:17 +08:00
|
|
|
|
|
|
|
// boxed size is a center widget
|
|
|
|
if (canvasModel.size.width > imageWidth) {
|
|
|
|
x -= ((canvasModel.size.width - imageWidth) / 2);
|
|
|
|
}
|
|
|
|
if (canvasModel.size.height > imageHeight) {
|
|
|
|
y -= ((canvasModel.size.height - imageHeight) / 2);
|
|
|
|
}
|
2022-08-12 20:14:53 +08:00
|
|
|
} else {
|
|
|
|
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-08-08 22:27:27 +08:00
|
|
|
bind.sessionSendMouse(id: id, msg: 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-08-03 22:03:31 +08:00
|
|
|
platformFFI.startDesktopWebListener();
|
2022-02-03 00:53:59 +08:00
|
|
|
} else {
|
2022-08-03 22:03:31 +08:00
|
|
|
platformFFI.stopDesktopWebListener();
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 02:07:53 +08:00
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
setMethodCallHandler(FMethod callback) {
|
2022-08-03 22:03:31 +08:00
|
|
|
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 {
|
2022-08-03 22:03:31 +08:00
|
|
|
return await 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-08-08 17:53:51 +08:00
|
|
|
Future<String> getDefaultAudioInput() async {
|
|
|
|
final input = await bind.mainGetOption(key: 'audio-input');
|
2022-07-14 12:32:01 +08:00
|
|
|
if (input.isEmpty && Platform.isWindows) {
|
2022-09-07 12:20:53 +08:00
|
|
|
return 'System Sound';
|
2022-07-14 12:32:01 +08:00
|
|
|
}
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:35:56 +08:00
|
|
|
setDefaultAudioInput(String input) {
|
2022-08-08 17:53:51 +08:00
|
|
|
bind.mainSetOption(key: 'audio-input', value: input);
|
2022-07-14 12:32:01 +08:00
|
|
|
}
|
2022-07-25 16:23:45 +08:00
|
|
|
|
|
|
|
Future<Map<String, String>> getHttpHeaders() async {
|
2022-08-08 17:53:51 +08:00
|
|
|
return {
|
2022-09-07 12:20:53 +08:00
|
|
|
'Authorization':
|
2022-09-07 22:06:05 +08:00
|
|
|
'Bearer ${await bind.mainGetLocalOption(key: 'access_token')}'
|
2022-08-08 17:53:51 +08:00
|
|
|
};
|
2022-07-25 16:23:45 +08:00
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class Display {
|
|
|
|
double x = 0;
|
|
|
|
double y = 0;
|
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
2022-09-12 10:52:38 +08:00
|
|
|
|
|
|
|
Display() {
|
|
|
|
width = (isDesktop || isWebDesktop)
|
|
|
|
? kDesktopDefaultDisplayWidth
|
|
|
|
: kMobileDefaultDisplayWidth;
|
|
|
|
height = (isDesktop || isWebDesktop)
|
|
|
|
? kDesktopDefaultDisplayHeight
|
|
|
|
: kMobileDefaultDisplayHeight;
|
|
|
|
}
|
2020-11-19 00:32:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class PeerInfo {
|
2022-09-07 12:20:53 +08:00
|
|
|
String version = '';
|
|
|
|
String username = '';
|
|
|
|
String hostname = '';
|
|
|
|
String platform = '';
|
2022-02-17 15:22:14 +08:00
|
|
|
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-09-12 10:52:38 +08:00
|
|
|
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();
|
2022-09-01 21:18:29 +08:00
|
|
|
final p = <String, dynamic>{};
|
2020-12-21 18:28:28 +08:00
|
|
|
p['xCursor'] = xCursor;
|
|
|
|
p['yCursor'] = yCursor;
|
|
|
|
p['xCanvas'] = xCanvas;
|
|
|
|
p['yCanvas'] = yCanvas;
|
|
|
|
p['scale'] = scale;
|
|
|
|
p['currentDisplay'] = currentDisplay;
|
2022-09-01 21:18:29 +08:00
|
|
|
prefs.setString('peer$id', json.encode(p));
|
2020-12-21 18:28:28 +08:00
|
|
|
}
|
|
|
|
|
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();
|
2022-09-01 21:18:29 +08:00
|
|
|
var p = prefs.getString('peer$id');
|
2020-12-21 18:28:28 +08:00
|
|
|
if (p == null) return null;
|
|
|
|
Map<String, dynamic> m = json.decode(p);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2022-09-12 10:52:38 +08:00
|
|
|
removePreference(String id) async {
|
2021-08-06 21:18:06 +08:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2022-09-01 21:18:29 +08:00
|
|
|
prefs.remove('peer$id');
|
2020-12-21 19:05:31 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 10:52:38 +08:00
|
|
|
initializeCursorAndCanvas(FFI ffi) async {
|
2022-06-13 21:07:26 +08:00
|
|
|
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.
|
2022-08-08 17:53:51 +08:00
|
|
|
// String translate(String name, {FFI? ffi}) {
|
|
|
|
// if (name.startsWith('Failed to') && name.contains(': ')) {
|
|
|
|
// return name.split(': ').map((x) => translate(x)).join(': ');
|
|
|
|
// }
|
|
|
|
// var a = 'translate';
|
|
|
|
// var b = '{"locale": "$localeName", "text": "$name"}';
|
|
|
|
//
|
|
|
|
// return (ffi ?? gFFI).getByName(a, b);
|
|
|
|
// }
|