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.
81 lines
2.5 KiB
81 lines
2.5 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 'package:collection/collection.dart'; |
|
import 'dart:io'; |
|
|
|
import 'package:win_text_editor/modules/module_router.dart'; |
|
|
|
class MenuActions { |
|
static Future<void> handleMenuAction(String value, BuildContext context) async { |
|
switch (value) { |
|
case MenuConstants.openFolder: |
|
await _openFolder(context); |
|
break; |
|
case MenuConstants.contentSearch: // 新增内容搜索菜单项 |
|
await _openContentSearch(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> _openContentSearch(BuildContext context) async { |
|
final tabManager = Provider.of<TabItemsController>(context, listen: false); |
|
|
|
// Create new tab with unique ID |
|
final tabId = DateTime.now().millisecondsSinceEpoch.toString(); |
|
|
|
await tabManager.addTab( |
|
tabId, |
|
title: "内容搜索", |
|
type: RouterKey.contentSearch, |
|
icon: Icons.search, |
|
content: "", |
|
); |
|
} |
|
|
|
static Future<void> _openTemplateParser(BuildContext context) async { |
|
final tabManager = Provider.of<TabItemsController>(context, listen: false); |
|
|
|
// 使用 firstWhereOrNull 查找选项卡 |
|
final existingTab = tabManager.tabs.firstWhereOrNull( |
|
(tab) => tab.type == RouterKey.templateParser, |
|
); |
|
|
|
if (existingTab != null) { |
|
// 如果存在,激活该选项卡 |
|
tabManager.setActiveTab(existingTab.id); |
|
} else { |
|
final tabId = DateTime.now().millisecondsSinceEpoch.toString(); |
|
await tabManager.addTab( |
|
tabId, |
|
title: "模板解析", |
|
type: RouterKey.templateParser, |
|
icon: Icons.auto_awesome_mosaic, |
|
content: "", |
|
); |
|
} |
|
} |
|
|
|
static void _exitApplication() { |
|
exit(0); |
|
} |
|
}
|
|
|