plexus-ui-engine 0.2.4

The plexus-ui engine — signals, live() over Plexus streams, the view tree over a renderer, Rhai folds, sections, hot-swap
Documentation
use super::*;
use std::cell::RefCell;

fn press(chord: &str, at_ms: u64) -> Press {
    Press { chord: chord.into(), key: chord.rsplit('+').next().unwrap_or(chord).into(), editable: false, activatable: false, repeat: false, target: Value::Null, at_ms }
}

fn typed(chord: &str) -> Press {
    Press { editable: true, ..press(chord, 0) }
}

/// A registry, and a log each entry writes `<name>:<chord>` to.
fn logged() -> (Keys, Rc<RefCell<Vec<String>>>) {
    (Keys::default(), Rc::new(RefCell::new(Vec::new())))
}

fn entry(log: &Rc<RefCell<Vec<String>>>, name: &str, chord: &str) -> (KeyDecl, KeyRun) {
    entry_with(log, name, KeyDecl::new(chord, name, None, false, false).unwrap())
}

fn entry_with(log: &Rc<RefCell<Vec<String>>>, name: &str, decl: KeyDecl) -> (KeyDecl, KeyRun) {
    let (log, name) = (log.clone(), name.to_string());
    (decl, Rc::new(move |ev: Value| log.borrow_mut().push(format!("{name}:{}", ev["chord"].as_str().unwrap_or("")))))
}

#[test]
fn a_chord_is_written_one_way() {
    assert_eq!(chord("ctrl+k").unwrap(), "ctrl+k");
    assert_eq!(chord("shift+ctrl+k").unwrap(), "ctrl+shift+k");
    assert_eq!(chord("meta+alt+arrowdown").unwrap(), "alt+meta+arrowdown");
    assert_eq!(chord("?").unwrap(), "?");
    assert_eq!(chord("ctrl++").unwrap(), "ctrl++");
    assert_eq!(chord("/").unwrap(), "/");
    assert_eq!(sequence("g  b").unwrap(), vec!["g".to_string(), "b".to_string()]);
    for bad in ["", "ctrl+", "ctrl+ctrl+k", "ctrl+kk", "shift+?", "hyper+k", "esc", "K", "ctrl+K", "ArrowDown", "Ctrl+k"] {
        assert!(sequence(bad).is_err(), "{bad:?} is refused");
    }
}

#[test]
fn an_event_is_its_chord() {
    assert_eq!(event_chord("k", true, false, false, false).as_deref(), Some("ctrl+k"));
    assert_eq!(event_chord("K", false, false, true, false).as_deref(), Some("shift+k"));
    assert_eq!(event_chord("?", false, false, true, false).as_deref(), Some("?"), "shift is in the character");
    assert_eq!(event_chord("ArrowDown", false, false, true, false).as_deref(), Some("shift+arrowdown"));
    assert_eq!(event_chord(" ", false, false, false, false).as_deref(), Some("space"));
    assert_eq!(event_chord("Control", true, false, false, false), None, "a modifier alone is no chord");
    assert_eq!(event_chord("Escape", false, false, false, false).as_deref(), Some("escape"));
}

#[test]
fn a_bound_chord_runs_and_is_taken() {
    let (keys, log) = logged();
    let _held = keys.add(1, "a-page", vec![entry(&log, "open", "ctrl+k")]);
    assert_eq!(keys.press(&press("ctrl+k", 0)), Heard::Ran(1));
    assert_eq!(keys.press(&press("ctrl+j", 0)), Heard::Nothing, "an unbound key is the browser's");
    assert_eq!(*log.borrow(), vec!["open:ctrl+k"]);
}

#[test]
fn an_input_hears_only_what_says_so() {
    let (keys, log) = logged();
    let _held = keys.add(
        1,
        "a-page",
        vec![
            entry(&log, "down", "j"),
            entry(&log, "open", "ctrl+k"),
            entry_with(&log, "close", KeyDecl::new("escape", "close", None, true, false).unwrap()),
        ],
    );
    assert_eq!(keys.press(&typed("j")), Heard::Nothing, "j in an input is typing");
    assert_eq!(keys.press(&typed("ctrl+k")), Heard::Ran(1), "a chord with ctrl is heard in an input");
    assert_eq!(keys.press(&typed("escape")), Heard::Ran(1), "in_inputs");
    assert_eq!(keys.press(&typed("?")), Heard::Nothing, "the overlay never opens from an input");
    assert_eq!(*log.borrow(), vec!["open:ctrl+k", "close:escape"]);
}

#[test]
fn a_sequence_within_800_ms() {
    let (keys, log) = logged();
    let _held = keys.add(1, "a-page", vec![entry(&log, "board", "g b"), entry(&log, "bee", "b")]);
    assert_eq!(keys.press(&press("g", 1_000)), Heard::Waiting);
    assert_eq!(keys.press(&press("b", 1_800)), Heard::Ran(1));
    // Too late: the `b` is a press of its own.
    assert_eq!(keys.press(&press("g", 3_000)), Heard::Waiting);
    assert_eq!(keys.press(&press("b", 3_801)), Heard::Ran(1));
    // A sequence that goes nowhere leaves the press alone, afresh.
    assert_eq!(keys.press(&press("g", 5_000)), Heard::Waiting);
    assert_eq!(keys.press(&press("x", 5_100)), Heard::Nothing);
    assert_eq!(*log.borrow(), vec!["board:g b", "bee:b"]);
}

