2
This commit is contained in:
1 parent
b49e096355
commit
63e1fe3861
48 files changed
+5817
-556
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/services/permission_service.dart';
|
||||
import 'package:code_editor/views/terminal_view.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -133,12 +134,16 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
onSaveAll: onSaveAll,
|
||||
onRunTasks: onRunTasks,
|
||||
onTerminal: onTerminal ??
|
||||
() {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (context) => const TerminalView(),
|
||||
),
|
||||
);
|
||||
() async {
|
||||
final wentToSettings = await PermissionService.instance.promptBatteryOptimizationIfNeeded(context);
|
||||
if (wentToSettings) return;
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (context) => const TerminalView(),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
onProjectDetect: onProjectDetect,
|
||||
onEditRunTasks: onEditRunTasks,
|
||||
|
||||
+265
-119
@@ -1,4 +1,5 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -34,18 +35,130 @@ class _EditorContextMenuItem extends PopupMenuItem<void> implements PreferredSiz
|
||||
Size get preferredSize => const Size(140, 36);
|
||||
}
|
||||
|
||||
/// 移动端选区悬浮工具栏布局代理:
|
||||
/// 负责根据 EditorToolbarPlacement 与 safeEditorRect(精准剔除 AppBar/TabBar/虚拟小键盘/软键盘)精确定位
|
||||
class _MobileToolbarLayoutDelegate extends SingleChildLayoutDelegate {
|
||||
final TextSelectionToolbarAnchors anchors;
|
||||
final Rect? renderRect;
|
||||
final MediaQueryData mediaQuery;
|
||||
|
||||
_MobileToolbarLayoutDelegate({
|
||||
required this.anchors,
|
||||
required this.renderRect,
|
||||
required this.mediaQuery,
|
||||
});
|
||||
|
||||
@override
|
||||
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
|
||||
return constraints.loosen();
|
||||
}
|
||||
|
||||
@override
|
||||
Offset getPositionForChild(Size size, Size childSize) {
|
||||
// 1. 计算受保护的可视安全编辑区 safeEditorRect
|
||||
// 顶部:排除 AppBar, TabBar (renderRect.top) 以及系统状态栏
|
||||
final double safeTop = max(renderRect?.top ?? 0.0, mediaQuery.padding.top) + 6.0;
|
||||
// 底部:排除软键盘 (viewInsets.bottom)、虚拟小键盘 (renderRect.bottom) 以及底部安全区
|
||||
final double screenBottomWithoutKeyboard = mediaQuery.size.height - mediaQuery.viewInsets.bottom - mediaQuery.padding.bottom;
|
||||
final double safeBottom = min(renderRect?.bottom ?? screenBottomWithoutKeyboard, screenBottomWithoutKeyboard) - 6.0;
|
||||
// 左右:留出安全内边距
|
||||
final double safeLeft = max(renderRect?.left ?? 0.0, mediaQuery.padding.left) + 8.0;
|
||||
final double safeRight = min(renderRect?.right ?? mediaQuery.size.width, mediaQuery.size.width - mediaQuery.padding.right) - 8.0;
|
||||
|
||||
final double availableWidth = max(0.0, safeRight - safeLeft);
|
||||
final double availableHeight = max(0.0, safeBottom - safeTop);
|
||||
|
||||
// 2. 根据 anchors 中的 EditorToolbarPlacement 或传统 anchors 计算位置
|
||||
EditorToolbarPlacement placement = EditorToolbarPlacement.aboveStart;
|
||||
Offset targetOffset = anchors.primaryAnchor;
|
||||
|
||||
if (anchors is EditorSelectionToolbarAnchors) {
|
||||
final customAnchors = anchors as EditorSelectionToolbarAnchors;
|
||||
placement = customAnchors.placement;
|
||||
targetOffset = customAnchors.targetOffset;
|
||||
}
|
||||
|
||||
double x;
|
||||
double y;
|
||||
|
||||
switch (placement) {
|
||||
case EditorToolbarPlacement.center:
|
||||
// 框选文本完全占据当前屏幕:在中间显示
|
||||
x = safeLeft + (availableWidth - childSize.width) / 2.0;
|
||||
y = safeTop + (availableHeight - childSize.height) / 2.0;
|
||||
break;
|
||||
|
||||
case EditorToolbarPlacement.top:
|
||||
// 选区整体在视口上方:在视口顶部贴边显示
|
||||
x = safeLeft + (availableWidth - childSize.width) / 2.0;
|
||||
y = safeTop + 4.0;
|
||||
break;
|
||||
|
||||
case EditorToolbarPlacement.bottom:
|
||||
// 选区整体在视口下方:在视口底部贴边显示(紧贴软键盘/小键盘上方)
|
||||
x = safeLeft + (availableWidth - childSize.width) / 2.0;
|
||||
y = max(safeTop, safeBottom - childSize.height - 4.0);
|
||||
break;
|
||||
|
||||
case EditorToolbarPlacement.aboveStart:
|
||||
// 在左端点上方弹出,若上方空间不足则翻转至下方
|
||||
x = targetOffset.dx - childSize.width / 2.0;
|
||||
final double yAbove = targetOffset.dy - childSize.height - 8.0;
|
||||
if (yAbove >= safeTop) {
|
||||
y = yAbove;
|
||||
} else {
|
||||
// 上方空间不足,翻转到左端点下方(行高约为 24)
|
||||
y = targetOffset.dy + 24.0 + 8.0;
|
||||
}
|
||||
break;
|
||||
|
||||
case EditorToolbarPlacement.belowEnd:
|
||||
// 在右端点下方弹出,若下方空间不足则翻转至上方
|
||||
x = targetOffset.dx - childSize.width / 2.0;
|
||||
// 右端点手柄下挂高度约 28px,避免遮挡光标水滴手柄
|
||||
final double yBelow = targetOffset.dy + 24.0 + 26.0;
|
||||
if (yBelow + childSize.height <= safeBottom) {
|
||||
y = yBelow;
|
||||
} else {
|
||||
// 下方空间不足(被软键盘或小键盘限制),翻转到右端点上方
|
||||
y = targetOffset.dy - childSize.height - 8.0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 3. 水平与垂直安全夹紧,防止任何溢出
|
||||
final double clampedX = (availableWidth >= childSize.width)
|
||||
? x.clamp(safeLeft, safeRight - childSize.width)
|
||||
: safeLeft;
|
||||
final double clampedY = (availableHeight >= childSize.height)
|
||||
? y.clamp(safeTop, safeBottom - childSize.height)
|
||||
: safeTop;
|
||||
|
||||
return Offset(clampedX, clampedY);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRelayout(covariant _MobileToolbarLayoutDelegate oldDelegate) {
|
||||
return anchors != oldDelegate.anchors ||
|
||||
renderRect != oldDelegate.renderRect ||
|
||||
mediaQuery != oldDelegate.mediaQuery;
|
||||
}
|
||||
}
|
||||
|
||||
/// 移动端选区悬浮工具栏(基于 OverlayEntry,绝不压入 Route,软键盘保持弹起)
|
||||
class _MobileSelectionToolbarWidget extends StatefulWidget {
|
||||
final TextSelectionToolbarAnchors anchors;
|
||||
final CodeLineEditingController controller;
|
||||
final VoidCallback onDismiss;
|
||||
final FocusNode? focusNode;
|
||||
final Rect? renderRect;
|
||||
|
||||
const _MobileSelectionToolbarWidget({
|
||||
required this.anchors,
|
||||
required this.controller,
|
||||
required this.onDismiss,
|
||||
this.focusNode,
|
||||
this.renderRect,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -79,76 +192,130 @@ 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 cutLabel = l10n?.cut ?? 'Cut';
|
||||
final copyLabel = l10n?.copy ?? 'Copy';
|
||||
final pasteLabel = l10n?.paste ?? 'Paste';
|
||||
final selectAllLabel = l10n?.selectAll ?? 'Select All';
|
||||
|
||||
final primary = effectiveAnchors.primaryAnchor;
|
||||
final secondary = effectiveAnchors.secondaryAnchor;
|
||||
final theme = Theme.of(context);
|
||||
|
||||
// 针对虚拟键盘或底栏的全局安全高度校准:
|
||||
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),
|
||||
final toolbarContent = Material(
|
||||
elevation: 6.0,
|
||||
shadowColor: Colors.black45,
|
||||
color: theme.colorScheme.surfaceContainerHighest,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant.withValues(alpha: 0.6),
|
||||
width: 0.8,
|
||||
),
|
||||
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),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: IntrinsicHeight(
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (hasSelection) ...[
|
||||
_buildItem(
|
||||
context,
|
||||
icon: Icons.content_cut,
|
||||
label: cutLabel,
|
||||
onTap: () {
|
||||
widget.controller.cut();
|
||||
widget.onDismiss();
|
||||
widget.focusNode?.requestFocus();
|
||||
},
|
||||
),
|
||||
_buildDivider(context),
|
||||
_buildItem(
|
||||
context,
|
||||
icon: Icons.copy,
|
||||
label: copyLabel,
|
||||
onTap: () {
|
||||
widget.controller.copy();
|
||||
widget.onDismiss();
|
||||
widget.focusNode?.requestFocus();
|
||||
},
|
||||
),
|
||||
],
|
||||
if (_canPaste) ...[
|
||||
if (hasSelection) _buildDivider(context),
|
||||
_buildItem(
|
||||
context,
|
||||
icon: Icons.paste,
|
||||
label: pasteLabel,
|
||||
onTap: () {
|
||||
widget.controller.paste();
|
||||
widget.onDismiss();
|
||||
widget.focusNode?.requestFocus();
|
||||
},
|
||||
),
|
||||
],
|
||||
if (!widget.controller.isEmpty) ...[
|
||||
_buildDivider(context),
|
||||
_buildItem(
|
||||
context,
|
||||
icon: Icons.select_all,
|
||||
label: selectAllLabel,
|
||||
onTap: () {
|
||||
widget.controller.selectAll();
|
||||
widget.onDismiss();
|
||||
widget.focusNode?.requestFocus();
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
secondaryAnchor: null,
|
||||
);
|
||||
}
|
||||
),
|
||||
);
|
||||
|
||||
return AdaptiveTextSelectionToolbar.buttonItems(
|
||||
anchors: effectiveAnchors,
|
||||
buttonItems: [
|
||||
if (hasSelection)
|
||||
ContextMenuButtonItem(
|
||||
label: l10n?.cut ?? '剪切',
|
||||
type: ContextMenuButtonType.cut,
|
||||
onPressed: () {
|
||||
widget.controller.cut();
|
||||
widget.onDismiss();
|
||||
widget.focusNode?.requestFocus();
|
||||
},
|
||||
),
|
||||
if (hasSelection)
|
||||
ContextMenuButtonItem(
|
||||
label: l10n?.copy ?? '复制',
|
||||
type: ContextMenuButtonType.copy,
|
||||
onPressed: () {
|
||||
widget.controller.copy();
|
||||
widget.onDismiss();
|
||||
widget.focusNode?.requestFocus();
|
||||
},
|
||||
),
|
||||
if (_canPaste)
|
||||
ContextMenuButtonItem(
|
||||
label: l10n?.paste ?? '粘贴',
|
||||
type: ContextMenuButtonType.paste,
|
||||
onPressed: () {
|
||||
widget.controller.paste();
|
||||
widget.onDismiss();
|
||||
widget.focusNode?.requestFocus();
|
||||
},
|
||||
),
|
||||
if (!widget.controller.isEmpty)
|
||||
ContextMenuButtonItem(
|
||||
label: l10n?.selectAll ?? '全选',
|
||||
type: ContextMenuButtonType.selectAll,
|
||||
onPressed: () {
|
||||
widget.controller.selectAll();
|
||||
widget.onDismiss();
|
||||
widget.focusNode?.requestFocus();
|
||||
},
|
||||
),
|
||||
],
|
||||
return CustomSingleChildLayout(
|
||||
delegate: _MobileToolbarLayoutDelegate(
|
||||
anchors: widget.anchors,
|
||||
renderRect: widget.renderRect,
|
||||
mediaQuery: MediaQuery.of(context),
|
||||
),
|
||||
child: toolbarContent,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildItem(
|
||||
BuildContext context, {
|
||||
required IconData icon,
|
||||
required String label,
|
||||
required VoidCallback onTap,
|
||||
}) {
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 16, color: colorScheme.onSurface),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDivider(BuildContext context) {
|
||||
return VerticalDivider(
|
||||
width: 1,
|
||||
thickness: 0.8,
|
||||
indent: 6,
|
||||
endIndent: 6,
|
||||
color: Theme.of(context).colorScheme.outlineVariant.withValues(alpha: 0.5),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -158,6 +325,12 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
final FocusNode? focusNode;
|
||||
late final SelectionToolbarController _mobileController;
|
||||
|
||||
CodeLineEditingController? _lastController;
|
||||
TextSelectionToolbarAnchors? _lastAnchors;
|
||||
Rect? _lastRenderRect;
|
||||
LayerLink? _lastLayerLink;
|
||||
ValueNotifier<bool>? _lastVisibility;
|
||||
|
||||
CodeEditorToolbarController({this.focusNode}) {
|
||||
_mobileController = MobileSelectionToolbarController(
|
||||
builder: ({
|
||||
@@ -166,12 +339,14 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
required CodeLineEditingController controller,
|
||||
required VoidCallback onDismiss,
|
||||
required VoidCallback onRefresh,
|
||||
Rect? renderRect,
|
||||
}) {
|
||||
return _MobileSelectionToolbarWidget(
|
||||
anchors: anchors,
|
||||
controller: controller,
|
||||
onDismiss: onDismiss,
|
||||
focusNode: focusNode,
|
||||
renderRect: renderRect,
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -191,6 +366,12 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
required LayerLink layerLink,
|
||||
required ValueNotifier<bool> visibility,
|
||||
}) {
|
||||
_lastController = controller;
|
||||
_lastAnchors = anchors;
|
||||
_lastRenderRect = renderRect;
|
||||
_lastLayerLink = layerLink;
|
||||
_lastVisibility = visibility;
|
||||
|
||||
if (renderRect == null) {
|
||||
_showDesktopMenu(
|
||||
context: context,
|
||||
@@ -198,11 +379,10 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
anchors: anchors,
|
||||
);
|
||||
} else {
|
||||
final safeAnchors = _sanitizeMobileAnchors(anchors, renderRect);
|
||||
_mobileController.show(
|
||||
context: context,
|
||||
controller: controller,
|
||||
anchors: safeAnchors,
|
||||
anchors: anchors,
|
||||
renderRect: renderRect,
|
||||
layerLink: layerLink,
|
||||
visibility: visibility,
|
||||
@@ -210,56 +390,22 @@ 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,
|
||||
);
|
||||
/// 重新唤起上一次的选区悬浮工具栏(常用于滚动结束时自动恢复)
|
||||
void reshowLastToolbar(BuildContext context) {
|
||||
final controller = _lastController;
|
||||
if (controller == null || controller.selection.isCollapsed || controller.selection.baseOffset == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 规则 2:选区位于第一行/第二行(上方空间不足 54px),但下方空间充足(>= 64px)
|
||||
// 智能翻转至选区下方,保证不超出屏幕顶部也不遮挡第一行
|
||||
if (spaceBelow >= 64.0) {
|
||||
return TextSelectionToolbarAnchors(
|
||||
primaryAnchor: const Offset(-10000, -10000),
|
||||
secondaryAnchor: Offset(safeX, bottomY),
|
||||
);
|
||||
if (_lastAnchors == null || _lastLayerLink == null || _lastVisibility == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 规则 3:选区跨越很大或上下空间均受限时,安全悬浮于可视区域上边缘内侧
|
||||
return TextSelectionToolbarAnchors(
|
||||
primaryAnchor: Offset(
|
||||
safeX,
|
||||
(renderRect.top + 56.0).clamp(renderRect.top + 10.0, renderRect.bottom - 10.0),
|
||||
),
|
||||
secondaryAnchor: null,
|
||||
show(
|
||||
context: context,
|
||||
controller: controller,
|
||||
anchors: _lastAnchors!,
|
||||
renderRect: _lastRenderRect,
|
||||
layerLink: _lastLayerLink!,
|
||||
visibility: _lastVisibility!,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -300,7 +446,7 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
),
|
||||
items: [
|
||||
_EditorContextMenuItem(
|
||||
text: l10n?.cut ?? '剪切',
|
||||
text: l10n?.cut ?? 'Cut',
|
||||
icon: Icons.content_cut,
|
||||
enabled: hasSelection,
|
||||
onTap: () {
|
||||
@@ -308,7 +454,7 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
},
|
||||
),
|
||||
_EditorContextMenuItem(
|
||||
text: l10n?.copy ?? '复制',
|
||||
text: l10n?.copy ?? 'Copy',
|
||||
icon: Icons.copy,
|
||||
enabled: hasSelection,
|
||||
onTap: () {
|
||||
@@ -316,7 +462,7 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
},
|
||||
),
|
||||
_EditorContextMenuItem(
|
||||
text: l10n?.paste ?? '粘贴',
|
||||
text: l10n?.paste ?? 'Paste',
|
||||
icon: Icons.paste,
|
||||
enabled: hasClipboard,
|
||||
onTap: () {
|
||||
@@ -325,7 +471,7 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
),
|
||||
const PopupMenuDivider(height: 1),
|
||||
_EditorContextMenuItem(
|
||||
text: l10n?.selectAll ?? '全选',
|
||||
text: l10n?.selectAll ?? 'Select All',
|
||||
icon: Icons.select_all,
|
||||
enabled: !controller.isEmpty,
|
||||
onTap: () {
|
||||
|
||||
@@ -457,56 +457,56 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
controller: controller,
|
||||
focusNode: _focusNode,
|
||||
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,
|
||||
backgroundColor: activeTheme.backgroundColor,
|
||||
cursorColor: activeTheme.cursorColor,
|
||||
cursorLineColor: activeTheme.cursorLineColor,
|
||||
selectionColor: activeTheme.selectionColor,
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
codeTheme: CodeHighlightTheme(
|
||||
languages: SyntaxHighlightHelper.getLanguagesForFile(_currentLoadedPath ?? widget.filePath),
|
||||
theme: activeTheme.highlightTheme,
|
||||
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,
|
||||
backgroundColor: activeTheme.backgroundColor,
|
||||
cursorColor: activeTheme.cursorColor,
|
||||
cursorLineColor: activeTheme.cursorLineColor,
|
||||
selectionColor: activeTheme.selectionColor,
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
codeTheme: CodeHighlightTheme(
|
||||
languages: SyntaxHighlightHelper.getLanguagesForFile(_currentLoadedPath ?? widget.filePath),
|
||||
theme: activeTheme.highlightTheme,
|
||||
),
|
||||
),
|
||||
indicatorBuilder: (context, editingController, chunkController, notifier) {
|
||||
return Row(
|
||||
children: [
|
||||
DefaultCodeLineNumber(
|
||||
controller: editingController,
|
||||
notifier: notifier,
|
||||
textStyle: TextStyle(
|
||||
color: activeTheme.gutterTextColor,
|
||||
fontSize: (displayFontSize - 1).clamp(9.0, 30.0),
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
),
|
||||
focusedTextStyle: TextStyle(
|
||||
color: activeTheme.focusedGutterTextColor,
|
||||
fontSize: (displayFontSize - 1).clamp(9.0, 30.0),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
),
|
||||
),
|
||||
DefaultCodeChunkIndicator(
|
||||
width: 20,
|
||||
controller: chunkController,
|
||||
notifier: notifier,
|
||||
painter: DefaultCodeChunkIndicatorPainter(
|
||||
color: activeTheme.gutterTextColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
indicatorBuilder: (context, editingController, chunkController, notifier) {
|
||||
return Row(
|
||||
children: [
|
||||
DefaultCodeLineNumber(
|
||||
controller: editingController,
|
||||
notifier: notifier,
|
||||
textStyle: TextStyle(
|
||||
color: activeTheme.gutterTextColor,
|
||||
fontSize: (displayFontSize - 1).clamp(9.0, 30.0),
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
),
|
||||
focusedTextStyle: TextStyle(
|
||||
color: activeTheme.focusedGutterTextColor,
|
||||
fontSize: (displayFontSize - 1).clamp(9.0, 30.0),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
),
|
||||
),
|
||||
DefaultCodeChunkIndicator(
|
||||
width: 20,
|
||||
controller: chunkController,
|
||||
notifier: notifier,
|
||||
painter: DefaultCodeChunkIndicatorPainter(
|
||||
color: activeTheme.gutterTextColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
if (_isPinching && _activeZoomFontSize != null)
|
||||
Center(
|
||||
child: IgnorePointer(
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/models/project_history.dart';
|
||||
import 'package:code_editor/providers/project_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:code_editor/services/internal_project_service.dart';
|
||||
import 'package:code_editor/services/project_history_service.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -196,7 +197,12 @@ class _ProjectHistoryWidgetState
|
||||
lastFile.trim().isNotEmpty)
|
||||
? p.basename(lastFile)
|
||||
: l10n.noOpenFile;
|
||||
final rootDir = item.rootPath ?? l10n.unknownDirectory;
|
||||
final rawRoot = item.rootPath;
|
||||
final displayRoot = (rawRoot != null && rawRoot.trim().isNotEmpty)
|
||||
? (InternalProjectService.instance.isInternalProject(rawRoot)
|
||||
? p.basename(rawRoot)
|
||||
: rawRoot)
|
||||
: l10n.unknownDirectory;
|
||||
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
@@ -235,9 +241,9 @@ class _ProjectHistoryWidgetState
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
// 下面:一行小字表示根目录
|
||||
// 下面:一行小字表示根目录或内部项目名
|
||||
Text(
|
||||
rootDir,
|
||||
displayRoot,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../models/run_task.dart';
|
||||
import '../providers/run_provider.dart';
|
||||
|
||||
/// 运行任务选择弹窗(带顶部搜索栏、区分系统探测与自定义任务,点击直接调度执行)
|
||||
/// 运行任务选择弹窗(带顶部搜索栏、区分系统探测与自定义任务、支持模块深度重新同步、点击直接调度执行)
|
||||
class RunTasksDialog extends StatefulWidget {
|
||||
final List<RunTask> customTasks;
|
||||
final List<RunTask> detectedTasks;
|
||||
final String? lastRunTaskId;
|
||||
final ValueChanged<RunTask> onTaskSelected;
|
||||
final VoidCallback onEditCustomTasks;
|
||||
final void Function(String moduleId)? onSyncModule;
|
||||
final bool Function(String moduleId)? isSyncingModule;
|
||||
|
||||
const RunTasksDialog({
|
||||
super.key,
|
||||
@@ -17,6 +21,8 @@ class RunTasksDialog extends StatefulWidget {
|
||||
this.lastRunTaskId,
|
||||
required this.onTaskSelected,
|
||||
required this.onEditCustomTasks,
|
||||
this.onSyncModule,
|
||||
this.isSyncingModule,
|
||||
});
|
||||
|
||||
static Future<void> show({
|
||||
@@ -26,6 +32,8 @@ class RunTasksDialog extends StatefulWidget {
|
||||
String? lastRunTaskId,
|
||||
required ValueChanged<RunTask> onTaskSelected,
|
||||
required VoidCallback onEditCustomTasks,
|
||||
void Function(String moduleId)? onSyncModule,
|
||||
bool Function(String moduleId)? isSyncingModule,
|
||||
}) {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
@@ -35,6 +43,8 @@ class RunTasksDialog extends StatefulWidget {
|
||||
lastRunTaskId: lastRunTaskId,
|
||||
onTaskSelected: onTaskSelected,
|
||||
onEditCustomTasks: onEditCustomTasks,
|
||||
onSyncModule: onSyncModule,
|
||||
isSyncingModule: isSyncingModule,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -67,9 +77,11 @@ class _RunTasksDialogState extends State<RunTasksDialog> {
|
||||
if (_searchQuery.isEmpty) return true;
|
||||
final name = task.getLocalizedName(l10n);
|
||||
final desc = task.getLocalizedDescription(l10n);
|
||||
final group = task.group ?? '';
|
||||
return name.toLowerCase().contains(_searchQuery) ||
|
||||
task.command.toLowerCase().contains(_searchQuery) ||
|
||||
desc.toLowerCase().contains(_searchQuery) ||
|
||||
group.toLowerCase().contains(_searchQuery) ||
|
||||
(task.description?.toLowerCase().contains(_searchQuery) ?? false);
|
||||
}
|
||||
|
||||
@@ -77,10 +89,30 @@ class _RunTasksDialogState extends State<RunTasksDialog> {
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final filteredCustom = widget.customTasks.where((t) => _matchesQuery(t, l10n)).toList();
|
||||
final filteredDetected = widget.detectedTasks.where((t) => _matchesQuery(t, l10n)).toList();
|
||||
|
||||
// 优先响应父级 Provider 数据变化,保证静默后台自省完成后对话框无感自动刷新
|
||||
RunProvider? runProvider;
|
||||
try {
|
||||
runProvider = Provider.of<RunProvider>(context);
|
||||
} catch (_) {}
|
||||
|
||||
final effectiveCustomTasks = runProvider?.customTasks ?? widget.customTasks;
|
||||
final effectiveDetectedTasks = runProvider?.detectedTasks ?? widget.detectedTasks;
|
||||
final effectiveLastRunTaskId = runProvider?.lastRunTaskId ?? widget.lastRunTaskId;
|
||||
|
||||
final filteredCustom = effectiveCustomTasks.where((t) => _matchesQuery(t, l10n)).toList();
|
||||
final filteredDetected = effectiveDetectedTasks.where((t) => _matchesQuery(t, l10n)).toList();
|
||||
final hasAny = filteredCustom.isNotEmpty || filteredDetected.isNotEmpty;
|
||||
|
||||
// 检测是否存在 Gradle 模块任务
|
||||
final hasGradleTasks = filteredDetected.any(
|
||||
(t) => t.moduleId == 'gradle' || t.id.startsWith('gradle_') || t.id.startsWith('detected_gradle_'),
|
||||
);
|
||||
|
||||
final isGradleSyncing = (widget.isSyncingModule != null)
|
||||
? widget.isSyncingModule!('gradle')
|
||||
: (runProvider?.isModuleSyncing('gradle') ?? false);
|
||||
|
||||
return Dialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
child: ConstrainedBox(
|
||||
@@ -148,13 +180,26 @@ class _RunTasksDialogState extends State<RunTasksDialog> {
|
||||
: ListView(
|
||||
children: [
|
||||
if (filteredDetected.isNotEmpty) ...[
|
||||
_buildSectionHeader(context, l10n.systemDetectedTasks, Icons.radar_outlined),
|
||||
...filteredDetected.map((task) => _buildTaskItem(context, task, l10n)),
|
||||
_buildSectionHeader(
|
||||
context,
|
||||
l10n.systemDetectedTasks,
|
||||
Icons.radar_outlined,
|
||||
trailing: hasGradleTasks
|
||||
? _buildGradleSyncAction(context, l10n, isGradleSyncing, () {
|
||||
if (widget.onSyncModule != null) {
|
||||
widget.onSyncModule!('gradle');
|
||||
} else {
|
||||
runProvider?.syncModuleTasks('gradle');
|
||||
}
|
||||
})
|
||||
: null,
|
||||
),
|
||||
...filteredDetected.map((task) => _buildTaskItem(context, task, l10n, effectiveLastRunTaskId)),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
if (filteredCustom.isNotEmpty) ...[
|
||||
_buildSectionHeader(context, l10n.userCustomTasks, Icons.person_outline),
|
||||
...filteredCustom.map((task) => _buildTaskItem(context, task, l10n)),
|
||||
...filteredCustom.map((task) => _buildTaskItem(context, task, l10n, effectiveLastRunTaskId)),
|
||||
],
|
||||
],
|
||||
),
|
||||
@@ -166,7 +211,70 @@ class _RunTasksDialogState extends State<RunTasksDialog> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSectionHeader(BuildContext context, String title, IconData icon) {
|
||||
Widget _buildGradleSyncAction(
|
||||
BuildContext context,
|
||||
AppLocalizations l10n,
|
||||
bool isSyncing,
|
||||
VoidCallback onSync,
|
||||
) {
|
||||
final theme = Theme.of(context);
|
||||
if (isSyncing) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 6),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 14,
|
||||
height: 14,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: theme.colorScheme.primary,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
l10n.syncingTasks,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: theme.colorScheme.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return InkWell(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
onTap: onSync,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.refresh, size: 14, color: theme.colorScheme.primary),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
l10n.resyncModuleTasks,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: theme.colorScheme.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSectionHeader(
|
||||
BuildContext context,
|
||||
String title,
|
||||
IconData icon, {
|
||||
Widget? trailing,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 4),
|
||||
@@ -174,21 +282,24 @@ class _RunTasksDialogState extends State<RunTasksDialog> {
|
||||
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,
|
||||
Expanded(
|
||||
child: Text(
|
||||
title,
|
||||
style: theme.textTheme.labelMedium?.copyWith(
|
||||
color: theme.colorScheme.primary,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (trailing != null) trailing,
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTaskItem(BuildContext context, RunTask task, AppLocalizations l10n) {
|
||||
Widget _buildTaskItem(BuildContext context, RunTask task, AppLocalizations l10n, String? lastRunTaskId) {
|
||||
final theme = Theme.of(context);
|
||||
final isLastRun = widget.lastRunTaskId != null && widget.lastRunTaskId == task.id;
|
||||
final isLastRun = lastRunTaskId != null && lastRunTaskId == task.id;
|
||||
|
||||
final cardColor = isLastRun
|
||||
? Colors.green.withValues(alpha: 0.15)
|
||||
@@ -222,6 +333,23 @@ class _RunTasksDialogState extends State<RunTasksDialog> {
|
||||
),
|
||||
),
|
||||
),
|
||||
if (task.group != null && task.group!.isNotEmpty && task.group != 'single_file')
|
||||
Container(
|
||||
margin: const EdgeInsets.only(left: 6),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1.5),
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.secondaryContainer.withValues(alpha: 0.6),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Text(
|
||||
task.group!,
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: theme.colorScheme.onSecondaryContainer,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (isLastRun)
|
||||
Container(
|
||||
margin: const EdgeInsets.only(left: 6),
|
||||
|
||||
@@ -0,0 +1,524 @@
|
||||
// ignore_for_file: implementation_imports
|
||||
import 'dart:math';
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:xterm/src/ui/render.dart';
|
||||
import 'package:xterm/xterm.dart' as xterm;
|
||||
|
||||
enum _DraggingHandle { none, start, end }
|
||||
|
||||
/// 终端文字框选交互叠加层
|
||||
/// 包含双端拖动手柄(Start/End Handle)与高层级浮动上下文操作菜单(复制、粘贴、全选)
|
||||
class TerminalSelectionOverlay extends StatefulWidget {
|
||||
final xterm.Terminal terminal;
|
||||
final xterm.TerminalController controller;
|
||||
final GlobalKey<xterm.TerminalViewState> terminalViewKey;
|
||||
final FocusNode focusNode;
|
||||
final Widget child;
|
||||
|
||||
const TerminalSelectionOverlay({
|
||||
super.key,
|
||||
required this.terminal,
|
||||
required this.controller,
|
||||
required this.terminalViewKey,
|
||||
required this.focusNode,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
@override
|
||||
State<TerminalSelectionOverlay> createState() => _TerminalSelectionOverlayState();
|
||||
}
|
||||
|
||||
class _TerminalSelectionOverlayState extends State<TerminalSelectionOverlay> {
|
||||
OverlayEntry? _menuOverlayEntry;
|
||||
_DraggingHandle _dragging = _DraggingHandle.none;
|
||||
bool _isScrolling = false;
|
||||
bool _isPointerSelecting = false;
|
||||
Offset? _lastActiveAnchor;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
widget.controller.addListener(_onSelectionChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(TerminalSelectionOverlay oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.controller != widget.controller) {
|
||||
oldWidget.controller.removeListener(_onSelectionChanged);
|
||||
widget.controller.addListener(_onSelectionChanged);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
widget.controller.removeListener(_onSelectionChanged);
|
||||
_hideMenu();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
RenderTerminal? get _renderTerminal {
|
||||
return widget.terminalViewKey.currentState?.renderTerminal;
|
||||
}
|
||||
|
||||
void _onSelectionChanged() {
|
||||
if (!mounted) return;
|
||||
final selection = widget.controller.selection;
|
||||
if (selection == null || selection.isCollapsed) {
|
||||
_hideMenu();
|
||||
setState(() {});
|
||||
return;
|
||||
}
|
||||
|
||||
// 若非正在拖动手柄或滑动,展示菜单
|
||||
if (_dragging == _DraggingHandle.none && !_isScrolling && !_isPointerSelecting) {
|
||||
_updateLastActiveAnchorDefault();
|
||||
_showMenu();
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
void _updateLastActiveAnchorDefault() {
|
||||
final render = _renderTerminal;
|
||||
final selection = widget.controller.selection;
|
||||
if (render == null || selection == null || selection.isCollapsed) return;
|
||||
|
||||
final range = selection.normalized;
|
||||
final endOffset = render.getOffset(range.end);
|
||||
final globalEnd = render.localToGlobal(
|
||||
endOffset + Offset(render.cellSize.width, render.cellSize.height),
|
||||
);
|
||||
_lastActiveAnchor = globalEnd;
|
||||
}
|
||||
|
||||
void _hideMenu() {
|
||||
_menuOverlayEntry?.remove();
|
||||
_menuOverlayEntry = null;
|
||||
}
|
||||
|
||||
void _showMenu([Offset? targetAnchor]) {
|
||||
_hideMenu();
|
||||
if (!mounted) return;
|
||||
|
||||
final selection = widget.controller.selection;
|
||||
if (selection == null || selection.isCollapsed) return;
|
||||
|
||||
final anchor = targetAnchor ?? _lastActiveAnchor;
|
||||
if (anchor == null) return;
|
||||
_lastActiveAnchor = anchor;
|
||||
|
||||
final overlayState = Overlay.of(context, rootOverlay: true);
|
||||
|
||||
_menuOverlayEntry = OverlayEntry(
|
||||
builder: (context) {
|
||||
return _TerminalContextMenuWidget(
|
||||
anchor: anchor,
|
||||
onCopy: _handleCopy,
|
||||
onPaste: _handlePaste,
|
||||
onSelectAll: _handleSelectAll,
|
||||
onDismiss: _hideMenu,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
overlayState.insert(_menuOverlayEntry!);
|
||||
}
|
||||
|
||||
Future<void> _handleCopy() async {
|
||||
final selection = widget.controller.selection;
|
||||
if (selection == null || selection.isCollapsed) {
|
||||
_hideMenu();
|
||||
return;
|
||||
}
|
||||
final text = widget.terminal.buffer.getText(selection);
|
||||
await Clipboard.setData(ClipboardData(text: text));
|
||||
_hideMenu();
|
||||
widget.controller.clearSelection();
|
||||
if (mounted) {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
if (l10n != null) {
|
||||
DialogUtils.showSuccessToast(context, l10n.copiedToClipboard);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _handlePaste() async {
|
||||
_hideMenu();
|
||||
final data = await Clipboard.getData(Clipboard.kTextPlain);
|
||||
final text = data?.text;
|
||||
if (text != null && text.isNotEmpty) {
|
||||
widget.terminal.paste(text);
|
||||
widget.controller.clearSelection();
|
||||
}
|
||||
}
|
||||
|
||||
void _handleSelectAll() {
|
||||
final width = widget.terminal.viewWidth;
|
||||
final height = widget.terminal.buffer.height;
|
||||
widget.controller.setSelection(
|
||||
widget.terminal.buffer.createAnchor(0, 0),
|
||||
widget.terminal.buffer.createAnchor(max(0, width - 1), max(0, height - 1)),
|
||||
mode: xterm.SelectionMode.line,
|
||||
);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
_updateLastActiveAnchorDefault();
|
||||
_showMenu();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// --- 手柄拖拽逻辑 ---
|
||||
|
||||
void _onStartHandlePanStart(DragStartDetails details) {
|
||||
_dragging = _DraggingHandle.start;
|
||||
_hideMenu();
|
||||
}
|
||||
|
||||
void _onStartHandlePanUpdate(DragUpdateDetails details) {
|
||||
final render = _renderTerminal;
|
||||
final selection = widget.controller.selection;
|
||||
if (render == null || selection == null) return;
|
||||
|
||||
final range = selection.normalized;
|
||||
final localPos = render.globalToLocal(details.globalPosition);
|
||||
final adjusted = Offset(localPos.dx, localPos.dy - render.cellSize.height / 2);
|
||||
final cell = render.getCellOffset(adjusted);
|
||||
|
||||
// 约束:起始手柄不能越过结束手柄
|
||||
if (cell.isBefore(range.end) || cell.isEqual(range.end)) {
|
||||
widget.controller.setSelection(
|
||||
widget.terminal.buffer.createAnchorFromOffset(cell),
|
||||
widget.terminal.buffer.createAnchorFromOffset(range.end),
|
||||
);
|
||||
final newLocal = render.getOffset(cell);
|
||||
_lastActiveAnchor = render.localToGlobal(newLocal + Offset(0, render.cellSize.height));
|
||||
}
|
||||
}
|
||||
|
||||
void _onStartHandlePanEnd(DragEndDetails details) {
|
||||
_dragging = _DraggingHandle.none;
|
||||
if (mounted && widget.controller.selection != null && !widget.controller.selection!.isCollapsed) {
|
||||
_showMenu(_lastActiveAnchor);
|
||||
}
|
||||
}
|
||||
|
||||
void _onEndHandlePanStart(DragStartDetails details) {
|
||||
_dragging = _DraggingHandle.end;
|
||||
_hideMenu();
|
||||
}
|
||||
|
||||
void _onEndHandlePanUpdate(DragUpdateDetails details) {
|
||||
final render = _renderTerminal;
|
||||
final selection = widget.controller.selection;
|
||||
if (render == null || selection == null) return;
|
||||
|
||||
final range = selection.normalized;
|
||||
final localPos = render.globalToLocal(details.globalPosition);
|
||||
final adjusted = Offset(localPos.dx, localPos.dy - render.cellSize.height / 2);
|
||||
final cell = render.getCellOffset(adjusted);
|
||||
|
||||
// 约束:结束手柄不能移到起始手柄之前
|
||||
if (cell.isAfter(range.begin) || cell.isEqual(range.begin)) {
|
||||
widget.controller.setSelection(
|
||||
widget.terminal.buffer.createAnchorFromOffset(range.begin),
|
||||
widget.terminal.buffer.createAnchorFromOffset(cell),
|
||||
);
|
||||
final newLocal = render.getOffset(cell);
|
||||
_lastActiveAnchor = render.localToGlobal(
|
||||
newLocal + Offset(render.cellSize.width, render.cellSize.height),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _onEndHandlePanEnd(DragEndDetails details) {
|
||||
_dragging = _DraggingHandle.none;
|
||||
if (mounted && widget.controller.selection != null && !widget.controller.selection!.isCollapsed) {
|
||||
_showMenu(_lastActiveAnchor);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final selection = widget.controller.selection;
|
||||
final hasSelection = selection != null && !selection.isCollapsed;
|
||||
final render = _renderTerminal;
|
||||
|
||||
Offset? startHandleLocal;
|
||||
Offset? endHandleLocal;
|
||||
|
||||
if (hasSelection && render != null && render.hasSize) {
|
||||
final range = selection.normalized;
|
||||
final startCellOffset = render.getOffset(range.begin);
|
||||
final endCellOffset = render.getOffset(range.end);
|
||||
|
||||
final viewportHeight = render.size.height;
|
||||
final viewportWidth = render.size.width;
|
||||
|
||||
// 起始手柄锚点(字符底部左侧)
|
||||
final rawStartY = startCellOffset.dy + render.cellSize.height;
|
||||
final clampedStartY = rawStartY.clamp(0.0, viewportHeight - 10.0);
|
||||
final clampedStartX = startCellOffset.dx.clamp(0.0, viewportWidth - 20.0);
|
||||
startHandleLocal = Offset(clampedStartX, clampedStartY);
|
||||
|
||||
// 结束手柄锚点(字符底部右侧)
|
||||
final rawEndY = endCellOffset.dy + render.cellSize.height;
|
||||
final clampedEndY = rawEndY.clamp(0.0, viewportHeight - 10.0);
|
||||
final clampedEndX = (endCellOffset.dx + render.cellSize.width).clamp(0.0, viewportWidth - 20.0);
|
||||
endHandleLocal = Offset(clampedEndX, clampedEndY);
|
||||
}
|
||||
|
||||
return NotificationListener<ScrollNotification>(
|
||||
onNotification: (notification) {
|
||||
if (notification is ScrollStartNotification) {
|
||||
_isScrolling = true;
|
||||
_hideMenu();
|
||||
} else if (notification is ScrollUpdateNotification) {
|
||||
if (mounted) setState(() {});
|
||||
} else if (notification is ScrollEndNotification) {
|
||||
_isScrolling = false;
|
||||
if (hasSelection && mounted && _dragging == _DraggingHandle.none) {
|
||||
_showMenu(_lastActiveAnchor);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
child: Listener(
|
||||
onPointerDown: (event) {
|
||||
_isPointerSelecting = true;
|
||||
},
|
||||
onPointerMove: (event) {
|
||||
if (_isPointerSelecting) {
|
||||
_hideMenu();
|
||||
}
|
||||
},
|
||||
onPointerUp: (event) {
|
||||
_isPointerSelecting = false;
|
||||
if (widget.controller.selection != null && !widget.controller.selection!.isCollapsed) {
|
||||
_lastActiveAnchor = event.position;
|
||||
if (_dragging == _DraggingHandle.none && !_isScrolling) {
|
||||
_showMenu(_lastActiveAnchor);
|
||||
}
|
||||
}
|
||||
setState(() {});
|
||||
},
|
||||
child: Stack(
|
||||
children: [
|
||||
widget.child,
|
||||
if (hasSelection && startHandleLocal != null)
|
||||
Positioned(
|
||||
left: startHandleLocal.dx - 32.0,
|
||||
top: startHandleLocal.dy - 10.0,
|
||||
child: _TerminalSelectionHandle(
|
||||
isStart: true,
|
||||
lineHeight: render?.cellSize.height ?? 20.0,
|
||||
onPanStart: _onStartHandlePanStart,
|
||||
onPanUpdate: _onStartHandlePanUpdate,
|
||||
onPanEnd: _onStartHandlePanEnd,
|
||||
),
|
||||
),
|
||||
if (hasSelection && endHandleLocal != null)
|
||||
Positioned(
|
||||
left: endHandleLocal.dx - 10.0,
|
||||
top: endHandleLocal.dy - 10.0,
|
||||
child: _TerminalSelectionHandle(
|
||||
isStart: false,
|
||||
lineHeight: render?.cellSize.height ?? 20.0,
|
||||
onPanStart: _onEndHandlePanStart,
|
||||
onPanUpdate: _onEndHandlePanUpdate,
|
||||
onPanEnd: _onEndHandlePanEnd,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 终端选区端点拖动手柄组件(采用与编辑区一致的 Material 水滴造型手柄,兼顾触屏大热区与鼠标拖拽)
|
||||
class _TerminalSelectionHandle extends StatelessWidget {
|
||||
final bool isStart;
|
||||
final double lineHeight;
|
||||
final GestureDragStartCallback onPanStart;
|
||||
final GestureDragUpdateCallback onPanUpdate;
|
||||
final GestureDragEndCallback onPanEnd;
|
||||
|
||||
const _TerminalSelectionHandle({
|
||||
required this.isStart,
|
||||
required this.lineHeight,
|
||||
required this.onPanStart,
|
||||
required this.onPanUpdate,
|
||||
required this.onPanEnd,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final type = isStart ? TextSelectionHandleType.left : TextSelectionHandleType.right;
|
||||
|
||||
return MouseRegion(
|
||||
cursor: SystemMouseCursors.grab,
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onPanStart: onPanStart,
|
||||
onPanUpdate: onPanUpdate,
|
||||
onPanEnd: onPanEnd,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
child: materialTextSelectionControls.buildHandle(
|
||||
context,
|
||||
type,
|
||||
lineHeight,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 终端高层级浮动上下文菜单(挂载于 Root Overlay)
|
||||
class _TerminalContextMenuWidget extends StatelessWidget {
|
||||
final Offset anchor;
|
||||
final VoidCallback onCopy;
|
||||
final VoidCallback onPaste;
|
||||
final VoidCallback onSelectAll;
|
||||
final VoidCallback onDismiss;
|
||||
|
||||
const _TerminalContextMenuWidget({
|
||||
required this.anchor,
|
||||
required this.onCopy,
|
||||
required this.onPaste,
|
||||
required this.onSelectAll,
|
||||
required this.onDismiss,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final theme = Theme.of(context);
|
||||
final mediaQuery = MediaQuery.of(context);
|
||||
|
||||
const menuWidth = 210.0;
|
||||
const menuHeight = 44.0;
|
||||
|
||||
// 水平居中并限制在屏幕内
|
||||
final screenWidth = mediaQuery.size.width;
|
||||
final double left = (anchor.dx - menuWidth / 2).clamp(12.0, max(12.0, screenWidth - menuWidth - 12.0));
|
||||
|
||||
// 垂直计算:优先位于锚点上方,若贴近屏幕顶端则悬浮于锚点下方
|
||||
final topThreshold = mediaQuery.padding.top + 48.0;
|
||||
final double top;
|
||||
if (anchor.dy - menuHeight - 12.0 >= topThreshold) {
|
||||
top = anchor.dy - menuHeight - 12.0;
|
||||
} else {
|
||||
top = (anchor.dy + 14.0).clamp(topThreshold, mediaQuery.size.height - menuHeight - 12.0);
|
||||
}
|
||||
|
||||
return Stack(
|
||||
children: [
|
||||
// 点击外部空白区域关闭菜单
|
||||
Positioned.fill(
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onTap: onDismiss,
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
left: left,
|
||||
top: top,
|
||||
child: Material(
|
||||
elevation: 8.0,
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
color: theme.colorScheme.surfaceContainerHighest,
|
||||
surfaceTintColor: theme.colorScheme.surfaceTint,
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Container(
|
||||
height: menuHeight,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4.0),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
border: Border.all(
|
||||
color: theme.colorScheme.outlineVariant.withValues(alpha: 0.5),
|
||||
width: 0.8,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_MenuItemButton(
|
||||
label: l10n.copy,
|
||||
icon: Icons.copy_rounded,
|
||||
onPressed: onCopy,
|
||||
),
|
||||
_buildDivider(theme),
|
||||
_MenuItemButton(
|
||||
label: l10n.paste,
|
||||
icon: Icons.paste_rounded,
|
||||
onPressed: onPaste,
|
||||
),
|
||||
_buildDivider(theme),
|
||||
_MenuItemButton(
|
||||
label: l10n.selectAll,
|
||||
icon: Icons.select_all_rounded,
|
||||
onPressed: onSelectAll,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDivider(ThemeData theme) {
|
||||
return Container(
|
||||
width: 1.0,
|
||||
height: 20.0,
|
||||
color: theme.colorScheme.outlineVariant.withValues(alpha: 0.6),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MenuItemButton extends StatelessWidget {
|
||||
final String label;
|
||||
final IconData icon;
|
||||
final VoidCallback onPressed;
|
||||
|
||||
const _MenuItemButton({
|
||||
required this.label,
|
||||
required this.icon,
|
||||
required this.onPressed,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return InkWell(
|
||||
onTap: onPressed,
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 8.0),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 16.0, color: theme.colorScheme.primary),
|
||||
const SizedBox(width: 4.0),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 13.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: theme.colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user