1
This commit is contained in:
1 parent
54c3083c24
commit
87e03ce0d8
44 files changed
+3965
-307
No files matched your search
@@ -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())),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user