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.
131 lines
4.8 KiB
131 lines
4.8 KiB
import 'package:flutter/material.dart'; |
|
import 'package:file_picker/file_picker.dart'; |
|
import 'package:provider/provider.dart'; |
|
import 'package:win_text_editor/framework/controllers/tab_items_controller.dart'; |
|
import 'package:win_text_editor/menus/menu_constants.dart'; |
|
import 'package:win_text_editor/framework/controllers/file_provider.dart'; |
|
import 'dart:io'; |
|
|
|
import 'package:win_text_editor/modules/module_router.dart'; |
|
|
|
class MenuActions { |
|
static final Map<String, Function(BuildContext)> _actionHandlers = { |
|
MenuConstants.openFolder: _openFolder, |
|
MenuConstants.contentSearch: _openContentSearch, |
|
MenuConstants.templateParser: _openTemplateParser, |
|
MenuConstants.dataFormat: _dataFormat, |
|
MenuConstants.dataCompare: _dataCompare, |
|
MenuConstants.dataExtract: _dataExtract, |
|
MenuConstants.xmlSearch: _xmlSearch, |
|
MenuConstants.memoryTable: _memoryTable, |
|
MenuConstants.uftComponent: _uftComponent, |
|
MenuConstants.callFunction: _callFunction, |
|
MenuConstants.demo: _demo, |
|
MenuConstants.outline: _outline, |
|
MenuConstants.codeCreater: _codeCreater, |
|
MenuConstants.exit: _exitApplication, |
|
}; |
|
|
|
static Future<void> handleMenuAction(String value, BuildContext context) async { |
|
final handler = _actionHandlers[value]; |
|
if (handler != null) { |
|
await handler(context); |
|
} |
|
} |
|
|
|
static Future<void> _openFolder(BuildContext context) async { |
|
try { |
|
final fileProvider = Provider.of<FileProvider>(context, listen: false); |
|
final String? selectedDirectory = await FilePicker.platform.getDirectoryPath(); |
|
if (selectedDirectory != null) { |
|
await fileProvider.loadDirectory(selectedDirectory); |
|
} |
|
} catch (e) { |
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('打开文件夹失败: $e'))); |
|
} |
|
} |
|
|
|
static Future<void> _openContentSearch(BuildContext context) async { |
|
await _openOrActivateTab(context, "内容搜索", RouterKey.contentSearch, Icons.search); |
|
} |
|
|
|
static Future<void> _openTemplateParser(BuildContext context) async { |
|
await _openOrActivateTab(context, "XML解析", RouterKey.templateParser, Icons.auto_awesome_mosaic); |
|
} |
|
|
|
static Future<void> _memoryTable(BuildContext context) async { |
|
await _openOrActivateTab(context, "内存表", RouterKey.memoryTable, Icons.drive_file_move); |
|
} |
|
|
|
static Future<void> _uftComponent(BuildContext context) async { |
|
await _openOrActivateTab(context, "标准组件", RouterKey.uftComponent, Icons.extension); |
|
} |
|
|
|
static Future<void> _callFunction(BuildContext context) async { |
|
await _openOrActivateTab(context, "功能号调用", RouterKey.callFunction, Icons.extension); |
|
} |
|
|
|
static Future<void> _dataFormat(BuildContext context) async { |
|
await _openOrActivateTab(context, "数据格式化", RouterKey.dataFormat, Icons.date_range); |
|
} |
|
|
|
static Future<void> _dataCompare(BuildContext context) async { |
|
await _openOrActivateTab(context, "数据对比", RouterKey.dataCompare, Icons.compare); |
|
} |
|
|
|
static Future<void> _dataExtract(BuildContext context) async { |
|
await _openOrActivateTab(context, "XML数据提取", RouterKey.dataExtract, Icons.outbox); |
|
} |
|
|
|
static Future<void> _xmlSearch(BuildContext context) async { |
|
await _openOrActivateTab(context, "XML搜索", RouterKey.xmlSearch, Icons.find_in_page); |
|
} |
|
|
|
static Future<void> _demo(BuildContext context) async { |
|
await _openOrActivateTab(context, "Demo", RouterKey.demo, Icons.code); |
|
} |
|
|
|
static Future<void> _codeCreater(BuildContext context) async { |
|
await _openOrActivateTab(context, "代码生成器", RouterKey.codeCreater, Icons.code); |
|
} |
|
|
|
static Future<void> _outline(BuildContext context) async { |
|
await _openOrActivateTab(context, "大纲", RouterKey.outline, Icons.outlined_flag_rounded); |
|
} |
|
|
|
static Future<void> _openOrActivateTab( |
|
BuildContext context, |
|
String title, |
|
String type, |
|
IconData icon, |
|
) async { |
|
try { |
|
final tabManager = Provider.of<TabItemsController>(context, listen: false); |
|
await tabManager.openOrActivateTab(title, type, icon); |
|
} catch (e) { |
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('打开标签页失败: $e'))); |
|
} |
|
} |
|
|
|
static Future<void> _exitApplication(BuildContext context) async { |
|
final shouldExit = await showDialog<bool>( |
|
context: context, |
|
builder: |
|
(context) => AlertDialog( |
|
title: const Text('退出应用'), |
|
content: const Text('确定要退出吗?未保存的内容可能会丢失。'), |
|
actions: [ |
|
TextButton( |
|
onPressed: () => Navigator.of(context).pop(false), |
|
child: const Text('取消'), |
|
), |
|
TextButton(onPressed: () => Navigator.of(context).pop(true), child: const Text('退出')), |
|
], |
|
), |
|
); |
|
|
|
if (shouldExit ?? false) { |
|
exit(0); |
|
} |
|
} |
|
}
|
|
|