remove subfolder flutter_hbb

This commit is contained in:
rustdesk
2021-08-30 21:12:31 +08:00
parent 83736732fc
commit 903a4ea27f
90 changed files with 1 additions and 0 deletions

204
lib/common.dart Normal file
View File

@@ -0,0 +1,204 @@
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:tuple/tuple.dart';
import 'dart:io';
typedef F = String Function(String);
class Translator {
static F call;
}
class MyTheme {
MyTheme._();
static const Color grayBg = Color(0xFFEEEEEE);
static const Color white = Color(0xFFFFFFFF);
static const Color accent = Color(0xFF0071FF);
static const Color accent50 = Color(0x770071FF);
static const Color accent80 = Color(0xAA0071FF);
static const Color canvasColor = Color(0xFF212121);
static const Color border = Color(0xFFCCCCCC);
}
final ButtonStyle flatButtonStyle = TextButton.styleFrom(
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16.0),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2.0)),
),
);
void Function() loadingCancelCallback = null;
void showLoading(String text, BuildContext context) {
if (_hasDialog && context != null) {
Navigator.pop(context);
}
dismissLoading();
if (Platform.isAndroid) {
EasyLoading.show(status: text, maskType: EasyLoadingMaskType.black);
return;
}
EasyLoading.showWidget(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(child: CircularProgressIndicator()),
SizedBox(height: 20),
Center(
child:
Text(Translator.call(text), style: TextStyle(fontSize: 15))),
SizedBox(height: 20),
Center(
child: TextButton(
style: flatButtonStyle,
onPressed: () {
// with out loadingCancelCallback, we can see unexpected input password
// dialog shown in home, no clue why, so use this as workaround
// why no such issue on android?
if (loadingCancelCallback != null) loadingCancelCallback();
Navigator.pop(context);
},
child: Text(Translator.call('Cancel'),
style: TextStyle(color: MyTheme.accent))))
],
),
maskType: EasyLoadingMaskType.black);
}
void dismissLoading() {
EasyLoading.dismiss();
}
bool _hasDialog = false;
typedef BuildAlertDailog = Tuple3<Widget, Widget, List<Widget>> Function(
void Function(void Function()));
Future<T> showAlertDialog<T>(BuildContext context, BuildAlertDailog build,
[WillPopCallback onWillPop,
bool barrierDismissible = false,
double contentPadding = 20]) async {
dismissLoading();
if (_hasDialog) {
Navigator.pop(context);
}
_hasDialog = true;
var dialog = StatefulBuilder(builder: (context, setState) {
var widgets = build(setState);
if (onWillPop == null) onWillPop = () async => false;
return WillPopScope(
onWillPop: onWillPop,
child: AlertDialog(
title: widgets.item1,
contentPadding: EdgeInsets.all(contentPadding),
content: widgets.item2,
actions: widgets.item3,
));
});
var res = await showDialog<T>(
context: context,
barrierDismissible: barrierDismissible,
builder: (context) => dialog);
_hasDialog = false;
return res;
}
void msgbox(String type, String title, String text, BuildContext context,
[bool hasCancel]) {
var wrap = (String text, void Function() onPressed) => ButtonTheme(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
materialTapTargetSize: MaterialTapTargetSize
.shrinkWrap, //limits the touch area to the button area
minWidth: 0, //wraps child's width
height: 0,
child: TextButton(
style: flatButtonStyle,
onPressed: onPressed,
child: Text(Translator.call(text),
style: TextStyle(color: MyTheme.accent))));
dismissLoading();
if (_hasDialog) {
Navigator.pop(context);
}
final buttons = [
Expanded(child: Container()),
wrap(Translator.call('OK'), () {
dismissLoading();
Navigator.pop(context);
})
];
if (hasCancel == null) {
hasCancel = type != 'error';
}
if (hasCancel) {
buttons.insert(
1,
wrap(Translator.call('Cancel'), () {
dismissLoading();
}));
}
EasyLoading.showWidget(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(Translator.call(title), style: TextStyle(fontSize: 21)),
SizedBox(height: 20),
Text(Translator.call(text), style: TextStyle(fontSize: 15)),
SizedBox(height: 20),
Row(
children: buttons,
)
],
),
maskType: EasyLoadingMaskType.black);
}
class PasswordWidget extends StatefulWidget {
PasswordWidget({Key key, this.controller}) : super(key: key);
final TextEditingController controller;
@override
_PasswordWidgetState createState() => _PasswordWidgetState();
}
class _PasswordWidgetState extends State<PasswordWidget> {
bool _passwordVisible = false;
@override
Widget build(BuildContext context) {
return TextField(
autofocus: true,
controller: widget.controller,
obscureText: !_passwordVisible, //This will obscure text dynamically
keyboardType: TextInputType.visiblePassword,
decoration: InputDecoration(
labelText: Translator.call('Password'),
hintText: Translator.call('Enter your password'),
// Here is key idea
suffixIcon: IconButton(
icon: Icon(
// Based on passwordVisible state choose the icon
_passwordVisible ? Icons.visibility : Icons.visibility_off,
color: Theme.of(context).primaryColorDark,
),
onPressed: () {
// Update the state i.e. toogle the state of passwordVisible variable
setState(() {
_passwordVisible = !_passwordVisible;
});
},
),
),
);
}
}
Color str2color(String str, [alpha = 0xFF]) {
var hash = 160 << 16 + 114 << 8 + 91;
for (var i = 0; i < str.length; i += 1) {
hash = str.codeUnitAt(i) + ((hash << 5) - hash);
}
hash = hash % 16777216;
return Color((hash & 0xFF7FFF) | (alpha << 24));
}

377
lib/home_page.dart Normal file
View File

