This commit is contained in:
LZX committed 2026-09-14 00:06:49 +08:00
1 parent 9fbb414ee6
commit 4554f22c87
233 files changed
+147430 -605

No files matched your search

+264 -82
View File
@@ -22,12 +22,27 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
late TextEditingController _labelController;
late TextEditingController _valueInputController;
late TextEditingController _pairValueController;
late TextEditingController _cursorOffsetController;
String? _selectedIcon;
late String _selectedAction;
String? _selectedPresetValue;
/// 终端特殊键:所选 TerminalKey 枚举名
String? _selectedNamedKey;
/// 终端特殊键:下方三个修饰键控件组成的组合
KeyboardKeyMods _selectedMods = KeyboardKeyMods.none;
/// 终端修饰键动作:所选的 ctrl / alt / shift
String _selectedModifierValue = 'ctrl';
/// 终端指令快捷键:发送后是否自动回车
bool _autoEnter = false;
bool get _isTerminal => widget.scope == KeyboardScope.terminal;
@override
void initState() {
super.initState();
@@ -35,6 +50,10 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
_labelController = TextEditingController(text: init?.label ?? '');
_valueInputController = TextEditingController(text: init?.value ?? '');
// 成对符号不再依赖预设列表,改为自由输入,默认给出最常用的圆括号
_pairValueController = TextEditingController(
text: (init != null && init.action == 'pair' && init.value.isNotEmpty) ? init.value : '()',
);
_cursorOffsetController = TextEditingController(
text: (init?.cursorOffset ?? (init?.action == 'pair' ? -1 : 0)).toString(),
);
@@ -57,40 +76,147 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
}
void _syncPresetValueFromInitial() {
final currentVal = widget.initialKey?.value ?? '';
final init = widget.initialKey;
final currentVal = init?.value ?? '';
_autoEnter = init?.autoEnter ?? false;
_selectedMods = (init != null && init.action == 'key') ? init.mods : KeyboardKeyMods.none;
// 终端修饰键动作取值
_selectedModifierValue = KeyboardScopeHelper.terminalModifierValues.contains(currentVal)
? currentVal
: KeyboardScopeHelper.terminalModifierValues.first;
// 终端特殊键取值(支持 31 命名键与 keyA~keyZ)
_selectedNamedKey = (init != null &&
init.action == 'key' &&
KeyboardScopeHelper.terminalSupportedKeyNames.contains(currentVal))
? currentVal
: KeyboardScopeHelper.terminalNamedKeys.first.value;
if (_selectedAction == 'command') {
if (KeyboardScopeHelper.editorCommands.any((c) => c.value == currentVal)) {
_selectedPresetValue = currentVal;
} else {
_selectedPresetValue = KeyboardScopeHelper.editorCommands.first.value;
}
} else if (_selectedAction == 'pair') {
if (KeyboardScopeHelper.editorPairs.any((p) => p.value == currentVal)) {
_selectedPresetValue = currentVal;
} else {
_selectedPresetValue = KeyboardScopeHelper.editorPairs.first.value;
}
} else if (_selectedAction == 'modifier') {
if (KeyboardScopeHelper.terminalModifiers.any((m) => m.value == currentVal)) {
_selectedPresetValue = currentVal;
} else {
_selectedPresetValue = KeyboardScopeHelper.terminalModifiers.first.value;
}
} else if (_selectedAction == 'terminal_key') {
if (KeyboardScopeHelper.terminalKeys.any((k) => k.value == currentVal)) {
_selectedPresetValue = currentVal;
} else {
_selectedPresetValue = KeyboardScopeHelper.terminalKeys.first.value;
}
_selectedPresetValue = KeyboardScopeHelper.editorCommands.any((c) => c.value == currentVal)
? currentVal
: KeyboardScopeHelper.editorCommands.first.value;
} else {
_selectedPresetValue = null;
}
}
/// 动作在下拉中的显示名(终端作用域下 input 即"指令快捷键")
String _actionLabel(AppLocalizations l10n, String action) {
if (_isTerminal && action == 'input') {
return l10n.keyboardActionInputTerminal;
}
return l10n.keyboardActionName(action);
}
/// 按键标签留空时的兜底文案:特殊键用「修饰符+紧凑键名」,其余沿用 value
String _fallbackLabel(String value, KeyboardKeyMods mods) {
if (_selectedAction != 'key') return value;
final prefix = [
if (mods.ctrl) 'Ctrl',
if (mods.alt) 'Alt',
if (mods.shift) 'Shift',
];
final short = KeyboardScopeHelper.terminalKeyShortLabel(value);
return prefix.isEmpty ? short : '${prefix.join('+')}+$short';
}
/// 终端特殊键下拉:按分组插入不可选的标题项
List<DropdownMenuItem<String?>> _buildNamedKeyItems(AppLocalizations l10n) {
final items = <DropdownMenuItem<String?>>[];
TerminalKeyGroup? lastGroup;
for (final option in KeyboardScopeHelper.terminalSelectableKeys) {
if (option.group != lastGroup) {
lastGroup = option.group;
items.add(
DropdownMenuItem<String?>(
value: null,
enabled: false,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: Text(
_groupLabel(l10n, option.group),
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary,
),
),
),
),
);
}
items.add(
DropdownMenuItem<String?>(
value: option.value,
child: Row(
children: [
Icon(KeyboardIconHelper.getIcon(option.icon), size: 18),
const SizedBox(width: 8),
Expanded(
child: Text(
// 字母键在每种语言下都显示同一个拉丁字母,无需走 l10n
KeyboardScopeHelper.isLetterKeyName(option.value)
? KeyboardScopeHelper.terminalKeyShortLabel(option.value)
: l10n.keyboardKeyName(option.value),
overflow: TextOverflow.ellipsis,
),
),
],
),
),
);
}
return items;
}
String _groupLabel(AppLocalizations l10n, TerminalKeyGroup group) {
switch (group) {
case TerminalKeyGroup.navigation:
return l10n.keyboardKeyGroupNavigation;
case TerminalKeyGroup.editing:
return l10n.keyboardKeyGroupEditing;
case TerminalKeyGroup.functionKeys:
return l10n.keyboardKeyGroupFunctionKeys;
case TerminalKeyGroup.letters:
return l10n.keyboardKeyGroupLetters;
}
}
/// 下方三个修饰键控件之一;切换后立即重新校验,避免出现静默无效的组合
Widget _buildModifierChip(AppLocalizations l10n, String modifier, bool selected) {
return FilterChip(
label: Text(l10n.keyboardModifierName(modifier)),
selected: selected,
onSelected: (value) {
setState(() {
KeyboardKeyMods mods = _selectedMods;
switch (modifier) {
case 'ctrl':
mods = mods.copyWith(ctrl: value);
break;
case 'alt':
mods = mods.copyWith(alt: value);
break;
case 'shift':
mods = mods.copyWith(shift: value);
break;
}
_selectedMods = mods;
});
_formKey.currentState?.validate();
},
);
}
@override
void dispose() {
_labelController.dispose();
_valueInputController.dispose();
_pairValueController.dispose();
_cursorOffsetController.dispose();
super.dispose();
}
@@ -100,19 +226,19 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
setState(() {
_selectedAction = newAction;
if (_selectedAction == 'pair') {
_selectedPresetValue = KeyboardScopeHelper.editorPairs.first.value;
if (_pairValueController.text.trim().isEmpty) {
_pairValueController.text = '()';
}
if (_cursorOffsetController.text.isEmpty || _cursorOffsetController.text == '0') {
_cursorOffsetController.text = '-1';
}
if (_labelController.text.isEmpty) {
_labelController.text = _selectedPresetValue!;
}
// 标签留空时由 _onSave 回退为成对符号内容,与 input 行为一致
} else if (_selectedAction == 'command') {
_selectedPresetValue = KeyboardScopeHelper.editorCommands.first.value;
} else if (_selectedAction == 'modifier') {
_selectedPresetValue = KeyboardScopeHelper.terminalModifiers.first.value;
} else if (_selectedAction == 'terminal_key') {
_selectedPresetValue = KeyboardScopeHelper.terminalKeys.first.value;
_selectedModifierValue = KeyboardScopeHelper.terminalModifierValues.first;
} else if (_selectedAction == 'key') {
_selectedNamedKey ??= KeyboardScopeHelper.terminalNamedKeys.first.value;
} else {
_selectedPresetValue = null;
}
@@ -125,23 +251,37 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
final String label = _labelController.text.trim();
String value;
int cursorOffset = 0;
KeyboardKeyMods mods = KeyboardKeyMods.none;
bool autoEnter = false;
if (_selectedAction == 'input') {
value = _valueInputController.text;
} else {
value = _selectedPresetValue ?? '';
}
if (_selectedAction == 'pair') {
cursorOffset = int.tryParse(_cursorOffsetController.text.trim()) ?? -1;
switch (_selectedAction) {
case 'input':
value = _valueInputController.text;
if (_isTerminal) autoEnter = _autoEnter;
break;
case 'pair':
value = _pairValueController.text.trim();
cursorOffset = int.tryParse(_cursorOffsetController.text.trim()) ?? -1;
break;
case 'key':
value = _selectedNamedKey ?? '';
mods = _selectedMods;
break;
case 'modifier':
value = _selectedModifierValue;
break;
default:
value = _selectedPresetValue ?? '';
}
final keyItem = KeyboardKeyItem(
label: label.isNotEmpty ? label : value,
label: label.isNotEmpty ? label : _fallbackLabel(value, mods),
icon: (_selectedIcon != null && _selectedIcon!.isNotEmpty) ? _selectedIcon : null,
action: _selectedAction,
value: value,
cursorOffset: cursorOffset,
mods: mods,
autoEnter: autoEnter,
);
Navigator.of(context).pop(keyItem);
@@ -208,7 +348,10 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
Icon(KeyboardIconHelper.getIcon(iconKey), size: 18),
const SizedBox(width: 8),
Expanded(
child: Text(iconKey, overflow: TextOverflow.ellipsis),
child: Text(
l10n.keyboardIconName(iconKey),
overflow: TextOverflow.ellipsis,
),
),
],
),
@@ -235,7 +378,10 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
items: availableActions.map(
(actionOption) => DropdownMenuItem<String>(
value: actionOption.action,
child: Text(actionOption.label, overflow: TextOverflow.ellipsis),
child: Text(
_actionLabel(l10n, actionOption.action),
overflow: TextOverflow.ellipsis,
),
),
).toList(),
onChanged: _onActionChanged,
@@ -243,7 +389,7 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
const SizedBox(height: 14),
// 动作值 Value 控件联动
if (_selectedAction == 'input')
if (_selectedAction == 'input') ...[
TextFormField(
controller: _valueInputController,
decoration: InputDecoration(
@@ -258,29 +404,38 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
}
return null;
},
)
),
// 终端指令快捷键:发送后补回车,实现"一格执行命令"
if (_isTerminal)
SwitchListTile(
contentPadding: EdgeInsets.zero,
dense: true,
title: Text(l10n.keyboardAutoEnter),
value: _autoEnter,
onChanged: (val) => setState(() => _autoEnter = val),
),
]
else if (_selectedAction == 'pair')
DropdownButtonFormField<String>(
isExpanded: true,
initialValue: _selectedPresetValue,
TextFormField(
controller: _pairValueController,
decoration: InputDecoration(
labelText: l10n.keyboardPairPresetFormField,
labelText: l10n.keyboardPairValueFormField,
hintText: l10n.keyboardPairValueFormFieldHint,
border: const OutlineInputBorder(),
isDense: true,
),
items: KeyboardScopeHelper.editorPairs.map(
(pair) => DropdownMenuItem<String>(
value: pair.value,
child: Text('${pair.label} (${pair.value})', overflow: TextOverflow.ellipsis),
),
).toList(),
onChanged: (val) {
setState(() {
_selectedPresetValue = val;
if (_labelController.text.isEmpty && val != null) {
_labelController.text = val;
}
});
validator: (val) {
final v = val?.trim() ?? '';
if (v.isEmpty) {
return l10n.keyboardPairValueRequiredError;
}
final half = v.length ~/ 2;
// 长度不足 2 或左右完全相同(如 "((" )无法构成有效的前后半段
if (v.length < 2 ||
(v.length.isEven && v.substring(0, half) == v.substring(half))) {
return l10n.keyboardPairValueInvalidError;
}
return null;
},
)
else if (_selectedAction == 'command')
@@ -295,7 +450,18 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
items: KeyboardScopeHelper.editorCommands.map(
(cmd) => DropdownMenuItem<String>(
value: cmd.value,
child: Text('${cmd.label} [${cmd.value}]', overflow: TextOverflow.ellipsis),
child: Row(
children: [
Icon(KeyboardIconHelper.getIcon(cmd.icon), size: 18),
const SizedBox(width: 8),
Expanded(
child: Text(
l10n.keyboardCommandName(cmd.value),
overflow: TextOverflow.ellipsis,
),
),
],
),
),
).toList(),
onChanged: (val) {
@@ -307,45 +473,62 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
else if (_selectedAction == 'modifier')
DropdownButtonFormField<String>(
isExpanded: true,
initialValue: _selectedPresetValue,
initialValue: _selectedModifierValue,
decoration: InputDecoration(
labelText: l10n.keyboardModifierPresetFormField,
border: const OutlineInputBorder(),
isDense: true,
),
items: KeyboardScopeHelper.terminalModifiers.map(
(m) => DropdownMenuItem<String>(
value: m.value,
child: Text(m.label, overflow: TextOverflow.ellipsis),
),
).toList(),
items: KeyboardScopeHelper.terminalModifierValues
.map(
(m) => DropdownMenuItem<String>(
value: m,
child: Text(l10n.keyboardModifierName(m), overflow: TextOverflow.ellipsis),
),
)
.toList(),
onChanged: (val) {
if (val == null) return;
setState(() {
_selectedPresetValue = val;
_selectedModifierValue = val;
});
},
)
else if (_selectedAction == 'terminal_key')
DropdownButtonFormField<String>(
else if (_selectedAction == 'key') ...[
DropdownButtonFormField<String?>(
isExpanded: true,
initialValue: _selectedPresetValue,
initialValue: _selectedNamedKey,
decoration: InputDecoration(
labelText: l10n.keyboardTerminalKeyPresetFormField,
labelText: l10n.keyboardKeyFormField,
border: const OutlineInputBorder(),
isDense: true,
),
items: KeyboardScopeHelper.terminalKeys.map(
(k) => DropdownMenuItem<String>(
value: k.value,
child: Text(k.label, overflow: TextOverflow.ellipsis),
),
).toList(),
items: _buildNamedKeyItems(l10n),
onChanged: (val) {
if (val == null) return;
setState(() {
_selectedPresetValue = val;
_selectedNamedKey = val;
});
},
validator: (val) {
final name = val ?? '';
if (name.isEmpty || !KeyboardScopeHelper.isSupportedTerminalKey(name)) {
return l10n.keyboardKeyInvalidError;
}
return null;
},
),
const SizedBox(height: 12),
// 单独提供三个修饰键:与下拉里选中的键叠加成 Ctrl+C 这类组合
Wrap(
spacing: 8,
children: [
_buildModifierChip(l10n, 'ctrl', _selectedMods.ctrl),
_buildModifierChip(l10n, 'alt', _selectedMods.alt),
_buildModifierChip(l10n, 'shift', _selectedMods.shift),
],
),
],
// 仅当 action == pair 时显示 cursorOffset
if (_selectedAction == 'pair') ...[
@@ -355,7 +538,6 @@ class _KeyEditDialogState extends State<KeyEditDialog> {
keyboardType: const TextInputType.numberWithOptions(signed: true),
decoration: InputDecoration(
labelText: l10n.keyboardCursorOffsetFormField,
helperText: l10n.keyboardCursorOffsetHelper,
border: const OutlineInputBorder(),
isDense: true,
),