mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 05:14:24 +00:00
Whole-number fields: table counts and warp distortion write whole numbers; a fine key step moves a share of the step only where the field's decimals show it
This commit is contained in:
@@ -302,8 +302,11 @@ impl<'a> WarpFields<'a> {
|
||||
];
|
||||
for (name, value, edit) in fields {
|
||||
Field::new(name.clone()).layout(caption).sizing(Sizing::Content).show(ui, |ui, _| {
|
||||
let field =
|
||||
NumberField::new(name.clone(), Bind::given(Some(value))).range(DISTORTION_RANGE).unit_kind(UnitKind::Percent).show(ui);
|
||||
let field = NumberField::new(name.clone(), Bind::given(Some(value)))
|
||||
.range(DISTORTION_RANGE)
|
||||
.unit_kind(UnitKind::Percent)
|
||||
.fixed_decimals(0)
|
||||
.show(ui);
|
||||
for event in field.edit.events.iter().cloned() {
|
||||
edits.push(edit(event.map(|typed| typed.unwrap_or(value))));
|
||||
}
|
||||
|
||||
@@ -277,7 +277,7 @@ impl<'a> TableProperties<'a> {
|
||||
fn count(ui: &mut Ui, caption: &Label, value: usize, least: usize, change: fn(usize) -> TableChange) -> Option<TableChange> {
|
||||
let mut picked = None;
|
||||
Field::new(caption.clone()).show(ui, |ui, _| {
|
||||
let edit = NumberField::new(caption.clone(), Bind::given(Some(value as f64))).range(least as f64..=f64::INFINITY).step(1.0).show(ui);
|
||||
let edit = NumberField::new(caption.clone(), Bind::given(Some(value as f64))).range(least as f64..=f64::INFINITY).fixed_decimals(0).show(ui);
|
||||
picked = edit.value().copied().flatten().map(|n| change(n.round().max(least as f64) as usize));
|
||||
});
|
||||
picked
|
||||
|
||||
@@ -412,15 +412,17 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
|
||||
/// Arrows, Page keys, the wheel and AccessKit Increment and Decrement step the value, AccessKit sets it, and the
|
||||
/// chevrons open the popups. Up and Down step by the step, with Shift by `LARGE_STEPS` steps and, on a field
|
||||
/// without a dropdown, with Alt by a tenth; on a field with presets or a popup slider Alt+Down opens the dropdown
|
||||
/// instead, and the wheel keeps its Alt fine step. A step starts from the applied value, or from 0 kept in the
|
||||
/// without a dropdown, with Alt by a tenth where the decimals show it (else by the step); on a field with presets
|
||||
/// or a popup slider Alt+Down opens the dropdown instead, so it has no fine key step, and the wheel keeps its Alt
|
||||
/// fine step. A step starts from the applied value, or from 0 kept in the
|
||||
/// range while the field is blank.
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
let rule = self.rule();
|
||||
let fine_shows = self.decimals() >= decimals_for_step(rule.step / LARGE_STEPS);
|
||||
while let Some((mv, mods)) = cx.intents().take_move() {
|
||||
let (notches, scale) = match mv {
|
||||
NavMove::Up => (1, key_scale(mods)),
|
||||
NavMove::Down => (-1, key_scale(mods)),
|
||||
NavMove::Up => (1, key_scale(mods, fine_shows)),
|
||||
NavMove::Down => (-1, key_scale(mods, fine_shows)),
|
||||
NavMove::PageUp => (1, LARGE_STEPS),
|
||||
NavMove::PageDown => (-1, LARGE_STEPS),
|
||||
// AccessKit Increment and Decrement: the text keeps the horizontal arrows, so only those arrive.
|
||||
|
||||
@@ -82,11 +82,12 @@ pub(super) fn round_to(value: f64, decimals: usize) -> f64 {
|
||||
(value * scale).round() / scale
|
||||
}
|
||||
|
||||
/// The scale of a step by an arrow key: Shift moves `LARGE_STEPS` steps, Alt a share of one, both together one.
|
||||
pub(super) fn key_scale(mods: MoveMods) -> f64 {
|
||||
/// The scale of a step by an arrow key: Shift moves `LARGE_STEPS` steps, Alt a share of one where the field's
|
||||
/// decimals show that share (`fine_shows`), both together one.
|
||||
pub(super) fn key_scale(mods: MoveMods, fine_shows: bool) -> f64 {
|
||||
match (mods.extend, mods.fine) {
|
||||
(true, false) => LARGE_STEPS,
|
||||
(false, true) => 1.0 / LARGE_STEPS,
|
||||
(false, true) if fine_shows => 1.0 / LARGE_STEPS,
|
||||
_ => 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ struct Rig {
|
||||
picked: Cell<Option<NumberPick>>,
|
||||
range: Option<RangeInclusive<f64>>,
|
||||
presets: Vec<Entry<NumberPick>>,
|
||||
/// The field writes whole numbers (`fixed_decimals(0)`).
|
||||
whole: bool,
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
@@ -33,14 +35,15 @@ impl Rig {
|
||||
picked: Cell::new(None),
|
||||
range,
|
||||
presets: Vec::new(),
|
||||
whole: false,
|
||||
};
|
||||
rig.idle(3);
|
||||
rig
|
||||
}
|
||||
|
||||
fn frame(&mut self, events: Vec<Event>) {
|
||||
let Rig { frames, value, rect, picked, range, presets } = self;
|
||||
frames.frame(events, |ui| show(ui, value, rect, picked, range, presets));
|
||||
let Rig { frames, value, rect, picked, range, presets, whole } = self;
|
||||
frames.frame(events, |ui| show(ui, value, rect, picked, range, presets, *whole));
|
||||
}
|
||||
|
||||
fn idle(&mut self, frames: usize) {
|
||||
@@ -50,9 +53,9 @@ impl Rig {
|
||||
}
|
||||
|
||||
fn focus(&mut self) {
|
||||
let Rig { frames, value, rect, picked, range, presets } = self;
|
||||
let Rig { frames, value, rect, picked, range, presets, whole } = self;
|
||||
let at = rect.get().center();
|
||||
click(frames, at, &mut |ui| show(ui, value, rect, picked, range, presets));
|
||||
click(frames, at, &mut |ui| show(ui, value, rect, picked, range, presets, *whole));
|
||||
self.idle(2);
|
||||
}
|
||||
|
||||
@@ -73,6 +76,7 @@ fn show(
|
||||
picked: &Cell<Option<NumberPick>>,
|
||||
range: &Option<RangeInclusive<f64>>,
|
||||
presets: &[Entry<NumberPick>],
|
||||
whole: bool,
|
||||
) {
|
||||
let mut guard = value.borrow_mut();
|
||||
let mut field = NumberField::new("Size", Bind::bound(&mut *guard)).id_salt("size");
|
||||
@@ -82,6 +86,9 @@ fn show(
|
||||
if !presets.is_empty() {
|
||||
field = field.presets(presets);
|
||||
}
|
||||
if whole {
|
||||
field = field.fixed_decimals(0);
|
||||
}
|
||||
let out = field.show(ui);
|
||||
rect.set(out.response.rect);
|
||||
if out.preset.is_some() {
|
||||
@@ -105,6 +112,22 @@ fn arrows_step_to_the_next_multiple_and_stay_in_range() {
|
||||
assert_eq!(rig.value(), Some(0.0), "Shift moves ten steps and stops at the start of the range");
|
||||
}
|
||||
|
||||
/// Alt steps a tenth where the field's decimals show it, and a whole step on a whole-number field.
|
||||
#[trial]
|
||||
fn a_fine_step_moves_only_as_far_as_the_decimals_show() {
|
||||
let mut rig = Rig::new(5.0, None);
|
||||
rig.focus();
|
||||
rig.key(Key::ArrowUp, Modifiers::ALT);
|
||||
let fine = rig.value().unwrap_or_default() - 5.0;
|
||||
let mut whole = Rig::new(5.0, None);
|
||||
whole.whole = true;
|
||||
whole.focus();
|
||||
whole.key(Key::ArrowUp, Modifiers::ALT);
|
||||
let plain = whole.value().unwrap_or_default() - 5.0;
|
||||
assert!(fine > 0.0 && fine < 1.0, "Alt moves a share of the step on a field with decimals");
|
||||
assert_eq!(plain, 1.0, "a whole-number field takes the plain step");
|
||||
}
|
||||
|
||||
/// A field of `kind` (plain without one) under `host`'s locale, with `text` typed over its value and committed.
|
||||
fn typed(host: Option<Arc<Host>>, kind: Option<UnitKind>, text: &str) -> Option<f64> {
|
||||
typed_with(host, text, |field| match kind {
|
||||
|
||||
Reference in New Issue
Block a user