@@ -0,0 +1,377 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
import 'package:package_info/package_info.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:async';
import 'common.dart';
import 'model.dart';
import 'remote_page.dart';
import 'dart:io';
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _idController = TextEditingController();
var _updateUrl = '';
@override
void initState() {
super.initState();
if (Platform.isAndroid) {
Timer(Duration(seconds: 5), () {
_updateUrl = FFI.getByName('software_update_url');
if (_updateUrl.isNotEmpty) setState(() {});
});
}
}
@override
Widget build(BuildContext context) {
Provider.of<FfiModel>(context);
if (_idController.text.isEmpty) _idController.text = FFI.getId();
// This method is rerun every time setState is called
return Scaffold(
backgroundColor: MyTheme.grayBg,
appBar: AppBar(
centerTitle: true,
actions: [
IconButton(
icon: Icon(Icons.more_vert),
onPressed: () {
() async {
var value = await showMenu(
context: context,
position: RelativeRect.fromLTRB(3000, 70, 3000, 70),
items: [
PopupMenuItem<String>(
child: Text(translate('ID Server')),
value: 'server'),
PopupMenuItem<String>(
child: Text(translate('About') + ' RustDesk'),
value: 'about'),
],
elevation: 8,
);
if (value == 'server') {
showServer(context);
} else if (value == 'about') {
showAbout(context);
}
}();
})
],
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_updateUrl.isEmpty
? SizedBox(height: 0)
: InkWell(
onTap: () async {
final url = _updateUrl + '.apk';
if (await canLaunch(url)) {
await launch(url);
}
},
child: Container(
alignment: AlignmentDirectional.center,
width: double.infinity,
color: Colors.pinkAccent,
padding: EdgeInsets.symmetric(vertical: 12),
child: Text(translate('Download new version'),
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold)))),
getSearchBarUI(),
getPeers(),
]),
));
}
void onConnect() {
var id = _idController.text.trim();
connect(id);
}
void connect(String id) {
if (id == '') return;
id = id.replaceAll(' ', '');
() async {
await Navigator.push<dynamic>(
context,
MaterialPageRoute<dynamic>(
builder: (BuildContext context) => RemotePage(id: id),
),
);
setState(() {});
}();
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
}
Widget getSearchBarUI() {
if (!FFI.ffiModel.initialized) {
return Container();
}
return Padding(
padding: const EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 0.0),
child: Container(
height: 84,
child: Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8),
child: Container(
decoration: BoxDecoration(
color: MyTheme.white,
borderRadius: const BorderRadius.only(
bottomRight: Radius.circular(13.0),
bottomLeft: Radius.circular(13.0),
topLeft: Radius.circular(13.0),
topRight: Radius.circular(13.0),
),
),
child: Row(
children: <Widget>[
Expanded(
child: Container(
padding: const EdgeInsets.only(left: 16, right: 16),
child: TextField(
autocorrect: false,
enableSuggestions: false,
keyboardType: TextInputType.visiblePassword,
// keyboardType: TextInputType.number,
style: TextStyle(
fontFamily: 'WorkSans',
fontWeight: FontWeight.bold,
fontSize: 30,
color: Color(0xFF00B6F0),
),
decoration: InputDecoration(
labelText: translate('Remote ID'),
// hintText: 'Enter your remote ID',
border: InputBorder.none,
helperStyle: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: Color(0xFFB9BABC),
),
labelStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
letterSpacing: 0.2,
color: Color(0xFFB9BABC),
),
),
autofocus: _idController.text.isEmpty,
controller: _idController,
),
),
),
SizedBox(
width: 60,
height: 60,
child: IconButton(
icon: Icon(Icons.arrow_forward,
color: Color(0xFFB9BABC), size: 45),
onPressed: onConnect,
autofocus: _idController.text.isNotEmpty,
),
)
],
),
),
),
),
);
}
@override
void dispose() {
_idController.dispose();
super.dispose();
}
Widget getPlatformImage(String platform) {
platform = platform.toLowerCase();
if (platform == 'mac os')
platform = 'mac';
else if (platform != 'linux') platform = 'win';
return Image.asset('assets/$platform.png', width: 24, height: 24);
}
Widget getPeers() {
if (!FFI.ffiModel.initialized) {
return Container();
}
final cards = <Widget>[];
var peers = FFI.peers();
peers.forEach((p) {
cards.add(Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: Card(
child: GestureDetector(
onTap: () => connect('${p.id}'),
onLongPressStart: (details) {
var x = details.globalPosition.dx;
var y = details.globalPosition.dy;
() async {
var value = await showMenu(
context: context,
position: RelativeRect.fromLTRB(x, y, x, y),
items: [
PopupMenuItem<String>(
child: Text(translate('Remove')),
value: 'remove'),
],
elevation: 8,
);
if (value == 'remove') {
setState(() => FFI.setByName('remove', '${p.id}'));
() async {
removePreference(p.id);
}();
}
}();
},
child: ListTile(
subtitle: Text('${p.username}@${p.hostname}'),
title: Text('${p.id}'),
leading: Container(
padding: const EdgeInsets.all(6),
child: getPlatformImage('${p.platform}'),
color: str2color('${p.id}${p.platform}', 0x7f)),
)))));
});
return Wrap(children: cards);
}
}
void showServer(BuildContext context) {
final formKey = GlobalKey<FormState>();
final id0 = FFI.getByName('option', 'custom-rendezvous-server');
final relay0 = FFI.getByName('option', 'relay-server');
final key0 = FFI.getByName('option', 'key');
var id = '';
var relay = '';
var key = '';
showAlertDialog(
context,
(setState) => Tuple3(
Text(translate('ID Server')),
Form(
key: formKey,
child:
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
TextFormField(
initialValue: id0,
decoration: InputDecoration(
labelText: translate('ID Server'),
),
validator: validate,
onSaved: (String value) {
id = value.trim();
},
),
/*
TextFormField(
initialValue: relay0,
decoration: InputDecoration(
labelText: translate('Relay Server'),
),
validator: validate,
onSaved: (String value) {
relay = value.trim();
},
),
*/
TextFormField(
initialValue: key0,
decoration: InputDecoration(
labelText: 'Key',
),
validator: null,
onSaved: (String value) {
key = value.trim();
},
),
])),
[
TextButton(
style: flatButtonStyle,
onPressed: () {
Navigator.pop(context);
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: () {
if (formKey.currentState.validate()) {
formKey.currentState.save();
if (id != id0)
FFI.setByName('option',
'{"name": "custom-rendezvous-server", "value": "${id}"}');
if (relay != relay0)
FFI.setByName('option',
'{"name": "relay-server", "value": "${relay}"}');
if (key != key0)
FFI.setByName(
'option', '{"name": "key", "value": "${key}"}');
Navigator.pop(context);
}
},
child: Text(translate('OK')),
),
],
));
}
Future<Null> showAbout(BuildContext context) async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
showAlertDialog(
context,
(setState) => Tuple3(
null,
Wrap(direction: Axis.vertical, spacing: 12, children: [
Text('Version: ${packageInfo.version}'),
InkWell(
onTap: () async {
const url = 'https://rustdesk.com/';
if (await canLaunch(url)) {
await launch(url);
}
},
child: Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: Text('Support',
style: TextStyle(
decoration: TextDecoration.underline,
)),
)),
]),
null),
() async => true,
true);
}
String validate(value) {
value = value.trim();
if (value.isEmpty) {
return null;
}
final res = FFI.getByName('test_if_valid_server', value);
return res.isEmpty ? null : res;
}

40
lib/main.dart Normal file
View File

@@ -0,0 +1,40 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
import 'package:firebase_core/firebase_core.dart';
import 'model.dart';
import 'home_page.dart';
Future<Null> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(App());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
final analytics = FirebaseAnalytics();
return ChangeNotifierProvider.value(
value: FFI.ffiModel,
child: ChangeNotifierProvider.value(
value: FFI.imageModel,
child: ChangeNotifierProvider.value(
value: FFI.cursorModel,
child: ChangeNotifierProvider.value(
value: FFI.canvasModel,
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'RustDesk',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(title: 'RustDesk'),
navigatorObservers: [
FirebaseAnalyticsObserver(analytics: analytics),
],
)))));
}
}

805
lib/model.dart Normal file
View File

