Merge pull request #14980 from jayzed80/fix/global-light-angle-range-14677

Global Light: Angle takes -360..360 whole numbers, out-of-range alerts, 180 stays 180 (#14677)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-15 15:51:09 +01:00
commit 386dc75fd1
6 files changed
+268 -7

No files matched your search

+6 -3
View File
@@ -723,9 +723,12 @@ fn global_light(s: &mut Session, p: &Value) -> Result<Value> {
Some(v) if v.is_finite() && v.abs() <= f64::from(f32::MAX) => Ok(v as f32),
Some(_) => Err(bad("layer.layerStyle.globalLight", format!("{key} must be a finite number"))),
};
let angle = f32_param("angle", cur.angle)?;
let raw_angle = f32_param("angle", cur.angle)?;
let altitude = f32_param("altitude", cur.altitude)?.clamp(0.0, 90.0);
let angle = (angle + 180.0).rem_euclid(360.0) - 180.0;
let mut angle = (raw_angle + 180.0).rem_euclid(360.0) - 180.0;
if angle == -180.0 && raw_angle > 0.0 {
angle = 180.0;
}
s.edit("Global Light", |doc, _| {
doc.global_light.angle = angle;
doc.global_light.altitude = altitude;
@@ -1070,7 +1073,7 @@ pub fn specs() -> Vec<CommandSpec> {
"layer.layerStyle.globalLight",
"Global Light…",
["Layer", "Layer Style"],
r##"{"angle":-180..180=120,"altitude":0..90=30}"##,
r##"{"angle":-360..360=120,"altitude":0..90=30}"##,
has_doc,
global_light
),
+3
View File
@@ -456,6 +456,9 @@ fn filter_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, d: &Dialog, fields: &
} else {
first_field_focused(ui, id, |ui| body(ui, fields));
}
if let Some(Value::String(msg)) = fields.remove("__rangeAlert") {
crate::panels::options_bar::range_alert(&mut app.ui, &msg);
}
}
/// The OK / Cancel row (with Apply, Alternate or Reset where the dialog has them), drawn in the
@@ -732,6 +732,69 @@ fn global_light_defaults(app: &PhotocraftApp, fields: &mut Map<String, Value>) {
fields.insert("altitude".into(), json!(st.doc.global_light.altitude));
}
fn filter_whole_int_input(ui: &egui::Ui, field_id: Option<egui::Id>) {
let Some(id) = field_id.filter(|id| ui.memory(|m| m.has_focus(*id))) else { return };
let at_start = egui::TextEdit::load_state(ui.ctx(), id)
.and_then(|s| s.cursor.char_range())
.is_none_or(|r| r.primary.index.min(r.secondary.index) == egui::text::CharIndex(0));
let clean = |s: &str| -> String {
let mut out = String::with_capacity(s.len());
for ch in s.chars() {
if ch.is_ascii_digit() || (ch == '-' && at_start && out.is_empty()) {
out.push(ch);
}
}
out
};
ui.input_mut(|i| {
i.events.retain_mut(|ev| match ev {
egui::Event::Text(text) | egui::Event::Paste(text) => {
*text = clean(text);
!text.is_empty()
}
_ => true,
});
});
}
fn global_light_field(ui: &mut egui::Ui, f: &mut Map<String, Value>, key: &str, value: &mut f32, range: std::ops::RangeInclusive<f32>) {
let t = Tokens::get(ui.ctx());
let slot_id = ui.make_persistent_id(("global-light-field", key));
let last_id = ui.ctx().data(|d| d.get_temp::<egui::Id>(slot_id));
filter_whole_int_input(ui, last_id);
let (field_id, alert) = if key == "angle" {
let (resp, out_of_range) = crate::widgets::angle_dial_row_whole_alerting(ui, "Angle", value, range);
ui.add_space(4.0);
(resp.id, out_of_range)
} else {
let name = tl!("Altitude");
let mut out_of_range = false;
let r = ui
.horizontal(|ui| {
ui.label(egui::RichText::new(name).color(t.text_dim));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let (resp, o) = crate::widgets::value_field_whole_alerting(ui, name, value, range.clone(), "", 74.0);
out_of_range = o;
resp.id
})
.inner
})
.inner;
crate::widgets::slider(ui, value, range, None);
ui.add_space(4.0);
(r, out_of_range)
};
ui.ctx().data_mut(|d| d.insert_temp(slot_id, field_id));
if alert {
let msg = if key == "angle" {
"An integer between -360 and 360 is required. Closest value inserted."
} else {
"An integer between 0 and 90 is required. Closest value inserted."
};
f.insert("__rangeAlert".into(), json!(tl!(msg)));
}
}
/// Proof Setup reflects the active document's current proofing condition, never remembered values.
fn proof_setup_defaults(app: &PhotocraftApp, fields: &mut Map<String, Value>) {
let Some(st) = app.session.active() else { return };
@@ -1187,6 +1250,11 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
f.insert(p.key, json!(count));
continue;
}
if cmd == "layer.layerStyle.globalLight" {
global_light_field(ui, f, &p.key, &mut v, min..=max);
f.insert(p.key, json!(v.round()));
continue;
}
// Replace Color's Range only applies with Localized Color Clusters on, as in Photoshop.
let enabled = !(cmd == REPLACE_COLOR && p.key == "range") || f.get("localized").and_then(Value::as_bool) == Some(true);
let unit = if is_pixel_param_of(&cmd, &p.key) {
@@ -1776,6 +1844,8 @@ impl FilterPreviewKey {
}
}
#[cfg(test)]
mod global_light_angle_range_tests;
#[cfg(test)]
mod tests;
@@ -0,0 +1,162 @@
use egui::{Event, Key, Modifiers, accesskit::Role};
use egui_kittest::{
Harness,
kittest::{NodeT, Queryable},
};
use serde_json::{Value, json};
use crate::{PhotocraftApp, Services, dialogs::show, state::DialogKind};
const CMD: &str = "layer.layerStyle.globalLight";
const ANGLE_ALERT: &str = "An integer between -360 and 360 is required. Closest value inserted.";
const ALTITUDE_ALERT: &str = "An integer between 0 and 90 is required. Closest value inserted.";
fn open() -> (Harness<'static, PhotocraftApp>, u64) {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default());
app.run("file.new", json!({"width": 16, "height": 16})).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1000.0, 700.0)).build_ui_state(|ui, app| show(app, ui.ctx()), app);
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
let ctx = h.ctx.clone();
let id = crate::menus::invoke(h.state_mut(), &ctx, CMD, Value::Null).unwrap()["dialog"].as_u64().unwrap();
h.run_steps(4);
(h, id)
}
fn focus_field(h: &mut Harness<'static, PhotocraftApp>, label: &str) {
let field = h.query_all_by_role(Role::SpinButton).find(|node| node.accesskit_node().label().as_deref() == Some(label)).unwrap();
field.click();
h.run_steps(2);
}
fn type_field(h: &mut Harness<'static, PhotocraftApp>, label: &str, input: &str) {
focus_field(h, label);
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.event(Event::Text(input.into()));
h.run_steps(2);
}
fn field_val(h: &Harness<'_, PhotocraftApp>, id: u64, key: &str) -> f64 {
h.state().ui.dialogs.iter().find(|d| d.id == id).unwrap().fields[key].as_f64().unwrap()
}
fn alerts(h: &Harness<'_, PhotocraftApp>) -> Vec<String> {
h.state().ui.dialogs.iter().filter(|d| d.kind == DialogKind::Error).filter_map(|d| d.fields.get("message")?.as_str().map(String::from)).collect()
}
fn dismiss_top_alert(h: &mut Harness<'static, PhotocraftApp>) {
let id = h.state().ui.dialogs.iter().rev().find(|d| d.kind == DialogKind::Error).unwrap().id;
h.state_mut().ui.close_dialog(id);
h.run_steps(2);
}
#[test]
fn global_light_angle_wraps_into_minus_180_to_180_and_keeps_180() {
for (typed, stored) in [("200", -160.0), ("-200", 160.0), ("359", -1.0), ("360", 0.0), ("180", 180.0)] {
let (mut h, id) = open();
type_field(&mut h, "Angle", typed);
assert_eq!(field_val(&h, id, "angle"), typed.parse::<f64>().unwrap(), "dialog keeps {typed} before OK");
crate::dialogs::confirm(h.state_mut(), id).unwrap();
assert_eq!(h.state().session.active().unwrap().doc.global_light.angle, stored, "confirm {typed}");
}
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default());
app.run("file.new", json!({"width": 16, "height": 16})).unwrap();
for (input, stored) in [(200.0, -160.0), (-200.0, 160.0), (359.0, -1.0), (360.0, 0.0), (180.0, 180.0), (-180.0, -180.0)] {
app.run(CMD, json!({"angle": input})).unwrap();
assert_eq!(app.session.active().unwrap().doc.global_light.angle, stored, "script {input}");
}
}
#[test]
fn global_light_out_of_range_shows_closest_value_alert_and_clamps() {
let (mut h, id) = open();
for (typed, clamped) in [("400", 360.0), ("-400", -360.0)] {
type_field(&mut h, "Angle", typed);
h.key_press(Key::Tab);
h.run_steps(3);
assert_eq!(alerts(&h), vec![ANGLE_ALERT.to_string()], "angle {typed}");
assert_eq!(field_val(&h, id, "angle"), clamped, "angle {typed}");
dismiss_top_alert(&mut h);
}
for (typed, clamped) in [("100", 90.0), ("-5", 0.0)] {
type_field(&mut h, "Altitude", typed);
h.key_press(Key::Tab);
h.run_steps(3);
assert_eq!(alerts(&h), vec![ALTITUDE_ALERT.to_string()], "altitude {typed}");
assert_eq!(field_val(&h, id, "altitude"), clamped, "altitude {typed}");
dismiss_top_alert(&mut h);
}
}
#[test]
fn global_light_fields_accept_whole_numbers_only() {
let (mut h, id) = open();
type_field(&mut h, "Angle", "12.6");
assert_eq!(field_val(&h, id, "angle"), 126.0, "dot stripped in Angle");
type_field(&mut h, "Altitude", "4.5");
assert_eq!(field_val(&h, id, "altitude"), 45.0, "dot stripped in Altitude");
assert!(alerts(&h).is_empty());
}
#[test]
fn global_light_arrow_keys_step_by_1_and_10_stopping_at_ends() {
let (mut h, id) = open();
focus_field(&mut h, "Angle");
h.key_press(Key::ArrowUp);
h.run_steps(2);
assert_eq!(field_val(&h, id, "angle"), 121.0);
h.key_press(Key::ArrowDown);
h.run_steps(2);
assert_eq!(field_val(&h, id, "angle"), 120.0);
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowUp);
h.run_steps(2);
assert_eq!(field_val(&h, id, "angle"), 130.0);
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowDown);
h.run_steps(2);
assert_eq!(field_val(&h, id, "angle"), 120.0);
type_field(&mut h, "Angle", "355");
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowUp);
h.run_steps(2);
assert_eq!(field_val(&h, id, "angle"), 360.0);
h.key_press(Key::ArrowUp);
h.run_steps(2);
assert_eq!(field_val(&h, id, "angle"), 360.0);
type_field(&mut h, "Angle", "-355");
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowDown);
h.run_steps(2);
assert_eq!(field_val(&h, id, "angle"), -360.0);
h.key_press(Key::ArrowDown);
h.run_steps(2);
assert_eq!(field_val(&h, id, "angle"), -360.0);
focus_field(&mut h, "Altitude");
h.key_press(Key::ArrowUp);
h.run_steps(2);
assert_eq!(field_val(&h, id, "altitude"), 31.0);
h.key_press(Key::ArrowDown);
h.run_steps(2);
assert_eq!(field_val(&h, id, "altitude"), 30.0);
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowUp);
h.run_steps(2);
assert_eq!(field_val(&h, id, "altitude"), 40.0);
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowDown);
h.run_steps(2);
assert_eq!(field_val(&h, id, "altitude"), 30.0);
type_field(&mut h, "Altitude", "85");
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowUp);
h.run_steps(2);
assert_eq!(field_val(&h, id, "altitude"), 90.0);
type_field(&mut h, "Altitude", "5");
h.key_press_modifiers(Modifiers::SHIFT, Key::ArrowDown);
h.run_steps(2);
assert_eq!(field_val(&h, id, "altitude"), 0.0);
assert!(alerts(&h).is_empty());
}
+1 -1
View File
@@ -7,7 +7,7 @@ use crate::theme::{self, Tokens};
mod angle_dial;
mod color_count;
pub use angle_dial::{angle_dial_line, angle_dial_row};
pub use angle_dial::{angle_dial_line, angle_dial_row, angle_dial_row_whole_alerting};
pub use color_count::color_count_row;
mod type_ahead;
pub use type_ahead::close_list as close_dropdown_list;
+26 -3
View File
@@ -2,7 +2,7 @@
//! Drop Shadow's light (#882), Motion Blur and the other direction angles of the filter
//! dialogs (#3152) share it, as Photoshop's dialogs do.
use super::{Response, Sense, Stroke, Tokens, Ui, row_label, value_field, vec2};
use super::{Response, Sense, Stroke, Tokens, Ui, row_label, value_field, value_field_whole_alerting, vec2};
/// `Label (dial) [field°]` for a direction in document angles: counter-clockwise from three
/// o'clock, y up. The dial points anywhere in -180..=180; the result is clamped to `range`, and
@@ -12,9 +12,27 @@ pub fn angle_dial_row(ui: &mut Ui, label: &str, angle: &mut f32, range: std::ops
angle_dial_line(ui, label, None, angle, range)
}
/// [`angle_dial_row`] with a whole-number alerting field (`value_field_whole_alerting`),
/// returning the field's response (for focus/id tracking) and whether an out-of-range value was
/// committed.
pub fn angle_dial_row_whole_alerting(ui: &mut Ui, label: &str, angle: &mut f32, range: std::ops::RangeInclusive<f32>) -> (Response, bool) {
angle_dial_line_in(ui, label, None, angle, range, true)
}
/// [`angle_dial_row`] with its label right-aligned in a `label_w` column ([`row_label`]) when
/// given, lined up with the [`slider_line`] rows around it (Layer Style).
pub fn angle_dial_line(ui: &mut Ui, label: &str, label_w: Option<f32>, angle: &mut f32, range: std::ops::RangeInclusive<f32>) -> Response {
angle_dial_line_in(ui, label, label_w, angle, range, false).0
}
fn angle_dial_line_in(
ui: &mut Ui,
label: &str,
label_w: Option<f32>,
angle: &mut f32,
range: std::ops::RangeInclusive<f32>,
whole_alerting: bool,
) -> (Response, bool) {
let t = Tokens::get(ui.ctx());
// The dial clamps with max/min, not `f32::clamp`, which panics if a malformed spec
// reverses the range.
@@ -62,11 +80,16 @@ pub fn angle_dial_line(ui: &mut Ui, label: &str, label_w: Option<f32>, angle: &m
}
}
// The dialogs' own number field: it takes the opening focus (#1757), Tab and arrow steps.
let field = value_field(ui, angle, range, "°", 64.0).accessible_name(tl!(label));
let (field, alert) = if whole_alerting {
value_field_whole_alerting(ui, tl!(label), angle, range, "°", 64.0)
} else {
(value_field(ui, angle, range, "°", 64.0).accessible_name(tl!(label)), false)
};
if field.changed() {
dial.mark_changed();
}
dial | field
let resp = if whole_alerting { field } else { dial | field };
(resp, alert)
})
.inner
}