mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Color Picker: Add to Swatches asks for the swatch name (#16745)
Add to Swatches in the Color Picker now opens a hosted "Color Swatch Name" dialog: a chip of the new colour and Name:, pre-filled with the first free "Swatch N" and selected on focus. OK adds the swatch under that name (keeping typed CMYK or Lab values) and returns to the picker, which stays open; Cancel and Escape add nothing. A blank name keeps the dialog open. The dialog is listed by ui.inspect and confirmed over the control channel with a `name` field.
This commit is contained in:
1 parent
830316b775
commit
1e87800fa4
4 files changed
+196
-4
No files matched your search
@@ -111,6 +111,7 @@ pub(crate) mod params {
|
||||
},
|
||||
ParamFamily { cancel: Some(filter_dialog::cancel), ..ParamFamily::new(crate::view_cmds::dialogs::is_form, filter_dialog::run) },
|
||||
crate::prefs_ui::PRESETS_FAMILY,
|
||||
crate::swatches_ui::SWATCH_NAME_FAMILY,
|
||||
ParamFamily { check: Some(crate::crop_presets::check), ..ParamFamily::new(crate::crop_presets::owns, crate::crop_presets::run) },
|
||||
crate::actions::dialogs::NEW_SET_FAMILY,
|
||||
crate::actions::dialogs::INSERT_STOP_FAMILY,
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
//! #16745: the Color Picker's Add to Swatches asks for the swatch's name first, in the "Color Swatch Name" dialog: the
|
||||
//! new colour's chip and `Name:`, pre-filled with the first free "Swatch N" and selected so typing replaces it. OK adds
|
||||
//! the swatch under that name and returns to the picker, which stays open; Cancel adds nothing. Over the control
|
||||
//! channel the dialog is listed by `ui.inspect` and confirmed with a `name` field.
|
||||
|
||||
use craft::color_ui::ui::ColorPickerDialog;
|
||||
use egui::vec2;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::*;
|
||||
use crate::control::{ControlRequest, Reply, handle};
|
||||
|
||||
/// A harness running the app's dialog pass, so clicks and keys reach the hosted dialogs.
|
||||
fn harness() -> Harness<'static, Box<PhotocraftApp>> {
|
||||
let app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
let mut h = Harness::builder()
|
||||
.with_context(app.ctx().clone())
|
||||
.with_size(vec2(900.0, 640.0))
|
||||
.build_ui_state(|ui, app: &mut Box<PhotocraftApp>| crate::dialogs::run(app, ui), app);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::AppTheme::studio());
|
||||
h.run_steps(2);
|
||||
h
|
||||
}
|
||||
|
||||
/// Opens the foreground picker on #336699 and clicks its Add to Swatches; returns the picker's id.
|
||||
fn add_to_swatches(h: &mut Harness<'static, Box<PhotocraftApp>>) -> DialogId {
|
||||
let picker = open(h.state_mut(), "foreground");
|
||||
testing::set_color(h.state_mut(), picker, "#336699");
|
||||
h.run_steps(4);
|
||||
h.get_by_label("Add to Swatches").click();
|
||||
h.run_steps(4);
|
||||
picker
|
||||
}
|
||||
|
||||
/// Every swatch as (name, hex).
|
||||
fn swatches(app: &mut PhotocraftApp) -> Vec<(String, String)> {
|
||||
let listed = app.session.execute("swatches.list", json!({})).unwrap();
|
||||
let groups = listed["groups"].as_array().cloned().unwrap_or_default();
|
||||
groups
|
||||
.iter()
|
||||
.flat_map(|g| g["swatches"].as_array().cloned().unwrap_or_default())
|
||||
.map(|s| (s["name"].as_str().unwrap_or_default().to_owned(), s["hex"].as_str().unwrap_or_default().to_owned()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The top hosted dialog as `ui.inspect` lists it.
|
||||
fn top(app: &mut PhotocraftApp) -> Value {
|
||||
let ctx = app.ctx().clone();
|
||||
let (req, _rx) = ControlRequest::new("ui.inspect", json!({}));
|
||||
let Reply::Done(reply) = handle(app, &ctx, &req) else { panic!("ui.inspect replies at once") };
|
||||
reply["result"]["dialogs"].as_array().and_then(|d| d.last().cloned()).unwrap_or(Value::Null)
|
||||
}
|
||||
|
||||
fn call(app: &mut PhotocraftApp, method: &str, params: Value) -> Value {
|
||||
let ctx = app.ctx().clone();
|
||||
let (req, _rx) = ControlRequest::new(method, params);
|
||||
let Reply::Done(reply) = handle(app, &ctx, &req) else { panic!("{method} replies at once") };
|
||||
reply
|
||||
}
|
||||
|
||||
fn picker_open(h: &Harness<'static, Box<PhotocraftApp>>, picker: DialogId) -> bool {
|
||||
h.state().framework.dialogs.get_by_id::<ColorPickerDialog>(picker).is_some()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_to_swatches_asks_for_the_name_and_ok_adds_it_with_the_picker_still_open() {
|
||||
let mut h = harness();
|
||||
let before = swatches(h.state_mut());
|
||||
let picker = add_to_swatches(&mut h);
|
||||
let dialog = top(h.state_mut());
|
||||
assert_eq!(dialog["key"], "colorPicker.swatchName", "{dialog}");
|
||||
assert!(h.query_all_by_label("Color Swatch Name").next().is_some(), "the dialog's title");
|
||||
assert_eq!(dialog["fields"]["name"], "Swatch 1", "pre-filled with the first free number: {dialog}");
|
||||
assert_eq!(swatches(h.state_mut()), before, "nothing is added before OK");
|
||||
assert!(h.get_all_by_role_and_label(egui::accesskit::Role::ColorWell, "Swatch color").next().is_some(), "the chip shows the colour");
|
||||
|
||||
// Enter answers the top dialog's OK; the picker below has its own OK and Cancel.
|
||||
h.key_press(egui::Key::Enter);
|
||||
h.run_steps(4);
|
||||
let after = swatches(h.state_mut());
|
||||
assert_eq!(after.len(), before.len() + 1);
|
||||
assert!(after.contains(&("Swatch 1".to_owned(), "#336699".to_owned())), "{after:?}");
|
||||
assert!(picker_open(&h, picker), "OK returns to the picker, which stays open");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typing_replaces_the_selected_default_name() {
|
||||
let mut h = harness();
|
||||
let picker = add_to_swatches(&mut h);
|
||||
h.event(egui::Event::Text("Sea".into()));
|
||||
h.run_steps(2);
|
||||
h.key_press(egui::Key::Enter);
|
||||
h.run_steps(4);
|
||||
assert!(swatches(h.state_mut()).contains(&("Sea".to_owned(), "#336699".to_owned())), "{:?}", swatches(h.state_mut()));
|
||||
assert!(picker_open(&h, picker));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_adds_nothing_and_keeps_the_picker() {
|
||||
let mut h = harness();
|
||||
let before = swatches(h.state_mut());
|
||||
let picker = add_to_swatches(&mut h);
|
||||
h.key_press(egui::Key::Escape);
|
||||
h.run_steps(4);
|
||||
assert_eq!(swatches(h.state_mut()), before);
|
||||
assert!(picker_open(&h, picker), "Escape returns to the picker");
|
||||
assert_eq!(top(h.state_mut())["id"], json!(picker.value().to_string()), "the name dialog is gone");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_default_is_the_first_free_number() {
|
||||
let mut h = harness();
|
||||
for name in ["Swatch 1", "Swatch 3"] {
|
||||
h.state_mut().run("swatches.add", json!({"color": "#000000", "name": name})).unwrap();
|
||||
}
|
||||
add_to_swatches(&mut h);
|
||||
assert_eq!(top(h.state_mut())["fields"]["name"], "Swatch 2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_control_channel_confirms_it_with_a_name() {
|
||||
let mut h = harness();
|
||||
let picker = add_to_swatches(&mut h);
|
||||
let dialog = top(h.state_mut())["id"].clone();
|
||||
assert_ne!(dialog, json!(picker.value().to_string()), "the name dialog sits above the picker");
|
||||
assert_eq!(call(h.state_mut(), "ui.dialog.set", json!({"dialog": dialog, "field": "name", "value": "Deep Sea"}))["ok"], true);
|
||||
assert_eq!(call(h.state_mut(), "ui.dialog.confirm", json!({"dialog": dialog}))["ok"], true);
|
||||
h.run_steps(2);
|
||||
assert!(swatches(h.state_mut()).contains(&("Deep Sea".to_owned(), "#336699".to_owned())));
|
||||
assert!(picker_open(&h, picker));
|
||||
}
|
||||
|
||||
/// A blank name, which `swatches.add` refuses, keeps the dialog open and adds nothing.
|
||||
#[test]
|
||||
fn a_blank_name_is_refused_and_the_dialog_stays() {
|
||||
let mut h = harness();
|
||||
let before = swatches(h.state_mut());
|
||||
add_to_swatches(&mut h);
|
||||
let dialog = top(h.state_mut())["id"].clone();
|
||||
assert_eq!(call(h.state_mut(), "ui.dialog.set", json!({"dialog": dialog, "field": "name", "value": " "}))["ok"], true);
|
||||
call(h.state_mut(), "ui.dialog.confirm", json!({"dialog": dialog}));
|
||||
h.run_steps(2);
|
||||
assert_eq!(swatches(h.state_mut()), before);
|
||||
assert_eq!(top(h.state_mut())["id"], dialog, "the dialog stays open");
|
||||
}
|
||||
@@ -487,6 +487,10 @@ pub(crate) mod live_preview;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "color_picker_swatch_name_tests.rs"]
|
||||
mod swatch_name_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod screen_integration_tests {
|
||||
use super::*;
|
||||
|
||||
@@ -22,12 +22,16 @@ use craft::color_ui::ui::{SwatchAt, SwatchEdit, SwatchKey, SwatchesCommand, Swat
|
||||
use craft::theme::Srgba;
|
||||
use craft::theme::variant::Tone;
|
||||
use craft::ui::bars::StatusMessage;
|
||||
use craft::ui::contract::{Bind, CommandSet};
|
||||
use craft::ui::choice::Swatch;
|
||||
use craft::ui::contract::{Bind, CommandSet, Show as _};
|
||||
use craft::ui::menu::PanelRow;
|
||||
use craft::ui::patterns::form_dialogs::{DialogFields, Param, ParamDialog, ParamKind, ParamResult};
|
||||
use craft::ui::patterns::presets::{PresetFileRequest, PresetWords};
|
||||
use craft::ui::runtime::commands::{CommandRef, CommandState};
|
||||
use craft::ui::runtime::env::Label;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::dialogs::params::{self, ParamFamily};
|
||||
|
||||
/// Runs engine command `cmd`; a failure goes to the status line and gives `None`.
|
||||
fn run(app: &mut PhotocraftApp, cmd: &str, params: Value) -> Option<Value> {
|
||||
@@ -281,15 +285,47 @@ pub(crate) fn run_fired(app: &mut PhotocraftApp, ctx: &egui::Context, command: &
|
||||
true
|
||||
}
|
||||
|
||||
/// The Color Picker's Add to Swatches: the picked colour, in the model the user typed it in
|
||||
/// (CMYK or Lab values kept by the picker), else RGB.
|
||||
/// The key of the Color Picker's Color Swatch Name dialog (#16745).
|
||||
const SWATCH_NAME: &str = "colorPicker.swatchName";
|
||||
|
||||
/// The Color Swatch Name dialog's result handler.
|
||||
pub(crate) const SWATCH_NAME_FAMILY: ParamFamily =
|
||||
ParamFamily { check: Some(check_swatch_name), ..ParamFamily::new(|key| key == SWATCH_NAME, run_swatch_name) };
|
||||
|
||||
/// The Color Picker's Add to Swatches: asks for the name first in the Color Swatch Name dialog (#16745), which shows
|
||||
/// the colour and offers the first free "Swatch N" selected. The colour is kept in the model the user typed it in
|
||||
/// (CMYK or Lab values kept by the picker), else RGB. The picker stays open.
|
||||
pub fn add_from_picker(app: &mut PhotocraftApp, picked: PickedColor) {
|
||||
let color = match picked.model {
|
||||
Some(m) if m.model == "cmyk" => json!({"model": "cmyk", "values": &m.values[..4]}),
|
||||
Some(m) if m.model == "lab" => json!({"model": "lab", "values": &m.values[..3]}),
|
||||
_ => json!(picked.color.to_hex()),
|
||||
};
|
||||
if let Some(r) = run(app, "swatches.add", json!({"color": color})) {
|
||||
let taken = |name: &str| app.session.presets().swatches.iter().any(|g| g.entries.iter().any(|e| e.name == name));
|
||||
let template = "Swatch {n}";
|
||||
let name = (1u32..).map(|n| crate::i18n::fmt(tl!(template), &[("n", &n.to_string())])).find(|name| !taken(name)).unwrap_or_default();
|
||||
let chip = picked.color;
|
||||
params::open_param_unsaved(app, SWATCH_NAME, DialogFields::new(), json!({"color": color}), move |fields: &mut DialogFields| {
|
||||
let rows = vec![
|
||||
Param::custom("swatch", Label::Empty, move |ui, _fields, _cx| Swatch::new(Label::from(tl!("Swatch color")), chip).show(ui).response).full_width(),
|
||||
Param::new("name", tl!("Name:"), ParamKind::text(name.clone())).select_on_focus(),
|
||||
];
|
||||
ParamDialog::command(SWATCH_NAME, tl!("Color Swatch Name"), rows).values(Bind::bound(fields))
|
||||
});
|
||||
}
|
||||
|
||||
/// Keeps the dialog open on a blank name, which `swatches.add` refuses.
|
||||
fn check_swatch_name(_app: &mut PhotocraftApp, result: &ParamResult) -> Result<(), Label> {
|
||||
match result.fields.text("name") {
|
||||
Some(name) if !name.trim().is_empty() => Ok(()),
|
||||
_ => Err(Label::from(tl!("Enter a name for the swatch."))),
|
||||
}
|
||||
}
|
||||
|
||||
/// OK: adds the colour the dialog opened with under the typed name and selects it.
|
||||
fn run_swatch_name(app: &mut PhotocraftApp, _ctx: &egui::Context, result: ParamResult) {
|
||||
let color = params::captured(app, SWATCH_NAME)["color"].clone();
|
||||
if let Some(r) = run(app, "swatches.add", json!({"color": color, "name": result.fields.text("name")})) {
|
||||
let name = r["name"].as_str().unwrap_or_default().to_string();
|
||||
app.ui.status_error = false;
|
||||
let template = "Added swatch {name}";
|
||||
@@ -389,7 +425,11 @@ mod tests {
|
||||
let dark = app.session.presets().swatches.iter().find(|g| g.name == "Dark").unwrap();
|
||||
assert_eq!(dark.entries.last().unwrap().name, "Color Swatch 1", "into the selected swatch's group");
|
||||
assert_eq!(app.ui.swatches.rename, Some(SwatchKey::Swatch("Color Swatch 1".into())), "renamed in place");
|
||||
// The picker's Add to Swatches asks for the name first (#16745); OK adds it.
|
||||
add_from_picker(&mut app, PickedColor::cmyk(Srgba::rgb(0.0, 1.0, 1.0), [100.0, 0.0, 0.0, 0.0]));
|
||||
crate::dialogs::testing::show(&mut app);
|
||||
let id = app.framework.dialogs.open_dialogs().last().expect("the Color Swatch Name dialog").id;
|
||||
crate::dialogs::testing::confirm(&mut app, id).unwrap();
|
||||
let s = app.session.presets().swatches[0].entries.last().unwrap();
|
||||
assert_eq!(s.color(), Some(&photocraft_engine::presets::swatches::SwatchColor::Cmyk([1.0, 0.0, 0.0, 0.0])));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user