#[test]
fn the_last_registered_runs_first_and_only_runs_alone() {
    let (keys, log) = logged();
    let _outer = keys.add(1, "outer", vec![entry(&log, "outer", "x"), entry(&log, "outer", "y")]);
    let _inner = keys.add(2, "inner", vec![entry(&log, "inner", "x"), entry_with(&log, "inner", KeyDecl::new("y", "inner", None, false, true).unwrap())]);
    assert_eq!(keys.press(&press("x", 0)), Heard::Ran(2));
    assert_eq!(keys.press(&press("y", 0)), Heard::Ran(1));
    assert_eq!(*log.borrow(), vec!["inner:x", "outer:x", "inner:y"]);
}

#[test]
fn entries_go_with_their_holder_and_their_member() {
    let (keys, log) = logged();
    let held = keys.add(1, "a", vec![entry(&log, "a", "x")]);
    let _b = keys.add(2, "b", vec![entry(&log, "b", "z")]);
    drop(held);
    assert_eq!(keys.press(&press("x", 0)), Heard::Nothing);
    keys.remove_member(2);
    assert_eq!(keys.press(&press("z", 0)), Heard::Nothing);
    assert!(keys.is_empty());
}

#[test]
fn the_overlay_lists_every_component_and_escape_closes_it() {
    let (keys, log) = logged();
    assert_eq!(keys.press(&press("?", 0)), Heard::Nothing, "with nothing bound `?` is the page's");
    let _a = keys.add(1, "a-page", vec![entry_with(&log, "open", KeyDecl::new("ctrl+k", "open", Some("jump to a ticket".into()), false, false).unwrap()), entry(&log, "down", "j")]);
    let _b = keys.add(2, "b-list", vec![entry(&log, "pick", "enter")]);
    assert_eq!(keys.press(&press("?", 0)), Heard::Overlay(true));
    assert!(keys.overlay());
    assert_eq!(
        keys.groups(),
        vec![
            ("a-page".to_string(), vec![("ctrl+k".to_string(), "jump to a ticket".to_string()), ("j".to_string(), "down".to_string())]),
            ("b-list".to_string(), vec![("enter".to_string(), "pick".to_string())]),
        ]
    );
    assert_eq!(keys.press(&press("escape", 0)), Heard::Overlay(false));
    // A component that binds `?` has it.
    let _c = keys.add(3, "c-help", vec![entry(&log, "help", "?")]);
    assert_eq!(keys.press(&press("?", 0)), Heard::Ran(1));
}

/// FRAME-38: plain `enter` / `space` on a button or a link is its click,
/// unless the entry says `in_controls`; with a modifier it is the host's.
#[test]
fn enter_on_a_control_is_its_click() {
    let (keys, log) = logged();
    let _held = keys.add(
        1,
        "a-list",
        vec![
            entry(&log, "open", "enter"),
            entry(&log, "save", "ctrl+enter"),
            entry_with(&log, "toggle", KeyDecl::new("space", "toggle", None, false, false).unwrap().controls(true)),
        ],
    );
    let on_button = |chord: &str| Press { activatable: true, ..press(chord, 0) };
    assert_eq!(keys.press(&on_button("enter")), Heard::Nothing, "the button clicks");
    assert_eq!(keys.press(&on_button("ctrl+enter")), Heard::Ran(1));
    assert_eq!(keys.press(&on_button("space")), Heard::Ran(1), "in_controls");
    assert_eq!(keys.press(&press("enter", 0)), Heard::Ran(1), "elsewhere enter is bound");
    assert_eq!(*log.borrow(), vec!["save:ctrl+enter", "toggle:space", "open:enter"]);
}

/// FRAME-38: an autorepeated `?` toggles nothing.
#[test]
fn a_held_question_mark_toggles_nothing() {
    let (keys, log) = logged();
    let _held = keys.add(1, "a", vec![entry(&log, "down", "j")]);
    let held = Press { repeat: true, ..press("?", 0) };
    assert_eq!(keys.press(&held), Heard::Nothing, "a repeat never opens it");
    assert_eq!(keys.press(&press("?", 0)), Heard::Overlay(true));
    assert_eq!(keys.press(&held), Heard::Kept, "held open: taken, still open");
    assert!(keys.overlay());
    assert_eq!(keys.press(&press("?", 0)), Heard::Overlay(false));
}

/// FRAME-38: every change of the registry is told, after it is made.
#[test]
fn the_registry_tells_its_changes() {
    let (keys, log) = logged();
    let seen = Rc::new(RefCell::new(Vec::new()));
    let (k, s) = (keys.clone(), seen.clone());
    keys.on_change(Rc::new(move || s.borrow_mut().push(k.len())));
    let a = keys.add(1, "a", vec![entry(&log, "a", "x")]);
    let _b = keys.add(2, "b", vec![entry(&log, "b", "y"), entry(&log, "b", "z")]);
    drop(a);
    keys.remove_member(2);
    keys.remove_member(2);
    assert_eq!(*seen.borrow(), vec![1, 3, 2, 0], "each change once, nothing for nothing");
    assert!(hides(&sequence("g").unwrap(), &sequence("g b").unwrap()));
    assert!(!hides(&sequence("g b").unwrap(), &sequence("g").unwrap()));
    assert!(!hides(&sequence("b").unwrap(), &sequence("g b").unwrap()));
}

/// EDITOR-13: the compiler's twin — a chord the browser keeps is refused, so
/// `bus.keys` throws on it, naming why.
#[test]
fn a_chord_the_browser_keeps_is_refused() {
    for bad in ["ctrl+t", "ctrl+w", "ctrl+n", "ctrl+tab", "ctrl+shift+w", "meta+t", "meta+shift+n", "g ctrl+w"] {
        let why = sequence(bad).unwrap_err();
        assert!(why.contains("the browser's"), "{bad}: {why}");
    }
    for ok in ["ctrl+alt+t", "alt+n", "ctrl+s", "meta+s", "tab", "shift+tab", "ctrl+enter"] {
        assert!(sequence(ok).is_ok(), "{ok} is bindable");
    }
}