Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat(ui): redesign the keyboard shortcut settings page

Predidit3aa3c470d1f9
diff --git a/lib/pages/settings/keyboard_settings.dart b/lib/pages/settings/keyboard_settings.dart
index 3eed858a..0385176e 100644
--- a/lib/pages/settings/keyboard_settings.dart
+++ b/lib/pages/settings/keyboard_settings.dart
@@ -5,6 +5,27 @@ import 'package:kazumi/utils/constants.dart';
import 'package:kazumi/bean/appbar/sys_app_bar.dart';
import 'package:kazumi/bean/dialog/dialog_helper.dart';
+/// Display group for the shortcut list. Functions missing from every group
+/// fall back to a trailing "其他" group so new shortcuts never disappear.
+class _ShortcutGroup {
+ const _ShortcutGroup(this.title, this.icon, this.functions);
+
+ final String title;
+ final IconData icon;
+ final List<String> functions;
+}
+
+const List<_ShortcutGroup> _shortcutGroups = [
+ _ShortcutGroup('播放控制', Icons.play_arrow_rounded,
+ ['playorpause', 'forward', 'rewind', 'skip', 'next', 'prev']),
+ _ShortcutGroup(
+ '音量', Icons.volume_up_rounded, ['volumeup', 'volumedown', 'togglemute']),
+ _ShortcutGroup('画面与弹幕', Icons.fullscreen_rounded,
+ ['fullscreen', 'exitfullscreen', 'screenshot', 'toggledanmaku']),
+ _ShortcutGroup('倍速', Icons.speed_rounded,
+ ['speed1', 'speed2', 'speed3', 'speedup', 'speeddown']),
+];
+
class KeyboardSettingsPage extends StatefulWidget {
const KeyboardSettingsPage({super.key});
@@ -15,38 +36,86 @@ class KeyboardSettingsPage extends StatefulWidget {
class _KeyboardSettingsPageState extends State<KeyboardSettingsPage> {
String? listeningFunction;
int? listeningIndex;
+
+ /// Value the listening slot held before recording started; empty means the
+ /// slot was newly added and should be dropped when recording is cancelled.
+ String originalValue = '';
+
late Map<String, List<String>> shortcuts;
final FocusNode focusNode = FocusNode();
+ bool get isListening => listeningFunction != null && listeningIndex != null;
+
@override
void initState() {
super.initState();
- // 根据默认快捷键生成可用快捷键列表,并读取已设置值
- shortcuts = {
- for (var key in defaultShortcuts.keys)
- key: GStorage.getStringListSettingByName(
- 'shortcut_$key',
- defaultValue: defaultShortcuts[key]?.toList() ?? <String>[],
- )
- };
+ // 根据默认快捷键生成可用快捷键列表,并读取已设置值。
+ // 旧版实现可能把 '' / '...' 占位符持久化过,甚至把配置存成空列表
+ // (单绑定进入录制后直接退出页面);界面保证每个功能至少保留一个
+ // 真实绑定,读入时清理占位符、空配置恢复默认,有变化则回写
+ shortcuts = {};
+ for (final key in defaultShortcuts.keys) {
+ final stored = GStorage.getStringListSettingByName(
+ 'shortcut_$key',
+ defaultValue: defaultShortcuts[key]!.toList(),
+ );
+ final keys =
+ stored.where((value) => value.isNotEmpty && value != '...').toList();
+ var changed = keys.length != stored.length;
+ if (keys.isEmpty) {
+ keys.addAll(defaultShortcuts[key]!);
+ changed = true;
+ }
+ if (changed) {
+ GStorage.putStringListSettingByName('shortcut_$key', keys);
+ }
+ shortcuts[key] = keys;
+ }
}
@override
void dispose() {
- // 清理空的快捷键设置
- for (final entry in shortcuts.entries) {
- final func = entry.key;
- final keys = entry.value;
- keys.removeWhere((key) => key.isEmpty || key == '...');
- GStorage.putStringListSettingByName('shortcut_$func', keys);
- }
+ cancelListening();
focusNode.dispose();
super.dispose();
}
+ /// Restores or removes the pending slot so no '...' placeholder is left
+ /// behind when recording moves elsewhere. Pure state mutation: build-path
+ /// callers wrap it in setState, dispose calls it bare.
+ void cancelListening() {
+ if (!isListening) return;
+ final func = listeningFunction!;
+ final keys = shortcuts[func]!;
+ final index = listeningIndex!;
+ if (index < keys.length) {
+ if (originalValue.isEmpty) {
+ keys.removeAt(index);
+ } else {
+ keys[index] = originalValue;
+ }
+ }
+ GStorage.putStringListSettingByName('shortcut_$func', keys);
+ listeningFunction = null;
+ listeningIndex = null;
+ originalValue = '';
+ }
+
+ void beginListening(String func, int index) {
+ originalValue = shortcuts[func]![index];
+ shortcuts[func]![index] = '...';
+ listeningFunction = func;
+ listeningIndex = index;
+
+ Future.delayed(const Duration(milliseconds: 50), () {
+ if (!mounted) return;
+ focusNode.requestFocus();
+ });
+ }
+
bool handleShortcutInput(String rawKey) {
- if (listeningFunction == null || listeningIndex == null) return false;
+ if (!isListening || rawKey.isEmpty) return false;
final func = listeningFunction!;
final index = listeningIndex!;
@@ -69,45 +138,93 @@ class _KeyboardSettingsPageState extends State<KeyboardSettingsPage> {
shortcuts[func]![index] = rawKey;
listeningFunction = null;
listeningIndex = null;
+ originalValue = '';
});
GStorage.putStringListSettingByName('shortcut_$func', shortcuts[func]!);
return true;
}
- void startListening(String func, int index) {
+ // 新槽位不落盘:录制成功或取消时才持久化,storage 里永远没有占位符
+ void onAddKey(String func) {
setState(() {
- listeningFunction = func;
- listeningIndex = index;
- shortcuts[func]![index] = '...';
+ cancelListening();
+ final keys = shortcuts[func]!;
+ keys.add('');
+ beginListening(func, keys.length - 1);
});
+ }
- Future.delayed(const Duration(milliseconds: 50), () {
- if (!mounted) return;
- focusNode.requestFocus();
+ void onKeyCapTap(String func, int index) {
+ if (listeningFunction == func && listeningIndex == index) {
+ setState(cancelListening);
+ return;
+ }
+ final keyValue = shortcuts[func]![index];
+ setState(() {
+ cancelListening();
+ // 取消可能移除了同列表中的待录制项,按值重新定位(同一功能内按键唯一)
+ final idx = shortcuts[func]!.indexOf(keyValue);
+ if (idx >= 0) {
+ beginListening(func, idx);
+ }
});
}
+ void onRemoveKey(String func, int index) {
+ final keyValue = shortcuts[func]![index];
+ setState(() {
+ cancelListening();
+ final keys = shortcuts[func]!;
+ keys.remove(keyValue);
+ GStorage.putStringListSettingByName('shortcut_$func', keys);
+ });
+ }
+
+ void restoreDefaults() {
+ setState(() {
+ listeningFunction = null;
+ listeningIndex = null;
+ originalValue = '';
+ for (final func in shortcuts.keys) {
+ shortcuts[func] = defaultShortcuts[func]?.toList() ?? [];
+ GStorage.putStringListSettingByName('shortcut_$func', shortcuts[func]!);
+ }
+ });
+ KazumiDialog.showToast(message: '已恢复默认快捷键');
+ }
+
+ List<_ShortcutGroup> get displayGroups {
+ final groups = <_ShortcutGroup>[];
+ final covered = <String>{};
+ for (final group in _shortcutGroups) {
+ final funcs = group.functions.where(shortcuts.containsKey).toList();
+ covered.addAll(funcs);
+ if (funcs.isNotEmpty) {
+ groups.add(_ShortcutGroup(group.title, group.icon, funcs));
+ }
+ }
+ final leftovers =
+ shortcuts.keys.where((func) => !covered.contains(func)).toList();
+ if (leftovers.isNotEmpty) {
+ groups.add(_ShortcutGroup('其他', Icons.keyboard_rounded, leftovers));
+ }
+ return groups;
+ }
+
@override
Widget build(BuildContext context) {
+ final colorScheme = Theme.of(context).colorScheme;
+ final textTheme = Theme.of(context).textTheme;
+
return Scaffold(
appBar: SysAppBar(
- title: Text('快捷键'),
+ title: const Text('操作设置'),
actions: [
IconButton(
- icon: const Icon(Icons.refresh),
+ icon: const Icon(Icons.settings_backup_restore_rounded),
tooltip: '恢复默认',
- onPressed: () {
- setState(() {
- for (final func in shortcuts.keys) {
- shortcuts[func] = defaultShortcuts[func]?.toList() ?? [];
- GStorage.putStringListSettingByName(
- 'shortcut_$func',
- shortcuts[func]!,
- );
- }
- });
- },
+ onPressed: restoreDefaults,
),
],
),
@@ -121,7 +238,7 @@ class _KeyboardSettingsPageState extends State<KeyboardSettingsPage> {
descendantsAreFocusable: true,
onKeyEvent: (node, event) {
if (event is! KeyDownEvent) return KeyEventResult.ignored;
- if (listeningFunction == null) return KeyEventResult.ignored;
+ if (!isListening) return KeyEventResult.ignored;
final rawKey = event.logicalKey.keyLabel.isNotEmpty
? event.logicalKey.keyLabel
@@ -132,72 +249,217 @@ class _KeyboardSettingsPageState extends State<KeyboardSettingsPage> {
},
child: ListView(
padding: const EdgeInsets.all(16),
- children: shortcuts.entries.map((entry) {
- final func = entry.key;
- final keys = entry.value;
- return Card(
- margin: const EdgeInsets.symmetric(vertical: 8),
- child: Padding(
- padding: const EdgeInsets.all(12),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- children: [
- Text(shortcutsChineseName[func] ?? func,
- style: const TextStyle(
- fontSize: 18, fontWeight: FontWeight.bold)),
- Spacer(),
- IconButton(
- icon: Icon(Icons.add),
- onPressed: () {
- keys.removeWhere(
- (key) => key.isEmpty || key == '...');
- setState(() => keys.add(''));
- GStorage.putStringListSettingByName(
- 'shortcut_$func', keys);
- startListening(func, keys.length - 1);
- },
- padding: EdgeInsets.zero,
- constraints: const BoxConstraints(),
- ),
- ],
+ children: [
+ Center(
+ child: ConstrainedBox(
+ constraints: const BoxConstraints(maxWidth: 1000),
+ child: Padding(
+ padding: const EdgeInsets.only(bottom: 12),
+ child: Text(
+ '点按按键标签,再按下新按键完成修改',
+ style: textTheme.bodySmall?.copyWith(
+ color: colorScheme.onSurfaceVariant,
),
- if (keys.isNotEmpty) const SizedBox(height: 8),
- Wrap(
- spacing: 8,
- runSpacing: 8,
- children: [
- for (int i = 0; i < keys.length; i++)
- ActionChip(
- label: Text(
- keyAliases[keys[i]] ?? keys[i],
- ),
- avatar: keys.length >= 2
- ? Icon(Icons.cancel)
- : Icon(Icons.edit),
- onPressed: (keys.length >= 2)
- ? () {
- setState(() {
- keys.removeAt(i);
- listeningIndex = null;
- if (keys.length > 1) {
- keys.removeWhere((key) =>
- key.isEmpty || key == '...');
- }
- GStorage.putStringListSettingByName(
- 'shortcut_$func', keys);
- });
- }
- : () => startListening(func, 0),
- ),
- ],
- ),
- ],
+ ),
+ ),
+ ),
+ ),
+ for (final group in displayGroups)
+ Center(
+ child: ConstrainedBox(
+ constraints: const BoxConstraints(maxWidth: 1000),
+ child: _buildGroupCard(group),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+
+ Widget _buildGroupCard(_ShortcutGroup group) {
+ final colorScheme = Theme.of(context).colorScheme;
+ final textTheme = Theme.of(context).textTheme;
+
+ return Card(
+ elevation: 0,
+ margin: const EdgeInsets.only(bottom: 12),
+ color: colorScheme.surfaceContainerLow,
+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
+ clipBehavior: Clip.antiAlias,
+ child: Padding(
+ padding: const EdgeInsets.fromLTRB(16, 16, 16, 12),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Row(
+ children: [
+ Container(
+ width: 36,
+ height: 36,
+ decoration: BoxDecoration(
+ color: colorScheme.secondaryContainer,
+ shape: BoxShape.circle,
+ ),
+ child: Icon(
+ group.icon,
+ size: 18,
+ color: colorScheme.onSecondaryContainer,
),
),
- );
- }).toList(),
+ const SizedBox(width: 12),
+ Text(
+ group.title,
+ style:
+ textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w700),
+ ),
+ ],
+ ),
+ const SizedBox(height: 8),
+ for (final func in group.functions) _buildShortcutRow(func),
+ ],
+ ),
+ ),
+ );
+ }
+
+ Widget _buildShortcutRow(String func) {
+ final textTheme = Theme.of(context).textTheme;
+ final keys = shortcuts[func]!;
+
+ return Padding(
+ padding: const EdgeInsets.symmetric(vertical: 6),
+ child: Row(
+ children: [
+ Text(shortcutsChineseName[func] ?? func, style: textTheme.bodyMedium),
+ const SizedBox(width: 12),
+ Expanded(
+ child: Wrap(
+ alignment: WrapAlignment.end,
+ crossAxisAlignment: WrapCrossAlignment.center,
+ spacing: 6,
+ runSpacing: 6,
+ children: [
+ for (int i = 0; i < keys.length; i++)
+ _buildKeyCap(func, keys, i),
+ _AddKeyButton(onTap: () => onAddKey(func)),
+ ],
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+
+ Widget _buildKeyCap(String func, List<String> keys, int i) {
+ final listening = listeningFunction == func && listeningIndex == i;
+ // 删除入口只按真实绑定数判定,待录制占位符不算数——
+ // 否则单绑定时点「添加」会让原按键出现删除按钮,可被误删成空绑定
+ final realCount = keys.where((value) => value != '...').length;
+ return _KeyCap(
+ label: listening ? '按任意键' : keyAliases[keys[i]] ?? keys[i],
+ listening: listening,
+ onTap: () => onKeyCapTap(func, i),
+ onDelete: realCount >= 2 && !listening
+ ? () => onRemoveKey(func, i)
+ : null,
+ );
+ }
+}
+
+/// Tonal keycap pill for one bound key; tap to re-record, tap again to
+/// cancel. Shows a close icon when the function has spare bindings.
+class _KeyCap extends StatelessWidget {
+ const _KeyCap({
+ required this.label,
+ required this.listening,
+ required this.onTap,
+ this.onDelete,
+ });
+
+ final String label;
+ final bool listening;
+ final VoidCallback onTap;
+ final VoidCallback? onDelete;
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = Theme.of(context).colorScheme;
+ final textTheme = Theme.of(context).textTheme;
+
+ return Material(
+ color: listening
+ ? colorScheme.primaryContainer
+ : colorScheme.surfaceContainerHighest,
+ borderRadius: BorderRadius.circular(8),
+ clipBehavior: Clip.antiAlias,
+ child: InkWell(
+ onTap: onTap,
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
+ child: Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Text(
+ label,
+ style: textTheme.labelMedium?.copyWith(
+ color: listening
+ ? colorScheme.onPrimaryContainer
+ : colorScheme.onSurface,
+ fontWeight: FontWeight.w600,
+ ),
+ ),
+ if (onDelete != null) ...[
+ const SizedBox(width: 4),
+ GestureDetector(
+ onTap: onDelete,
+ behavior: HitTestBehavior.opaque,
+ child: Icon(
+ Icons.close_rounded,
+ size: 14,
+ color: colorScheme.onSurfaceVariant,
+ ),
+ ),
+ ],
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+}
+
+/// Outlined pill that appends a new binding slot and starts recording.
+class _AddKeyButton extends StatelessWidget {
+ const _AddKeyButton({required this.onTap});
+
+ final VoidCallback onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = Theme.of(context).colorScheme;
+ final shape = RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(8),
+ side: BorderSide(color: colorScheme.outlineVariant),
+ );
+
+ return Tooltip(
+ message: '添加按键',
+ child: Material(
+ color: Colors.transparent,
+ shape: shape,
+ clipBehavior: Clip.antiAlias,
+ child: InkWell(
+ onTap: onTap,
+ customBorder: shape,
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
+ child: Icon(
+ Icons.add_rounded,
+ size: 16,
+ color: colorScheme.onSurfaceVariant,
+ ),
),
),
),