rustdesk/lib/pages/home_page.dart

442 lines
14 KiB
Dart
Raw Normal View History

2020-11-15 20:04:05 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_hbb/pages/server_page.dart';
2020-11-16 01:13:26 +08:00
import 'package:provider/provider.dart';
2020-11-26 00:33:45 +08:00
import 'package:tuple/tuple.dart';
2020-11-30 11:35:43 +08:00
import 'package:url_launcher/url_launcher.dart';
2020-11-30 15:04:59 +08:00
import 'dart:async';
import '../common.dart';
import '../models/model.dart';
import 'remote_page.dart';
2020-11-15 20:04:05 +08:00
abstract class PageShape extends Widget {
final String title = "";
final Icon icon = Icon(null);
final List<Widget> appBarActions = [];
}
2020-11-15 20:04:05 +08:00
class HomePage extends StatefulWidget {
HomePage({Key? key}) : super(key: key);
2020-11-15 20:04:05 +08:00
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var _selectedIndex = 0;
final List<PageShape> _pages = [ConnectionPage(), ServerPage()];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: MyTheme.grayBg,
appBar: AppBar(
centerTitle: true,
title: Text("RustDesk"),
actions: _pages.elementAt(_selectedIndex).appBarActions,
),
bottomNavigationBar: BottomNavigationBar(
items: _pages
.map((page) =>
BottomNavigationBarItem(icon: page.icon, label: page.title))
.toList(),
currentIndex: _selectedIndex,
type: BottomNavigationBarType.fixed,
selectedItemColor: MyTheme.accent,
unselectedItemColor: MyTheme.darkGray,
onTap: (index) => setState(() {
_selectedIndex = index;
}),
),
body: _pages.elementAt(_selectedIndex),
);
}
}
class ConnectionPage extends StatefulWidget implements PageShape {
ConnectionPage({Key? key}) : super(key: key);
@override
final icon = Icon(Icons.connected_tv);
@override
final title = translate("Connection");
@override
final appBarActions = [
PopupMenuButton<String>(
itemBuilder: (context) => [
PopupMenuItem<String>(
child: Text(translate('ID Server')), value: 'id_server'),
PopupMenuItem<String>(
child: Text(translate('About') + ' RustDesk'), value: 'about')
],
onSelected: (value) {
if (value == 'id_server') {
showServer();
} else if (value == 'about') {
showAbout();
}
})
];
@override
_ConnectionPageState createState() => _ConnectionPageState();
}
class _ConnectionPageState extends State<ConnectionPage> {
final _idController = TextEditingController();
2020-11-30 15:04:59 +08:00
var _updateUrl = '';
2022-02-10 02:07:53 +08:00
var _menuPos;
2020-11-30 15:04:59 +08:00
@override
void initState() {
super.initState();
2022-01-25 18:13:11 +08:00
if (isAndroid) {
2021-08-14 14:14:01 +08:00
Timer(Duration(seconds: 5), () {
_updateUrl = FFI.getByName('software_update_url');
if (_updateUrl.isNotEmpty) setState(() {});
});
}
2020-11-30 15:04:59 +08:00
}
2020-11-15 20:04:05 +08:00
@override
Widget build(BuildContext context) {
2020-11-17 11:12:55 +08:00
Provider.of<FfiModel>(context);
2020-11-18 12:49:43 +08:00
if (_idController.text.isEmpty) _idController.text = FFI.getId();
return SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
getUpdateUI(),
getSearchBarUI(),
Container(height: 12),
getPeers(),
]),
);
2020-11-16 01:13:26 +08:00
}
void onConnect() {
var id = _idController.text.trim();
2020-11-25 18:33:09 +08:00
connect(id);
}
void connect(String id) {
2020-11-18 00:28:55 +08:00
if (id == '') return;
id = id.replaceAll(' ', '');
2020-11-25 18:33:09 +08:00
() 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();
}
2020-11-15 20:04:05 +08:00
}
Widget getUpdateUI() {
return _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))));
}
2020-11-15 20:04:05 +08:00
Widget getSearchBarUI() {
2020-11-24 22:03:04 +08:00
if (!FFI.ffiModel.initialized) {
return Container();
}
2022-01-25 19:04:59 +08:00
var w = Padding(
2020-11-30 15:04:59 +08:00
padding: const EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 0.0),
2020-11-16 01:13:26 +08:00
child: Container(
height: 84,
child: Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8),
2022-01-31 16:22:05 +08:00
child: Ink(
2020-11-16 01:13:26 +08:00
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),
2020-11-23 01:16:17 +08:00
child: TextField(
autocorrect: false,
enableSuggestions: false,
2020-12-22 14:41:14 +08:00
keyboardType: TextInputType.visiblePassword,
// keyboardType: TextInputType.number,
2020-11-16 01:13:26 +08:00
style: TextStyle(
fontFamily: 'WorkSans',
fontWeight: FontWeight.bold,
fontSize: 30,
2022-01-31 16:22:05 +08:00
color: MyTheme.idColor,
2020-11-16 01:13:26 +08:00
),
decoration: InputDecoration(
labelText: translate('Remote ID'),
2020-11-23 01:16:17 +08:00
// hintText: 'Enter your remote ID',
2020-11-16 01:13:26 +08:00
border: InputBorder.none,
helperStyle: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
2022-01-31 16:22:05 +08:00
color: MyTheme.darkGray,
2020-11-16 01:13:26 +08:00
),
labelStyle: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
letterSpacing: 0.2,
2022-01-31 16:22:05 +08:00
color: MyTheme.darkGray,
2020-11-15 20:04:05 +08:00
),
),
autofocus: _idController.text.isEmpty,
controller: _idController,
2020-11-15 20:04:05 +08:00
),
2020-11-16 01:13:26 +08:00
),
2020-11-15 20:04:05 +08:00
),
2020-11-16 01:13:26 +08:00
SizedBox(
width: 60,
height: 60,
child: IconButton(
icon: Icon(Icons.arrow_forward,
2022-01-31 16:22:05 +08:00
color: MyTheme.darkGray, size: 45),
2020-11-16 01:13:26 +08:00
onPressed: onConnect,
autofocus: _idController.text.isNotEmpty,
2020-11-16 01:13:26 +08:00
),
)
],
2020-11-15 20:04:05 +08:00
),
),
2020-11-16 01:13:26 +08:00
),
2020-11-15 20:04:05 +08:00
),
);
2022-01-25 19:04:59 +08:00
return Center(
child: Container(constraints: BoxConstraints(maxWidth: 600), child: w));
2020-11-15 20:04:05 +08:00
}
2020-11-16 01:13:26 +08:00
@override
void dispose() {
_idController.dispose();
2020-11-16 01:13:26 +08:00
super.dispose();
}
2020-11-25 18:33:09 +08:00
Widget getPlatformImage(String platform) {
platform = platform.toLowerCase();
2020-11-25 20:39:08 +08:00
if (platform == 'mac os')
2020-11-25 18:33:09 +08:00
platform = 'mac';
else if (platform != 'linux') platform = 'win';
return Image.asset('assets/$platform.png', width: 24, height: 24);
2020-11-25 18:33:09 +08:00
}
Widget getPeers() {
if (!FFI.ffiModel.initialized) {
return Container();
}
2022-01-31 16:22:05 +08:00
final size = MediaQuery.of(context).size;
final space = 8.0;
var width = size.width - 2 * space;
final minWidth = 320.0;
if (size.width > minWidth + 2 * space) {
final n = (size.width / (minWidth + 2 * space)).floor();
width = size.width / n - 2 * space;
}
2020-11-25 18:33:09 +08:00
final cards = <Widget>[];
var peers = FFI.peers();
peers.forEach((p) {
2022-01-31 16:22:05 +08:00
cards.add(Container(
width: width,
2020-11-30 21:14:23 +08:00
child: Card(
child: GestureDetector(
2022-01-31 16:22:05 +08:00
onTap: () => {
if (!isDesktop) {connect('${p.id}')}
},
onDoubleTap: () => {
if (isDesktop) {connect('${p.id}')}
},
2020-11-30 21:14:23 +08:00
onLongPressStart: (details) {
var x = details.globalPosition.dx;
var y = details.globalPosition.dy;
2022-01-31 16:22:05 +08:00
this._menuPos = RelativeRect.fromLTRB(x, y, x, y);
this.showPeerMenu(context, p.id);
2020-11-30 21:14:23 +08:00
},
child: ListTile(
2022-01-31 16:22:05 +08:00
contentPadding: const EdgeInsets.only(left: 12),
2020-11-30 21:14:23 +08:00
subtitle: Text('${p.username}@${p.hostname}'),
title: Text('${p.id}'),
leading: Container(
padding: const EdgeInsets.all(6),
child: getPlatformImage('${p.platform}'),
2021-08-14 14:14:01 +08:00
color: str2color('${p.id}${p.platform}', 0x7f)),
2022-01-31 16:22:05 +08:00
trailing: InkWell(
child: Padding(
padding: const EdgeInsets.all(12),
child: Icon(Icons.more_vert)),
onTapDown: (e) {
var x = e.globalPosition.dx;
var y = e.globalPosition.dy;
this._menuPos = RelativeRect.fromLTRB(x, y, x, y);
},
onDoubleTap: () {},
onTap: () {
showPeerMenu(context, p.id);
}),
2020-11-30 21:14:23 +08:00
)))));
2020-11-25 18:33:09 +08:00
});
2022-01-31 16:22:05 +08:00
return Wrap(children: cards, spacing: space, runSpacing: space);
}
void showPeerMenu(BuildContext context, String id) async {
var value = await showMenu(
context: context,
position: this._menuPos,
items: [
PopupMenuItem<String>(
child: Text(translate('Remove')), value: 'remove'),
],
elevation: 8,
);
if (value == 'remove') {
setState(() => FFI.setByName('remove', '$id'));
() async {
removePreference(id);
}();
}
2020-11-25 18:33:09 +08:00
}
2020-11-15 20:04:05 +08:00
}
2020-11-26 00:33:45 +08:00
void showServer() {
2020-11-26 00:47:52 +08:00
final formKey = GlobalKey<FormState>();
2021-08-06 22:29:11 +08:00
final id0 = FFI.getByName('option', 'custom-rendezvous-server');
final relay0 = FFI.getByName('option', 'relay-server');
final key0 = FFI.getByName('option', 'key');
2020-11-26 00:47:52 +08:00
var id = '';
var relay = '';
2021-04-01 16:59:42 +08:00
var key = '';
2022-02-28 16:11:21 +08:00
showAlertDialog((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) {
if (value != null) id = value.trim();
},
),
/*
2020-11-26 00:47:52 +08:00
TextFormField(
initialValue: relay0,
2020-11-26 00:47:52 +08:00
decoration: InputDecoration(
labelText: translate('Relay Server'),
2020-11-26 00:47:52 +08:00
),
validator: validate,
2020-11-26 00:47:52 +08:00
onSaved: (String value) {
relay = value.trim();
2020-11-26 00:47:52 +08:00
},
),
2021-08-06 22:45:45 +08:00
*/
2022-02-28 16:11:21 +08:00
TextFormField(
initialValue: key0,
decoration: InputDecoration(
labelText: 'Key',
),
validator: null,
onSaved: (String? value) {
if (value != null) key = value.trim();
2020-11-26 00:33:45 +08:00
},
),
2022-02-28 16:11:21 +08:00
])),
[
TextButton(
style: flatButtonStyle,
onPressed: () {
DialogManager.reset();
2022-02-28 16:11:21 +08:00
},
child: Text(translate('Cancel')),
),
TextButton(
style: flatButtonStyle,
onPressed: () {
if (formKey.currentState != null &&
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"}');
DialogManager.reset();
2022-02-28 16:11:21 +08:00
}
},
child: Text(translate('OK')),
),
],
));
2020-11-30 11:35:43 +08:00
}
Future<Null> showAbout() async {
2022-01-26 19:00:23 +08:00
var version = await FFI.getVersion();
2020-11-30 11:35:43 +08:00
showAlertDialog(
(setState) => Tuple3(
2022-02-17 15:22:14 +08:00
SizedBox.shrink(), // TODO test old:null
2020-11-30 11:35:43 +08:00
Wrap(direction: Axis.vertical, spacing: 12, children: [
2022-01-26 19:00:23 +08:00
Text('Version: $version'),
2020-11-30 11:35:43 +08:00
InkWell(
onTap: () async {
2021-04-07 21:58:30 +08:00
const url = 'https://rustdesk.com/';
2020-11-30 11:35:43 +08:00
if (await canLaunch(url)) {
await launch(url);
}
},
child: Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: Text('Support',
style: TextStyle(
decoration: TextDecoration.underline,
)),
)),
]),
2022-02-17 15:22:14 +08:00
[]),
2020-11-26 00:33:45 +08:00
() async => true,
true);
}
2022-02-17 15:22:14 +08:00
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;
}