Skip to content

Commit a663ad0

Browse files
committed
feat: 支持动态刷新配置
1 parent 68b1138 commit a663ad0

8 files changed

Lines changed: 98 additions & 36 deletions

File tree

src-tauri/src/config.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,24 @@ pub fn get_app_config_internal() -> Result<AppConfig, String> {
319319
}
320320

321321
#[command]
322-
pub async fn update_app_config(config: AppConfig) -> Result<(), String> {
322+
pub async fn update_app_config(
323+
config: AppConfig,
324+
app_handle: tauri::AppHandle,
325+
) -> Result<(), String> {
326+
use tauri::Emitter;
327+
323328
let mut guard = get_config_manager()?;
324329
if let Some(config_manager) = guard.as_mut() {
325330
config_manager.config = config;
326-
config_manager.save_config()
331+
let result = config_manager.save_config();
332+
333+
// 保存成功后发送配置更新事件
334+
if result.is_ok() {
335+
app_handle.emit("config-updated", ()).ok();
336+
info!("配置已更新,已发送 config-updated 事件");
337+
}
338+
339+
result
327340
} else {
328341
Err("配置管理器未初始化".to_string())
329342
}

src-tauri/src/env_commands.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,9 @@ pub async fn switch_environment_version(
4646
) -> Result<(), String> {
4747
info!("切换 {} 到版本 {}", language, version);
4848
let manager = env_manager.lock().await;
49-
let result = manager.switch_version(&language, &version).await;
50-
51-
if result.is_ok() {
52-
// 发送配置更新事件通知前端刷新配置
53-
app_handle.emit("config-updated", ()).ok();
54-
info!("已发送配置更新事件");
55-
}
56-
57-
result
49+
manager
50+
.switch_version(&language, &version, app_handle)
51+
.await
5852
}
5953

6054
#[tauri::command]

src-tauri/src/env_manager.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use async_trait::async_trait;
2-
use log::{error, info};
2+
use log::{error, info, warn};
33
use serde::{Deserialize, Serialize};
44
use std::collections::HashMap;
55
use std::path::PathBuf;
@@ -24,6 +24,7 @@ pub struct EnvironmentInfo {
2424
pub current_version: Option<String>,
2525
pub installed_versions: Vec<EnvironmentVersion>,
2626
pub available_versions: Vec<EnvironmentVersion>,
27+
pub error: Option<String>, // 错误信息(如获取可用版本失败)
2728
}
2829

2930
// 下载进度事件
@@ -67,7 +68,7 @@ pub trait EnvironmentProvider: Send + Sync {
6768
) -> Result<String, String>;
6869

6970
// 切换到指定版本
70-
async fn switch_version(&self, version: &str) -> Result<(), String>;
71+
async fn switch_version(&self, version: &str, app_handle: AppHandle) -> Result<(), String>;
7172

7273
// 获取当前激活的版本
7374
async fn get_current_version(&self) -> Result<Option<String>, String>;
@@ -105,16 +106,22 @@ impl EnvironmentManager {
105106

106107
let current_version = provider.get_current_version().await.ok().flatten();
107108
let installed_versions = provider.get_installed_versions().await.unwrap_or_default();
108-
let available_versions = provider
109-
.fetch_available_versions()
110-
.await
111-
.unwrap_or_default();
109+
110+
// 获取可用版本,如果失败也要返回已安装版本,并在错误信息中说明
111+
let (available_versions, error) = match provider.fetch_available_versions().await {
112+
Ok(versions) => (versions, None),
113+
Err(e) => {
114+
warn!("获取可用版本失败: {}", e);
115+
(vec![], Some(format!("获取可用版本失败: {}", e)))
116+
}
117+
};
112118

113119
Ok(EnvironmentInfo {
114120
language: language.to_string(),
115121
current_version,
116122
installed_versions,
117123
available_versions,
124+
error,
118125
})
119126
}
120127

@@ -133,14 +140,19 @@ impl EnvironmentManager {
133140
provider.download_and_install(version, app_handle).await
134141
}
135142

136-
pub async fn switch_version(&self, language: &str, version: &str) -> Result<(), String> {
143+
pub async fn switch_version(
144+
&self,
145+
language: &str,
146+
version: &str,
147+
app_handle: AppHandle,
148+
) -> Result<(), String> {
137149
let provider = self
138150
.providers
139151
.get(language)
140152
.ok_or_else(|| format!("暂未支持 {} 语言,请前往 github 提供 issues", language))?;
141153

142154
info!("切换 {} 到版本 {}", language, version);
143-
provider.switch_version(version).await
155+
provider.switch_version(version, app_handle).await
144156
}
145157

146158
pub fn get_supported_languages(&self) -> Vec<String> {

src-tauri/src/env_providers/clojure.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,12 @@ impl ClojureEnvironmentProvider {
594594
Ok(())
595595
}
596596

597-
async fn update_plugin_config(&self, version: &str, install_path: &str) -> Result<(), String> {
597+
async fn update_plugin_config(
598+
&self,
599+
version: &str,
600+
install_path: &str,
601+
app_handle: &AppHandle,
602+
) -> Result<(), String> {
598603
use crate::config::{get_app_config_internal, update_app_config};
599604

600605
info!(
@@ -616,7 +621,7 @@ impl ClojureEnvironmentProvider {
616621
}
617622
}
618623

619-
update_app_config(config)
624+
update_app_config(config, app_handle.clone())
620625
.await
621626
.map_err(|e| format!("保存配置失败: {}", e))?;
622627

@@ -794,7 +799,7 @@ impl EnvironmentProvider for ClojureEnvironmentProvider {
794799
// 清理临时解压目录
795800
std::fs::remove_dir_all(&temp_extract_dir).ok();
796801

797-
self.update_plugin_config(version, &install_path.to_string_lossy())
802+
self.update_plugin_config(version, &install_path.to_string_lossy(), &app_handle)
798803
.await?;
799804

800805
emit_download_progress(
@@ -810,7 +815,7 @@ impl EnvironmentProvider for ClojureEnvironmentProvider {
810815
Ok(install_path.to_string_lossy().to_string())
811816
}
812817

813-
async fn switch_version(&self, version: &str) -> Result<(), String> {
818+
async fn switch_version(&self, version: &str, app_handle: AppHandle) -> Result<(), String> {
814819
info!("切换 Clojure 版本到 {}", version);
815820

816821
if !self.is_version_installed(version) {
@@ -819,7 +824,7 @@ impl EnvironmentProvider for ClojureEnvironmentProvider {
819824

820825
let install_path = self.get_version_install_path(version);
821826

822-
self.update_plugin_config(version, &install_path.to_string_lossy())
827+
self.update_plugin_config(version, &install_path.to_string_lossy(), &app_handle)
823828
.await?;
824829

825830
info!("成功切换到 Clojure {}", version);

src-tauri/src/env_providers/scala.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,12 @@ impl ScalaEnvironmentProvider {
414414
}
415415

416416
// 更新配置以使用新版本
417-
async fn update_plugin_config(&self, version: &str, install_path: &str) -> Result<(), String> {
417+
async fn update_plugin_config(
418+
&self,
419+
version: &str,
420+
install_path: &str,
421+
app_handle: &AppHandle,
422+
) -> Result<(), String> {
418423
use crate::config::{get_app_config_internal, update_app_config};
419424

420425
info!(
@@ -444,7 +449,7 @@ impl ScalaEnvironmentProvider {
444449
}
445450
}
446451

447-
update_app_config(config)
452+
update_app_config(config, app_handle.clone())
448453
.await
449454
.map_err(|e| format!("保存配置失败: {}", e))?;
450455

@@ -622,7 +627,7 @@ impl EnvironmentProvider for ScalaEnvironmentProvider {
622627
}
623628

624629
// 更新插件配置
625-
self.update_plugin_config(version, &actual_install_path.to_string_lossy())
630+
self.update_plugin_config(version, &actual_install_path.to_string_lossy(), &app_handle)
626631
.await?;
627632

628633
emit_download_progress(
@@ -638,7 +643,7 @@ impl EnvironmentProvider for ScalaEnvironmentProvider {
638643
Ok(actual_install_path.to_string_lossy().to_string())
639644
}
640645

641-
async fn switch_version(&self, version: &str) -> Result<(), String> {
646+
async fn switch_version(&self, version: &str, app_handle: AppHandle) -> Result<(), String> {
642647
info!("切换 Scala 版本到 {}", version);
643648

644649
if !self.is_version_installed(version) {
@@ -659,7 +664,7 @@ impl EnvironmentProvider for ScalaEnvironmentProvider {
659664
}
660665
}
661666

662-
self.update_plugin_config(version, &actual_install_path.to_string_lossy())
667+
self.update_plugin_config(version, &actual_install_path.to_string_lossy(), &app_handle)
663668
.await?;
664669

665670
info!("成功切换到 Scala {}", version);

src/components/setting/EnvironmentManager.vue

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,16 @@
6969
</div>
7070
</div>
7171

72+
<!-- 获取可用版本错误信息 -->
73+
<div v-if="environmentInfo?.error" class="mt-6 p-3 bg-yellow-50 dark:bg-yellow-900/20 rounded-lg">
74+
<div class="flex items-center space-x-2 text-yellow-600 dark:text-yellow-400">
75+
<AlertCircle class="w-5 h-5"/>
76+
<span class="text-sm">{{ environmentInfo.error }}</span>
77+
</div>
78+
</div>
79+
7280
<!-- 可用版本 -->
73-
<div class="space-y-2 mt-4">
81+
<div v-else class="space-y-2 mt-4">
7482
<h4 class="text-sm font-semibold text-gray-700 dark:text-gray-300">可下载版本</h4>
7583

7684
<!-- 下载进度 -->
@@ -79,7 +87,8 @@
7987
<div class="flex items-center space-x-2">
8088
<svg class="animate-spin h-4 w-4 text-blue-600 dark:text-blue-400" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
8189
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
82-
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
90+
<path class="opacity-75" fill="currentColor"
91+
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
8392
</svg>
8493
<span class="text-sm font-medium text-blue-900 dark:text-blue-100">
8594
{{ downloadStatusText }}
@@ -130,7 +139,7 @@
130139

131140
<script setup lang="ts">
132141
import { computed } from 'vue'
133-
import { Folder, CheckCircle, AlertCircle, Download } from 'lucide-vue-next'
142+
import { AlertCircle, CheckCircle, Download, Folder } from 'lucide-vue-next'
134143
import { useEnvironmentManager } from '../../composables/useEnvironmentManager'
135144
import Label from '../../ui/Label.vue'
136145
import Input from '../../ui/Input.vue'
@@ -158,7 +167,9 @@ const {
158167
159168
// 下载状态文本
160169
const downloadStatusText = computed(() => {
161-
if (!downloadProgress.value) return ''
170+
if (!downloadProgress.value) {
171+
return ''
172+
}
162173
163174
switch (downloadProgress.value.status) {
164175
case 'downloading':
@@ -178,13 +189,17 @@ const downloadStatusText = computed(() => {
178189
179190
// 显示所有可用版本(包括已安装的)
180191
const availableVersionsToShow = computed(() => {
181-
if (!environmentInfo.value) return []
192+
if (!environmentInfo.value) {
193+
return []
194+
}
182195
return environmentInfo.value.available_versions
183196
})
184197
185198
// 格式化文件大小
186199
const formatSize = (bytes: number) => {
187-
if (bytes === 0) return '0 B'
200+
if (bytes === 0) {
201+
return '0 B'
202+
}
188203
const k = 1024
189204
const sizes = ['B', 'KB', 'MB', 'GB']
190205
const i = Math.floor(Math.log(bytes) / Math.log(k))

src/composables/useLanguageManager.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {ref, type Ref} from 'vue'
1+
import {ref, type Ref, onMounted, onUnmounted} from 'vue'
22
import {invoke} from '@tauri-apps/api/core'
3+
import {listen} from '@tauri-apps/api/event'
34
import {EnvInfo, Language, LanguageInfo} from '../types/app.ts'
45

56
export function useLanguageManager(
@@ -194,6 +195,22 @@ export function useLanguageManager(
194195
}
195196
}
196197

198+
// 监听配置更新事件
199+
let unlistenConfigUpdate: (() => void) | null = null
200+
201+
onMounted(async () => {
202+
unlistenConfigUpdate = await listen('config-updated', async () => {
203+
console.log('收到配置更新事件,重新加载环境信息')
204+
await refreshEnvInfo()
205+
})
206+
})
207+
208+
onUnmounted(() => {
209+
if (unlistenConfigUpdate) {
210+
unlistenConfigUpdate()
211+
}
212+
})
213+
197214
return {
198215
currentLanguage,
199216
supportedLanguages,

src/types/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export interface EnvironmentInfo
7373
current_version: string | null
7474
installed_versions: EnvironmentVersion[]
7575
available_versions: EnvironmentVersion[]
76+
error?: string | null // 错误信息(如获取可用版本失败)
7677
}
7778

7879
export interface DownloadProgress

0 commit comments

Comments
 (0)