mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 09:26:43 +00:00
Color panel: a preference change made outside the panel is kept
CodeRabbit on #9606 (inline 4373831220): prefs.set or a reset changed the stored Color panel options, and the next frame's persist wrote the panel's stale options back over them. persist now remembers what it last synced; a stored value that differs from it came from outside and is taken into the live panel. An empty value (a reset) restores the defaults. Also covers the Color panel in the dock lifecycle test (CodeRabbit nitpick), which runs real frames and a preferences JSON round trip. Tests: an_outside_preference_change_reaches_the_live_panel, dragging_the_splitter_resizes_and_survives_a_ui_state_round_trip Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a7b412d422
commit
6cc516a3d2
4 files changed
+47
-8
No files matched your search
@@ -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");
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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<serde_json::Value>,
|
||||
/// 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(),
|
||||
|
||||
Reference in new issue
Block a user