rustdesk/flutter/lib/desktop/pages/server_page.dart
Kingtous b8f7e85c0b feat: main window custom bar & drag
Signed-off-by: Kingtous <kingtous@qq.com>
2022-08-18 17:26:39 +08:00

592 lines
17 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
// import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:provider/provider.dart';
import 'package:window_manager/window_manager.dart';
import '../../common.dart';
import '../../mobile/pages/home_page.dart';
import '../../models/platform_model.dart';
import '../../models/server_model.dart';
class DesktopServerPage extends StatefulWidget implements PageShape {
@override
final title = translate("Share Screen");
@override
final icon = Icon(Icons.mobile_screen_share);
@override
final appBarActions = [
PopupMenuButton<String>(
icon: Icon(Icons.more_vert),
itemBuilder: (context) {
return [
PopupMenuItem(
child: Text(translate("Change ID")),
padding: EdgeInsets.symmetric(horizontal: 16.0),
value: "changeID",
enabled: false,
),
PopupMenuItem(
child: Text(translate("Set permanent password")),
padding: EdgeInsets.symmetric(horizontal: 16.0),
value: "setPermanentPassword",
enabled:
gFFI.serverModel.verificationMethod != kUseTemporaryPassword,
),
PopupMenuItem(
child: Text(translate("Set temporary password length")),
padding: EdgeInsets.symmetric(horizontal: 16.0),
value: "setTemporaryPasswordLength",
enabled:
gFFI.serverModel.verificationMethod != kUsePermanentPassword,
),
const PopupMenuDivider(),
PopupMenuItem(
padding: EdgeInsets.symmetric(horizontal: 0.0),
value: kUseTemporaryPassword,
child: Container(
child: ListTile(
title: Text(translate("Use temporary password")),
trailing: Icon(
Icons.check,
color: gFFI.serverModel.verificationMethod ==
kUseTemporaryPassword
? null
: Color(0xFFFFFFFF),
))),
),
PopupMenuItem(
padding: EdgeInsets.symmetric(horizontal: 0.0),
value: kUsePermanentPassword,
child: ListTile(
title: Text(translate("Use permanent password")),
trailing: Icon(
Icons.check,
color: gFFI.serverModel.verificationMethod ==
kUsePermanentPassword
? null
: Color(0xFFFFFFFF),
)),
),
PopupMenuItem(
padding: EdgeInsets.symmetric(horizontal: 0.0),
value: kUseBothPasswords,
child: ListTile(
title: Text(translate("Use both passwords")),
trailing: Icon(
Icons.check,
color: gFFI.serverModel.verificationMethod !=
kUseTemporaryPassword &&
gFFI.serverModel.verificationMethod !=
kUsePermanentPassword
? null
: Color(0xFFFFFFFF),
)),
),
];
},
onSelected: (value) {
if (value == "changeID") {
// TODO
} else if (value == "setPermanentPassword") {
// setPermanentPasswordDialog();
} else if (value == "setTemporaryPasswordLength") {
// setTemporaryPasswordLengthDialog();
} else if (value == kUsePermanentPassword ||
value == kUseTemporaryPassword ||
value == kUseBothPasswords) {
bind.mainSetOption(key: "verification-method", value: value);
gFFI.serverModel.updatePasswordModel();
}
})
];
@override
State<StatefulWidget> createState() => _DesktopServerPageState();
}
class _DesktopServerPageState extends State<DesktopServerPage>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return ChangeNotifierProvider.value(
value: gFFI.serverModel,
child: Consumer<ServerModel>(
builder: (context, serverModel, child) => Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(child: ConnectionManager()),
SizedBox.fromSize(size: Size(0, 15.0)),
],
),
),
)));
}
@override
bool get wantKeepAlive => true;
}
class ConnectionManager extends StatelessWidget {
@override
Widget build(BuildContext context) {
final serverModel = Provider.of<ServerModel>(context);
// test case:
// serverModel.clients.clear();
// serverModel.clients[0] = Client(
// false, false, "Readmi-M21sdfsdf", "123123123", true, false, false);
return serverModel.clients.isEmpty
? Column(
children: [
buildTitleBar(Offstage()),
Expanded(
child: Center(
child: Text(translate("Waiting")),
),
),
],
)
: DefaultTabController(
length: serverModel.clients.length,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: kTextTabBarHeight,
child: buildTitleBar(TabBar(
isScrollable: true,
tabs: serverModel.clients.entries
.map((entry) => buildTab(entry))
.toList(growable: false))),
),
Expanded(
child: TabBarView(
children: serverModel.clients.entries
.map((entry) => buildConnectionCard(entry))
.toList(growable: false)),
)
],
),
);
}
Widget buildTitleBar(Widget middle) {
return GestureDetector(
onPanDown: (d) {
windowManager.startDragging();
},
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_AppIcon(),
Expanded(child: middle),
const SizedBox(
width: 4.0,
),
_CloseButton()
],
),
);
}
Widget buildConnectionCard(MapEntry<int, Client> entry) {
final client = entry.value;
return Column(
key: ValueKey(entry.key),
children: [
_CmHeader(client: client),
client.isFileTransfer ? Offstage() : _PrivilegeBoard(client: client),
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: _CmControlPanel(client: client),
))
],
).paddingSymmetric(vertical: 8.0, horizontal: 8.0);
}
Widget buildTab(MapEntry<int, Client> entry) {
return Tab(
child: Row(
children: [
SizedBox(
width: 80,
child: Text(
"${entry.value.name}",
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
)),
],
),
);
}
}
class _AppIcon extends StatelessWidget {
const _AppIcon({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 4.0),
child: Image.asset(
'assets/logo.ico',
width: 30,
height: 30,
),
);
}
}
class _CloseButton extends StatelessWidget {
const _CloseButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Ink(
child: InkWell(
onTap: () {
windowManager.close();
},
child: Icon(
Icons.close,
size: 30,
)),
);
}
}
class _CmHeader extends StatefulWidget {
final Client client;
const _CmHeader({Key? key, required this.client}) : super(key: key);
@override
State<_CmHeader> createState() => _CmHeaderState();
}
class _CmHeaderState extends State<_CmHeader>
with AutomaticKeepAliveClientMixin {
Client get client => widget.client;
var _time = 0.obs;
Timer? _timer;
@override
void initState() {
super.initState();
_timer = Timer.periodic(Duration(seconds: 1), (_) {
_time.value = _time.value + 1;
});
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
super.build(context);
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// icon
Container(
width: 100,
height: 100,
alignment: Alignment.center,
decoration: BoxDecoration(color: str2color(client.name)),
child: Text(
"${client.name[0]}",
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white, fontSize: 75),
),
).marginOnly(left: 4.0, right: 8.0),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"${client.name}",
style: TextStyle(
color: MyTheme.cmIdColor,
fontWeight: FontWeight.bold,
fontSize: 20,
overflow: TextOverflow.ellipsis,
),
maxLines: 1,
),
Text("(${client.peerId})",
style: TextStyle(color: MyTheme.cmIdColor, fontSize: 14)),
SizedBox(
height: 16.0,
),
Row(
children: [
Text("${translate("Connected")}").marginOnly(right: 8.0),
Obx(() => Text(
"${formatDurationToTime(Duration(seconds: _time.value))}"))
],
)
],
),
),
Offstage(
offstage: client.isFileTransfer,
child: IconButton(
onPressed: handleSendMsg,
icon: Icon(Icons.message_outlined),
),
)
],
);
}
void handleSendMsg() {}
@override
bool get wantKeepAlive => true;
}
class _PrivilegeBoard extends StatelessWidget {
final Client client;
const _PrivilegeBoard({Key? key, required this.client}) : super(key: key);
Widget buildPermissionIcon(bool enabled, ImageProvider icon,
Function(bool)? onTap, String? tooltip) {
return Tooltip(
message: tooltip ?? "",
child: Ink(
decoration:
BoxDecoration(color: enabled ? MyTheme.accent80 : Colors.grey),
padding: EdgeInsets.all(4.0),
child: InkWell(
onTap: () => onTap?.call(!enabled),
child: Image(
image: icon,
width: 50,
height: 50,
fit: BoxFit.scaleDown,
),
),
).marginSymmetric(horizontal: 4.0),
);
}
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(top: 16.0, bottom: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
translate("Permissions"),
style: TextStyle(fontSize: 16),
).marginOnly(left: 4.0),
SizedBox(
height: 8.0,
),
Row(
children: [
buildPermissionIcon(
client.keyboard, iconKeyboard, (enable) => null, null),
buildPermissionIcon(
client.clipboard, iconClipboard, (enable) => null, null),
buildPermissionIcon(
client.audio, iconAudio, (enable) => null, null),
// TODO: file transfer
buildPermissionIcon(false, iconFile, (enable) => null, null),
],
),
],
),
);
}
}
class _CmControlPanel extends StatelessWidget {
final Client client;
const _CmControlPanel({Key? key, required this.client}) : super(key: key);
@override
Widget build(BuildContext context) {
return client.authorized
? buildAuthorized(context)
: buildUnAuthorized(context);
}
buildAuthorized(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Ink(
width: 200,
height: 40,
decoration: BoxDecoration(
color: Colors.redAccent, borderRadius: BorderRadius.circular(10)),
child: InkWell(
onTap: () => handleDisconnect(context),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
translate("Disconnect"),
style: TextStyle(color: Colors.white),
),
],
)),
)
],
);
}
buildUnAuthorized(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Ink(
width: 100,
height: 40,
decoration: BoxDecoration(
color: MyTheme.accent, borderRadius: BorderRadius.circular(10)),
child: InkWell(
onTap: () => handleAccept(context),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
translate("Accept"),
style: TextStyle(color: Colors.white),
),
],
)),
),
SizedBox(
width: 30,
),
Ink(
width: 100,
height: 40,
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.grey)),
child: InkWell(
onTap: () => handleDisconnect(context),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
translate("Cancel"),
style: TextStyle(),
),
],
)),
)
],
);
}
void handleDisconnect(BuildContext context) {
bind.cmCloseConnection(connId: client.id);
}
void handleAccept(BuildContext context) {
final model = Provider.of<ServerModel>(context, listen: false);
model.sendLoginResponse(client, true);
}
}
class PaddingCard extends StatelessWidget {
PaddingCard({required this.child, this.title, this.titleIcon});
final String? title;
final IconData? titleIcon;
final Widget child;
@override
Widget build(BuildContext context) {
final children = [child];
if (title != null) {
children.insert(
0,
Padding(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Row(
children: [
titleIcon != null
? Padding(
padding: EdgeInsets.only(right: 10),
child: Icon(titleIcon,
color: MyTheme.accent80, size: 30))
: SizedBox.shrink(),
Text(
title!,
style: TextStyle(
fontFamily: 'WorkSans',
fontWeight: FontWeight.bold,
fontSize: 20,
color: MyTheme.accent80,
),
)
],
)));
}
return Container(
width: double.maxFinite,
child: Card(
margin: EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 0),
child: Padding(
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
),
),
));
}
}
Widget clientInfo(Client client) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Row(
children: [
Expanded(
flex: -1,
child: Padding(
padding: EdgeInsets.only(right: 12),
child: CircleAvatar(
child: Text(client.name[0]),
backgroundColor: MyTheme.border))),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(client.name,
style: TextStyle(color: MyTheme.idColor, fontSize: 18)),
SizedBox(width: 8),
Text(client.peerId,
style: TextStyle(color: MyTheme.idColor, fontSize: 10))
]))
],
),
]));
}