From be9a07c365ff7b970cbfe013a20e8047e56b135a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 22:06:14 -0800 Subject: [PATCH] Opacity: Control bar, gradient stop and Color Wheel fields keep stepping with the arrow keys An opacity is stored as an f32 fraction, and these fields showed it as f64::from(opacity) * 100.0, so 51% read back as 50.999999%. The number field treats that value as off its step grid, so an Up press went to 51% again and every later press repeated it; Down stalled the same way at values such as 99%. The Control bar's Opacity, the Gradient panel's stop Opacity and the Color Wheel panel's Show Opacity field now show the whole percent when it stores as the same f32 (panels::shown_percent), and the exact percent otherwise. Part of #10903. #10910 fixes the Properties and Transparency Opacity fields the same way. --- crates/ui-egui/src/chrome/control_bar.rs | 2 +- crates/ui-egui/src/chrome/tests.rs | 24 +++++++++++++++++++ crates/ui-egui/src/panels.rs | 9 +++++++ crates/ui-egui/src/panels/color_wheel.rs | 2 +- crates/ui-egui/src/panels/gradient.rs | 2 +- crates/ui-egui/src/panels/gradient/tests.rs | 17 +++++++++++++ .../src/panels/tests_color_wheel_opacity.rs | 15 ++++++++++++ 7 files changed, 68 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/chrome/control_bar.rs b/crates/ui-egui/src/chrome/control_bar.rs index 0c02c32828..266726909a 100644 --- a/crates/ui-egui/src/chrome/control_bar.rs +++ b/crates/ui-egui/src/chrome/control_bar.rs @@ -289,7 +289,7 @@ fn opacity_field(app: &mut VectorcraftApp, ui: &mut Ui, opacity: f32, selected: let target = crate::panels::transparency_target(app); group(ui, rank::OPACITY, |ui| { Field::new(label).side(SideLabel::new().link().scrub(true)).show(ui, |ui, _| { - let number = NumberField::new(label, Bind::given(Some(f64::from(opacity) * 100.0))) + let number = NumberField::new(label, Bind::given(Some(crate::panels::shown_percent(opacity)))) .range(0.0..=100.0) .unit("%") .scrub_by_label() diff --git a/crates/ui-egui/src/chrome/tests.rs b/crates/ui-egui/src/chrome/tests.rs index c52bf2b1a4..24df04ceba 100644 --- a/crates/ui-egui/src/chrome/tests.rs +++ b/crates/ui-egui/src/chrome/tests.rs @@ -3194,6 +3194,30 @@ fn the_control_bar_opacity_drops_an_unfinished_value_when_the_selection_changes( assert_eq!((opacity(&bar, &a), opacity(&bar, &b)), (0.5, 0.01), "the typing gives the other object 1%, not 20+1"); } +/// The arrow keys step the Control bar's Opacity by one percent at every press: 51% reads back from its f32 as +/// 50.999999%, and the next Up still goes to 52%, as 99% going Down goes to 98%. +#[test] +fn the_control_bar_opacity_steps_at_every_arrow_press() { + use egui::{Key, Modifiers}; + crate::i18n::set_current(crate::i18n::Lang::EN); + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let id = app.run("shape.rectangle", json!({"x": 20, "y": 50, "width": 40, "height": 40})).unwrap()["id"].clone(); + let node = vectorcraft_doc::NodeId(id.as_u64().unwrap()); + let mut bar = crate::tests::panel_harness::Panel::new(app, crate::chrome::control_bar); + bar.room(egui::vec2(2400.0, 300.0)).frame(); + for (start, key, steps) in [(50, Key::ArrowUp, [51.0, 52.0, 53.0]), (100, Key::ArrowDown, [99.0, 98.0, 97.0])] { + bar.app().run("transparency.set", json!({"ids": [id], "opacity": start})).unwrap(); + bar.frame(); + bar.click_role("Opacity:", Role::SPIN_BUTTON); + for percent in steps { + bar.key(key, Modifiers::NONE); + let opacity = bar.app().session.active().unwrap().doc.node(node).unwrap().opacity; + assert_eq!(opacity, (percent / 100.0) as f32, "{key:?} from {start}% reaches {percent}%"); + } + } +} + /// File › Revert drops an unfinished Opacity expression in the Control bar, though the reverted document has the same /// object, with the same opacity, selected again before the next frame. #[test] diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 6ca221d315..9ebabb5ba6 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -1262,6 +1262,15 @@ pub(crate) fn whole_steps(range: std::ops::RangeInclusive) -> ChannelRule { rule } +/// An opacity stored as an f32 fraction (0 to 1), as the percent an Opacity field shows: the whole percent when it +/// stores as the same f32, so that an arrow key steps on from it, and the exact percent otherwise. 51 % stores as +/// 0.51f32, which reads back as 50.999999 %; an Up press from there would step to 51 % again. +pub(crate) fn shown_percent(fraction: f32) -> f64 { + let percent = f64::from(fraction) * 100.0; + let whole = percent.round(); + if (whole / 100.0) as f32 == fraction { whole } else { percent } +} + /// A section of a panel or dialog under its heading in label text: the reference app's section headings are regular /// weight, not bold. pub(crate) fn section<'a>(title: impl Into