PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui-egui): Fixed Size marquee width and height take any length unit (#7910)
Korg1ch53c3ff543b3d
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 04e775439..8eabb7540 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -1153,6 +1153,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}ui.spacing_mut().item_spacing.x = 8.0;widgets::vline(ui, 22.0);+ let (units, size) = fixed_size_units(app);let o = &mut app.ui.tool_options;opt_label(ui, tl!("Feather"));widgets::value_field(ui, &mut o.feather, 0.0..=1000.0, "px", 62.0);@@ -1171,15 +1172,29 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {(o.marquee_width, o.marquee_height) = if o.marquee_style == "fixedSize" { (64.0, 64.0) } else { (1.0, 1.0) };}let fixed = sized && o.marquee_style != "normal";- let (unit, range) = if o.marquee_style == "fixedSize" { ("px", 1.0..=300_000.0) } else { ("", 0.001..=999.0) };+ let fixed_size = o.marquee_style == "fixedSize";+ // Fixed Size shows its pixels in the unit picked by right-clicking Width or Height (#7910).+ let units = widgets::FieldUnits { unit: photocraft_engine::prefs::Unit::parse(&o.marquee_unit).unwrap_or_default(), ..units };ui.add_enabled_ui(fixed, |ui| {+ let mut picked = None;opt_label(ui, tl!("Width"));- widgets::value_field(ui, &mut o.marquee_width, range.clone(), unit, 62.0);+ if fixed_size {+ picked = fixed_size_field(ui, &mut o.marquee_width, units, size[0]);+ } else {+ widgets::value_field(ui, &mut o.marquee_width, 0.001..=999.0, "", 62.0);+ }if icons::button(ui, "arrow-left-right", 22.0, false, tl!("Swaps height and width")).clicked() {std::mem::swap(&mut o.marquee_width, &mut o.marquee_height);}opt_label(ui, tl!("Height"));- widgets::value_field(ui, &mut o.marquee_height, range, unit, 62.0);+ if fixed_size {+ picked = fixed_size_field(ui, &mut o.marquee_height, units, size[1]).or(picked);+ } else {+ widgets::value_field(ui, &mut o.marquee_height, 0.001..=999.0, "", 62.0);+ }+ if let Some(name) = picked {+ o.marquee_unit = name.into();+ }});widgets::vline(ui, 22.0);if widgets::secondary_button(ui, tl!("Select and Mask…"), 0.0).clicked() {@@ -1625,6 +1640,40 @@ fn opt_label(ui: &mut egui::Ui, s: &str) {widgets::scrub_option_label(ui, &label);}+/// The largest Fixed Size side, in pixels.+const FIXED_SIZE_MAX_PX: f64 = 300_000.0;++/// The resolution Fixed Size's units convert at (the active document's, 72 ppi without one) and+/// the document's size in pixels, which a percentage is of. The unit is the caller's.+fn fixed_size_units(app: &PhotocraftApp) -> (widgets::FieldUnits, [f64; 2]) {+ let ppi = app.session.prefs().units_and_rulers.point_size.per_inch();+ let (dpi, size) = app+ .session+ .active()+ .map_or((72.0, [1.0, 1.0]), |d| (f64::from(d.doc.resolution_dpi.max(1.0)), [f64::from(d.doc.size.width), f64::from(d.doc.size.height)]));+ (widgets::FieldUnits { unit: photocraft_engine::prefs::Unit::Pixels, dpi, ppi }, size)+}++/// One of Fixed Size's Width and Height (#7910): `px` shown in `units.unit` (a typed unit converts),+/// still stored in pixels. A right-click lists the units, as on the rulers; returns the one picked.+fn fixed_size_field(ui: &mut egui::Ui, px: &mut f32, units: widgets::FieldUnits, extent: f64) -> Option<&'static str> {+ let widgets::FieldUnits { unit, dpi, ppi } = units;+ let to_unit = |px: f64| unit.from_px(px, dpi, extent, ppi) as f32;+ let before = to_unit(f64::from(*px));+ let mut v = before;+ let places = unit.decimals() as u32;+ let resp = widgets::value_field_extent(ui, &mut v, to_unit(1.0)..=to_unit(FIXED_SIZE_MAX_PX), unit.suffix(), 62.0, places, units, extent);+ // Leaving the field re-reads the rounded text it showed (100 px at 72 ppi reads back as+ // 1.389 in); that isn't an edit, so the pixels stay exact.+ let shown = |x: f32| (f64::from(x) * 10f64.powi(places as i32)).round();+ if resp.changed() && shown(v) != shown(before) {+ *px = unit.to_px(f64::from(v), dpi, extent, ppi).clamp(1.0, FIXED_SIZE_MAX_PX) as f32;+ }+ let mut picked = None;+ resp.context_menu(|ui| picked = crate::rulers::unit_menu(ui, unit));+ picked+}+fn percent_field(ui: &mut egui::Ui, label: &str, value: &mut f32, range: std::ops::RangeInclusive<f32>, width: f32) {opt_label(ui, label);let mut percent = *value * 100.0;diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rsindex 974d3fde3..af351b845 100644--- a/crates/ui-egui/src/state.rs+++ b/crates/ui-egui/src/state.rs@@ -605,6 +605,10 @@ pub struct ToolOptions {pub marquee_width: f32,#[serde(default = "one")]pub marquee_height: f32,+ /// The unit Fixed Size's Width and Height show (a [`photocraft_engine::prefs::Unit`] name,+ /// picked by right-clicking them); the size itself stays in pixels (#7910).+ #[serde(default = "default_marquee_unit")]+ pub marquee_unit: String,/// Move tool: Auto-Select (with "layer" or "group" target) and Show Transform Controls./// Auto-Select is on with the "group" target by default, as in current Photoshop;/// ⌘/Ctrl-click inverts it.@@ -738,6 +742,10 @@ fn default_marquee_style() -> String {"normal".into()}+fn default_marquee_unit() -> String {+ "pixels".into()+}+fn one() -> f32 {1.0}@@ -869,6 +877,7 @@ impl Default for ToolOptions {marquee_style: default_marquee_style(),marquee_width: 1.0,marquee_height: 1.0,+ marquee_unit: default_marquee_unit(),move_auto_select: true,move_target: default_move_target(),move_show_transform: false,diff --git a/crates/ui-egui/src/tool_reset.rs b/crates/ui-egui/src/tool_reset.rsindex 3c154d3a4..dfe7abf10 100644--- a/crates/ui-egui/src/tool_reset.rs+++ b/crates/ui-egui/src/tool_reset.rs@@ -157,7 +157,7 @@ pub fn option_keys(tool: Tool) -> &'static [&'static str] {}match tool {Tool::Move => reset!(move_auto_select, move_target, move_show_transform),- Tool::RectMarquee | Tool::EllipseMarquee => reset!(feather, anti_alias, marquee_style, marquee_width, marquee_height),+ Tool::RectMarquee | Tool::EllipseMarquee => reset!(feather, anti_alias, marquee_style, marquee_width, marquee_height, marquee_unit),// The single-pixel marquees are never anti-aliased: Feather is their only option.Tool::SingleRowMarquee | Tool::SingleColumnMarquee => reset!(feather),Tool::Lasso | Tool::PolygonLasso => reset!(feather, anti_alias),