1- use crate :: plugins:: PluginConfig ;
21// 全局配置管理器
32use crate :: plugin:: PluginManagerState ;
3+ use crate :: plugins:: PluginConfig ;
44use log:: { info, warn} ;
55use serde:: { Deserialize , Serialize } ;
66use std:: fs;
@@ -72,11 +72,8 @@ impl ConfigManager {
7272 Ok ( mut config) => {
7373 println ! ( "读取配置 -> 成功加载配置文件: {:?}" , config_path) ;
7474
75- // 检查 plugins 是否为 null,如果是则加载默认配置
76- if config. plugins . is_none ( ) {
77- println ! ( "读取配置 -> plugins 为 null,加载默认插件配置" ) ;
78- config. plugins = Self :: get_default_plugins_config ( app_handle) ;
79- }
75+ // 合并插件配置(现有配置 + 默认配置中缺失的插件)
76+ config. plugins = Self :: merge_plugins_config ( config. plugins , app_handle) ;
8077
8178 Ok ( config)
8279 }
@@ -96,6 +93,59 @@ impl ConfigManager {
9693 }
9794 }
9895
96+ // 合并插件配置(现有配置 + 默认配置中缺失的插件)
97+ fn merge_plugins_config (
98+ existing_plugins : Option < Vec < PluginConfig > > ,
99+ app_handle : Option < & AppHandle > ,
100+ ) -> Option < Vec < PluginConfig > > {
101+ // 获取所有默认插件配置
102+ let default_plugins = Self :: get_default_plugins_config ( app_handle) . unwrap_or_default ( ) ;
103+
104+ if let Some ( existing) = existing_plugins {
105+ let mut merged_plugins = Vec :: new ( ) ;
106+
107+ // 遍历所有默认插件
108+ for default_plugin in & default_plugins {
109+ // 检查现有配置中是否已存在该插件
110+ if let Some ( existing_plugin) = existing
111+ . iter ( )
112+ . find ( |p| p. language == default_plugin. language )
113+ {
114+ // 如果存在,使用现有配置
115+ merged_plugins. push ( existing_plugin. clone ( ) ) ;
116+ println ! ( "读取配置 -> 使用现有插件配置: {}" , existing_plugin. language) ;
117+ } else {
118+ // 如果不存在,使用默认配置
119+ merged_plugins. push ( default_plugin. clone ( ) ) ;
120+ println ! (
121+ "读取配置 -> 添加缺失的默认插件配置: {}" ,
122+ default_plugin. language
123+ ) ;
124+ }
125+ }
126+
127+ // 添加现有配置中有但默认配置中没有的插件(用户自定义的插件)
128+ for existing_plugin in existing {
129+ if !default_plugins
130+ . iter ( )
131+ . any ( |p| p. language == existing_plugin. language )
132+ {
133+ merged_plugins. push ( existing_plugin. clone ( ) ) ;
134+ println ! (
135+ "读取配置 -> 保留用户自定义插件配置: {}" ,
136+ existing_plugin. language
137+ ) ;
138+ }
139+ }
140+
141+ Some ( merged_plugins)
142+ } else {
143+ // 如果现有配置中没有 plugins,直接使用默认配置
144+ println ! ( "读取配置 -> plugins 为 null,使用默认插件配置" ) ;
145+ Some ( default_plugins)
146+ }
147+ }
148+
99149 fn get_default_plugins_config ( app_handle : Option < & AppHandle > ) -> Option < Vec < PluginConfig > > {
100150 if let Some ( handle) = app_handle {
101151 // 从 Tauri 状态中获取 PluginManager
0 commit comments