mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #12237 from hectorviov/fix/perspective-crop-field-limits
Perspective Crop: W, H and Resolution clamp to the measured ranges with alerts
This commit is contained in:
4 files changed
+329
-11
No files matched your search
@@ -27,8 +27,9 @@ const CMD: &str = "image.perspectiveCrop";
|
||||
const MAX_SIDE: u32 = 300_000;
|
||||
/// Corner coordinates beyond this are refused (far past any canvas).
|
||||
const MAX_COORD: f64 = 1e7;
|
||||
/// The highest resolution (ppi) the crop may set, as for Image Size.
|
||||
const MAX_RESOLUTION: f64 = 10_000.0;
|
||||
/// The highest resolution (ppi) the crop may set: the Perspective Crop resolution field's limit
|
||||
/// (#11977).
|
||||
const MAX_RESOLUTION: f64 = 29_999.999;
|
||||
|
||||
fn bad(msg: impl Into<String>) -> EngineError {
|
||||
EngineError::BadParams { cmd: CMD.into(), msg: msg.into() }
|
||||
|
||||
@@ -33,6 +33,15 @@ pub const PX_PER_CM: &str = "px/cm";
|
||||
/// The largest number a length or resolution field keeps (well past any canvas or ppi limit,
|
||||
/// which `image.crop` enforces with its own message).
|
||||
const MAX_FIELD: f64 = 1e9;
|
||||
/// Perspective Crop's W and H take 1 to 300000 px, its resolution 1 to 29999.999 px/in (#11977).
|
||||
pub const PCROP_MAX_PX: f64 = 300_000.0;
|
||||
pub const PCROP_MAX_PPI: f64 = 29_999.999;
|
||||
/// Perspective Crop's alerts for a W or H, and a resolution, out of range; `{min}` and `{max}`
|
||||
/// with three decimals and the system's decimal separator.
|
||||
pub const LENGTH_RANGE_ALERT: &str = "A value between {min} {unit} and {max} {unit} is required. Closest value inserted.";
|
||||
pub const RESOLUTION_RANGE_ALERT: &str = "A number between {min} and {max} is required. Closest value inserted.";
|
||||
/// Perspective Crop's alert for a field that doesn't hold a number.
|
||||
pub const INVALID_ENTRY_ALERT: &str = "Invalid numeric entry. Restored last valid value.";
|
||||
|
||||
/// Photoshop's built-in crop presets: (dropdown key, label, width, height, resolution in px/in).
|
||||
pub const PRESETS: &[(&str, &str, &str, &str, &str)] = &[
|
||||
@@ -69,6 +78,12 @@ fn unit_of(suffix: &str) -> Option<Unit> {
|
||||
/// A typed length: a positive number and an optional unit ("4 in", "4in", "10,5 cm", "1024").
|
||||
/// A bare number takes `fallback`. `None` for anything else (empty, zero, negative, unknown unit).
|
||||
pub fn parse_length(text: &str, fallback: Unit) -> Option<(f64, Unit)> {
|
||||
let (v, unit) = parse_number(text, fallback)?;
|
||||
(v > 0.0 && v <= MAX_FIELD).then_some((v, unit))
|
||||
}
|
||||
|
||||
/// A typed number of any sign with an optional unit, as [`parse_length`] reads it.
|
||||
fn parse_number(text: &str, fallback: Unit) -> Option<(f64, Unit)> {
|
||||
let text = text.trim();
|
||||
if text.is_empty() || text.len() > 64 {
|
||||
return None;
|
||||
@@ -77,7 +92,80 @@ pub fn parse_length(text: &str, fallback: Unit) -> Option<(f64, Unit)> {
|
||||
let (num, suffix) = (text.get(..split)?.trim(), text.get(split..)?.trim());
|
||||
let v = crate::widgets::parse_num(&num.replace(',', "."))?;
|
||||
let unit = if suffix.is_empty() { fallback } else { unit_of(suffix)? };
|
||||
(v.is_finite() && v > 0.0 && v <= MAX_FIELD).then_some((v, unit))
|
||||
v.is_finite().then_some((v, unit))
|
||||
}
|
||||
|
||||
/// A limit in an alert: three decimals with the system's decimal separator ("300000.000").
|
||||
fn alert_number(v: f64) -> String {
|
||||
let text = format!("{v:.3}");
|
||||
match crate::i18n::decimal_separator() {
|
||||
Some(',') => text.replace('.', ","),
|
||||
_ => text,
|
||||
}
|
||||
}
|
||||
|
||||
fn unit_name(unit: Unit) -> &'static str {
|
||||
match unit {
|
||||
Unit::Pixels => tl!("Pixels"),
|
||||
Unit::Inches => tl!("Inches"),
|
||||
Unit::Centimeters => tl!("Centimeters"),
|
||||
Unit::Millimeters => tl!("Millimeters"),
|
||||
Unit::Points => tl!("Points"),
|
||||
Unit::Picas => tl!("Picas"),
|
||||
Unit::Percent => tl!("Percent"),
|
||||
}
|
||||
}
|
||||
|
||||
/// What a Perspective Crop W or H field keeps once typed, and the alert to show (#11977): a length
|
||||
/// out of 1 to 300000 px gets the closest one (the limits converted into its unit at `dpi` and
|
||||
/// `pt_per_in`), text that isn't a number the previous value, empty stays empty.
|
||||
pub fn clamp_length(typed: &str, previous: &str, dpi: f64, pt_per_in: f64) -> (String, Option<String>) {
|
||||
if typed.trim().is_empty() {
|
||||
return (String::new(), None);
|
||||
}
|
||||
let Some((v, unit)) = parse_number(typed, field_unit(previous)) else {
|
||||
return (previous.to_string(), Some(tl!(INVALID_ENTRY_ALERT).to_string()));
|
||||
};
|
||||
let (lo, hi) = (unit.from_px(1.0, dpi, 0.0, pt_per_in), unit.from_px(PCROP_MAX_PX, dpi, 0.0, pt_per_in));
|
||||
if !(lo.is_finite() && hi.is_finite() && lo <= hi) {
|
||||
return (previous.to_string(), Some(tl!(INVALID_ENTRY_ALERT).to_string()));
|
||||
}
|
||||
if (lo..=hi).contains(&v) {
|
||||
return (format_length(v, unit), None);
|
||||
}
|
||||
let alert = crate::i18n::fmt(tl!(LENGTH_RANGE_ALERT), &[("min", &alert_number(lo)), ("max", &alert_number(hi)), ("unit", unit_name(unit))]);
|
||||
(format_length(v.clamp(lo, hi), unit), Some(alert))
|
||||
}
|
||||
|
||||
/// What the Perspective Crop resolution field keeps once typed, and the alert to show (#11977):
|
||||
/// 1 to 29999.999 px/in (divided by 2.54 for px/cm), the closest limit outside it.
|
||||
pub fn clamp_resolution(typed: &str, previous: &str, per_cm: bool) -> (String, Option<String>) {
|
||||
if typed.trim().is_empty() {
|
||||
return (String::new(), None);
|
||||
}
|
||||
let Some(v) = crate::widgets::parse_num(&typed.trim().replace(',', ".")).filter(|v| v.is_finite()) else {
|
||||
return (previous.to_string(), Some(tl!(INVALID_ENTRY_ALERT).to_string()));
|
||||
};
|
||||
let (lo, hi) = resolution_limits(per_cm);
|
||||
if (lo..=hi).contains(&v) {
|
||||
return (fit_resolution(v, per_cm), None);
|
||||
}
|
||||
let alert = crate::i18n::fmt(tl!(RESOLUTION_RANGE_ALERT), &[("min", &alert_number(lo)), ("max", &alert_number(hi))]);
|
||||
(fit_resolution(v, per_cm), Some(alert))
|
||||
}
|
||||
|
||||
/// Perspective Crop's resolution range in px/in, or in px/cm (the per-inch limits divided by 2.54).
|
||||
fn resolution_limits(per_cm: bool) -> (f64, f64) {
|
||||
if per_cm { (1.0 / 2.54, PCROP_MAX_PPI / 2.54) } else { (1.0, PCROP_MAX_PPI) }
|
||||
}
|
||||
|
||||
/// A resolution as the Perspective Crop field keeps it: three decimals, so the top limit is kept as
|
||||
/// it is, and clamped to the limits rounded inwards, so the stored text never converts past them
|
||||
/// (Front Image on a 30000 ppi document keeps 29999.999).
|
||||
pub fn fit_resolution(v: f64, per_cm: bool) -> String {
|
||||
let (lo, hi) = resolution_limits(per_cm);
|
||||
let (lo, hi) = ((lo * 1000.0).ceil() / 1000.0, (hi * 1000.0).floor() / 1000.0);
|
||||
crate::widgets::fmt_num_prec(((v * 1000.0).round() / 1000.0).clamp(lo, hi), 3)
|
||||
}
|
||||
|
||||
/// "4 in", "1024 px", "8.5 in", "2.54 cm": the number (up to its unit's decimals) and the unit.
|
||||
@@ -286,7 +374,7 @@ pub fn keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {
|
||||
|
||||
/// A text field for a length or the resolution: what is typed is kept while it has focus and
|
||||
/// normalised into `value` (by `normalize(typed, previous)`) when it loses it. Returns true then.
|
||||
fn text_field(ui: &mut egui::Ui, id: &str, value: &mut String, hint: &str, width: f32, normalize: fn(&str, &str) -> String) -> bool {
|
||||
fn text_field(ui: &mut egui::Ui, id: &str, value: &mut String, hint: &str, width: f32, normalize: impl Fn(&str, &str) -> String) -> bool {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let id = ui.scope_id().with(id);
|
||||
let mut buf = ui.data(|d| d.get_temp::<String>(id)).unwrap_or_else(|| value.clone());
|
||||
@@ -317,15 +405,50 @@ fn text_field(ui: &mut egui::Ui, id: &str, value: &mut String, hint: &str, width
|
||||
/// The W x H x Resolution fields of the options bar: W, ⇄, H, the resolution and its unit.
|
||||
/// Returns true when ⇄ swapped W and H, so the Crop tool turns its frame with them.
|
||||
pub fn fields(o: &mut ToolOptions, ui: &mut egui::Ui) -> bool {
|
||||
text_field(ui, "crop-width", &mut o.crop_width, tl!("Width"), 64.0, normalize_length);
|
||||
fields_with(o, ui, None).0
|
||||
}
|
||||
|
||||
/// The same fields for Perspective Crop, with its limits and alerts (#11977): lengths convert at
|
||||
/// the typed resolution or `doc_dpi`, points and picas at `pt_per_in`. Returns the alert to show.
|
||||
pub fn perspective_fields(o: &mut ToolOptions, ui: &mut egui::Ui, doc_dpi: f64, pt_per_in: f64) -> Option<String> {
|
||||
fields_with(o, ui, Some((doc_dpi, pt_per_in))).1
|
||||
}
|
||||
|
||||
/// The fields; with `limits` (Perspective Crop's document resolution and point size) they clamp
|
||||
/// and return an alert.
|
||||
fn fields_with(o: &mut ToolOptions, ui: &mut egui::Ui, limits: Option<(f64, f64)>) -> (bool, Option<String>) {
|
||||
let alert = std::cell::Cell::new(None);
|
||||
let dpi = limits.map(|(doc_dpi, ppi)| (resolution_ppi(o).unwrap_or(doc_dpi), ppi));
|
||||
let per_cm = o.crop_resolution_unit == PX_PER_CM;
|
||||
let length = |typed: &str, previous: &str| match dpi {
|
||||
Some((dpi, ppi)) => {
|
||||
let (kept, a) = clamp_length(typed, previous, dpi, ppi);
|
||||
if a.is_some() {
|
||||
alert.set(a);
|
||||
}
|
||||
kept
|
||||
}
|
||||
None => normalize_length(typed, previous),
|
||||
};
|
||||
let resolution = |typed: &str, previous: &str| match limits {
|
||||
Some(_) => {
|
||||
let (kept, a) = clamp_resolution(typed, previous, per_cm);
|
||||
if a.is_some() {
|
||||
alert.set(a);
|
||||
}
|
||||
kept
|
||||
}
|
||||
None => normalize_resolution(typed, previous),
|
||||
};
|
||||
text_field(ui, "crop-width", &mut o.crop_width, tl!("Width"), 64.0, length);
|
||||
// Always swaps (Perspective Crop shows the fields without the mode); true in W x H x Resolution
|
||||
// mode, where the Crop tool turns its frame with W and H.
|
||||
let swapped = crate::icons::button(ui, "arrow-left-right", 22.0, false, tl!("Swaps height and width")).clicked() && {
|
||||
swap_fields(o);
|
||||
o.crop_ratio == WHR
|
||||
};
|
||||
text_field(ui, "crop-height", &mut o.crop_height, tl!("Height"), 64.0, normalize_length);
|
||||
text_field(ui, "crop-resolution", &mut o.crop_resolution, tl!("Resolution"), 64.0, normalize_resolution);
|
||||
text_field(ui, "crop-height", &mut o.crop_height, tl!("Height"), 64.0, length);
|
||||
text_field(ui, "crop-resolution", &mut o.crop_resolution, tl!("Resolution"), 64.0, resolution);
|
||||
let units = [(PX_PER_IN.to_string(), "px/in"), (PX_PER_CM.to_string(), "px/cm")];
|
||||
let before = o.crop_resolution_unit.clone();
|
||||
if crate::widgets::dropdown(ui, "crop-resolution-unit", tl!("Resolution"), &mut o.crop_resolution_unit, &units, 64.0) && before != o.crop_resolution_unit {
|
||||
@@ -335,7 +458,7 @@ pub fn fields(o: &mut ToolOptions, ui: &mut egui::Ui) -> bool {
|
||||
o.crop_resolution = crate::widgets::fmt_num2(v);
|
||||
}
|
||||
}
|
||||
swapped
|
||||
(swapped, alert.take())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
//! #11977: Perspective Crop's W, H and Resolution clamp to the reference application's ranges with
|
||||
//! its alerts, text that isn't a number restores the last value with an alert, Clear also resets
|
||||
//! the resolution unit, and the Crop tool's fields keep today's behaviour.
|
||||
|
||||
use egui_kittest::kittest::Queryable;
|
||||
|
||||
use super::*;
|
||||
use crate::crop_size::{clamp_length, clamp_resolution, normalize_length, normalize_resolution};
|
||||
|
||||
const PX_ALERT: &str = "A value between 1.000 Pixels and 300000.000 Pixels is required. Closest value inserted.";
|
||||
const RES_ALERT: &str = "A number between 1.000 and 29999.999 is required. Closest value inserted.";
|
||||
const INVALID: &str = "Invalid numeric entry. Restored last valid value.";
|
||||
|
||||
fn length(typed: &str, previous: &str) -> (String, Option<String>) {
|
||||
crate::i18n::with_decimal_separator(Some('.'), || clamp_length(typed, previous, 72.0, 72.0))
|
||||
}
|
||||
|
||||
fn resolution(typed: &str, previous: &str, per_cm: bool) -> (String, Option<String>) {
|
||||
crate::i18n::with_decimal_separator(Some('.'), || clamp_resolution(typed, previous, per_cm))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn w_and_h_clamp_to_1_and_300000_px_with_the_alert() {
|
||||
assert_eq!(length("-5", "100 px"), ("1 px".to_string(), Some(PX_ALERT.to_string())));
|
||||
assert_eq!(length("0", "100 px"), ("1 px".to_string(), Some(PX_ALERT.to_string())));
|
||||
assert_eq!(length("999999", "100 px"), ("300000 px".to_string(), Some(PX_ALERT.to_string())));
|
||||
assert_eq!(length("1", "100 px"), ("1 px".to_string(), None));
|
||||
assert_eq!(length("300000", ""), ("300000 px".to_string(), None));
|
||||
assert_eq!(length("640", "100 px"), ("640 px".to_string(), None));
|
||||
assert_eq!(length("", "100 px"), (String::new(), None), "empty stays empty, no alert");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_units_clamp_to_the_converted_limits() {
|
||||
// At 72 ppi, 300000 px is 4166.667 in; 1 px is 0.014 in.
|
||||
let (kept, alert) = length("9999 in", "100 px");
|
||||
assert_eq!(kept, "4166.667 in");
|
||||
assert_eq!(alert.as_deref(), Some("A value between 0.014 Inches and 4166.667 Inches is required. Closest value inserted."));
|
||||
assert_eq!(length("4 in", "100 px"), ("4 in".to_string(), None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_that_isnt_a_number_restores_the_last_value() {
|
||||
assert_eq!(length("abc", "120 px"), ("120 px".to_string(), Some(INVALID.to_string())));
|
||||
assert_eq!(length("12 parsecs", "120 px"), ("120 px".to_string(), Some(INVALID.to_string())));
|
||||
assert_eq!(resolution("lots", "300", false), ("300".to_string(), Some(INVALID.to_string())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_resolution_clamps_to_1_and_29999_999_with_its_alert() {
|
||||
assert_eq!(resolution("0", "300", false), ("1".to_string(), Some(RES_ALERT.to_string())));
|
||||
assert_eq!(resolution("99999", "300", false), ("29999.999".to_string(), Some(RES_ALERT.to_string())));
|
||||
assert_eq!(resolution("20000", "300", false), ("20000".to_string(), None), "20000 is kept, and commits (below)");
|
||||
assert_eq!(resolution("72", "", false), ("72".to_string(), None));
|
||||
// px/cm: the per-inch limits divided by 2.54 (not measured beyond that conversion).
|
||||
let (kept, alert) = resolution("99999", "100", true);
|
||||
assert_eq!(kept, "11811.023");
|
||||
assert_eq!(alert.as_deref(), Some("A number between 0.394 and 11811.023 is required. Closest value inserted."));
|
||||
assert_eq!(resolution("0", "100", true).0, "0.394");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_alerts_use_the_system_decimal_separator() {
|
||||
let (_, alert) = crate::i18n::with_decimal_separator(Some(','), || clamp_length("0", "100 px", 72.0, 72.0));
|
||||
assert_eq!(alert.as_deref(), Some("A value between 1,000 Pixels and 300000,000 Pixels is required. Closest value inserted."));
|
||||
let (_, alert) = crate::i18n::with_decimal_separator(Some(','), || clamp_resolution("99999", "300", false));
|
||||
assert_eq!(alert.as_deref(), Some("A number between 1,000 and 29999,999 is required. Closest value inserted."));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_crop_tools_fields_keep_todays_behaviour() {
|
||||
assert_eq!(normalize_length("-5", "100 px"), "100 px");
|
||||
assert_eq!(normalize_length("999999", "100 px"), "999999 px");
|
||||
assert_eq!(normalize_length("abc", "100 px"), "100 px");
|
||||
assert_eq!(normalize_resolution("99999", "300"), "99999");
|
||||
assert_eq!(normalize_resolution("0", "300"), "300");
|
||||
}
|
||||
|
||||
/// A 200 × 150 document with the Perspective Crop tool and a pending quad.
|
||||
fn app() -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 150})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.tool = Tool::PerspectiveCrop;
|
||||
app
|
||||
}
|
||||
|
||||
fn crop(app: &mut PhotocraftApp, resolution: f64) -> Result<Value, String> {
|
||||
let corners = [[10.0, 10.0], [190.0, 10.0], [190.0, 140.0], [10.0, 140.0]];
|
||||
app.run("image.perspectiveCrop", json!({"corners": corners, "resolution": resolution})).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_crop_accepts_up_to_29999_999_ppi() {
|
||||
let mut app = app();
|
||||
assert!(crop(&mut app, 20000.0).is_ok(), "20000 ppi typed into the field commits");
|
||||
let mut app2 = self::app();
|
||||
assert!(crop(&mut app2, 29_999.999).is_ok());
|
||||
let mut app3 = self::app();
|
||||
let err = crop(&mut app3, 30_000.0).unwrap_err();
|
||||
assert!(err.contains("29999.999"), "{err}");
|
||||
}
|
||||
|
||||
/// The options bar in a harness with the app's fonts and theme.
|
||||
fn harness(app: PhotocraftApp) -> egui_kittest::Harness<'static, PhotocraftApp> {
|
||||
let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1400.0, 80.0)).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
}
|
||||
crate::panels::options_bar(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
/// The options bar in a harness, with `typed` committed (Enter) into its `nth` text field (0 = W).
|
||||
fn type_into(app: PhotocraftApp, nth: usize, typed: &str) -> PhotocraftApp {
|
||||
use egui::accesskit::Role;
|
||||
let mut h = harness(app);
|
||||
h.get_all_by_role(Role::TextInput).nth(nth).expect("the field").click();
|
||||
h.run();
|
||||
h.get_all_by_role(Role::TextInput).nth(nth).expect("the field").type_text(typed);
|
||||
h.run();
|
||||
h.key_press(egui::Key::Enter);
|
||||
h.run();
|
||||
h.run();
|
||||
h.into_state()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_out_of_range_width_in_the_options_bar_opens_the_alert() {
|
||||
let app = crate::i18n::with_decimal_separator(Some('.'), || type_into(app(), 0, "999999"));
|
||||
assert_eq!(app.ui.tool_options.crop_width, "300000 px");
|
||||
let messages: Vec<&str> =
|
||||
app.ui.dialogs.iter().filter(|d| d.kind == crate::state::DialogKind::Error).filter_map(|d| d.fields.get("message")?.as_str()).collect();
|
||||
assert_eq!(messages, [PX_ALERT]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_also_resets_the_resolution_unit() {
|
||||
let mut app = app();
|
||||
let o = &mut app.ui.tool_options;
|
||||
o.crop_width = "4 in".into();
|
||||
o.crop_height = "5 in".into();
|
||||
o.crop_resolution = "118.11".into();
|
||||
o.crop_resolution_unit = crate::crop_size::PX_PER_CM.into();
|
||||
let mut h = harness(app);
|
||||
h.get_by_label("Clear").click();
|
||||
h.run();
|
||||
let o = &h.state().ui.tool_options;
|
||||
assert!(o.crop_width.is_empty() && o.crop_height.is_empty() && o.crop_resolution.is_empty());
|
||||
assert_eq!(o.crop_resolution_unit, crate::crop_size::PX_PER_IN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_resolution_never_rounds_past_the_limit() {
|
||||
// The px/cm limit is 11811.0232…: just below it keeps 11811.023, just above it is out of range.
|
||||
assert_eq!(resolution("11811.0232", "100", true), ("11811.023".to_string(), None));
|
||||
assert_eq!(resolution("11811.0236", "100", true).0, "11811.023");
|
||||
assert_eq!(crate::crop_size::fit_resolution(29_999.999_5, false), "29999.999");
|
||||
assert_eq!(crate::crop_size::fit_resolution(118.110_236, true), "118.11");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn front_image_keeps_a_30000_ppi_documents_resolution_within_the_limit() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 150, "resolution": 30000})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.tool = Tool::PerspectiveCrop;
|
||||
let mut h = harness(app);
|
||||
h.get_by_label("Front Image").click();
|
||||
h.run();
|
||||
let app = h.into_state();
|
||||
assert_eq!(app.ui.tool_options.crop_resolution, "29999.999");
|
||||
let ppi = crate::crop_size::resolution_ppi(&app.ui.tool_options).unwrap();
|
||||
let mut app = app;
|
||||
assert!(crop(&mut app, ppi).is_ok());
|
||||
}
|
||||
@@ -459,18 +459,26 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform
|
||||
/// Options bar: W, ⇄, H, the resolution (the Crop tool's fields), Front Image, Clear, Show Grid,
|
||||
/// and cancel / commit.
|
||||
pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
crate::crop_size::fields(&mut app.ui.tool_options, ui);
|
||||
let doc_dpi = app.session.active().map_or(72.0, |st| f64::from(st.doc.resolution_dpi));
|
||||
let pt_per_in = app.session.prefs().units_and_rulers.point_size.per_inch();
|
||||
// Out-of-range numbers and text get the reference application's alerts (#11977).
|
||||
if let Some(message) = crate::crop_size::perspective_fields(&mut app.ui.tool_options, ui, doc_dpi, pt_per_in) {
|
||||
app.ui.open_dialog(crate::state::DialogKind::Error, json!({ "message": message }).as_object().cloned().unwrap_or_default());
|
||||
}
|
||||
if crate::widgets::secondary_button(ui, tl!("Front Image"), 0.0).clicked()
|
||||
&& let Some((size, dpi)) = app.session.active().map(|st| (st.doc.size, f64::from(st.doc.resolution_dpi)))
|
||||
{
|
||||
let o = &mut app.ui.tool_options;
|
||||
o.crop_width = crate::crop_size::format_length(f64::from(size.width), Unit::Pixels);
|
||||
o.crop_height = crate::crop_size::format_length(f64::from(size.height), Unit::Pixels);
|
||||
let res = if o.crop_resolution_unit == crate::crop_size::PX_PER_CM { dpi / 2.54 } else { dpi };
|
||||
o.crop_resolution = crate::widgets::fmt_num2(res);
|
||||
let per_cm = o.crop_resolution_unit == crate::crop_size::PX_PER_CM;
|
||||
// Within the field's limits, so a 30000 ppi document's resolution still commits.
|
||||
o.crop_resolution = crate::crop_size::fit_resolution(if per_cm { dpi / 2.54 } else { dpi }, per_cm);
|
||||
}
|
||||
if crate::widgets::secondary_button(ui, tl!("Clear"), 0.0).clicked() {
|
||||
crate::crop_size::clear(&mut app.ui.tool_options);
|
||||
// Clear also puts the resolution unit back to px/in (#11977).
|
||||
app.ui.tool_options.crop_resolution_unit = crate::crop_size::PX_PER_IN.into();
|
||||
}
|
||||
crate::widgets::vline(ui, 22.0);
|
||||
crate::widgets::checkbox(ui, &mut app.ui.tool_options.perspective_crop_grid, tl!("Show Grid"));
|
||||
@@ -489,6 +497,10 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "perspective_crop_fields_tests.rs"]
|
||||
mod perspective_crop_fields_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "perspective_crop_click_preview_tests.rs"]
|
||||
mod perspective_crop_click_preview_tests;
|
||||
|
||||
Reference in new issue
Block a user