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.
 
 
 

91 lines
3.1 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_manager.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';
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 const String contentSearchTabType = "content_search";
static const String contentSearchTabTitle = "内容搜索";
static const IconData contentSearchTabIcon = Icons.search;
static const String contentSearchDefaultContent = "";
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<TabManager>(context, listen: false);
// Create new tab with unique ID
final tabId = DateTime.now().millisecondsSinceEpoch.toString();
await tabManager.addTab(
tabId,
title: contentSearchTabTitle,
type: contentSearchTabType,
icon: contentSearchTabIcon,
content: contentSearchDefaultContent,
);
}
static Future<void> _openTemplateParser(BuildContext context) async {
final tabManager = Provider.of<TabManager>(context, listen: false);
// 使用 firstWhereOrNull 查找选项卡
final existingTab = tabManager.tabs.firstWhereOrNull(
(tab) => tab.type == templateParserTabType,
);
if (existingTab != null) {
// 如果存在,激活该选项卡
tabManager.setActiveTab(existingTab.id);
} else {
final tabId = DateTime.now().millisecondsSinceEpoch.toString();
await tabManager.addTab(
tabId,
title: templateParserTabTitle,
type: templateParserTabType,
icon: templateParserTabIcon,
content: templateParserDefaultContent,
);
}
}
static void _exitApplication() {
exit(0);
}
}