Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(ui): Create Rectangle/Ellipse/Triangle/Polygon: right-click Width or Height to pick their unit (#7786)

Korg1chdb2b55f66e1b
diff --git a/crates/ui-egui/src/shape_dialog.rs b/crates/ui-egui/src/shape_dialog.rs
index f643c210c..517fa9024 100644
--- a/crates/ui-egui/src/shape_dialog.rs
+++ b/crates/ui-egui/src/shape_dialog.rs
@@ -2,11 +2,14 @@
//! opens Photoshop's creation dialog instead of drawing. OK runs the same `shape.create` command as
//! a drag, with the options bar's fill and stroke, at the clicked point (or centred on it).
//! Width and height are stored in document pixels (so automation can set them with
-//! `ui.dialog.set`) and shown in the ruler unit.
+//! `ui.dialog.set`) and shown in the ruler unit, or in one picked from the Width and Height
+//! fields' right-click menu (remembered with the other values).
use egui::{Align2, Sense, pos2, vec2};
use serde_json::{Map, Value, json};
+use photocraft_engine::prefs::Unit;
+
use crate::PhotocraftApp;
use crate::state::{DialogKind, Tool};
use crate::theme::Tokens;
@@ -15,7 +18,7 @@ const MARK: &str = "__createShape";
/// Largest side the dialog accepts, in pixels (Photoshop's document limit).
const MAX_PX: f64 = 300_000.0;
/// Remembered values (document pixels; sides and radii come from the options bar).
-const KEYS: [&str; 4] = ["width", "height", "fromCenter", "starRatio"];
+const KEYS: [&str; 5] = ["width", "height", "fromCenter", "starRatio", "unit"];
pub fn owns(fields: &Map<String, Value>) -> bool {
fields.contains_key(MARK)
@@ -40,6 +43,7 @@ fn valid(key: &str, v: &Value) -> bool {
match key {
"width" | "height" => v.as_f64().is_some_and(|x| x.is_finite() && (1.0..=MAX_PX).contains(&x)),
"starRatio" => v.as_f64().is_some_and(|x| x.is_finite() && (1.0..=100.0).contains(&x)),
+ "unit" => v.as_str().is_some_and(|u| Unit::parse(u).is_some()),
_ => v.is_boolean(),
}
}
@@ -68,6 +72,8 @@ pub fn open(app: &mut PhotocraftApp, tool: Tool, at: [f64; 2]) -> Option<u64> {
f.insert("width".into(), json!(100.0));
f.insert("height".into(), json!(100.0));
f.insert("fromCenter".into(), json!(false));
+ // The unit Width and Height show (the ruler unit until one is picked with a right-click).
+ f.insert("unit".into(), json!(app.session.prefs().units_and_rulers.rulers.name()));
match kind {
"rectangle" => {
f.insert("radii".into(), json!([r, r, r, r]));
@@ -160,9 +166,11 @@ fn label(ui: &mut egui::Ui, text: &str) {
pub fn body(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<String, Value>) {
ui.spacing_mut().item_spacing.y = 6.0;
let kind = f.get(MARK).and_then(Value::as_str).unwrap_or("").to_string();
- // Width and height in the ruler unit (percent of the document's side).
+ // Width and height in the dialog's unit (percent of the document's side): the ruler unit, or
+ // the one picked from the fields' right-click menu, as in Photoshop (#7786).
let ur = &app.session.prefs().units_and_rulers;
- let (unit, ppi) = (ur.rulers, ur.point_size.per_inch());
+ let unit = f.get("unit").and_then(Value::as_str).and_then(Unit::parse).unwrap_or(ur.rulers);
+ let ppi = ur.point_size.per_inch();
let (dpi, size) = app
.session
.active()
@@ -174,10 +182,17 @@ pub fn body(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<String, Value>)
let to_unit = |px: f64| unit.from_px(px, dpi, extent, ppi) as f32;
let mut v = to_unit(px);
let places = unit.decimals() as u32;
- if crate::widgets::value_field_prec(ui, &mut v, to_unit(1.0)..=to_unit(MAX_PX), unit.suffix(), 110.0, places).changed() {
+ let resp = crate::widgets::value_field_prec(ui, &mut v, to_unit(1.0)..=to_unit(MAX_PX), unit.suffix(), 110.0, places);
+ if resp.changed() {
let px = unit.to_px(f64::from(v), dpi, extent, ppi).clamp(1.0, MAX_PX);
f.insert(key.into(), json!((px * 1000.0).round() / 1000.0));
}
+ // Right-click: the length units, for both fields (the sizes stay in pixels).
+ resp.context_menu(|ui| {
+ if let Some(name) = crate::rulers::unit_menu(ui, unit) {
+ f.insert("unit".into(), json!(name));
+ }
+ });
});
}
match kind.as_str() {