From 4b1298b92d716832c8ecdcd09aac0c020aaf0a42 Mon Sep 17 00:00:00 2001 From: hejl Date: Mon, 19 May 2025 18:44:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E8=B7=AF=E7=94=B1=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E5=88=B0=E4=B8=80=E8=B5=B7=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/tab_items_controller.dart | 13 ++--- .../lib/framework/widgets/tab_view.dart | 13 +---- win_text_editor/lib/menus/menu_actions.dart | 32 +++++------- .../lib/modules/module_router.dart | 49 +++++++++++++++++++ 4 files changed, 65 insertions(+), 42 deletions(-) create mode 100644 win_text_editor/lib/modules/module_router.dart diff --git a/win_text_editor/lib/framework/controllers/tab_items_controller.dart b/win_text_editor/lib/framework/controllers/tab_items_controller.dart index 1b5ec0b..a02f493 100644 --- a/win_text_editor/lib/framework/controllers/tab_items_controller.dart +++ b/win_text_editor/lib/framework/controllers/tab_items_controller.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:win_text_editor/framework/models/tab_model.dart'; -import 'package:win_text_editor/modules/content_search/controllers/content_search_controller.dart'; -import 'package:win_text_editor/modules/template_parser/controllers/template_parser_controller.dart'; +import 'package:win_text_editor/modules/module_router.dart'; import 'package:win_text_editor/shared/base/base_content_controller.dart'; import 'package:win_text_editor/framework/controllers/logger.dart'; @@ -54,14 +53,7 @@ class TabItemsController with ChangeNotifier { } BaseContentController? createContentController(AppTab tab) { - switch (tab.type) { - case 'content_search': - return ContentSearchController(); - case 'template_parser': - return TemplateParserController(); - default: - return null; - } + return ModuleRouter.createControllerForTab(tab); } BaseContentController? get activeContentController { @@ -74,6 +66,7 @@ class TabItemsController with ChangeNotifier { Logger().error("没有活动的内容控制器,activeTabId:$_activeTabId", source: 'TabItemsController'); return null; } + // ignore: unnecessary_type_check if (controller is! BaseContentController) { Logger().error("活动内容控制器不是BaseContentController类型", source: 'TabItemsController'); return null; diff --git a/win_text_editor/lib/framework/widgets/tab_view.dart b/win_text_editor/lib/framework/widgets/tab_view.dart index 4ed0a5b..7f9f95f 100644 --- a/win_text_editor/lib/framework/widgets/tab_view.dart +++ b/win_text_editor/lib/framework/widgets/tab_view.dart @@ -1,10 +1,8 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -import 'package:win_text_editor/modules/template_parser/widgets/template_parser_view.dart'; -import 'package:win_text_editor/shared/components/text_editor.dart'; +import 'package:win_text_editor/modules/module_router.dart'; import 'package:win_text_editor/framework/controllers/tab_items_controller.dart'; import 'package:win_text_editor/framework/models/tab_model.dart'; -import 'package:win_text_editor/modules/content_search/widgets/content_search_view.dart'; class TabView extends StatefulWidget { final List tabs; @@ -57,14 +55,7 @@ class _TabViewState extends State { } Widget _buildTabItem(AppTab tab, ChangeNotifier? controller) { - switch (tab.type) { - case 'content_search': - return ContentSearchView(tabId: tab.id); - case 'template_parser': - return TemplateParserView(tabId: tab.id); - default: - return TextEditor(tabId: tab.id, initialContent: tab.content); - } + return ModuleRouter.buildWidgetForTab(tab, controller); } } diff --git a/win_text_editor/lib/menus/menu_actions.dart b/win_text_editor/lib/menus/menu_actions.dart index f0eda6e..f862473 100644 --- a/win_text_editor/lib/menus/menu_actions.dart +++ b/win_text_editor/lib/menus/menu_actions.dart @@ -7,19 +7,9 @@ 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 = ""; +import 'package:win_text_editor/modules/module_router.dart'; +class MenuActions { static Future handleMenuAction(String value, BuildContext context) async { switch (value) { case MenuConstants.openFolder: @@ -55,10 +45,10 @@ class MenuActions { await tabManager.addTab( tabId, - title: contentSearchTabTitle, - type: contentSearchTabType, - icon: contentSearchTabIcon, - content: contentSearchDefaultContent, + title: "内容搜索", + type: RouterKey.contentSearch, + icon: Icons.search, + content: "", ); } @@ -67,7 +57,7 @@ class MenuActions { // 使用 firstWhereOrNull 查找选项卡 final existingTab = tabManager.tabs.firstWhereOrNull( - (tab) => tab.type == templateParserTabType, + (tab) => tab.type == RouterKey.templateParser, ); if (existingTab != null) { @@ -77,10 +67,10 @@ class MenuActions { final tabId = DateTime.now().millisecondsSinceEpoch.toString(); await tabManager.addTab( tabId, - title: templateParserTabTitle, - type: templateParserTabType, - icon: templateParserTabIcon, - content: templateParserDefaultContent, + title: "模板解析", + type: RouterKey.templateParser, + icon: Icons.auto_awesome_mosaic, + content: "", ); } } diff --git a/win_text_editor/lib/modules/module_router.dart b/win_text_editor/lib/modules/module_router.dart new file mode 100644 index 0000000..004deec --- /dev/null +++ b/win_text_editor/lib/modules/module_router.dart @@ -0,0 +1,49 @@ +// modules/tab_content_registry.dart +import 'package:flutter/material.dart'; +import 'package:win_text_editor/framework/models/tab_model.dart'; +import 'package:win_text_editor/shared/base/base_content_controller.dart'; +import 'package:win_text_editor/modules/content_search/controllers/content_search_controller.dart'; +import 'package:win_text_editor/modules/template_parser/controllers/template_parser_controller.dart'; +import 'package:win_text_editor/modules/content_search/widgets/content_search_view.dart'; +import 'package:win_text_editor/modules/template_parser/widgets/template_parser_view.dart'; + +class RouterKey { + static const String contentSearch = 'content_search'; + static const String templateParser = 'template_parser'; + static const String textEditor = 'text_editor'; +} + +class ModuleRouter { + //映射控制器 + static final Map _controllerCreators = { + RouterKey.contentSearch: (tab) => ContentSearchController(), + RouterKey.templateParser: (tab) => TemplateParserController(), + }; + + // 映射UI组件 + static final Map _widgetBuilders = { + RouterKey.contentSearch: (tab, controller) => ContentSearchView(tabId: tab.id), + RouterKey.templateParser: (tab, controller) => TemplateParserView(tabId: tab.id), + }; + + static BaseContentController? createControllerForTab(AppTab tab) { + final creator = _controllerCreators[tab.type]; + return creator != null ? creator(tab) : null; + } + + static Widget buildWidgetForTab(AppTab tab, ChangeNotifier? controller) { + final builder = _widgetBuilders[tab.type] ?? _widgetBuilders['default']; + return builder!(tab, controller); + } + + static void registerControllerCreator(String type, ContentControllerCreator creator) { + _controllerCreators[type] = creator; + } + + static void registerWidgetBuilder(String type, ContentWidgetBuilder builder) { + _widgetBuilders[type] = builder; + } +} + +typedef ContentControllerCreator = BaseContentController Function(AppTab tab); +typedef ContentWidgetBuilder = Widget Function(AppTab tab, ChangeNotifier? controller);