peer tab recorder

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2022-12-04 17:44:33 +08:00
parent be74f90334
commit 9bbe236651

@ -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';
@ -30,8 +31,9 @@ class _TabEntry {
class _PeerTabPageState extends State<PeerTabPage> class _PeerTabPageState extends State<PeerTabPage>
with SingleTickerProviderStateMixin { with SingleTickerProviderStateMixin {
late final RxInt _tabHiddenFlag; late final RxInt tabHiddenFlag;
late final RxString _currentTab; late final RxString currentTab;
late final RxList<String> visibleOrderedTabs;
final List<_TabEntry> entries = [ final List<_TabEntry> entries = [
_TabEntry( _TabEntry(
'Recent Sessions', 'Recent Sessions',
@ -61,12 +63,30 @@ class _PeerTabPageState extends State<PeerTabPage>
@override @override
void initState() { void initState() {
_tabHiddenFlag = (int.tryParse( tabHiddenFlag = (int.tryParse(
bind.getLocalFlutterConfig(k: 'hidden-peer-card'), bind.getLocalFlutterConfig(k: 'hidden-peer-card'),
radix: 2) ?? radix: 2) ??
0) 0)
.obs; .obs;
_currentTab = bind.getLocalFlutterConfig(k: 'current-peer-tab').obs; currentTab = bind.getLocalFlutterConfig(k: 'current-peer-tab').obs;
visibleOrderedTabs = entries
.where((e) => !isTabHidden(e.name))
.map((e) => e.name)
.toList()
.obs;
try {
final json = jsonDecode(bind.getLocalFlutterConfig(k: 'peer-tab-order'));
if (json is List) {
final List<String> list = json.map((e) => e.toString()).toList();
if (list.length == visibleOrderedTabs.length &&
visibleOrderedTabs.every((e) => list.contains(e))) {
visibleOrderedTabs.value = list;
}
}
} catch (e) {
debugPrint('$e');
}
adjustTab(); adjustTab();
final uiType = bind.getLocalFlutterConfig(k: 'peer-card-ui-type'); final uiType = bind.getLocalFlutterConfig(k: 'peer-card-ui-type');
@ -78,9 +98,8 @@ class _PeerTabPageState extends State<PeerTabPage>
super.initState(); super.initState();
} }
// hard code for now
Future<void> handleTabSelection(String tabName) async { Future<void> handleTabSelection(String tabName) async {
_currentTab.value = tabName; currentTab.value = tabName;
await bind.setLocalFlutterConfig(k: 'current-peer-tab', v: tabName); await bind.setLocalFlutterConfig(k: 'current-peer-tab', v: tabName);
entries.firstWhereOrNull((e) => e.name == tabName)?.load(); entries.firstWhereOrNull((e) => e.name == tabName)?.load();
} }
@ -122,16 +141,34 @@ 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(() => ListView( return Obx(() {
int indexCounter = -1;
return ReorderableListView(
buildDefaultDragHandles: false,
onReorder: (oldIndex, newIndex) {
var list = visibleOrderedTabs.toList();
if (oldIndex < newIndex) {
newIndex -= 1;
}
final String item = list.removeAt(oldIndex);
list.insert(newIndex, item);
bind.setLocalFlutterConfig(
k: 'peer-tab-order', v: jsonEncode(list));
visibleOrderedTabs.value = list;
},
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
shrinkWrap: true, shrinkWrap: true,
controller: ScrollController(), scrollController: ScrollController(),
children: entries.where((e) => !isTabHidden(e.name)).map((t) { children: visibleOrderedTabs.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(
color: _currentTab.value == t.name color: currentTab.value == t
? Theme.of(context).backgroundColor ? Theme.of(context).backgroundColor
: null, : null,
borderRadius: BorderRadius.circular(isDesktop ? 2 : 6), borderRadius: BorderRadius.circular(isDesktop ? 2 : 6),
@ -139,28 +176,27 @@ class _PeerTabPageState extends State<PeerTabPage>
child: Align( child: Align(
alignment: Alignment.center, alignment: Alignment.center,
child: Text( child: Text(
translate(t.name), translate(t),
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: TextStyle( style: TextStyle(
height: 1, height: 1,
fontSize: 14, fontSize: 14,
color: color: currentTab.value == t ? textColor : textColor
_currentTab.value == t.name ? textColor : textColor
?..withOpacity(0.5)), ?..withOpacity(0.5)),
), ),
)), )),
onTap: () async => await handleTabSelection(t.name), onTap: () async => await handleTabSelection(t),
),
); );
}).toList())); }).toList());
});
} }
Widget _createPeersView() { Widget _createPeersView() {
final verticalMargin = isDesktop ? 12.0 : 6.0; final verticalMargin = isDesktop ? 12.0 : 6.0;
return Expanded( return Expanded(
child: Obx(() => child: Obx(() =>
entries entries.firstWhereOrNull((e) => e.name == currentTab.value)?.widget ??
.firstWhereOrNull((e) => e.name == _currentTab.value)
?.widget ??
visibleContextMenuListener(Center( visibleContextMenuListener(Center(
child: Text(translate('Right click to select tabs')), child: Text(translate('Right click to select tabs')),
))).marginSymmetric(vertical: verticalMargin), ))).marginSymmetric(vertical: verticalMargin),
@ -200,21 +236,19 @@ class _PeerTabPageState extends State<PeerTabPage>
bool isTabHidden(String name) { bool isTabHidden(String name) {
int index = entries.indexWhere((e) => e.name == name); int index = entries.indexWhere((e) => e.name == name);
if (index >= 0) { if (index >= 0) {
return _tabHiddenFlag & (1 << index) != 0; return tabHiddenFlag & (1 << index) != 0;
} }
assert(false); assert(false);
return false; return false;
} }
adjustTab() { adjustTab() {
List<String> visibleTabs = if (visibleOrderedTabs.isNotEmpty) {
entries.where((e) => !isTabHidden(e.name)).map((e) => e.name).toList(); if (!visibleOrderedTabs.contains(currentTab.value)) {
if (visibleTabs.isNotEmpty) { handleTabSelection(visibleOrderedTabs[0]);
if (!visibleTabs.contains(_currentTab.value)) {
handleTabSelection(visibleTabs[0]);
} }
} else { } else {
_currentTab.value = ''; currentTab.value = '';
} }
} }
@ -243,17 +277,25 @@ class _PeerTabPageState extends State<PeerTabPage>
switchType: SwitchType.scheckbox, switchType: SwitchType.scheckbox,
text: translate(e.value.name), text: translate(e.value.name),
getter: () async { getter: () async {
return _tabHiddenFlag.value & bitMask == 0; return tabHiddenFlag.value & bitMask == 0;
}, },
setter: (show) async { setter: (show) async {
if (show) { if (show) {
_tabHiddenFlag.value &= ~bitMask; tabHiddenFlag.value &= ~bitMask;
} else { } else {
_tabHiddenFlag.value |= bitMask; tabHiddenFlag.value |= bitMask;
} }
await bind.setLocalFlutterConfig( await bind.setLocalFlutterConfig(
k: 'hidden-peer-card', k: 'hidden-peer-card', v: tabHiddenFlag.value.toRadixString(2));
v: _tabHiddenFlag.value.toRadixString(2)); visibleOrderedTabs.removeWhere((e) => isTabHidden(e));
visibleOrderedTabs.addAll(entries
.where((e) =>
!visibleOrderedTabs.contains(e.name) &&
!isTabHidden(e.name))
.map((e) => e.name)
.toList());
await bind.setLocalFlutterConfig(
k: 'peer-tab-order', v: jsonEncode(visibleOrderedTabs));
cancelFunc(); cancelFunc();
adjustTab(); adjustTab();
}); });