mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge #18014 into the merge queue batch
Fill: grey History in Contents when its source does not match (#17464)
This commit is contained in:
commit
99fbbb7cbc
5 files changed
+161
-6
No files matched your search
@@ -51,6 +51,14 @@ pub fn checked_source(st: &DocState, p: &Value, cmd: &str) -> Result<std::result
|
||||
Ok(Ok(doc))
|
||||
}
|
||||
|
||||
/// Whether Fill › History has a source to paint from: the History panel's source, set, of this canvas size and of
|
||||
/// the document's colour mode and bit depth. The Fill dialog greys History in Contents when not (#17464); asking the
|
||||
/// checks `edit.fill` runs ([`checked_source`], [`same_mode_and_depth`]) keeps the dropdown and the command from
|
||||
/// disagreeing.
|
||||
pub fn fill_source_usable(st: &DocState) -> bool {
|
||||
matches!(checked_source(st, &Value::Null, "edit.fill"), Ok(Ok(source)) if same_mode_and_depth(st, &source))
|
||||
}
|
||||
|
||||
/// Fill › History's rule beyond the canvas size: the source must have the document's colour mode
|
||||
/// and bit depth, whatever happened in between (8 to 16 to 8 bit fills again). Document modes are
|
||||
/// compared, so an Indexed document refuses its RGB source. Unlike [`refuse_other_format`], every
|
||||
|
||||
@@ -17,7 +17,7 @@ use photocraft_engine_core::{
|
||||
use photocraft_engine_core::{command_specs, hidden_target, param_schema, prefs};
|
||||
|
||||
pub mod cmd;
|
||||
mod history_source;
|
||||
pub mod history_source;
|
||||
mod install;
|
||||
|
||||
pub use cmd::adjustment_brush_cmds;
|
||||
|
||||
@@ -16,8 +16,8 @@ pub use photocraft_engine_core::Session as CoreSession;
|
||||
pub use photocraft_engine_core::*;
|
||||
// The painting commands' modules, at the paths they had inside the engine.
|
||||
pub use photocraft_engine_paint::{
|
||||
adjustment_brush_cmds, brush_cmds, brush_key_cmds, eraser_cmds, face_cmds, fill_cmds, fill_key_cmds, paint_cmds, pick_cmds, redeye_cmds, retouch_cmds,
|
||||
sampler_cmds, stamp_cmds,
|
||||
adjustment_brush_cmds, brush_cmds, brush_key_cmds, eraser_cmds, face_cmds, fill_cmds, fill_key_cmds, history_source, paint_cmds, pick_cmds, redeye_cmds,
|
||||
retouch_cmds, sampler_cmds, stamp_cmds,
|
||||
};
|
||||
// The vector-area commands' modules, likewise.
|
||||
pub use photocraft_engine_vector::{
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
//! #17464: the Fill dialog greys History in Contents whenever `edit.fill` would refuse the History source (another
|
||||
//! canvas size after Image Size, Canvas Size, Crop or Rotate 90°; another colour mode or bit depth, #17463), by the
|
||||
//! engine's own check; with a matching source it stays enabled. The other entries keep their order and state.
|
||||
|
||||
use craft::ui::patterns::form_dialogs::HostedParamDialog;
|
||||
use photocraft_engine::history_source::fill_source_usable;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::*;
|
||||
use crate::smart_filter_dialog::tests::cancel;
|
||||
|
||||
/// A white 40×30 document with a pixel layer "Paint" active and a selection, so only History's rule can grey an entry.
|
||||
/// The current state is the History source, so the source has the layer Fill paints on.
|
||||
fn app() -> Box<PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 40, "height": 30, "background": "white"})).unwrap();
|
||||
app.run("layer.new.layer", json!({"name": "Paint"})).unwrap();
|
||||
app.run("select.rect", json!({"x": 5, "y": 5, "width": 20, "height": 15})).unwrap();
|
||||
source_current(&mut app);
|
||||
app
|
||||
}
|
||||
|
||||
/// Make the current state the History source.
|
||||
fn source_current(app: &mut PhotocraftApp) {
|
||||
let current = app.session.active().unwrap().history.entries().len() - 1;
|
||||
app.run("history.setSource", json!({"state": current})).unwrap();
|
||||
}
|
||||
|
||||
/// The canvas changes that leave the opening state's source at another size. Each is run by [`resized`].
|
||||
fn resizes() -> Vec<(&'static str, Value)> {
|
||||
vec![
|
||||
("image.imageSize", json!({"width": 20, "height": 15})),
|
||||
("image.canvasSize", json!({"width": 50, "height": 30})),
|
||||
("image.crop", json!({"x": 0, "y": 0, "width": 30, "height": 20})),
|
||||
("image.imageRotation.90cw", json!({})),
|
||||
]
|
||||
}
|
||||
|
||||
/// The conversions that leave the source in another colour mode or bit depth.
|
||||
fn conversions() -> Vec<(&'static str, Value)> {
|
||||
vec![("image.mode.bits16", json!({})), ("image.mode.grayscale", json!({}))]
|
||||
}
|
||||
|
||||
/// [`app`] after `cmd`, with a selection again (Image Size drops it), so Content-Aware's own rule stays out.
|
||||
fn resized(cmd: &str, p: Value) -> Box<PhotocraftApp> {
|
||||
let mut app = app();
|
||||
app.run(cmd, p).unwrap();
|
||||
app.run("select.rect", json!({"x": 2, "y": 2, "width": 8, "height": 6})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
fn contents_row(app: &PhotocraftApp) -> Param {
|
||||
rows(app, [0.0; 3], None, None).into_iter().find(|r| r.key == "contents").expect("a Contents row")
|
||||
}
|
||||
|
||||
fn keys(row: &Param) -> Vec<String> {
|
||||
let ParamKind::Choice { options, .. } = &row.kind else { panic!("Contents is a choice row") };
|
||||
options.iter().map(|(k, _)| k.clone()).collect()
|
||||
}
|
||||
|
||||
fn choosable(app: &mut PhotocraftApp, id: DialogId, key: &str) -> bool {
|
||||
let ctx = app.ctx().clone();
|
||||
app.framework.dialogs.set_json_field(&ctx, id, "contents", json!(key)).is_ok()
|
||||
}
|
||||
|
||||
fn contents(app: &PhotocraftApp, id: DialogId) -> Option<String> {
|
||||
let dialog = app.framework.dialogs.get_by_id::<HostedParamDialog>(id).expect("a hosted dialog");
|
||||
dialog.fields().text("contents").map(str::to_string)
|
||||
}
|
||||
|
||||
/// Whether `edit.fill` with History succeeds, undone again so the document stays as it was.
|
||||
fn history_fill_succeeds(app: &mut PhotocraftApp) -> bool {
|
||||
let ok = app.run(COMMAND, json!({"contents": "history"})).is_ok();
|
||||
if ok {
|
||||
app.run("edit.undo", json!({})).unwrap();
|
||||
}
|
||||
ok
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_is_enabled_on_the_unchanged_document() {
|
||||
let mut app = app();
|
||||
assert!(contents_row(&app).option_enabled("history"));
|
||||
let id = open(&mut app);
|
||||
assert!(choosable(&mut app, id, "history"));
|
||||
cancel(&mut app, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_source_that_does_not_match_greys_history_and_it_cannot_be_chosen() {
|
||||
for (cmd, p) in resizes().into_iter().chain(conversions()) {
|
||||
let mut app = resized(cmd, p);
|
||||
let row = contents_row(&app);
|
||||
assert!(!row.option_enabled("history"), "{cmd}: History greyed");
|
||||
let order: Vec<String> = CONTENTS.iter().map(|(k, _)| k.to_string()).collect();
|
||||
assert_eq!(keys(&row), order, "{cmd}: every entry listed, in the same order");
|
||||
for (key, _) in CONTENTS.iter().filter(|(k, _)| *k != "history") {
|
||||
assert!(row.option_enabled(key), "{cmd}: {key} stays enabled");
|
||||
}
|
||||
|
||||
let id = open(&mut app);
|
||||
let before = contents(&app, id);
|
||||
assert!(!choosable(&mut app, id, "history"), "{cmd}: History cannot be chosen");
|
||||
assert_eq!(contents(&app, id), before, "{cmd}: the choice is unchanged");
|
||||
cancel(&mut app, id);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_matching_source_keeps_history_enabled() {
|
||||
for (cmd, p) in resizes().into_iter().chain(conversions()) {
|
||||
let mut app = resized(cmd, p);
|
||||
// The current state, which has the new size, becomes the source.
|
||||
source_current(&mut app);
|
||||
assert!(contents_row(&app).option_enabled("history"), "{cmd}: History enabled with a source of this size");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_dropdown_and_the_command_agree() {
|
||||
let mut cases = vec![app()];
|
||||
for (cmd, p) in resizes().into_iter().chain(conversions()) {
|
||||
cases.push(resized(cmd, p));
|
||||
}
|
||||
for mut app in cases {
|
||||
let usable = fill_source_usable(app.session.active().unwrap());
|
||||
assert_eq!(contents_row(&app).option_enabled("history"), usable);
|
||||
assert_eq!(history_fill_succeeds(&mut app), usable, "History offered exactly when the fill succeeds");
|
||||
}
|
||||
}
|
||||
|
||||
/// The framework restores only the remembered values a row accepts: a remembered History is dropped once greyed and the
|
||||
/// row opens on its default, as it does for any unavailable choice; with a usable source it is kept.
|
||||
#[test]
|
||||
fn a_remembered_history_choice_keeps_todays_fallback_once_greyed() {
|
||||
let mut saved = DialogFields::new();
|
||||
saved.set("contents", "history");
|
||||
let unchanged = ParamDialog::remembered(&rows(&app(), [0.0; 3], None, None), &saved);
|
||||
assert_eq!(unchanged.text("contents"), Some("history"), "kept while usable");
|
||||
let app = resized("image.imageSize", json!({"width": 20, "height": 15}));
|
||||
let greyed = ParamDialog::remembered(&rows(&app, [0.0; 3], None, None), &saved);
|
||||
assert_eq!(greyed.text("contents"), None, "dropped once greyed: the row opens on Foreground Color");
|
||||
}
|
||||
@@ -62,9 +62,10 @@ fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option<String>
|
||||
let contents = CONTENTS.iter().map(|(k, l)| (k.to_string(), Label::from(tl!(l)))).collect();
|
||||
let mut contents = Param::new("contents", tl!("Contents:"), ParamKind::choice(contents, Some(opening_contents(app).into())));
|
||||
// Content-Aware fills a selection from around it: without one it is listed greyed, as in the reference (#17462).
|
||||
if !has_selection(app) {
|
||||
contents = contents.disabled_options(["contentAware"]);
|
||||
}
|
||||
// History is greyed whenever `edit.fill` would refuse its source, by the engine's own check (#17464).
|
||||
let greyed =
|
||||
[("contentAware", !has_selection(app)), ("history", app.session.active().is_none_or(|st| !photocraft_engine::history_source::fill_source_usable(st)))];
|
||||
contents = contents.disabled_options(greyed.into_iter().filter(|(_, off)| *off).map(|(key, _)| key));
|
||||
let mut rows = vec![
|
||||
contents,
|
||||
Param::new("color", tl!("Color:"), ParamKind::color(Some(foreground))).visible_when("contents", "color"),
|
||||
@@ -213,6 +214,9 @@ pub(crate) fn run(app: &mut PhotocraftApp, ctx: &egui::Context, result: ParamRes
|
||||
#[cfg(test)]
|
||||
mod content_aware_greyed_tests;
|
||||
#[cfg(test)]
|
||||
#[path = "fill_history_greyed_tests.rs"]
|
||||
mod fill_history_greyed_tests;
|
||||
#[cfg(test)]
|
||||
mod no_selection_fallback_tests;
|
||||
#[cfg(test)]
|
||||
mod opacity_alert_tests;
|
||||
|
||||
Reference in new issue
Block a user