2
This commit is contained in:
1 parent
9fbb414ee6
commit
4554f22c87
233 files changed
+147430
-605
No files matched your search
@@ -15,6 +15,16 @@ class KeyboardIconHelper {
|
||||
'arrow_down': Icons.arrow_downward,
|
||||
'home': Icons.vertical_align_top,
|
||||
'end': Icons.vertical_align_bottom,
|
||||
'first_page': Icons.first_page,
|
||||
'last_page': Icons.last_page,
|
||||
'page_up': Icons.keyboard_double_arrow_up,
|
||||
'page_down': Icons.keyboard_double_arrow_down,
|
||||
'enter': Icons.keyboard_return,
|
||||
'space': Icons.space_bar,
|
||||
'escape': Icons.cancel_outlined,
|
||||
'insert': Icons.input,
|
||||
'clear': Icons.clear,
|
||||
'keyboard': Icons.keyboard_outlined,
|
||||
'backspace': Icons.backspace_outlined,
|
||||
'delete': Icons.delete_outline,
|
||||
'copy': Icons.copy,
|
||||
@@ -22,6 +32,7 @@ class KeyboardIconHelper {
|
||||
'paste': Icons.content_paste,
|
||||
'save': Icons.save_outlined,
|
||||
'search': Icons.search,
|
||||
'select_all': Icons.select_all,
|
||||
'keyboard_hide': Icons.keyboard_hide_outlined,
|
||||
};
|
||||
|
||||
@@ -36,21 +47,17 @@ class KeyboardIconHelper {
|
||||
}
|
||||
|
||||
/// 虚拟小键盘作用域(编辑区或终端)
|
||||
///
|
||||
/// 界面展示名称取自 l10n(`editorScope` / `terminalScope`),此处不再自带文案。
|
||||
enum KeyboardScope {
|
||||
editor,
|
||||
terminal;
|
||||
|
||||
String get displayName {
|
||||
switch (this) {
|
||||
case KeyboardScope.editor:
|
||||
return '编辑区';
|
||||
case KeyboardScope.terminal:
|
||||
return '终端';
|
||||
}
|
||||
}
|
||||
terminal,
|
||||
}
|
||||
|
||||
/// 按键动作定义项辅助
|
||||
///
|
||||
/// 注意:[label] 与 [description] 仅作为代码内的定义说明,界面展示名称一律取自
|
||||
/// l10n(`keyboardActionName`),避免中英文文案两处维护。
|
||||
class KeyboardActionOption {
|
||||
final String action;
|
||||
final String label;
|
||||
@@ -64,13 +71,116 @@ class KeyboardActionOption {
|
||||
}
|
||||
|
||||
/// 预设值选项辅助
|
||||
///
|
||||
/// 注意:[label] 仅作为代码内的定义说明,界面展示名称一律取自 l10n
|
||||
/// (`keyboardCommandName` / `keyboardModifierName` / `keyboardKeyName`)。
|
||||
class KeyboardValueOption {
|
||||
final String value;
|
||||
final String label;
|
||||
|
||||
/// 下拉菜单中展示的内置图标常量名称(可选,取值同 [KeyboardIconHelper])
|
||||
final String? icon;
|
||||
|
||||
const KeyboardValueOption({
|
||||
required this.value,
|
||||
required this.label,
|
||||
this.icon,
|
||||
});
|
||||
}
|
||||
|
||||
/// 按键修饰符组合(Ctrl / Alt / Shift)
|
||||
///
|
||||
/// 仅在终端作用域的 `key` 动作上有实际含义:xterm 会把它换算成对应转义序列。
|
||||
/// 三个都为 false 时视为"无修饰",JSON 导出时整个 `mods` 字段省略。
|
||||
class KeyboardKeyMods {
|
||||
final bool ctrl;
|
||||
final bool alt;
|
||||
final bool shift;
|
||||
|
||||
const KeyboardKeyMods({
|
||||
this.ctrl = false,
|
||||
this.alt = false,
|
||||
this.shift = false,
|
||||
});
|
||||
|
||||
/// 无修饰的常量实例
|
||||
static const KeyboardKeyMods none = KeyboardKeyMods();
|
||||
|
||||
bool get isEmpty => !ctrl && !alt && !shift;
|
||||
|
||||
bool get isNotEmpty => !isEmpty;
|
||||
|
||||
/// 已勾选的修饰键数量
|
||||
int get count => (ctrl ? 1 : 0) + (alt ? 1 : 0) + (shift ? 1 : 0);
|
||||
|
||||
KeyboardKeyMods copyWith({bool? ctrl, bool? alt, bool? shift}) {
|
||||
return KeyboardKeyMods(
|
||||
ctrl: ctrl ?? this.ctrl,
|
||||
alt: alt ?? this.alt,
|
||||
shift: shift ?? this.shift,
|
||||
);
|
||||
}
|
||||
|
||||
/// 从 `{"ctrl": true, "alt": false}` 形式的 JSON 解析,非法输入一律视为无修饰
|
||||
factory KeyboardKeyMods.fromJson(dynamic json) {
|
||||
if (json is! Map) return none;
|
||||
return KeyboardKeyMods(
|
||||
ctrl: json['ctrl'] == true,
|
||||
alt: json['alt'] == true,
|
||||
shift: json['shift'] == true,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (ctrl) 'ctrl': true,
|
||||
if (alt) 'alt': true,
|
||||
if (shift) 'shift': true,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is KeyboardKeyMods &&
|
||||
other.ctrl == ctrl &&
|
||||
other.alt == alt &&
|
||||
other.shift == shift;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(ctrl, alt, shift);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
if (isEmpty) return 'none';
|
||||
return [
|
||||
if (ctrl) 'ctrl',
|
||||
if (alt) 'alt',
|
||||
if (shift) 'shift',
|
||||
].join('+');
|
||||
}
|
||||
}
|
||||
|
||||
/// 终端命名键分组(用于弹窗下拉的分组展示)
|
||||
enum TerminalKeyGroup {
|
||||
navigation,
|
||||
editing,
|
||||
functionKeys,
|
||||
letters,
|
||||
}
|
||||
|
||||
/// 终端命名键选项:value 为 xterm `TerminalKey` 的枚举名
|
||||
class KeyboardNamedKeyOption {
|
||||
final String value;
|
||||
final TerminalKeyGroup group;
|
||||
|
||||
/// 下拉菜单中展示的内置图标常量名称(可选,取值同 [KeyboardIconHelper])
|
||||
final String? icon;
|
||||
|
||||
const KeyboardNamedKeyOption({
|
||||
required this.value,
|
||||
required this.group,
|
||||
this.icon,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -78,69 +188,197 @@ class KeyboardValueOption {
|
||||
class KeyboardScopeHelper {
|
||||
/// 编辑区支持的动作类型
|
||||
static const List<KeyboardActionOption> editorActions = [
|
||||
KeyboardActionOption(action: 'input', label: '普通文本 (input)', description: '直接插入指定文本内容'),
|
||||
KeyboardActionOption(action: 'pair', label: '成对符号 (pair)', description: '括号/引号等成对符号,支持选区包裹与光标偏移'),
|
||||
KeyboardActionOption(action: 'command', label: '编辑器命令 (command)', description: '触发缩进、撤销、光标移动等编辑器功能'),
|
||||
KeyboardActionOption(action: 'input', label: '普通文本', description: '直接插入指定文本内容'),
|
||||
KeyboardActionOption(action: 'pair', label: '成对符号', description: '括号/引号等成对符号,支持选区包裹与光标偏移'),
|
||||
KeyboardActionOption(action: 'command', label: '编辑器命令', description: '触发缩进、撤销、光标移动等编辑器功能'),
|
||||
];
|
||||
|
||||
/// 终端支持的动作类型
|
||||
static const List<KeyboardActionOption> terminalActions = [
|
||||
KeyboardActionOption(action: 'input', label: '普通文本 (input)', description: '直接向终端发送指定文本'),
|
||||
KeyboardActionOption(action: 'modifier', label: '修饰键 (modifier)', description: 'Ctrl / Alt 等组合修饰键状态切换'),
|
||||
KeyboardActionOption(action: 'terminal_key', label: '终端按键 (terminal_key)', description: 'Esc、Tab、Ctrl+C、方向键等终端特殊键'),
|
||||
KeyboardActionOption(action: 'input', label: '指令快捷键', description: '向终端发送一段文本,可附加自动回车'),
|
||||
KeyboardActionOption(action: 'modifier', label: '修饰键', description: 'Ctrl / Alt / Shift 修饰状态切换'),
|
||||
KeyboardActionOption(action: 'key', label: '特殊键', description: 'Esc、Tab、方向键、功能键及 Ctrl/Alt 组合'),
|
||||
];
|
||||
|
||||
/// 编辑区预设命令列表
|
||||
/// 终端动作取值集合(供校验使用)
|
||||
static const Set<String> terminalActionValues = {'input', 'modifier', 'key'};
|
||||
|
||||
/// 编辑区动作取值集合(供校验使用)
|
||||
static const Set<String> editorActionValues = {'input', 'pair', 'command'};
|
||||
|
||||
/// 编辑区预设命令列表(icon 用于下拉菜单前缀展示)
|
||||
static const List<KeyboardValueOption> editorCommands = [
|
||||
KeyboardValueOption(value: 'tab', label: 'Tab (向右缩进)'),
|
||||
KeyboardValueOption(value: 'untab', label: 'Untab (向左缩进)'),
|
||||
KeyboardValueOption(value: 'undo', label: '撤销 (Undo)'),
|
||||
KeyboardValueOption(value: 'redo', label: '重做 (Redo)'),
|
||||
KeyboardValueOption(value: 'cursor_left', label: '光标左移'),
|
||||
KeyboardValueOption(value: 'cursor_right', label: '光标右移'),
|
||||
KeyboardValueOption(value: 'cursor_up', label: '光标上移'),
|
||||
KeyboardValueOption(value: 'cursor_down', label: '光标下移'),
|
||||
KeyboardValueOption(value: 'line_start', label: '移动至行首'),
|
||||
KeyboardValueOption(value: 'line_end', label: '移动至行尾'),
|
||||
KeyboardValueOption(value: 'page_start', label: '移动至文首'),
|
||||
KeyboardValueOption(value: 'page_end', label: '移动至文末'),
|
||||
KeyboardValueOption(value: 'copy', label: '复制 (Copy)'),
|
||||
KeyboardValueOption(value: 'cut', label: '剪切 (Cut)'),
|
||||
KeyboardValueOption(value: 'paste', label: '粘贴 (Paste)'),
|
||||
KeyboardValueOption(value: 'delete', label: '删除 (Delete)'),
|
||||
KeyboardValueOption(value: 'select_all', label: '全选 (Select All)'),
|
||||
KeyboardValueOption(value: 'keyboard_hide', label: '收起小键盘'),
|
||||
KeyboardValueOption(value: 'tab', label: '向右缩进', icon: 'tab'),
|
||||
KeyboardValueOption(value: 'untab', label: '向左缩进', icon: 'untab'),
|
||||
KeyboardValueOption(value: 'undo', label: '撤销', icon: 'undo'),
|
||||
KeyboardValueOption(value: 'redo', label: '重做', icon: 'redo'),
|
||||
KeyboardValueOption(value: 'cursor_left', label: '光标左移', icon: 'arrow_left'),
|
||||
KeyboardValueOption(value: 'cursor_right', label: '光标右移', icon: 'arrow_right'),
|
||||
KeyboardValueOption(value: 'cursor_up', label: '光标上移', icon: 'arrow_up'),
|
||||
KeyboardValueOption(value: 'cursor_down', label: '光标下移', icon: 'arrow_down'),
|
||||
KeyboardValueOption(value: 'line_start', label: '移动至行首', icon: 'home'),
|
||||
KeyboardValueOption(value: 'line_end', label: '移动至行尾', icon: 'end'),
|
||||
KeyboardValueOption(value: 'page_start', label: '移动至文首', icon: 'first_page'),
|
||||
KeyboardValueOption(value: 'page_end', label: '移动至文末', icon: 'last_page'),
|
||||
KeyboardValueOption(value: 'copy', label: '复制', icon: 'copy'),
|
||||
KeyboardValueOption(value: 'cut', label: '剪切', icon: 'cut'),
|
||||
KeyboardValueOption(value: 'paste', label: '粘贴', icon: 'paste'),
|
||||
KeyboardValueOption(value: 'delete', label: '删除', icon: 'delete'),
|
||||
KeyboardValueOption(value: 'select_all', label: '全选', icon: 'select_all'),
|
||||
KeyboardValueOption(value: 'keyboard_hide', label: '收起小键盘', icon: 'keyboard_hide'),
|
||||
];
|
||||
|
||||
/// 编辑区成对符号预设列表
|
||||
static const List<KeyboardValueOption> editorPairs = [
|
||||
KeyboardValueOption(value: '()', label: '圆括号 ()'),
|
||||
KeyboardValueOption(value: '[]', label: '方括号 []'),
|
||||
KeyboardValueOption(value: '{}', label: '花括号 {}'),
|
||||
KeyboardValueOption(value: '""', label: '双引号 ""'),
|
||||
KeyboardValueOption(value: "''", label: "单引号 ''"),
|
||||
KeyboardValueOption(value: '<>', label: '尖括号 <>'),
|
||||
// ==========================================
|
||||
// 终端命名键表(值为 xterm TerminalKey 的枚举名)
|
||||
//
|
||||
// 说明:xterm 4.0.0 的默认 keytab 只对下列 31 个命名键定义了转义序列,
|
||||
// 其余 HID 键码(digit0-9、numpad0-9、media*、gameButton* 等)在默认
|
||||
// keytab 中没有条目,`Terminal.keyInput` 会返回 false 且不产生任何输出,
|
||||
// 因此这里作为"终端中真正可用"的键清单,由弹窗下拉与 JSON 校验共用。
|
||||
// 组合键由弹窗下方的三个修饰键控件(Ctrl / Alt / Shift)叠加产生。
|
||||
// ==========================================
|
||||
|
||||
/// 终端可用命名键(导航键)
|
||||
static const List<KeyboardNamedKeyOption> terminalNavigationKeys = [
|
||||
KeyboardNamedKeyOption(value: 'arrowUp', group: TerminalKeyGroup.navigation, icon: 'arrow_up'),
|
||||
KeyboardNamedKeyOption(value: 'arrowDown', group: TerminalKeyGroup.navigation, icon: 'arrow_down'),
|
||||
KeyboardNamedKeyOption(value: 'arrowLeft', group: TerminalKeyGroup.navigation, icon: 'arrow_left'),
|
||||
KeyboardNamedKeyOption(value: 'arrowRight', group: TerminalKeyGroup.navigation, icon: 'arrow_right'),
|
||||
KeyboardNamedKeyOption(value: 'home', group: TerminalKeyGroup.navigation, icon: 'home'),
|
||||
KeyboardNamedKeyOption(value: 'end', group: TerminalKeyGroup.navigation, icon: 'end'),
|
||||
KeyboardNamedKeyOption(value: 'pageUp', group: TerminalKeyGroup.navigation, icon: 'page_up'),
|
||||
KeyboardNamedKeyOption(value: 'pageDown', group: TerminalKeyGroup.navigation, icon: 'page_down'),
|
||||
];
|
||||
|
||||
/// 终端修饰键预设列表
|
||||
static const List<KeyboardValueOption> terminalModifiers = [
|
||||
KeyboardValueOption(value: 'ctrl', label: 'Ctrl 键'),
|
||||
KeyboardValueOption(value: 'alt', label: 'Alt 键'),
|
||||
/// 终端可用命名键(编辑键)
|
||||
static const List<KeyboardNamedKeyOption> terminalEditingKeys = [
|
||||
KeyboardNamedKeyOption(value: 'escape', group: TerminalKeyGroup.editing, icon: 'escape'),
|
||||
KeyboardNamedKeyOption(value: 'tab', group: TerminalKeyGroup.editing, icon: 'tab'),
|
||||
KeyboardNamedKeyOption(value: 'backtab', group: TerminalKeyGroup.editing, icon: 'untab'),
|
||||
KeyboardNamedKeyOption(value: 'returnKey', group: TerminalKeyGroup.editing, icon: 'enter'),
|
||||
KeyboardNamedKeyOption(value: 'enter', group: TerminalKeyGroup.editing, icon: 'enter'),
|
||||
KeyboardNamedKeyOption(value: 'numpadEnter', group: TerminalKeyGroup.editing, icon: 'enter'),
|
||||
KeyboardNamedKeyOption(value: 'backspace', group: TerminalKeyGroup.editing, icon: 'backspace'),
|
||||
KeyboardNamedKeyOption(value: 'delete', group: TerminalKeyGroup.editing, icon: 'delete'),
|
||||
KeyboardNamedKeyOption(value: 'insert', group: TerminalKeyGroup.editing, icon: 'insert'),
|
||||
KeyboardNamedKeyOption(value: 'space', group: TerminalKeyGroup.editing, icon: 'space'),
|
||||
KeyboardNamedKeyOption(value: 'numpadClear', group: TerminalKeyGroup.editing, icon: 'clear'),
|
||||
];
|
||||
|
||||
/// 终端按键预设列表
|
||||
static const List<KeyboardValueOption> terminalKeys = [
|
||||
KeyboardValueOption(value: 'esc', label: 'Esc (退出键)'),
|
||||
KeyboardValueOption(value: 'terminal_tab', label: 'Tab (补全键)'),
|
||||
KeyboardValueOption(value: 'ctrl_c', label: 'Ctrl + C (中断信号)'),
|
||||
KeyboardValueOption(value: 'ctrl_d', label: 'Ctrl + D (EOF / 退出)'),
|
||||
KeyboardValueOption(value: 'ctrl_z', label: 'Ctrl + Z (挂起挂起)'),
|
||||
KeyboardValueOption(value: 'ctrl_l', label: 'Ctrl + L (清屏)'),
|
||||
KeyboardValueOption(value: 'arrow_up', label: '方向上键 (历史上一条)'),
|
||||
KeyboardValueOption(value: 'arrow_down', label: '方向下键 (历史下一条)'),
|
||||
KeyboardValueOption(value: 'arrow_left', label: '方向左键'),
|
||||
KeyboardValueOption(value: 'arrow_right', label: '方向右键'),
|
||||
/// 终端可用命名键(功能键 F1~F12,统一用键盘图标)
|
||||
static const List<KeyboardNamedKeyOption> terminalFunctionKeys = [
|
||||
KeyboardNamedKeyOption(value: 'f1', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f2', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f3', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f4', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f5', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f6', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f7', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f8', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f9', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f10', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f11', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
KeyboardNamedKeyOption(value: 'f12', group: TerminalKeyGroup.functionKeys, icon: 'keyboard'),
|
||||
];
|
||||
|
||||
/// 终端全部可用命名键(31 个,按分组顺序拼接)
|
||||
static const List<KeyboardNamedKeyOption> terminalNamedKeys = [
|
||||
...terminalNavigationKeys,
|
||||
...terminalEditingKeys,
|
||||
...terminalFunctionKeys,
|
||||
];
|
||||
|
||||
/// 终端字母键 keyA ~ keyZ(同样用键盘图标;配合下方修饰键控件可组成 Ctrl+C)
|
||||
///
|
||||
/// 用 getter 而不是 `static final`:静态字段在热重载后不会重新初始化,
|
||||
/// 会让这里的改动(例如给字母键补图标)"看起来没生效"。
|
||||
static List<KeyboardNamedKeyOption> get terminalLetterKeys => [
|
||||
for (int i = 0; i < 26; i++)
|
||||
KeyboardNamedKeyOption(
|
||||
value: 'key${String.fromCharCode(0x41 + i)}',
|
||||
group: TerminalKeyGroup.letters,
|
||||
icon: 'keyboard',
|
||||
),
|
||||
];
|
||||
|
||||
/// 弹窗下拉可选的终端键(31 命名键 + 26 字母键)
|
||||
static List<KeyboardNamedKeyOption> get terminalSelectableKeys => [
|
||||
...terminalNamedKeys,
|
||||
...terminalLetterKeys,
|
||||
];
|
||||
|
||||
/// 终端特殊键的紧凑显示名,用于按键标签自动生成(符号/缩写无需 l10n)
|
||||
static String terminalKeyShortLabel(String value) {
|
||||
switch (value) {
|
||||
case 'escape':
|
||||
return 'Esc';
|
||||
case 'tab':
|
||||
return 'Tab';
|
||||
case 'backtab':
|
||||
return '⇤';
|
||||
case 'returnKey':
|
||||
case 'enter':
|
||||
case 'numpadEnter':
|
||||
return '⏎';
|
||||
case 'backspace':
|
||||
return '⌫';
|
||||
case 'delete':
|
||||
return 'Del';
|
||||
case 'insert':
|
||||
return 'Ins';
|
||||
case 'space':
|
||||
return 'Space';
|
||||
case 'numpadClear':
|
||||
return 'Clear';
|
||||
case 'arrowUp':
|
||||
return '↑';
|
||||
case 'arrowDown':
|
||||
return '↓';
|
||||
case 'arrowLeft':
|
||||
return '←';
|
||||
case 'arrowRight':
|
||||
return '→';
|
||||
case 'home':
|
||||
return 'Home';
|
||||
case 'end':
|
||||
return 'End';
|
||||
case 'pageUp':
|
||||
return 'PgUp';
|
||||
case 'pageDown':
|
||||
return 'PgDn';
|
||||
default:
|
||||
if (isLetterKeyName(value)) return value.substring(3);
|
||||
if (value.length > 1 && value.startsWith('f') && int.tryParse(value.substring(1)) != null) {
|
||||
return value.toUpperCase();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/// 终端 action 为 `key` 时的合法 value 集合(31 个命名键 + keyA~keyZ)
|
||||
static Set<String> get terminalSupportedKeyNames => {
|
||||
for (final key in terminalNamedKeys) key.value,
|
||||
for (int i = 0; i < 26; i++) 'key${String.fromCharCode(0x41 + i)}',
|
||||
};
|
||||
|
||||
/// 是否为字母键名(keyA ~ keyZ)
|
||||
static bool isLetterKeyName(String value) {
|
||||
if (value.length != 4 || !value.startsWith('key')) return false;
|
||||
final code = value.codeUnitAt(3);
|
||||
return code >= 0x41 && code <= 0x5A;
|
||||
}
|
||||
|
||||
/// 判断终端 `key` 动作的 value 是否为受支持的键。
|
||||
///
|
||||
/// - 31 个命名键:任意修饰组合都可用
|
||||
/// - 字母键 keyA~keyZ:**不受修饰符约束**,单独成格时就是该字母本身
|
||||
/// (运行时走 `charInput`:裸字母发字母,Ctrl/Alt 组合发对应控制序列)
|
||||
static bool isSupportedTerminalKey(String value) {
|
||||
return terminalNamedKeys.any((k) => k.value == value) || isLetterKeyName(value);
|
||||
}
|
||||
|
||||
/// 修饰键动作允许的取值
|
||||
static const Set<String> terminalModifierValues = {'ctrl', 'alt', 'shift'};
|
||||
}
|
||||
|
||||
/// 小键盘单个按键数据模型
|
||||
@@ -152,25 +390,34 @@ class KeyboardKeyItem {
|
||||
final String? icon;
|
||||
|
||||
/// 动作类型:
|
||||
/// - 'input': 普通文本输入(默认)
|
||||
/// - 'pair': 括号/引号等成对符号,支持选区包裹与光标偏移
|
||||
/// - 'command': 编辑器动作命令,例如 'cursor_left', 'tab', 'undo' 等
|
||||
/// - 'modifier': 终端修饰键,如 'ctrl', 'alt'
|
||||
/// - 'terminal_key': 终端按键,如 'esc', 'ctrl_c'
|
||||
/// - 'input': 普通文本输入(默认)。终端作用域下即"指令快捷键",配合
|
||||
/// [autoEnter] 可实现"输入并执行"
|
||||
/// - 'pair': 括号/引号等成对符号,支持选区包裹与光标偏移(仅编辑区)
|
||||
/// - 'command': 编辑器动作命令,例如 'cursor_left', 'tab', 'undo' 等(仅编辑区)
|
||||
/// - 'modifier': 终端修饰键,取值 'ctrl' / 'alt' / 'shift'(仅终端)
|
||||
/// - 'key': 终端特殊键,取值为 xterm `TerminalKey` 枚举名(仅终端)
|
||||
final String action;
|
||||
|
||||
/// 动作附带值(输入的文本或命令名称)
|
||||
/// 动作附带值(输入的文本、命令名称或特殊键名)
|
||||
final String value;
|
||||
|
||||
/// 光标插入后的相对偏移(如 pair \"()\" 插入后光标向左移动 1 位,则为 -1)
|
||||
final int cursorOffset;
|
||||
|
||||
/// 修饰符组合,仅终端 `key` 动作有效(文本通道不接受修饰符)
|
||||
final KeyboardKeyMods mods;
|
||||
|
||||
/// 仅终端 `input` 动作有效:发送文本后补一个回车符 `\r`,实现"输入即执行"
|
||||
final bool autoEnter;
|
||||
|
||||
const KeyboardKeyItem({
|
||||
required this.label,
|
||||
this.icon,
|
||||
this.action = 'input',
|
||||
required this.value,
|
||||
this.cursorOffset = 0,
|
||||
this.mods = KeyboardKeyMods.none,
|
||||
this.autoEnter = false,
|
||||
});
|
||||
|
||||
KeyboardKeyItem copyWith({
|
||||
@@ -180,6 +427,8 @@ class KeyboardKeyItem {
|
||||
String? action,
|
||||
String? value,
|
||||
int? cursorOffset,
|
||||
KeyboardKeyMods? mods,
|
||||
bool? autoEnter,
|
||||
}) {
|
||||
return KeyboardKeyItem(
|
||||
label: label ?? this.label,
|
||||
@@ -187,6 +436,8 @@ class KeyboardKeyItem {
|
||||
action: action ?? this.action,
|
||||
value: value ?? this.value,
|
||||
cursorOffset: cursorOffset ?? this.cursorOffset,
|
||||
mods: mods ?? this.mods,
|
||||
autoEnter: autoEnter ?? this.autoEnter,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -214,6 +465,8 @@ class KeyboardKeyItem {
|
||||
action: action,
|
||||
value: value,
|
||||
cursorOffset: cursorOffset,
|
||||
mods: KeyboardKeyMods.fromJson(json['mods']),
|
||||
autoEnter: json['autoEnter'] == true,
|
||||
);
|
||||
}
|
||||
return const KeyboardKeyItem(label: '', value: '');
|
||||
@@ -231,6 +484,12 @@ class KeyboardKeyItem {
|
||||
if (cursorOffset != 0) {
|
||||
map['cursorOffset'] = cursorOffset;
|
||||
}
|
||||
if (mods.isNotEmpty) {
|
||||
map['mods'] = mods.toJson();
|
||||
}
|
||||
if (autoEnter) {
|
||||
map['autoEnter'] = true;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -339,8 +598,12 @@ class VirtualKeyboardConfig {
|
||||
};
|
||||
}
|
||||
|
||||
/// 校验 JSON 文本合法性,若合法返回 null,若非法返回具体错误描述
|
||||
static String? validateJson(String jsonStr) {
|
||||
/// 校验 JSON 文本合法性,若合法返回 null,若非法返回具体错误描述。
|
||||
///
|
||||
/// [scope] 决定按键动作与取值的合法集合:
|
||||
/// - 编辑区:`input` / `pair` / `command`
|
||||
/// - 终端:`input`(指令快捷键)/ `modifier` / `key`(特殊键)
|
||||
static String? validateJson(String jsonStr, [KeyboardScope scope = KeyboardScope.editor]) {
|
||||
final trimmed = jsonStr.trim();
|
||||
if (trimmed.isEmpty) {
|
||||
return '配置内容不能为空';
|
||||
@@ -435,6 +698,12 @@ class VirtualKeyboardConfig {
|
||||
if (key.containsKey('cursorOffset') && key['cursorOffset'] is! int) {
|
||||
return '第 $pageNum 页第 $rowNum 行第 $keyNum 个按键的 "cursorOffset" 必须为整数';
|
||||
}
|
||||
final actionError = _validateKeyAction(
|
||||
key: key,
|
||||
scope: scope,
|
||||
location: '第 $pageNum 页第 $rowNum 行第 $keyNum 个按键',
|
||||
);
|
||||
if (actionError != null) return actionError;
|
||||
} else {
|
||||
return '第 $pageNum 页第 $rowNum 行第 $keyNum 个按键类型错误,只能是文本字符串或对象';
|
||||
}
|
||||
@@ -445,9 +714,140 @@ class VirtualKeyboardConfig {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 默认配置预设
|
||||
/// 按作用域校验单个按键对象的 action / value / mods / autoEnter 组合。
|
||||
///
|
||||
/// 返回 null 表示合法;否则返回可直接展示给用户的错误描述。
|
||||
/// 这里的严格程度是刻意为之:宁可保存时报错,也不要在运行时静默无输出
|
||||
/// (例如把 digit1 当作终端特殊键,或给文本通道挂修饰符)。
|
||||
static String? _validateKeyAction({
|
||||
required Map<String, dynamic> key,
|
||||
required KeyboardScope scope,
|
||||
required String location,
|
||||
}) {
|
||||
final action = key['action']?.toString() ?? 'input';
|
||||
final value = key['value']?.toString() ?? '';
|
||||
final mods = KeyboardKeyMods.fromJson(key['mods']);
|
||||
|
||||
if (key.containsKey('mods') && key['mods'] != null && key['mods'] is! Map) {
|
||||
return '$location 的 "mods" 必须是对象,例如 {"ctrl": true}';
|
||||
}
|
||||
|
||||
if (scope == KeyboardScope.editor) {
|
||||
if (!KeyboardScopeHelper.editorActionValues.contains(action)) {
|
||||
return '$location 的动作 "$action" 不适用于编辑区,可用动作: '
|
||||
'${KeyboardScopeHelper.editorActionValues.join(' / ')}';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 终端作用域
|
||||
if (!KeyboardScopeHelper.terminalActionValues.contains(action)) {
|
||||
return '$location 的动作 "$action" 不适用于终端,可用动作: '
|
||||
'${KeyboardScopeHelper.terminalActionValues.join(' / ')}';
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case 'modifier':
|
||||
if (!KeyboardScopeHelper.terminalModifierValues.contains(value)) {
|
||||
return '$location 的修饰键取值 "$value" 无效,只能是 '
|
||||
'${KeyboardScopeHelper.terminalModifierValues.join(' / ')}';
|
||||
}
|
||||
if (mods.isNotEmpty) {
|
||||
return '$location 是修饰键动作,不应再附带 "mods"';
|
||||
}
|
||||
return null;
|
||||
|
||||
case 'key':
|
||||
if (value.isEmpty) {
|
||||
return '$location 缺少特殊键值 ("value")';
|
||||
}
|
||||
if (!KeyboardScopeHelper.isSupportedTerminalKey(value)) {
|
||||
return '$location 的特殊键 "$value" 在终端中不会产生任何输出,'
|
||||
'请选择受支持的命名键(如 escape / tab / arrowUp / f1)或字母键 keyA ~ keyZ';
|
||||
}
|
||||
return null;
|
||||
|
||||
case 'input':
|
||||
default:
|
||||
if (value.isEmpty) {
|
||||
return '$location 缺少文本内容 ("value")';
|
||||
}
|
||||
if (mods.isNotEmpty) {
|
||||
return '$location 是文本指令,附加 "mods" 不会生效'
|
||||
'(终端文本通道不接受修饰符,请改用"特殊键"类型)';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// 默认配置预设(编辑区)
|
||||
static VirtualKeyboardConfig defaultConfiguration() => _defaultConfig();
|
||||
|
||||
/// 按作用域返回默认配置预设
|
||||
static VirtualKeyboardConfig defaultForScope(KeyboardScope scope) =>
|
||||
scope == KeyboardScope.terminal ? defaultTerminalConfiguration() : _defaultConfig();
|
||||
|
||||
/// ## 终端小键盘 JSON 结构
|
||||
///
|
||||
/// ```json
|
||||
/// {
|
||||
/// "pages": [
|
||||
/// { "count": 7, "keys": [ [ <key>, <key>, ... ], ... ] }
|
||||
/// ]
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// 终端作用域下 `<key>` 有三种动作:
|
||||
///
|
||||
/// - **指令快捷键**:`{"label":"cd ..","action":"input","value":"cd ..","autoEnter":true}`
|
||||
/// `autoEnter` 为 true 时,发送文本后再补一个回车符 `\r`(等价于按下回车),
|
||||
/// 实现"一格执行命令";value 已以 `\r`/`\n` 结尾时不会重复补。
|
||||
/// - **特殊键**:`{"label":"^C","action":"key","value":"keyC","mods":{"ctrl":true}}`
|
||||
/// - `value` 取 xterm `TerminalKey` 的枚举名,可用集合见
|
||||
/// [KeyboardScopeHelper.terminalNamedKeys](31 个命名键)与
|
||||
/// [KeyboardScopeHelper.terminalSupportedKeyNames](再加 keyA~keyZ)
|
||||
/// - `mods` 形如 `{"ctrl":bool,"alt":bool,"shift":bool}`,**仅 `key` 动作有效**;
|
||||
/// 字母键必须且只能单独搭配 Ctrl 或 Alt,这是 xterm 输入链路的硬限制
|
||||
/// - **修饰键**:`{"label":"Ctrl","action":"modifier","value":"ctrl"}`
|
||||
/// `value` ∈ {ctrl, alt, shift},只切换运行时状态(单击一次性 / 双击锁定),不产生输出。
|
||||
///
|
||||
/// 编辑区作用域继续使用 `input` / `pair` / `command`;`mods` 与 `autoEnter`
|
||||
/// 在无值时导出省略,因此既有编辑区配置零迁移。
|
||||
///
|
||||
/// 默认终端配置预设:两行特殊键 + 修饰键
|
||||
///
|
||||
/// 行1:Esc Tab Ctrl Alt - ↑ 回车
|
||||
/// 行2:Ins End Shift : ← ↓ →
|
||||
static VirtualKeyboardConfig defaultTerminalConfiguration() {
|
||||
return const VirtualKeyboardConfig(
|
||||
pages: [
|
||||
KeyboardPageItem(
|
||||
count: 7,
|
||||
keys: [
|
||||
[
|
||||
KeyboardKeyItem(label: 'Esc', action: 'key', value: 'escape'),
|
||||
KeyboardKeyItem(label: 'Tab', icon: 'tab', action: 'key', value: 'tab'),
|
||||
KeyboardKeyItem(label: 'Ctrl', action: 'modifier', value: 'ctrl'),
|
||||
KeyboardKeyItem(label: 'Alt', action: 'modifier', value: 'alt'),
|
||||
KeyboardKeyItem(label: '-', action: 'input', value: '-'),
|
||||
KeyboardKeyItem(label: '↑', icon: 'arrow_up', action: 'key', value: 'arrowUp'),
|
||||
KeyboardKeyItem(label: '回车', icon: 'enter', action: 'key', value: 'enter'),
|
||||
],
|
||||
[
|
||||
KeyboardKeyItem(label: 'Ins', action: 'key', value: 'insert'),
|
||||
KeyboardKeyItem(label: 'End', action: 'key', value: 'end'),
|
||||
KeyboardKeyItem(label: 'Shift', action: 'modifier', value: 'shift'),
|
||||
KeyboardKeyItem(label: ':', action: 'input', value: ':'),
|
||||
KeyboardKeyItem(label: '←', icon: 'arrow_left', action: 'key', value: 'arrowLeft'),
|
||||
KeyboardKeyItem(label: '↓', icon: 'arrow_down', action: 'key', value: 'arrowDown'),
|
||||
KeyboardKeyItem(label: '→', icon: 'arrow_right', action: 'key', value: 'arrowRight'),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
static VirtualKeyboardConfig _defaultConfig() {
|
||||
return const VirtualKeyboardConfig(
|
||||
pages: [
|
||||
@@ -461,8 +861,8 @@ class VirtualKeyboardConfig {
|
||||
KeyboardKeyItem(label: '()', action: 'pair', value: '()', cursorOffset: -1),
|
||||
KeyboardKeyItem(label: '[]', action: 'pair', value: '[]', cursorOffset: -1),
|
||||
KeyboardKeyItem(label: '{}', action: 'pair', value: '{}', cursorOffset: -1),
|
||||
KeyboardKeyItem(label: '撤销', icon: 'undo', action: 'command', value: 'undo'),
|
||||
KeyboardKeyItem(label: '重做', icon: 'redo', action: 'command', value: 'redo'),
|
||||
KeyboardKeyItem(label: 'Undo', icon: 'undo', action: 'command', value: 'undo'),
|
||||
KeyboardKeyItem(label: 'Redo', icon: 'redo', action: 'command', value: 'redo'),
|
||||
],
|
||||
[
|
||||
KeyboardKeyItem(label: '<-', icon: 'arrow_left', action: 'command', value: 'cursor_left'),
|
||||
@@ -503,9 +903,19 @@ class VirtualKeyboardConfig {
|
||||
);
|
||||
}
|
||||
|
||||
/// 转换为格式化好的默认 JSON 字符串
|
||||
/// 转换为格式化好的默认 JSON 字符串(编辑区)
|
||||
static String defaultJsonPretty() {
|
||||
const encoder = JsonEncoder.withIndent(' ');
|
||||
return encoder.convert(_defaultConfig().toJson());
|
||||
}
|
||||
|
||||
/// 转换为格式化好的默认 JSON 字符串(终端)
|
||||
static String defaultTerminalJsonPretty() {
|
||||
const encoder = JsonEncoder.withIndent(' ');
|
||||
return encoder.convert(defaultTerminalConfiguration().toJson());
|
||||
}
|
||||
|
||||
/// 按作用域返回格式化好的默认 JSON 字符串
|
||||
static String defaultJsonPrettyFor(KeyboardScope scope) =>
|
||||
scope == KeyboardScope.terminal ? defaultTerminalJsonPretty() : defaultJsonPretty();
|
||||
}
|
||||
Reference in new issue
Block a user