fix: restore legacy progress after owned tasks finish

This commit is contained in:
objz committed 2026-10-01 23:01:33 +02:00
1 parent cf9007fa06
commit 615d3fb8fc
2 files changed
+55 -23

No files matched your search

+33 -23
View File
@@ -12,7 +12,7 @@ pub struct ProgressState {
pub progress: Option<(u64, u64)>,
pub sub_action: Option<String>,
tasks: BTreeMap<u64, TaskState>,
legacy_active: bool,
legacy: Option<TaskState>,
}
#[derive(Debug, Clone)]
@@ -28,7 +28,6 @@ static NEXT_TASK_ID: AtomicU64 = AtomicU64::new(1);
pub struct ProgressTask {
id: u64,
finished: bool,
}
#[derive(Clone)]
@@ -53,10 +52,7 @@ impl ProgressTask {
}
tracing::info!("{}", action);
super::request_redraw();
Self {
id,
finished: false,
}
Self { id }
}
pub fn handle(&self) -> ProgressTaskHandle {
@@ -75,15 +71,13 @@ impl ProgressTask {
update_task_progress(self.id, current, total);
}
pub fn finish(mut self) {
self.remove();
self.finished = true;
pub fn finish(self) {
drop(self);
}
pub fn fail(mut self, error: impl std::fmt::Display) {
pub fn fail(self, error: impl std::fmt::Display) {
tracing::debug!("Progress task failed: {error}");
self.remove();
self.finished = true;
drop(self);
}
fn remove(&self) {
@@ -138,18 +132,21 @@ fn update_task_progress(id: u64, current: u64, total: u64) {
impl Drop for ProgressTask {
fn drop(&mut self) {
if !self.finished {
self.remove();
}
self.remove();
}
}
fn refresh_visible(state: &mut ProgressState) {
if let Some((_, task)) = state.tasks.last_key_value() {
if let Some(task) = state
.tasks
.last_key_value()
.map(|(_, task)| task)
.or(state.legacy.as_ref())
{
state.current_action = Some(task.action.clone());
state.sub_action.clone_from(&task.sub_action);
state.progress = task.progress;
} else if !state.legacy_active {
} else {
state.current_action = None;
state.sub_action = None;
state.progress = None;
@@ -160,9 +157,16 @@ pub fn set_action(text: impl Into<String>) {
let text = text.into();
match PROGRESS.lock() {
Ok(mut state) => {
state.legacy_active = true;
state.current_action = Some(text.clone());
state.progress = None;
let sub_action = state
.legacy
.as_ref()
.and_then(|task| task.sub_action.clone());
state.legacy = Some(TaskState {
action: text.clone(),
sub_action,
progress: None,
});
refresh_visible(&mut state);
super::request_redraw();
}
Err(e) => {
@@ -175,7 +179,10 @@ pub fn set_action(text: impl Into<String>) {
pub fn set_progress(current: u64, total: u64) {
match PROGRESS.lock() {
Ok(mut state) => {
state.progress = Some((current, total));
if let Some(task) = state.legacy.as_mut() {
task.progress = Some((current, total));
}
refresh_visible(&mut state);
super::request_redraw();
}
Err(e) => {
@@ -188,7 +195,10 @@ pub fn set_sub_action(text: impl Into<String>) {
let text = text.into();
match PROGRESS.lock() {
Ok(mut state) => {
state.sub_action = Some(text.clone());
if let Some(task) = state.legacy.as_mut() {
task.sub_action = Some(text.clone());
}
refresh_visible(&mut state);
super::request_redraw();
}
Err(e) => {
@@ -201,7 +211,7 @@ pub fn set_sub_action(text: impl Into<String>) {
pub fn clear() {
match PROGRESS.lock() {
Ok(mut state) => {
state.legacy_active = false;
state.legacy = None;
refresh_visible(&mut state);
super::request_redraw();
}
+22
View File
@@ -59,6 +59,28 @@ fn clearing_legacy_progress_keeps_owned_tasks() {
clear();
}
#[test]
fn owned_tasks_restore_legacy_progress_and_legacy_updates_do_not_cover_them() {
let _guard = TEST_LOCK.lock().unwrap();
clear();
set_action("legacy");
set_sub_action("download");
set_progress(1, 3);
let task = ProgressTask::start("owned");
set_progress(2, 3);
assert_eq!(
PROGRESS.lock().unwrap().current_action.as_deref(),
Some("owned")
);
task.finish();
let state = PROGRESS.lock().unwrap();
assert_eq!(state.current_action.as_deref(), Some("legacy"));
assert_eq!(state.sub_action.as_deref(), Some("download"));
assert_eq!(state.progress, Some((2, 3)));
drop(state);
clear();
}
#[test]
fn a_new_legacy_action_does_not_reuse_the_previous_phase_progress() {
let _guard = TEST_LOCK.lock().unwrap();