1
This commit is contained in:
1 parent
54c3083c24
commit
87e03ce0d8
44 files changed
+3965
-307
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/views/terminal_view.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
final String? filePath;
|
||||
@@ -9,6 +11,7 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
final VoidCallback? onSave;
|
||||
final VoidCallback? onSaveAll;
|
||||
final VoidCallback? onRun;
|
||||
final VoidCallback? onTerminal;
|
||||
final VoidCallback? onCloseAllTabs;
|
||||
final VoidCallback? onCloseProject;
|
||||
final VoidCallback? onSettings;
|
||||
@@ -21,6 +24,7 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
this.onSave,
|
||||
this.onSaveAll,
|
||||
this.onRun,
|
||||
this.onTerminal,
|
||||
this.onCloseAllTabs,
|
||||
this.onCloseProject,
|
||||
this.onSettings,
|
||||
@@ -61,104 +65,322 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
subtitleText = null;
|
||||
}
|
||||
|
||||
return AppBar(
|
||||
backgroundColor: theme.colorScheme.primaryContainer,
|
||||
foregroundColor: theme.colorScheme.onSurface,
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
titleText,
|
||||
style: const TextStyle(
|
||||
fontSize: 16.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
if (subtitleText != null)
|
||||
return CodeEditorTapRegion(
|
||||
child: AppBar(
|
||||
backgroundColor: theme.colorScheme.primaryContainer,
|
||||
foregroundColor: theme.colorScheme.onSurface,
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
subtitleText,
|
||||
style: TextStyle(
|
||||
fontSize: 11.0,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
titleText,
|
||||
style: const TextStyle(
|
||||
fontSize: 16.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
if (subtitleText != null)
|
||||
Text(
|
||||
subtitleText,
|
||||
style: TextStyle(
|
||||
fontSize: 11.0,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
Focus(
|
||||
canRequestFocus: false,
|
||||
skipTraversal: true,
|
||||
child: IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.play_arrow),
|
||||
tooltip: l10n.run,
|
||||
onPressed: onRun ?? () {},
|
||||
),
|
||||
),
|
||||
Focus(
|
||||
canRequestFocus: false,
|
||||
skipTraversal: true,
|
||||
child: IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.save_outlined),
|
||||
tooltip: l10n.save,
|
||||
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,
|
||||
onCloseAllTabs: onCloseAllTabs,
|
||||
onCloseProject: onCloseProject,
|
||||
onSettings: onSettings,
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.play_arrow),
|
||||
tooltip: l10n.run,
|
||||
onPressed: onRun ?? () {},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 基于 OverlayEntry 的右上角更多菜单组件(带对齐右上角缩放淡入淡出动画,非模态不关闭键盘)
|
||||
class _MoreMenuButton extends StatefulWidget {
|
||||
final VoidCallback? onSaveAll;
|
||||
final VoidCallback? onCloseAllTabs;
|
||||
final VoidCallback? onCloseProject;
|
||||
final VoidCallback? onSettings;
|
||||
|
||||
const _MoreMenuButton({
|
||||
this.onSaveAll,
|
||||
this.onCloseAllTabs,
|
||||
this.onCloseProject,
|
||||
this.onSettings,
|
||||
});
|
||||
|
||||
@override
|
||||
State<_MoreMenuButton> createState() => _MoreMenuButtonState();
|
||||
}
|
||||
|
||||
class _MoreMenuButtonState extends State<_MoreMenuButton> with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _animController;
|
||||
late final Animation<double> _fadeAnimation;
|
||||
late final Animation<double> _scaleAnimation;
|
||||
OverlayEntry? _barrierEntry;
|
||||
OverlayEntry? _menuEntry;
|
||||
final GlobalKey _buttonKey = GlobalKey();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_animController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 150),
|
||||
reverseDuration: const Duration(milliseconds: 100),
|
||||
);
|
||||
_fadeAnimation = CurvedAnimation(
|
||||
parent: _animController,
|
||||
curve: Curves.easeOutCubic,
|
||||
reverseCurve: Curves.easeInCubic,
|
||||
);
|
||||
_scaleAnimation = Tween<double>(begin: 0.85, end: 1.0).animate(
|
||||
CurvedAnimation(
|
||||
parent: _animController,
|
||||
curve: Curves.easeOutCubic,
|
||||
reverseCurve: Curves.easeInCubic,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_animController.dispose();
|
||||
_forceRemoveEntries();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _forceRemoveEntries() {
|
||||
if (_barrierEntry != null && _barrierEntry!.mounted) {
|
||||
_barrierEntry!.remove();
|
||||
}
|
||||
_barrierEntry = null;
|
||||
|
||||
if (_menuEntry != null && _menuEntry!.mounted) {
|
||||
_menuEntry!.remove();
|
||||
}
|
||||
_menuEntry = null;
|
||||
}
|
||||
|
||||
void _closeMenu({VoidCallback? onClosed}) async {
|
||||
if (_menuEntry == null) {
|
||||
onClosed?.call();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await _animController.reverse();
|
||||
} catch (_) {}
|
||||
if (mounted) {
|
||||
_forceRemoveEntries();
|
||||
onClosed?.call();
|
||||
}
|
||||
}
|
||||
|
||||
void _showMenu() {
|
||||
_forceRemoveEntries();
|
||||
|
||||
final overlay = Overlay.maybeOf(context, rootOverlay: true);
|
||||
if (overlay == null) return;
|
||||
|
||||
final renderBox = _buttonKey.currentContext?.findRenderObject() as RenderBox?;
|
||||
if (renderBox == null) return;
|
||||
|
||||
final size = renderBox.size;
|
||||
final offset = renderBox.localToGlobal(Offset.zero);
|
||||
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final onSurfaceColor = theme.colorScheme.onSurface;
|
||||
|
||||
_barrierEntry = OverlayEntry(
|
||||
builder: (ctx) => Positioned.fill(
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onTap: () => _closeMenu(),
|
||||
),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.save_outlined),
|
||||
tooltip: l10n.save,
|
||||
onPressed: onSave ?? () {},
|
||||
),
|
||||
PopupMenuButton<String>(
|
||||
icon: const Icon(Icons.more_vert),
|
||||
tooltip: l10n.more,
|
||||
position: PopupMenuPosition.under,
|
||||
onSelected: (value) {
|
||||
if (value == 'save_all') {
|
||||
onSaveAll?.call();
|
||||
} else if (value == 'close_all_tabs') {
|
||||
onCloseAllTabs?.call();
|
||||
} else if (value == 'close_project') {
|
||||
onCloseProject?.call();
|
||||
} else if (value == 'settings') {
|
||||
onSettings?.call();
|
||||
}
|
||||
},
|
||||
itemBuilder: (context) => [
|
||||
PopupMenuItem<String>(
|
||||
value: 'save_all',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.save_as_outlined, size: 20),
|
||||
const SizedBox(width: 10),
|
||||
Text(l10n.saveAll),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
_menuEntry = OverlayEntry(
|
||||
builder: (ctx) {
|
||||
return Positioned(
|
||||
top: offset.dy + size.height + 4,
|
||||
right: 8,
|
||||
child: FadeTransition(
|
||||
opacity: _fadeAnimation,
|
||||
child: ScaleTransition(
|
||||
scale: _scaleAnimation,
|
||||
alignment: Alignment.topRight,
|
||||
child: Material(
|
||||
color: theme.colorScheme.surface,
|
||||
surfaceTintColor: theme.colorScheme.surfaceTint,
|
||||
elevation: 6,
|
||||
shadowColor: Colors.black26,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Container(
|
||||
width: 170,
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
_buildMenuItem(
|
||||
icon: Icons.save_as_outlined,
|
||||
title: l10n.saveAll,
|
||||
iconColor: onSurfaceColor,
|
||||
onTap: () {
|
||||
_closeMenu(onClosed: widget.onSaveAll);
|
||||
},
|
||||
),
|
||||
_buildMenuItem(
|
||||
icon: Icons.close_fullscreen_outlined,
|
||||
title: l10n.closeAllTabs,
|
||||
iconColor: onSurfaceColor,
|
||||
onTap: () {
|
||||
_closeMenu(onClosed: widget.onCloseAllTabs);
|
||||
},
|
||||
),
|
||||
Divider(
|
||||
height: 9,
|
||||
thickness: 0.8,
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
),
|
||||
_buildMenuItem(
|
||||
icon: Icons.folder_off_outlined,
|
||||
title: l10n.closeProject,
|
||||
iconColor: onSurfaceColor,
|
||||
onTap: () {
|
||||
_closeMenu(onClosed: widget.onCloseProject);
|
||||
},
|
||||
),
|
||||
Divider(
|
||||
height: 9,
|
||||
thickness: 0.8,
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
),
|
||||
_buildMenuItem(
|
||||
icon: Icons.settings,
|
||||
title: l10n.settings,
|
||||
iconColor: onSurfaceColor,
|
||||
onTap: () {
|
||||
_closeMenu(onClosed: widget.onSettings);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
PopupMenuItem<String>(
|
||||
value: 'close_all_tabs',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.close_fullscreen_outlined, size: 20),
|
||||
const SizedBox(width: 10),
|
||||
Text(l10n.closeAllTabs),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
overlay.insertAll([_barrierEntry!, _menuEntry!]);
|
||||
_animController.forward(from: 0.0);
|
||||
}
|
||||
|
||||
Widget _buildMenuItem({
|
||||
required IconData icon,
|
||||
required String title,
|
||||
required Color iconColor,
|
||||
required VoidCallback onTap,
|
||||
bool isDestructive = false,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: 18,
|
||||
color: isDestructive ? theme.colorScheme.error : iconColor,
|
||||
),
|
||||
PopupMenuItem<String>(
|
||||
value: 'close_project',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.folder_off_outlined, size: 20),
|
||||
const SizedBox(width: 10),
|
||||
Text(l10n.closeProject),
|
||||
],
|
||||
),
|
||||
),
|
||||
PopupMenuItem<String>(
|
||||
value: 'settings',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.settings, size: 20),
|
||||
const SizedBox(width: 10),
|
||||
Text(l10n.settings),
|
||||
],
|
||||
const SizedBox(width: 10),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 14.0,
|
||||
color: isDestructive ? theme.colorScheme.error : theme.colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
return Focus(
|
||||
canRequestFocus: false,
|
||||
skipTraversal: true,
|
||||
child: IconButton(
|
||||
key: _buttonKey,
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.more_vert),
|
||||
tooltip: l10n.more,
|
||||
onPressed: _showMenu,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/providers/project_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:code_editor/services/permission_service.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:code_editor/widgets/project_history_widget.dart';
|
||||
import 'package:code_editor/widgets/file_tree_widget.dart';
|
||||
@@ -11,15 +12,27 @@ import 'package:provider/provider.dart';
|
||||
class CodeEditorDrawer extends StatelessWidget {
|
||||
const CodeEditorDrawer({super.key});
|
||||
|
||||
static ProjectProvider _getProjectProvider(BuildContext context, {bool listen = false}) {
|
||||
return listen ? context.watch<ProjectProvider>() : context.read<ProjectProvider>();
|
||||
static ProjectProvider _getProjectProvider(
|
||||
BuildContext context, {
|
||||
bool listen = false,
|
||||
}) {
|
||||
return listen
|
||||
? context.watch<ProjectProvider>()
|
||||
: context.read<ProjectProvider>();
|
||||
}
|
||||
|
||||
static TabProvider _getTabProvider(BuildContext context, {bool listen = false}) {
|
||||
static TabProvider _getTabProvider(
|
||||
BuildContext context, {
|
||||
bool listen = false,
|
||||
}) {
|
||||
return listen ? context.watch<TabProvider>() : context.read<TabProvider>();
|
||||
}
|
||||
|
||||
Future<void> _handleNewFile(BuildContext context, String rootPath) async {
|
||||
final hasPermission = await PermissionService.instance
|
||||
.ensureStoragePermission(context: context);
|
||||
if (!hasPermission || !context.mounted) return;
|
||||
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final name = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
@@ -35,13 +48,20 @@ class CodeEditorDrawer extends StatelessWidget {
|
||||
}
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
DialogUtils.showErrorToast(context, l10n?.createFileFailed(e.toString()) ?? '创建文件失败: $e');
|
||||
DialogUtils.showErrorToast(
|
||||
context,
|
||||
l10n?.createFileFailed(e.toString()) ?? '创建文件失败: $e',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _handleNewFolder(BuildContext context, String rootPath) async {
|
||||
final hasPermission = await PermissionService.instance
|
||||
.ensureStoragePermission(context: context);
|
||||
if (!hasPermission || !context.mounted) return;
|
||||
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final name = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
@@ -50,10 +70,15 @@ class CodeEditorDrawer extends StatelessWidget {
|
||||
);
|
||||
if (name != null && name.trim().isNotEmpty && context.mounted) {
|
||||
try {
|
||||
await _getProjectProvider(context).createDirectory(rootPath, name.trim());
|
||||
await _getProjectProvider(
|
||||
context,
|
||||
).createDirectory(rootPath, name.trim());
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
DialogUtils.showErrorToast(context, l10n?.createFolderFailed(e.toString()) ?? '创建文件夹失败: $e');
|
||||
DialogUtils.showErrorToast(
|
||||
context,
|
||||
l10n?.createFolderFailed(e.toString()) ?? '创建文件夹失败: $e',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,101 +93,120 @@ class CodeEditorDrawer extends StatelessWidget {
|
||||
final hasProject = rootPath != null && rootPath.trim().isNotEmpty;
|
||||
|
||||
return Drawer(
|
||||
child: SafeArea(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.primaryContainer,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
l10n.fileDirectory,
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: l10n.refreshDirectory,
|
||||
onPressed: () => _getProjectProvider(context).refreshTree(),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.folder_open),
|
||||
tooltip: l10n.openFileDirectory,
|
||||
onPressed: () async {
|
||||
final canProceed = await _getTabProvider(context).checkUnsavedChanges(context);
|
||||
if (!canProceed || !context.mounted) return;
|
||||
_getProjectProvider(context).openDirectory();
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.history),
|
||||
tooltip: l10n.viewProjectHistory,
|
||||
onPressed: () {
|
||||
ProjectHistoryWidget.show(
|
||||
context,
|
||||
onSelectHistory: (selectedHistory) async {
|
||||
final canProceed = await _getTabProvider(context).checkUnsavedChanges(context);
|
||||
if (!canProceed || !context.mounted) return;
|
||||
_getProjectProvider(context).switchProject(selectedHistory);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
rootPath ?? '',
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.primaryContainer,
|
||||
),
|
||||
child: SafeArea(
|
||||
bottom: false,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
l10n.fileDirectory,
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
),
|
||||
if (hasProject) ...[
|
||||
const SizedBox(width: 8),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
iconSize: 18,
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(minWidth: 28, minHeight: 28),
|
||||
icon: const Icon(Icons.note_add_outlined),
|
||||
tooltip: l10n.newFile,
|
||||
onPressed: () => _handleNewFile(context, rootPath),
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: l10n.refreshDirectory,
|
||||
onPressed: () =>
|
||||
_getProjectProvider(context).refreshTree(),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
iconSize: 18,
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(minWidth: 28, minHeight: 28),
|
||||
icon: const Icon(Icons.create_new_folder_outlined),
|
||||
tooltip: l10n.newFolder,
|
||||
onPressed: () => _handleNewFolder(context, rootPath),
|
||||
icon: const Icon(Icons.folder_open),
|
||||
tooltip: l10n.openFileDirectory,
|
||||
onPressed: () async {
|
||||
final hasPermission = await PermissionService
|
||||
.instance
|
||||
.ensureStoragePermission(context: context);
|
||||
if (!hasPermission || !context.mounted) return;
|
||||
final canProceed = await _getTabProvider(
|
||||
context,
|
||||
).checkUnsavedChanges(context);
|
||||
if (!canProceed || !context.mounted) return;
|
||||
_getProjectProvider(context).openDirectory();
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.history),
|
||||
tooltip: l10n.viewProjectHistory,
|
||||
onPressed: () {
|
||||
ProjectHistoryWidget.show(
|
||||
context,
|
||||
onSelectHistory: (selectedHistory) async {
|
||||
final canProceed = await _getTabProvider(
|
||||
context,
|
||||
).checkUnsavedChanges(context);
|
||||
if (!canProceed || !context.mounted) return;
|
||||
_getProjectProvider(
|
||||
context,
|
||||
).switchProject(selectedHistory);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
rootPath ?? '',
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
),
|
||||
if (hasProject) ...[
|
||||
const SizedBox(width: 8),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
iconSize: 18,
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 28,
|
||||
minHeight: 28,
|
||||
),
|
||||
icon: const Icon(Icons.note_add_outlined),
|
||||
tooltip: l10n.newFile,
|
||||
onPressed: () => _handleNewFile(context, rootPath),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
iconSize: 18,
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 28,
|
||||
minHeight: 28,
|
||||
),
|
||||
icon: const Icon(Icons.create_new_folder_outlined),
|
||||
tooltip: l10n.newFolder,
|
||||
onPressed: () =>
|
||||
_handleNewFolder(context, rootPath),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const Expanded(
|
||||
child: FileTreeWidget(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Expanded(child: SafeArea(top: false, child: FileTreeWidget())),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
/// 桌面端与移动端代码编辑器的浮动/右键菜单项组件
|
||||
/// 桌面端代码编辑器的右键菜单项组件
|
||||
class _EditorContextMenuItem extends PopupMenuItem<void> implements PreferredSizeWidget {
|
||||
_EditorContextMenuItem({
|
||||
required String text,
|
||||
@@ -26,13 +26,126 @@ class _EditorContextMenuItem extends PopupMenuItem<void> implements PreferredSiz
|
||||
Size get preferredSize => const Size(140, 36);
|
||||
}
|
||||
|
||||
/// 移动端选区悬浮工具栏(基于 OverlayEntry,绝不压入 Route,软键盘保持弹起)
|
||||
class _MobileSelectionToolbarWidget extends StatefulWidget {
|
||||
final TextSelectionToolbarAnchors anchors;
|
||||
final CodeLineEditingController controller;
|
||||
final VoidCallback onDismiss;
|
||||
final FocusNode? focusNode;
|
||||
|
||||
const _MobileSelectionToolbarWidget({
|
||||
required this.anchors,
|
||||
required this.controller,
|
||||
required this.onDismiss,
|
||||
this.focusNode,
|
||||
});
|
||||
|
||||
@override
|
||||
State<_MobileSelectionToolbarWidget> createState() => _MobileSelectionToolbarWidgetState();
|
||||
}
|
||||
|
||||
class _MobileSelectionToolbarWidgetState extends State<_MobileSelectionToolbarWidget> {
|
||||
bool _canPaste = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_checkClipboard();
|
||||
}
|
||||
|
||||
Future<void> _checkClipboard() async {
|
||||
try {
|
||||
final hasStrings = await Clipboard.hasStrings();
|
||||
if (mounted && !hasStrings) {
|
||||
setState(() {
|
||||
_canPaste = false;
|
||||
});
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final hasSelection = widget.controller.selection.baseOffset != -1 &&
|
||||
widget.controller.selection.extentOffset != -1 &&
|
||||
!widget.controller.selection.isCollapsed;
|
||||
|
||||
return AdaptiveTextSelectionToolbar.buttonItems(
|
||||
anchors: widget.anchors,
|
||||
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();
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 自定义代码编辑器选区与右键菜单控制器
|
||||
class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
const CodeEditorToolbarController();
|
||||
final FocusNode? focusNode;
|
||||
late final SelectionToolbarController _mobileController;
|
||||
|
||||
CodeEditorToolbarController({this.focusNode}) {
|
||||
_mobileController = MobileSelectionToolbarController(
|
||||
builder: ({
|
||||
required BuildContext context,
|
||||
required TextSelectionToolbarAnchors anchors,
|
||||
required CodeLineEditingController controller,
|
||||
required VoidCallback onDismiss,
|
||||
required VoidCallback onRefresh,
|
||||
}) {
|
||||
return _MobileSelectionToolbarWidget(
|
||||
anchors: anchors,
|
||||
controller: controller,
|
||||
onDismiss: onDismiss,
|
||||
focusNode: focusNode,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void hide(BuildContext context) {
|
||||
// 菜单由 showMenu / 遮罩自动关闭,不需要额外动作
|
||||
_mobileController.hide(context);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -43,6 +156,29 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
Rect? renderRect,
|
||||
required LayerLink layerLink,
|
||||
required ValueNotifier<bool> visibility,
|
||||
}) {
|
||||
if (renderRect == null) {
|
||||
_showDesktopMenu(
|
||||
context: context,
|
||||
controller: controller,
|
||||
anchors: anchors,
|
||||
);
|
||||
} else {
|
||||
_mobileController.show(
|
||||
context: context,
|
||||
controller: controller,
|
||||
anchors: anchors,
|
||||
renderRect: renderRect,
|
||||
layerLink: layerLink,
|
||||
visibility: visibility,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _showDesktopMenu({
|
||||
required BuildContext context,
|
||||
required CodeLineEditingController controller,
|
||||
required TextSelectionToolbarAnchors anchors,
|
||||
}) async {
|
||||
final hasSelection = controller.selection.baseOffset != -1 &&
|
||||
controller.selection.extentOffset != -1 &&
|
||||
@@ -110,5 +246,9 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
if (focusNode != null && !focusNode!.hasFocus) {
|
||||
focusNode!.requestFocus();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import 'package:code_editor/providers/project_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
class CodeEditorTabBar extends StatelessWidget {
|
||||
const CodeEditorTabBar({super.key});
|
||||
@@ -64,9 +65,10 @@ class CodeEditorTabBar extends StatelessWidget {
|
||||
final activePath = tabProvider.currentFilePath;
|
||||
final rootPath = projectProvider?.rootPath;
|
||||
|
||||
return Container(
|
||||
height: 36.0,
|
||||
width: double.infinity,
|
||||
return CodeEditorTapRegion(
|
||||
child: Container(
|
||||
height: 36.0,
|
||||
width: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.surfaceContainerHighest.withValues(alpha: 0.2),
|
||||
border: Border(
|
||||
@@ -120,7 +122,8 @@ class CodeEditorTabBar extends StatelessWidget {
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTabItem(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/models/app_font.dart';
|
||||
import 'package:code_editor/models/editor_tab_item.dart';
|
||||
import 'package:code_editor/models/editor_theme.dart';
|
||||
import 'package:code_editor/models/virtual_keyboard_config.dart';
|
||||
@@ -41,10 +42,13 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
|
||||
CodeLineEditingController? _controller;
|
||||
CodeScrollController? _scrollController;
|
||||
final FocusNode _focusNode = FocusNode();
|
||||
late final CodeEditorToolbarController _toolbarController;
|
||||
bool _isLoading = false;
|
||||
int _currentLoadVersion = 0;
|
||||
String? _errorMessage;
|
||||
String? _currentLoadedPath;
|
||||
int? _currentIndentSize;
|
||||
bool _isShowingConflictDialog = false;
|
||||
|
||||
EditorTabItem? _getCurrentTab() {
|
||||
@@ -121,6 +125,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_toolbarController = CodeEditorToolbarController(focusNode: _focusNode);
|
||||
_loadFileContent();
|
||||
}
|
||||
|
||||
@@ -151,6 +156,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
try {
|
||||
_getTabProvider(context).registerSaveHandler(null);
|
||||
} catch (_) {}
|
||||
_toolbarController.hide(context);
|
||||
final currentTab = _getCurrentTab();
|
||||
_saveCurrentTabScrollState(currentTab);
|
||||
_scrollController?.verticalScroller.dispose();
|
||||
@@ -158,6 +164,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
_scrollController?.dispose();
|
||||
_controller?.removeListener(_onTextChanged);
|
||||
_controller?.dispose();
|
||||
_focusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -341,6 +348,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
bool activeWordWrap = true;
|
||||
bool enableVirtualKeyboard = false;
|
||||
VirtualKeyboardConfig? keyboardConfig;
|
||||
AppFontItem activeEditorFont = AppFonts.editorMonospace;
|
||||
try {
|
||||
final settings = _getSettingsProvider(context, listen: true);
|
||||
activeTheme = settings.editorTheme;
|
||||
@@ -348,6 +356,12 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
activeWordWrap = settings.wordWrap;
|
||||
enableVirtualKeyboard = settings.enableVirtualKeyboard;
|
||||
keyboardConfig = settings.virtualKeyboardConfig;
|
||||
activeEditorFont = settings.editorFont;
|
||||
if (_currentIndentSize != null && _currentIndentSize != settings.indentSize) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) _loadFileContent();
|
||||
});
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
return Container(
|
||||
@@ -360,9 +374,10 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
child: CodeEditor(
|
||||
key: ValueKey(_currentLoadedPath),
|
||||
controller: controller,
|
||||
focusNode: _focusNode,
|
||||
scrollController: _scrollController,
|
||||
wordWrap: activeWordWrap,
|
||||
toolbarController: const CodeEditorToolbarController(),
|
||||
toolbarController: _toolbarController,
|
||||
style: CodeEditorStyle(
|
||||
fontSize: activeFontSize,
|
||||
textColor: activeTheme.textColor,
|
||||
@@ -370,7 +385,8 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
cursorColor: activeTheme.cursorColor,
|
||||
cursorLineColor: activeTheme.cursorLineColor,
|
||||
selectionColor: activeTheme.selectionColor,
|
||||
fontFamily: 'Consolas, Monaco, "Courier New", monospace',
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
codeTheme: CodeHighlightTheme(
|
||||
languages: SyntaxHighlightHelper.getLanguagesForFile(_currentLoadedPath ?? widget.filePath),
|
||||
theme: activeTheme.highlightTheme,
|
||||
@@ -385,13 +401,15 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
textStyle: TextStyle(
|
||||
color: activeTheme.gutterTextColor,
|
||||
fontSize: (activeFontSize - 1).clamp(9.0, 30.0),
|
||||
fontFamily: 'Consolas, Monaco, "Courier New", monospace',
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
),
|
||||
focusedTextStyle: TextStyle(
|
||||
color: activeTheme.focusedGutterTextColor,
|
||||
fontSize: (activeFontSize - 1).clamp(9.0, 30.0),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontFamily: 'Consolas, Monaco, "Courier New", monospace',
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
),
|
||||
),
|
||||
DefaultCodeChunkIndicator(
|
||||
@@ -410,6 +428,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
if (enableVirtualKeyboard && keyboardConfig != null)
|
||||
VirtualKeyboardWidget(
|
||||
controller: controller,
|
||||
focusNode: _focusNode,
|
||||
config: keyboardConfig,
|
||||
),
|
||||
],
|
||||
@@ -449,7 +468,15 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
? filePath
|
||||
: p.join(rootPath, filePath);
|
||||
|
||||
if (_currentLoadedPath != null && p.equals(_currentLoadedPath!, fullFilePath) && _controller != null) {
|
||||
int activeIndentSize = 2;
|
||||
try {
|
||||
activeIndentSize = _getSettingsProvider(context).indentSize;
|
||||
} catch (_) {}
|
||||
|
||||
if (_currentLoadedPath != null &&
|
||||
p.equals(_currentLoadedPath!, fullFilePath) &&
|
||||
_controller != null &&
|
||||
_currentIndentSize == activeIndentSize) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -467,10 +494,14 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
tab.content = normalizedContent;
|
||||
tab.originalContent = normalizedOriginal;
|
||||
|
||||
final newController = CodeLineEditingController.fromText(normalizedContent);
|
||||
final newController = CodeLineEditingController.fromText(
|
||||
normalizedContent,
|
||||
CodeLineOptions(indentSize: activeIndentSize),
|
||||
);
|
||||
|
||||
_controller = newController;
|
||||
_currentLoadedPath = fullFilePath;
|
||||
_currentIndentSize = activeIndentSize;
|
||||
_errorMessage = null;
|
||||
_isLoading = false;
|
||||
|
||||
@@ -514,10 +545,14 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
final oldController = _controller;
|
||||
oldController?.removeListener(_onTextChanged);
|
||||
|
||||
final newController = CodeLineEditingController.fromText(diskContent);
|
||||
final newController = CodeLineEditingController.fromText(
|
||||
diskContent,
|
||||
CodeLineOptions(indentSize: activeIndentSize),
|
||||
);
|
||||
|
||||
_controller = newController;
|
||||
_currentLoadedPath = fullFilePath;
|
||||
_currentIndentSize = activeIndentSize;
|
||||
_errorMessage = null;
|
||||
|
||||
if (tab != null) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/models/file_item.dart';
|
||||
import 'package:code_editor/providers/project_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:code_editor/services/permission_service.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -303,6 +304,12 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
final provider = _getProjectProvider(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
|
||||
// 对于文件变动类操作,先校验并确保已获取存储访问权限
|
||||
if (['new_file', 'new_folder', 'paste', 'rename', 'delete'].contains(action)) {
|
||||
final hasPermission = await PermissionService.instance.ensureStoragePermission(context: context);
|
||||
if (!hasPermission || !mounted) return;
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case 'new_file':
|
||||
final name = await DialogUtils.showInputDialog(
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import 'package:code_editor/providers/terminal_provider.dart';
|
||||
import 'package:code_editor/widgets/terminal_session_item_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
/// 终端右侧抽屉(会话列表与管理)
|
||||
class TerminalDrawer extends StatelessWidget {
|
||||
const TerminalDrawer({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final provider = context.watch<TerminalProvider>();
|
||||
final sessions = provider.sessions;
|
||||
final activeIndex = provider.activeIndex;
|
||||
|
||||
return Drawer(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// 抽屉头部:使用与文件树一致的 primaryContainer 背景色并沉浸延伸至顶部状态栏
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.primaryContainer,
|
||||
),
|
||||
child: SafeArea(
|
||||
bottom: false,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 16.0, right: 8.0, top: 4.0, bottom: 4.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
'会话',
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: theme.colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
tooltip: '添加终端',
|
||||
onPressed: () {
|
||||
// 直接添加终端,但不跳转
|
||||
provider.createSession(name: '会话', distroId: 'alpine', activate: false);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
// 终端项目列表
|
||||
Expanded(
|
||||
child: sessions.isEmpty
|
||||
? Center(
|
||||
child: Text(
|
||||
'暂无会话,请点击右上角添加',
|
||||
style: TextStyle(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
fontSize: 13.0,
|
||||
),
|
||||
),
|
||||
)
|
||||
: ReorderableListView.builder(
|
||||
buildDefaultDragHandles: false,
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
itemCount: sessions.length,
|
||||
onReorderItem: (oldIndex, newIndex) {
|
||||
provider.reorderSessions(oldIndex, newIndex);
|
||||
},
|
||||
itemBuilder: (context, index) {
|
||||
final session = sessions[index];
|
||||
return TerminalSessionItemWidget(
|
||||
key: ValueKey(session.id),
|
||||
session: session,
|
||||
index: index,
|
||||
isSelected: index == activeIndex,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
import 'package:code_editor/models/terminal_session.dart';
|
||||
import 'package:code_editor/providers/terminal_provider.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
/// 独立的终端项目组件
|
||||
class TerminalSessionItemWidget extends StatelessWidget {
|
||||
final TerminalSession session;
|
||||
final int index;
|
||||
final bool isSelected;
|
||||
|
||||
const TerminalSessionItemWidget({
|
||||
super.key,
|
||||
required this.session,
|
||||
required this.index,
|
||||
required this.isSelected,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final displayIndex = index + 1; // 索引从 1 开始
|
||||
final titleText = '($displayIndex) ${session.name}';
|
||||
final isHost = session.distroId == 'host';
|
||||
final badgeLabel = isHost ? 'Host' : 'Alpine';
|
||||
|
||||
final textStyle = theme.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||||
color: isSelected ? theme.colorScheme.primary : null,
|
||||
);
|
||||
|
||||
final iconColor = isSelected ? theme.colorScheme.primary : null;
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0),
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: GestureDetector(
|
||||
onLongPressStart: (details) {
|
||||
_showContextMenu(context, details.globalPosition, displayIndex);
|
||||
},
|
||||
child: ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 0.0),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
selected: isSelected,
|
||||
selectedTileColor: theme.colorScheme.primaryContainer.withValues(alpha: 0.5),
|
||||
leading: Icon(
|
||||
Icons.terminal,
|
||||
size: 20,
|
||||
color: iconColor,
|
||||
),
|
||||
title: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
titleText,
|
||||
style: textStyle,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6.0, vertical: 1.5),
|
||||
decoration: BoxDecoration(
|
||||
color: isHost
|
||||
? theme.colorScheme.surfaceContainerHighest
|
||||
: theme.colorScheme.primaryContainer,
|
||||
borderRadius: BorderRadius.circular(4.0),
|
||||
),
|
||||
child: Text(
|
||||
badgeLabel,
|
||||
style: TextStyle(
|
||||
fontSize: 10.0,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: isHost
|
||||
? theme.colorScheme.onSurfaceVariant
|
||||
: theme.colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
trailing: ReorderableDragStartListener(
|
||||
index: index,
|
||||
child: MouseRegion(
|
||||
cursor: SystemMouseCursors.grab,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4.0, vertical: 8.0),
|
||||
child: Icon(
|
||||
Icons.drag_handle,
|
||||
size: 20,
|
||||
color: theme.colorScheme.onSurfaceVariant.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
dense: true,
|
||||
onTap: () {
|
||||
final provider = context.read<TerminalProvider>();
|
||||
provider.selectSession(index);
|
||||
final scaffold = Scaffold.maybeOf(context);
|
||||
if (scaffold != null && scaffold.isEndDrawerOpen) {
|
||||
scaffold.closeEndDrawer();
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 长按弹出菜单(样式与文件树长按菜单完全对齐)
|
||||
void _showContextMenu(BuildContext context, Offset tapPosition, int displayIndex) async {
|
||||
final overlay = Overlay.of(context).context.findRenderObject() as RenderBox;
|
||||
final position = RelativeRect.fromRect(
|
||||
Rect.fromLTWH(tapPosition.dx, tapPosition.dy, 0, 0),
|
||||
Offset.zero & overlay.size,
|
||||
);
|
||||
|
||||
final theme = Theme.of(context);
|
||||
final onSurfaceColor = theme.colorScheme.onSurface;
|
||||
|
||||
final selectedAction = await showMenu<String>(
|
||||
context: context,
|
||||
position: position,
|
||||
color: theme.colorScheme.surface,
|
||||
surfaceTintColor: theme.colorScheme.surfaceTint,
|
||||
elevation: 6,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
items: [
|
||||
_buildMenuItem(
|
||||
value: 'rename',
|
||||
icon: Icons.edit_outlined,
|
||||
title: '重命名',
|
||||
color: onSurfaceColor,
|
||||
),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem(
|
||||
value: 'delete',
|
||||
icon: Icons.delete_outline,
|
||||
title: '删除',
|
||||
color: theme.colorScheme.error,
|
||||
isDestructive: true,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
if (selectedAction != null && context.mounted) {
|
||||
_handleMenuAction(context, selectedAction, displayIndex);
|
||||
}
|
||||
}
|
||||
|
||||
PopupMenuItem<String> _buildMenuItem({
|
||||
required String value,
|
||||
required IconData icon,
|
||||
required String title,
|
||||
required Color color,
|
||||
bool isDestructive = false,
|
||||
}) {
|
||||
return PopupMenuItem<String>(
|
||||
value: value,
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, size: 18, color: color),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(color: color),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleMenuAction(BuildContext context, String action, int displayIndex) async {
|
||||
final provider = context.read<TerminalProvider>();
|
||||
|
||||
switch (action) {
|
||||
case 'rename':
|
||||
final newName = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
title: '重命名',
|
||||
hintText: '新名称',
|
||||
initialValue: session.name,
|
||||
);
|
||||
if (newName != null && newName.trim().isNotEmpty && newName.trim() != session.name) {
|
||||
provider.renameSession(session.id, newName.trim());
|
||||
}
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
final confirmed = await DialogUtils.showDestructiveConfirmDialog(
|
||||
context,
|
||||
title: '确认删除',
|
||||
message: '确定要删除终端 "($displayIndex) ${session.name}" 吗?',
|
||||
);
|
||||
if (confirmed && context.mounted) {
|
||||
provider.removeSession(session.id);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,14 @@ import 'package:re_editor/re_editor.dart';
|
||||
/// 编辑器下方的虚拟辅助小键盘组件
|
||||
class VirtualKeyboardWidget extends StatefulWidget {
|
||||
final CodeLineEditingController? controller;
|
||||
final FocusNode? focusNode;
|
||||
final VirtualKeyboardConfig config;
|
||||
final Color? backgroundColor;
|
||||
|
||||
const VirtualKeyboardWidget({
|
||||
super.key,
|
||||
required this.controller,
|
||||
this.focusNode,
|
||||
required this.config,
|
||||
this.backgroundColor,
|
||||
});
|
||||
@@ -43,6 +45,11 @@ class _VirtualKeyboardWidgetState extends State<VirtualKeyboardWidget> {
|
||||
// 轻微触觉反馈提升敲击手感
|
||||
HapticFeedback.lightImpact();
|
||||
|
||||
// 如果提供了 focusNode 且当前未聚焦,或者为了确保软键盘不掉,请求聚焦
|
||||
if (widget.focusNode != null && !widget.focusNode!.hasFocus) {
|
||||
widget.focusNode!.requestFocus();
|
||||
}
|
||||
|
||||
switch (keyItem.action) {
|
||||
case 'command':
|
||||
_handleCommand(controller, keyItem.value);
|
||||
@@ -160,6 +167,11 @@ class _VirtualKeyboardWidgetState extends State<VirtualKeyboardWidget> {
|
||||
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);
|
||||
@@ -181,35 +193,37 @@ class _VirtualKeyboardWidgetState extends State<VirtualKeyboardWidget> {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: bgColor,
|
||||
border: Border(
|
||||
top: BorderSide(color: borderColor, width: 0.8),
|
||||
return CodeEditorTapRegion(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: bgColor,
|
||||
border: Border(
|
||||
top: BorderSide(color: borderColor, width: 0.8),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
top: false,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
height: 84, // 两行按键的高度
|
||||
child: PageView.builder(
|
||||
controller: _pageController,
|
||||
itemCount: pages.length,
|
||||
onPageChanged: (idx) {
|
||||
setState(() {
|
||||
_currentPage = idx;
|
||||
});
|
||||
},
|
||||
itemBuilder: (context, pageIndex) {
|
||||
return _buildPage(pages[pageIndex], isDark);
|
||||
},
|
||||
child: SafeArea(
|
||||
top: false,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
height: 84, // 两行按键的高度
|
||||
child: PageView.builder(
|
||||
controller: _pageController,
|
||||
itemCount: pages.length,
|
||||
onPageChanged: (idx) {
|
||||
setState(() {
|
||||
_currentPage = idx;
|
||||
});
|
||||
},
|
||||
itemBuilder: (context, pageIndex) {
|
||||
return _buildPage(pages[pageIndex], isDark);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
if (pages.length > 1) _buildPageIndicator(pages.length, theme),
|
||||
],
|
||||
if (pages.length > 1) _buildPageIndicator(pages.length, theme),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user