mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 20:23:38 +00:00
merge-fwb: A13: blend row keys step past greyed modes, a greyed mode's letter picks nothing (#13852)
This commit is contained in:
@@ -237,6 +237,8 @@ impl<K> BlendKeyPick<K> {
|
||||
/// - A [`BlendShortcut`] picks the entry whose position it is. A position whose mode isn't listed (Clear or Behind for
|
||||
/// layers, Pass Through for a painting tool, Overlay for the Healing Brush) picks nothing: the command is taken, so
|
||||
/// the letter does not reach the single-letter tool keys.
|
||||
/// - An entry [`enabled`](Self::enabled) rules out is never picked: a step goes on past it the same way, staying put
|
||||
/// when no other entry is enabled, and its letter picks nothing (the command is still taken).
|
||||
/// - A key release picks nothing; holding a step key repeats the step.
|
||||
/// - No AccessKit node: the app's blend control announces the new mode. Keys are not directional, so nothing mirrors
|
||||
/// in a right-to-left interface.
|
||||
@@ -244,12 +246,19 @@ impl<K> BlendKeyPick<K> {
|
||||
pub struct BlendKeys<'a, K> {
|
||||
entries: &'a [BlendKeyEntry<K>],
|
||||
current: Option<&'a K>,
|
||||
enabled: Option<&'a dyn Fn(&K) -> bool>,
|
||||
}
|
||||
|
||||
impl<'a, K: Clone + PartialEq> BlendKeys<'a, K> {
|
||||
/// The keys over `entries`, in menu order.
|
||||
pub fn new(entries: &'a [BlendKeyEntry<K>]) -> Self {
|
||||
Self { entries, current: None }
|
||||
Self { entries, current: None, enabled: None }
|
||||
}
|
||||
|
||||
/// Which entries the keys may pick: a greyed mode is skipped. Every entry when not set.
|
||||
pub fn enabled(mut self, enabled: &'a dyn Fn(&K) -> bool) -> Self {
|
||||
self.enabled = Some(enabled);
|
||||
self
|
||||
}
|
||||
|
||||
/// The target's mode now, where the steps start from.
|
||||
@@ -269,29 +278,44 @@ impl<'a, K: Clone + PartialEq> BlendKeys<'a, K> {
|
||||
if phase == FirePhase::Release {
|
||||
return None;
|
||||
}
|
||||
let enabled = |key: &K| self.enabled.is_none_or(|enabled| enabled(key));
|
||||
if let Some(step) = BlendStep::from_ref(command) {
|
||||
let step = match step {
|
||||
BlendStep::Next => 1,
|
||||
BlendStep::Previous => -1,
|
||||
};
|
||||
return stepped(self.entries, self.current, step).map(|entry| BlendKeyPick::Stepped(entry.key.clone()));
|
||||
return stepped(self.entries, self.current, step, &enabled).map(|entry| BlendKeyPick::Stepped(entry.key.clone()));
|
||||
}
|
||||
let shortcut = BlendShortcut::from_ref(command)?;
|
||||
self.entries.iter().find(|entry| entry.shortcut == Some(shortcut)).map(|entry| BlendKeyPick::Lettered(entry.key.clone()))
|
||||
self.entries
|
||||
.iter()
|
||||
.find(|entry| entry.shortcut == Some(shortcut))
|
||||
.filter(|entry| enabled(&entry.key))
|
||||
.map(|entry| BlendKeyPick::Lettered(entry.key.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
/// The entry `step` places after (or before, when negative) `current` in `entries`, wrapping; an
|
||||
/// unlisted or absent `current` steps from the top (`step > 0`) or the bottom. `None` for an
|
||||
/// empty list.
|
||||
fn stepped<'e, K: PartialEq>(entries: &'e [BlendKeyEntry<K>], current: Option<&K>, step: i32) -> Option<&'e BlendKeyEntry<K>> {
|
||||
// pc: crates/ui-egui/src/input/blend_keys.rs:18 `stepped` (`rem_euclid`; unlisted: 0 or n − 1).
|
||||
/// The first enabled entry `step` places after (or before, when negative) `current` in `entries`, wrapping; an
|
||||
/// unlisted or absent `current` steps from the top (`step > 0`) or the bottom. `None` for an empty list, or when no
|
||||
/// entry other than `current` is enabled.
|
||||
fn stepped<'e, K: PartialEq>(
|
||||
entries: &'e [BlendKeyEntry<K>],
|
||||
current: Option<&K>,
|
||||
step: i32,
|
||||
enabled: &dyn Fn(&K) -> bool,
|
||||
) -> Option<&'e BlendKeyEntry<K>> {
|
||||
// pc: crates/ui-egui/src/input/blend_keys.rs:18 `stepped` (`rem_euclid`; unlisted: 0 or n − 1); fade_modes.rs
|
||||
// `step_past` for greyed modes.
|
||||
let n = i32::try_from(entries.len()).ok().filter(|n| *n > 0)?;
|
||||
let at = current.and_then(|c| entries.iter().position(|e| e.key == *c)).and_then(|i| i32::try_from(i).ok());
|
||||
let next = match at {
|
||||
let first = match at {
|
||||
Some(i) => (i + step).rem_euclid(n),
|
||||
None if step > 0 => 0,
|
||||
None => n - 1,
|
||||
};
|
||||
entries.get(usize::try_from(next).ok()?)
|
||||
(0..n)
|
||||
.map(|k| (first + k * step.signum()).rem_euclid(n))
|
||||
.take_while(|i| Some(*i) != at)
|
||||
.filter_map(|i| entries.get(usize::try_from(i).ok()?))
|
||||
.find(|entry| enabled(&entry.key))
|
||||
}
|
||||
|
||||
@@ -325,13 +325,12 @@ impl<'p> Pass<'p> {
|
||||
fired.push(letter);
|
||||
}
|
||||
let mut current = self.work.text(¶m.key).map(str::to_owned).or_else(|| param.kind.default_value().and_then(text_of)).unwrap_or_default();
|
||||
let offered = |id: &String| param.option_enabled(id);
|
||||
for fired in fired {
|
||||
if let Some(pick) = BlendKeys::new(&entries).current(¤t).fired(&fired) {
|
||||
if let Some(pick) = BlendKeys::new(&entries).current(¤t).enabled(&offered).fired(&fired) {
|
||||
let id = pick.into_key();
|
||||
if param.option_enabled(&id) {
|
||||
self.work.set(param.key.as_str(), FieldValue::Text(id.clone()));
|
||||
current = id;
|
||||
}
|
||||
self.work.set(param.key.as_str(), FieldValue::Text(id.clone()));
|
||||
current = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,6 +174,31 @@ fn a_blend_row_steps_and_picks_by_its_keys_inside_the_dialog() {
|
||||
fixture.key(Key::M, Modifiers::SHIFT | Modifiers::ALT).settle();
|
||||
assert_eq!(held("mode").as_deref(), Some("multiply"), "Shift+Alt+M picks Multiply");
|
||||
assert_eq!(held("other").as_deref(), Some("normal"), "a second Blend row does not move");
|
||||
|
||||
// A greyed mode (#13852): the keys step past it and its letter picks nothing; the closed list's Down skips it too.
|
||||
let values = Rc::new(RefCell::new(DialogFields::new()));
|
||||
let shown = values.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let params = vec![Param::new("mode", "Mode", ParamKind::blend(modes(), None)).disabled_options(["dissolve"])];
|
||||
let _ = ParamDialog::new("blend", "Blend", params).values(Bind::bound(&mut *shown.borrow_mut())).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let held = || values.borrow().text("mode").map(str::to_owned);
|
||||
fixture.key(Key::Equals, Modifiers::SHIFT).settle();
|
||||
assert_eq!(held().as_deref(), Some("multiply"), "Shift+= steps past the greyed Dissolve");
|
||||
fixture.key(Key::Minus, Modifiers::SHIFT).settle();
|
||||
assert_eq!(held().as_deref(), Some("normal"), "Shift+- steps back past it");
|
||||
fixture.key(Key::I, Modifiers::SHIFT | Modifiers::ALT).settle();
|
||||
assert_eq!(held().as_deref(), Some("normal"), "the greyed mode's letter picks nothing");
|
||||
// Tab to the Mode list: the closed list's role types ahead and moves with the arrows.
|
||||
for _ in 0..4 {
|
||||
if fixture.focused_role().is_some_and(|role| role.type_ahead && role.moves) {
|
||||
break;
|
||||
}
|
||||
fixture.key(Key::Tab, Modifiers::NONE).settle();
|
||||
}
|
||||
fixture.key(Key::ArrowDown, Modifiers::NONE).settle();
|
||||
assert_eq!(held().as_deref(), Some("multiply"), "Down on the closed list skips the greyed mode");
|
||||
}
|
||||
|
||||
/// A pattern row shows the first pattern for a stored id the rows lack without writing it, a pick writes the pattern's
|
||||
|
||||
Reference in New Issue
Block a user