@@ -0,0 +1,805 @@
import 'package:ffi/ffi.dart';
import 'package:flutter/services.dart';
import 'package:flutter/gestures.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:device_info/device_info.dart';
import 'dart:io';
import 'dart:math';
import 'dart:ffi';
import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:tuple/tuple.dart';
import 'dart:async';
import 'common.dart';
class RgbaFrame extends Struct {
@Uint32()
int len;
Pointer<Uint8> data;
}
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
typedef F4 = void Function(Pointer<RgbaFrame>);
typedef F5 = Pointer<RgbaFrame> Function();
class FfiModel with ChangeNotifier {
PeerInfo _pi;
Display _display;
var _decoding = false;
bool _waitForImage;
bool _initialized = false;
final _permissions = Map<String, bool>();
bool _secure;
bool _direct;
get permissions => _permissions;
get initialized => _initialized;
get display => _display;
get secure => _secure;
get direct => _direct;
get pi => _pi;
FfiModel() {
Translator.call = translate;
clear();
() async {
await FFI.init();
_initialized = true;
print("FFI initialized");
notifyListeners();
}();
}
void updatePermission(Map<String, dynamic> evt) {
evt.forEach((k, v) {
if (k == 'name') return;
_permissions[k] = v == 'true';
});
print('$_permissions');
}
bool keyboard() => _permissions['keyboard'] != false;
void clear() {
_pi = PeerInfo();
_display = Display();
_waitForImage = false;
_secure = null;
_direct = null;
clearPermissions();
}
void setConnectionType(bool secure, bool direct) {
_secure = secure;
_direct = direct;
}
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);
}
void clearPermissions() {
_permissions.clear();
}
void update(
String id,
BuildContext context,
void Function(
Map<String, dynamic> evt,
String id,
)
handleMsgbox) {
var pos;
for (;;) {
var evt = FFI.popEvent();
if (evt == null) break;
var name = evt['name'];
if (name == 'msgbox') {
handleMsgbox(evt, id);
} else if (name == 'peer_info') {
handlePeerInfo(evt, context);
} else if (name == 'connection_ready') {
FFI.ffiModel.setConnectionType(
evt['secure'] == 'true', evt['direct'] == 'true');
} else if (name == 'switch_display') {
handleSwitchDisplay(evt);
} else if (name == 'cursor_data') {
FFI.cursorModel.updateCursorData(evt);
} else if (name == 'cursor_id') {
FFI.cursorModel.updateCursorId(evt);
} else if (name == 'cursor_position') {
pos = evt;
} else if (name == 'clipboard') {
Clipboard.setData(ClipboardData(text: evt['content']));
} else if (name == 'permission') {
FFI.ffiModel.updatePermission(evt);
}
}
if (pos != null) FFI.cursorModel.updateCursorPosition(pos);
if (!_decoding) {
var rgba = FFI.getRgba();
if (rgba != null) {
if (_waitForImage) {
_waitForImage = false;
dismissLoading();
}
_decoding = true;
final pid = FFI.id;
ui.decodeImageFromPixels(
rgba, _display.width, _display.height, ui.PixelFormat.bgra8888,
(image) {
FFI.clearRgbaFrame();
_decoding = false;
if (FFI.id != pid) return;
try {
// my throw exception, because the listener maybe already dispose
FFI.imageModel.update(image);
} catch (e) {
print('update image: $e');
}
});
}
}
}
void handleSwitchDisplay(Map<String, dynamic> evt) {
var old = _pi.currentDisplay;
_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)
FFI.cursorModel.updateDisplayOrigin(_display.x, _display.y);
notifyListeners();
}
void handlePeerInfo(Map<String, dynamic> evt, BuildContext context) {
dismissLoading();
_pi.version = evt['version'];
_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']);
List<dynamic> displays = json.decode(evt['displays']);
_pi.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];
initializeCursorAndCanvas();
}
if (displays.length > 0) {
showLoading(translate('Connected, waiting for image...'), context);
_waitForImage = true;
}
notifyListeners();
}
}
class ImageModel with ChangeNotifier {
ui.Image _image;
ui.Image get image => _image;
void update(ui.Image image) {
if (_image == null && image != null) {
final size = MediaQueryData.fromWindow(ui.window).size;
final xscale = size.width / image.width;
final yscale = size.height / image.height;
FFI.canvasModel.scale = max(xscale, yscale);
}
_image = image;
if (image != null) notifyListeners();
}
double get maxScale {
if (_image == null) return 1.0;
final size = MediaQueryData.fromWindow(ui.window).size;
final xscale = size.width / _image.width;
final yscale = size.height / _image.height;
return max(1.0, max(xscale, yscale));
}
double get minScale {
if (_image == null) return 1.0;
final size = MediaQueryData.fromWindow(ui.window).size;
final xscale = size.width / _image.width;
final yscale = size.height / _image.height;
return min(xscale, yscale);
}
}
class CanvasModel with ChangeNotifier {
double _x;
double _y;
double _scale;
CanvasModel() {
clear();
}
double get x => _x;
double get y => _y;
double get scale => _scale;
void update(double x, double y, double scale) {
_x = x;
_y = y;
_scale = scale;
notifyListeners();
}
set scale(v) {
_scale = v;
notifyListeners();
}
void panX(double dx) {
_x += dx;
notifyListeners();
}
void resetOffset() {
_x = 0;
_y = 0;
notifyListeners();
}
void panY(double dy) {
_y += dy;
notifyListeners();
}
void updateScale(double v) {
if (FFI.imageModel.image == null) return;
final offset = FFI.cursorModel.offset;
var r = FFI.cursorModel.getVisibleRect();
final px0 = (offset.dx - r.left) * _scale;
final py0 = (offset.dy - r.top) * _scale;
_scale *= v;
final maxs = FFI.imageModel.maxScale;
final mins = FFI.imageModel.minScale;
if (_scale > maxs) _scale = maxs;
if (_scale < mins) _scale = mins;
r = FFI.cursorModel.getVisibleRect();
final px1 = (offset.dx - r.left) * _scale;
final py1 = (offset.dy - r.top) * _scale;
_x -= px1 - px0;
_y -= py1 - py0;
notifyListeners();
}
void clear([bool notify=false]) {
_x = 0;
_y = 0;
_scale = 1.0;
if (notify) notifyListeners();
}
}
class CursorModel with ChangeNotifier {
ui.Image _image;
final _images = Map<int, Tuple3<ui.Image, double, double>>();
double _x = -10000;
double _y = -10000;
double _hotx = 0;
double _hoty = 0;
double _displayOriginX = 0;
double _displayOriginY = 0;
ui.Image get image => _image;
double get x => _x - _displayOriginX;
double get y => _y - _displayOriginY;
Offset get offset => Offset(_x, _y);
double get hotx => _hotx;
double get hoty => _hoty;
// remote physical display coordinate
Rect getVisibleRect() {
final size = MediaQueryData.fromWindow(ui.window).size;
final xoffset = FFI.canvasModel.x;
final yoffset = FFI.canvasModel.y;
final scale = FFI.canvasModel.scale;
final x0 = _displayOriginX - xoffset / scale;
final y0 = _displayOriginY - yoffset / scale;
return Rect.fromLTWH(x0, y0, size.width / scale, size.height / scale);
}
double adjustForKeyboard() {
final m = MediaQueryData.fromWindow(ui.window);
var keyboardHeight = m.viewInsets.bottom;
final size = m.size;
if (keyboardHeight < 100) return 0;
final s = FFI.canvasModel.scale;
final thresh = (size.height - keyboardHeight) / 2;
var h = (_y - getVisibleRect().top) * s; // local physical display height
return h - thresh;
}
void touch(double x, double y, bool right) {
final scale = FFI.canvasModel.scale;
final xoffset = FFI.canvasModel.x;
final yoffset = FFI.canvasModel.y;
_x = (x - xoffset) / scale + _displayOriginX;
_y = (y - yoffset) / scale + _displayOriginY;
FFI.moveMouse(_x, _y);
FFI.tap(right);
notifyListeners();
}
void reset() {
_x = _displayOriginX;
_y = _displayOriginY;
FFI.moveMouse(_x, _y);
FFI.canvasModel.clear(true);
notifyListeners();
}
void updatePan(double dx, double dy, bool touchMode, bool drag) {
if (FFI.imageModel.image == null) return;
if (touchMode) {
if (drag) {
final scale = FFI.canvasModel.scale;
_x += dx / scale;
_y += dy / scale;
FFI.moveMouse(_x, _y);
notifyListeners();
} else {
FFI.canvasModel.panX(dx);
FFI.canvasModel.panY(dy);
}
return;
}
final scale = FFI.canvasModel.scale;
dx /= scale;
dy /= scale;
final r = getVisibleRect();
var cx = r.center.dx;
var cy = r.center.dy;
var tryMoveCanvasX = false;
if (dx > 0) {
final maxCanvasCanMove =
_displayOriginX + FFI.imageModel.image.width - r.right;
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) {
final maxCanvasCanMove = _displayOriginX - r.left;
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) {
final mayCanvasCanMove =
_displayOriginY + FFI.imageModel.image.height - r.bottom;
tryMoveCanvasY = _y + dy > cy && mayCanvasCanMove > 0;
if (tryMoveCanvasY) {
dy = min(dy, mayCanvasCanMove);
} else {
final mayCursorCanMove = r.bottom - _y;
dy = min(dy, mayCursorCanMove);
}
} else if (dy < 0) {
final mayCanvasCanMove = _displayOriginY - r.top;
tryMoveCanvasY = _y + dy < cy && mayCanvasCanMove < 0;
if (tryMoveCanvasY) {
dy = max(dy, mayCanvasCanMove);
} else {
final mayCursorCanMove = r.top - _y;
dy = max(dy, mayCursorCanMove);
}
}
if (dx == 0 && dy == 0) return;
_x += dx;
_y += dy;
if (tryMoveCanvasX && dx != 0) {
FFI.canvasModel.panX(-dx);
}
if (tryMoveCanvasY && dy != 0) {
FFI.canvasModel.panY(-dy);
}
FFI.moveMouse(_x, _y);
notifyListeners();
}
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 = FFI.id;
ui.decodeImageFromPixels(rgba, width, height, ui.PixelFormat.rgba8888,
(image) {
if (FFI.id != pid) return;
_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');
}
});
}
void updateCursorId(Map<String, dynamic> evt) {
final tmp = _images[int.parse(evt['id'])];
if (tmp != null) {
_image = tmp.item1;
_hotx = tmp.item2;
_hoty = tmp.item3;
notifyListeners();
}
}
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;
_x = x + 1;
_y = y + 1;
FFI.moveMouse(x, y);
FFI.canvasModel.resetOffset();
notifyListeners();
}
void updateDisplayOriginWithCursor(
double x, double y, double xCursor, double yCursor) {
_displayOriginX = x;
_displayOriginY = y;
_x = xCursor;
_y = yCursor;
FFI.moveMouse(x, y);
notifyListeners();
}
void clear() {
_x = -10000;
_x = -10000;
_image = null;
_images.clear();
}
}
class FFI {
static String id = "";
static String _dir = '';
static F2 _getByName;
static F3 _setByName;
static F4 _freeRgba;
static F5 _getRgba;
static Pointer<RgbaFrame> _lastRgbaFrame;
static var shift = false;
static var ctrl = false;
static var alt = false;
static var command = false;
static final imageModel = ImageModel();
static final ffiModel = FfiModel();
static final cursorModel = CursorModel();
static final canvasModel = CanvasModel();
static String getId() {
return getByName('remote_id');
}
static void tap(bool right) {
sendMouse('down', right ? 'right' : 'left');
sendMouse('up', right ? 'right' : 'left');
}
static void scroll(double y) {
var y2 = y.round();
if (y2 == 0) return;
setByName('send_mouse',
json.encode(modify({'type': 'wheel', 'y': y2.toString()})));
}
static void reconnect() {
setByName('reconnect');
FFI.ffiModel.clearPermissions();
}
static void resetModifiers() {
shift = ctrl = alt = command = false;
}
static Map<String, String> modify(Map<String, String> evt) {
if (ctrl) evt['ctrl'] = 'true';
if (shift) evt['shift'] = 'true';
if (alt) evt['alt'] = 'true';
if (command) evt['command'] = 'true';
return evt;
}
static void sendMouse(String type, String buttons) {
if (!ffiModel.keyboard()) return;
setByName(
'send_mouse', json.encode(modify({'type': type, 'buttons': buttons})));
}
static void inputKey(String name) {
if (!ffiModel.keyboard()) return;
setByName('input_key', json.encode(modify({'name': name})));
}
static void moveMouse(double x, double y) {
if (!ffiModel.keyboard()) return;
var x2 = x.toInt();
var y2 = y.toInt();
setByName('send_mouse', json.encode(modify({'x': '$x2', 'y': '$y2'})));
}
static List<Peer> peers() {
try {
List<dynamic> peers = json.decode(getByName('peers'));
return peers
.map((s) => s as List<dynamic>)
.map((s) =>
Peer.fromJson(s[0] as String, s[1] as Map<String, dynamic>))
.toList();
} catch (e) {
print('peers(): $e');
}
return [];
}
static void connect(String id) {
setByName('connect', id);
FFI.id = id;
}
static void clearRgbaFrame() {
if (_lastRgbaFrame != null && _lastRgbaFrame != nullptr)
_freeRgba(_lastRgbaFrame);
}
static Uint8List getRgba() {
if (_getRgba == null) return null;
_lastRgbaFrame = _getRgba();
if (_lastRgbaFrame == null || _lastRgbaFrame == nullptr) return null;
final ref = _lastRgbaFrame.ref;
return Uint8List.sublistView(ref.data.asTypedList(ref.len));
}
static Map<String, dynamic> popEvent() {
var s = getByName('event');
if (s == '') return null;
try {
Map<String, dynamic> event = json.decode(s);
return event;
} catch (e) {
print('popEvent(): $e');
}
return null;
}
static void login(String password, bool remember) {
setByName(
'login',
json.encode({
'password': password,
'remember': remember ? 'true' : 'false',
}));
}
static void close() {
savePreference(id, cursorModel.x, cursorModel.y, canvasModel.x,
canvasModel.y, canvasModel.scale, ffiModel.pi.currentDisplay);
id = "";
setByName('close', '');
imageModel.update(null);
cursorModel.clear();
ffiModel.clear();
canvasModel.clear();
resetModifiers();
}
static void setByName(String name, [String value = '']) {
if (_setByName == null) return;
var a = name.toNativeUtf8();
var b = value.toNativeUtf8();
_setByName(a, b);
calloc.free(a);
calloc.free(b);
}
static String getByName(String name, [String arg = '']) {
if (_getByName == null) return '';
var a = name.toNativeUtf8();
var b = arg.toNativeUtf8();
var p = _getByName(a, b);
assert(p != nullptr && p != null);
var res = p.toDartString();
calloc.free(p);
calloc.free(a);
calloc.free(b);
return res;
}
static Future<Null> init() async {
final dylib = Platform.isAndroid
? DynamicLibrary.open('librustdesk.so')
: DynamicLibrary.process();
print('initializing FFI');
try {
_getByName = dylib.lookupFunction<F2, F2>('get_by_name');
_setByName =
dylib.lookupFunction<Void Function(Pointer<Utf8>, Pointer<Utf8>), F3>(
'set_by_name');
_freeRgba = dylib
.lookupFunction<Void Function(Pointer<RgbaFrame>), F4>('free_rgba');
_getRgba = dylib.lookupFunction<F5, F5>('get_rgba');
_dir = (await getApplicationDocumentsDirectory()).path;
String id = 'NA';
String name = 'Flutter';
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
name = '${androidInfo.brand}-${androidInfo.model}';
id = androidInfo.id.hashCode.toString();
} else {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
name = iosInfo.utsname.machine;
id = iosInfo.identifierForVendor.hashCode.toString();
}
setByName('info1', id);
setByName('info2', name);
setByName('init', _dir);
} catch (e) {
print(e);
}
}
}
class Peer {
final String id;
final String username;
final String hostname;
final String platform;
Peer.fromJson(String id, Map<String, dynamic> json)
: id = id,
username = json['username'],
hostname = json['hostname'],
platform = json['platform'];
}
class Display {
double x = 0;
double y = 0;
int width = 0;
int height = 0;
}
class PeerInfo {
String version;
String username;
String hostname;
String platform;
bool sasEnabled;
int currentDisplay;
List<Display> displays;
}
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));
}
Future<Map<String, dynamic>> getPreference(String id) async {
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;
}
void removePreference(String id) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove('peer' + id);
}
void initializeCursorAndCanvas() 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);
}
final locale = Platform.localeName;
final bool isCn =
locale.startsWith('zh') && (locale.endsWith('CN') || locale.endsWith('SG'));
final langs = <String, Map<String, String>>{
'cn': <String, String>{
'Remote ID': '远程ID',
'Paste': '粘贴',
'Are you sure to close the connection?': '是否确认关闭连接?',
'Download new version': '下载新版本',
'Touch mode': '触屏模式',
'Reset canvas': '重置画布',
},
'en': <String, String>{}
};
String translate(String name) {
if (name.startsWith('Failed') && name.contains(':')) {
return name.split(': ').map((x) => translate(x)).join(': ');
}
final tmp = isCn ? langs['cn'] : langs['en'];
final v = tmp[name];
if (v == null) {
var a = 'translate';
var b = '{"locale": "${Platform.localeName}", "text": "${name}"}';
return FFI.getByName(a, b);
} else {
return v;
}
}

