Merge pull request #3051 from 21pages/reorder-tab

recover reordered peer tab
This commit is contained in:
RustDesk 2023-02-03 15:04:17 +08:00 committed by GitHub
commit dbb5eb0c8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'package:bot_toast/bot_toast.dart'; import 'package:bot_toast/bot_toast.dart';
@ -20,7 +21,9 @@ const int groupTabIndex = 4;
const String defaultGroupTabname = 'Group'; const String defaultGroupTabname = 'Group';
class StatePeerTab { class StatePeerTab {
final RxInt currentTab = 0.obs; final RxInt currentTab = 0.obs; // index in tabNames
final RxList<int> visibleOrderedTabs = RxList.empty(growable: true);
List<int> tabOrder = List.from([0, 1, 2, 3, 4]); // constant length
final RxInt tabHiddenFlag = 0.obs; final RxInt tabHiddenFlag = 0.obs;
final RxList<String> tabNames = [ final RxList<String> tabNames = [
'Recent Sessions', 'Recent Sessions',
@ -31,53 +34,80 @@ class StatePeerTab {
].obs; ].obs;
StatePeerTab._() { StatePeerTab._() {
// init tabHiddenFlag
tabHiddenFlag.value = (int.tryParse( tabHiddenFlag.value = (int.tryParse(
bind.getLocalFlutterConfig(k: 'hidden-peer-card'), bind.getLocalFlutterConfig(k: 'hidden-peer-card'),
radix: 2) ?? radix: 2) ??
0); 0);
var tabs = _notHiddenTabs(); var tabs = _notHiddenTabs();
// remove dynamic tabs
tabs.remove(groupTabIndex);
// init tabOrder
try {
final conf = bind.getLocalFlutterConfig(k: 'peer-tab-order');
if (conf.isNotEmpty) {
final json = jsonDecode(conf);
if (json is List) {
final List<int> list =
json.map((e) => int.tryParse(e.toString()) ?? -1).toList();
if (list.length == tabOrder.length &&
tabOrder.every((e) => list.contains(e))) {
tabOrder = list;
}
}
}
} catch (e) {
debugPrintStack(label: '$e');
}
// init visibleOrderedTabs
var tempList = tabOrder.toList();
tempList.removeWhere((e) => !tabs.contains(e));
visibleOrderedTabs.value = tempList;
// init currentTab
currentTab.value = currentTab.value =
int.tryParse(bind.getLocalFlutterConfig(k: 'peer-tab-index')) ?? 0; int.tryParse(bind.getLocalFlutterConfig(k: 'peer-tab-index')) ?? 0;
if (!tabs.contains(currentTab.value)) { if (!tabs.contains(currentTab.value)) {
if (tabs.isNotEmpty) {
currentTab.value = tabs[0];
} else {
currentTab.value = 0; currentTab.value = 0;
} }
} }
}
static final StatePeerTab instance = StatePeerTab._(); static final StatePeerTab instance = StatePeerTab._();
// check dynamic tabs
check() { check() {
var tabs = _notHiddenTabs(); tabOrder2visibleOrderedTabs();
if (filterGroupCard()) { if (visibleOrderedTabs.contains(groupTabIndex) &&
if (currentTab.value == groupTabIndex) {
currentTab.value =
tabs.firstWhereOrNull((e) => e != groupTabIndex) ?? 0;
bind.setLocalFlutterConfig(
k: 'peer-tab-index', v: currentTab.value.toString());
}
} else {
if (gFFI.userModel.isAdmin.isFalse &&
gFFI.userModel.groupName.isNotEmpty) {
tabNames[groupTabIndex] = gFFI.userModel.groupName.value;
} else {
tabNames[groupTabIndex] = defaultGroupTabname;
}
if (tabs.contains(groupTabIndex) &&
int.tryParse(bind.getLocalFlutterConfig(k: 'peer-tab-index')) == int.tryParse(bind.getLocalFlutterConfig(k: 'peer-tab-index')) ==
groupTabIndex) { groupTabIndex) {
currentTab.value = groupTabIndex; currentTab.value = groupTabIndex;
} }
if (gFFI.userModel.isAdmin.isFalse && gFFI.userModel.groupName.isNotEmpty) {
tabNames[groupTabIndex] = gFFI.userModel.groupName.value;
} else {
tabNames[groupTabIndex] = defaultGroupTabname;
} }
} }
List<int> currentTabs() { visibleOrderedTabs2TabOrder() {
var v = List<int>.empty(growable: true); var tmpTabOrder = visibleOrderedTabs.toList();
for (int i = 0; i < tabNames.length; i++) { var left = tabOrder.where((e) => !tmpTabOrder.contains(e)).toList();
if (!_isTabHidden(i) && !_isTabFilter(i)) { for (var t in left) {
v.add(i); _addTabInOrder(tmpTabOrder, t);
} }
} statePeerTab.tabOrder = tmpTabOrder;
return v; bind.setLocalFlutterConfig(k: 'peer-tab-order', v: jsonEncode(tmpTabOrder));
} }
tabOrder2visibleOrderedTabs() {
var visible = statePeerTab.visibleTabs();
statePeerTab.visibleOrderedTabs.value =
statePeerTab.tabOrder.where((e) => visible.contains(e)).toList();
}
// return true if hide group card
bool filterGroupCard() { bool filterGroupCard() {
if (gFFI.groupModel.users.isEmpty || if (gFFI.groupModel.users.isEmpty ||
(gFFI.userModel.isAdmin.isFalse && gFFI.userModel.groupName.isEmpty)) { (gFFI.userModel.isAdmin.isFalse && gFFI.userModel.groupName.isEmpty)) {
@ -87,6 +117,17 @@ class StatePeerTab {
} }
} }
// return index array of tabNames
List<int> visibleTabs() {
var v = List<int>.empty(growable: true);
for (int i = 0; i < tabNames.length; i++) {
if (!_isTabHidden(i) && !_isTabFilter(i)) {
v.add(i);
}
}
return v;
}
bool _isTabHidden(int tabindex) { bool _isTabHidden(int tabindex) {
return tabHiddenFlag & (1 << tabindex) != 0; return tabHiddenFlag & (1 << tabindex) != 0;
} }
@ -107,6 +148,41 @@ class StatePeerTab {
} }
return v; return v;
} }
// add tabIndex to list
_addTabInOrder(List<int> list, int tabIndex) {
if (!tabOrder.contains(tabIndex) || list.contains(tabIndex)) {
return;
}
bool sameOrder = true;
int lastIndex = -1;
for (int i = 0; i < list.length; i++) {
var index = tabOrder.lastIndexOf(list[i]);
if (index > lastIndex) {
lastIndex = index;
continue;
} else {
sameOrder = false;
break;
}
}
if (sameOrder) {
var indexInTabOrder = tabOrder.indexOf(tabIndex);
var left = List.empty(growable: true);
for (int i = 0; i < indexInTabOrder; i++) {
left.add(tabOrder[i]);
}
int insertIndex = list.lastIndexWhere((e) => left.contains(e));
if (insertIndex < 0) {
insertIndex = 0;
} else {
insertIndex += 1;
}
list.insert(insertIndex, tabIndex);
} else {
list.add(tabIndex);
}
}
} }
final statePeerTab = StatePeerTab.instance; final statePeerTab = StatePeerTab.instance;
@ -177,11 +253,6 @@ class _PeerTabPageState extends State<PeerTabPage>
} }
} }
@override
void dispose() {
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(
@ -215,13 +286,29 @@ class _PeerTabPageState extends State<PeerTabPage>
Widget _createSwitchBar(BuildContext context) { Widget _createSwitchBar(BuildContext context) {
final textColor = Theme.of(context).textTheme.titleLarge?.color; final textColor = Theme.of(context).textTheme.titleLarge?.color;
return Obx(() { return Obx(() {
var tabs = statePeerTab.currentTabs(); var tabs = statePeerTab.visibleOrderedTabs;
return ListView( int indexCounter = -1;
return ReorderableListView(
buildDefaultDragHandles: false,
onReorder: (oldIndex, newIndex) {
if (oldIndex < newIndex) {
newIndex -= 1;
}
var list = tabs.toList();
final int item = list.removeAt(oldIndex);
list.insert(newIndex, item);
tabs.value = list;
statePeerTab.visibleOrderedTabs2TabOrder();
},
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
physics: NeverScrollableScrollPhysics(), physics: NeverScrollableScrollPhysics(),
controller: ScrollController(), scrollController: ScrollController(),
children: tabs.map((t) { children: tabs.map((t) {
return InkWell( indexCounter++;
return ReorderableDragStartListener(
key: ValueKey(t),
index: indexCounter,
child: InkWell(
child: Container( child: Container(
padding: const EdgeInsets.symmetric(horizontal: 8), padding: const EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration( decoration: BoxDecoration(
@ -249,6 +336,7 @@ class _PeerTabPageState extends State<PeerTabPage>
await bind.setLocalFlutterConfig( await bind.setLocalFlutterConfig(
k: 'peer-tab-index', v: t.toString()); k: 'peer-tab-index', v: t.toString());
}, },
),
); );
}).toList()); }).toList());
}); });
@ -275,7 +363,7 @@ class _PeerTabPageState extends State<PeerTabPage>
final verticalMargin = isDesktop ? 12.0 : 6.0; final verticalMargin = isDesktop ? 12.0 : 6.0;
return Expanded( return Expanded(
child: Obx(() { child: Obx(() {
var tabs = statePeerTab.currentTabs(); var tabs = statePeerTab.visibleOrderedTabs;
if (tabs.isEmpty) { if (tabs.isEmpty) {
return visibleContextMenuListener(Center( return visibleContextMenuListener(Center(
child: Text(translate('Right click to select tabs')), child: Text(translate('Right click to select tabs')),
@ -322,7 +410,7 @@ class _PeerTabPageState extends State<PeerTabPage>
} }
adjustTab() { adjustTab() {
var tabs = statePeerTab.currentTabs(); var tabs = statePeerTab.visibleOrderedTabs;
if (tabs.isNotEmpty && !tabs.contains(statePeerTab.currentTab.value)) { if (tabs.isNotEmpty && !tabs.contains(statePeerTab.currentTab.value)) {
statePeerTab.currentTab.value = tabs[0]; statePeerTab.currentTab.value = tabs[0];
} }
@ -349,11 +437,13 @@ class _PeerTabPageState extends State<PeerTabPage>
Widget visibleContextMenu(CancelFunc cancelFunc) { Widget visibleContextMenu(CancelFunc cancelFunc) {
return Obx(() { return Obx(() {
final List<MenuEntryBase> menu = List.empty(growable: true); final List<MenuEntryBase> menu = List.empty(growable: true);
final List<int> menuIndex = List.empty(growable: true);
for (int i = 0; i < statePeerTab.tabNames.length; i++) { for (int i = 0; i < statePeerTab.tabNames.length; i++) {
if (i == groupTabIndex && statePeerTab.filterGroupCard()) { if (i == groupTabIndex && statePeerTab.filterGroupCard()) {
continue; continue;
} }
int bitMask = 1 << i; int bitMask = 1 << i;
menuIndex.add(i);
menu.add(MenuEntrySwitch( menu.add(MenuEntrySwitch(
switchType: SwitchType.scheckbox, switchType: SwitchType.scheckbox,
text: translatedTabname(i), text: translatedTabname(i),
@ -369,12 +459,21 @@ class _PeerTabPageState extends State<PeerTabPage>
await bind.setLocalFlutterConfig( await bind.setLocalFlutterConfig(
k: 'hidden-peer-card', k: 'hidden-peer-card',
v: statePeerTab.tabHiddenFlag.value.toRadixString(2)); v: statePeerTab.tabHiddenFlag.value.toRadixString(2));
statePeerTab.tabOrder2visibleOrderedTabs();
cancelFunc(); cancelFunc();
adjustTab(); adjustTab();
})); }));
} }
// show in tabOrder
List<MenuEntryBase> menu2 = List.empty(growable: true);
statePeerTab.tabOrder.map((e) {
final index = menuIndex.indexOf(e);
if (index >= 0) {
menu2.add(menu[index]);
}
}).toList();
return mod_menu.PopupMenu( return mod_menu.PopupMenu(
items: menu items: menu2
.map((entry) => entry.build( .map((entry) => entry.build(
context, context,
const MenuConfig( const MenuConfig(
@ -421,7 +520,9 @@ class _PeerSearchBarState extends State<PeerSearchBar> {
FocusNode focusNode = FocusNode(); FocusNode focusNode = FocusNode();
focusNode.addListener(() { focusNode.addListener(() {
focused.value = focusNode.hasFocus; focused.value = focusNode.hasFocus;
peerSearchTextController.selection = TextSelection(baseOffset: 0, extentOffset: peerSearchTextController.value.text.length); peerSearchTextController.selection = TextSelection(
baseOffset: 0,
extentOffset: peerSearchTextController.value.text.length);
}); });
return Container( return Container(
width: 120, width: 120,