diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rs index 6dacd3ec97..5cee5b2159 100644 --- a/crates/ops/src/lib.rs +++ b/crates/ops/src/lib.rs @@ -632,7 +632,7 @@ impl History { continue; } total = total.saturating_add(tile_bytes(&s.doc, &mut seen)); - if total > self.max_bytes && keep >= 1 { + if total > self.max_bytes && (keep >= 1 || source == Some(undo.len() - 1)) { break; } keep += 1; diff --git a/crates/ops/src/tests.rs b/crates/ops/src/tests.rs index 7cb1fc328a..bd7de002f0 100644 --- a/crates/ops/src/tests.rs +++ b/crates/ops/src/tests.rs @@ -222,6 +222,28 @@ fn byte_budget_keeps_the_brush_source_of_the_cleared_states() { assert!(Arc::ptr_eq(&h.row_doc(0, &cur).unwrap(), &source), "the History Brush paints from the same state"); } +/// When the History Brush's source is the newest past state, it is the state the byte budget +/// always keeps, and no older state is kept beside it. +#[test] +fn byte_budget_counts_a_newest_brush_source_as_the_kept_state() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + for i in 0..5 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + assert!(h.set_brush_source(4)); + let source = h.state(4).unwrap(); + h.max_bytes = 1; + assert_eq!(h.trim(&cur), 4); + assert_counter(&h, &cur); + assert_eq!(h.past_len(), 1); + assert_eq!(h.brush_source(), Some(0)); + assert!(Arc::ptr_eq(&h.row_doc(0, &cur).unwrap(), &source)); +} + #[test] fn new_edit_clears_redo() { let mut h = History::default();