mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
Entries a step skips: Entry::stepped, one step rule for Select and ComboBox
A step over a closed control stops on the enabled, stepped entries only. A value on none of them (mixed, or an entry only the open list offers) has no place: it reports no number, so both steps are offered, and a step forward lands on the first stop and a step back on the last. Select and ComboBox step with the shared step_stops and step_stop. LutPicker marks Load 3D LUT…, the folder files and the loaded LUT as not stepped, in place of its filter on step events. The loaded LUT then offers both steps, which the craft-color activation suite asks for.
This commit is contained in:
@@ -171,7 +171,8 @@ fn seconds() -> u64 {
|
||||
/// slow); until a scan ends the list shows the last one. Another folder drops the old list at
|
||||
/// once. Web has no folder.
|
||||
/// - The wheel and the arrow keys over the closed field step through None and the built-ins
|
||||
/// only (a folder file would be read from disk at each step).
|
||||
/// only (a folder file would be read from disk at each step). From a folder file or the loaded
|
||||
/// LUT, a step forward lands on None and a step back on the last built-in.
|
||||
#[derive(craft_ui::contract::Component, craft_ui::contract::Props)]
|
||||
#[component]
|
||||
pub struct LutPicker<'a> {
|
||||
@@ -192,25 +193,26 @@ impl<'a> LutPicker<'a> {
|
||||
}
|
||||
|
||||
fn entries(words: &LutWords, current: Option<&LutChoice>, files: &FolderFiles) -> Vec<Entry<Row>> {
|
||||
let mut entries = vec![Entry::new(Row::Load, words.load.clone()), Entry::new(Row::Choice(LutChoice::None), words.none.clone())];
|
||||
let mut entries =
|
||||
vec![Entry::new(Row::Load, words.load.clone()).stepped(false), Entry::new(Row::Choice(LutChoice::None), words.none.clone())];
|
||||
for (id, _) in craft_cms::lutfile::BUILTIN {
|
||||
entries.push(Entry::new(Row::Choice(LutChoice::Builtin(id)), builtin_name(words, id)));
|
||||
}
|
||||
let mut listed = false;
|
||||
for (name, path) in files {
|
||||
listed |= matches!(current, Some(LutChoice::Folder(held)) if held == path);
|
||||
entries.push(Entry::new(Row::Choice(LutChoice::Folder(path.clone())), Label::from(name.clone())));
|
||||
entries.push(Entry::new(Row::Choice(LutChoice::Folder(path.clone())), Label::from(name.clone())).stepped(false));
|
||||
}
|
||||
// The file in use while a scan has not found it yet (or the folder changed under it).
|
||||
if let Some(LutChoice::Folder(path)) = current
|
||||
&& !listed
|
||||
{
|
||||
let name = path.file_name().map_or_else(|| path.display().to_string(), |n| n.to_string_lossy().into_owned());
|
||||
entries.push(Entry::new(Row::Choice(LutChoice::Folder(path.clone())), Label::from(name)));
|
||||
entries.push(Entry::new(Row::Choice(LutChoice::Folder(path.clone())), Label::from(name)).stepped(false));
|
||||
}
|
||||
if let Some(LutChoice::Loaded { name, size }) = current {
|
||||
let text = words.custom.fill(&[("name", name), ("size", &size.to_string())]);
|
||||
entries.push(Entry::new(Row::Choice(current.cloned().unwrap_or(LutChoice::None)), Label::from(text)));
|
||||
entries.push(Entry::new(Row::Choice(current.cloned().unwrap_or(LutChoice::None)), Label::from(text)).stepped(false));
|
||||
}
|
||||
entries
|
||||
}
|
||||
@@ -320,12 +322,7 @@ impl Component for LutPicker<'_> {
|
||||
for event in &out.events {
|
||||
match event.value() {
|
||||
Some(Row::Load) => self.load_asked |= matches!(event, EditEvent::Commit { .. }),
|
||||
Some(Row::Choice(choice)) => {
|
||||
let step = matches!(event, EditEvent::Set { .. });
|
||||
if !step || matches!(choice, LutChoice::None | LutChoice::Builtin(_)) {
|
||||
adopted.push(event.clone());
|
||||
}
|
||||
}
|
||||
Some(Row::Choice(_)) => adopted.push(event.clone()),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,13 @@ struct Item<T> {
|
||||
enabled: bool,
|
||||
reason: Option<Label>,
|
||||
checked: bool,
|
||||
stepped: bool,
|
||||
}
|
||||
|
||||
impl<T> Entry<T> {
|
||||
/// An entry reporting `value` when picked, shown as `label`: enabled and unchecked, without an icon.
|
||||
pub fn new(value: T, label: impl Into<Label>) -> Self {
|
||||
Entry { row: Row::Item(Item { value, label: label.into(), icon: None, enabled: true, reason: None, checked: false }) }
|
||||
Entry { row: Row::Item(Item { value, label: label.into(), icon: None, enabled: true, reason: None, checked: false, stepped: true }) }
|
||||
}
|
||||
|
||||
/// A rule between groups of entries. It is not a row: it takes no pick and no lead.
|
||||
@@ -76,6 +77,17 @@ impl<T> Entry<T> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether a step over the closed control (an arrow, Home, End, the wheel, an AccessKit step) stops on the entry.
|
||||
/// A row only the open list offers is not stepped: an action such as "Load…", or a choice that is costly to reach in
|
||||
/// passing (a file read from disk at each step).
|
||||
#[must_use]
|
||||
pub fn stepped(mut self, on: bool) -> Self {
|
||||
if let Row::Item(item) = &mut self.row {
|
||||
item.stepped = on;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether the entry shows as the current choice.
|
||||
#[must_use]
|
||||
pub fn checked(mut self, on: bool) -> Self {
|
||||
@@ -114,6 +126,11 @@ impl<T> Entry<T> {
|
||||
matches!(&self.row, Row::Item(item) if item.enabled)
|
||||
}
|
||||
|
||||
/// Whether a step over the closed control stops on the entry: an enabled, stepped item.
|
||||
pub fn is_stepped(&self) -> bool {
|
||||
matches!(&self.row, Row::Item(item) if item.enabled && item.stepped)
|
||||
}
|
||||
|
||||
/// Why the entry is disabled, while it is.
|
||||
pub fn why_disabled(&self) -> Option<&Label> {
|
||||
match &self.row {
|
||||
@@ -132,3 +149,23 @@ impl<T> Entry<T> {
|
||||
matches!(self.row, Row::Separator)
|
||||
}
|
||||
}
|
||||
|
||||
/// The positions of the entries a step over a closed control stops on ([`Entry::is_stepped`]), in order.
|
||||
pub fn step_stops<T>(entries: &[Entry<T>]) -> Vec<usize> {
|
||||
(0..entries.len()).filter(|&at| entries[at].is_stepped()).collect()
|
||||
}
|
||||
|
||||
/// The stop one step from the entry at `from` (a position in the entries), toward the end when `forward`. A step holds
|
||||
/// at the first and last stop. A value that is none of the stops (mixed, or an entry that is not stepped) has no
|
||||
/// place: a step forward lands on the first stop and a step back on the last. Every control that steps over entries
|
||||
/// steps with this, and reports such a value without a number, so both steps are offered.
|
||||
pub fn step_stop(stops: &[usize], from: Option<usize>, forward: bool) -> Option<usize> {
|
||||
let last = stops.len().checked_sub(1)?;
|
||||
let to = match (from.and_then(|from| stops.iter().position(|&stop| stop == from)), forward) {
|
||||
(Some(at), true) => (at + 1).min(last),
|
||||
(Some(at), false) => at.saturating_sub(1),
|
||||
(None, true) => 0,
|
||||
(None, false) => last,
|
||||
};
|
||||
Some(stops[to])
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ pub use cx::THUMB;
|
||||
pub use cx::{
|
||||
Accessible, Activation, BodyCx, Cx, CxCore, Edit, Focus, ItemCx, ItemOpener, MeasureCx, PartBuilder, PartsCx, Pointer, Styled, WordSetCx,
|
||||
};
|
||||
pub use entry::Entry;
|
||||
pub use entry::{Entry, step_stop, step_stops};
|
||||
pub use group::{GroupHandle, GroupKind, GroupState};
|
||||
pub use held::Held;
|
||||
#[doc(hidden)]
|
||||
|
||||
@@ -13,7 +13,7 @@ use super::words::PickerWords;
|
||||
use crate::buttons::IconButton;
|
||||
use crate::contract::{
|
||||
Bind, BindMut, CollectionEvent, Common, Component, Cx, Edit as _, EditResponse, Entry, Focus as _, Input, MeasureCx, Overlay, OverlayUi,
|
||||
Overlays as _, PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role, ShowCollection as _, WordSetCx as _,
|
||||
Overlays as _, PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role, ShowCollection as _, WordSetCx as _, step_stop, step_stops,
|
||||
};
|
||||
use crate::list::{List, ListRow};
|
||||
use crate::primitives::{TextKey, TextOptions, TextRun};
|
||||
@@ -41,7 +41,7 @@ struct Typing {
|
||||
/// A text field with a list of options. The typed text filters the list by the options' labels: the labels that
|
||||
/// match it best first, ignoring case and accents. The chevron and Alt+Down open the list on every option. Enter or a
|
||||
/// click on a row puts the option's text in the field; Escape closes the list, and the next Escape restores the text.
|
||||
/// Up, Down and the wheel on the focused, closed field step to the previous or next option.
|
||||
/// Up, Down and the wheel on the focused, closed field step to the previous or next stepped option ([`Entry::stepped`]).
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(overlay)]
|
||||
pub struct ComboBox<'a> {
|
||||
@@ -81,19 +81,10 @@ impl<'a> ComboBox<'a> {
|
||||
self.options.iter().filter(|option| !option.is_separator()).any(|option| matcher.score(cx.env.resolve(option.label()).as_str()).is_some())
|
||||
}
|
||||
|
||||
/// The option whose text steps from the field's text in the direction of the move, over the options that can be
|
||||
/// picked. From text that is none of them, forward goes to the first and back to the last.
|
||||
/// The option whose text steps from the field's text in the direction of the move, over the stepped options.
|
||||
fn neighbour(&self, text: &str, forward: bool) -> Option<usize> {
|
||||
let pickable: Vec<usize> =
|
||||
(0..self.options.len()).filter(|&at| self.options[at].value().is_some() && self.options[at].is_enabled()).collect();
|
||||
let here = pickable.iter().position(|&at| self.options[at].value().is_some_and(|value| value == text));
|
||||
let to = match (here, forward) {
|
||||
(Some(here), true) => (here + 1).min(pickable.len().checked_sub(1)?),
|
||||
(Some(here), false) => here.saturating_sub(1),
|
||||
(None, true) => 0,
|
||||
(None, false) => pickable.len().checked_sub(1)?,
|
||||
};
|
||||
pickable.get(to).copied()
|
||||
let here = self.options.iter().position(|option| option.value().is_some_and(|value| value == text));
|
||||
step_stop(&step_stops(self.options), here, forward)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ use crate::contract::overlay::{Popup, Trigger};
|
||||
use crate::contract::{
|
||||
Access, Bind, BindMut, BodyCx, CollectionEvent, Common, Component, Cx, Edit as _, EditResponse, Entry, Focus as _, LeadOnOpen, LeadSource,
|
||||
MeasureCx, Numeric, Overlay, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, PopupKind, Props as _, Remember as _, Role, Show as _,
|
||||
ShowCollection as _, ShowContainer as _, WordSetCx as _,
|
||||
ShowCollection as _, ShowContainer as _, WordSetCx as _, step_stop, step_stops,
|
||||
};
|
||||
use crate::list::{List, ListRow};
|
||||
use crate::menu::Menu;
|
||||
@@ -67,7 +67,8 @@ impl<'a> HeaderMenu<'a> {
|
||||
/// A field that shows the picked entry and opens a listbox of all of them. A click, Enter, Space or Alt+Down opens the
|
||||
/// listbox with the picked entry active; Up, Down, Home, End and type-ahead move through it, Enter or a click picks,
|
||||
/// Escape and Alt+Up close it. Closed, the arrow keys, Home, End and the wheel (while the field is focused or armed)
|
||||
/// step the pick through the enabled entries. The field is as wide as its widest entry, placeholder or mixed word, so it keeps its
|
||||
/// step the pick through the stepped entries ([`Entry::stepped`]); from a value that is none of them, forward lands on
|
||||
/// the first and back on the last. The field is as wide as its widest entry, placeholder or mixed word, so it keeps its
|
||||
/// width whichever is shown.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(overlay, probe(Select<'static, u8>))]
|
||||
@@ -113,23 +114,16 @@ impl<'a, T: Value + 'static> Select<'a, T> {
|
||||
cx.set(value.clone());
|
||||
}
|
||||
}
|
||||
|
||||
/// The positions of the entries that can be picked, in order.
|
||||
fn picks(&self) -> Vec<usize> {
|
||||
(0..self.entries.len()).filter(|&at| self.entries[at].is_enabled()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// The pick a move reaches: `picks` are the positions of the enabled entries and `from` the position of the current
|
||||
/// value. From a value that is none of the picks, an arrow lands on the first.
|
||||
fn step(picks: &[usize], from: Option<usize>, mv: NavMove) -> Option<usize> {
|
||||
let (first, last) = (*picks.first()?, *picks.last()?);
|
||||
let at = from.and_then(|from| picks.iter().position(|&pick| pick == from));
|
||||
/// The stop a move over the closed field reaches: `stops` are the positions of the stepped entries and `from` the
|
||||
/// position of the current value.
|
||||
fn step(stops: &[usize], from: Option<usize>, mv: NavMove) -> Option<usize> {
|
||||
match mv {
|
||||
NavMove::Up | NavMove::Back => Some(at.map_or(first, |at| picks[at.saturating_sub(1)])),
|
||||
NavMove::Down | NavMove::Forward => Some(at.map_or(first, |at| picks[(at + 1).min(picks.len() - 1)])),
|
||||
NavMove::Start => Some(first),
|
||||
NavMove::End => Some(last),
|
||||
NavMove::Up | NavMove::Back => step_stop(stops, from, false),
|
||||
NavMove::Down | NavMove::Forward => step_stop(stops, from, true),
|
||||
NavMove::Start => stops.first().copied(),
|
||||
NavMove::End => stops.last().copied(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -173,7 +167,7 @@ impl<T: Value + 'static> Component for Select<'_, T> {
|
||||
}
|
||||
|
||||
/// Enter and Space toggle the listbox; a press on the face toggles it through the part. While it is closed, the
|
||||
/// arrow keys, Home, End and the wheel step the pick through the enabled entries, and AccessKit sets it. A preview
|
||||
/// arrow keys, Home, End and the wheel step the pick through the stepped entries, and AccessKit sets it. A preview
|
||||
/// that the list closed on without a pick is cancelled.
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
let list = cx.popup("list");
|
||||
@@ -181,36 +175,38 @@ impl<T: Value + 'static> Component for Select<'_, T> {
|
||||
if self.preview && !list.is_open(cx) && matches!(cx.gesture(), Some(GesturePhase::Begin | GesturePhase::Update)) {
|
||||
cx.cancel(CancelReason::Dismissed);
|
||||
}
|
||||
let picks = self.picks();
|
||||
if list.is_open(cx) || picks.is_empty() {
|
||||
let stops = step_stops(self.entries);
|
||||
if list.is_open(cx) || stops.is_empty() {
|
||||
return;
|
||||
}
|
||||
let held = self.position(cx.value());
|
||||
if let Some(at) = cx.intents().take_set_value() {
|
||||
let to = (at.round().max(0.0) as usize).min(picks.len() - 1);
|
||||
self.pick(cx, picks[to]);
|
||||
let to = (at.round().max(0.0) as usize).min(stops.len() - 1);
|
||||
self.pick(cx, stops[to]);
|
||||
}
|
||||
while let Some((mv, _)) = cx.intents().take_move() {
|
||||
if let Some(to) = step(&picks, self.position(cx.value()), mv) {
|
||||
if let Some(to) = step(&stops, self.position(cx.value()), mv) {
|
||||
self.pick(cx, to);
|
||||
}
|
||||
}
|
||||
if let Some(wheel) = cx.take_wheel() {
|
||||
let at = held.and_then(|held| picks.iter().position(|&pick| pick == held)).unwrap_or(0);
|
||||
let steps = ValueSteps::new(at as f64, 0.0, (picks.len() - 1) as f64, 1.0, 1.0);
|
||||
if let StepResult::Set(next) = wheel_step(&steps, ValueAxes::Both, &wheel, cx.env.direction().is_rtl()) {
|
||||
self.pick(cx, picks[(next.round().max(0.0) as usize).min(picks.len() - 1)]);
|
||||
}
|
||||
// The wheel gives one notch, up for a step forward.
|
||||
if let Some(wheel) = cx.take_wheel()
|
||||
&& let StepResult::Set(along) =
|
||||
wheel_step(&ValueSteps::new(0.0, -1.0, 1.0, 1.0, 1.0), ValueAxes::Both, &wheel, cx.env.direction().is_rtl())
|
||||
&& along != 0.0
|
||||
&& let Some(to) = step_stop(&stops, held, along > 0.0)
|
||||
{
|
||||
self.pick(cx, to);
|
||||
}
|
||||
}
|
||||
|
||||
/// The picked entry's place among the entries that can be picked, shown as its label, so AccessKit steps and sets it.
|
||||
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
|
||||
let picks = self.picks();
|
||||
let range = 0.0..=picks.len().saturating_sub(1) as f64;
|
||||
let stops = step_stops(self.entries);
|
||||
let range = 0.0..=stops.len().saturating_sub(1) as f64;
|
||||
let held = self.position(cx.value());
|
||||
let numeric = match held.and_then(|held| picks.iter().position(|&pick| pick == held)) {
|
||||
Some(at) => Numeric::new(at as f64, range).text(cx.env.resolve(self.entries[picks[at]].label()).as_str()),
|
||||
let numeric = match held.and_then(|held| stops.iter().position(|&stop| stop == held)) {
|
||||
Some(at) => Numeric::new(at as f64, range).text(cx.env.resolve(self.entries[stops[at]].label()).as_str()),
|
||||
None => Numeric::empty(range),
|
||||
};
|
||||
access.numeric(numeric.step(1.0));
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//! Dropdown pickers.
|
||||
|
||||
mod date;
|
||||
mod select_steps;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
//! A Select steps over its stepped entries only. A value on an entry that is not stepped has no place among them: it
|
||||
//! offers both steps, and a step forward lands on the first stop and a step back on the last.
|
||||
|
||||
use craft_ui::contract::{Bind, Entry, Role, Show as _};
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::testing::access::Declared;
|
||||
use craft_ui::testing::{Fixture, Story, StoryData, StoryOutput, trial};
|
||||
use egui::{Key, Modifiers, Ui};
|
||||
|
||||
/// Entries 1 to 4; 2 and 4 are reached from the open list only.
|
||||
fn select(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let entries = [Entry::new(1u8, "One"), Entry::new(2u8, "Two").stepped(false), Entry::new(3u8, "Three"), Entry::new(4u8, "Four").stepped(false)];
|
||||
Select::new("Pick", Bind::bound(data.value("value", 4u8)), &entries).show(ui).into()
|
||||
}
|
||||
|
||||
/// The value after `key` on the focused, closed field holding `held`, and whether it offered Increment and Decrement.
|
||||
fn step(held: u8, key: Key) -> (u8, (bool, bool)) {
|
||||
let mut fixture = Fixture::story(&Story::new("select_steps", select));
|
||||
*fixture.data().value("value", 4u8) = held;
|
||||
fixture.settle();
|
||||
fixture.key(Key::Tab, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
let node = fixture.by_role(Role::COMBO_BOX).first().expect("the field").node;
|
||||
let tree = fixture.tree();
|
||||
let offered = (tree.declares_key(node, Declared::Increment), tree.declares_key(node, Declared::Decrement));
|
||||
fixture.key(key, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
let value = *fixture.data().value("value", 4u8);
|
||||
(value, offered)
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_select_steps_over_its_stepped_entries_and_from_an_unstepped_one_to_an_end() {
|
||||
assert_eq!(step(1, Key::ArrowDown), (3, (true, false)), "the first stop skips the unstepped Two and offers no step back");
|
||||
assert_eq!(step(4, Key::ArrowDown), (1, (true, true)), "from an unstepped entry, forward lands on the first stop");
|
||||
assert_eq!(step(4, Key::ArrowUp), (3, (true, true)), "from an unstepped entry, back lands on the last stop");
|
||||
}
|
||||
Reference in New Issue
Block a user