show peer note (#13140)

Signed-off-by: 21pages <sunboeasy@gmail.com>
Co-authored-by: RustDesk <71636191+rustdesk@users.noreply.github.com>
This commit is contained in:
21pages
2025-10-12 14:59:42 +08:00
committed by GitHub
parent 9826c4e943
commit 30552fd202
56 changed files with 394 additions and 90 deletions

View File

@@ -13,6 +13,7 @@ import 'package:flutter_hbb/desktop/widgets/refresh_wrapper.dart';
import 'package:flutter_hbb/desktop/widgets/tabbar_widget.dart';
import 'package:flutter_hbb/main.dart';
import 'package:flutter_hbb/models/peer_model.dart';
import 'package:flutter_hbb/models/peer_tab_model.dart';
import 'package:flutter_hbb/models/state_model.dart';
import 'package:flutter_hbb/utils/multi_window_manager.dart';
import 'package:flutter_hbb/utils/platform_channel.dart';
@@ -1630,7 +1631,8 @@ bool mainGetPeerBoolOptionSync(String id, String key) {
// Use `sessionGetToggleOption()` and `sessionToggleOption()` instead.
// Because all session options use `Y` and `<Empty>` as values.
Future<bool> matchPeer(String searchText, Peer peer) async {
Future<bool> matchPeer(
String searchText, Peer peer, PeerTabIndex peerTabIndex) async {
if (searchText.isEmpty) {
return true;
}
@@ -1641,11 +1643,14 @@ Future<bool> matchPeer(String searchText, Peer peer) async {
peer.username.toLowerCase().contains(searchText)) {
return true;
}
final alias = peer.alias;
if (alias.isEmpty) {
return false;
if (peer.alias.toLowerCase().contains(searchText)) {
return true;
}
return alias.toLowerCase().contains(searchText);
if (peerTabShowNote(peerTabIndex) &&
peer.note.toLowerCase().contains(searchText)) {
return true;
}
return false;
}
/// Get the image for the current [platform].
@@ -4008,3 +4013,7 @@ String decode_http_response(http.Response resp) {
return resp.body;
}
}
bool peerTabShowNote(PeerTabIndex peerTabIndex) {
return peerTabIndex == PeerTabIndex.ab || peerTabIndex == PeerTabIndex.group;
}

View File

@@ -89,6 +89,7 @@ class PeerPayload {
"platform": _platform(p.info['os']),
"hostname": p.info['device_name'],
"device_group_name": p.device_group_name,
"note": p.note,
});
}

View File

@@ -466,6 +466,7 @@ class _AddressBookState extends State<AddressBook> {
IDTextEditingController idController = IDTextEditingController(text: '');
TextEditingController aliasController = TextEditingController(text: '');
TextEditingController passwordController = TextEditingController(text: '');
TextEditingController noteController = TextEditingController(text: '');
final tags = List.of(gFFI.abModel.currentAbTags);
var selectedTag = List<dynamic>.empty(growable: true).obs;
final style = TextStyle(fontSize: 14.0);
@@ -494,7 +495,11 @@ class _AddressBookState extends State<AddressBook> {
password = passwordController.text;
}
String? errMsg2 = await gFFI.abModel.addIdToCurrent(
id, aliasController.text.trim(), password, selectedTag);
id,
aliasController.text.trim(),
password,
selectedTag,
noteController.text);
if (errMsg2 != null) {
setState(() {
isInProgress = false;
@@ -600,6 +605,24 @@ class _AddressBookState extends State<AddressBook> {
),
).workaroundFreezeLinuxMint(),
)),
row(
label: Text(
translate('Note'),
style: style,
),
input: Obx(
() => TextField(
controller: noteController,
maxLines: 3,
minLines: 1,
maxLength: 300,
decoration: InputDecoration(
labelText: stateGlobal.isPortrait.isFalse
? null
: translate('Note'),
),
).workaroundFreezeLinuxMint(),
)),
if (gFFI.abModel.currentAbTags.isNotEmpty)
Align(
alignment: Alignment.centerLeft,

View File

@@ -1783,6 +1783,49 @@ void editAbTagDialog(
});
}
void editAbPeerNoteDialog(String id) {
var isInProgress = false;
final currentNote = gFFI.abModel.getPeerNote(id);
var controller = TextEditingController(text: currentNote);
gFFI.dialogManager.show((setState, close, context) {
submit() async {
setState(() {
isInProgress = true;
});
await gFFI.abModel.changeNote(id: id, note: controller.text);
close();
}
return CustomAlertDialog(
title: Text(translate("Edit note")),
content: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
controller: controller,
autofocus: true,
maxLines: 3,
minLines: 1,
maxLength: 300,
decoration: InputDecoration(
labelText: translate('Note'),
),
).workaroundFreezeLinuxMint(),
// NOT use Offstage to wrap LinearProgressIndicator
if (isInProgress) const LinearProgressIndicator(),
],
),
actions: [
dialogButton("Cancel", onPressed: close, isOutline: true),
dialogButton("OK", onPressed: submit),
],
onSubmit: submit,
onCancel: close,
);
});
}
void renameDialog(
{required String oldName,
FormFieldValidator<String>? validator,

View File

@@ -127,6 +127,10 @@ class _PeerCardState extends State<_PeerCard>
);
}
bool _showNote(Peer peer) {
return peerTabShowNote(widget.tab) && peer.note.isNotEmpty;
}
makeChild(bool isPortrait, Peer peer) {
final name = hideUsernameOnCard == true
? peer.hostname
@@ -134,6 +138,8 @@ class _PeerCardState extends State<_PeerCard>
final greyStyle = TextStyle(
fontSize: 11,
color: Theme.of(context).textTheme.titleLarge?.color?.withOpacity(0.6));
final showNote = _showNote(peer);
return Row(
mainAxisSize: MainAxisSize.max,
children: [
@@ -185,14 +191,44 @@ class _PeerCardState extends State<_PeerCard>
style: Theme.of(context).textTheme.titleSmall,
)),
]).marginOnly(top: isPortrait ? 0 : 2),
Align(
alignment: Alignment.centerLeft,
child: Text(
name,
style: isPortrait ? null : greyStyle,
textAlign: TextAlign.start,
overflow: TextOverflow.ellipsis,
),
Row(
children: [
Flexible(
child: Tooltip(
message: name,
waitDuration: const Duration(seconds: 1),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
name,
style: isPortrait ? null : greyStyle,
textAlign: TextAlign.start,
overflow: TextOverflow.ellipsis,
),
),
),
),
if (showNote)
Expanded(
child: Tooltip(
message: peer.note,
waitDuration: const Duration(seconds: 1),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
peer.note,
style: isPortrait ? null : greyStyle,
textAlign: TextAlign.start,
overflow: TextOverflow.ellipsis,
).marginOnly(
left: peerCardUiType.value ==
PeerUiType.list
? 32
: 4),
),
),
)
],
),
],
).marginOnly(top: 2),
@@ -278,7 +314,7 @@ class _PeerCardState extends State<_PeerCard>
padding: const EdgeInsets.all(6),
child:
getPlatformImage(peer.platform, size: 60),
).marginOnly(top: 4),
),
Row(
children: [
Expanded(
@@ -297,8 +333,26 @@ class _PeerCardState extends State<_PeerCard>
),
],
),
if (_showNote(peer))
Row(
children: [
Expanded(
child: Tooltip(
message: peer.note,
waitDuration: const Duration(seconds: 1),
child: Text(
peer.note,
style: const TextStyle(
color: Colors.white38,
fontSize: 10),
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
),
))
],
),
],
).paddingAll(4.0),
).paddingOnly(top: 4.0, left: 4.0, right: 4.0),
),
],
),
@@ -1134,6 +1188,7 @@ class AddressBookPeerCard extends BasePeerCard {
if (gFFI.abModel.currentAbTags.isNotEmpty) {
menuItems.add(_editTagAction(peer.id));
}
menuItems.add(_editNoteAction(peer.id));
}
final addressbooks = gFFI.abModel.addressBooksCanWrite();
if (gFFI.peerTabModel.currentTab == PeerTabIndex.ab.index) {
@@ -1173,6 +1228,21 @@ class AddressBookPeerCard extends BasePeerCard {
);
}
@protected
MenuEntryBase<String> _editNoteAction(String id) {
return MenuEntryButton<String>(
childBuilder: (TextStyle? style) => Text(
translate('Edit note'),
style: style,
),
proc: () {
editAbPeerNoteDialog(id);
},
padding: super.menuPadding,
dismissOnClicked: true,
);
}
@protected
@override
Future<String> _getAlias(String id) async =>

