Skip to content

Commit a246049

Browse files
committed
feat (core): 添加新版本提示
1 parent 88417ce commit a246049

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src-tauri/src/setup/menus/app.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,31 @@ use tauri::{
66
menu::{MenuItemBuilder, Submenu, SubmenuBuilder},
77
};
88

9+
use crate::update::check_for_updates;
10+
11+
fn check_update_sync() -> bool {
12+
match tokio::runtime::Runtime::new() {
13+
Ok(rt) => match rt.block_on(check_for_updates()) {
14+
Ok(Some(_)) => true,
15+
Ok(None) => false,
16+
Err(_e) => false,
17+
},
18+
Err(_e) => false,
19+
}
20+
}
21+
922
pub fn create_app_submenu(app: &AppHandle) -> tauri::Result<Submenu<tauri::Wry>> {
1023
let about_item = MenuItemBuilder::new("关于 CodeForge")
1124
.id("about")
1225
.build(app)?;
1326

14-
let update_item = MenuItemBuilder::new("检查更新").id("update").build(app)?;
27+
let update_text = if check_update_sync() {
28+
"检查更新 (有新版本)"
29+
} else {
30+
"检查更新"
31+
};
32+
33+
let update_item = MenuItemBuilder::new(update_text).id("update").build(app)?;
1534

1635
let settings_item = MenuItemBuilder::new("设置")
1736
.id("settings")

src/components/AppHeader.vue

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
</div>
3232

3333
<div class="flex items-center space-x-3">
34+
<Button type="warning" :icon="CheckCircle" v-if="hasUpdate">
35+
有新版本
36+
</Button>
37+
3438
<!-- 清空输出按钮 -->
3539
<Button @click="handleClearOutput"
3640
:disabled="isRunning"
@@ -43,12 +47,13 @@
4347
</template>
4448

4549
<script setup lang="ts">
46-
import {computed} from 'vue'
47-
import {FileCode, Play, Square, Trash2} from 'lucide-vue-next'
50+
import {computed, onMounted, ref} from 'vue'
51+
import {CheckCircle, FileCode, Play, Square, Trash2} from 'lucide-vue-next'
4852
import Select from '../ui/Select.vue'
4953
import Button from '../ui/Button.vue'
5054
import {Language} from '../types/app.ts'
5155
import {invoke} from "@tauri-apps/api/core";
56+
import {useUpdateManager} from "../composables/useUpdateManager.ts";
5257
5358
const props = defineProps<{
5459
isRunning: boolean
@@ -66,6 +71,8 @@ const emit = defineEmits<{
6671
'load-example': [content: string]
6772
}>()
6873
74+
const {checkForUpdates} = useUpdateManager()
75+
6976
// 使用计算属性来处理双向绑定
7077
const selectedLanguage = computed({
7178
get: () => props.currentLanguage,
@@ -75,6 +82,7 @@ const selectedLanguage = computed({
7582
}
7683
}
7784
})
85+
const hasUpdate = ref(false)
7886
7987
// 事件处理函数 - 确保不传递任何参数
8088
const handleRunCode = () => {
@@ -89,6 +97,13 @@ const handleClearOutput = () => {
8997
emit('clear-output')
9098
}
9199
100+
const checkUpdater = async () => {
101+
const result = await checkForUpdates()
102+
if (result) {
103+
hasUpdate.value = true
104+
}
105+
}
106+
92107
// 处理 Select 组件的 change 事件
93108
const handleLanguageChange = (value: string) => {
94109
if (value !== props.currentLanguage) {
@@ -105,4 +120,8 @@ const loadExample = async () => {
105120
console.error('Error loading example:', error)
106121
}
107122
}
123+
124+
onMounted(async () => {
125+
await checkUpdater()
126+
})
108127
</script>

src/composables/useUpdateManager.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ref } from 'vue'
2-
import { invoke } from '@tauri-apps/api/core'
3-
import { listen } from '@tauri-apps/api/event'
4-
import { CheckCircle, Download, RefreshCw, Wifi } from 'lucide-vue-next'
1+
import {ref} from 'vue'
2+
import {invoke} from '@tauri-apps/api/core'
3+
import {listen} from '@tauri-apps/api/event'
4+
import {CheckCircle, Download, RefreshCw, Wifi} from 'lucide-vue-next'
55

66
interface UpdateInfo
77
{
@@ -164,16 +164,19 @@ export function useUpdateManager()
164164
updateInfo.value = result
165165
hasUpdate.value = true
166166
currentStatus.value = 'UPDATE_AVAILABLE'
167+
return result
167168
}
168169
else {
169170
hasUpdate.value = false
170171
currentStatus.value = 'NO_UPDATE'
172+
return null
171173
}
172174
}
173175
catch (error) {
174176
console.error('Failed to check for updates:', error)
175177
errorMessage.value = String(error)
176178
currentStatus.value = 'ERROR'
179+
return null
177180
}
178181
finally {
179182
isChecking.value = false
@@ -191,7 +194,7 @@ export function useUpdateManager()
191194
errorMessage.value = ''
192195

193196
try {
194-
await invoke('start_update', { updateInfo: updateInfo.value })
197+
await invoke('start_update', {updateInfo: updateInfo.value})
195198
}
196199
catch (error) {
197200
console.error('Failed to start update:', error)
@@ -214,7 +217,7 @@ export function useUpdateManager()
214217
const setupUpdateListeners = async () => {
215218
// 监听下载进度
216219
await listen('update-progress', (event: any) => {
217-
const { progress, speed, status } = event.payload
220+
const {progress, speed, status} = event.payload
218221
downloadProgress.value = progress
219222
downloadSpeed.value = speed
220223

@@ -259,11 +262,11 @@ export function useUpdateManager()
259262
unitIndex++
260263
}
261264

262-
return `${ size.toFixed(1) } ${ units[unitIndex] }`
265+
return `${size.toFixed(1)} ${units[unitIndex]}`
263266
}
264267

265268
const formatSpeed = (bytesPerSecond: number) => {
266-
return `${ formatSize(bytesPerSecond) }/s`
269+
return `${formatSize(bytesPerSecond)}/s`
267270
}
268271

269272
return {

0 commit comments

Comments
 (0)