rustdesk/flutter/lib/desktop/pages/connection_page.dart

363 lines
12 KiB
Dart
Raw Normal View History

2022-09-19 18:38:19 +08:00
// main window right pane
import 'dart:async';
2022-07-22 23:12:31 +08:00
import 'dart:convert';
import 'dart:io';
2022-07-22 23:12:31 +08:00
2022-05-29 04:39:12 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_hbb/common/widgets/address_book.dart';
2022-09-23 15:12:50 +08:00
import 'package:flutter_hbb/consts.dart';
2022-09-28 11:20:57 +08:00
import 'package:flutter_hbb/desktop/widgets/scroll_wrapper.dart';
2022-07-14 12:32:01 +08:00
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:window_manager/window_manager.dart';
2022-05-29 04:39:12 +08:00
import '../../common.dart';
import '../../common/formatter/id_formatter.dart';
import '../../common/widgets/peer_tab_page.dart';
import '../../common/widgets/peers_view.dart';
import '../../models/platform_model.dart';
2022-09-23 17:28:22 +08:00
import '../widgets/button.dart';
2022-05-29 04:39:12 +08:00
/// Connection page for connecting to a remote peer.
class ConnectionPage extends StatefulWidget {
const ConnectionPage({Key? key}) : super(key: key);
2022-05-29 04:39:12 +08:00
@override
State<ConnectionPage> createState() => _ConnectionPageState();
2022-05-29 04:39:12 +08:00
}
/// State for the connection page.
2022-09-28 11:20:57 +08:00
class _ConnectionPageState extends State<ConnectionPage>
with SingleTickerProviderStateMixin, WindowListener {
2022-05-29 04:39:12 +08:00
/// Controller for the id input bar.
final _idController = IDTextEditingController();
2022-05-29 04:39:12 +08:00
2022-09-28 11:20:57 +08:00
/// Nested scroll controller
final _scrollController = ScrollController();
Timer? _updateTimer;
final RxBool _idInputFocused = false.obs;
final FocusNode _idFocusNode = FocusNode();
var svcStopped = false.obs;
var svcStatusCode = 0.obs;
var svcIsUsingPublicServer = true.obs;
bool isWindowMinisized = false;
2022-05-29 04:39:12 +08:00
@override
void initState() {
super.initState();
if (_idController.text.isEmpty) {
() async {
final lastRemoteId = await bind.mainGetLastRemoteId();
if (lastRemoteId != _idController.id) {
setState(() {
_idController.id = lastRemoteId;
});
}
}();
}
_updateTimer = Timer.periodic(Duration(seconds: 1), (timer) {
updateStatus();
});
_idFocusNode.addListener(() {
_idInputFocused.value = _idFocusNode.hasFocus;
});
Get.put<RxBool>(svcStopped, tag: 'service-stop');
windowManager.addListener(this);
}
@override
void dispose() {
_idController.dispose();
_updateTimer?.cancel();
Get.delete<RxBool>(tag: 'service-stop');
windowManager.removeListener(this);
super.dispose();
2022-05-29 04:39:12 +08:00
}
@override
void onWindowEvent(String eventName) {
super.onWindowEvent(eventName);
if (eventName == 'minimize') {
isWindowMinisized = true;
} else if (eventName == 'maximize' || eventName == 'restore') {
if (isWindowMinisized && Platform.isWindows) {
// windows can't update when minisized.
Get.forceAppUpdate();
}
isWindowMinisized = false;
}
}
@override
void onWindowClose() {
super.onWindowClose();
bind.mainOnMainWindowClose();
}
2022-05-29 04:39:12 +08:00
@override
Widget build(BuildContext context) {
2022-09-28 11:20:57 +08:00
return Column(
children: [
Expanded(
child: DesktopScrollWrapper(
scrollController: _scrollController,
child: CustomScrollView(
controller: _scrollController,
2022-09-28 21:21:54 +08:00
physics: NeverScrollableScrollPhysics(),
2022-09-28 11:20:57 +08:00
slivers: [
SliverList(
delegate: SliverChildListDelegate([
Row(
children: [
2022-09-21 17:54:47 +08:00
_buildRemoteIDTextField(context),
],
).marginOnly(top: 22),
SizedBox(height: 12),
2022-09-28 21:21:54 +08:00
Divider().paddingOnly(right: 12),
2022-09-28 11:20:57 +08:00
])),
SliverFillRemaining(
hasScrollBody: false,
child: PeerTabPage(
tabs: [
translate('Recent Sessions'),
translate('Favorites'),
translate('Discovered'),
translate('Address Book')
],
children: [
RecentPeersView(
menuPadding: kDesktopMenuPadding,
),
FavoritePeersView(
menuPadding: kDesktopMenuPadding,
),
DiscoveredPeersView(
menuPadding: kDesktopMenuPadding,
),
const AddressBook(
menuPadding: kDesktopMenuPadding,
),
],
2022-09-28 21:21:54 +08:00
).paddingOnly(right: 12.0),
2022-09-28 11:20:57 +08:00
)
],
2022-09-28 21:21:54 +08:00
).paddingOnly(left: 12.0),
2022-09-28 11:20:57 +08:00
),
),
const Divider(height: 1),
buildStatus()
2022-09-28 11:20:57 +08:00
],
2022-05-29 04:39:12 +08:00
);
}
/// Callback for the connect button.
/// Connects to the selected peer.
void onConnect({bool isFileTransfer = false}) {
final id = _idController.id;
connect(context, id, isFileTransfer: isFileTransfer);
2022-05-29 04:39:12 +08:00
}
2022-09-21 17:54:47 +08:00
/// UI for the remote ID TextField.
2022-05-29 04:39:12 +08:00
/// Search for a peer and connect to it if the id exists.
2022-09-21 17:54:47 +08:00
Widget _buildRemoteIDTextField(BuildContext context) {
2022-07-14 12:32:01 +08:00
var w = Container(
width: 320 + 20 * 2,
padding: const EdgeInsets.fromLTRB(20, 24, 20, 22),
2022-07-14 12:32:01 +08:00
decoration: BoxDecoration(
2022-09-23 16:31:50 +08:00
color: Theme.of(context).backgroundColor,
2022-07-14 12:32:01 +08:00
borderRadius: const BorderRadius.all(Radius.circular(13)),
),
child: Ink(
child: Column(
children: [
Row(
children: [
Text(
translate('Control Remote Desktop'),
2022-09-23 16:31:50 +08:00
style: Theme.of(context)
.textTheme
.titleLarge
?.merge(TextStyle(height: 1)),
),
],
).marginOnly(bottom: 15),
Row(
children: [
2022-07-14 12:32:01 +08:00
Expanded(
child: Obx(
() => TextField(
autocorrect: false,
enableSuggestions: false,
keyboardType: TextInputType.visiblePassword,
focusNode: _idFocusNode,
style: const TextStyle(
fontFamily: 'WorkSans',
fontSize: 22,
height: 1,
),
maxLines: 1,
2022-09-23 16:31:50 +08:00
cursorColor:
Theme.of(context).textTheme.titleLarge?.color,
decoration: InputDecoration(
hintText: _idInputFocused.value
? null
: translate('Enter Remote ID'),
border: OutlineInputBorder(
borderRadius: BorderRadius.zero,
borderSide: BorderSide(
color: MyTheme.color(context).border!)),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.zero,
borderSide: BorderSide(
color: MyTheme.color(context).border!)),
focusedBorder: const OutlineInputBorder(
borderRadius: BorderRadius.zero,
borderSide:
BorderSide(color: MyTheme.button, width: 3),
),
isDense: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 12)),
controller: _idController,
inputFormatters: [IDTextInputFormatter()],
onSubmitted: (s) {
onConnect();
},
),
2022-07-14 12:32:01 +08:00
),
2022-05-29 04:39:12 +08:00
),
2022-07-14 12:32:01 +08:00
],
),
Padding(
padding: const EdgeInsets.only(top: 13.0),
2022-07-14 12:32:01 +08:00
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
2022-09-23 17:28:22 +08:00
Button(
isOutline: true,
onTap: () {
onConnect(isFileTransfer: true);
},
text: "Transfer File",
),
const SizedBox(
width: 17,
2022-07-14 12:32:01 +08:00
),
2022-09-23 17:28:22 +08:00
Button(onTap: onConnect, text: "Connect"),
2022-07-14 12:32:01 +08:00
],
),
)
],
2022-05-29 04:39:12 +08:00
),
),
);
return Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 600), child: w));
2022-05-29 04:39:12 +08:00
}
Widget buildStatus() {
final em = 14.0;
return ConstrainedBox(
constraints: BoxConstraints.tightFor(height: 3 * em),
child: Obx(() => Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: 8,
width: 8,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: svcStopped.value || svcStatusCode.value == 0
? kColorWarn
: (svcStatusCode.value == 1
? Color.fromARGB(255, 50, 190, 166)
: Color.fromARGB(255, 224, 79, 95)),
),
).marginSymmetric(horizontal: em),
Text(
svcStopped.value
? translate("Service is not running")
: svcStatusCode.value == 0
? translate("connecting_status")
: svcStatusCode.value == -1
? translate("not_ready_status")
: translate('Ready'),
style: TextStyle(fontSize: em)),
// stop
Offstage(
offstage: !svcStopped.value,
child: GestureDetector(
onTap: () async {
bool checked = !bind.mainIsInstalled() ||
await bind.mainCheckSuperUserPermission();
if (checked) {
bind.mainSetOption(key: "stop-service", value: "");
bind.mainSetOption(key: "access-mode", value: "");
}
},
child: Text(translate("Start Service"),
style: TextStyle(
decoration: TextDecoration.underline,
fontSize: em)))
.marginOnly(left: em),
),
// ready && public
Flexible(
child: Offstage(
offstage: !(!svcStopped.value &&
svcStatusCode.value == 1 &&
svcIsUsingPublicServer.value),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(', ', style: TextStyle(fontSize: em)),
Flexible(
child: InkWell(
onTap: onUsePublicServerGuide,
child: Row(
children: [
Flexible(
child: Text(
translate('setup_server_tip'),
style: TextStyle(
decoration: TextDecoration.underline,
fontSize: em),
),
),
],
),
),
)
],
),
),
)
],
)),
);
}
void onUsePublicServerGuide() {
const url = "https://rustdesk.com/blog/id-relay-set/";
canLaunchUrlString(url).then((can) {
if (can) {
launchUrlString(url);
}
});
}
updateStatus() async {
2022-08-09 20:50:45 +08:00
svcStopped.value = await bind.mainGetOption(key: "stop-service") == "Y";
final status =
jsonDecode(await bind.mainGetConnectStatus()) as Map<String, dynamic>;
svcStatusCode.value = status["status_num"];
svcIsUsingPublicServer.value = await bind.mainIsUsingPublicServer();
}
}