diff --git a/crates/ui-egui/src/color_panel_tests.rs b/crates/ui-egui/src/color_panel_tests.rs index 902e51f748..6f0d36f5cf 100644 --- a/crates/ui-egui/src/color_panel_tests.rs +++ b/crates/ui-egui/src/color_panel_tests.rs @@ -520,3 +520,26 @@ fn color_panel_options_survive_a_restart() { crate::color_panel_ui::restore(&mut fresh); assert_eq!(fresh.ui.color_panel.mode, ColorPanelMode::HueCube, "no saved options: the default"); } + +/// CodeRabbit on #9606: a preference change made outside the panel (`prefs.set`, a reset) is kept. +/// The next frame takes it into the live panel instead of writing the panel's old options back. +#[test] +fn an_outside_preference_change_reaches_the_live_panel() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.ui.color_panel.mode = ColorPanelMode::HsbSliders; + crate::color_panel_ui::persist(&mut app); + // As prefs.set or a reset to defaults would: the stored options change under the panel. + let mut other = app.ui.color_panel; + other.mode = ColorPanelMode::GrayscaleSlider; + app.session.prefs.edit(|p| p.color_panel = serde_json::to_value(other).unwrap()); + crate::color_panel_ui::persist(&mut app); + assert_eq!(app.ui.color_panel.mode, ColorPanelMode::GrayscaleSlider, "the live panel follows the preference"); + let stored: crate::state::ColorPanelState = serde_json::from_value(app.session.prefs().color_panel.clone()).unwrap(); + assert_eq!(stored.mode, ColorPanelMode::GrayscaleSlider, "and the preference isn't overwritten"); + // A reset to defaults clears the stored options: the panel goes back to its defaults too. + app.session.prefs.edit(|p| p.color_panel = serde_json::Value::Null); + crate::color_panel_ui::persist(&mut app); + assert_eq!(app.ui.color_panel.mode, ColorPanelMode::HueCube, "reset: the default mode"); + crate::color_panel_ui::persist(&mut app); + assert_eq!(app.ui.color_panel.mode, ColorPanelMode::HueCube, "and it stays reset"); +} diff --git a/crates/ui-egui/src/color_panel_ui.rs b/crates/ui-egui/src/color_panel_ui.rs index 29553fead2..8185da8eb2 100644 --- a/crates/ui-egui/src/color_panel_ui.rs +++ b/crates/ui-egui/src/color_panel_ui.rs @@ -768,15 +768,22 @@ pub(crate) fn panel_menu(app: &mut PhotocraftApp, ui: &mut egui::Ui) { /// Remember the Color panel's options in the preferences when they change, so they survive a /// restart (#9592). Cheap: a small JSON compare per frame. pub fn persist(app: &mut PhotocraftApp) { - let Ok(now) = serde_json::to_value(app.ui.color_panel) else { return }; - if app.session.prefs().color_panel != now { - app.session.prefs.edit(|p| p.color_panel = now); + let stored = app.session.prefs().color_panel.clone(); + // Changed outside the panel since the last sync (`prefs.set`, a reset): the panel takes it. + if app.ui.color_panel_synced.as_ref().is_some_and(|synced| *synced != stored) { + restore(app); + app.ui.color_panel_synced = Some(stored); + return; } + let Ok(now) = serde_json::to_value(app.ui.color_panel) else { return }; + if stored != now { + app.session.prefs.edit(|p| p.color_panel = now.clone()); + } + app.ui.color_panel_synced = Some(now); } -/// Restore the remembered Color panel options at launch; unreadable ones keep the defaults. +/// Restore the remembered Color panel options (at launch, or after a preference change made +/// outside the panel); none stored (a reset) or unreadable ones are the defaults. pub fn restore(app: &mut PhotocraftApp) { - if let Ok(c) = serde_json::from_value(app.session.prefs().color_panel.clone()) { - app.ui.color_panel = c; - } + app.ui.color_panel = serde_json::from_value(app.session.prefs().color_panel.clone()).unwrap_or_default(); } diff --git a/crates/ui-egui/src/dock_tests.rs b/crates/ui-egui/src/dock_tests.rs index c03c067f6d..d3884e20fa 100644 --- a/crates/ui-egui/src/dock_tests.rs +++ b/crates/ui-egui/src/dock_tests.rs @@ -109,7 +109,9 @@ fn switching_layer_kinds_keeps_the_layers_panel_still() { /// A group height dragged in the dock is part of the UI state and the remembered layout. #[test] fn dragging_the_splitter_resizes_and_survives_a_ui_state_round_trip() { - let (app, _, _) = app_with_layers(); + let (mut app, _, _) = app_with_layers(); + // CodeRabbit on #9606: the Color panel's options ride the same frames and preferences. + app.ui.color_panel.mode = crate::state::ColorPanelMode::HsbSliders; let mut h = harness(app, vec2(1200.0, 800.0), ThemeKind::ProMedium); let props = rect_of(&h, Group::Properties); let color = rect_of(&h, Group::Color); @@ -137,6 +139,8 @@ fn dragging_the_splitter_resizes_and_survives_a_ui_state_round_trip() { restore(&mut app3); assert!(app3.ui.docking.is_some()); assert_eq!(app3.ui.docking.as_ref().map(|w| &w.layout), h.state().ui.docking.as_ref().map(|w| &w.layout)); + crate::color_panel_ui::restore(&mut app3); + assert_eq!(app3.ui.color_panel.mode, crate::state::ColorPanelMode::HsbSliders, "the Color panel's mode too"); // …unless Remember Workspace Changes is off. let mut s3 = photocraft_engine::Session::new(); s3.load_prefs_json(&prefs).unwrap(); diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index d9bad0e07c..7f4a066cfb 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -1598,6 +1598,10 @@ pub struct UiState { /// Which chip the Color panel edits, and how it shows the colour. #[serde(default)] pub color_panel: ColorPanelState, + /// The Color panel options as last synced with the preferences: a stored value that differs + /// from it was changed outside the panel (`prefs.set`, a reset) and is taken, not overwritten. + #[serde(skip)] + pub color_panel_synced: Option, /// Histogram panel options: view, channel, statistics, colours, source. #[serde(default)] pub histogram_panel: crate::histogram_panel::HistogramPanelState, @@ -1751,6 +1755,7 @@ impl Default for UiState { docking_collapsed: Vec::new(), docking_reveal: None, color_panel: Default::default(), + color_panel_synced: None, brush_section: 0, brush_tab: 0, brushes_panel: Default::default(),