mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 09:26:43 +00:00
fix: remember foreground and background colors across launches
This commit is contained in:
1 parent
78b46b1a99
commit
6dacefce4d
4 files changed
+130
-3
No files matched your search
@@ -1008,6 +1008,9 @@ pub struct Preferences {
|
||||
/// Last selected tool, restored at launch. A tool-name string owned and validated by the
|
||||
/// shell; null in older files. Kept separate from tool options so neither overwrites the other.
|
||||
pub selected_tool: Value,
|
||||
/// Foreground/background swatches for pixels and masks, restored by the shell at launch.
|
||||
/// Null in older files, which retain the default black/white pixel colours.
|
||||
pub tool_colors: Value,
|
||||
/// View › Extras, the View › Show items (Pixel Grid, Grid, Guides, …) and the View › Snap To
|
||||
/// targets that differ from their defaults, saved as the user toggles them and restored at
|
||||
/// launch, as Photoshop keeps them between sessions (#3762). JSON owned by the shell.
|
||||
|
||||
@@ -2059,7 +2059,7 @@ impl PhotocraftApp {
|
||||
impl eframe::App for PhotocraftApp {
|
||||
fn on_exit(&mut self) {
|
||||
if let Err(err) = tool_memory::save_on_exit(self) {
|
||||
log::warn!("Preferences: could not save the final tool selection: {err}");
|
||||
log::warn!("Preferences: could not save the final tool state: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ pub struct Runtime {
|
||||
/// Tool options as last remembered in the preferences (see `tool_memory`).
|
||||
pub(crate) tool_options: Option<crate::state::ToolOptions>,
|
||||
pub(crate) selected_tool: Option<crate::state::Tool>,
|
||||
pub(crate) tool_colors: Option<[[[f32; 4]; 2]; 2]>,
|
||||
/// The tool owning the session brush and the other tools' brushes, as last remembered.
|
||||
pub(crate) tool_brushes: Option<crate::tool_memory::ToolBrushes>,
|
||||
}
|
||||
@@ -3499,6 +3500,84 @@ mod tests {
|
||||
assert!(app3.ui.brush_picker_list.show_name && app3.ui.brush_picker_list.show_stroke && app3.ui.brush_picker_list.show_tip);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_colors_survive_restart_before_the_first_frame() {
|
||||
let (mut app, store) = app_with_store();
|
||||
let ctx = egui::Context::default();
|
||||
let fg = [0.2, 0.4, 0.6, 0.8];
|
||||
let bg = [0.9, 0.7, 0.3, 1.0];
|
||||
app.run("tools.setColors", json!({"foreground": fg, "background": bg})).unwrap();
|
||||
crate::tool_memory::persist(&mut app, &ctx);
|
||||
tick(&mut app, &ctx);
|
||||
let (restored, _) = app_with_saved(store.lock().unwrap().clone());
|
||||
assert_eq!(restored.session.tools.foreground, fg);
|
||||
assert_eq!(restored.session.tools.background, bg);
|
||||
let rev = app.session.prefs.rev();
|
||||
crate::tool_memory::persist(&mut app, &ctx);
|
||||
assert_eq!(app.session.prefs.rev(), rev, "unchanged colours do not rewrite preferences");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_colors_preserve_finite_hdr_components() {
|
||||
let (mut app, store) = app_with_store();
|
||||
let fg = [1.5, -0.1, 0.6, 1.0];
|
||||
app.run("tools.setColors", json!({"foreground": fg})).unwrap();
|
||||
crate::tool_memory::save_on_exit(&mut app).unwrap();
|
||||
let (restored, _) = app_with_saved(store.lock().unwrap().clone());
|
||||
assert_eq!(restored.session.tools.foreground, fg);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_colors_flush_swaps_and_defaults_on_exit_without_another_frame() {
|
||||
let (mut app, store) = app_with_store();
|
||||
app.run("tools.setColors", json!({"foreground": "#336699", "background": "#cc8844"})).unwrap();
|
||||
app.run("tools.swapColors", json!({})).unwrap();
|
||||
crate::tool_memory::save_on_exit(&mut app).unwrap();
|
||||
let (mut restored, restored_store) = app_with_saved(store.lock().unwrap().clone());
|
||||
assert_eq!(restored.session.tools.foreground, app.session.tools.foreground);
|
||||
assert_eq!(restored.session.tools.background, app.session.tools.background);
|
||||
restored.run("tools.defaultColors", json!({})).unwrap();
|
||||
crate::tool_memory::save_on_exit(&mut restored).unwrap();
|
||||
let (defaults, _) = app_with_saved(restored_store.lock().unwrap().clone());
|
||||
assert_eq!(defaults.session.tools.foreground, [0.0, 0.0, 0.0, 1.0]);
|
||||
assert_eq!(defaults.session.tools.background, [1.0; 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_colors_keep_pixel_and_mask_pairs_when_exiting_on_a_mask() {
|
||||
let (mut app, store) = app_with_store();
|
||||
app.session.tools.foreground = [0.2, 0.4, 0.6, 1.0];
|
||||
app.session.tools.background = [0.9, 0.7, 0.3, 1.0];
|
||||
let pixels = [app.session.tools.foreground, app.session.tools.background];
|
||||
app.session.tools.target_mask(true);
|
||||
app.session.tools.foreground = [0.5, 0.5, 0.5, 1.0];
|
||||
let masks = [app.session.tools.foreground, app.session.tools.background];
|
||||
crate::tool_memory::save_on_exit(&mut app).unwrap();
|
||||
let (mut restored, _) = app_with_saved(store.lock().unwrap().clone());
|
||||
assert_eq!([restored.session.tools.foreground, restored.session.tools.background], pixels);
|
||||
restored.session.tools.target_mask(true);
|
||||
assert_eq!([restored.session.tools.foreground, restored.session.tools.background], masks);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_colors_ignore_invalid_swatches_without_losing_valid_preferences() {
|
||||
for bad in [json!(null), json!("red"), json!([1, 0, 0]), json!([1e100, 0, 0, 1]), json!([0, "bad", 0, 1])] {
|
||||
let (app, _) = app_with_saved(Some(
|
||||
json!({
|
||||
"toolColors": {"foreground": bad, "background": [0.2, 0.4, 0.6, 1.0]},
|
||||
"toolOptions": {"feather": 12.0}
|
||||
})
|
||||
.to_string(),
|
||||
));
|
||||
assert_eq!(app.session.tools.foreground, [0.0, 0.0, 0.0, 1.0]);
|
||||
assert_eq!(app.session.tools.background, [0.2, 0.4, 0.6, 1.0]);
|
||||
assert_eq!(app.ui.tool_options.feather, 12.0);
|
||||
}
|
||||
let (app, _) = app_with_saved(Some(json!({"toolOptions": {"feather": 12.0}}).to_string()));
|
||||
assert_eq!(app.session.tools.foreground, [0.0, 0.0, 0.0, 1.0]);
|
||||
assert_eq!(app.session.tools.background, [1.0; 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_tool_survives_a_restart_without_option_changes() {
|
||||
use crate::state::Tool;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Selected tool and tool options remembered between launches, as in Photoshop (#2814).
|
||||
//! Selected tool, colours and tool options remembered between launches, as in Photoshop (#2814).
|
||||
//!
|
||||
//! `Preferences::tool_options` holds the [`ToolOptions`] fields that differ from their defaults,
|
||||
//! so a default changed in a later release still reaches users who never touched that option.
|
||||
@@ -68,6 +68,7 @@ pub fn persist(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
persist_colors(app, ctx);
|
||||
persist_brushes(app);
|
||||
if app.prefs_rt.tool_options.as_ref() == Some(&app.ui.tool_options) {
|
||||
return;
|
||||
@@ -79,6 +80,49 @@ pub fn persist(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep pixel and mask swatches separately, even when the app exits with a mask targeted.
|
||||
fn colors(app: &PhotocraftApp) -> [[[f32; 4]; 2]; 2] {
|
||||
let t = &app.session.tools;
|
||||
let current = [t.foreground, t.background];
|
||||
if t.mask_targeted { [t.other_colors, current] } else { [current, t.other_colors] }
|
||||
}
|
||||
|
||||
fn persist_colors(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
let now = colors(app);
|
||||
if app.prefs_rt.tool_colors == Some(now) {
|
||||
return;
|
||||
}
|
||||
app.prefs_rt.tool_colors = Some(now);
|
||||
let [[foreground, background], [mask_foreground, mask_background]] = now;
|
||||
app.session.prefs.edit(|p| {
|
||||
p.tool_colors = json!({
|
||||
"foreground": foreground, "background": background,
|
||||
"maskForeground": mask_foreground, "maskBackground": mask_background,
|
||||
});
|
||||
});
|
||||
// Panels and shortcuts can change colours after prefs_ui::tick already ran this frame.
|
||||
ctx.request_repaint();
|
||||
}
|
||||
|
||||
fn restore_colors(app: &mut PhotocraftApp) {
|
||||
let saved = &app.session.prefs().tool_colors;
|
||||
let mut pairs = colors(app);
|
||||
let [[foreground, background], [mask_foreground, mask_background]] = &mut pairs;
|
||||
for (key, color) in [("foreground", foreground), ("background", background), ("maskForeground", mask_foreground), ("maskBackground", mask_background)] {
|
||||
if let Some(v) = saved.get(key) {
|
||||
match serde_json::from_value::<[f32; 4]>(v.clone()) {
|
||||
Ok(c) if c.iter().all(|x| x.is_finite()) => *color = c,
|
||||
_ => log::warn!("Preferences: ignoring the remembered `{key}` colour"),
|
||||
}
|
||||
}
|
||||
}
|
||||
let [pixels, mask] = pairs;
|
||||
let (current, other) = if app.session.tools.mask_targeted { (mask, pixels) } else { (pixels, mask) };
|
||||
[app.session.tools.foreground, app.session.tools.background] = current;
|
||||
app.session.tools.other_colors = other;
|
||||
app.prefs_rt.tool_colors = Some(pairs);
|
||||
}
|
||||
|
||||
/// Remember the painting tools' own brushes when they change (a compare per frame otherwise:
|
||||
/// 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.
|
||||
@@ -153,7 +197,7 @@ fn restore_brushes(app: &mut PhotocraftApp) {
|
||||
app.prefs_rt.tool_brushes = Some((owner, app.ui.tool_brushes.clone()));
|
||||
}
|
||||
|
||||
/// Flush the final choice on normal exit, even if no further frame ran after the tool changed.
|
||||
/// Flush the final tool state on normal exit, even if no further frame ran after a change.
|
||||
/// 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> {
|
||||
let ctx = egui::Context::default();
|
||||
@@ -195,6 +239,7 @@ pub fn restore(app: &mut PhotocraftApp) {
|
||||
log::warn!("Preferences: the remembered tool options are not an object; using the defaults");
|
||||
}
|
||||
app.prefs_rt.tool_options = Some(app.ui.tool_options.clone());
|
||||
restore_colors(app);
|
||||
restore_brushes(app);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user