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

625 lines
21 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart' hide MenuItem;
2022-07-14 12:32:01 +08:00
import 'package:flutter/services.dart';
import 'package:flutter_hbb/common.dart';
import 'package:flutter_hbb/consts.dart';
2022-05-29 04:39:12 +08:00
import 'package:flutter_hbb/desktop/pages/connection_page.dart';
import 'package:flutter_hbb/desktop/pages/desktop_setting_page.dart';
import 'package:flutter_hbb/desktop/pages/desktop_tab_page.dart';
2022-09-28 11:20:57 +08:00
import 'package:flutter_hbb/desktop/widgets/scroll_wrapper.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:flutter_hbb/models/server_model.dart';
import 'package:flutter_hbb/utils/multi_window_manager.dart';
2022-07-14 12:32:01 +08:00
import 'package:get/get.dart';
import 'package:provider/provider.dart';
2022-09-25 21:45:37 +08:00
import 'package:url_launcher/url_launcher.dart';
import 'package:window_size/window_size.dart' as window_size;
2022-05-23 16:44:23 +08:00
2022-09-23 20:18:11 +08:00
import '../widgets/button.dart';
2022-05-23 16:44:23 +08:00
class DesktopHomePage extends StatefulWidget {
const DesktopHomePage({Key? key}) : super(key: key);
2022-05-23 16:44:23 +08:00
@override
State<DesktopHomePage> createState() => _DesktopHomePageState();
2022-05-23 16:44:23 +08:00
}
2022-05-29 19:55:50 +08:00
const borderColor = Color(0xFF2F65BA);
class _DesktopHomePageState extends State<DesktopHomePage>
2022-12-01 11:19:51 +08:00
with AutomaticKeepAliveClientMixin {
2022-09-28 11:20:57 +08:00
final _leftPaneScrollController = ScrollController();
@override
bool get wantKeepAlive => true;
2022-09-23 17:28:22 +08:00
var updateUrl = '';
var systemError = '';
2022-10-18 10:29:33 +08:00
StreamSubscription? _uniLinksSubscription;
var svcStopped = false.obs;
var watchIsCanScreenRecording = false;
var watchIsProcessTrust = false;
Timer? _updateTimer;
2022-05-23 16:44:23 +08:00
@override
Widget build(BuildContext context) {
super.build(context);
return Row(
2022-09-28 11:20:57 +08:00
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2022-09-19 18:38:19 +08:00
buildLeftPane(context),
const VerticalDivider(
width: 1,
thickness: 1,
),
Expanded(
2022-09-19 18:38:19 +08:00
child: buildRightPane(context),
),
],
);
}
2022-09-28 11:20:57 +08:00
Widget buildLeftPane(BuildContext context) {
return ChangeNotifierProvider.value(
value: gFFI.serverModel,
2022-05-29 10:25:36 +08:00
child: Container(
width: 200,
2022-09-23 16:31:50 +08:00
color: Theme.of(context).backgroundColor,
2022-09-28 11:20:57 +08:00
child: DesktopScrollWrapper(
scrollController: _leftPaneScrollController,
child: SingleChildScrollView(
controller: _leftPaneScrollController,
child: Column(
children: [
buildTip(context),
buildIDBoard(context),
buildPasswordBoard(context),
buildHelpCards(),
],
),
),
2022-05-29 10:25:36 +08:00
),
),
);
}
2022-09-19 18:38:19 +08:00
buildRightPane(BuildContext context) {
return Container(
2022-09-23 16:31:50 +08:00
color: Theme.of(context).scaffoldBackgroundColor,
child: ConnectionPage(),
);
}
buildIDBoard(BuildContext context) {
final model = gFFI.serverModel;
2022-05-29 10:25:36 +08:00
return Container(
margin: const EdgeInsets.only(left: 20, right: 11),
height: 57,
2022-05-29 10:25:36 +08:00
child: Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Container(
width: 2,
decoration: const BoxDecoration(color: MyTheme.accent),
).marginOnly(top: 5),
2022-05-29 10:25:36 +08:00
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 7),
2022-05-29 10:25:36 +08:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 25,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
translate("ID"),
style: TextStyle(
fontSize: 14,
2022-09-23 16:31:50 +08:00
color: Theme.of(context)
.textTheme
.titleLarge
?.color
?.withOpacity(0.5)),
).marginOnly(top: 5),
buildPopupMenu(context)
],
),
2022-05-29 10:25:36 +08:00
),
Flexible(
child: GestureDetector(
onDoubleTap: () {
Clipboard.setData(
ClipboardData(text: model.serverId.text));
showToast(translate("Copied"));
},
child: TextFormField(
controller: model.serverId,
readOnly: true,
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(bottom: 20),
),
style: TextStyle(
fontSize: 22,
),
),
),
)
2022-05-29 10:25:36 +08:00
],
),
),
2022-05-29 10:25:36 +08:00
),
],
),
);
}
Widget buildPopupMenu(BuildContext context) {
2022-09-23 16:31:50 +08:00
final textColor = Theme.of(context).textTheme.titleLarge?.color;
RxBool hover = false.obs;
return InkWell(
onTap: DesktopTabPage.onAddSetting,
child: Obx(
() => CircleAvatar(
radius: 15,
backgroundColor: hover.value
2022-09-23 16:31:50 +08:00
? Theme.of(context).scaffoldBackgroundColor
: Theme.of(context).backgroundColor,
child: Icon(
Icons.more_vert_outlined,
size: 20,
2022-09-23 16:31:50 +08:00
color: hover.value ? textColor : textColor?.withOpacity(0.5),
),
),
),
onHover: (value) => hover.value = value,
);
}
2022-05-29 10:25:36 +08:00
buildPasswordBoard(BuildContext context) {
final model = gFFI.serverModel;
RxBool refreshHover = false.obs;
2022-09-19 18:38:19 +08:00
RxBool editHover = false.obs;
2022-09-23 16:31:50 +08:00
final textColor = Theme.of(context).textTheme.titleLarge?.color;
2022-05-29 10:25:36 +08:00
return Container(
margin: EdgeInsets.only(left: 20.0, right: 16, top: 13, bottom: 13),
2022-05-29 10:25:36 +08:00
child: Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Container(
width: 2,
height: 52,
2022-05-29 10:25:36 +08:00
decoration: BoxDecoration(color: MyTheme.accent),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 7),
2022-05-29 10:25:36 +08:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AutoSizeText(
translate("One-time Password"),
style: TextStyle(
2022-09-23 16:31:50 +08:00
fontSize: 14, color: textColor?.withOpacity(0.5)),
maxLines: 1,
2022-05-29 10:25:36 +08:00
),
Row(
children: [
Expanded(
child: GestureDetector(
onDoubleTap: () {
if (model.verificationMethod !=
kUsePermanentPassword) {
Clipboard.setData(
ClipboardData(text: model.serverPasswd.text));
showToast(translate("Copied"));
}
},
child: TextFormField(
controller: model.serverPasswd,
readOnly: true,
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(bottom: 2),
),
style: TextStyle(fontSize: 15),
),
),
),
InkWell(
child: Obx(
() => Icon(
Icons.refresh,
color: refreshHover.value
2022-09-23 16:31:50 +08:00
? textColor
: Color(0xFFDDDDDD), // TODO
size: 22,
).marginOnly(right: 8, bottom: 2),
),
onTap: () => bind.mainUpdateTemporaryPassword(),
onHover: (value) => refreshHover.value = value,
),
2022-09-19 18:38:19 +08:00
InkWell(
child: Obx(
() => Icon(
Icons.edit,
color: editHover.value
2022-09-23 16:31:50 +08:00
? textColor
: Color(0xFFDDDDDD), // TODO
2022-09-19 18:38:19 +08:00
size: 22,
).marginOnly(right: 8, bottom: 2),
),
onTap: () => DesktopSettingPage.switch2page(1),
2022-09-19 18:38:19 +08:00
onHover: (value) => editHover.value = value,
),
],
),
2022-05-29 10:25:36 +08:00
],
),
),
2022-05-29 10:25:36 +08:00
),
],
),
);
}
buildTip(BuildContext context) {
return Padding(
padding:
const EdgeInsets.only(left: 20.0, right: 16, top: 16.0, bottom: 5),
2022-05-29 10:25:36 +08:00
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
translate("Your Desktop"),
2022-09-23 16:31:50 +08:00
style: Theme.of(context).textTheme.titleLarge,
// style: TextStyle(
// // color: MyTheme.color(context).text,
// fontWeight: FontWeight.normal,
// fontSize: 19),
2022-05-29 10:25:36 +08:00
),
SizedBox(
height: 10.0,
2022-05-29 10:25:36 +08:00
),
Text(
translate("desk_tip"),
overflow: TextOverflow.clip,
2022-09-23 16:31:50 +08:00
style: Theme.of(context).textTheme.bodySmall,
2022-05-29 10:25:36 +08:00
)
],
),
);
2022-05-23 16:44:23 +08:00
}
2022-09-23 17:28:22 +08:00
Widget buildHelpCards() {
if (updateUrl.isNotEmpty) {
2022-09-25 21:45:37 +08:00
return buildInstallCard(
"Status",
"There is a newer version of ${bind.mainGetAppNameSync()} ${bind.mainGetNewVersion()} available.",
"Click to download", () async {
final Uri url = Uri.parse('https://rustdesk.com');
await launchUrl(url);
});
2022-09-23 17:28:22 +08:00
}
if (systemError.isNotEmpty) {
return buildInstallCard("", systemError, "", () {});
}
2022-12-04 18:39:59 +08:00
if (Platform.isWindows) {
if (!bind.mainIsInstalled()) {
return buildInstallCard(
"", "install_tip", "Install", bind.mainGotoInstall);
} else if (bind.mainIsInstalledLowerVersion()) {
return buildInstallCard("Status", "Your installation is lower version.",
"Click to upgrade", bind.mainUpdateMe);
}
} else if (Platform.isMacOS) {
if (!bind.mainIsCanScreenRecording(prompt: false)) {
return buildInstallCard("Permissions", "config_screen", "Configure",
() async {
bind.mainIsCanScreenRecording(prompt: true);
watchIsCanScreenRecording = true;
}, help: 'Help', link: translate("doc_mac_permission"));
} else if (!bind.mainIsProcessTrusted(prompt: false)) {
return buildInstallCard("Permissions", "config_acc", "Configure",
() async {
bind.mainIsProcessTrusted(prompt: true);
watchIsProcessTrust = true;
}, help: 'Help', link: translate("doc_mac_permission"));
} else if (!svcStopped.value &&
bind.mainIsInstalled() &&
!bind.mainIsInstalledDaemon(prompt: false)) {
return buildInstallCard("", "install_daemon_tip", "Install", () async {
bind.mainIsInstalledDaemon(prompt: true);
});
}
2022-12-04 18:39:59 +08:00
} else if (Platform.isLinux) {
if (bind.mainCurrentIsWayland()) {
return buildInstallCard(
"Warning", translate("wayland_experiment_tip"), "", () async {},
help: 'Help',
link: 'https://rustdesk.com/docs/en/manual/linux/#x11-required');
} else if (bind.mainIsLoginWayland()) {
return buildInstallCard("Warning",
"Login screen using Wayland is not supported", "", () async {},
help: 'Help',
link: 'https://rustdesk.com/docs/en/manual/linux/#login-screen');
}
}
2022-09-23 17:28:22 +08:00
return Container();
}
2022-09-25 21:45:37 +08:00
Widget buildInstallCard(String title, String content, String btnText,
GestureTapCallback onPressed,
{String? help, String? link}) {
2022-09-23 17:28:22 +08:00
return Container(
margin: EdgeInsets.only(top: 20),
2022-09-23 20:18:11 +08:00
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
Color.fromARGB(255, 226, 66, 188),
Color.fromARGB(255, 244, 114, 124),
],
)),
padding: EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: (title.isNotEmpty
? <Widget>[
Center(
child: Text(
translate(title),
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 15),
).marginOnly(bottom: 6)),
]
: <Widget>[]) +
<Widget>[
Text(
translate(content),
style: TextStyle(
height: 1.5,
color: Colors.white,
fontWeight: FontWeight.normal,
fontSize: 13),
).marginOnly(bottom: 20)
] +
(btnText.isNotEmpty
? <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FixedWidthButton(
width: 150,
padding: 8,
isOutline: true,
text: translate(btnText),
textColor: Colors.white,
borderColor: Colors.white,
textSize: 20,
radius: 10,
onTap: onPressed,
)
])
]
: <Widget>[]) +
(help != null
? <Widget>[
Center(
child: InkWell(
onTap: () async =>
await launchUrl(Uri.parse(link!)),
child: Text(
translate(help),
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.white,
fontSize: 12),
)).marginOnly(top: 6)),
]
: <Widget>[]))),
2022-09-23 17:28:22 +08:00
);
}
@override
void initState() {
super.initState();
2022-11-29 22:36:35 +08:00
_updateTimer = periodic_immediate(const Duration(seconds: 1), () async {
await gFFI.serverModel.fetchID();
final url = await bind.mainGetSoftwareUpdateUrl();
if (updateUrl != url) {
updateUrl = url;
setState(() {});
}
final error = await bind.mainGetError();
if (systemError != error) {
systemError = error;
setState(() {});
}
final v = await bind.mainGetOption(key: "stop-service") == "Y";
if (v != svcStopped.value) {
svcStopped.value = v;
setState(() {});
}
if (watchIsCanScreenRecording) {
if (bind.mainIsCanScreenRecording(prompt: false)) {
watchIsCanScreenRecording = false;
setState(() {});
}
}
if (watchIsProcessTrust) {
if (bind.mainIsProcessTrusted(prompt: false)) {
watchIsProcessTrust = false;
setState(() {});
}
}
2022-09-23 17:28:22 +08:00
});
Get.put<RxBool>(svcStopped, tag: 'stop-service');
2022-11-05 23:41:22 +08:00
rustDeskWinManager.registerActiveWindowListener(onActiveWindowChanged);
rustDeskWinManager.setMethodHandler((call, fromWindowId) async {
debugPrint(
2022-11-05 23:41:22 +08:00
"[Main] call ${call.method} with args ${call.arguments} from window $fromWindowId");
if (call.method == kWindowMainWindowOnTop) {
2022-08-09 19:32:19 +08:00
window_on_top(null);
} else if (call.method == kWindowGetWindowInfo) {
final screen = (await window_size.getWindowInfo()).screen;
if (screen == null) {
return "";
} else {
return jsonEncode({
'frame': {
'l': screen.frame.left,
't': screen.frame.top,
'r': screen.frame.right,
'b': screen.frame.bottom,
},
'visibleFrame': {
'l': screen.visibleFrame.left,
't': screen.visibleFrame.top,
'r': screen.visibleFrame.right,
'b': screen.visibleFrame.bottom,
},
'scaleFactor': screen.scaleFactor,
});
}
} else if (call.method == kWindowActionRebuild) {
reloadCurrentWindow();
2022-11-05 23:41:22 +08:00
} else if (call.method == kWindowEventShow) {
rustDeskWinManager.registerActiveWindow(call.arguments["id"]);
} else if (call.method == kWindowEventHide) {
rustDeskWinManager.unregisterActiveWindow(call.arguments["id"]);
} else if (call.method == kWindowConnect) {
await connectMainDesktop(
call.arguments['id'],
isFileTransfer: call.arguments['isFileTransfer'],
isTcpTunneling: call.arguments['isTcpTunneling'],
isRDP: call.arguments['isRDP'],
);
}
});
2022-10-18 10:29:33 +08:00
_uniLinksSubscription = listenUniLinks();
}
@override
void dispose() {
2022-10-18 10:29:33 +08:00
_uniLinksSubscription?.cancel();
Get.delete<RxBool>(tag: 'stop-service');
_updateTimer?.cancel();
super.dispose();
}
2022-05-23 16:44:23 +08:00
}
void setPasswordDialog() async {
final pw = await bind.mainGetPermanentPassword();
final p0 = TextEditingController(text: pw);
final p1 = TextEditingController(text: pw);
var errMsg0 = "";
var errMsg1 = "";
2022-08-12 18:42:02 +08:00
gFFI.dialogManager.show((setState, close) {
submit() {
setState(() {
errMsg0 = "";
errMsg1 = "";
});
final pass = p0.text.trim();
if (pass.length < 6 && pass.isNotEmpty) {
setState(() {
errMsg0 = translate("Too short, at least 6 characters.");
});
return;
}
if (p1.text.trim() != pass) {
setState(() {
errMsg1 = translate("The confirmation is not identical.");
});
return;
}
bind.mainSetPermanentPassword(password: pass);
close();
}
return CustomAlertDialog(
title: Text(translate("Set Password")),
content: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 500),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 8.0,
),
Row(
children: [
ConstrainedBox(
constraints: const BoxConstraints(minWidth: 100),
child: Text(
"${translate('Password')}:",
textAlign: TextAlign.start,
).marginOnly(bottom: 16.0)),
const SizedBox(
width: 24.0,
),
Expanded(
child: TextField(
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
errorText: errMsg0.isNotEmpty ? errMsg0 : null),
controller: p0,
focusNode: FocusNode()..requestFocus(),
),
),
],
),
const SizedBox(
height: 8.0,
),
Row(
children: [
ConstrainedBox(
constraints: const BoxConstraints(minWidth: 100),
child: Text("${translate('Confirmation')}:")
.marginOnly(bottom: 16.0)),
const SizedBox(
width: 24.0,
),
Expanded(
child: TextField(
obscureText: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
errorText: errMsg1.isNotEmpty ? errMsg1 : null),
controller: p1,
),
),
],
),
],
),
),
actions: [
TextButton(onPressed: close, child: Text(translate("Cancel"))),
TextButton(onPressed: submit, child: Text(translate("OK"))),
],
onSubmit: submit,
onCancel: close,
);
});
}