|
|
|
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 'package:collection/collection.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.uftFile: _openUftFile,
|
|
|
|
MenuConstants.demo: _demo,
|
|
|
|
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> _openUftFile(BuildContext context) async {
|
|
|
|
await _openOrActivateTab(context, "UFT文件", RouterKey.uftFile, Icons.drive_file_move);
|
|
|
|
}
|
|
|
|
|
|
|
|
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> _demo(BuildContext context) async {
|
|
|
|
await _openOrActivateTab(context, "Demo", RouterKey.demo, Icons.code);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> _openOrActivateTab(
|
|
|
|
BuildContext context,
|
|
|
|
String title,
|
|
|
|
String type,
|
|
|
|
IconData icon,
|
|
|
|
) async {
|
|
|
|
try {
|
|
|
|
final tabManager = Provider.of<TabItemsController>(context, listen: false);
|
|
|
|
final existingTab = tabManager.tabs.firstWhereOrNull((tab) => tab.type == type);
|
|
|
|
|
|
|
|
if (existingTab != null) {
|
|
|
|
tabManager.setActiveTab(existingTab.id);
|
|
|
|
} else {
|
|
|
|
final tabId = DateTime.now().millisecondsSinceEpoch.toString();
|
|
|
|
await tabManager.addTab(tabId, title: title, type: type, icon: icon, content: "");
|
|
|
|
}
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|