diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e61a4fa..c3f178b 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -192,7 +192,7 @@ async fn get_info( #[tauri::command] async fn get_supported_languages( plugin_manager: State<'_, PluginManagerState>, -) -> Result, String> { +) -> Result, String> { let manager = plugin_manager.lock().await; Ok(manager.get_supported_languages()) } diff --git a/src-tauri/src/plugins/manager.rs b/src-tauri/src/plugins/manager.rs index 59c03ea..394f3b6 100644 --- a/src-tauri/src/plugins/manager.rs +++ b/src-tauri/src/plugins/manager.rs @@ -19,8 +19,16 @@ impl PluginManager { self.plugins.get(language).map(|plugin| plugin.as_ref()) } - pub fn get_supported_languages(&self) -> Vec { - self.plugins.keys().cloned().collect() + pub fn get_supported_languages(&self) -> Vec { + self.plugins + .iter() + .map(|(key, plugin)| { + serde_json::json!({ + "name": plugin.get_language_name(), + "value": key + }) + }) + .collect() } #[allow(dead_code)] diff --git a/src-tauri/src/plugins/python2.rs b/src-tauri/src/plugins/python2.rs index 9e7e8cd..b0d12a1 100644 --- a/src-tauri/src/plugins/python2.rs +++ b/src-tauri/src/plugins/python2.rs @@ -40,7 +40,7 @@ impl LanguagePlugin for Python2Plugin { fn post_execute_hook(&self, result: &mut ExecutionResult) -> Result<(), String> { // Python 特定的后处理 if result.success && result.stdout.is_empty() && result.stderr.is_empty() { - result.stdout = "Code executed successfully (no output)".to_string(); + result.stdout = "代码执行成功 (无输出)".to_string(); } // 清理 Python 特定的错误信息 diff --git a/src-tauri/src/plugins/python3.rs b/src-tauri/src/plugins/python3.rs index f3aac58..06723d4 100644 --- a/src-tauri/src/plugins/python3.rs +++ b/src-tauri/src/plugins/python3.rs @@ -40,7 +40,7 @@ impl LanguagePlugin for Python3Plugin { fn post_execute_hook(&self, result: &mut ExecutionResult) -> Result<(), String> { // Python 特定的后处理 if result.success && result.stdout.is_empty() && result.stderr.is_empty() { - result.stdout = "Code executed successfully (no output)".to_string(); + result.stdout = "代码执行成功 (无输出)".to_string(); } // 清理 Python 特定的错误信息 diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 804f125..e818205 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -14,7 +14,8 @@ { "title": "CodeForge", "width": 1800, - "height": 1200 + "height": 1200, + "additionalBrowserArgs": "--disable-context-menu" } ], "security": { diff --git a/src/App.vue b/src/App.vue index 0841bb2..1897951 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,8 +2,11 @@
@@ -11,15 +14,12 @@
-

Python 代码编辑器

+

{{ getLanguageDisplayName(currentLanguage) }} 代码编辑器

{{ code.length }} 字符, {{ code.split('\n').length }}
- - +
@@ -35,10 +35,7 @@
- - + = { + python: `# Welcome to CodeForge! # Write your Python code here and click Run to execute print("Hello, CodeForge!") @@ -102,14 +107,52 @@ print(f"The result of {x} + {y} = {result}") numbers = [1, 2, 3, 4, 5] squared = [n**2 for n in numbers] print(f"Original: {numbers}") -print(f"Squared: {squared}")`) +print(f"Squared: {squared}")`, + + python2: `# Welcome to CodeForge - Python 2! +# Write your Python 2 code here and click Run to execute + +print "Hello, CodeForge from Python 2!" + +# Example: Simple calculation +x = 10 +y = 20 +result = x + y +print "The result of %d + %d = %d" % (x, y, result) + +# Example: List operations +numbers = [1, 2, 3, 4, 5] +squared = [n**2 for n in numbers] +print "Original:", numbers +print "Squared:", squared`, + + python3: `# Welcome to CodeForge - Python 3! +# Write your Python 3 code here and click Run to execute + +print("Hello, CodeForge from Python 3!") + +# Example: Simple calculation +x = 10 +y = 20 +result = x + y +print(f"The result of {x} + {y} = {result}") + +# Example: List operations +numbers = [1, 2, 3, 4, 5] +squared = [n**2 for n in numbers] +print(f"Original: {numbers}") +print(f"Squared: {squared}")` +} +const code = ref('') +const currentLanguage = ref('python') const output = ref('') const isRunning = ref(false) const isSuccess = ref(false) const lastExecutionTime = ref(0) const activeTab = ref('output') const showSettings = ref(false) +const supportedLanguages = ref([]) const envInfo = ref({ installed: false, @@ -126,15 +169,17 @@ const toast = ref({ const showToast = (message: string, type: 'success' | 'error' | 'info' = 'success') => { toast.value = { show: true, message, type } - setTimeout(() => { - toast.value.show = false - }, 3000) +} + +const getLanguageDisplayName = (languageValue: string) => { + const language = supportedLanguages.value.find(lang => lang.value === languageValue) + return language ? language.name : languageValue } const refreshEnvInfo = async () => { try { const info: LanguageInfo = await invoke('get_info', { - language: 'python2' + language: currentLanguage.value }) envInfo.value = { @@ -150,11 +195,48 @@ const refreshEnvInfo = async () => { installed: false, version: 'Error', path: 'Error', - language: 'python2' + language: currentLanguage.value } } } +const getSupportedLanguages = async () => { + try { + const languages = await invoke('get_supported_languages') + supportedLanguages.value = languages.map((language) => ({ + name: language.name, + value: language.value + })) + + // 设置默认语言 + if (supportedLanguages.value.length > 0 && !currentLanguage.value) { + currentLanguage.value = supportedLanguages.value[0].value + } + } + catch (error) { + console.error('Error getting supported languages:', error) + supportedLanguages.value = [] + } +} + +const handleLanguageChange = async (newLanguage: string) => { + currentLanguage.value = newLanguage + + // 更新代码模板 + code.value = codeTemplates[newLanguage] || `# ${ getLanguageDisplayName(newLanguage) } Code +# Write your code here... + +print("Hello from ${ getLanguageDisplayName(newLanguage) }!")` + + // 清空输出 + output.value = '' + + // 刷新环境信息 + await refreshEnvInfo() + + showToast(`已切换到 ${ getLanguageDisplayName(newLanguage) }`, 'info') +} + const runCode = async () => { if (!envInfo.value.installed) { showToast(`${ envInfo.value.language } 环境未安装`, 'error') @@ -168,7 +250,7 @@ const runCode = async () => { const result: ExecutionResult = await invoke('execute_code', { request: { code: code.value, - language: 'python2' + language: currentLanguage.value } }) @@ -201,7 +283,20 @@ const clearOutput = () => { showToast('输出已清空', 'info') } +window.addEventListener("contextmenu", (e) => e.preventDefault(), false); + onMounted(async () => { + await getSupportedLanguages() + + // 设置初始代码模板 + if (supportedLanguages.value.length > 0) { + currentLanguage.value = supportedLanguages.value[0].value + code.value = codeTemplates[currentLanguage.value] || codeTemplates.python + } + else { + code.value = codeTemplates.python + } + await refreshEnvInfo() }) \ No newline at end of file diff --git a/src/components/AppHeader.vue b/src/components/AppHeader.vue index 3f68377..bbb8a00 100644 --- a/src/components/AppHeader.vue +++ b/src/components/AppHeader.vue @@ -1,12 +1,22 @@ \ No newline at end of file + +const selectedLanguage = ref(props.currentLanguage) + +// 监听外部语言变化 +watch(() => props.currentLanguage, (newLanguage) => { + selectedLanguage.value = newLanguage +}) + +const handleLanguageChange = () => { + emit('language-change', selectedLanguage.value) +} + diff --git a/src/components/StatusBar.vue b/src/components/StatusBar.vue index 3aff18a..4d2c965 100644 --- a/src/components/StatusBar.vue +++ b/src/components/StatusBar.vue @@ -1,9 +1,10 @@