From fc6d61e41b1b0e0686d1346694445dc119b97872 Mon Sep 17 00:00:00 2001
From: Ariver <shanghai3168@gmail.com>
Date: Sun, 28 Jun 2026 23:58:07 +0800
Subject: [PATCH] Fix hold recording head and tail clipping
---
privatevoice.src/frontend/src/components/settings/LanguagePage.svelte | 160 ++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 146 insertions(+), 14 deletions(-)
diff --git a/privatevoice.src/frontend/src/components/settings/LanguagePage.svelte b/privatevoice.src/frontend/src/components/settings/LanguagePage.svelte
index 2bb1b72..39de74f 100644
--- a/privatevoice.src/frontend/src/components/settings/LanguagePage.svelte
+++ b/privatevoice.src/frontend/src/components/settings/LanguagePage.svelte
@@ -30,6 +30,8 @@
let languageOptions = $state<LanguageOption[]>([])
let modelOptions = $state<ModelOption[]>([])
let modelBusyID = $state('')
+ let modelBusyKind = $state<'download' | 'select' | ''>('')
+ let modelCancellingID = $state('')
let modelProgress = $state(0)
let modelError = $state('')
@@ -39,8 +41,30 @@
Events.On('model:download-progress', (ev: any) => {
const data = ev?.data
- if (data?.modelID && data.modelID === modelBusyID && typeof data.percent === 'number') {
+ if (data?.modelID && typeof data.percent === 'number') {
+ modelBusyID = data.modelID
+ modelBusyKind = 'download'
+ if (modelCancellingID !== data.modelID) {
+ modelCancellingID = ''
+ }
modelProgress = data.percent
+ }
+ })
+
+ Events.On('model:download-cancelled', (ev: any) => {
+ const data = ev?.data
+ if (data?.modelID) {
+ modelBusyID = data.modelID
+ modelBusyKind = 'download'
+ modelCancellingID = data.modelID
+ }
+ })
+
+ Events.On('model:download-finished', (ev: any) => {
+ const data = ev?.data
+ if (!data?.modelID || data.modelID === modelBusyID) {
+ clearModelBusy()
+ loadModelOptions()
}
})
@@ -50,6 +74,7 @@
applyLanguageSettings(lang)
} catch {}
await loadModelOptions()
+ await syncModelDownloadStatus()
settingsLoaded = true
}
loadSettings()
@@ -92,6 +117,37 @@
}
}
+ async function syncModelDownloadStatus(): Promise<boolean> {
+ try {
+ const status: any = await Call.ByName('voicesnap/services.EngineService.GetModelDownloadStatus')
+ return applyModelDownloadStatus(status)
+ } catch {
+ return false
+ }
+ }
+
+ function applyModelDownloadStatus(status: any): boolean {
+ if (status?.active && status?.modelID) {
+ modelBusyID = status.modelID
+ modelBusyKind = 'download'
+ modelCancellingID = status.cancelling ? status.modelID : ''
+ modelProgress = typeof status.percent === 'number' ? status.percent : 0
+ modelError = ''
+ return true
+ }
+ if (modelBusyKind === 'download') {
+ clearModelBusy()
+ }
+ return false
+ }
+
+ function clearModelBusy() {
+ modelBusyID = ''
+ modelBusyKind = ''
+ modelCancellingID = ''
+ modelProgress = 0
+ }
+
function modelDescription(option: ModelOption): string {
switch (option.modelID) {
case 'sensevoice-zh': return t('models.sensevoiceDesc')
@@ -119,8 +175,8 @@
function modelActionLabel(option: ModelOption): string {
if (modelBusyID === option.modelID) {
- if (!option.installed && modelProgress > 0) {
- return `${Math.min(100, Math.max(0, modelProgress)).toFixed(0)}%`
+ if (modelBusyKind === 'download') {
+ return modelCancellingID === option.modelID ? t('settings.modelCancelling') : t('settings.modelCancel')
}
return t('settings.modelWorking')
}
@@ -129,9 +185,73 @@
return t('settings.modelDownload')
}
+ function modelMeta(option: ModelOption): string {
+ let meta = option.installed ? t('settings.modelInstalled') : t('settings.modelNotInstalled')
+ if (option.downloadSize) {
+ meta += ` · ${option.downloadSize}`
+ }
+ if (modelBusyID === option.modelID && modelBusyKind === 'download' && modelProgress > 0) {
+ const pct = Math.min(100, Math.max(0, modelProgress)).toFixed(0)
+ meta += ` · ${pct}%`
+ }
+ return meta
+ }
+
+ function isCancelError(err: any): boolean {
+ const text = errorMessage(err).toLowerCase()
+ return text.includes('context canceled') || text.includes('cancelled') || text.includes('canceled')
+ }
+
+ function isAlreadyDownloadingError(err: any): boolean {
+ return errorMessage(err).toLowerCase().includes('already downloading')
+ }
+
+ function errorMessage(err: any): string {
+ const raw = String(err?.message || err || '')
+ if (!raw.startsWith('{')) return raw
+ try {
+ const parsed = JSON.parse(raw)
+ return String(parsed?.message || raw)
+ } catch {
+ return raw
+ }
+ }
+
+ function canCancelModel(option: ModelOption): boolean {
+ return modelBusyID === option.modelID && modelBusyKind === 'download'
+ }
+
+ async function onModelButton(option: ModelOption) {
+ if (canCancelModel(option)) {
+ await cancelModelDownload(option)
+ return
+ }
+ await onModelAction(option)
+ }
+
+ async function cancelModelDownload(option: ModelOption) {
+ if (!canCancelModel(option) || modelCancellingID) return
+ modelCancellingID = option.modelID
+ modelError = ''
+ try {
+ const cancelled: any = await Call.ByName('voicesnap/services.EngineService.CancelModelDownload', option.modelID)
+ if (!cancelled) {
+ clearModelBusy()
+ await loadModelOptions()
+ await syncModelDownloadStatus()
+ }
+ } catch (err: any) {
+ modelError = errorMessage(err) || t('settings.modelActionFailed')
+ clearModelBusy()
+ await loadModelOptions()
+ await syncModelDownloadStatus()
+ }
+ }
+
async function onModelAction(option: ModelOption) {
if ((option.isCurrent && option.installed) || modelBusyID) return
modelBusyID = option.modelID
+ modelBusyKind = option.installed ? 'select' : 'download'
modelProgress = 0
modelError = ''
try {
@@ -144,11 +264,22 @@
}
await loadModelOptions()
} catch (err: any) {
- modelError = err?.message || String(err || t('settings.modelActionFailed'))
+ if (isAlreadyDownloadingError(err)) {
+ const active = await syncModelDownloadStatus()
+ if (!active) {
+ modelError = errorMessage(err)
+ }
+ } else if (!isCancelError(err)) {
+ modelError = errorMessage(err) || t('settings.modelActionFailed')
+ }
await loadModelOptions()
} finally {
- modelBusyID = ''
- modelProgress = 0
+ if (modelBusyID === option.modelID) {
+ const active = await syncModelDownloadStatus()
+ if (!active || modelBusyID !== option.modelID) {
+ clearModelBusy()
+ }
+ }
}
}
@@ -171,6 +302,7 @@
applyLanguageSettings(settings)
await syncEngineForCurrentModel()
await loadModelOptions()
+ await syncModelDownloadStatus()
} catch {
languageModeVal = prevMode
languageIDVal = prevID
@@ -236,20 +368,16 @@
</span>
</div>
<span class="model-desc">{modelDescription(option)}</span>
- <span class="model-meta">
- {option.installed ? t('settings.modelInstalled') : t('settings.modelNotInstalled')}
- {#if option.downloadSize}
- · {option.downloadSize}
- {/if}
- </span>
+ <span class="model-meta">{modelMeta(option)}</span>
</div>
{#if option.isCurrent && option.installed}
<span class="model-current-status">{t('settings.modelCurrent')}</span>
{:else}
<button
class="model-action primary"
- disabled={!!modelBusyID}
- onclick={() => onModelAction(option)}
+ class:cancel={canCancelModel(option)}
+ disabled={!!modelBusyID && modelBusyID !== option.modelID}
+ onclick={() => onModelButton(option)}
>
{modelActionLabel(option)}
</button>
@@ -455,6 +583,10 @@
color: white;
}
+ .model-action.primary.cancel {
+ background: var(--color-red);
+ }
+
.model-action:disabled {
cursor: not-allowed;
opacity: 0.72;
--
Gitblit v1.9.3