967
lib/remote_page.dart Normal file
View File

@@ -0,0 +1,967 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/services.dart';
import 'dart:ui' as ui;
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'dart:async';
import 'package:tuple/tuple.dart';
import 'package:wakelock/wakelock.dart';
import 'common.dart';
import 'model.dart';
import 'dart:io';
final initText = '\1' * 1024;
class RemotePage extends StatefulWidget {
RemotePage({Key key, this.id}) : super(key: key);
final String id;
@override
_RemotePageState createState() => _RemotePageState();
}
class _RemotePageState extends State<RemotePage> {
Timer _interval;
Timer _timer;
bool _showBar = true;
double _bottom = 0;
String _value = '';
double _xOffset = 0;
double _yOffset = 0;
double _yOffset0 = 0;
double _scale = 1;
bool _mouseTools = false;
var _drag = false;
var _right = false;
var _scroll = false;
var _more = true;
var _fn = false;
final FocusNode _focusNode = FocusNode();
var _showKeyboard = false;
var _reconnects = 1;
var _touchMode = false;
@override
void initState() {
super.initState();
FFI.connect(widget.id);
WidgetsBinding.instance.addPostFrameCallback((_) {
SystemChrome.setEnabledSystemUIOverlays([]);
showLoading(translate('Connecting...'), context);
_interval =
Timer.periodic(Duration(milliseconds: 30), (timer) => interval());
});
Wakelock.enable();
loadingCancelCallback = () => _interval.cancel();
_touchMode = FFI.getByName('peer_option', "touch-mode") != '';
}
@override
void dispose() {
_focusNode.dispose();
FFI.close();
loadingCancelCallback = null;
_interval.cancel();
_timer?.cancel();
dismissLoading();
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
Wakelock.disable();
super.dispose();
}
void resetTool() {
_scroll = _drag = _right = false;
FFI.resetModifiers();
}
void interval() {
var v = MediaQuery.of(context).viewInsets.bottom;
if (v != _bottom) {
resetTool();
setState(() {
_bottom = v;
if (v < 100) {
_showKeyboard = false;
SystemChrome.setEnabledSystemUIOverlays([]);
}
});
}
FFI.ffiModel.update(widget.id, context, handleMsgbox);
}
void handleMsgbox(Map<String, dynamic> evt, String id) {
var type = evt['type'];
var title = evt['title'];
var text = evt['text'];
if (type == 're-input-password') {
wrongPasswordDialog(id, context);
} else if (type == 'input-password') {
enterPasswordDialog(id, context);
} else {
var hasRetry = evt['hasRetry'] == 'true';
showMsgBox(type, title, text, hasRetry);
}
}
void showMsgBox(String type, String title, String text, bool hasRetry) {
msgbox(type, title, text, context);
if (hasRetry) {
_timer?.cancel();
_timer = Timer(Duration(seconds: _reconnects), () {
FFI.reconnect();
showLoading(translate('Connecting...'), context);
});
_reconnects *= 2;
} else {
_reconnects = 1;
}
}
void handleInput(String newValue) {
var oldValue = _value;
_value = newValue;
if (Platform.isIOS) {
var i = newValue.length - 1;
for (; i >= 0 && newValue[i] != '\1'; --i) {}
var j = oldValue.length - 1;
for (; j >= 0 && oldValue[j] != '\1'; --j) {}
if (i < j) j = i;
newValue = newValue.substring(j + 1);
oldValue = oldValue.substring(j + 1);
var common = 0;
for (;
common < oldValue.length &&
common < newValue.length &&
newValue[common] == oldValue[common];
++common);
for (i = 0; i < oldValue.length - common; ++i) {
FFI.inputKey('VK_BACK');
}
if (newValue.length > common) {
var s = newValue.substring(common);
if (s.length > 1) {
FFI.setByName('input_string', s);
} else {
inputChar(s);
}
}
return;
}
if (oldValue.length > 0 &&
newValue.length > 0 &&
oldValue[0] == '\1' &&
newValue[0] != '\1') {
// clipboard
oldValue = '';
}
if (newValue.length <= oldValue.length) {
final char = 'VK_BACK';
FFI.inputKey(char);
} else {
final content = newValue.substring(oldValue.length);
if (content.length > 1) {
if (oldValue != '' &&
content.length == 2 &&
(content == '""' ||
content == '()' ||
content == '[]' ||
content == '<>' ||
content == "{}" ||
content == '”“' ||
content == '《》' ||
content == '' ||
content == '【】')) {
// can not only input content[0], because when input ], [ are also auo insert, which cause ] never be input
FFI.setByName('input_string', content);
openKeyboard();
return;
}
FFI.setByName('input_string', content);
} else {
inputChar(content);
}
}
}
void inputChar(String char) {
if (char == '\n') {
char = 'VK_RETURN';
} else if (char == ' ') {
char = 'VK_SPACE';
}
FFI.inputKey(char);
}
void openKeyboard() {
// destroy first, so that our _value trick can work
_value = initText;
resetMouse();
setState(() => _showKeyboard = false);
_timer?.cancel();
_timer = Timer(Duration(milliseconds: 30), () {
// show now, and sleep a while to requestFocus to
// make sure edit ready, so that keyboard wont show/hide/show/hide happen
setState(() => _showKeyboard = true);
_timer?.cancel();
_timer = Timer(Duration(milliseconds: 30), () {
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
SystemChannels.textInput.invokeMethod('TextInput.show');
_focusNode.requestFocus();
});
});
}
void resetMouse() {
_drag = false;
_scroll = false;
_right = false;
_mouseTools = false;
}
@override
Widget build(BuildContext context) {
final pi = Provider.of<FfiModel>(context).pi;
final hideKeyboard = Platform.isIOS && _showKeyboard;
final showActionButton = !_showBar || hideKeyboard;
EasyLoading.instance.loadingStyle = EasyLoadingStyle.light;
return WillPopScope(
onWillPop: () async {
close();
return false;
},
child: Scaffold(
floatingActionButton: !showActionButton
? null
: FloatingActionButton(
mini: !hideKeyboard,
child: Icon(
hideKeyboard ? Icons.expand_more : Icons.expand_less),
backgroundColor: MyTheme.accent50,
onPressed: () {
setState(() {
if (hideKeyboard) {
_showKeyboard = !_showKeyboard;
} else {
_showBar = !_showBar;
}
});
}),
bottomNavigationBar: _showBar && pi.displays != null
? BottomAppBar(
elevation: 10,
color: MyTheme.accent,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(children: [
IconButton(
color: Colors.white,
icon: Icon(Icons.clear),
onPressed: () {
close();
},
),
IconButton(
color: Colors.white,
icon: Icon(Icons.keyboard),
onPressed: openKeyboard),
IconButton(
color: Colors.white,
icon: Icon(Icons.tv),
onPressed: () {
setState(() => _showKeyboard = false);
showOptions(context);
},
),
Container(
color: _mouseTools ? Colors.blue[500] : null,
child: IconButton(
color: Colors.white,
icon: Icon(Icons.mouse),
onPressed: () {
setState(() {
_mouseTools = !_mouseTools;
resetTool();
if (_mouseTools) _drag = true;
});
},
)),
IconButton(
color: Colors.white,
icon: Icon(Icons.more_vert),
onPressed: () {
setState(() => _showKeyboard = false);
showActions(context);
},
),
]),
IconButton(
color: Colors.white,
icon: Icon(Icons.expand_more),
onPressed: () {
setState(() => _showBar = !_showBar);
}),
],
),
)
: null,
body: FlutterEasyLoading(
child: Container(
color: Colors.black,
child: SafeArea(
child: GestureDetector(
onLongPress: () {
if (_drag || _scroll) return;
FFI.tap(true);
},
onTapUp: (details) {
if (_drag || _scroll) return;
if (_touchMode) {
FFI.cursorModel.touch(details.localPosition.dx,
details.localPosition.dy, _right);
} else {
FFI.tap(_right);
}
},
onScaleStart: (details) {
_scale = 1;
_xOffset = details.focalPoint.dx;
_yOffset = _yOffset0 = details.focalPoint.dy;
if (_drag) {
FFI.sendMouse('down', 'left');
}
},
onScaleUpdate: (details) {
var scale = details.scale;
if (scale == 1) {
if (!_scroll) {
var x = details.focalPoint.dx;
var y = details.focalPoint.dy;
var dx = x - _xOffset;
var dy = y - _yOffset;
FFI.cursorModel
.updatePan(dx, dy, _touchMode, _drag);
_xOffset = x;
_yOffset = y;
} else {
_xOffset = details.focalPoint.dx;
_yOffset = details.focalPoint.dy;
}
} else if (!_drag && !_scroll) {
FFI.canvasModel.updateScale(scale / _scale);
_scale = scale;
}
},
onScaleEnd: (details) {
if (_drag) {
FFI.sendMouse('up', 'left');
setState(resetMouse);
} else if (_scroll) {
var dy = (_yOffset - _yOffset0) / 10;
if (dy.abs() > 0.1) {
if (dy > 0 && dy < 1) dy = 1;
if (dy < 0 && dy > -1) dy = -1;
FFI.scroll(dy);
}
}
},
child: Container(
color: MyTheme.canvasColor,
child: Stack(children: [
ImagePaint(),
CursorPaint(),
getHelpTools(),
SizedBox(
width: 0,
height: 0,
child: !_showKeyboard
? Container()
: TextFormField(
textInputAction:
TextInputAction.newline,
autocorrect: false,
enableSuggestions: false,
focusNode: _focusNode,
maxLines: null,
initialValue:
_value, // trick way to make backspace work always
keyboardType: TextInputType.multiline,
onChanged: handleInput,
),
),
]))))),
)),
);
}
void showActions(BuildContext context) {
final size = MediaQuery.of(context).size;
final x = 120.0;
final y = size.height;
final more = <PopupMenuItem<String>>[];
if (FFI.ffiModel.pi.version.isNotEmpty) {
more.add(PopupMenuItem<String>(
child: Text(translate('Refresh')), value: 'refresh'));
}
if (FFI.ffiModel.permissions['keyboard'] != false &&
FFI.ffiModel.permissions['clipboard'] != false) {
more.add(PopupMenuItem<String>(
child: Text(translate('Paste')), value: 'paste'));
}
more.add(PopupMenuItem<String>(
child: Row(
children: ([
Container(width: 100.0, child: Text(translate('OS Password'))),
TextButton(
style: flatButtonStyle,
onPressed: () {
Navigator.pop(context);
showSetOSPassword(context, false);
},
child: Icon(Icons.edit, color: MyTheme.accent),
)
])),
value: 'enter_os_password'));
more.add(PopupMenuItem<String>(
child: Row(
children: ([
Container(width: 100.0, child: Text(translate('Touch mode'))),
Padding(padding: EdgeInsets.symmetric(horizontal: 16.0)),
Icon(
_touchMode
? Icons.check_box_outlined
: Icons.check_box_outline_blank,
color: MyTheme.accent)
])),
value: 'touch_mode'));
more.add(PopupMenuItem<String>(
child: Text(translate('Reset canvas')), value: 'reset_canvas'));
() async {
var value = await showMenu(
context: context,
position: RelativeRect.fromLTRB(x, y, x, y),
items: [
PopupMenuItem<String>(
child: Text(translate('Insert') + ' Ctrl + Alt + Del'),
value: 'cad'),
PopupMenuItem<String>(
child: Text(translate('Insert Lock')), value: 'lock'),
] +
more,
elevation: 8,
);
if (value == 'cad') {
FFI.setByName('ctrl_alt_del');
} else if (value == 'lock') {
FFI.setByName('lock_screen');
} else if (value == 'refresh') {
FFI.setByName('refresh');
} else if (value == 'paste') {
() async {
ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
if (data.text != null) {
FFI.setByName('input_string', '${data.text}');
}
}();
} else if (value == 'enter_os_password') {
var password = FFI.getByName('peer_option', "os-password");
if (password != "") {
FFI.setByName('input_os_password', password);
} else {
showSetOSPassword(context, true);
}
} else if (value == 'touch_mode') {
_touchMode = !_touchMode;
final v = _touchMode ? 'Y' : '';
FFI.setByName('peer_option', '{"name": "touch-mode", "value": "${v}"}');
} else if (value == 'reset_canvas') {
FFI.cursorModel.reset();
}
}();
}
void close() {
msgbox('', 'Close', 'Are you sure to close the connection?', context);
}
Widget getHelpTools() {
final keyboard = _showKeyboard;
if (!_mouseTools && !keyboard) {
return SizedBox();
}
final size = MediaQuery.of(context).size;
var wrap =
(String text, void Function() onPressed, [bool active, IconData icon]) {
return TextButton(
style: TextButton.styleFrom(
minimumSize: Size(0, 0),
padding: EdgeInsets.symmetric(
vertical: 10,
horizontal: 9.75), //adds padding inside the button
tapTargetSize: MaterialTapTargetSize
.shrinkWrap, //limits the touch area to the button area
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
backgroundColor: active == true ? MyTheme.accent80 : null,
),
child: icon != null
? Icon(icon, size: 17, color: Colors.white)
: Text(translate(text),
style: TextStyle(color: Colors.white, fontSize: 11)),
onPressed: onPressed);
};
final mouse = <Widget>[
wrap('Drag', () {
setState(() {
_drag = !_drag;
if (_drag) {
_scroll = false;
_right = false;
}
});
}, _drag),
wrap('Scroll', () {
setState(() {
_scroll = !_scroll;
if (_scroll) {
_drag = false;
_right = false;
}
});
}, _scroll),
wrap('Right', () {
setState(() {
_right = !_right;
if (_right) {
_scroll = false;
_drag = false;
}
});
}, _right)
];
final pi = FFI.ffiModel.pi;
final isMac = pi.platform == "Mac OS";
final modifiers = <Widget>[
wrap('Ctrl ', () {
setState(() => FFI.ctrl = !FFI.ctrl);
}, FFI.ctrl),
wrap(' Alt ', () {
setState(() => FFI.alt = !FFI.alt);
}, FFI.alt),
wrap('Shift', () {
setState(() => FFI.shift = !FFI.shift);
}, FFI.shift),
wrap(isMac ? ' Cmd ' : ' Win ', () {
setState(() => FFI.command = !FFI.command);
}, FFI.command),
];
final keys = <Widget>[
wrap(
' Fn ',
() => setState(
() {
_fn = !_fn;
if (_fn) {
_more = false;
}
},
),
_fn),
wrap(
' ... ',
() => setState(
() {
_more = !_more;
if (_more) {
_fn = false;
}
},
),
_more),
];
final fn = <Widget>[
SizedBox(width: 9999),
];
for (var i = 1; i <= 12; ++i) {
final name = 'F' + i.toString();
fn.add(wrap(name, () {
FFI.inputKey('VK_' + name);
}));
}
final more = <Widget>[
SizedBox(width: 9999),
wrap('Esc', () {
FFI.inputKey('VK_ESCAPE');
}),
wrap('Tab', () {
FFI.inputKey('VK_TAB');
}),
wrap('Home', () {
FFI.inputKey('VK_HOME');
}),
wrap('End', () {
FFI.inputKey('VK_END');
}),
wrap('Del', () {
FFI.inputKey('VK_DELETE');
}),
wrap('PgUp', () {
FFI.inputKey('VK_PRIOR');
}),
wrap('PgDn', () {
FFI.inputKey('VK_NEXT');
}),
SizedBox(width: 9999),
wrap('', () {
FFI.inputKey('VK_LEFT');
}, false, Icons.keyboard_arrow_left),
wrap('', () {
FFI.inputKey('VK_UP');
}, false, Icons.keyboard_arrow_up),
wrap('', () {
FFI.inputKey('VK_DOWN');
}, false, Icons.keyboard_arrow_down),
wrap('', () {
FFI.inputKey('VK_RIGHT');
}, false, Icons.keyboard_arrow_right),
wrap(isMac ? 'Cmd+C' : 'Ctrl+C', () {
sendPrompt(isMac, 'VK_C');
}),
wrap(isMac ? 'Cmd+V' : 'Ctrl+V', () {
sendPrompt(isMac, 'VK_V');
}),
wrap(isMac ? 'Cmd+S' : 'Ctrl+S', () {
sendPrompt(isMac, 'VK_S');
}),
];
final space = size.width > 320 ? 4.0 : 2.0;
return Container(
color: Color(0xAA000000),
padding: EdgeInsets.only(
top: keyboard ? 24 : 4, left: 0, right: 0, bottom: 8),
child: Wrap(
spacing: space,
runSpacing: space,
children: <Widget>[SizedBox(width: 9999)] +
(keyboard
? modifiers + keys + (_fn ? fn : []) + (_more ? more : [])
: mouse + modifiers),
));
}
}
class ImagePaint extends StatelessWidget {
@override
Widget build(BuildContext context) {
final m = Provider.of<ImageModel>(context);
final c = Provider.of<CanvasModel>(context);
final adjust = FFI.cursorModel.adjustForKeyboard();
var s = c.scale;
return CustomPaint(
painter: new ImagePainter(
image: m.image, x: c.x / s, y: (c.y - adjust) / s, scale: s),
);
}
}
class CursorPaint extends StatelessWidget {
@override
Widget build(BuildContext context) {
final m = Provider.of<CursorModel>(context);
final c = Provider.of<CanvasModel>(context);
final adjust = FFI.cursorModel.adjustForKeyboard();
var s = c.scale;
return CustomPaint(
painter: new ImagePainter(
image: m.image,
x: m.x * s - m.hotx + c.x,
y: m.y * s - m.hoty + c.y - adjust,
scale: 1),
);
}
}
class ImagePainter extends CustomPainter {
ImagePainter({
this.image,
this.x,
this.y,
this.scale,
});
ui.Image image;
double x;
double y;
double scale;
@override
void paint(Canvas canvas, Size size) {
if (image == null) return;
canvas.scale(scale, scale);
canvas.drawImage(image, new Offset(x, y), new Paint());
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return oldDelegate != this;
}
}
void enterPasswordDialog(String id, BuildContext context) {
final controller = TextEditingController();
var remember = FFI.getByName('remember', id) == 'true';
showAlertDialog(
context,
(setState) => Tuple3(
Text(translate('Password Required')),
Column(mainAxisSize: MainAxisSize.min, children: [
PasswordWidget(controller: controller),
CheckboxListTile(
contentPadding: const EdgeInsets.all(0),
dense: true,
controlAffinity: ListTileControlAffinity.leading,
title: Text(
translate('Remember password'),
),
value: remember,
onChanged: (v) {
setState(() => remember = v);
},
),
]),
[
TextButton(
style: flatButtonStyle,
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: () {
var text = controller.text.trim();
if (text == '') return;
FFI.login(text, remember);
showLoading(translate('Logging in...'), null);
Navigator.pop(context);
},
child: Text(translate('OK')),
),
],
));
}
void wrongPasswordDialog(String id, BuildContext context) {
showAlertDialog(
context,
(_) => Tuple3(Text(translate('Wrong Password')),
Text(translate('Do you want to enter again?')), [
TextButton(
style: flatButtonStyle,
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: () {
enterPasswordDialog(id, context);
},
child: Text(translate('Retry')),
),
]));
}
void showOptions(BuildContext context) {
String quality = FFI.getByName('image_quality');
if (quality == '') quality = 'balanced';
var displays = <Widget>[];
final pi = FFI.ffiModel.pi;
final image = FFI.ffiModel.getConnectionImage();
if (image != null)
displays.add(Padding(padding: const EdgeInsets.only(top: 8), child: image));
if (pi.displays.length > 1) {
final cur = pi.currentDisplay;
final children = <Widget>[];
for (var i = 0; i < pi.displays.length; ++i)
children.add(InkWell(
onTap: () {
if (i == cur) return;
FFI.setByName('switch_display', i.toString());
Navigator.pop(context);
},
child: Ink(
width: 40,
height: 40,
decoration: BoxDecoration(
border: Border.all(color: Colors.black87),
color: i == cur ? Colors.black87 : Colors.white),
child: Center(
child: Text((i + 1).toString(),
style: TextStyle(
color: i == cur ? Colors.white : Colors.black87))))));
displays.add(Padding(
padding: const EdgeInsets.only(top: 8),
child: Wrap(
alignment: WrapAlignment.center,
spacing: 8,
children: children,
)));
}
if (displays.isNotEmpty) {
displays.add(Divider(color: MyTheme.border));
}
showAlertDialog(context, (setState) {
final more = <Widget>[];
if (FFI.ffiModel.permissions['audio'] != false) {
more.add(CheckboxListTile(
value: FFI.getByName('toggle_option', 'disable-audio') == 'true',
onChanged: (v) {
setState(() {
FFI.setByName('toggle_option', 'disable-audio');
});
},
title: Text(translate('Mute'))));
}
if (FFI.ffiModel.permissions['keyboard'] != false) {
more.add(CheckboxListTile(
value: FFI.getByName('toggle_option', 'lock-after-session-end') ==
'true',
onChanged: (v) {
setState(() {
FFI.setByName('toggle_option', 'lock-after-session-end');
});
},
title: Text(translate('Lock after session end'))));
}
return Tuple3(
null,
Column(
mainAxisSize: MainAxisSize.min,
children: displays +
<Widget>[
RadioListTile<String>(
controlAffinity: ListTileControlAffinity.trailing,
title: Text(translate('Good image quality')),
value: 'best',
groupValue: quality,
onChanged: (String value) {
setState(() {
quality = value;
FFI.setByName('image_quality', value);
});
},
),
RadioListTile<String>(
controlAffinity: ListTileControlAffinity.trailing,
title: Text(translate('Balanced')),
value: 'balanced',
groupValue: quality,
onChanged: (String value) {
setState(() {
quality = value;
FFI.setByName('image_quality', value);
});
},
),
RadioListTile<String>(
controlAffinity: ListTileControlAffinity.trailing,
title: Text(translate('Optimize reaction time')),
value: 'low',
groupValue: quality,
onChanged: (String value) {
setState(() {
quality = value;
FFI.setByName('image_quality', value);
});
},
),
Divider(color: MyTheme.border),
CheckboxListTile(
value: FFI.getByName(
'toggle_option', 'show-remote-cursor') ==
'true',
onChanged: (v) {
setState(() {
FFI.setByName('toggle_option', 'show-remote-cursor');
});
},
title: Text(translate('Show remote cursor'))),
] +
more),
null);
}, () async => true, true, 0);
}
void showSetOSPassword(BuildContext context, bool login) {
final controller = TextEditingController();
var password = FFI.getByName('peer_option', "os-password");
var autoLogin = FFI.getByName('peer_option', "auto-login") != "";
controller.text = password;
showAlertDialog(
context,
(setState) => Tuple3(
Text(translate('OS Password')),
Column(mainAxisSize: MainAxisSize.min, children: [
PasswordWidget(controller: controller),
CheckboxListTile(
contentPadding: const EdgeInsets.all(0),
dense: true,
controlAffinity: ListTileControlAffinity.leading,
title: Text(
translate('Auto Login'),
),
value: autoLogin,
onChanged: (v) {
setState(() => autoLogin = v);
},
),
]),
[
TextButton(
style: flatButtonStyle,
onPressed: () {
Navigator.pop(context);
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: () {
var text = controller.text.trim();
FFI.setByName('peer_option',
'{"name": "os-password", "value": "${text}"}');
FFI.setByName('peer_option',
'{"name": "auto-login", "value": "${autoLogin ? 'Y' : ''}"}');
if (text != "" && login) {
FFI.setByName('input_os_password', text);
}
Navigator.pop(context);
},
child: Text(translate('OK')),
),
],
));
}
void sendPrompt(bool isMac, String key) {
final old = isMac ? FFI.command : FFI.ctrl;
if (isMac) {
FFI.command = true;
} else {
FFI.ctrl = true;
}
FFI.inputKey(key);
if (isMac) {
FFI.command = old;
} else {
FFI.ctrl = old;
}
}