mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #18034 from davidmann4/fix/17470-fill-color-opens-picker
Fill: Color… opens the colour picker on every choice (#17470)
This commit is contained in:
8 files changed
+206
-12
No files matched your search
@@ -424,6 +424,14 @@ fn commit(app: &mut PhotocraftApp, _id: DialogId, target: PickTarget, picked: &P
|
||||
PickTarget::NewSolidFill { document } => crate::solid_fill_ui::confirm_new(app, *document, picked),
|
||||
PickTarget::Request { parent, key } => {
|
||||
if let Some(dialog) = app.framework.dialogs.get_mut_by_id::<HostedParamDialog>(*parent) {
|
||||
// The colour goes in with its inks at once, so an OK of the parent right after this one (an agent's next
|
||||
// request, before a dialog pass has taken the answer) fills with it; a row with opacity keeps its own.
|
||||
let [r, g, b] = picked.color.to_rgb_array();
|
||||
let value = match dialog.fields().get(key) {
|
||||
Some(FieldValue::Rgba([.., a])) => FieldValue::Rgba([r, g, b, *a]),
|
||||
_ => FieldValue::Color([r, g, b]),
|
||||
};
|
||||
dialog.set_field(key.clone(), value);
|
||||
dialog.set_field(ink_field(key), ink_value(picked.color, inks_of(picked)));
|
||||
}
|
||||
app.color_answers.push((*parent, key.clone(), picked.color));
|
||||
|
||||
@@ -50,7 +50,7 @@ fn resized(cmd: &str, p: Value) -> Box<PhotocraftApp> {
|
||||
}
|
||||
|
||||
fn contents_row(app: &PhotocraftApp) -> Param {
|
||||
rows(app, [0.0; 3], None, None).into_iter().find(|r| r.key == "contents").expect("a Contents row")
|
||||
rows(app, None, None).into_iter().find(|r| r.key == "contents").expect("a Contents row")
|
||||
}
|
||||
|
||||
fn keys(row: &Param) -> Vec<String> {
|
||||
@@ -135,9 +135,9 @@ fn the_dropdown_and_the_command_agree() {
|
||||
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);
|
||||
let unchanged = ParamDialog::remembered(&rows(&app(), 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);
|
||||
let greyed = ParamDialog::remembered(&rows(&app, None, None), &saved);
|
||||
assert_eq!(greyed.text("contents"), None, "dropped once greyed: the row opens on Foreground Color");
|
||||
}
|
||||
@@ -24,7 +24,7 @@ fn quick_mask_on(app: &mut PhotocraftApp, on: bool) {
|
||||
}
|
||||
|
||||
fn row(app: &PhotocraftApp, key: &str) -> Param {
|
||||
rows(app, [0.0; 3], None, None).into_iter().find(|r| r.key == key).unwrap_or_else(|| panic!("a {key} row"))
|
||||
rows(app, None, None).into_iter().find(|r| r.key == key).unwrap_or_else(|| panic!("a {key} row"))
|
||||
}
|
||||
|
||||
/// A field of the open dialog `id`, and whether it takes `value`.
|
||||
|
||||
@@ -22,11 +22,11 @@ pub const COMMAND: &str = "edit.fill";
|
||||
const REMEMBER: &str = "photocraft.dialog.edit.fill";
|
||||
const REMEMBER_INKS: &str = "photocraft.dialog.edit.fill.inks";
|
||||
|
||||
/// The Contents menu: (param, label), in Photoshop's order. Color reads without an ellipsis: choosing it opens nothing.
|
||||
/// The Contents menu: (param, label), in Photoshop's order. Color… opens the Color Picker on every choice (#17470).
|
||||
pub const CONTENTS: [(&str, &str); 9] = [
|
||||
("foreground", "Foreground Color"),
|
||||
("background", "Background Color"),
|
||||
("color", "Color"),
|
||||
("color", "Color…"),
|
||||
("contentAware", "Content-Aware"),
|
||||
("pattern", "Pattern"),
|
||||
("history", "History"),
|
||||
@@ -80,7 +80,7 @@ fn quick_mask_greyed_keys() -> impl Iterator<Item = String> {
|
||||
}
|
||||
|
||||
/// The dialog's rows, with the Preserve Transparency the target forces.
|
||||
fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option<String>, preserve: Option<bool>) -> Vec<Param> {
|
||||
fn rows(app: &PhotocraftApp, first_pattern: Option<String>, preserve: Option<bool>) -> Vec<Param> {
|
||||
let quick_mask = quick_mask(app);
|
||||
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())));
|
||||
@@ -101,9 +101,11 @@ fn rows(app: &PhotocraftApp, foreground: [f32; 3], first_pattern: Option<String>
|
||||
if !greyed.is_empty() {
|
||||
contents = contents.disabled_options(greyed);
|
||||
}
|
||||
// Each choice of Color…, also while it is the current one, opens "Color Picker (Fill Color)" at the colour under
|
||||
// `color`, which has no row of its own (#17470).
|
||||
contents = contents.option_picks_color("color", "color", tl!("Fill Color"));
|
||||
let mut rows = vec![
|
||||
contents,
|
||||
Param::new("color", tl!("Color:"), ParamKind::color(Some(foreground))).visible_when("contents", "color"),
|
||||
Param::new("pattern", tl!("Custom Pattern:"), ParamKind::pattern(first_pattern)).visible_when("contents", "pattern"),
|
||||
// Script lays the pattern out tile by tile in the chosen script's own dialog, opened by OK (#17428).
|
||||
Param::new("script", tl!("Script"), ParamKind::bool(false)).visible_when("contents", "pattern"),
|
||||
@@ -160,11 +162,13 @@ pub fn open(app: &mut PhotocraftApp) -> DialogId {
|
||||
let [r, g, b, _] = app.session.tools.foreground;
|
||||
let first = app.session.patterns.items.first().map(|p| p.id.clone());
|
||||
let patterns = crate::panels::preset_browser::pattern_swatches(app);
|
||||
let rows = rows(app, [r, g, b], first.clone(), preserve);
|
||||
let rows = rows(app, first.clone(), preserve);
|
||||
let inks = crate::color_picker_ui::ink_value(craft::theme::Srgba::rgb(r, g, b), app.session.tools.picked_role_cmyk(0));
|
||||
let mut reset = defaults([r, g, b], first, app.session.tools.picked_role_cmyk(0), preserve);
|
||||
reset.set("contents", opening_contents(app));
|
||||
let mut fields = opening_fields(app.ctx(), COMMAND);
|
||||
// The picker starts on the remembered fill colour, which the dialog keeps over this one; the foreground until then.
|
||||
fields.set("color", FieldValue::Color([r, g, b]));
|
||||
let remembered: Option<DialogFields> = MemoryStage::peek_saved(app.ctx(), &SaveKey::named(REMEMBER_INKS));
|
||||
let remembered_inks = remembered.as_ref().and_then(|saved| saved.get(&crate::color_picker_ui::ink_field("color"))).cloned();
|
||||
fields.set(crate::color_picker_ui::ink_field("color"), remembered_inks.unwrap_or(inks));
|
||||
@@ -316,6 +320,8 @@ fn keep_mode(ctx: &egui::Context, key: &str, mode: String) {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod color_entry_tests;
|
||||
#[cfg(test)]
|
||||
mod content_aware_greyed_tests;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
//! #17470: Color… in Fill's Contents opens "Color Picker (Fill Color)" on the remembered fill colour each time it is
|
||||
//! chosen, also when it is already the current entry; the dialog shows no swatch of the colour, and agents still set it.
|
||||
|
||||
use craft::ui::patterns::form_dialogs::HostedParamDialog;
|
||||
use egui::accesskit::Role;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
use crate::color_picker_ui::{parse_hex, testing as picker, title_for};
|
||||
use crate::dialogs::testing::{cancel, confirm};
|
||||
|
||||
const GREEN: &str = "#33cc66";
|
||||
const BLUE: &str = "#3366ff";
|
||||
|
||||
type H = Harness<'static, Box<PhotocraftApp>>;
|
||||
|
||||
/// A transparent 20×20 document with a red foreground, drawn through `dialogs::run` as the window shows its dialogs.
|
||||
fn harness() -> H {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 20, "height": 20, "background": "transparent"})).unwrap();
|
||||
app.run("tools.setColors", json!({"foreground": "#ff0000"})).unwrap();
|
||||
let h = Harness::builder()
|
||||
.with_context(app.ctx().clone())
|
||||
.with_size(egui::vec2(900.0, 900.0))
|
||||
.build_ui_state(|ui, app: &mut Box<PhotocraftApp>| crate::dialogs::run(app, ui), app);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::AppTheme::pro());
|
||||
h
|
||||
}
|
||||
|
||||
fn open_fill(h: &mut H) -> DialogId {
|
||||
let id = open(h.state_mut());
|
||||
h.run_steps(3);
|
||||
id
|
||||
}
|
||||
|
||||
fn fields(h: &H, id: DialogId) -> DialogFields {
|
||||
h.state().framework.dialogs.get_by_id::<HostedParamDialog>(id).expect("Fill is open").fields().clone()
|
||||
}
|
||||
|
||||
fn set(h: &mut H, id: DialogId, key: &str, value: Value) {
|
||||
let ctx = h.ctx.clone();
|
||||
h.state_mut().framework.dialogs.set_json_field(&ctx, id, key, value).unwrap_or_else(|e| panic!("{key}: {e}"));
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
fn close(a: [f32; 3], b: [f32; 3]) -> bool {
|
||||
a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1.5 / 255.0)
|
||||
}
|
||||
|
||||
fn rgb(hex: &str) -> [f32; 3] {
|
||||
parse_hex(hex).expect("a colour")
|
||||
}
|
||||
|
||||
/// Opens the Contents list and chooses Color… in it, as a click does; returns the Color Picker that opened.
|
||||
fn choose_color(h: &mut H) -> Option<DialogId> {
|
||||
h.get_by_role_and_label(Role::ComboBox, "Contents").click();
|
||||
h.run_steps(2);
|
||||
h.get_by_label("Color…").click();
|
||||
h.run_steps(4);
|
||||
crate::color_picker_ui::top(h.state())
|
||||
}
|
||||
|
||||
/// The colour the open picker `id` shows.
|
||||
fn shown(h: &H, id: DialogId) -> [f32; 3] {
|
||||
picker::current(h.state(), id).expect("the picker shows a colour").color.to_rgb_array()
|
||||
}
|
||||
|
||||
fn pixel(h: &H) -> [f32; 4] {
|
||||
let st = h.state().session.active().unwrap();
|
||||
st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(10, 10)
|
||||
}
|
||||
|
||||
/// Choosing Color… opens the titled picker on top of Fill at the colour Fill holds, and choosing it again while it is
|
||||
/// the current entry opens the picker again; Cancel leaves Contents on Color… and the colour as it was.
|
||||
#[test]
|
||||
fn choosing_color_opens_the_fill_color_picker_every_time() {
|
||||
let mut h = harness();
|
||||
let fill = open_fill(&mut h);
|
||||
set(&mut h, fill, "color", json!(rgb(GREEN)));
|
||||
|
||||
let first = choose_color(&mut h).expect("choosing Color… opens the Color Picker");
|
||||
assert!(h.query_all_by_label(&title_for(tl!("Fill Color"))).next().is_some(), "the picker is titled Color Picker (Fill Color)");
|
||||
assert!(close(shown(&h, first), rgb(GREEN)), "the picker starts on the colour Fill holds");
|
||||
assert_eq!(fields(&h, fill).text("contents"), Some("color"));
|
||||
|
||||
cancel(h.state_mut(), first).unwrap();
|
||||
h.run_steps(3);
|
||||
assert!(crate::color_picker_ui::top(h.state()).is_none(), "Cancel closes the picker");
|
||||
assert_eq!(fields(&h, fill).text("contents"), Some("color"), "Cancel leaves Contents on Color…");
|
||||
assert!(close(fields(&h, fill).color("color").unwrap(), rgb(GREEN)), "Cancel keeps the colour");
|
||||
|
||||
let again = choose_color(&mut h).expect("choosing the current Color… opens the picker again");
|
||||
assert!(close(shown(&h, again), rgb(GREEN)));
|
||||
}
|
||||
|
||||
/// Picker OK sets the colour, Fill OK fills with it and remembers it: Fill reopens on Color… with no picker and no
|
||||
/// swatch row, and the next picker starts on that colour.
|
||||
#[test]
|
||||
fn picker_ok_then_fill_ok_fills_with_the_colour_and_remembers_it() {
|
||||
let mut h = harness();
|
||||
let fill = open_fill(&mut h);
|
||||
let id = choose_color(&mut h).expect("the picker opens");
|
||||
assert!(close(shown(&h, id), [1.0, 0.0, 0.0]), "with nothing remembered the picker starts on the foreground");
|
||||
picker::set_color(h.state_mut(), id, GREEN);
|
||||
confirm(h.state_mut(), id).unwrap();
|
||||
h.run_steps(3);
|
||||
assert!(close(fields(&h, fill).color("color").unwrap(), rgb(GREEN)), "picker OK sets the colour for this fill");
|
||||
confirm(h.state_mut(), fill).unwrap();
|
||||
h.run_steps(3);
|
||||
let [r, g, b, a] = pixel(&h);
|
||||
assert!(close([r, g, b], rgb(GREEN)) && a == 1.0, "Fill OK fills with the picked colour: {:?}", pixel(&h));
|
||||
|
||||
let reopened = open_fill(&mut h);
|
||||
assert!(crate::color_picker_ui::top(h.state()).is_none(), "reopening on Color… opens no picker by itself");
|
||||
assert_eq!(fields(&h, reopened).text("contents"), Some("color"));
|
||||
assert!(h.query_all_by_label("Color:").next().is_none(), "Fill has no colour swatch row");
|
||||
let id = choose_color(&mut h).expect("the picker opens");
|
||||
assert!(close(shown(&h, id), rgb(GREEN)), "the remembered colour is the next start colour");
|
||||
}
|
||||
|
||||
/// Fill Cancel forgets a colour picked in that session: the next picker starts on the last committed one.
|
||||
#[test]
|
||||
fn fill_cancel_forgets_the_picked_colour() {
|
||||
let mut h = harness();
|
||||
let fill = open_fill(&mut h);
|
||||
let id = choose_color(&mut h).expect("the picker opens");
|
||||
picker::set_color(h.state_mut(), id, GREEN);
|
||||
confirm(h.state_mut(), id).unwrap();
|
||||
h.run_steps(3);
|
||||
cancel(h.state_mut(), fill).unwrap();
|
||||
h.run_steps(3);
|
||||
assert_eq!(pixel(&h)[3], 0.0, "nothing is filled");
|
||||
|
||||
open_fill(&mut h);
|
||||
let id = choose_color(&mut h).expect("the picker opens");
|
||||
assert!(close(shown(&h, id), [1.0, 0.0, 0.0]), "the picked colour is forgotten");
|
||||
}
|
||||
|
||||
/// From Foreground Color, a cancelled picker still leaves Contents on Color…, and Fill OK fills with the remembered
|
||||
/// colour; an agent sets the colour without the picker.
|
||||
#[test]
|
||||
fn picker_cancel_from_foreground_fills_with_the_remembered_colour() {
|
||||
let mut h = harness();
|
||||
let fill = open_fill(&mut h);
|
||||
set(&mut h, fill, "contents", json!("color"));
|
||||
assert!(crate::color_picker_ui::top(h.state()).is_none(), "an agent's choice of Color… opens no picker");
|
||||
set(&mut h, fill, "color", json!(rgb(BLUE)));
|
||||
confirm(h.state_mut(), fill).unwrap();
|
||||
h.run_steps(3);
|
||||
h.state_mut().run("edit.undo", json!({})).unwrap();
|
||||
|
||||
let fill = open_fill(&mut h);
|
||||
set(&mut h, fill, "contents", json!("foreground"));
|
||||
let id = choose_color(&mut h).expect("the picker opens");
|
||||
assert!(close(shown(&h, id), rgb(BLUE)), "the picker starts on the remembered colour");
|
||||
cancel(h.state_mut(), id).unwrap();
|
||||
h.run_steps(3);
|
||||
assert_eq!(fields(&h, fill).text("contents"), Some("color"), "Contents stays Color… after Cancel");
|
||||
confirm(h.state_mut(), fill).unwrap();
|
||||
h.run_steps(3);
|
||||
let [r, g, b, _] = pixel(&h);
|
||||
assert!(close([r, g, b], rgb(BLUE)), "Fill OK fills with the remembered colour: {:?}", pixel(&h));
|
||||
}
|
||||
|
||||
/// Fill OK straight after picker OK, with no dialog pass between them (an agent's two requests), fills with the picked
|
||||
/// colour: the picker writes it into Fill at once.
|
||||
#[test]
|
||||
fn fill_ok_right_after_picker_ok_fills_with_the_picked_colour() {
|
||||
let mut h = harness();
|
||||
let fill = open_fill(&mut h);
|
||||
let id = choose_color(&mut h).expect("the picker opens");
|
||||
picker::set_color(h.state_mut(), id, GREEN);
|
||||
confirm(h.state_mut(), id).unwrap();
|
||||
confirm(h.state_mut(), fill).unwrap();
|
||||
h.run_steps(3);
|
||||
let [r, g, b, _] = pixel(&h);
|
||||
assert!(close([r, g, b], rgb(GREEN)), "Fill OK fills with the picked colour: {:?}", pixel(&h));
|
||||
}
|
||||
@@ -20,7 +20,7 @@ fn app(selected: bool) -> Box<PhotocraftApp> {
|
||||
|
||||
/// The Contents row of the dialog `app` would open.
|
||||
fn contents_row(app: &PhotocraftApp) -> Param {
|
||||
rows(app, [0.0; 3], None, None).into_iter().find(|r| r.key == "contents").expect("a Contents row")
|
||||
rows(app, None, None).into_iter().find(|r| r.key == "contents").expect("a Contents row")
|
||||
}
|
||||
|
||||
/// The keys of a choice row, in order.
|
||||
|
||||
@@ -25,7 +25,7 @@ fn app(mode: &str) -> Box<PhotocraftApp> {
|
||||
}
|
||||
|
||||
fn row(app: &PhotocraftApp, key: &str) -> Param {
|
||||
rows(app, [0.0; 3], None, None).into_iter().find(|r| r.key == key).expect("the row")
|
||||
rows(app, None, None).into_iter().find(|r| r.key == key).expect("the row")
|
||||
}
|
||||
|
||||
/// The Mode row's entries as (key, label), and the labels of the enabled ones.
|
||||
|
||||
@@ -314,7 +314,7 @@ fn reset_fill_restores_the_original_native_inks_for_the_same_rgb() {
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
let original = [0., 0., 0., 1.];
|
||||
let reset = defaults([0.; 3], None, Some(original), None);
|
||||
let rows = rows(&app(), [0.; 3], None, None);
|
||||
let rows = rows(&app(), None, None);
|
||||
let fields = Rc::new(RefCell::new(reset.clone()));
|
||||
let shown = fields.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
|
||||
Reference in new issue
Block a user