You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.2 KiB
66 lines
2.2 KiB
import 'package:flutter/material.dart'; |
|
import 'package:file_picker/file_picker.dart'; |
|
import 'package:provider/provider.dart'; |
|
import 'package:win_text_editor/app/menus/menu_constants.dart'; |
|
import 'package:win_text_editor/app/providers/file_provider.dart'; |
|
import 'package:win_text_editor/app/providers/editor_provider.dart'; |
|
import 'package:collection/collection.dart'; |
|
import 'dart:io'; |
|
|
|
class MenuActions { |
|
// 定义模板解析选项卡的常量 |
|
static const String templateParserTabType = "template_parser"; |
|
static const String templateParserTabTitle = "模板解析"; |
|
static const IconData templateParserTabIcon = Icons.auto_awesome_mosaic; |
|
static const String templateParserDefaultContent = ""; |
|
|
|
static Future<void> handleMenuAction(String value, BuildContext context) async { |
|
switch (value) { |
|
case MenuConstants.openFolder: |
|
await _openFolder(context); |
|
break; |
|
case MenuConstants.templateParser: |
|
await _openTemplateParser(context); |
|
break; |
|
case MenuConstants.exit: |
|
_exitApplication(); |
|
break; |
|
// 其他菜单项可以在这里添加处理逻辑 |
|
} |
|
} |
|
|
|
static Future<void> _openFolder(BuildContext context) async { |
|
final fileProvider = Provider.of<FileProvider>(context, listen: false); |
|
final String? selectedDirectory = await FilePicker.platform.getDirectoryPath(); |
|
|
|
if (selectedDirectory != null) { |
|
await fileProvider.loadDirectory(selectedDirectory); |
|
} |
|
} |
|
|
|
static Future<void> _openTemplateParser(BuildContext context) async { |
|
final editorProvider = Provider.of<EditorProvider>(context, listen: false); |
|
|
|
// 使用 firstWhereOrNull 查找选项卡 |
|
final existingTab = editorProvider.tabs.firstWhereOrNull( |
|
(tab) => tab.type == templateParserTabType, |
|
); |
|
|
|
if (existingTab != null) { |
|
// 如果存在,激活该选项卡 |
|
editorProvider.setActiveTab(existingTab.id); |
|
} else { |
|
// 如果不存在,创建新选项卡 |
|
await editorProvider.addTab( |
|
title: templateParserTabTitle, |
|
type: templateParserTabType, |
|
icon: templateParserTabIcon, |
|
content: templateParserDefaultContent, |
|
); |
|
} |
|
} |
|
|
|
static void _exitApplication() { |
|
exit(0); |
|
} |
|
}
|
|
|