diff --git a/flutter/lib/common/shared_state.dart b/flutter/lib/common/shared_state.dart index 4cfe219e4..5ae618dfe 100644 --- a/flutter/lib/common/shared_state.dart +++ b/flutter/lib/common/shared_state.dart @@ -201,3 +201,25 @@ class RemoteCountState { static RxInt find() => Get.find(tag: tag()); } + +class PeerStringOption { + static String tag(String id, String opt) => 'peer_{$opt}_$id'; + + static void init(String id, String opt, String Function() init_getter) { + final key = tag(id, opt); + if (!Get.isRegistered(tag: key)) { + final RxString value = RxString(init_getter()); + Get.put(value, tag: key); + } + } + + static void delete(String id, String opt) { + final key = tag(id, opt); + if (Get.isRegistered(tag: key)) { + Get.delete(tag: key); + } + } + + static RxString find(String id, String opt) => + Get.find(tag: tag(id, opt)); +} diff --git a/flutter/lib/common/widgets/peer_card.dart b/flutter/lib/common/widgets/peer_card.dart index c99cf2e68..5796c7d20 100644 --- a/flutter/lib/common/widgets/peer_card.dart +++ b/flutter/lib/common/widgets/peer_card.dart @@ -584,7 +584,7 @@ abstract class BasePeerCard extends StatelessWidget { submit() async { isInProgress.value = true; name = controller.text; - await bind.mainSetPeerOption(id: id, key: 'alias', value: name); + await bind.mainSetPeerAlias(id: id, alias: name); if (isAddressBook) { gFFI.abModel.setPeerAlias(id, name); await gFFI.abModel.pushAb(); diff --git a/flutter/lib/desktop/pages/file_manager_tab_page.dart b/flutter/lib/desktop/pages/file_manager_tab_page.dart index d0f6c800e..7e07eaa9a 100644 --- a/flutter/lib/desktop/pages/file_manager_tab_page.dart +++ b/flutter/lib/desktop/pages/file_manager_tab_page.dart @@ -83,6 +83,7 @@ class _FileManagerTabPageState extends State { controller: tabController, onWindowCloseButton: handleWindowCloseButton, tail: const AddButton().paddingOnly(left: 10), + labelGetter: DesktopTab.labelGetterAlias, )), ); return Platform.isMacOS diff --git a/flutter/lib/desktop/pages/port_forward_tab_page.dart b/flutter/lib/desktop/pages/port_forward_tab_page.dart index 403afe343..d4c0a86f8 100644 --- a/flutter/lib/desktop/pages/port_forward_tab_page.dart +++ b/flutter/lib/desktop/pages/port_forward_tab_page.dart @@ -94,6 +94,7 @@ class _PortForwardTabPageState extends State { return true; }, tail: AddButton().paddingOnly(left: 10), + labelGetter: DesktopTab.labelGetterAlias, )), ); return Platform.isMacOS diff --git a/flutter/lib/desktop/pages/remote_tab_page.dart b/flutter/lib/desktop/pages/remote_tab_page.dart index 4f7e9c5aa..5e49895f4 100644 --- a/flutter/lib/desktop/pages/remote_tab_page.dart +++ b/flutter/lib/desktop/pages/remote_tab_page.dart @@ -128,6 +128,7 @@ class _ConnectionTabPageState extends State { onWindowCloseButton: handleWindowCloseButton, tail: const AddButton().paddingOnly(left: 10), pageViewBuilder: (pageView) => pageView, + labelGetter: DesktopTab.labelGetterAlias, tabBuilder: (key, icon, label, themeConf) => Obx(() { final connectionType = ConnectionTypeState.find(key); if (!connectionType.isValid()) { diff --git a/flutter/lib/desktop/widgets/tabbar_widget.dart b/flutter/lib/desktop/widgets/tabbar_widget.dart index b9e126c61..506399e6f 100644 --- a/flutter/lib/desktop/widgets/tabbar_widget.dart +++ b/flutter/lib/desktop/widgets/tabbar_widget.dart @@ -9,6 +9,7 @@ import 'package:flutter/material.dart' hide TabBarTheme; import 'package:flutter_hbb/common.dart'; import 'package:flutter_hbb/consts.dart'; import 'package:flutter_hbb/main.dart'; +import 'package:flutter_hbb/common/shared_state.dart'; import 'package:flutter_hbb/models/platform_model.dart'; import 'package:flutter_hbb/models/state_model.dart'; import 'package:get/get.dart'; @@ -252,6 +253,15 @@ class DesktopTab extends StatelessWidget { tabType == DesktopTabType.main || tabType == DesktopTabType.cm; } + static RxString labelGetterAlias(String peerId) { + final opt = 'alias'; + PeerStringOption.init(peerId, opt, () { + final alias = bind.mainGetPeerOptionSync(id: peerId, key: opt); + return alias.isEmpty ? peerId : alias; + }); + return PeerStringOption.find(peerId, opt); + } + @override Widget build(BuildContext context) { return Column(children: [ diff --git a/flutter/lib/models/model.dart b/flutter/lib/models/model.dart index 616a1c985..53a040eed 100644 --- a/flutter/lib/models/model.dart +++ b/flutter/lib/models/model.dart @@ -15,6 +15,7 @@ import 'package:flutter_hbb/models/file_model.dart'; import 'package:flutter_hbb/models/server_model.dart'; import 'package:flutter_hbb/models/user_model.dart'; import 'package:flutter_hbb/models/state_model.dart'; +import 'package:flutter_hbb/common/shared_state.dart'; import 'package:flutter_hbb/utils/multi_window_manager.dart'; import 'package:tuple/tuple.dart'; import 'package:image/image.dart' as img2; @@ -189,6 +190,8 @@ class FfiModel with ChangeNotifier { rustDeskWinManager.newRemoteDesktop(arg); }); } + } else if (name == 'alias') { + handleAliasChanged(evt); } }; } @@ -198,6 +201,13 @@ class FfiModel with ChangeNotifier { platformFFI.setEventCallback(startEventListener(peerId)); } + handleAliasChanged(Map evt) { + final rxAlias = PeerStringOption.find(evt['id'], 'alias'); + if (rxAlias.value != evt['alias']) { + rxAlias.value = evt['alias']; + } + } + handleSwitchDisplay(Map evt) { final oldOrientation = _display.width > _display.height; var old = _pi.currentDisplay; @@ -927,7 +937,7 @@ class CursorModel with ChangeNotifier { // my throw exception, because the listener maybe already dispose notifyListeners(); } catch (e) { - debugPrint('notify cursor: $e'); + debugPrint('WARNING: updateCursorId $id, without notifyListeners(). $e'); } } @@ -980,6 +990,9 @@ class CursorModel with ChangeNotifier { _hotx = tmp.item2; _hoty = tmp.item3; notifyListeners(); + } else { + debugPrint( + 'WARNING: updateCursorId $id, cache is ${_cache == null ? "null" : "not null"}. without notifyListeners()'); } } diff --git a/src/flutter_ffi.rs b/src/flutter_ffi.rs index 21603227f..a0f94fdbd 100644 --- a/src/flutter_ffi.rs +++ b/src/flutter_ffi.rs @@ -592,6 +592,11 @@ pub fn main_set_peer_option_sync(id: String, key: String, value: String) -> Sync SyncReturn(true) } +pub fn main_set_peer_alias(id: String, alias: String) { + main_broadcast_message(&HashMap::from([("name", "alias"), ("id", &id), ("alias", &alias)])); + set_peer_option(id, "alias".to_owned(), alias) +} + pub fn main_forget_password(id: String) { forget_password(id) }