rustdesk/flutter/lib/models/model.dart

1220 lines
37 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
2022-10-10 10:53:10 +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';
import 'package:flutter_hbb/consts.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';
2022-10-11 19:52:03 +08:00
import 'package:flutter_hbb/utils/multi_window_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
2020-11-23 23:18:42 +08:00
import 'package:tuple/tuple.dart';
import 'package:flutter_custom_cursor/flutter_custom_cursor.dart';
2022-09-27 18:34:05 +08:00
import 'package:flutter_svg/flutter_svg.dart';
import '../common.dart';
import '../common/shared_state.dart';
import '../utils/image.dart' as img;
2022-05-24 23:33:00 +08:00
import '../mobile/widgets/dialog.dart';
2022-09-27 20:35:02 +08:00
import 'input_model.dart';
import 'platform_model.dart';
2020-11-19 00:32:46 +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-02-02 00:46:21 +08:00
var _inputBlocked = false;
final _permissions = <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
}
toggleTouchMode() {
if (!isPeerAndroid) {
_touchMode = !_touchMode;
notifyListeners();
}
}
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';
});
// Only inited at remote page
if (desktopType == DesktopType.remote) {
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
}
2020-11-24 23:36:46 +08:00
bool keyboard() => _permissions['keyboard'] != false;
2020-11-23 23:18:42 +08:00
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();
}
setConnectionType(String peerId, bool secure, bool direct) {
_secure = secure;
_direct = direct;
try {
var connectionType = ConnectionTypeState.find(peerId);
connectionType.setSecure(secure);
connectionType.setDirect(direct);
} catch (e) {
//
}
}
2022-09-27 18:34:05 +08:00
Widget? getConnectionImage() {
if (secure == null || direct == null) {
return null;
} else {
final icon =
'${secure == true ? 'secure' : 'insecure'}${direct == true ? '' : '_relay'}';
2022-09-29 14:08:15 +08:00
return SvgPicture.asset('assets/$icon.svg', width: 48, height: 48);
}
}
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
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') {
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') {
await parent.target?.cursorModel.updateCursorData(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'cursor_id') {
await parent.target?.cursorModel.updateCursorId(evt);
2022-05-31 17:36:36 +08:00
} else if (name == 'cursor_position') {
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') {
parent.target?.ffiModel.updatePermission(evt, peerId);
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
.receive(int.parse(evt['id'] as String), evt['text'] ?? '');
2022-05-31 17:36:36 +08:00
} 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);
} else if (name == 'add_connection') {
parent.target?.serverModel.addConnection(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-10-11 19:52:03 +08:00
} else if (name == 'new_connection') {
2022-10-12 21:57:19 +08:00
var arg = evt['peer_id'].toString();
if (arg.startsWith(kUniLinksPrefix)) {
parseRustdeskUri(arg);
} else {
Future.delayed(Duration.zero, () {
rustDeskWinManager.newRemoteDesktop(arg);
});
}
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.
updateEventListener(String peerId) {
platformFFI.setEventCallback(startEventListener(peerId));
2020-11-19 00:32:46 +08:00
}
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
}
parent.target?.recordingModel.onSwitchDisplay();
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].
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].
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].
handlePeerInfo(Map<String, dynamic> evt, String peerId) async {
// 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'];
_pi.sasEnabled = evt['sas_enabled'] == 'true';
2020-11-19 00:32:46 +08:00
_pi.currentDisplay = int.parse(evt['current_display']);
try {
CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
} catch (e) {
//
}
if (isPeerAndroid) {
_touchMode = true;
if (parent.target != null &&
parent.target!.connType == ConnType.defaultConn &&
parent.target!.ffiModel.permissions['keyboard'] != false) {
2022-09-29 20:50:01 +08:00
Timer(
const Duration(milliseconds: 100),
() => parent.target!.dialogManager
.showMobileActionsOverlay(ffi: parent.target!));
}
} else {
_touchMode =
await bind.sessionGetOption(id: peerId, arg: 'touch-mode') != '';
}
if (parent.target != null &&
parent.target!.connType == ConnType.fileTransfer) {
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.isNotEmpty) {
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-09-13 21:52:22 +08:00
String id = '';
2022-05-31 22:09:36 +08:00
WeakReference<FFI> parent;
ImageModel(this.parent);
onRgba(Uint8List rgba) {
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);
2022-05-31 22:09:36 +08:00
} catch (e) {
debugPrint('update image: $e');
2022-05-19 23:45:44 +08:00
}
});
}
update(ui.Image? image) async {
2020-11-24 22:03:04 +08:00
if (_image == null && image != null) {
if (isWebDesktop || isDesktop) {
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;
final canvasWidth = size.width;
final canvasHeight = size.height;
final xscale = canvasWidth / image.width;
final yscale = canvasHeight / image.height;
parent.target?.canvasModel.scale = min(xscale, yscale);
}
if (parent.target != null) {
await initializeCursorAndCanvas(parent.target!);
}
if (parent.target?.ffiModel.isPeerAndroid ?? false) {
2022-09-13 21:52:22 +08:00
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
}
// 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,
}
class ViewStyle {
final String style;
final double width;
final double height;
final int displayWidth;
final int displayHeight;
ViewStyle({
this.style = '',
this.width = 0.0,
this.height = 0.0,
this.displayWidth = 0,
this.displayHeight = 0,
});
static int _double2Int(double v) => (v * 100).round().toInt();
@override
bool operator ==(Object other) =>
other is ViewStyle &&
other.runtimeType == runtimeType &&
_innerEqual(other);
bool _innerEqual(ViewStyle other) {
return style == other.style &&
ViewStyle._double2Int(other.width) == ViewStyle._double2Int(width) &&
ViewStyle._double2Int(other.height) == ViewStyle._double2Int(height) &&
other.displayWidth == displayWidth &&
other.displayHeight == displayHeight;
}
@override
int get hashCode => Object.hash(
style,
ViewStyle._double2Int(width),
ViewStyle._double2Int(height),
displayWidth,
displayHeight,
).hashCode;
double get scale {
double s = 1.0;
if (style == 'adaptive') {
final s1 = width / displayWidth;
final s2 = height / displayHeight;
s = s1 < s2 ? s1 : s2;
}
return s;
}
}
2020-11-23 23:18:42 +08:00
class CanvasModel with ChangeNotifier {
// 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;
// the window border's width
double windowBorderWidth = 0.0;
2022-09-13 21:52:22 +08:00
// remote id
String id = '';
// scroll offset x percent
double _scrollX = 0.0;
// scroll offset y percent
double _scrollY = 0.0;
ScrollStyle _scrollStyle = ScrollStyle.scrollauto;
ViewStyle _lastViewStyle = ViewStyle();
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;
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 sizeWidth = size.width;
final sizeHeight = size.height;
final displayWidth = getDisplayWidth();
final displayHeight = getDisplayHeight();
final viewStyle = ViewStyle(
style: style,
width: sizeWidth,
height: sizeHeight,
displayWidth: displayWidth,
displayHeight: displayHeight,
);
if (_lastViewStyle == viewStyle) {
return;
2022-02-03 00:53:59 +08:00
}
_lastViewStyle = viewStyle;
_scale = viewStyle.scale;
_x = (sizeWidth - displayWidth * _scale) / 2;
_y = (sizeHeight - displayHeight * _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();
}
update(double x, double y, double scale) {
_x = x;
_y = y;
_scale = scale;
notifyListeners();
}
int getDisplayWidth() {
final defaultWidth = (isDesktop || isWebDesktop)
? kDesktopDefaultDisplayWidth
: kMobileDefaultDisplayWidth;
return parent.target?.ffiModel.display.width ?? defaultWidth;
}
int getDisplayHeight() {
final defaultHeight = (isDesktop || isWebDesktop)
? kDesktopDefaultDisplayHeight
: kMobileDefaultDisplayHeight;
return parent.target?.ffiModel.display.height ?? defaultHeight;
}
Size get size {
final size = MediaQueryData.fromWindow(ui.window).size;
return Size(size.width - windowBorderWidth * 2,
size.height - tabBarHeight - windowBorderWidth * 2);
}
moveDesktopMouse(double x, double y) {
// On mobile platforms, move the canvas with the cursor.
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();
}
// If keyboard is not permitted, do not move cursor when mouse is moving.
if (parent.target != null && parent.target!.ffiModel.keyboard()) {
// Draw cursor if is not desktop.
if (!isDesktop) {
parent.target!.cursorModel.moveLocal(x, y);
} else {
try {
RemoteCursorMovedState.find(id).value = false;
} catch (e) {
//
}
}
}
2022-02-06 16:29:56 +08:00
}
set scale(v) {
_scale = v;
notifyListeners();
}
panX(double dx) {
2020-11-24 22:03:04 +08:00
_x += dx;
notifyListeners();
2020-11-23 23:18:42 +08:00
}
resetOffset() {
2022-05-23 16:02:37 +08:00
if (isWebDesktop) {
2022-02-03 00:53:59 +08:00
updateViewStyle();
} else {
_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();
}
panY(double dy) {
2020-11-23 23:18:42 +08:00
_y += dy;
notifyListeners();
}
updateScale(double v) {
if (parent.target?.imageModel.image == null) return;
final offset = parent.target?.cursorModel.offset ?? const 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();
}
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
}
}
// 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,
});
int _doubleToInt(double v) => (v * 10e6).round().toInt();
String key(double scale) =>
'${peerId}_${id}_${_doubleToInt(width * scale)}_${_doubleToInt(height * scale)}';
}
2020-11-19 00:32:46 +08:00
class CursorModel with ChangeNotifier {
2022-02-17 15:22:14 +08:00
ui.Image? _image;
final _images = <int, Tuple3<ui.Image, double, double>>{};
CursorData? _cacheLinux;
final _cacheMapLinux = <int, CursorData>{};
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-13 21:52:22 +08:00
String id = '';
WeakReference<FFI> parent;
2020-11-19 00:32:46 +08:00
2022-02-17 15:22:14 +08:00
ui.Image? get image => _image;
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
CursorModel(this.parent);
Set<String> get cachedKeysLinux => _cacheKeysLinux;
addKeyLinux(String key) => _cacheKeysLinux.add(key);
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
}
move(double x, double y) {
moveLocal(x, y);
2022-09-27 20:35:02 +08:00
parent.target?.inputModel.moveMouse(_x, _y);
}
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();
}
reset() {
2021-08-21 17:18:14 +08:00
_x = _displayOriginX;
_y = _displayOriginY;
2022-09-27 20:35:02 +08:00
parent.target?.inputModel.moveMouse(_x, _y);
parent.target?.canvasModel.clear(true);
2021-08-21 17:18:14 +08:00
notifyListeners();
}
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;
2022-09-27 20:35:02 +08:00
parent.target?.inputModel.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
}
2022-09-27 20:35:02 +08:00
parent.target?.inputModel.moveMouse(_x, _y);
2020-11-24 22:03:04 +08:00
notifyListeners();
}
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());
var pid = parent.target?.id;
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');
}
}
_updateCacheLinux(ui.Image image, int id, int w, int h) async {
2022-10-10 10:53:10 +08:00
ByteData? data;
if (Platform.isWindows) {
data = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
} else {
data = await image.toByteData(format: ui.ImageByteFormat.png);
}
_cacheLinux = CursorData(
peerId: this.id,
data: data?.buffer.asUint8List(),
id: id,
hotx: _hotx,
hoty: _hoty,
width: w,
height: h,
);
_cacheMapLinux[id] = _cacheLinux!;
}
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) {
_image = tmp.item1;
_hotx = tmp.item2;
_hoty = tmp.item3;
2020-11-19 00:32:46 +08:00
notifyListeners();
}
}
/// Update the cursor position.
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']);
try {
RemoteCursorMovedState.find(id).value = true;
} catch (e) {
//
}
2020-11-19 00:32:46 +08:00
notifyListeners();
}
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-09-27 20:35:02 +08:00
parent.target?.inputModel.moveMouse(x, y);
parent.target?.canvasModel.resetOffset();
2020-11-19 00:32:46 +08:00
notifyListeners();
}
updateDisplayOriginWithCursor(
2022-05-31 14:44:06 +08:00
double x, double y, double xCursor, double yCursor) {
_displayOriginX = x;
_displayOriginY = y;
_x = xCursor;
_y = yCursor;
2022-09-27 20:35:02 +08:00
parent.target?.inputModel.moveMouse(x, y);
notifyListeners();
}
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();
_clearCacheLinux();
_cacheLinux = null;
_cacheMapLinux.clear();
}
_clearCacheLinux() {
final cachedKeys = {...cachedKeysLinux};
for (var key in cachedKeys) {
customCursorController.freeCache(key);
}
2020-11-19 00:32:46 +08:00
}
Uint8List? cachedForbidmemoryCursorData;
void updateForbiddenCursorBuffer() {
cachedForbidmemoryCursorData ??= base64Decode(
'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAkZQTFRFAAAA2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4GWAwCAAAAAAAA2B4GAAAAMTExAAAAAAAA2B4G2B4G2B4GAAAAmZmZkZGRAQEBAAAA2B4G2B4G2B4G////oKCgAwMDag8D2B4G2B4G2B4Gra2tBgYGbg8D2B4G2B4Gubm5CQkJTwsCVgwC2B4GxcXFDg4OAAAAAAAA2B4G2B4Gz8/PFBQUAAAAAAAA2B4G2B4G2B4G2B4G2B4G2B4G2B4GDgIA2NjYGxsbAAAAAAAA2B4GFwMB4eHhIyMjAAAAAAAA2B4G6OjoLCwsAAAAAAAA2B4G2B4G2B4G2B4G2B4GCQEA4ODgv7+/iYmJY2NjAgICAAAA9PT0Ojo6AAAAAAAAAAAA+/v7SkpKhYWFr6+vAAAAAAAA8/PzOTk5ERER9fX1KCgoAAAAgYGBKioqAAAAAAAApqamlpaWAAAAAAAAAAAAAAAAAAAAAAAALi4u/v7+GRkZAAAAAAAAAAAAAAAAAAAAfn5+AAAAAAAAV1dXkJCQAAAAAAAAAQEBAAAAAAAAAAAA7Hz6BAAAAMJ0Uk5TAAIWEwEynNz6//fVkCAatP2fDUHs6cDD8d0mPfT5fiEskiIR584A0gejr3AZ+P4plfALf5ZiTL85a4ziD6697fzN3UYE4v/4TwrNHuT///tdRKZh///+1U/ZBv///yjb///eAVL//50Cocv//6oFBbPvpGZCbfT//7cIhv///8INM///zBEcWYSZmO7//////1P////ts/////8vBv//////gv//R/z///QQz9sevP///2waXhNO/+fc//8mev/5gAe2r90MAAAByUlEQVR4nGNggANGJmYWBpyAlY2dg5OTi5uHF6s0H78AJxRwCAphyguLgKRExcQlQLSkFLq8tAwnp6ycPNABjAqKQKNElVDllVU4OVVhVquJA81Q10BRoAkUUYbJa4Edoo0sr6PLqaePLG/AyWlohKTAmJPTBFnelAFoixmSAnNOTgsUeQZLTk4rJAXWnJw2EHlbiDyDPCenHZICe04HFrh+RydnBgYWPU5uJAWinJwucPNd3dw9GDw5Ob2QFHBzcnrD7ffx9fMPCOTkDEINhmC4+3x8Q0LDwlEDIoKTMzIKKg9SEBIdE8sZh6SAJZ6Tkx0qD1YQkpCYlIwclCng0AXLQxSEpKalZyCryATKZwkhKQjJzsnNQ1KQXwBUUVhUXBJYWgZREFJeUVmFpMKlWg+anmqgCkJq6+obkG1pLEBTENLU3NKKrIKhrb2js8u4G6Kgpze0r3/CRAZMAHbkpJDJU6ZMmTqtFbuC6TNmhsyaMnsOFlmwgrnzpsxfELJwEXZ5Bp/FS3yWLlsesmLlKuwKVk9Ys5Zh3foN0zduwq5g85atDAzbpqSGbN9RhV0FGOzctWH3lD14FOzdt3H/gQw8Cg4u2gQPAwBYDXXdIH+wqAAAAABJRU5ErkJggg==');
}
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 {
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();
} catch (e) {
//
}
2022-08-04 17:24:02 +08:00
}
}
class RecordingModel with ChangeNotifier {
WeakReference<FFI> parent;
RecordingModel(this.parent);
bool _start = false;
get start => _start;
onSwitchDisplay() {
if (!isDesktop || !_start) return;
var id = parent.target?.id;
int? width = parent.target?.canvasModel.getDisplayWidth();
int? height = parent.target?.canvasModel.getDisplayWidth();
if (id == null || width == null || height == null) return;
bind.sessionRecordScreen(id: id, start: true, width: width, height: height);
}
toggle() {
if (!isDesktop) return;
var id = parent.target?.id;
if (id == null) return;
_start = !_start;
notifyListeners();
if (_start) {
bind.sessionRefresh(id: id);
} else {
bind.sessionRecordScreen(id: id, start: false, width: 0, height: 0);
}
}
onClose() {
if (!isDesktop) return;
var id = parent.target?.id;
if (id == null) return;
_start = false;
bind.sessionRecordScreen(id: id, start: false, width: 0, height: 0);
}
}
enum ConnType { defaultConn, fileTransfer, portForward, rdp }
2022-09-13 21:52:22 +08:00
/// Flutter state manager and data communication with the Rust core.
2020-11-19 00:32:46 +08:00
class FFI {
var id = '';
var version = '';
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
late final RecordingModel recordingModel; // recording
2022-09-27 20:35:02 +08:00
late final InputModel inputModel; // session
FFI() {
imageModel = ImageModel(WeakReference(this));
ffiModel = FfiModel(WeakReference(this));
cursorModel = CursorModel(WeakReference(this));
canvasModel = CanvasModel(WeakReference(this));
2022-09-13 21:52:22 +08:00
serverModel = ServerModel(WeakReference(this));
chatModel = ChatModel(WeakReference(this));
fileModel = FileModel(WeakReference(this));
abModel = AbModel(WeakReference(this));
userModel = UserModel(WeakReference(this));
qualityMonitorModel = QualityMonitorModel(WeakReference(this));
recordingModel = RecordingModel(WeakReference(this));
2022-09-27 20:35:02 +08:00
inputModel = InputModel(WeakReference(this));
}
2020-11-19 00:32:46 +08:00
2022-09-27 20:35:02 +08:00
/// Start with the given [id]. Only transfer file if [isFileTransfer], only port forward if [isPortForward].
void start(String id,
{bool isFileTransfer = false, bool isPortForward = false}) {
assert(!(isFileTransfer && isPortForward), 'more than one connect type');
if (isFileTransfer) {
connType = ConnType.fileTransfer;
id = 'ft_$id';
} else if (isPortForward) {
connType = ConnType.portForward;
id = 'pf_$id';
} else {
chatModel.resetClientMode();
canvasModel.id = id;
2022-09-13 21:52:22 +08:00
imageModel.id = id;
cursorModel.id = id;
}
// ignore: unused_local_variable
final addRes = bind.sessionAddSync(
id: id, isFileTransfer: isFileTransfer, isPortForward: isPortForward);
final stream = bind.sessionStart(id: id);
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);
await cb(event);
} catch (e) {
debugPrint('json.decode fail1(): $e, ${message.field0}');
2022-05-31 17:36:36 +08:00
}
} else if (message is Rgba) {
imageModel.onRgba(message.field0);
2022-05-31 17:36:36 +08:00
}
}
}();
// every instance will bind a stream
this.id = id;
if (isFileTransfer) {
fileModel.initFileFetcher();
}
2020-11-19 00:32:46 +08:00
}
2022-05-28 03:56:42 +08:00
/// Login with [password], choose if the client should [remember] it.
2022-09-27 20:35:02 +08:00
void login(String id, String password, bool remember) {
bind.sessionLogin(id: id, password: password, remember: remember);
2020-11-19 00:32:46 +08:00
}
2022-05-28 03:56:42 +08:00
/// Close the remote session.
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);
id = '';
imageModel.update(null);
2020-11-25 16:28:46 +08:00
cursorModel.clear();
ffiModel.clear();
canvasModel.clear();
2022-09-27 20:35:02 +08:00
inputModel.resetModifiers();
debugPrint('model $id closed');
2020-11-19 00:32:46 +08:00
}
2022-09-27 20:35:02 +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
}
}
class Display {
double x = 0;
double y = 0;
int width = 0;
int height = 0;
Display() {
width = (isDesktop || isWebDesktop)
? kDesktopDefaultDisplayWidth
: kMobileDefaultDisplayWidth;
height = (isDesktop || isWebDesktop)
? kDesktopDefaultDisplayHeight
: kMobileDefaultDisplayHeight;
}
2020-11-19 00:32:46 +08:00
}
class PeerInfo {
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
}
2022-09-27 20:35:02 +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 = <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;
}
2022-09-27 20:35:02 +08:00
void removePreference(String id) async {
2021-08-06 21:18:06 +08:00
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove('peer$id');
2020-12-21 19:05:31 +08:00
}
2022-09-27 20:35:02 +08:00
Future<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);
}