Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Address CodeRabbit review: save the brush owner only with its brush

David Mann87e5d77b0e6e
diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rs
index 2b4b455bf..93ddaf017 100644
--- a/crates/ui-egui/src/prefs_ui.rs
+++ b/crates/ui-egui/src/prefs_ui.rs
@@ -3629,6 +3629,49 @@ mod tests {
}
}
+ /// #6212: `toolBrushes.owner` names the tool whose brush `last_brush` holds. A brush that
+ /// can't be remembered yet (a sampled tip too big to embed) leaves `last_brush` at the old
+ /// owner's brush, so the old owner stays saved: the next launch gives that brush back to it,
+ /// not to the tool selected since.
+ #[test]
+ fn the_saved_brush_owner_follows_the_saved_last_brush() {
+ use crate::state::Tool;
+ use photocraft_engine::paint::{GrayTile, TipShape};
+ let (mut app, store) = app_with_store();
+ let ctx = egui::Context::default();
+ tick(&mut app, &ctx);
+ app.session.tools.brush.size = 77.0;
+ app.session.tools.brush_changed();
+ crate::last_brush::persist(&mut app, &ctx);
+ crate::tool_memory::persist(&mut app, &ctx);
+ // The Brush's tip becomes a big sampled one, then the Eraser takes over that brush.
+ app.session.tools.brush.tip = TipShape::Sampled(GrayTile::from_fn(400, 300, |x, y| ((x * y) % 7) as f32 / 7.0));
+ app.session.tools.brush_changed();
+ app.ui.tool = Tool::Eraser;
+ crate::paint_mouse::sync_tool_brush(&mut app);
+ crate::last_brush::persist(&mut app, &ctx);
+ crate::tool_memory::persist(&mut app, &ctx);
+ tick(&mut app, &ctx);
+ let saved = stored(&store);
+ assert_eq!(saved["lastBrush"]["brush"]["size"], json!(77.0), "the big tip is not remembered yet");
+ assert_ne!(saved["toolBrushes"]["owner"], json!("Eraser"), "{saved}");
+ let (mut app2, _) = app_with_saved(store.lock().unwrap().clone());
+ tick(&mut app2, &ctx);
+ assert_eq!(app2.ui.brush_tool, Tool::Brush, "the 77 px brush is the Brush's");
+ assert_eq!(app2.session.tools.brush.size, 77.0);
+
+ // Once the Eraser's brush is saved, the Eraser owns it in the preferences too.
+ app.session.tools.brush.tip = TipShape::Round;
+ app.session.tools.brush.size = 33.0;
+ app.session.tools.brush_changed();
+ crate::last_brush::persist(&mut app, &ctx);
+ crate::tool_memory::persist(&mut app, &ctx);
+ tick(&mut app, &ctx);
+ let saved = stored(&store);
+ assert_eq!(saved["toolBrushes"]["owner"], json!("Eraser"), "{saved}");
+ assert_eq!(saved["lastBrush"]["brush"]["size"], json!(33.0));
+ }
+
/// #6212: a damaged or hand-edited `toolBrushes` never panics: what reads back loads (clamped
/// to the options bar's ranges), the rest is skipped and those tools start from the brush.
#[test]
@@ -3678,6 +3721,7 @@ mod tests {
}
app.ui.tool = Tool::Smudge;
crate::paint_mouse::sync_tool_brush(&mut app);
+ crate::last_brush::persist(&mut app, &ctx);
crate::tool_memory::persist(&mut app, &ctx);
tick(&mut app, &ctx);
let saved = stored(&store);
@@ -3685,6 +3729,7 @@ mod tests {
assert!(saved["toolBrushes"]["brushes"].get("Eraser").is_some() && saved["toolBrushes"]["brushes"].get("Brush").is_some());
crate::menus::invoke(&mut app, &ctx, crate::tool_reset::RESET, json!({})).unwrap();
+ crate::last_brush::persist(&mut app, &ctx);
crate::tool_memory::persist(&mut app, &ctx);
tick(&mut app, &ctx);
let saved = stored(&store);
@@ -3693,6 +3738,7 @@ mod tests {
assert!(saved["toolBrushes"]["brushes"].get("Eraser").is_some());
crate::menus::invoke(&mut app, &ctx, crate::tool_reset::RESET_ALL, json!({"confirmed": true})).unwrap();
+ crate::last_brush::persist(&mut app, &ctx);
crate::tool_memory::persist(&mut app, &ctx);
tick(&mut app, &ctx);
let saved = stored(&store);
diff --git a/crates/ui-egui/src/tool_memory.rs b/crates/ui-egui/src/tool_memory.rs
index 80fefe8ad..60895bb09 100644
--- a/crates/ui-egui/src/tool_memory.rs
+++ b/crates/ui-egui/src/tool_memory.rs
@@ -83,7 +83,7 @@ pub fn persist(app: &mut PhotocraftApp, ctx: &egui::Context) {
/// tip bitmaps are shared, so equal brushes compare by pointer). A brush that can't be remembered
/// yet (a sampled tip too big to embed, before the preset store takes it) keeps what was saved.
fn persist_brushes(app: &mut PhotocraftApp) {
- let (owner, brushes) = (app.ui.brush_tool, &app.ui.tool_brushes);
+ let (owner, brushes) = (saved_owner(app), &app.ui.tool_brushes);
if app.prefs_rt.tool_brushes.as_ref().is_some_and(|(o, b)| *o == owner && b == brushes) {
return;
}
@@ -110,6 +110,18 @@ fn persist_brushes(app: &mut PhotocraftApp) {
}
}
+/// The tool whose brush `last_brush` holds: the one that owns the session brush, once
+/// `last_brush::persist` has saved that brush. Until then (a sampled tip too big to embed, before
+/// the preset store takes it) `last_brush` still holds the previously saved owner's brush, so
+/// that owner stands; with none saved it is the Brush, as `toolBrushes` is null then.
+fn saved_owner(app: &PhotocraftApp) -> Tool {
+ let t = &app.session.tools;
+ if t.brush_rev == t.brush_saved_rev {
+ return app.ui.brush_tool;
+ }
+ app.session.prefs().tool_brushes.get("owner").and_then(|v| serde_json::from_value::<Tool>(v.clone()).ok()).unwrap_or(Tool::Brush)
+}
+
/// Restore the painting tools' own brushes and which tool the session brush (`last_brush`)
/// belongs to. Unknown tools, tools without a brush and brushes that don't read back are skipped
/// with a warning: such a tool starts from the current brush, as on a first launch.
@@ -142,8 +154,11 @@ fn restore_brushes(app: &mut PhotocraftApp) {
}
/// Flush the final choice on normal exit, even if no further frame ran after the tool changed.
+/// The brush first, as in a frame, so the saved brush owner matches the saved brush.
pub(crate) fn save_on_exit(app: &mut PhotocraftApp) -> Result<(), String> {
- persist(app, &egui::Context::default());
+ let ctx = egui::Context::default();
+ crate::last_brush::persist(app, &ctx);
+ persist(app, &ctx);
crate::prefs_ui::save_preferences(app)
}