mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge pull request #19043
Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
commit
80b95b94bf
3 files changed
+154
-5
No files matched your search
@@ -327,6 +327,10 @@ pub(crate) fn params(app: &mut PhotocraftApp, command: &str, spec: &str) -> Vec<
|
||||
let kind = if command == "layer.layerStyle.globalLight" { ParamKind::angle_in(min, max, default, 0) } else { ParamKind::angle(default) };
|
||||
p = Param::new(p.key, p.label, kind);
|
||||
}
|
||||
// Add Frame to Layer uses plain pixel fields without sliders (#17323).
|
||||
if command == "layer.frame.convert" && matches!(p.key.as_str(), "width" | "height") {
|
||||
p = Param::new(p.key, p.label, ParamKind::int(Some(1), Some(300_000), 100)).unit(UnitKind::Pixels);
|
||||
}
|
||||
// Artboard from Layers' Width and Height take 4 to 32000 px; a value typed outside is
|
||||
// clamped when the field is left (#14577).
|
||||
if command == "layer.new.artboardFromLayers" && matches!(p.key.as_str(), "width" | "height") {
|
||||
@@ -795,6 +799,10 @@ pub(crate) fn command_params(command: &str, fields: &DialogFields) -> Value {
|
||||
}
|
||||
}
|
||||
}
|
||||
// An empty Convert to Frame name uses the command's existing layer-name default.
|
||||
if command == "layer.frame.convert" && map.get("name").and_then(Value::as_str) == Some("") {
|
||||
map.remove("name");
|
||||
}
|
||||
// The exports to files take the JPEG quality only for a JPEG (a quality would make a WebP lossy).
|
||||
if matches!(command, "file.export.layersToFiles" | "file.export.layerCompsToFiles") && map.get("format").and_then(Value::as_str) != Some("jpg") {
|
||||
map.remove("quality");
|
||||
|
||||
@@ -114,11 +114,13 @@ fn frame_convert_dialog(app: &mut PhotocraftApp, id: &str) -> Result<Value, Stri
|
||||
let b = photocraft_engine::transform_cmds::transform_frame(&st.doc, layer);
|
||||
let mut fixed = serde_json::Map::new();
|
||||
fixed.insert("layer".into(), json!(layer.id.0));
|
||||
let dialog = crate::filter_dialog::open_with_spec(app, id, tl!("New Frame"), r#"{"name":text,"width":1..300000=100,"height":1..300000=100}"#, fixed);
|
||||
let ctx = app.ctx().clone();
|
||||
for (field, value) in [("__preview", json!(false)), ("name", json!("Frame")), ("width", json!(b.width().max(1))), ("height", json!(b.height().max(1)))] {
|
||||
app.framework.dialogs.set_json_field(&ctx, dialog, field, value).map_err(|e| e.to_string())?;
|
||||
}
|
||||
fixed.insert("name".into(), json!(layer.name));
|
||||
fixed.insert("width".into(), json!(b.width().max(1)));
|
||||
fixed.insert("height".into(), json!(b.height().max(1)));
|
||||
// This command has no preview row. Seed the supported fields before opening so a
|
||||
// failed write to a shell-only field cannot leave the visible defaults in place.
|
||||
let dialog =
|
||||
crate::filter_dialog::open_with_spec(app, id, tl!("Add Frame to Layer"), r#"{"name":text,"width":1..300000=100,"height":1..300000=100}"#, fixed);
|
||||
Ok(dialog_reply(dialog))
|
||||
}
|
||||
|
||||
@@ -911,3 +913,7 @@ fn select_layer_under_cursor(app: &mut PhotocraftApp, ctx: &egui::Context) -> Re
|
||||
#[cfg(test)]
|
||||
#[path = "view_show_grid_extras_off_tests.rs"]
|
||||
mod view_show_grid_extras_off_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "invoke/frame_convert_dialog_tests.rs"]
|
||||
mod frame_convert_dialog_tests;
|
||||
@@ -0,0 +1,135 @@
|
||||
//! #17323: Convert to Frame opens with the layer's name and current bounds.
|
||||
|
||||
use craft::ui::dialog::DialogId;
|
||||
use egui::accesskit::Role;
|
||||
use egui::{Key, vec2};
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use photocraft_doc::LayerId;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::new_layer_ui::testing::{fields, set, top};
|
||||
|
||||
const COMMAND: &str = "layer.frame.convert";
|
||||
type H = Harness<'static, Box<PhotocraftApp>>;
|
||||
|
||||
fn dialog(text: bool) -> (H, DialogId, LayerId) {
|
||||
let mut h = Harness::builder().with_size(vec2(1400.0, 900.0)).with_max_steps(64).build_eframe(move |cc| {
|
||||
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
|
||||
let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.background_jobs = false;
|
||||
app.run("file.new", json!({"width": 600, "height": 400})).unwrap();
|
||||
if text {
|
||||
app.run("type.create", json!({"x": 100, "y": 100, "text": "Text", "size": 28})).unwrap();
|
||||
app.run("layer.setProps", json!({"name": "Text"})).unwrap();
|
||||
} else {
|
||||
app.run("shape.create", json!({"kind": "rect", "rect": [200, 60, 80, 60], "fill": "#e07020"})).unwrap();
|
||||
app.run("layer.setProps", json!({"name": "Rect"})).unwrap();
|
||||
}
|
||||
app
|
||||
});
|
||||
h.run_steps(3);
|
||||
let layer = h.state().session.active().unwrap().active_layer.unwrap();
|
||||
let ctx = h.ctx.clone();
|
||||
let reply = crate::menus::invoke(h.state_mut(), &ctx, COMMAND, json!({})).expect("the dialog opens without an unknown __preview field");
|
||||
let id = top(h.state());
|
||||
assert_eq!(reply["dialog"], id.value().to_string());
|
||||
h.run_steps(4);
|
||||
(h, id, layer)
|
||||
}
|
||||
|
||||
fn frame(app: &PhotocraftApp, id: LayerId) -> &photocraft_doc::Layer {
|
||||
app.session.active().unwrap().doc.layer(id).unwrap()
|
||||
}
|
||||
|
||||
fn history(app: &PhotocraftApp) -> usize {
|
||||
app.session.active().unwrap().history.past_len()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rect_dialog_opens_with_its_name_size_and_plain_fields() {
|
||||
let (h, id, _) = dialog(false);
|
||||
let frame_dialog = h.get_by_role_and_label(Role::Dialog, "Add Frame to Layer");
|
||||
assert_eq!(fields(h.state(), id).text("name"), Some("Rect"));
|
||||
assert_eq!(fields(h.state(), id).number("width"), Some(80.0));
|
||||
assert_eq!(fields(h.state(), id).number("height"), Some(60.0));
|
||||
frame_dialog.get_by_role_and_label(Role::SpinButton, "Width");
|
||||
frame_dialog.get_by_role_and_label(Role::SpinButton, "Height");
|
||||
assert!(frame_dialog.query_all_by_role(Role::Slider).next().is_none());
|
||||
assert!(frame_dialog.query_by_label("Preview").is_none());
|
||||
assert!(fields(h.state(), id).get("__preview").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_dialog_uses_the_existing_transform_bounds() {
|
||||
let (h, id, layer) = dialog(true);
|
||||
let st = h.state().session.active().unwrap();
|
||||
let bounds = photocraft_engine::transform_cmds::transform_frame(&st.doc, st.doc.layer(layer).unwrap());
|
||||
assert_eq!(fields(h.state(), id).text("name"), Some("Text"));
|
||||
assert_eq!(fields(h.state(), id).number("width"), Some(f64::from(bounds.width().max(1))));
|
||||
assert_eq!(fields(h.state(), id).number("height"), Some(f64::from(bounds.height().max(1))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_name_ok_uses_the_layer_name_and_one_undo_step() {
|
||||
for text in [false, true] {
|
||||
let (mut h, id, layer) = dialog(text);
|
||||
set(h.state_mut(), id, "name", json!("")).unwrap();
|
||||
let before = history(h.state());
|
||||
h.get_by_role_and_label(Role::Button, "OK").click();
|
||||
h.run_steps(4);
|
||||
assert!(h.state().framework.dialogs.is_empty());
|
||||
assert!(frame(h.state(), layer).frame.is_some());
|
||||
assert_eq!(frame(h.state(), layer).name, if text { "Text" } else { "Rect" });
|
||||
assert_eq!(history(h.state()), before + 1);
|
||||
h.state_mut().session.undo();
|
||||
assert!(frame(h.state(), layer).frame.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enter_from_name_accepts_the_name_and_requested_size() {
|
||||
let (mut h, id, layer) = dialog(false);
|
||||
set(h.state_mut(), id, "name", json!("Hero")).unwrap();
|
||||
set(h.state_mut(), id, "width", json!(160)).unwrap();
|
||||
set(h.state_mut(), id, "height", json!(100)).unwrap();
|
||||
h.run_steps(2);
|
||||
h.get_by_role_and_label(Role::TextInput, "Name").click();
|
||||
h.key_press(Key::Enter);
|
||||
h.run_steps(4);
|
||||
assert!(h.state().framework.dialogs.is_empty());
|
||||
let layer = frame(h.state(), layer);
|
||||
assert_eq!(layer.name, "Hero");
|
||||
assert_eq!(layer.frame.as_ref().unwrap().transform.m, [160.0, 0.0, 0.0, 100.0, 160.0, 40.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_and_cancel_leave_the_layer_and_history_unchanged() {
|
||||
for escape in [false, true] {
|
||||
let (mut h, id, layer) = dialog(false);
|
||||
let before = history(h.state());
|
||||
set(h.state_mut(), id, "name", json!("Discarded")).unwrap();
|
||||
h.run_steps(2);
|
||||
if escape {
|
||||
h.get_by_role_and_label(Role::TextInput, "Name").click();
|
||||
h.key_press(Key::Escape);
|
||||
} else {
|
||||
h.get_by_role_and_label(Role::Button, "Cancel").click();
|
||||
}
|
||||
h.run_steps(4);
|
||||
assert!(h.state().framework.dialogs.is_empty());
|
||||
assert_eq!(frame(h.state(), layer).name, "Rect");
|
||||
assert!(frame(h.state(), layer).frame.is_none());
|
||||
assert_eq!(history(h.state()), before);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_blank_name_fallback_is_scoped_to_convert_to_frame() {
|
||||
let values = craft::ui::patterns::form_dialogs::DialogFields::from_pairs([("name", "")]);
|
||||
let converted = crate::filter_dialog::command_params(COMMAND, &values);
|
||||
assert!(converted.get("name").is_none());
|
||||
let new_frame = crate::filter_dialog::command_params("layer.new.frame", &values);
|
||||
assert_eq!(new_frame["name"], "");
|
||||
}
|
||||
Reference in new issue
Block a user