View File

@@ -71,10 +71,12 @@ class _PeersView extends StatefulWidget {
final Peers peers;
final PeerFilter? peerFilter;
final PeerCardBuilder peerCardBuilder;
final PeerTabIndex peerTabIndex;
const _PeersView(
{required this.peers,
required this.peerCardBuilder,
required this.peerTabIndex,
this.peerFilter,
Key? key})
: super(key: key);
@@ -395,8 +397,8 @@ class _PeersViewState extends State<_PeersView>
return peers;
}
searchText = searchText.toLowerCase();
final matches =
await Future.wait(peers.map((peer) => matchPeer(searchText, peer)));
final matches = await Future.wait(
peers.map((peer) => matchPeer(searchText, peer, widget.peerTabIndex)));
final filteredList = List<Peer>.empty(growable: true);
for (var i = 0; i < peers.length; i++) {
if (matches[i]) {
@@ -441,7 +443,10 @@ abstract class BasePeersView extends StatelessWidget {
break;
}
return _PeersView(
peers: peers, peerFilter: peerFilter, peerCardBuilder: peerCardBuilder);
peers: peers,
peerFilter: peerFilter,
peerCardBuilder: peerCardBuilder,
peerTabIndex: peerTabIndex);
}
}

View File

@@ -374,6 +374,7 @@ class _ConnectionPageState extends State<ConnectionPage>
rdpUsername: '',
loginName: '',
device_group_name: '',
note: '',
);
_autocompleteOpts = [emptyPeer];
} else {
@@ -536,64 +537,68 @@ class _ConnectionPageState extends State<ConnectionPage>
builder: (context, setState) {
var offset = Offset(0, 0);
return Obx(() => InkWell(
child: _menuOpen.value
? Transform.rotate(
angle: pi,
child: Icon(IconFont.more, size: 14),
child: _menuOpen.value
? Transform.rotate(
angle: pi,
child: Icon(IconFont.more, size: 14),
)
: Icon(IconFont.more, size: 14),
onTapDown: (e) {
offset = e.globalPosition;
},
onTap: () async {
_menuOpen.value = true;
final x = offset.dx;
final y = offset.dy;
await mod_menu
.showMenu(
context: context,
position: RelativeRect.fromLTRB(x, y, x, y),
items: [
(
'Transfer file',
() => onConnect(isFileTransfer: true)
),
(
'View camera',
() => onConnect(isViewCamera: true)
),
(
'${translate('Terminal')} (beta)',
() => onConnect(isTerminal: true)
),
]
.map((e) => MenuEntryButton<String>(
childBuilder: (TextStyle? style) =>
Text(
translate(e.$1),
style: style,
),
proc: () => e.$2(),
padding: EdgeInsets.symmetric(
horizontal:
kDesktopMenuPadding.left),
dismissOnClicked: true,
))
.map((e) => e.build(
context,
const MenuConfig(
commonColor: CustomPopupMenuTheme
.commonColor,
height:
CustomPopupMenuTheme.height,
dividerHeight:
CustomPopupMenuTheme
.dividerHeight)))
.expand((i) => i)
.toList(),
elevation: 8,
)
: Icon(IconFont.more, size: 14),
onTapDown: (e) {
offset = e.globalPosition;
},
onTap: () async {
_menuOpen.value = true;
final x = offset.dx;
final y = offset.dy;
await mod_menu
.showMenu(
context: context,
position: RelativeRect.fromLTRB(x, y, x, y),
items: [
(
'Transfer file',
() => onConnect(isFileTransfer: true)
),
(
'View camera',
() => onConnect(isViewCamera: true)
),
(
'${translate('Terminal')} (beta)',
() => onConnect(isTerminal: true)
),
]
.map((e) => MenuEntryButton<String>(
childBuilder: (TextStyle? style) => Text(
translate(e.$1),
style: style,
),
proc: () => e.$2(),
padding: EdgeInsets.symmetric(
horizontal: kDesktopMenuPadding.left),
dismissOnClicked: true,
))
.map((e) => e.build(
context,
const MenuConfig(
commonColor:
CustomPopupMenuTheme.commonColor,
height: CustomPopupMenuTheme.height,
dividerHeight: CustomPopupMenuTheme
.dividerHeight)))
.expand((i) => i)
.toList(),
elevation: 8,
)
.then((_) {
_menuOpen.value = false;
});
},
));
.then((_) {
_menuOpen.value = false;
});
},
));
},
),
),

