2
This commit is contained in:
1 parent
9fbb414ee6
commit
4554f22c87
233 files changed
+147430
-605
No files matched your search
@@ -12,10 +12,14 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
final VoidCallback? onSave;
|
||||
final VoidCallback? onSaveAll;
|
||||
final VoidCallback? onRun;
|
||||
final VoidCallback? onRunTasks;
|
||||
final VoidCallback? onTerminal;
|
||||
final VoidCallback? onCloseAllTabs;
|
||||
final VoidCallback? onCloseProject;
|
||||
final VoidCallback? onSettings;
|
||||
final VoidCallback? onProjectDetect;
|
||||
final VoidCallback? onEditRunTasks;
|
||||
final bool isDetecting;
|
||||
|
||||
const CodeEditorAppBar({
|
||||
super.key,
|
||||
@@ -25,10 +29,14 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
this.onSave,
|
||||
this.onSaveAll,
|
||||
this.onRun,
|
||||
this.onRunTasks,
|
||||
this.onTerminal,
|
||||
this.onCloseAllTabs,
|
||||
this.onCloseProject,
|
||||
this.onSettings,
|
||||
this.onProjectDetect,
|
||||
this.onEditRunTasks,
|
||||
this.isDetecting = false,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -121,28 +129,23 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
onPressed: onSave ?? () {},
|
||||
),
|
||||
),
|
||||
Focus(
|
||||
canRequestFocus: false,
|
||||
skipTraversal: true,
|
||||
child: IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.terminal),
|
||||
tooltip: '终端',
|
||||
onPressed: onTerminal ??
|
||||
() {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (context) => const TerminalView(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
_MoreMenuButton(
|
||||
onSaveAll: onSaveAll,
|
||||
onRunTasks: onRunTasks,
|
||||
onTerminal: onTerminal ??
|
||||
() {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (context) => const TerminalView(),
|
||||
),
|
||||
);
|
||||
},
|
||||
onProjectDetect: onProjectDetect,
|
||||
onEditRunTasks: onEditRunTasks,
|
||||
onCloseAllTabs: onCloseAllTabs,
|
||||
onCloseProject: onCloseProject,
|
||||
onSettings: onSettings,
|
||||
isDetecting: isDetecting,
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -153,15 +156,25 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
/// 基于 OverlayEntry 的右上角更多菜单组件(带对齐右上角缩放淡入淡出动画,非模态不关闭键盘)
|
||||
class _MoreMenuButton extends StatefulWidget {
|
||||
final VoidCallback? onSaveAll;
|
||||
final VoidCallback? onRunTasks;
|
||||
final VoidCallback? onTerminal;
|
||||
final VoidCallback? onProjectDetect;
|
||||
final VoidCallback? onEditRunTasks;
|
||||
final VoidCallback? onCloseAllTabs;
|
||||
final VoidCallback? onCloseProject;
|
||||
final VoidCallback? onSettings;
|
||||
final bool isDetecting;
|
||||
|
||||
const _MoreMenuButton({
|
||||
this.onSaveAll,
|
||||
this.onRunTasks,
|
||||
this.onTerminal,
|
||||
this.onProjectDetect,
|
||||
this.onEditRunTasks,
|
||||
this.onCloseAllTabs,
|
||||
this.onCloseProject,
|
||||
this.onSettings,
|
||||
this.isDetecting = false,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -280,12 +293,14 @@ class _MoreMenuButtonState extends State<_MoreMenuButton> with SingleTickerProvi
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Container(
|
||||
width: 170,
|
||||
// 菜单宽度随系统字号放大,避免大字号下文案被截断(上限避免超出窄屏)
|
||||
width: (170.0 * MediaQuery.textScalerOf(context).scale(1.0)).clamp(170.0, 280.0),
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// 分组 1: 保存所有,运行任务,终端
|
||||
_buildMenuItem(
|
||||
icon: Icons.save_as_outlined,
|
||||
title: l10n.saveAll,
|
||||
@@ -295,11 +310,19 @@ class _MoreMenuButtonState extends State<_MoreMenuButton> with SingleTickerProvi
|
||||
},
|
||||
),
|
||||
_buildMenuItem(
|
||||
icon: Icons.close_fullscreen_outlined,
|
||||
title: l10n.closeAllTabs,
|
||||
icon: Icons.playlist_play,
|
||||
title: l10n.runTasks,
|
||||
iconColor: onSurfaceColor,
|
||||
onTap: () {
|
||||
_closeMenu(onClosed: widget.onCloseAllTabs);
|
||||
_closeMenu(onClosed: widget.onRunTasks);
|
||||
},
|
||||
),
|
||||
_buildMenuItem(
|
||||
icon: Icons.terminal,
|
||||
title: l10n.terminal,
|
||||
iconColor: onSurfaceColor,
|
||||
onTap: () {
|
||||
_closeMenu(onClosed: widget.onTerminal);
|
||||
},
|
||||
),
|
||||
Divider(
|
||||
@@ -307,6 +330,39 @@ class _MoreMenuButtonState extends State<_MoreMenuButton> with SingleTickerProvi
|
||||
thickness: 0.8,
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
),
|
||||
// 分组 2: 项目探测,运行任务编辑
|
||||
_buildMenuItem(
|
||||
icon: widget.isDetecting ? Icons.hourglass_top : Icons.radar_outlined,
|
||||
title: widget.isDetecting ? l10n.projectDetecting : l10n.projectDetect,
|
||||
iconColor: onSurfaceColor,
|
||||
enabled: !widget.isDetecting,
|
||||
onTap: () {
|
||||
if (widget.isDetecting) return;
|
||||
_closeMenu(onClosed: widget.onProjectDetect);
|
||||
},
|
||||
),
|
||||
_buildMenuItem(
|
||||
icon: Icons.tune_outlined,
|
||||
title: l10n.editRunTasks,
|
||||
iconColor: onSurfaceColor,
|
||||
onTap: () {
|
||||
_closeMenu(onClosed: widget.onEditRunTasks);
|
||||
},
|
||||
),
|
||||
Divider(
|
||||
height: 9,
|
||||
thickness: 0.8,
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
),
|
||||
// 分组 3: 关闭所有标签,关闭当前项目
|
||||
_buildMenuItem(
|
||||
icon: Icons.close_fullscreen_outlined,
|
||||
title: l10n.closeAllTabs,
|
||||
iconColor: onSurfaceColor,
|
||||
onTap: () {
|
||||
_closeMenu(onClosed: widget.onCloseAllTabs);
|
||||
},
|
||||
),
|
||||
_buildMenuItem(
|
||||
icon: Icons.folder_off_outlined,
|
||||
title: l10n.closeProject,
|
||||
@@ -320,6 +376,7 @@ class _MoreMenuButtonState extends State<_MoreMenuButton> with SingleTickerProvi
|
||||
thickness: 0.8,
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
),
|
||||
// 分组 4: 设置
|
||||
_buildMenuItem(
|
||||
icon: Icons.settings,
|
||||
title: l10n.settings,
|
||||
@@ -348,10 +405,18 @@ class _MoreMenuButtonState extends State<_MoreMenuButton> with SingleTickerProvi
|
||||
required Color iconColor,
|
||||
required VoidCallback onTap,
|
||||
bool isDestructive = false,
|
||||
bool enabled = true,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
final effectiveColor = enabled
|
||||
? (isDestructive ? theme.colorScheme.error : iconColor)
|
||||
: theme.colorScheme.outline.withValues(alpha: 0.5);
|
||||
final effectiveTextColor = enabled
|
||||
? (isDestructive ? theme.colorScheme.error : theme.colorScheme.onSurface)
|
||||
: theme.colorScheme.outline.withValues(alpha: 0.5);
|
||||
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
onTap: enabled ? onTap : null,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
|
||||
child: Row(
|
||||
@@ -360,14 +425,18 @@ class _MoreMenuButtonState extends State<_MoreMenuButton> with SingleTickerProvi
|
||||
Icon(
|
||||
icon,
|
||||
size: 18,
|
||||
color: isDestructive ? theme.colorScheme.error : iconColor,
|
||||
color: effectiveColor,
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 14.0,
|
||||
color: isDestructive ? theme.colorScheme.error : theme.colorScheme.onSurface,
|
||||
Flexible(
|
||||
child: Text(
|
||||
title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 14.0,
|
||||
color: effectiveTextColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:math';
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -17,7 +18,14 @@ class _EditorContextMenuItem extends PopupMenuItem<void> implements PreferredSiz
|
||||
children: [
|
||||
Icon(icon, size: 16),
|
||||
const SizedBox(width: 8),
|
||||
Text(text, style: const TextStyle(fontSize: 13)),
|
||||
Flexible(
|
||||
child: Text(
|
||||
text,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(fontSize: 13),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -71,8 +79,34 @@ class _MobileSelectionToolbarWidgetState extends State<_MobileSelectionToolbarWi
|
||||
widget.controller.selection.extentOffset != -1 &&
|
||||
!widget.controller.selection.isCollapsed;
|
||||
|
||||
final mediaQuery = MediaQuery.of(context);
|
||||
final availableBottom = mediaQuery.size.height - mediaQuery.viewInsets.bottom;
|
||||
TextSelectionToolbarAnchors effectiveAnchors = widget.anchors;
|
||||
|
||||
final primary = effectiveAnchors.primaryAnchor;
|
||||
final secondary = effectiveAnchors.secondaryAnchor;
|
||||
|
||||
// 针对虚拟键盘或底栏的全局安全高度校准:
|
||||
if (secondary != null && secondary.dy + 65.0 > availableBottom) {
|
||||
effectiveAnchors = TextSelectionToolbarAnchors(
|
||||
primaryAnchor: Offset(
|
||||
secondary.dx.clamp(120.0, max(120.0, mediaQuery.size.width - 120.0)),
|
||||
(availableBottom - 12.0).clamp(mediaQuery.padding.top + 48.0, availableBottom),
|
||||
),
|
||||
secondaryAnchor: null,
|
||||
);
|
||||
} else if (primary.dy + 60.0 > availableBottom && primary.dx >= 0) {
|
||||
effectiveAnchors = TextSelectionToolbarAnchors(
|
||||
primaryAnchor: Offset(
|
||||
primary.dx.clamp(120.0, max(120.0, mediaQuery.size.width - 120.0)),
|
||||
(availableBottom - 12.0).clamp(mediaQuery.padding.top + 48.0, availableBottom),
|
||||
),
|
||||
secondaryAnchor: null,
|
||||
);
|
||||
}
|
||||
|
||||
return AdaptiveTextSelectionToolbar.buttonItems(
|
||||
anchors: widget.anchors,
|
||||
anchors: effectiveAnchors,
|
||||
buttonItems: [
|
||||
if (hasSelection)
|
||||
ContextMenuButtonItem(
|
||||
@@ -164,10 +198,11 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
anchors: anchors,
|
||||
);
|
||||
} else {
|
||||
final safeAnchors = _sanitizeMobileAnchors(anchors, renderRect);
|
||||
_mobileController.show(
|
||||
context: context,
|
||||
controller: controller,
|
||||
anchors: anchors,
|
||||
anchors: safeAnchors,
|
||||
renderRect: renderRect,
|
||||
layerLink: layerLink,
|
||||
visibility: visibility,
|
||||
@@ -175,6 +210,59 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
}
|
||||
}
|
||||
|
||||
/// 智能校准移动端选区工具栏锚点:
|
||||
/// 1. 优先展示在选区上方(primaryAnchor),避免遮挡当前选中的代码内容及下方代码;
|
||||
/// 2. 当选区位于视口顶部(上方空间不足 54px 时),智能翻转至选区下方(secondaryAnchor);
|
||||
/// 3. 当选区靠近视口底部或键盘时,保护次锚点不落入键盘/底栏区域,坚决保持在上方;
|
||||
/// 4. 水平坐标智能居中并进行安全边缘约束(左右各留出 120px 缓冲),防止菜单按钮溢出屏幕。
|
||||
static TextSelectionToolbarAnchors _sanitizeMobileAnchors(
|
||||
TextSelectionToolbarAnchors anchors,
|
||||
Rect renderRect,
|
||||
) {
|
||||
final primary = anchors.primaryAnchor;
|
||||
final secondary = anchors.secondaryAnchor;
|
||||
|
||||
// 确定水平基准坐标并进行安全边距夹取(左右两端预留充足空间以保证多个操作按钮完整可见)
|
||||
final double rawX = (primary.dx > 0) ? primary.dx : (secondary?.dx ?? renderRect.center.dx);
|
||||
final double safeX = (renderRect.width > 240.0)
|
||||
? rawX.clamp(renderRect.left + 120.0, renderRect.right - 120.0)
|
||||
: renderRect.center.dx;
|
||||
|
||||
// 计算选区上方与下方的垂直可用净空间
|
||||
final double topY = (primary.dy > 0) ? primary.dy : (secondary?.dy ?? renderRect.top + 60.0);
|
||||
final double bottomY = (secondary != null && secondary.dy > 0) ? secondary.dy : (topY + 30.0);
|
||||
|
||||
final double spaceAbove = topY - renderRect.top;
|
||||
final double spaceBelow = renderRect.bottom - bottomY;
|
||||
|
||||
// 规则 1:选区上方空间充足(>= 54.0px,足够容纳工具栏),置于选区上方!
|
||||
// 选区本身与下方的代码行将保持 100% 清晰可见,绝不遮挡。
|
||||
if (spaceAbove >= 54.0) {
|
||||
return TextSelectionToolbarAnchors(
|
||||
primaryAnchor: Offset(safeX, topY),
|
||||
secondaryAnchor: spaceBelow >= 64.0 ? Offset(safeX, bottomY) : null,
|
||||
);
|
||||
}
|
||||
|
||||
// 规则 2:选区位于第一行/第二行(上方空间不足 54px),但下方空间充足(>= 64px)
|
||||
// 智能翻转至选区下方,保证不超出屏幕顶部也不遮挡第一行
|
||||
if (spaceBelow >= 64.0) {
|
||||
return TextSelectionToolbarAnchors(
|
||||
primaryAnchor: const Offset(-10000, -10000),
|
||||
secondaryAnchor: Offset(safeX, bottomY),
|
||||
);
|
||||
}
|
||||
|
||||
// 规则 3:选区跨越很大或上下空间均受限时,安全悬浮于可视区域上边缘内侧
|
||||
return TextSelectionToolbarAnchors(
|
||||
primaryAnchor: Offset(
|
||||
safeX,
|
||||
(renderRect.top + 56.0).clamp(renderRect.top + 10.0, renderRect.bottom - 10.0),
|
||||
),
|
||||
secondaryAnchor: null,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _showDesktopMenu({
|
||||
required BuildContext context,
|
||||
required CodeLineEditingController controller,
|
||||
|
||||
@@ -82,7 +82,8 @@ class CodeEditorTabBar extends StatelessWidget {
|
||||
builder: (context, constraints) {
|
||||
final totalWidth = constraints.maxWidth;
|
||||
final tabCount = tabs.length;
|
||||
final equalShare = math.max(28.0, totalWidth / tabCount);
|
||||
// 下限需容纳内边距(8) + 关闭按钮(18) + 脏标记(10),否则标签内 Row 会右溢出
|
||||
final equalShare = math.max(40.0, totalWidth / tabCount);
|
||||
|
||||
return Theme(
|
||||
data: theme.copyWith(
|
||||
@@ -178,26 +179,21 @@ class CodeEditorTabBar extends StatelessWidget {
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (tab.isModified)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 2.0),
|
||||
child: Text(
|
||||
'*',
|
||||
// 脏标记并入同一个 Text.rich,避免它在极窄标签里单独撑破 Row
|
||||
child: Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
if (tab.isModified)
|
||||
TextSpan(
|
||||
text: '*',
|
||||
style: TextStyle(
|
||||
color: theme.colorScheme.error,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 12.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
displayName,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
TextSpan(
|
||||
text: displayName,
|
||||
style: TextStyle(
|
||||
fontSize: 12.0,
|
||||
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
|
||||
@@ -206,8 +202,10 @@ class CodeEditorTabBar extends StatelessWidget {
|
||||
: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
InkResponse(
|
||||
|
||||
@@ -459,6 +459,8 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
scrollController: _scrollController,
|
||||
wordWrap: activeWordWrap,
|
||||
toolbarController: _toolbarController,
|
||||
margin: const EdgeInsets.only(left: 2.0, top: 4.0, right: 8, bottom: 8.0),
|
||||
padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
|
||||
style: CodeEditorStyle(
|
||||
fontSize: displayFontSize,
|
||||
textColor: activeTheme.textColor,
|
||||
|
||||
@@ -0,0 +1,470 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../l10n/app_localizations.dart';
|
||||
|
||||
/// 颜色 → `#RRGGBB` 文本(用于工具提示与设置项副标题)
|
||||
String colorToHex(Color color) {
|
||||
final argb = color.toARGB32();
|
||||
final rgb = argb & 0xFFFFFF;
|
||||
return '#${rgb.toRadixString(16).padLeft(6, '0').toUpperCase()}';
|
||||
}
|
||||
|
||||
/// 通用交互式调色板弹窗:
|
||||
///
|
||||
/// 包含 HSV 2D 饱和度/明度调色区、色相滑条、HEX 十六进制编辑、
|
||||
/// 当前/新色对比预览,以及经典预设色板。
|
||||
class ColorPaletteDialog extends StatefulWidget {
|
||||
const ColorPaletteDialog({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.current,
|
||||
required this.palette,
|
||||
});
|
||||
|
||||
final String title;
|
||||
final Color current;
|
||||
final List<Color> palette;
|
||||
|
||||
/// 界面主题色板:Material 主色 + 中性色
|
||||
static const List<Color> uiPalette = [
|
||||
Color(0xFFF44336),
|
||||
Color(0xFFE91E63),
|
||||
Color(0xFF9C27B0),
|
||||
Color(0xFF673AB7),
|
||||
Color(0xFF3F51B5),
|
||||
Color(0xFF2196F3),
|
||||
Color(0xFF03A9F4),
|
||||
Color(0xFF00BCD4),
|
||||
Color(0xFF009688),
|
||||
Color(0xFF4CAF50),
|
||||
Color(0xFF8BC34A),
|
||||
Color(0xFFCDDC39),
|
||||
Color(0xFFFFEB3B),
|
||||
Color(0xFFFFC107),
|
||||
Color(0xFFFF9800),
|
||||
Color(0xFFFF5722),
|
||||
Color(0xFF795548),
|
||||
Color(0xFF607D8B),
|
||||
Color(0xFF9E9E9E),
|
||||
Color(0xFF000000),
|
||||
];
|
||||
|
||||
/// 终端背景色板:常用深色配色 + 少量浅色
|
||||
static const List<Color> terminalPalette = [
|
||||
Color(0xFF000000),
|
||||
Color(0xFF0D1117),
|
||||
Color(0xFF101418),
|
||||
Color(0xFF181818),
|
||||
Color(0xFF1E1E1E),
|
||||
Color(0xFF1E1E2E),
|
||||
Color(0xFF212121),
|
||||
Color(0xFF263238),
|
||||
Color(0xFF282C34),
|
||||
Color(0xFF2B2B2B),
|
||||
Color(0xFF2D2D30),
|
||||
Color(0xFF303030),
|
||||
Color(0xFF1B2B34),
|
||||
Color(0xFF002B36),
|
||||
Color(0xFF073642),
|
||||
Color(0xFF3C3836),
|
||||
Color(0xFFF5F5F5),
|
||||
Color(0xFFFFFFFF),
|
||||
];
|
||||
|
||||
static Future<Color?> show(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
required Color current,
|
||||
required List<Color> palette,
|
||||
}) {
|
||||
return showDialog<Color>(
|
||||
context: context,
|
||||
builder: (_) => ColorPaletteDialog(
|
||||
title: title,
|
||||
current: current,
|
||||
palette: palette,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
State<ColorPaletteDialog> createState() => _ColorPaletteDialogState();
|
||||
}
|
||||
|
||||
class _ColorPaletteDialogState extends State<ColorPaletteDialog> {
|
||||
late double _hue;
|
||||
late double _saturation;
|
||||
late double _value;
|
||||
late Color _currentColor;
|
||||
late TextEditingController _hexController;
|
||||
bool _isInternalHexUpdate = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_currentColor = widget.current;
|
||||
final hsv = HSVColor.fromColor(widget.current);
|
||||
_hue = hsv.hue;
|
||||
_saturation = hsv.saturation;
|
||||
_value = hsv.value;
|
||||
_hexController = TextEditingController(text: colorToHex(widget.current));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_hexController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _updateColor(Color color, {bool updateHex = true}) {
|
||||
final hsv = HSVColor.fromColor(color);
|
||||
setState(() {
|
||||
_currentColor = color;
|
||||
if (hsv.saturation > 0 || color.toARGB32() != Colors.black.toARGB32()) {
|
||||
_hue = hsv.hue;
|
||||
}
|
||||
_saturation = hsv.saturation;
|
||||
_value = hsv.value;
|
||||
if (updateHex) {
|
||||
_isInternalHexUpdate = true;
|
||||
_hexController.text = colorToHex(color);
|
||||
_isInternalHexUpdate = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _onHexChanged(String text) {
|
||||
if (_isInternalHexUpdate) return;
|
||||
String clean = text.trim();
|
||||
if (clean.startsWith('#')) clean = clean.substring(1);
|
||||
if (clean.length == 6) {
|
||||
final val = int.tryParse(clean, radix: 16);
|
||||
if (val != null) {
|
||||
final color = Color(0xFF000000 | val);
|
||||
final hsv = HSVColor.fromColor(color);
|
||||
setState(() {
|
||||
_currentColor = color;
|
||||
if (hsv.saturation > 0 || color.toARGB32() != Colors.black.toARGB32()) {
|
||||
_hue = hsv.hue;
|
||||
}
|
||||
_saturation = hsv.saturation;
|
||||
_value = hsv.value;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _onSvPan(Offset localPosition, double width, double height) {
|
||||
final s = (localPosition.dx / width).clamp(0.0, 1.0);
|
||||
final v = (1.0 - (localPosition.dy / height)).clamp(0.0, 1.0);
|
||||
setState(() {
|
||||
_saturation = s;
|
||||
_value = v;
|
||||
_currentColor = HSVColor.fromAHSV(1.0, _hue, _saturation, _value).toColor();
|
||||
_isInternalHexUpdate = true;
|
||||
_hexController.text = colorToHex(_currentColor);
|
||||
_isInternalHexUpdate = false;
|
||||
});
|
||||
}
|
||||
|
||||
void _onHuePan(Offset localPosition, double width) {
|
||||
final h = (localPosition.dx / width * 360.0).clamp(0.0, 360.0);
|
||||
setState(() {
|
||||
_hue = h == 360.0 ? 0.0 : h;
|
||||
_currentColor = HSVColor.fromAHSV(1.0, _hue, _saturation, _value).toColor();
|
||||
_isInternalHexUpdate = true;
|
||||
_hexController.text = colorToHex(_currentColor);
|
||||
_isInternalHexUpdate = false;
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildSaturationValuePicker(double width, double height) {
|
||||
return GestureDetector(
|
||||
onPanDown: (details) => _onSvPan(details.localPosition, width, height),
|
||||
onPanUpdate: (details) => _onSvPan(details.localPosition, width, height),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: SizedBox(
|
||||
width: width,
|
||||
height: height,
|
||||
child: Stack(
|
||||
children: [
|
||||
// 色相底层
|
||||
Container(
|
||||
color: HSVColor.fromAHSV(1.0, _hue, 1.0, 1.0).toColor(),
|
||||
),
|
||||
// 水平渐变(白色 -> 透明)
|
||||
Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.centerLeft,
|
||||
end: Alignment.centerRight,
|
||||
colors: [Colors.white, Color(0x00FFFFFF)],
|
||||
),
|
||||
),
|
||||
),
|
||||
// 垂直渐变(透明 -> 黑色)
|
||||
Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Colors.transparent, Colors.black],
|
||||
),
|
||||
),
|
||||
),
|
||||
// 取色定位圆环指示器
|
||||
Positioned(
|
||||
left: (_saturation * width - 10).clamp(-10.0, width - 10.0),
|
||||
top: ((1.0 - _value) * height - 10).clamp(-10.0, height - 10.0),
|
||||
child: IgnorePointer(
|
||||
child: Container(
|
||||
width: 20,
|
||||
height: 20,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: Colors.white, width: 2.5),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Colors.black45,
|
||||
blurRadius: 4,
|
||||
offset: Offset(0, 1),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHueSlider(double width) {
|
||||
const double sliderHeight = 22.0;
|
||||
return GestureDetector(
|
||||
onPanDown: (details) => _onHuePan(details.localPosition, width),
|
||||
onPanUpdate: (details) => _onHuePan(details.localPosition, width),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(sliderHeight / 2),
|
||||
child: SizedBox(
|
||||
width: width,
|
||||
height: sliderHeight,
|
||||
child: Stack(
|
||||
children: [
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(sliderHeight / 2),
|
||||
gradient: const LinearGradient(
|
||||
colors: [
|
||||
Color(0xFFFF0000),
|
||||
Color(0xFFFFFF00),
|
||||
Color(0xFF00FF00),
|
||||
Color(0xFF00FFFF),
|
||||
Color(0xFF0000FF),
|
||||
Color(0xFFFF00FF),
|
||||
Color(0xFFFF0000),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
left: ((_hue / 360.0) * width - 10).clamp(-10.0, width - 10.0),
|
||||
top: 1,
|
||||
child: IgnorePointer(
|
||||
child: Container(
|
||||
width: 20,
|
||||
height: sliderHeight - 2,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: Colors.white, width: 2.5),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Colors.black45,
|
||||
blurRadius: 3,
|
||||
offset: Offset(0, 1),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPreviewAndHex(BuildContext context, AppLocalizations l10n) {
|
||||
final theme = Theme.of(context);
|
||||
return Row(
|
||||
children: [
|
||||
// 初始颜色(点击可快速重置回原色)
|
||||
Tooltip(
|
||||
message: '${l10n.currentColor}: ${colorToHex(widget.current)}',
|
||||
child: InkWell(
|
||||
onTap: () => _updateColor(widget.current),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: widget.current,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: theme.colorScheme.outlineVariant),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Icon(Icons.arrow_forward, size: 18, color: theme.colorScheme.outline),
|
||||
const SizedBox(width: 8),
|
||||
// 调色后的实时新色预览
|
||||
Tooltip(
|
||||
message: '${l10n.newColor}: ${colorToHex(_currentColor)}',
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: _currentColor,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: theme.colorScheme.primary,
|
||||
width: 2,
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: _currentColor.withValues(alpha: 0.35),
|
||||
blurRadius: 6,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
// HEX 颜色输入框
|
||||
SizedBox(
|
||||
width: 120,
|
||||
height: 42,
|
||||
child: TextField(
|
||||
controller: _hexController,
|
||||
onChanged: _onHexChanged,
|
||||
style: const TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
),
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
|
||||
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
|
||||
labelText: l10n.hexColor,
|
||||
counterText: '',
|
||||
),
|
||||
maxLength: 7,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPresets(BuildContext context, AppLocalizations l10n) {
|
||||
final theme = Theme.of(context);
|
||||
final currentRgb = _currentColor.toARGB32() & 0xFFFFFF;
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
l10n.presetColors,
|
||||
style: theme.textTheme.labelMedium?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 6,
|
||||
children: [
|
||||
for (final color in widget.palette)
|
||||
Tooltip(
|
||||
message: colorToHex(color),
|
||||
child: InkWell(
|
||||
key: ValueKey<int>(color.toARGB32()),
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
onTap: () => _updateColor(color),
|
||||
child: Container(
|
||||
width: 28,
|
||||
height: 28,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(
|
||||
color: (color.toARGB32() & 0xFFFFFF) == currentRgb
|
||||
? theme.colorScheme.primary
|
||||
: theme.colorScheme.outlineVariant,
|
||||
width: (color.toARGB32() & 0xFFFFFF) == currentRgb ? 2.5 : 1,
|
||||
),
|
||||
),
|
||||
child: (color.toARGB32() & 0xFFFFFF) == currentRgb
|
||||
? Icon(
|
||||
Icons.check,
|
||||
size: 16,
|
||||
color: color.computeLuminance() > 0.5
|
||||
? Colors.black87
|
||||
: Colors.white,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
|
||||
return AlertDialog(
|
||||
title: Text(widget.title),
|
||||
content: SizedBox(
|
||||
width: 280,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
_buildSaturationValuePicker(280, 130),
|
||||
const SizedBox(height: 12),
|
||||
_buildHueSlider(280),
|
||||
const SizedBox(height: 12),
|
||||
_buildPreviewAndHex(context, l10n),
|
||||
const SizedBox(height: 12),
|
||||
const Divider(height: 1),
|
||||
const SizedBox(height: 10),
|
||||
_buildPresets(context, l10n),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(l10n.cancel),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(context).pop(_currentColor),
|
||||
child: Text(l10n.confirm),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,13 @@ class DistroSelectorDialog extends StatelessWidget {
|
||||
children: [
|
||||
const Icon(Icons.layers_outlined, size: 18),
|
||||
const SizedBox(width: 8),
|
||||
Text(l10n.importBuiltinAlpine),
|
||||
Flexible(
|
||||
child: Text(
|
||||
l10n.importBuiltinAlpine,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -70,7 +76,13 @@ class DistroSelectorDialog extends StatelessWidget {
|
||||
children: [
|
||||
const Icon(Icons.folder_open_outlined, size: 18),
|
||||
const SizedBox(width: 8),
|
||||
Text(l10n.importExternalTarGz),
|
||||
Flexible(
|
||||
child: Text(
|
||||
l10n.importExternalTarGz,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -289,11 +289,15 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
children: [
|
||||
Icon(icon, size: 18, color: iconColor),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: isDestructive
|
||||
? TextStyle(color: theme.colorScheme.error)
|
||||
: TextStyle(color: theme.colorScheme.onSurface),
|
||||
Flexible(
|
||||
child: Text(
|
||||
title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: isDestructive
|
||||
? TextStyle(color: theme.colorScheme.error)
|
||||
: TextStyle(color: theme.colorScheme.onSurface),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../models/run_task.dart';
|
||||
|
||||
/// 运行任务选择弹窗(带顶部搜索栏、区分系统探测与自定义任务,点击直接调度执行)
|
||||
class RunTasksDialog extends StatefulWidget {
|
||||
final List<RunTask> customTasks;
|
||||
final List<RunTask> detectedTasks;
|
||||
final String? lastRunTaskId;
|
||||
final ValueChanged<RunTask> onTaskSelected;
|
||||
final VoidCallback onEditCustomTasks;
|
||||
|
||||
const RunTasksDialog({
|
||||
super.key,
|
||||
required this.customTasks,
|
||||
required this.detectedTasks,
|
||||
this.lastRunTaskId,
|
||||
required this.onTaskSelected,
|
||||
required this.onEditCustomTasks,
|
||||
});
|
||||
|
||||
static Future<void> show({
|
||||
required BuildContext context,
|
||||
required List<RunTask> customTasks,
|
||||
required List<RunTask> detectedTasks,
|
||||
String? lastRunTaskId,
|
||||
required ValueChanged<RunTask> onTaskSelected,
|
||||
required VoidCallback onEditCustomTasks,
|
||||
}) {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
builder: (ctx) => RunTasksDialog(
|
||||
customTasks: customTasks,
|
||||
detectedTasks: detectedTasks,
|
||||
lastRunTaskId: lastRunTaskId,
|
||||
onTaskSelected: onTaskSelected,
|
||||
onEditCustomTasks: onEditCustomTasks,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
State<RunTasksDialog> createState() => _RunTasksDialogState();
|
||||
}
|
||||
|
||||
class _RunTasksDialogState extends State<RunTasksDialog> {
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
String _searchQuery = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_searchController.addListener(() {
|
||||
setState(() {
|
||||
_searchQuery = _searchController.text.trim().toLowerCase();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool _matchesQuery(RunTask task) {
|
||||
if (_searchQuery.isEmpty) return true;
|
||||
return task.name.toLowerCase().contains(_searchQuery) ||
|
||||
task.command.toLowerCase().contains(_searchQuery) ||
|
||||
(task.description?.toLowerCase().contains(_searchQuery) ?? false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final filteredCustom = widget.customTasks.where(_matchesQuery).toList();
|
||||
final filteredDetected = widget.detectedTasks.where(_matchesQuery).toList();
|
||||
final hasAny = filteredCustom.isNotEmpty || filteredDetected.isNotEmpty;
|
||||
|
||||
return Dialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 560, maxHeight: 620),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// 标题栏:简洁标题 + 编辑按钮 + 关闭 X
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.runTasks,
|
||||
style: theme.textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.tune_outlined),
|
||||
tooltip: l10n.editCustomTasksTooltip,
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
widget.onEditCustomTasks();
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: l10n.close,
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// 搜索输入框
|
||||
TextField(
|
||||
controller: _searchController,
|
||||
decoration: InputDecoration(
|
||||
hintText: l10n.searchTasksHint,
|
||||
prefixIcon: const Icon(Icons.search),
|
||||
suffixIcon: _searchQuery.isNotEmpty
|
||||
? IconButton(
|
||||
icon: const Icon(Icons.clear, size: 18),
|
||||
onPressed: () => _searchController.clear(),
|
||||
)
|
||||
: null,
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// 任务展示列表
|
||||
Expanded(
|
||||
child: !hasAny
|
||||
? Center(
|
||||
child: Text(
|
||||
_searchQuery.isEmpty ? l10n.noTasksAvailable : l10n.noMatchingTasks,
|
||||
style: theme.textTheme.bodyMedium?.copyWith(color: theme.colorScheme.outline),
|
||||
),
|
||||
)
|
||||
: ListView(
|
||||
children: [
|
||||
if (filteredDetected.isNotEmpty) ...[
|
||||
_buildSectionHeader(context, l10n.systemDetectedTasks, Icons.radar_outlined),
|
||||
...filteredDetected.map((task) => _buildTaskItem(context, task)),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
if (filteredCustom.isNotEmpty) ...[
|
||||
_buildSectionHeader(context, l10n.userCustomTasks, Icons.person_outline),
|
||||
...filteredCustom.map((task) => _buildTaskItem(context, task)),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSectionHeader(BuildContext context, String title, IconData icon) {
|
||||
final theme = Theme.of(context);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, size: 16, color: theme.colorScheme.primary),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
title,
|
||||
style: theme.textTheme.labelMedium?.copyWith(
|
||||
color: theme.colorScheme.primary,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTaskItem(BuildContext context, RunTask task) {
|
||||
final theme = Theme.of(context);
|
||||
final isLastRun = widget.lastRunTaskId != null && widget.lastRunTaskId == task.id;
|
||||
|
||||
final cardColor = isLastRun
|
||||
? Colors.green.withValues(alpha: 0.15)
|
||||
: theme.colorScheme.surfaceContainerHighest.withValues(alpha: 0.4);
|
||||
|
||||
final borderColor = isLastRun
|
||||
? Colors.green.withValues(alpha: 0.8)
|
||||
: theme.colorScheme.outlineVariant.withValues(alpha: 0.5);
|
||||
|
||||
return Card(
|
||||
margin: const EdgeInsets.symmetric(vertical: 4),
|
||||
elevation: 0,
|
||||
color: cardColor,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
side: BorderSide(color: borderColor, width: isLastRun ? 1.5 : 1.0),
|
||||
),
|
||||
child: ListTile(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||
dense: true,
|
||||
title: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
task.name,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w600,
|
||||
color: isLastRun ? Colors.green.shade700 : null,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (isLastRun)
|
||||
Container(
|
||||
margin: const EdgeInsets.only(left: 6),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.green.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.history,
|
||||
size: 14,
|
||||
color: Colors.green.shade700,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
subtitle: (task.description != null && task.description!.trim().isNotEmpty)
|
||||
? Text(
|
||||
task.description!.trim(),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
trailing: CircleAvatar(
|
||||
radius: 16,
|
||||
backgroundColor: isLastRun
|
||||
? Colors.green.withValues(alpha: 0.25)
|
||||
: theme.colorScheme.primaryContainer,
|
||||
foregroundColor: isLastRun ? Colors.green : theme.colorScheme.onPrimaryContainer,
|
||||
child: const Icon(Icons.play_arrow, size: 20),
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
widget.onTaskSelected(task);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/providers/distro_provider.dart';
|
||||
import 'package:code_editor/providers/project_provider.dart';
|
||||
import 'package:code_editor/providers/terminal_provider.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:code_editor/widgets/terminal_session_item_widget.dart';
|
||||
@@ -60,12 +61,16 @@ class TerminalDrawer extends StatelessWidget {
|
||||
DialogUtils.showToast(context, l10n.noSystemSelectedWarning);
|
||||
return;
|
||||
}
|
||||
// 基于当前选择的系统创建新终端
|
||||
// 基于当前选择的系统创建新终端并直接激活进入
|
||||
final projectRoot = context.read<ProjectProvider?>()?.rootPath;
|
||||
provider.createSession(
|
||||
name: l10n.sessionDefaultName,
|
||||
distroId: currentSystem,
|
||||
activate: false,
|
||||
workspacePath: projectRoot,
|
||||
activate: true,
|
||||
);
|
||||
// 自动关闭右侧抽屉,直接进入新创建的会话
|
||||
Navigator.of(context).maybePop();
|
||||
},
|
||||
),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
import '../models/virtual_keyboard_config.dart';
|
||||
import 'virtual_keyboard_sink.dart';
|
||||
|
||||
/// 解析 xterm 的 [TerminalKey] 枚举名,失败返回 null。
|
||||
///
|
||||
/// 模型层只保存字符串名(不依赖 xterm),由这里完成到枚举的映射,
|
||||
/// 名称的合法性在保存配置时已由 [KeyboardScopeHelper.isSupportedTerminalKey] 校验。
|
||||
TerminalKey? resolveTerminalKey(String name) {
|
||||
for (final key in TerminalKey.values) {
|
||||
if (key.name == name) return key;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 终端实现:把按键语义写入 xterm 终端。
|
||||
///
|
||||
/// 所有输入都经 `Terminal.onOutput`,在 `TerminalSession` 中该回调被接到
|
||||
/// PTY,因此这里等价于用户真按了键盘。
|
||||
/// 注意不要用 `TerminalSession.write()`——那是往屏幕缓冲区写显示内容。
|
||||
class TerminalKeyboardSink implements VirtualKeyboardSink {
|
||||
final Terminal terminal;
|
||||
final FocusNode? focusNode;
|
||||
|
||||
TerminalKeyboardSink({required this.terminal, this.focusNode});
|
||||
|
||||
@override
|
||||
void sendText(String text, {bool appendEnter = false}) {
|
||||
if (text.isEmpty && !appendEnter) return;
|
||||
|
||||
var payload = text;
|
||||
// 避免用户已在 value 里写了换行时再补一个,造成双回车
|
||||
if (appendEnter && !payload.endsWith('\r') && !payload.endsWith('\n')) {
|
||||
payload = '$payload\r';
|
||||
}
|
||||
if (payload.isEmpty) return;
|
||||
|
||||
// 多字符或含换行时走 paste:会遵循 bracketed paste mode,更安全
|
||||
if (payload.length > 1 || payload.contains('\n') || payload.contains('\r')) {
|
||||
terminal.paste(payload);
|
||||
} else {
|
||||
terminal.textInput(payload);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void sendPair(String pairText, int cursorOffset) {
|
||||
// 终端没有"包裹选区 + 光标偏移"的语义,降级为直接发送文本
|
||||
sendText(pairText);
|
||||
}
|
||||
|
||||
@override
|
||||
void sendCommand(String command) {
|
||||
switch (command.toLowerCase().trim()) {
|
||||
case 'keyboard_hide':
|
||||
case 'hide_keyboard':
|
||||
hideKeyboard();
|
||||
break;
|
||||
default:
|
||||
// 编辑区命令在终端无意义;配置校验已禁止,这里仅作兜底
|
||||
if (kDebugMode) {
|
||||
debugPrint('[VirtualKeyboard] 终端不支持编辑器命令: $command');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void sendNamedKey(String keyName, {KeyboardKeyMods mods = KeyboardKeyMods.none}) {
|
||||
// 字母键走 charInput:无修饰时就是该字母本身,Ctrl/Alt 组合由 xterm 换算
|
||||
if (KeyboardScopeHelper.isLetterKeyName(keyName)) {
|
||||
final lowerCode = keyName.codeUnitAt(3) + 0x20; // keyC -> 'c'
|
||||
final handled = terminal.charInput(lowerCode, ctrl: mods.ctrl, alt: mods.alt);
|
||||
if (handled) return;
|
||||
|
||||
// charInput 不支持的组合(无修饰 / 仅 Shift / Ctrl+非字母)按普通文本发送,
|
||||
// 与 xterm 自身 `_onInsert` 的"先按键、后退化为文本"策略一致
|
||||
final letter = String.fromCharCode(lowerCode);
|
||||
terminal.textInput(mods.shift ? letter.toUpperCase() : letter);
|
||||
return;
|
||||
}
|
||||
|
||||
final key = resolveTerminalKey(keyName);
|
||||
if (key == null) {
|
||||
if (kDebugMode) {
|
||||
debugPrint('[VirtualKeyboard] 未知的终端特殊键: $keyName');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final handled = terminal.keyInput(
|
||||
key,
|
||||
ctrl: mods.ctrl,
|
||||
alt: mods.alt,
|
||||
shift: mods.shift,
|
||||
);
|
||||
if (!handled && kDebugMode) {
|
||||
// 例如 keytab 中不存在的组合:不会产生任何输出
|
||||
debugPrint('[VirtualKeyboard] 该按键组合在终端不会产生输出: $keyName'
|
||||
'${mods.isEmpty ? '' : ' ($mods)'}');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void hideKeyboard() {
|
||||
focusNode?.unfocus();
|
||||
SystemChannels.textInput.invokeMethod('TextInput.hide');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
import '../models/virtual_keyboard_config.dart';
|
||||
|
||||
/// 终端修饰键(小键盘上的 Ctrl / Alt / Shift 格)的运行时状态。
|
||||
///
|
||||
/// 语义:
|
||||
/// - 单击 = 一次性:点亮后由**下一次真实输入**带上并自动熄灭
|
||||
/// - 双击 = 锁定:持续生效,再点一次解除
|
||||
///
|
||||
/// 该状态由键盘组件与终端共用,因此它必须独立于 widget 生命周期存在:
|
||||
/// 终端据此把修饰符叠加到"硬件键盘按键"和"软键盘打进来的字符"上。
|
||||
class TerminalModifierState extends ChangeNotifier {
|
||||
TerminalModifierState({DateTime Function()? clock}) : clock = clock ?? DateTime.now;
|
||||
|
||||
/// 双击判定时钟(测试可注入假时钟以确定性地控制单击/双击)
|
||||
DateTime Function() clock;
|
||||
|
||||
/// 双击判定窗口
|
||||
static const Duration doubleTapWindow = Duration(milliseconds: 350);
|
||||
|
||||
final Set<String> _oneshot = <String>{};
|
||||
final Set<String> _locked = <String>{};
|
||||
final Map<String, DateTime> _lastTap = <String, DateTime>{};
|
||||
|
||||
bool isActive(String modifier) =>
|
||||
_oneshot.contains(modifier) || _locked.contains(modifier);
|
||||
|
||||
bool get isEmpty => _oneshot.isEmpty && _locked.isEmpty;
|
||||
|
||||
bool get isNotEmpty => !isEmpty;
|
||||
|
||||
/// 当前生效的修饰符组合
|
||||
KeyboardKeyMods get mods => KeyboardKeyMods(
|
||||
ctrl: isActive('ctrl'),
|
||||
alt: isActive('alt'),
|
||||
shift: isActive('shift'),
|
||||
);
|
||||
|
||||
/// 单击一次性点亮 / 双击锁定 / 点击已锁定项解除
|
||||
void toggle(String modifier) {
|
||||
final now = clock();
|
||||
final last = _lastTap[modifier];
|
||||
final isDoubleTap = last != null && now.difference(last) < doubleTapWindow;
|
||||
|
||||
if (_locked.contains(modifier)) {
|
||||
_locked.remove(modifier);
|
||||
_oneshot.remove(modifier);
|
||||
} else if (isDoubleTap) {
|
||||
_oneshot.remove(modifier);
|
||||
_locked.add(modifier);
|
||||
} else if (_oneshot.contains(modifier)) {
|
||||
_oneshot.remove(modifier);
|
||||
} else {
|
||||
_oneshot.add(modifier);
|
||||
}
|
||||
_lastTap[modifier] = now;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 一次性状态被一次真实输入消耗(锁定状态保留)
|
||||
void consume() {
|
||||
if (_oneshot.isEmpty) return;
|
||||
_oneshot.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 清空全部状态
|
||||
void clear() {
|
||||
if (isEmpty) return;
|
||||
_oneshot.clear();
|
||||
_locked.clear();
|
||||
_lastTap.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// 把"软键盘 / IME 直接打进来的文本"按当前修饰键状态改写。
|
||||
///
|
||||
/// 软键盘的字符不会产生 Flutter KeyEvent,xterm 内部走的是
|
||||
/// `terminal.textInput()`,因此只能在 `Terminal.onOutput` 这一层改写。
|
||||
/// 规则与 xterm 的 `Terminal.charInput` 保持一致:
|
||||
/// - Ctrl + a~z → 0x01~0x1a
|
||||
/// - Ctrl + [ \ ] ^ _ → 0x1b~0x1f
|
||||
/// - Alt + a~z → ESC + 大写字母
|
||||
/// - Shift + a~z → 大写字母
|
||||
///
|
||||
/// 只处理**单个字符**:多字符(粘贴、指令快捷键)原样放过,避免误伤。
|
||||
/// 返回 null 表示"无需改写"。
|
||||
String? applyModifierToTypedText(String data, KeyboardKeyMods mods) {
|
||||
if (mods.isEmpty) return null;
|
||||
|
||||
final runes = data.runes.toList();
|
||||
if (runes.length != 1) return null;
|
||||
|
||||
final code = runes.first;
|
||||
final isLetter = (code >= 0x61 && code <= 0x7a) || (code >= 0x41 && code <= 0x5a);
|
||||
final lower = (code >= 0x41 && code <= 0x5a) ? code + 0x20 : code;
|
||||
|
||||
if (mods.ctrl) {
|
||||
if (isLetter) return String.fromCharCode(lower - 0x61 + 1);
|
||||
if (code >= 0x5b && code <= 0x5f) return String.fromCharCode(code - 0x5b + 27);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mods.alt && !mods.shift) {
|
||||
if (isLetter) return String.fromCharCodes([0x1b, lower - 0x20]);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mods.shift && code >= 0x61 && code <= 0x7a) {
|
||||
return String.fromCharCode(code - 0x20);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 代理输入处理器:把运行时修饰键叠加到按键事件上,再交给 xterm 默认链路。
|
||||
///
|
||||
/// 覆盖硬件键盘与"小键盘 key 格"的按键事件(它们都走 `Terminal.keyInput`);
|
||||
/// 软键盘打进来的字符不产生 KeyEvent,由 [applyModifierToTypedText] 处理。
|
||||
class ModifierAwareInputHandler implements TerminalInputHandler {
|
||||
ModifierAwareInputHandler({required this.state, required this.delegate});
|
||||
|
||||
final TerminalModifierState state;
|
||||
final TerminalInputHandler delegate;
|
||||
|
||||
@override
|
||||
String? call(TerminalKeyboardEvent event) {
|
||||
final mods = state.mods;
|
||||
final result = delegate(
|
||||
mods.isEmpty
|
||||
? event
|
||||
: event.copyWith(
|
||||
ctrl: event.ctrl || mods.ctrl,
|
||||
alt: event.alt || mods.alt,
|
||||
shift: event.shift || mods.shift,
|
||||
),
|
||||
);
|
||||
|
||||
// 真的产生了输出才消耗一次性状态,避免"按了没反应还把修饰键吃掉"
|
||||
if (result != null && mods.isNotEmpty) {
|
||||
state.consume();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -26,12 +26,20 @@ class TerminalSessionItemWidget extends StatelessWidget {
|
||||
final badgeLabel = session.distroId;
|
||||
final isHost = session.distroId == 'host';
|
||||
|
||||
final isTerminated = !session.isProcessRunning;
|
||||
|
||||
final textStyle = theme.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||||
color: isSelected ? theme.colorScheme.primary : null,
|
||||
color: isTerminated
|
||||
? theme.colorScheme.error
|
||||
: (isSelected ? theme.colorScheme.primary : null),
|
||||
decoration: isTerminated ? TextDecoration.lineThrough : null,
|
||||
decorationColor: isTerminated ? theme.colorScheme.error : null,
|
||||
);
|
||||
|
||||
final iconColor = isSelected ? theme.colorScheme.primary : null;
|
||||
final iconColor = isTerminated
|
||||
? theme.colorScheme.error
|
||||
: (isSelected ? theme.colorScheme.primary : null);
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0),
|
||||
@@ -66,6 +74,8 @@ class TerminalSessionItemWidget extends StatelessWidget {
|
||||
const SizedBox(width: 6),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6.0, vertical: 1.5),
|
||||
// 系统名可能来自用户自定义导入,过长时限宽避免把标题行挤出边界
|
||||
constraints: const BoxConstraints(maxWidth: 96.0),
|
||||
decoration: BoxDecoration(
|
||||
color: isHost
|
||||
? theme.colorScheme.surfaceContainerHighest
|
||||
@@ -74,6 +84,8 @@ class TerminalSessionItemWidget extends StatelessWidget {
|
||||
),
|
||||
child: Text(
|
||||
badgeLabel,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 10.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
@@ -175,9 +187,13 @@ class TerminalSessionItemWidget extends StatelessWidget {
|
||||
children: [
|
||||
Icon(icon, size: 18, color: color),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(color: color),
|
||||
Flexible(
|
||||
child: Text(
|
||||
title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(color: color),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
|
||||
@@ -24,9 +24,13 @@ class VirtualKeyboardPageConfigSection extends StatelessWidget {
|
||||
children: [
|
||||
Icon(Icons.tune, size: 20, color: theme.colorScheme.primary),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
l10n.keyboardPageConfigSectionTitle,
|
||||
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.keyboardPageConfigSectionTitle,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -41,20 +45,24 @@ class VirtualKeyboardPageConfigSection extends StatelessWidget {
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(l10n.keyboardRowButtons, style: const TextStyle(fontWeight: FontWeight.w500)),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
l10n.keyboardRowGridDescription(count),
|
||||
style: TextStyle(fontSize: 12, color: theme.hintColor),
|
||||
),
|
||||
],
|
||||
// 左侧文案必须可收缩,否则 Row 内的定宽控件会被挤出右边界
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(l10n.keyboardRowButtons, style: const TextStyle(fontWeight: FontWeight.w500)),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
l10n.keyboardRowGridDescription(count),
|
||||
style: TextStyle(fontSize: 12, color: theme.hintColor),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton.filledTonal(
|
||||
icon: const Icon(Icons.remove, size: 18),
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
import '../models/virtual_keyboard_config.dart';
|
||||
|
||||
/// 虚拟小键盘的输入汇聚接口。
|
||||
///
|
||||
/// 键盘组件只负责"按键语义"(文本 / 成对符号 / 编辑器命令 / 命名特殊键),
|
||||
/// 由具体实现决定把语义落到编辑器还是终端上,避免键盘组件per-scope 分叉。
|
||||
///
|
||||
/// 不同作用域对同一语义的支持程度不同,不支持的实现应安全降级为空操作,
|
||||
/// 绝不静默插入无关内容。
|
||||
abstract class VirtualKeyboardSink {
|
||||
/// 发送一段文本(编辑区:插入;终端:写入 PTY)
|
||||
///
|
||||
/// [appendEnter] 仅终端有意义:发送后补一个回车符,实现"输入即执行"
|
||||
void sendText(String text, {bool appendEnter = false});
|
||||
|
||||
/// 成对符号:有选区时包裹选区,无选区时插入并把光标按 [cursorOffset] 偏移
|
||||
void sendPair(String pairText, int cursorOffset);
|
||||
|
||||
/// 编辑器命令(缩进、撤销、光标移动等)
|
||||
void sendCommand(String command);
|
||||
|
||||
/// 命名特殊键(终端:escape / tab / arrowUp / keyC + Ctrl 组合等)
|
||||
void sendNamedKey(String keyName, {KeyboardKeyMods mods = KeyboardKeyMods.none});
|
||||
|
||||
/// 收起软键盘
|
||||
void hideKeyboard();
|
||||
}
|
||||
|
||||
/// 编辑区实现:把按键语义落到 [CodeLineEditingController] 上
|
||||
class EditorKeyboardSink implements VirtualKeyboardSink {
|
||||
final CodeLineEditingController controller;
|
||||
final FocusNode? focusNode;
|
||||
|
||||
EditorKeyboardSink({required this.controller, this.focusNode});
|
||||
|
||||
@override
|
||||
void sendText(String text, {bool appendEnter = false}) {
|
||||
if (text.isEmpty) return;
|
||||
// 编辑区不存在"回车执行"语义,appendEnter 直接忽略(由校验层禁止该组合)
|
||||
controller.replaceSelection(text);
|
||||
}
|
||||
|
||||
@override
|
||||
void sendPair(String pairText, int cursorOffset) {
|
||||
final hasSelection = !controller.selection.isCollapsed &&
|
||||
controller.selection.baseOffset != -1 &&
|
||||
controller.selection.extentOffset != -1;
|
||||
|
||||
if (hasSelection) {
|
||||
final selected = controller.selectedText;
|
||||
if (pairText.length >= 2) {
|
||||
final left = pairText.substring(0, pairText.length ~/ 2);
|
||||
final right = pairText.substring(pairText.length ~/ 2);
|
||||
controller.replaceSelection(left + selected + right);
|
||||
} else {
|
||||
controller.replaceSelection(pairText + selected + pairText);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
controller.replaceSelection(pairText);
|
||||
if (cursorOffset != 0) {
|
||||
final curSel = controller.selection;
|
||||
final targetOffset = (curSel.extentOffset + cursorOffset).clamp(0, controller.extentLine.length);
|
||||
controller.selection = CodeLineSelection.collapsed(
|
||||
index: curSel.extentIndex,
|
||||
offset: targetOffset,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void sendCommand(String command) {
|
||||
final cmd = command.toLowerCase().trim();
|
||||
switch (cmd) {
|
||||
case 'tab':
|
||||
controller.applyIndent();
|
||||
break;
|
||||
case 'untab':
|
||||
case 'outdent':
|
||||
controller.applyOutdent();
|
||||
break;
|
||||
case 'cursor_left':
|
||||
case 'left':
|
||||
controller.moveCursor(AxisDirection.left);
|
||||
break;
|
||||
case 'cursor_right':
|
||||
case 'right':
|
||||
controller.moveCursor(AxisDirection.right);
|
||||
break;
|
||||
case 'cursor_up':
|
||||
case 'up':
|
||||
controller.moveCursor(AxisDirection.up);
|
||||
break;
|
||||
case 'cursor_down':
|
||||
case 'down':
|
||||
controller.moveCursor(AxisDirection.down);
|
||||
break;
|
||||
case 'line_start':
|
||||
case 'home':
|
||||
controller.moveCursorToLineStart();
|
||||
break;
|
||||
case 'line_end':
|
||||
case 'end':
|
||||
controller.moveCursorToLineEnd();
|
||||
break;
|
||||
case 'page_start':
|
||||
controller.moveCursorToPageStart();
|
||||
break;
|
||||
case 'page_end':
|
||||
controller.moveCursorToPageEnd();
|
||||
break;
|
||||
case 'undo':
|
||||
if (controller.canUndo) {
|
||||
controller.undo();
|
||||
}
|
||||
break;
|
||||
case 'redo':
|
||||
if (controller.canRedo) {
|
||||
controller.redo();
|
||||
}
|
||||
break;
|
||||
case 'copy':
|
||||
controller.copy();
|
||||
break;
|
||||
case 'cut':
|
||||
controller.cut();
|
||||
break;
|
||||
case 'paste':
|
||||
controller.paste();
|
||||
break;
|
||||
case 'backspace':
|
||||
case 'delete_backward':
|
||||
controller.deleteBackward();
|
||||
break;
|
||||
case 'delete':
|
||||
case 'delete_forward':
|
||||
controller.deleteForward();
|
||||
break;
|
||||
case 'select_all':
|
||||
controller.selectAll();
|
||||
break;
|
||||
case 'keyboard_hide':
|
||||
case 'hide_keyboard':
|
||||
hideKeyboard();
|
||||
break;
|
||||
default:
|
||||
// 未知命令安全降级为直接输入其命令名称或原文本
|
||||
controller.replaceSelection(command);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void sendNamedKey(String keyName, {KeyboardKeyMods mods = KeyboardKeyMods.none}) {
|
||||
// 编辑区没有"终端命名键"概念;配置校验已禁止该组合,这里仅作兜底不产生副作用
|
||||
if (kDebugMode) {
|
||||
debugPrint('[VirtualKeyboard] 编辑区不支持命名特殊键: $keyName${mods.isEmpty ? '' : ' ($mods)'}');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void hideKeyboard() {
|
||||
focusNode?.unfocus();
|
||||
SystemChannels.textInput.invokeMethod('TextInput.hide');
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,22 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
import 'virtual_keyboard_sink.dart';
|
||||
import 'terminal_modifier_state.dart';
|
||||
|
||||
/// 编辑器下方的虚拟辅助小键盘组件
|
||||
class VirtualKeyboardWidget extends StatefulWidget {
|
||||
final CodeLineEditingController? controller;
|
||||
final FocusNode? focusNode;
|
||||
|
||||
/// 输入汇聚实现。为空时按 [controller] 自动构造编辑区实现;
|
||||
/// 终端作用域传入 `TerminalKeyboardSink`。
|
||||
final VirtualKeyboardSink? sink;
|
||||
|
||||
/// 终端修饰键运行时状态。终端作用域由外部注入,使终端也能读到同一份状态
|
||||
/// (把修饰符叠加到软键盘/硬件键盘的输入上);为空时组件内部自建一份。
|
||||
final TerminalModifierState? modifierState;
|
||||
|
||||
final VirtualKeyboardConfig config;
|
||||
final Color? backgroundColor;
|
||||
|
||||
@@ -14,6 +26,8 @@ class VirtualKeyboardWidget extends StatefulWidget {
|
||||
super.key,
|
||||
required this.controller,
|
||||
this.focusNode,
|
||||
this.sink,
|
||||
this.modifierState,
|
||||
required this.config,
|
||||
this.backgroundColor,
|
||||
});
|
||||
@@ -28,6 +42,15 @@ class _VirtualKeyboardWidgetState extends State<VirtualKeyboardWidget>
|
||||
late AnimationController _animationController;
|
||||
int _currentPage = 0;
|
||||
|
||||
/// 输入汇聚实现(终端作用域由外部注入,否则按 controller 构造编辑区实现)
|
||||
VirtualKeyboardSink? _sink;
|
||||
|
||||
/// 修饰键运行时状态(终端作用域由外部注入,使终端能共用同一份状态)
|
||||
late TerminalModifierState _modifiers;
|
||||
|
||||
/// 是否由本组件创建了 [_modifiers](自建时才负责释放)
|
||||
bool _ownsModifiers = false;
|
||||
|
||||
static const double _rowHeight = 30.0;
|
||||
static const double _indicatorHeight = 12.0;
|
||||
|
||||
@@ -40,18 +63,64 @@ class _VirtualKeyboardWidgetState extends State<VirtualKeyboardWidget>
|
||||
duration: const Duration(milliseconds: 200),
|
||||
value: 0.0, // 默认折叠(0.0 表示仅显示第一行,1.0 表示完全展开)
|
||||
);
|
||||
_resolveSink();
|
||||
_resolveModifiers();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant VirtualKeyboardWidget oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.sink != widget.sink || oldWidget.controller != widget.controller) {
|
||||
_resolveSink();
|
||||
}
|
||||
if (oldWidget.modifierState != widget.modifierState) {
|
||||
_releaseModifiers();
|
||||
_resolveModifiers();
|
||||
}
|
||||
}
|
||||
|
||||
/// 终端作用域由外部注入 sink;编辑区按 controller 自动构造
|
||||
void _resolveSink() {
|
||||
_sink = widget.sink ??
|
||||
(widget.controller != null
|
||||
? EditorKeyboardSink(controller: widget.controller!, focusNode: widget.focusNode)
|
||||
: null);
|
||||
}
|
||||
|
||||
void _resolveModifiers() {
|
||||
final injected = widget.modifierState;
|
||||
if (injected != null) {
|
||||
_modifiers = injected;
|
||||
_ownsModifiers = false;
|
||||
} else {
|
||||
_modifiers = TerminalModifierState();
|
||||
_ownsModifiers = true;
|
||||
}
|
||||
_modifiers.addListener(_onModifiersChanged);
|
||||
}
|
||||
|
||||
void _releaseModifiers() {
|
||||
_modifiers.removeListener(_onModifiersChanged);
|
||||
if (_ownsModifiers) {
|
||||
_modifiers.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
void _onModifiersChanged() {
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_releaseModifiers();
|
||||
_pageController.dispose();
|
||||
_animationController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _handleKeyTap(KeyboardKeyItem keyItem) {
|
||||
final controller = widget.controller;
|
||||
if (controller == null) return;
|
||||
final sink = _sink;
|
||||
if (sink == null) return;
|
||||
|
||||
// 轻微触觉反馈提升敲击手感
|
||||
HapticFeedback.lightImpact();
|
||||
@@ -61,133 +130,45 @@ class _VirtualKeyboardWidgetState extends State<VirtualKeyboardWidget>
|
||||
widget.focusNode!.requestFocus();
|
||||
}
|
||||
|
||||
// 修饰键只切换运行时状态,不产生输出
|
||||
if (keyItem.action == 'modifier') {
|
||||
_modifiers.toggle(keyItem.value);
|
||||
return;
|
||||
}
|
||||
|
||||
final mods = _effectiveMods(keyItem.mods);
|
||||
|
||||
switch (keyItem.action) {
|
||||
case 'command':
|
||||
_handleCommand(controller, keyItem.value);
|
||||
sink.sendCommand(keyItem.value);
|
||||
break;
|
||||
case 'pair':
|
||||
_handlePair(controller, keyItem.value, keyItem.cursorOffset);
|
||||
sink.sendPair(keyItem.value, keyItem.cursorOffset);
|
||||
break;
|
||||
case 'key':
|
||||
sink.sendNamedKey(keyItem.value, mods: mods);
|
||||
break;
|
||||
case 'input':
|
||||
default:
|
||||
_handleInput(controller, keyItem.value.isNotEmpty ? keyItem.value : keyItem.label);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _handleInput(CodeLineEditingController controller, String text) {
|
||||
if (text.isEmpty) return;
|
||||
controller.replaceSelection(text);
|
||||
}
|
||||
|
||||
void _handlePair(CodeLineEditingController controller, String pairText, int offset) {
|
||||
final hasSelection = !controller.selection.isCollapsed &&
|
||||
controller.selection.baseOffset != -1 &&
|
||||
controller.selection.extentOffset != -1;
|
||||
|
||||
if (hasSelection) {
|
||||
final selected = controller.selectedText;
|
||||
if (pairText.length >= 2) {
|
||||
final left = pairText.substring(0, pairText.length ~/ 2);
|
||||
final right = pairText.substring(pairText.length ~/ 2);
|
||||
final wrapped = left + selected + right;
|
||||
controller.replaceSelection(wrapped);
|
||||
} else {
|
||||
controller.replaceSelection(pairText + selected + pairText);
|
||||
}
|
||||
} else {
|
||||
controller.replaceSelection(pairText);
|
||||
if (offset != 0) {
|
||||
final curSel = controller.selection;
|
||||
final targetOffset = (curSel.extentOffset + offset).clamp(0, controller.extentLine.length);
|
||||
controller.selection = CodeLineSelection.collapsed(
|
||||
index: curSel.extentIndex,
|
||||
offset: targetOffset,
|
||||
sink.sendText(
|
||||
keyItem.value.isNotEmpty ? keyItem.value : keyItem.label,
|
||||
appendEnter: keyItem.autoEnter,
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 一次性修饰键被"下一次任意按键"消耗(含 Esc、方向键这类不产生字符的键)
|
||||
_modifiers.consume();
|
||||
}
|
||||
|
||||
void _handleCommand(CodeLineEditingController controller, String command) {
|
||||
final cmd = command.toLowerCase().trim();
|
||||
switch (cmd) {
|
||||
case 'tab':
|
||||
controller.applyIndent();
|
||||
break;
|
||||
case 'untab':
|
||||
case 'outdent':
|
||||
controller.applyOutdent();
|
||||
break;
|
||||
case 'cursor_left':
|
||||
case 'left':
|
||||
controller.moveCursor(AxisDirection.left);
|
||||
break;
|
||||
case 'cursor_right':
|
||||
case 'right':
|
||||
controller.moveCursor(AxisDirection.right);
|
||||
break;
|
||||
case 'cursor_up':
|
||||
case 'up':
|
||||
controller.moveCursor(AxisDirection.up);
|
||||
break;
|
||||
case 'cursor_down':
|
||||
case 'down':
|
||||
controller.moveCursor(AxisDirection.down);
|
||||
break;
|
||||
case 'line_start':
|
||||
case 'home':
|
||||
controller.moveCursorToLineStart();
|
||||
break;
|
||||
case 'line_end':
|
||||
case 'end':
|
||||
controller.moveCursorToLineEnd();
|
||||
break;
|
||||
case 'page_start':
|
||||
controller.moveCursorToPageStart();
|
||||
break;
|
||||
case 'page_end':
|
||||
controller.moveCursorToPageEnd();
|
||||
break;
|
||||
case 'undo':
|
||||
if (controller.canUndo) {
|
||||
controller.undo();
|
||||
}
|
||||
break;
|
||||
case 'redo':
|
||||
if (controller.canRedo) {
|
||||
controller.redo();
|
||||
}
|
||||
break;
|
||||
case 'copy':
|
||||
controller.copy();
|
||||
break;
|
||||
case 'cut':
|
||||
controller.cut();
|
||||
break;
|
||||
case 'paste':
|
||||
controller.paste();
|
||||
break;
|
||||
case 'backspace':
|
||||
case 'delete_backward':
|
||||
controller.deleteBackward();
|
||||
break;
|
||||
case 'delete':
|
||||
case 'delete_forward':
|
||||
controller.deleteForward();
|
||||
break;
|
||||
case 'select_all':
|
||||
controller.selectAll();
|
||||
break;
|
||||
case 'keyboard_hide':
|
||||
case 'hide_keyboard':
|
||||
widget.focusNode?.unfocus();
|
||||
SystemChannels.textInput.invokeMethod('TextInput.hide');
|
||||
break;
|
||||
default:
|
||||
// 未知命令安全降级为直接输入其命令名称或原文本
|
||||
controller.replaceSelection(command);
|
||||
break;
|
||||
}
|
||||
/// 合并按键自身配置的修饰符与运行时激活的修饰符
|
||||
KeyboardKeyMods _effectiveMods(KeyboardKeyMods own) {
|
||||
final active = _modifiers.mods;
|
||||
return KeyboardKeyMods(
|
||||
ctrl: own.ctrl || active.ctrl,
|
||||
alt: own.alt || active.alt,
|
||||
shift: own.shift || active.shift,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -392,9 +373,18 @@ class _VirtualKeyboardWidgetState extends State<VirtualKeyboardWidget>
|
||||
Widget _buildKeyButton(KeyboardKeyItem keyItem, bool isDark) {
|
||||
final iconData = KeyboardIconHelper.getIcon(keyItem.icon);
|
||||
|
||||
final btnBgColor = isDark ? const Color(0xFF2C2C2C) : const Color(0xFFFFFFFF);
|
||||
final btnTextColor = isDark ? const Color(0xFFE0E0E0) : const Color(0xFF303133);
|
||||
final borderColor = isDark ? const Color(0xFF383838) : const Color(0xFFE4E7ED);
|
||||
// 修饰键处于激活(一次性或锁定)状态时高亮,让用户看清当前修饰状态
|
||||
final isModifierActive = keyItem.action == 'modifier' && _modifiers.isActive(keyItem.value);
|
||||
|
||||
final btnBgColor = isModifierActive
|
||||
? (isDark ? const Color(0xFF2F4F6F) : const Color(0xFFD6E6FF))
|
||||
: (isDark ? const Color(0xFF2C2C2C) : const Color(0xFFFFFFFF));
|
||||
final btnTextColor = isModifierActive
|
||||
? (isDark ? const Color(0xFFFFFFFF) : const Color(0xFF0B57D0))
|
||||
: (isDark ? const Color(0xFFE0E0E0) : const Color(0xFF303133));
|
||||
final borderColor = isModifierActive
|
||||
? const Color(0xFF0B57D0)
|
||||
: (isDark ? const Color(0xFF383838) : const Color(0xFFE4E7ED));
|
||||
|
||||
Widget content;
|
||||
if (iconData != null) {
|
||||
|
||||
Reference in new issue
Block a user