mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
panels-w4: written from its sheet
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
use egui::{Response, Ui};
|
||||
|
||||
use craft_behavior::IconId;
|
||||
use craft_behavior::geometry::selection_op::SelectionOperation;
|
||||
use craft_ui::bars::{Bar, BarKind};
|
||||
use craft_ui::buttons::{Button, IconButton};
|
||||
use craft_ui::choice::Segmented;
|
||||
@@ -14,6 +13,8 @@ use craft_ui::primitives::IconSpec;
|
||||
use craft_ui::runtime::env::{Env, UnitKind};
|
||||
use craft_ui::text::NumberField;
|
||||
|
||||
use crate::ui::select::operations::{SelectionOpWords, selection_operations};
|
||||
|
||||
use super::lasso::FillAreaEdit;
|
||||
use super::tool::{FillTool, FillToolOptions};
|
||||
use super::words::FillWords;
|
||||
@@ -80,13 +81,7 @@ impl<'a> FillBar<'a> {
|
||||
}
|
||||
}
|
||||
FillTool::Lasso => {
|
||||
let icon = |name: &'static str| IconSpec::svg(IconId::new(name));
|
||||
let operations = [
|
||||
Entry::new(SelectionOperation::New, words.new_selection.clone()).icon(icon("square")),
|
||||
Entry::new(SelectionOperation::Add, words.add_to_selection.clone()).icon(icon("plus")),
|
||||
Entry::new(SelectionOperation::Subtract, words.subtract_from_selection.clone()).icon(icon("minus")),
|
||||
Entry::new(SelectionOperation::Intersect, words.intersect_with_selection.clone()).icon(icon("squares_subtract")),
|
||||
];
|
||||
let operations = selection_operations(&env.words::<SelectionOpWords>());
|
||||
let picked = Segmented::new(words.options.clone(), Bind::given(options.lasso), &operations).show(ui).value().copied();
|
||||
if let Some(operation) = picked {
|
||||
options.lasso = operation;
|
||||
@@ -101,7 +96,7 @@ impl<'a> FillBar<'a> {
|
||||
if let Some(by) = number(ui, &words.grow_by, options.grow_by, min..=max) {
|
||||
options.grow_by = by;
|
||||
}
|
||||
if clicked(&IconButton::new(icon("undo")).label(reset.clone()).id_salt("reset").show(ui)) {
|
||||
if clicked(&IconButton::new(IconSpec::svg(IconId::new("undo"))).label(reset.clone()).id_salt("reset").show(ui)) {
|
||||
reset_fill_area = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,6 @@ craft_ui::words! {
|
||||
add: "fill.add" = "Add";
|
||||
subtract: "fill.subtract" = "Subtract";
|
||||
size: "fill.size" = "Size";
|
||||
new_selection: "fill.newSelection" = "New selection";
|
||||
add_to_selection: "fill.addToSelection" = "Add to selection";
|
||||
subtract_from_selection: "fill.subtractFromSelection" = "Subtract from selection";
|
||||
intersect_with_selection: "fill.intersectWithSelection" = "Intersect with selection";
|
||||
/// The caption of the Expand and Contract amount in the options bar.
|
||||
grow_by: "fill.growBy" = "By";
|
||||
expand: "fill.expand" = "Expand";
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
//! The four selection operations as entries for a `Segmented`: New, Add, Subtract and Intersect.
|
||||
|
||||
use craft_behavior::IconId;
|
||||
use craft_behavior::geometry::selection_op::SelectionOperation;
|
||||
use craft_ui::contract::Entry;
|
||||
use craft_ui::primitives::IconSpec;
|
||||
|
||||
craft_ui::words! {
|
||||
/// The selection operations' words, English by default (Photoshop's names).
|
||||
@@ -15,6 +17,11 @@ craft_ui::words! {
|
||||
|
||||
/// The four entries (icons `square`, `plus`, `minus`, `squares_subtract`) for a `Segmented`.
|
||||
pub fn selection_operations(words: &SelectionOpWords) -> [Entry<SelectionOperation>; 4] {
|
||||
let _ = words;
|
||||
todo!("pn.C19")
|
||||
let icon = |name: &'static str| IconSpec::svg(IconId::new(name));
|
||||
[
|
||||
Entry::new(SelectionOperation::New, words.new_selection.clone()).icon(icon("square")),
|
||||
Entry::new(SelectionOperation::Add, words.add_to_selection.clone()).icon(icon("plus")),
|
||||
Entry::new(SelectionOperation::Subtract, words.subtract_from_selection.clone()).icon(icon("minus")),
|
||||
Entry::new(SelectionOperation::Intersect, words.intersect_with_selection.clone()).icon(icon("squares_subtract")),
|
||||
]
|
||||
}
|
||||
|
||||
@@ -338,17 +338,28 @@ impl<C: Component> Cx<'_, C> {
|
||||
}
|
||||
|
||||
impl<C: Component> Cx<'_, C> {
|
||||
/// Advances the component's wheel run, one edit gesture through the gesture slot: a `WheelPhase::End` share commits
|
||||
/// a running run, any other notch with `Some(next)` proposes it.
|
||||
#[allow(dead_code)] // P3(panels-w4): pn.C23
|
||||
/// Advances the component's wheel run, one edit gesture through the gesture slot: a notch with `Some(next)`
|
||||
/// proposes it (the first notch begins the run), and a `WheelPhase::End` share, which the runtime hands a value
|
||||
/// control when the wheel gesture ends, commits a running run. A run a host change or a removal cancelled stays
|
||||
/// cancelled; the end then changes nothing.
|
||||
pub fn wheel_run(&mut self, phase: WheelPhase, next: Option<ValueOf<C>>) {
|
||||
let _ = (phase, next);
|
||||
todo!("pn.C23")
|
||||
if let Some(next) = next {
|
||||
let running = self.gesture_slot().is_running();
|
||||
self.propose(next);
|
||||
let slot = self.gesture_slot();
|
||||
// Only a run the wheel began is a wheel run: a notch inside a press-carried gesture joins that gesture.
|
||||
if !running && slot.is_running() {
|
||||
slot.wheel = true;
|
||||
}
|
||||
}
|
||||
if phase == WheelPhase::End && self.in_wheel_run() {
|
||||
self.commit();
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the running gesture is a wheel run.
|
||||
#[allow(dead_code)] // P3(panels-w4): pn.C23
|
||||
pub fn in_wheel_run(&self) -> bool {
|
||||
todo!("pn.C23")
|
||||
let slot = self.core.memory.recall::<Gesture<ValueOf<C>>>(gesture_key(self.core.id));
|
||||
slot.is_running() && slot.wheel
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,9 +211,11 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
}
|
||||
|
||||
/// Zoom typing: a Tab rewrites the draft and a press on the field's other parts keeps it unapplied.
|
||||
#[allow(dead_code)] // P3(panels-w4): cw.C59
|
||||
pub(super) fn zoom_typing(self) -> Self {
|
||||
todo!("cw.C59")
|
||||
#[must_use]
|
||||
pub(super) fn zoom_typing(mut self) -> Self {
|
||||
self.options.tab_rewrites = true;
|
||||
self.options.holds_drafts = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// A drag on the field's side label scrubs the value too, with or without the focus in the field.
|
||||
@@ -229,17 +231,16 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
Rule::new(range.as_ref(), self.step)
|
||||
}
|
||||
|
||||
/// Reads what the field keeps from the env: a length field's scale from stored to shown, and the symbol of its unit
|
||||
/// kind after the number unless the field sets a unit.
|
||||
/// Reads a length field's scale from stored to shown from the env.
|
||||
fn settle_lengths(&mut self, units: &Units) {
|
||||
if let Some(lengths) = self.lengths {
|
||||
self.scale = units.length_scale(lengths).unwrap_or(1.0);
|
||||
}
|
||||
if self.unit.is_none()
|
||||
&& let Some(kind) = self.unit_kind
|
||||
{
|
||||
self.unit = Some(units.symbol(kind).into());
|
||||
}
|
||||
}
|
||||
|
||||
/// The unit shown after the number: the field's own, else the symbol of its unit kind.
|
||||
fn shown_unit(&self, units: &Units) -> Option<Label> {
|
||||
self.unit.clone().or_else(|| self.unit_kind.map(|kind| units.symbol(kind).into()))
|
||||
}
|
||||
|
||||
/// `stored` in the shown unit.
|
||||
@@ -275,12 +276,19 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The decimals the field shows: its codec's, else its fixed decimals, else its unit kind's.
|
||||
fn shown_decimals(&self) -> RangeInclusive<usize> {
|
||||
match (&self.codec, self.fixed_decimals, self.unit_kind) {
|
||||
(Some(codec), _, _) => 0..=codec.decimals(),
|
||||
(None, Some(fixed), _) => fixed..=fixed,
|
||||
(None, None, Some(kind)) => kind.decimals(),
|
||||
(None, None, None) => 0..=DEFAULT_DECIMALS,
|
||||
}
|
||||
}
|
||||
|
||||
/// The most decimals the field writes.
|
||||
fn decimals(&self) -> usize {
|
||||
self.codec.as_ref().map_or_else(
|
||||
|| self.fixed_decimals.unwrap_or_else(|| self.unit_kind.map_or(DEFAULT_DECIMALS, |kind| *kind.decimals().end())),
|
||||
NumberCodec::decimals,
|
||||
)
|
||||
*self.shown_decimals().end()
|
||||
}
|
||||
|
||||
/// The names the presets list holds that a value of this type can be: the labels of its `Choice` rows.
|
||||
@@ -324,13 +332,13 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
/// content-sized field's unit is as wide as the widest of its column group's.
|
||||
fn after_text(&self, cx: &MeasureCx<'_, Self>, chevrons: &[(PartKey, Label)]) -> Vec<Adornment> {
|
||||
let shared = content_sized(cx);
|
||||
let unit = self.unit.iter().map(|unit| {
|
||||
let unit = Adornment::text(unit.clone(), cx.style.unit.role).apart().measured(cx);
|
||||
let unit = self.shown_unit(&cx.env.units()).map(|unit| {
|
||||
let unit = Adornment::text(unit, cx.style.unit.role).apart().measured(cx);
|
||||
if shared { unit.in_column(cx.ui.ctx(), &UNITS) } else { unit }
|
||||
});
|
||||
let chevrons =
|
||||
chevrons.iter().map(|(key, name)| Adornment::button(*key, crate::controls::disclosure::DisclosureGlyph::dropdown(), name.clone()));
|
||||
unit.chain(chevrons).collect()
|
||||
unit.into_iter().chain(chevrons).collect()
|
||||
}
|
||||
|
||||
/// The inside label's glyph, measured: what sits before the text.
|
||||
@@ -348,6 +356,21 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
fn laid_out(&self, style: &NumberStyle, rect: Rect, rtl: bool) -> Placed {
|
||||
place(rect, &style.field, &self.leading, &self.trailing, rtl)
|
||||
}
|
||||
|
||||
/// The alert for `typed`, a typed number the rule held to its range, in shown units; `kept` is the committed
|
||||
/// value, in shown units too.
|
||||
fn range_alert(&self, typed: f64, kept: Option<f64>) -> RangeAlert {
|
||||
let rule = self.rule();
|
||||
RangeAlert {
|
||||
typed,
|
||||
kept: kept.unwrap_or_else(|| rule.settle(typed)),
|
||||
range: rule.bounds(),
|
||||
whole: self.commit_decimals == Some(0) || self.decimals() == 0,
|
||||
unit: self.unit.clone(),
|
||||
unit_kind: self.unit_kind,
|
||||
decimals: self.shown_decimals(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
const ROLE: Role = Role::SPIN_BUTTON;
|
||||
@@ -488,8 +511,9 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
/// 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 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.
|
||||
/// fine step. A step starts from the applied value (the text part applies a typed draft first), or from 0 kept in
|
||||
/// the range while the field is blank. A wheel run is one edit gesture that the wheel gesture's end commits; a key
|
||||
/// or an AccessKit set during the run commits it first and steps from the value it showed.
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
// A drag from the slider's chevron is one gesture with the slider it opened: its release closes the slider.
|
||||
if cx.part_if_declared(SLIDER).is_some_and(|part| matches!(part.press.phase, PressPhase::Release { click: false, .. })) {
|
||||
@@ -508,6 +532,9 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
NavMove::Back => (-1, 1.0),
|
||||
_ => continue,
|
||||
};
|
||||
if cx.in_wheel_run() {
|
||||
cx.commit();
|
||||
}
|
||||
let from = self.shown_number(cx.value()).unwrap_or_else(|| rule.blank());
|
||||
let next = match &self.stepper {
|
||||
Some(stepper) => rule.settle(stepper.next(from, notches)),
|
||||
@@ -518,15 +545,25 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
if let Some(wheel) = cx.take_wheel() {
|
||||
let from = self.shown_number(cx.value()).unwrap_or_else(|| rule.blank());
|
||||
let steps = ValueSteps::new(from, rule.low, rule.high, rule.step, rule.step * LARGE_STEPS).fine(rule.step / LARGE_STEPS);
|
||||
if let StepResult::Set(next) = wheel_step(&steps, ValueAxes::Both, &wheel, cx.env.direction().is_rtl()) {
|
||||
let next = match &self.stepper {
|
||||
Some(stepper) if next != from => stepper.next(from, if next > from { 1 } else { -1 }),
|
||||
_ => round_to(next, decimals_for_step(rule.step / LARGE_STEPS)),
|
||||
};
|
||||
cx.set(V::of_number(self.to_stored(rule.settle(next))));
|
||||
}
|
||||
// A notch that changes the value proposes it; the runtime's zero-scroll end share changes none and ends
|
||||
// the run.
|
||||
let next = match wheel_step(&steps, ValueAxes::Both, &wheel, cx.env.direction().is_rtl()) {
|
||||
StepResult::Set(next) => {
|
||||
let next = match &self.stepper {
|
||||
Some(stepper) if next != from => stepper.next(from, if next > from { 1 } else { -1 }),
|
||||
_ => round_to(next, decimals_for_step(rule.step / LARGE_STEPS)),
|
||||
};
|
||||
let next = rule.settle(next);
|
||||
(next != from).then(|| V::of_number(self.to_stored(next)))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
cx.wheel_run(wheel.phase, next);
|
||||
}
|
||||
if let Some(next) = cx.intents().take_set_value() {
|
||||
if cx.in_wheel_run() {
|
||||
cx.commit();
|
||||
}
|
||||
cx.set(V::of_number(self.to_stored(rule.settle(next))));
|
||||
}
|
||||
}
|
||||
@@ -563,7 +600,7 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
let chevrons = self.chevrons(&cx.words::<TextWords>());
|
||||
let placed = self.laid_out(cx.style, cx.content_rect, rtl);
|
||||
// A mixed value shows the dash alone: a unit beside it would claim a measure the selection does not have.
|
||||
if self.unit.is_some()
|
||||
if self.shown_unit(&cx.env.units()).is_some()
|
||||
&& cx.value().is_some_and(|value| !non_finite(Some(value)))
|
||||
&& let (Some(unit), Some(at)) = (self.trailing.first(), placed.trailing.first())
|
||||
{
|
||||
@@ -577,13 +614,24 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The value as the field shows it: its ruled codec's text, in the shown unit, a name as its presets row's label.
|
||||
fn word_value(&self, value: &V, cx: &Cx<'_, Self>) -> Option<String> {
|
||||
let names = self.names(|row| cx.env.resolve(&row.label().clone()));
|
||||
Some(self.ruled(&cx.env.units(), false, names).format(value))
|
||||
}
|
||||
|
||||
/// The edit, the preset picked, and what the committed text's parse learned: its unit, whether the range held it
|
||||
/// (only a typed commit carries a note, so steps, the wheel and a scrub never report out of range) and whether
|
||||
/// the text was cut.
|
||||
fn finish(self, cx: Cx<'_, Self>) -> NumberOutput<V> {
|
||||
let committed = cx.text(&text_part::<V>()).and_then(|outcome| outcome.committed);
|
||||
let out_of_range = committed.and_then(|note| note.held).map(|typed| self.range_alert(typed, self.shown_number(cx.value())));
|
||||
NumberOutput {
|
||||
edit: cx.edit_response(),
|
||||
preset: self.picked,
|
||||
typed_unit: cx.text(&text_part::<V>()).and_then(|outcome| outcome.committed).and_then(|note| note.unit).and_then(UnitKind::from_tag), // P3(panels-w4): pn.N1
|
||||
out_of_range: None, // P3(panels-w4): pn.C22
|
||||
cut: false, // P3(panels-w4): cw.C59
|
||||
typed_unit: committed.and_then(|note| note.unit).and_then(UnitKind::from_tag),
|
||||
out_of_range,
|
||||
cut: committed.is_some_and(|note| note.cut),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,18 +166,21 @@ impl<V: NumberValue> Codec for Ruled<V> {
|
||||
self.parse_tagged(text, start).map(|(value, _)| value)
|
||||
}
|
||||
|
||||
/// The inner codec's note, with `held` set to the number read (in shown units) when it lies outside the range;
|
||||
/// snapping and rounding are not holding. A name or a blank gives the default note.
|
||||
fn parse_tagged(&self, text: &str, start: &V) -> Result<(V, ParseNote), NotAValue> {
|
||||
// P3(panels-w4): cw.N5
|
||||
if let Some(name) = self.name_of(text) {
|
||||
return Ok((name, ParseNote::default()));
|
||||
}
|
||||
if text.trim().is_empty() && (self.mixed || V::blank().is_none()) {
|
||||
return Err(NotAValue);
|
||||
}
|
||||
// P3(panels-w4): cw.N5 sets `held` when the number lies outside the rule's range.
|
||||
let (read, tag) = self.inner.parse_tagged(text, &self.shown(start))?;
|
||||
let (read, mut note) = self.inner.parse_tagged(text, &self.shown(start))?;
|
||||
match read {
|
||||
Some(number) => Ok((V::of_number(self.held(number) / self.scale), tag)),
|
||||
Some(number) => {
|
||||
note.held = (number < self.rule.low || number > self.rule.high).then_some(number);
|
||||
Ok((V::of_number(self.held(number) / self.scale), note))
|
||||
}
|
||||
None => V::blank().map(|blank| (blank, ParseNote::default())).ok_or(NotAValue),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,13 +153,11 @@ pub struct RangeAlert {
|
||||
pub range: RangeInclusive<f64>,
|
||||
/// The field commits whole numbers.
|
||||
pub whole: bool,
|
||||
/// The unit the field shows after its number: the field's own `unit` prop.
|
||||
/// The field's own unit label (its `unit` prop); `None` when only a unit kind names the unit.
|
||||
pub unit: Option<Label>,
|
||||
/// The field's unit kind, for the one number-with-unit rule (`Units::text`).
|
||||
// P3(panels-w4): pn.C22 `RangeAlert::unit_kind`
|
||||
/// What the number counts, which words the range's ends with their unit.
|
||||
pub unit_kind: Option<UnitKind>,
|
||||
/// The decimals the field shows, for the alert's `low` and `high`.
|
||||
// P3(panels-w4): pn.C22 `RangeAlert::decimals`
|
||||
/// The decimals the field shows, which the range's ends are written at.
|
||||
pub decimals: RangeInclusive<usize>,
|
||||
}
|
||||
|
||||
|
||||
@@ -379,6 +379,168 @@ fn the_digits_sit_on_the_field_centre_line() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A typed value past the range commits the range's end and reports it in that pass only, for Enter, Tab and a focus
|
||||
/// loss to another control's press; arrows, the wheel and a scrub that run into the end never report it.
|
||||
#[trial]
|
||||
fn a_typed_value_past_the_range_commits_the_end_and_reports_it_once() {
|
||||
use craft_ui::testing::Fixture;
|
||||
use craft_ui::testing::pointer::{moved, wheel_lines};
|
||||
use craft_ui::text::RangeAlert;
|
||||
use std::rc::Rc;
|
||||
let value = Rc::new(Cell::new(Some(50.0)));
|
||||
let alerts: Rc<RefCell<Vec<RangeAlert>>> = Rc::default();
|
||||
let (held, seen) = (value.clone(), alerts.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut now = held.get();
|
||||
let out = NumberField::new("Amount", Bind::bound(&mut now))
|
||||
.id_salt("amount")
|
||||
.range(0.0..=100.0)
|
||||
.unit_kind(UnitKind::Percent)
|
||||
.commit_decimals(0)
|
||||
.show(ui);
|
||||
held.set(now);
|
||||
seen.borrow_mut().extend(out.out_of_range.clone());
|
||||
NumberField::new("Other", Bind::bound(&mut Some(1.0))).id_salt("other").show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let amount = fixture.by_label("Amount").expect("the field");
|
||||
let other = fixture.by_label("Other").expect("the other field");
|
||||
let type_over = |fixture: &mut Fixture, text: &str| {
|
||||
fixture.click(&amount).settle();
|
||||
fixture.key(Key::A, Modifiers::COMMAND).type_text(text);
|
||||
};
|
||||
|
||||
type_over(&mut fixture, "250");
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!(value.get(), Some(100.0), "a typed value past the end commits the end");
|
||||
{
|
||||
let alerts = alerts.borrow();
|
||||
assert_eq!(alerts.len(), 1, "Enter reports it in one pass: {alerts:?}");
|
||||
let alert = &alerts[0];
|
||||
assert_eq!((alert.typed, alert.kept, alert.range.clone()), (250.0, 100.0, 0.0..=100.0));
|
||||
assert!(alert.whole, "the field commits whole numbers");
|
||||
assert_eq!(alert.unit, None, "the field sets no unit label of its own");
|
||||
assert_eq!(alert.unit_kind, Some(UnitKind::Percent));
|
||||
}
|
||||
|
||||
type_over(&mut fixture, "-5");
|
||||
fixture.key(Key::Tab, Modifiers::NONE).settle();
|
||||
assert_eq!(value.get(), Some(0.0), "Tab commits the start");
|
||||
assert_eq!(alerts.borrow().len(), 2, "Tab reports it");
|
||||
assert_eq!(alerts.borrow()[1].typed, -5.0);
|
||||
|
||||
type_over(&mut fixture, "300");
|
||||
fixture.click(&other).settle();
|
||||
assert_eq!(value.get(), Some(100.0), "a press on another control commits the end");
|
||||
assert_eq!(alerts.borrow().len(), 3, "the focus loss reports it");
|
||||
|
||||
type_over(&mut fixture, "95");
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
fixture.key(Key::PageUp, Modifiers::NONE).key(Key::PageUp, Modifiers::NONE).settle();
|
||||
assert_eq!(value.get(), Some(100.0), "Page Up runs into the end");
|
||||
fixture.frame(vec![moved(amount.rect.center())]);
|
||||
for _ in 0..3 {
|
||||
fixture.frame(vec![wheel_lines(1.0)]);
|
||||
}
|
||||
fixture.settle();
|
||||
fixture.click(&other).settle();
|
||||
fixture.drag_at(amount.rect.center(), Vec2::new(60.0, 0.0));
|
||||
fixture.settle();
|
||||
assert_eq!(alerts.borrow().len(), 3, "a value in range, keys, the wheel and a scrub report nothing: {:?}", alerts.borrow());
|
||||
}
|
||||
|
||||
/// The name of an edit event's step.
|
||||
fn step_name<V>(event: &craft_behavior::EditEvent<V>) -> &'static str {
|
||||
use craft_behavior::EditEvent;
|
||||
match event {
|
||||
EditEvent::Begin { .. } => "begin",
|
||||
EditEvent::Update { .. } => "update",
|
||||
EditEvent::Commit { .. } => "commit",
|
||||
EditEvent::Cancel { .. } => "cancel",
|
||||
EditEvent::Set { .. } => "set",
|
||||
_ => "other",
|
||||
}
|
||||
}
|
||||
|
||||
/// Wheel notches over a field are one edit gesture, one undo step: Begin, then Updates, and one Commit when the
|
||||
/// wheel gesture ends. A value written from outside while the run is open cancels it, and the end then commits
|
||||
/// nothing.
|
||||
#[trial]
|
||||
fn a_wheel_run_on_a_field_is_one_edit_gesture() {
|
||||
use craft_behavior::GESTURE_GAP;
|
||||
use craft_ui::testing::Fixture;
|
||||
use craft_ui::testing::pointer::{moved, wheel_lines};
|
||||
use std::rc::Rc;
|
||||
let value = Rc::new(RefCell::new(Some(START)));
|
||||
let steps: Rc<RefCell<Vec<&'static str>>> = Rc::default();
|
||||
let (held, seen) = (value.clone(), steps.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut guard = held.borrow_mut();
|
||||
let out = NumberField::new("Size", Bind::bound(&mut *guard)).id_salt("size").range(0.0..=100.0).show(ui);
|
||||
for event in &out.edit.events {
|
||||
seen.borrow_mut().push(step_name(event));
|
||||
}
|
||||
});
|
||||
fixture.settle();
|
||||
let field = fixture.by_label("Size").expect("the field");
|
||||
fixture.click(&field).settle();
|
||||
fixture.frame(vec![moved(field.rect.center())]);
|
||||
steps.borrow_mut().clear();
|
||||
for _ in 0..3 {
|
||||
fixture.frame(vec![wheel_lines(1.0)]);
|
||||
}
|
||||
assert_ne!(*value.borrow(), Some(START), "the notches moved the value");
|
||||
{
|
||||
let steps = steps.borrow();
|
||||
assert!(steps.len() >= 2 && steps[0] == "begin", "the first notch begins the run: {steps:?}");
|
||||
assert!(steps[1..].iter().all(|step| *step == "update"), "later notches update it, nothing commits yet: {steps:?}");
|
||||
}
|
||||
fixture.advance(GESTURE_GAP * 2.0).idle(3);
|
||||
{
|
||||
let steps = steps.borrow();
|
||||
assert_eq!(steps.iter().filter(|step| **step == "begin").count(), 1, "one run: {steps:?}");
|
||||
assert_eq!(steps.iter().filter(|step| **step == "commit").count(), 1, "the gesture's end commits once: {steps:?}");
|
||||
assert_eq!(steps.last(), Some(&"commit"), "{steps:?}");
|
||||
assert!(!steps.contains(&"set"), "no notch is a one-shot set: {steps:?}");
|
||||
}
|
||||
|
||||
steps.borrow_mut().clear();
|
||||
for _ in 0..2 {
|
||||
fixture.frame(vec![wheel_lines(1.0)]);
|
||||
}
|
||||
*value.borrow_mut() = Some(77.0);
|
||||
fixture.idle(2);
|
||||
assert!(steps.borrow().contains(&"cancel"), "a value written from outside cancels the run: {:?}", steps.borrow());
|
||||
fixture.advance(GESTURE_GAP * 2.0).idle(3);
|
||||
assert!(!steps.borrow().contains(&"commit"), "the end commits no cancelled run: {:?}", steps.borrow());
|
||||
assert_eq!(*value.borrow(), Some(77.0), "the outside value stays");
|
||||
}
|
||||
|
||||
/// An arrow over a typed sum applies the sum first and steps from it, and the field keeps focus.
|
||||
#[trial]
|
||||
fn arrow_keys_step_from_a_typed_sum() {
|
||||
use craft_ui::testing::Fixture;
|
||||
use std::rc::Rc;
|
||||
let value = Rc::new(Cell::new(Some(100.0)));
|
||||
let id = Rc::new(Cell::new(egui::Id::NULL));
|
||||
let (held, seen) = (value.clone(), id.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut now = held.get();
|
||||
let out = NumberField::new("Size", Bind::bound(&mut now)).id_salt("size").range(0.0..=1000.0).show(ui);
|
||||
held.set(now);
|
||||
seen.set(out.response.id);
|
||||
});
|
||||
fixture.settle();
|
||||
let field = fixture.by_label("Size").expect("the field");
|
||||
fixture.click(&field).settle();
|
||||
fixture.key(Key::A, Modifiers::COMMAND).type_text("5+5").settle();
|
||||
fixture.key(Key::ArrowUp, Modifiers::NONE).settle();
|
||||
assert_eq!(value.get(), Some(11.0), "Arrow Up applies 5+5 and steps from 10");
|
||||
fixture.key(Key::ArrowDown, Modifiers::NONE).settle();
|
||||
assert_eq!(value.get(), Some(10.0), "Arrow Down steps back");
|
||||
assert!(fixture.focused_stop().is_some_and(|(stop, _)| stop == id.get()), "the field keeps focus");
|
||||
}
|
||||
|
||||
/// A drag on the popup slider of a field whose app applies each update, as the Layers panel's Opacity does, runs each
|
||||
/// drag frame once: an edit of a given value reruns the frame only when something read it earlier in the frame.
|
||||
#[trial]
|
||||
|
||||
Reference in New Issue
Block a user