feat: stop every download at once

A stop button appears beside Download all whenever anything is
downloading or waiting to, and disappears when nothing is. It kills the
running processes, marks everything the database still calls queued or
running as cancelled, and clears the part files. Downloads already
finished are untouched.

That also covers two cases a per-video Cancel cannot reach: a download
parked on a slot, which stands down when its turn comes, and a row left
"running" by a crash that no process backs any more.

Cancelling no longer reports itself as a failure. download_video
returned Err("Download was cancelled.") when it found its child gone,
which the caller turned into an error dialog — pressing Stop all would
have raised one per download. Stopping something is a normal outcome:
the state is already recorded and the event already sent, so it returns
cleanly.
This commit is contained in:
vincent
2026-08-29 16:31:41 +02:00
parent 5b2be50852
commit 967bcdde93
6 changed files with 127 additions and 8 deletions
+46 -6
View File
@@ -1158,12 +1158,13 @@ pub async fn download_video(
}
}
let mut child = state
.children
.lock()
.await
.remove(&video_id)
.ok_or_else(|| "Download was cancelled.".to_string())?;
// Gone from the map means it was cancelled: the state is already recorded
// and the event already sent. Stopping something is a normal outcome, not
// a failure to report — a rejection here would raise a dialog per download
// the moment you press Stop all.
let Some(mut child) = state.children.lock().await.remove(&video_id) else {
return Ok(());
};
let status = child
.wait()
@@ -1442,6 +1443,45 @@ pub async fn fetch_subtitles(
Ok(read_vtt_dir(&dir).await)
}
/// Stops everything downloading or waiting to download.
///
/// Kills the running processes, then marks every row the database still calls
/// queued or running as cancelled — which covers downloads parked on a slot,
/// and any left behind by a crash that no process backs any more.
#[tauri::command]
pub async fn cancel_all_downloads(
app: AppHandle,
state: State<'_, AppState>,
) -> Result<usize, String> {
// Drain first and kill after, so no child is killed while the map is held.
let children: Vec<(String, tokio::process::Child)> =
state.children.lock().await.drain().collect();
for (_, mut child) in children {
let _ = child.kill().await;
}
let ids = state.db.lock().await.active_downloads()?;
let library = state.library.lock().await.clone();
for id in &ids {
state
.db
.lock()
.await
.set_download_state(id, DownloadState::Cancelled, None)?;
cleanup_partials(library.clone(), id).await;
let _ = app.emit(
"download:state",
DownloadStateEvent {
video_id: id.clone(),
state: DownloadState::Cancelled,
error: None,
path: None,
},
);
}
Ok(ids.len())
}
/// Removes every download and the files behind them, including subtitles.
#[tauri::command]
pub async fn delete_all_downloads(state: State<'_, AppState>) -> Result<usize, String> {