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.
This commit is contained in:
dotCooCoo committed 2026-12-04 22:06:14 -08:00
1 parent ec875e7201
commit be9a07c365
7 files changed
+68 -3

No files matched your search

+1 -1
View File
@@ -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()
+24
View File
@@ -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]
+9
View File
@@ -1262,6 +1262,15 @@ pub(crate) fn whole_steps(range: std::ops::RangeInclusive<f64>) -> 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<Label>) -> Section<'a> {
+1 -1
View File
@@ -1901,7 +1901,7 @@ fn opacity_row(app: &mut VectorcraftApp, panel: &mut WheelUi, ui: &mut Ui, tgt:
// The row reads dim and takes no edit with nothing it can set.
Stack::new().enabled(live).show(ui, |ui, _| {
let rule = super::whole_steps(0.0..=100.0);
let row = ChannelRow::new(tl!("Opacity").to_string(), Bind::given(f64::from(o) * 100.0), rule)
let row = ChannelRow::new(tl!("Opacity").to_string(), Bind::given(super::shown_percent(o)), rule)
.unit(ChannelUnit::Percent)
.track(track.clone())
.checker(true);
+1 -1
View File
@@ -727,7 +727,7 @@ fn host_fields(app: &mut VectorcraftApp, ui: &mut Ui, g: &Gradient, is_grad: boo
Row::new().wrap(true).show(ui, |ui, _| {
Row::new().show(ui, |ui, _| {
Text::new(tl!("Opacity:")).shade(Shade::Muted).show(ui);
let shown = stop.map_or(100.0, |s| f64::from(s.opacity) * 100.0);
let shown = stop.map_or(100.0, |s| super::shown_percent(s.opacity));
let opacity = NumberField::new(tl!("Opacity"), Bind::given(Some(shown)))
.range(0.0..=100.0)
.unit("%")
@@ -361,6 +361,23 @@ fn an_unfinished_stop_value_does_not_reach_another_stop() {
assert_eq!((stops_of(&rig, a)[0].1, stops_of(&rig, b)[0].1), (1.0, 0.01), "the other object's stop takes 1%");
}
/// The arrow keys step a stop'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 a_stops_opacity_steps_at_every_arrow_press() {
let mut app = app(json!({"gradient": {"stops": [{"offset": 0, "color": "#ffffff"}, {"offset": 1, "color": "#000000"}]}}));
app.run("gradient.selectStop", json!({"index": 0})).unwrap();
let mut rig = Rig::new(app);
let opacity = |rig: &Rig| fill(&rig.app()).gradient.stops[0].opacity;
for (start, key, steps) in [("50", Key::ArrowUp, [51.0, 52.0, 53.0]), ("100", Key::ArrowDown, [99.0, 98.0, 97.0])] {
rig.type_into("Opacity", start);
for percent in steps {
rig.fixture.key(key, Modifiers::NONE).settle();
assert_eq!(opacity(&rig), (percent / 100.0) as f32, "{key:?} from {start}% reaches {percent}%");
}
}
}
/// An unfinished value in a freeform point's Opacity or Spread field does not reach the next point selected, and one
/// in Angle does not reach another object's gradient.
#[test]
@@ -132,6 +132,21 @@ fn the_slider_sets_the_fill_opacity_and_takes_the_arrow_keys() {
assert_eq!((fill_opacity(&p.app()) * 100.0).round(), before + 1.0);
}
/// The arrow keys step the row's percent field 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_percent_field_steps_at_every_arrow_press() {
let mut p = Panel::new(app());
for (start, key, steps) in [(50.0, Key::ArrowUp, [51.0, 52.0, 53.0]), (100.0, Key::ArrowDown, [99.0, 98.0, 97.0])] {
p.run("paint.setFill", json!({"opacity": start}));
for percent in steps {
let t = field(&p);
p.fx.access(AccessActionKind::Focus, &t).key(key, Modifiers::NONE).idle(2);
assert_eq!(fill_opacity(&p.app()), (percent / 100.0) as f32, "{key:?} from {start}% reaches {percent}%");
}
}
}
/// Hiding the row during a drag on its slider keeps the opacity the drag previewed as one undo step
/// and leaves no interaction open.
#[test]