rustdesk/flutter/lib/models/model.dart

1314 lines
39 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
2022-07-14 12:32:01 +08:00
import 'dart:io';
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';
import 'package:flutter_hbb/models/ab_model.dart';
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';
import 'package:flutter_hbb/models/user_model.dart';
import 'package:shared_preferences/shared_preferences.dart';
2020-11-23 23:18:42 +08:00
import 'package:tuple/tuple.dart';
import '../common.dart';
2022-05-24 23:33:00 +08:00
import '../mobile/widgets/dialog.dart';
import '../mobile/widgets/overlay.dart';
import 'peer_model.dart';
import 'platform_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-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;
bool _touchMode = false;
Timer? _timer;
var _reconnects = 1;
WeakReference<FFI> parent;
2020-11-22 21:08:19 +08:00
Map<String, bool> get permissions => _permissions;
2022-02-02 17:25:56 +08:00
Display get display => _display;
2022-02-02 17:25:56 +08:00
bool? get secure => _secure;
2022-02-02 17:25:56 +08:00
bool? get direct => _direct;
2022-02-02 17:25:56 +08:00
PeerInfo get pi => _pi;
2022-02-17 15:22:14 +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
FfiModel(this.parent) {
2020-11-22 21:08:19 +08:00
clear();
2022-04-17 00:44:05 +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;
_secure = null;
_direct = null;
2022-02-02 00:46:21 +08:00
_inputBlocked = false;
_timer?.cancel();
_timer = null;
2020-11-28 13:22:19 +08:00
clearPermissions();
}
void setConnectionType(bool secure, bool direct) {
_secure = secure;
_direct = direct;
}
2022-02-17 15:22:14 +08:00
Image? getConnectionImage() {
String? icon;
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') {
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') {
parent.target?.cursorModel.updateCursorData(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'cursor_id') {
parent.target?.cursorModel.updateCursorId(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'cursor_position') {
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') {
parent.target?.ffiModel.updatePermission(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'chat_client_mode') {
parent.target?.chatModel
.receive(ChatModel.clientModeID, evt['text'] ?? "");
2022-05-31 17:36:36 +08:00
} else if (name == 'chat_server_mode') {
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') {
parent.target?.fileModel.receiveFileDir(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'job_progress') {
parent.target?.fileModel.tryUpdateJobProgress(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'job_done') {
parent.target?.fileModel.jobDone(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'job_error') {
parent.target?.fileModel.jobError(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'override_file_confirm') {
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') {
parent.target?.serverModel.loginRequest(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'on_client_authorized') {
parent.target?.serverModel.onClientAuthorized(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'on_client_remove') {
parent.target?.serverModel.onClientRemove(evt);
2022-08-05 20:29:43 +08:00
} else if (name == 'update_quality_status') {
parent.target?.qualityMonitorModel.updateQualityStatus(evt);
} else if (name == 'update_block_input_state') {
updateBlockInputState(evt, peerId);
} else if (name == 'update_privacy_mode') {
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.
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);
} else if (name == 'connection_ready') {
parent.target?.ffiModel.setConnectionType(
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') {
parent.target?.cursorModel.updateCursorData(evt);
2020-11-19 00:32:46 +08:00
} else if (name == 'cursor_id') {
parent.target?.cursorModel.updateCursorId(evt);
2020-11-19 00:32:46 +08:00
} else if (name == 'cursor_position') {
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') {
parent.target?.ffiModel.updatePermission(evt);
} else if (name == 'chat_client_mode') {
parent.target?.chatModel
.receive(ChatModel.clientModeID, evt['text'] ?? "");
} else if (name == 'chat_server_mode') {
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') {
parent.target?.fileModel.receiveFileDir(evt);
2022-03-19 23:28:29 +08:00
} else if (name == 'job_progress') {
parent.target?.fileModel.tryUpdateJobProgress(evt);
2022-03-19 23:28:29 +08:00
} else if (name == 'job_done') {
parent.target?.fileModel.jobDone(evt);
2022-03-19 23:28:29 +08:00
} else if (name == 'job_error') {
parent.target?.fileModel.jobError(evt);
2022-05-17 20:56:36 +08:00
} else if (name == 'override_file_confirm') {
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') {
parent.target?.serverModel.loginRequest(evt);
2022-03-22 21:47:42 +08:00
} else if (name == 'on_client_authorized') {
parent.target?.serverModel.onClientAuthorized(evt);
2022-03-19 23:28:29 +08:00
} else if (name == 'on_client_remove') {
parent.target?.serverModel.onClientRemove(evt);
2022-08-04 17:24:02 +08:00
} else if (name == 'update_quality_status') {
parent.target?.qualityMonitorModel.updateQualityStatus(evt);
} else if (name == 'update_block_input_state') {
updateBlockInputState(evt, peerId);
} else if (name == 'update_privacy_mode') {
updatePrivacyMode(evt, peerId);
2020-11-19 00:32:46 +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;
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']);
if (old != _pi.currentDisplay)
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) {
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].
void 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;
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);
} 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);
} else {
var hasRetry = evt['hasRetry'] == 'true';
2022-08-12 18:42:02 +08:00
showMsgBox(id, type, title, text, hasRetry, dialogManager);
}
}
2022-05-28 03:56:42 +08:00
/// Show a message box with [type], [title] and [text].
2022-08-12 18:42:02 +08:00
void 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();
if (hasRetry) {
_timer = Timer(Duration(seconds: _reconnects), () {
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...'),
onCancel: closeConnection);
});
_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-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'];
_pi.sasEnabled = evt['sas_enabled'] == "true";
_pi.currentDisplay = int.parse(evt['current_display']);
try {
CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
} catch (e) {
//
}
if (isPeerAndroid) {
_touchMode = true;
if (parent.target?.ffiModel.permissions['keyboard'] != false) {
2022-05-16 00:01:27 +08:00
Timer(Duration(milliseconds: 100), showMobileActionsOverlay);
}
} else {
_touchMode =
2022-08-16 15:22:57 +08:00
await bind.sessionGetOption(id: peerId, arg: "touch-mode") != '';
}
2022-03-19 23:28:29 +08:00
if (evt['is_file_transfer'] == "true") {
parent.target?.fileModel.onReady();
2022-03-19 23:28:29 +08:00
} else {
_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...'),
onCancel: closeConnection);
_waitForImage = true;
_reconnects = 1;
}
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
}
updateBlockInputState(Map<String, dynamic> evt, String peerId) {
_inputBlocked = evt['input_state'] == 'on';
notifyListeners();
try {
BlockInputState.find(peerId).value = evt['input_state'] == 'on';
} catch (e) {
//
}
}
updatePrivacyMode(Map<String, dynamic> evt, String peerId) {
notifyListeners();
try {
PrivacyModeState.find(peerId).value =
bind.sessionGetToggleOptionSync(id: peerId, arg: 'privacy-mode');
} catch (e) {
//
}
}
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
WeakReference<FFI> parent;
ImageModel(this.parent);
void 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
}
final pid = parent.target?.id;
2022-05-31 22:09:36 +08:00
ui.decodeImageFromPixels(
rgba,
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) {
if (parent.target?.id != pid) return;
2022-05-31 22:09:36 +08:00
try {
// my throw exception, because the listener maybe already dispose
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
}
});
}
void update(ui.Image? image, double tabBarHeight) {
2020-11-24 22:03:04 +08:00
if (_image == null && image != null) {
if (isWebDesktop || isDesktop) {
parent.target?.canvasModel.updateViewStyle();
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;
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);
}
if (parent.target != null) {
initializeCursorAndCanvas(parent.target!);
2022-02-03 00:53:59 +08:00
}
Future.delayed(Duration(milliseconds: 1), () {
if (parent.target?.ffiModel.isPeerAndroid ?? false) {
bind.sessionPeerOption(id: _id, name: "view-style", value: "shrink");
parent.target?.canvasModel.updateViewStyle();
}
});
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
}
// mobile only
// for desktop, height should minus tabbar height
double get maxScale {
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;
return max(1.5, max(xscale, yscale));
}
// mobile only
// for desktop, height should minus tabbar height
double get minScale {
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;
return min(xscale, yscale) / 1.5;
}
2020-11-19 00:32:46 +08:00
}
enum ScrollStyle {
scrollbar,
scrollauto,
}
2020-11-23 23:18:42 +08:00
class CanvasModel with ChangeNotifier {
// scroll offset x percent
double _scrollX = 0.0;
// scroll offset y percent
double _scrollY = 0.0;
2022-02-17 15:22:14 +08:00
double _x = 0;
double _y = 0;
double _scale = 1.0;
double _tabBarHeight = 0.0;
2022-05-31 22:09:36 +08:00
String id = ""; // TODO multi canvas model
ScrollStyle _scrollStyle = ScrollStyle.scrollauto;
2020-11-23 23:18:42 +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;
ScrollStyle get scrollStyle => _scrollStyle;
2020-11-23 23:18:42 +08:00
setScrollPercent(double x, double y) {
_scrollX = x;
_scrollY = y;
}
double get scrollX => _scrollX;
double get scrollY => _scrollY;
set tabBarHeight(double h) => _tabBarHeight = h;
double get tabBarHeight => _tabBarHeight;
2022-05-31 22:09:36 +08:00
void updateViewStyle() async {
2022-08-16 15:22:57 +08:00
final style = await bind.sessionGetOption(id: id, arg: 'view-style');
if (style == null) {
2022-05-31 22:09:36 +08:00
return;
}
final s1 = size.width / (parent.target?.ffiModel.display.width ?? 720);
final s2 = size.height / (parent.target?.ffiModel.display.height ?? 1280);
// 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;
}
};
// Closure to perform stretch operation.
final stretchOp = () {
final s = s1 < s2 ? s1 : s2;
2022-02-03 00:53:59 +08:00
if (s > 1) {
_scale = s;
}
};
// Closure to perform default operation(set the scale to 1.0).
final defaultOp = () {
_scale = 1.0;
};
// // On desktop, shrink is the default behavior.
// if (isDesktop) {
// shrinkOp();
// } else {
defaultOp();
// }
if (style == 'shrink') {
shrinkOp();
} else if (style == 'stretch') {
stretchOp();
2022-02-03 00:53:59 +08:00
}
_x = (size.width - getDisplayWidth() * _scale) / 2;
_y = (size.height - getDisplayHeight() * _scale) / 2;
2022-02-03 00:53:59 +08:00
notifyListeners();
}
updateScrollStyle() async {
2022-08-16 15:22:57 +08:00
final style = await bind.sessionGetOption(id: id, arg: 'scroll-style');
if (style == 'scrollbar') {
_scrollStyle = ScrollStyle.scrollbar;
_scrollX = 0.0;
_scrollY = 0.0;
} else {
_scrollStyle = ScrollStyle.scrollauto;
}
notifyListeners();
}
void update(double x, double y, double scale) {
_x = x;
_y = y;
_scale = scale;
notifyListeners();
}
int getDisplayWidth() {
return parent.target?.ffiModel.display.width ?? 1080;
}
int getDisplayHeight() {
return parent.target?.ffiModel.display.height ?? 720;
}
Size get size {
final size = MediaQueryData.fromWindow(ui.window).size;
return Size(size.width, size.height - _tabBarHeight);
}
2022-02-06 16:29:56 +08:00
void moveDesktopMouse(double x, double y) {
// On mobile platforms, move the canvas with the cursor.
//if (!isDesktop) {
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
}
if (dh > size.height) {
dyOffset = (y - dh * (y / size.height) - _y).toInt();
}
_x += dxOffset;
_y += dyOffset;
if (dxOffset != 0 || dyOffset != 0) {
notifyListeners();
}
//}
parent.target?.cursorModel.moveLocal(x, y);
2022-02-06 16:29:56 +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 {
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) {
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;
final maxs = parent.target?.imageModel.maxScale ?? 1;
final mins = parent.target?.imageModel.minScale ?? 1;
if (_scale > maxs) _scale = maxs;
if (_scale < mins) _scale = mins;
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
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
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;
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;
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
}
void touch(double x, double y, MouseButtons button) {
moveLocal(x, y);
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) {
moveLocal(x, y);
parent.target?.moveMouse(_x, _y);
}
void moveLocal(double x, double y) {
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;
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) {
if (parent.target?.imageModel.image == null) return;
2021-08-21 17:18:14 +08:00
if (touchMode) {
final scale = parent.target?.canvasModel.scale ?? 1.0;
2022-04-19 13:07:45 +08:00
_x += dx / scale;
_y += dy / scale;
parent.target?.moveMouse(_x, _y);
2022-04-19 13:07:45 +08:00
notifyListeners();
2021-08-21 17:18:14 +08:00
return;
}
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 +
(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 +
(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) {
parent.target?.canvasModel.panX(-dx);
2020-11-24 22:03:04 +08:00
}
if (tryMoveCanvasY && dy != 0) {
parent.target?.canvasModel.panY(-dy);
2020-11-24 22:03:04 +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());
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) {
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();
}
}
/// 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;
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) {
_displayOriginX = x;
_displayOriginY = y;
_x = xCursor;
_y = yCursor;
parent.target?.moveMouse(x, y);
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;
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 {
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-03-07 22:54:34 +08:00
extension ToString on MouseButtons {
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 {
var id = "";
var shift = false;
var ctrl = false;
var alt = false;
var command = false;
var version = "";
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
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));
this.abModel = AbModel(WeakReference(this));
this.userModel = UserModel(WeakReference(this));
2022-08-04 17:24:02 +08:00
this.qualityMonitorModel = QualityMonitorModel(WeakReference(this));
}
2020-11-19 00:32:46 +08:00
2022-05-28 03:56:42 +08:00
/// Send a mouse tap event(down and up).
void tap(MouseButtons button) {
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].
void scroll(int y) {
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-05-31 22:09:36 +08:00
// static void reconnect() {
// setByName('reconnect');
// 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].
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.
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;
}
2022-05-28 03:56:42 +08:00
/// Send mouse press event.
void sendMouse(String type, MouseButtons button) {
2020-11-25 16:28:46 +08:00
if (!ffiModel.keyboard()) return;
bind.sessionSendMouse(
id: id,
msg: json.encode(modify({'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).
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
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);
}
2022-05-28 03:56:42 +08:00
/// Send mouse movement event with distance in [x] and [y].
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();
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.
Future<List<Peer>> peers() async {
2020-11-19 00:32:46 +08:00
try {
var str = await bind.mainGetRecentPeers();
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) {
print('peers(): $e');
2020-11-19 00:32:46 +08:00
}
return [];
}
/// Connect with the given [id]. Only transfer file if [isFileTransfer], only port forward if [isPortForward].
void connect(String id,
{bool isFileTransfer = false,
bool isPortForward = false,
double tabBarHeight = 0.0}) {
assert(!(isFileTransfer && isPortForward), "more than one connect type");
if (isFileTransfer) {
id = 'ft_${id}';
} else if (isPortForward) {
id = 'pf_${id}';
} else {
chatModel.resetClientMode();
canvasModel.id = id;
imageModel._id = id;
cursorModel.id = id;
}
final stream = bind.sessionConnect(
id: id, isFileTransfer: isFileTransfer, isPortForward: isPortForward);
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);
cb(event);
} catch (e) {
print('json.decode fail(): $e');
2022-05-31 17:36:36 +08:00
}
} else if (message is Rgba) {
imageModel.onRgba(message.field0, tabBarHeight);
2022-05-31 17:36:36 +08:00
}
}
}();
// every instance will bind a stream
this.id = id;
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.
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.
Future<void> close() async {
2022-03-25 16:34:27 +08:00
chatModel.close();
if (imageModel.image != null && !isWebDesktop) {
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
}
bind.sessionClose(id: id);
2020-11-28 17:42:29 +08:00
id = "";
imageModel.update(null, 0.0);
2020-11-25 16:28:46 +08:00
cursorModel.clear();
ffiModel.clear();
canvasModel.clear();
resetModifiers();
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.
// 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].
// void setByName(String name, [String value = '']) {
// platformFFI.setByName(name, value);
// }
2020-11-19 00:32:46 +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'];
var y = max(0.0, (evt['y'] as double) - tabBarHeight);
2022-02-06 16:29:56 +08:00
if (isMove) {
canvasModel.moveDesktopMouse(x, y);
2022-02-06 16:29:56 +08:00
}
final d = ffiModel.display;
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;
// 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);
}
} 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;
}
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;
bind.sessionSendMouse(id: id, msg: json.encode(evt));
2022-02-03 00:53:59 +08:00
}
listenToMouse(bool yesOrNo) {
2022-02-03 00:53:59 +08:00
if (yesOrNo) {
platformFFI.startDesktopWebListener();
2022-02-03 00:53:59 +08:00
} else {
platformFFI.stopDesktopWebListener();
2022-02-03 00:53:59 +08:00
}
}
2022-02-10 02:07:53 +08:00
void setMethodCallHandler(FMethod callback) {
platformFFI.setMethodCallHandler(callback);
2022-02-10 02:07:53 +08:00
}
Future<bool> invokeMethod(String method, [dynamic arguments]) async {
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();
}
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) {
return "System Sound";
}
return input;
}
void setDefaultAudioInput(String input) {
bind.mainSetOption(key: 'audio-input', value: input);
2022-07-14 12:32:01 +08:00
}
Future<Map<String, String>> getHttpHeaders() async {
return {
"Authorization":
"Bearer " + await bind.mainGetLocalOption(key: "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
}
Future<void> savePreference(String id, double xCursor, double yCursor,
double xCanvas, double yCanvas, double scale, int currentDisplay) async {
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;
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
}
void initializeCursorAndCanvas(FFI ffi) async {
var p = await getPreference(ffi.id);
int currentDisplay = 0;
if (p != null) {
currentDisplay = p['currentDisplay'];
}
if (p == null || currentDisplay != ffi.ffiModel.pi.currentDisplay) {
ffi.cursorModel
.updateDisplayOrigin(ffi.ffiModel.display.x, ffi.ffiModel.display.y);
return;
}
double xCursor = p['xCursor'];
double yCursor = p['yCursor'];
double xCanvas = p['xCanvas'];
double yCanvas = p['yCanvas'];
double scale = p['scale'];
ffi.cursorModel.updateDisplayOriginWithCursor(
ffi.ffiModel.display.x, ffi.ffiModel.display.y, xCursor, yCursor);
ffi.canvasModel.update(xCanvas, yCanvas, scale);
}
2021-08-02 20:54:56 +08:00
2022-05-28 03:56:42 +08:00
/// Translate text based on the pre-defined dictionary.
/// 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}) {
// 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);
// }