View File

@@ -182,6 +182,7 @@ class _ConnectionPageState extends State<ConnectionPage> {
rdpUsername: '',
loginName: '',
device_group_name: '',
note: '',
);
_autocompleteOpts = [emptyPeer];
} else {

View File

@@ -319,8 +319,8 @@ class AbModel {
// #endregion
// #region peer
Future<String?> addIdToCurrent(
String id, String alias, String password, List<dynamic> tags) async {
Future<String?> addIdToCurrent(String id, String alias, String password,
List<dynamic> tags, String note) async {
if (currentAbPeers.where((element) => element.id == id).isNotEmpty) {
return "$id already exists in address book $_currentName";
}
@@ -333,6 +333,9 @@ class AbModel {
if (password.isNotEmpty) {
peer['password'] = password;
}
if (note.isNotEmpty) {
peer['note'] = note;
}
final ret = await addPeersTo([peer], _currentName.value);
_syncAllFromRecent = true;
return ret;
@@ -376,6 +379,14 @@ class AbModel {
return res;
}
Future<bool> changeNote({required String id, required String note}) async {
bool res = await current.changeNote(id: id, note: note);
await pullNonLegacyAfterChange();
currentAbPeers.refresh();
// no need to save cache
return res;
}
Future<bool> changePersonalHashPassword(String id, String hash) async {
var ret = false;
final personalAb = addressbooks[_personalAddressBookName];
@@ -658,6 +669,15 @@ class AbModel {
}
}
String getPeerNote(String id) {
final it = currentAbPeers.where((p0) => p0.id == id);
if (it.isEmpty) {
return '';
} else {
return it.first.note;
}
}
Color getCurrentAbTagColor(String tag) {
if (tag == kUntagged) {
return MyTheme.accent;
@@ -863,6 +883,8 @@ abstract class BaseAb {
Future<bool> changeAlias({required String id, required String alias});
Future<bool> changeNote({required String id, required String note});
Future<bool> changePersonalHashPassword(String id, String hash);
Future<bool> changeSharedPassword(String id, String password);
@@ -1090,6 +1112,12 @@ class LegacyAb extends BaseAb {
return await pushAb();
}
@override
Future<bool> changeNote({required String id, required String note}) async {
// no need to implement
return false;
}
@override
Future<bool> changeSharedPassword(String id, String password) async {
// no need to implement
@@ -1549,6 +1577,27 @@ class Ab extends BaseAb {
}
}
@override
Future<bool> changeNote({required String id, required String note}) async {
try {
final api =
"${await bind.mainGetApiServer()}/api/ab/peer/update/${profile.guid}";
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
final body = jsonEncode({"id": id, "note": note});
final resp = await http.put(Uri.parse(api), headers: headers, body: body);
final errMsg = _jsonDecodeActionResp(resp);
if (errMsg.isNotEmpty) {
BotToast.showText(contentColor: Colors.red, text: errMsg);
return false;
}
return true;
} catch (err) {
debugPrint('changeNote err: ${err.toString()}');
return false;
}
}
Future<bool> _setPassword(Object bodyContent) async {
try {
final api =
@@ -1815,6 +1864,11 @@ class DummyAb extends BaseAb {
return false;
}
@override
Future<bool> changeNote({required String id, required String note}) async {
return false;
}
@override
Future<bool> changePersonalHashPassword(String id, String hash) async {
return false;

View File

@@ -20,6 +20,7 @@ class Peer {
bool online = false;
String loginName; //login username
String device_group_name;
String note;
bool? sameServer;
String getId() {
@@ -43,6 +44,7 @@ class Peer {
rdpUsername = json['rdpUsername'] ?? '',
loginName = json['loginName'] ?? '',
device_group_name = json['device_group_name'] ?? '',
note = json['note'] is String ? json['note'] : '',
sameServer = json['same_server'];
Map<String, dynamic> toJson() {
@@ -60,6 +62,7 @@ class Peer {
"rdpUsername": rdpUsername,
'loginName': loginName,
'device_group_name': device_group_name,
'note': note,
'same_server': sameServer,
};
}
@@ -104,6 +107,7 @@ class Peer {
required this.rdpUsername,
required this.loginName,
required this.device_group_name,
required this.note,
this.sameServer,
});
@@ -122,6 +126,7 @@ class Peer {
rdpUsername: '',
loginName: '',
device_group_name: '',
note: '',
);
bool equal(Peer other) {
return id == other.id &&
@@ -136,7 +141,8 @@ class Peer {
rdpPort == other.rdpPort &&
rdpUsername == other.rdpUsername &&
device_group_name == other.device_group_name &&
loginName == other.loginName;
loginName == other.loginName &&
note == other.note;
}
Peer.copy(Peer other)
@@ -154,6 +160,7 @@ class Peer {
rdpUsername: other.rdpUsername,
loginName: other.loginName,
device_group_name: other.device_group_name,
note: other.note,
sameServer: other.sameServer);
}