mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
# Conflicts: # crates/automation/Cargo.toml # crates/automation/src/access.rs # crates/automation/src/synthetic.rs # crates/automation/tests/it/access.rs # crates/automation/tests/it/synthetic.rs # crates/behavior/src/codec.rs # crates/behavior/src/edit_history.rs # crates/behavior/src/lib.rs # crates/behavior/src/wheel.rs # crates/color-ui/src/ui/paint_chip.rs # crates/color-ui/src/ui/pair.rs # crates/color-ui/src/ui/palette.rs # crates/color-ui/src/ui/palette/stories.rs # crates/dialogs/src/service.rs # crates/draw/src/ui/transform/transform_box.rs # crates/draw/src/ui/transform/transform_box/stories.rs # crates/draw/tests/it/transform_box.rs # crates/font-manager/src/glyphs.rs # crates/font-manager/src/picker.rs # crates/font-manager/src/picker/rank.rs # crates/font-manager/src/picker/rows.rs # crates/font-manager/src/picker/stories.rs # crates/font-manager/tests/it/picker.rs # crates/host/src/lib.rs # crates/menu/src/context.rs # crates/menu/src/mac.rs # crates/menu/src/model.rs # crates/pages/src/ui/view.rs # crates/platform/README.md # crates/platform/src/browser_files.rs # crates/print/src/ui/dialog.rs # crates/print/src/ui/mod.rs # crates/print/src/ui/panel.rs # crates/print/src/ui/stories.rs # crates/print/tests/it/dialog.rs # crates/text/src/ui/character.rs # crates/text/src/ui/character/rows.rs # crates/text/src/ui/editor.rs # crates/text/tests/it/editor.rs # crates/text/tests/it/main.rs # crates/ui-canvas/src/canvas/mod.rs # crates/ui-canvas/src/canvas/split/stories.rs # crates/ui-canvas/src/canvas/tool_host.rs # crates/ui-canvas/src/canvas_controls/outline/mod.rs # crates/ui-canvas/src/canvas_controls/task_bar.rs # crates/ui-canvas/src/canvas_controls/task_bar/stories.rs # crates/ui-canvas/tests/it/canvas/navigate.rs # crates/ui-canvas/tests/it/canvas/tool_keys.rs # crates/ui-canvas/tests/it/canvas_controls/task_bar.rs # crates/ui-contract/src/contract/collection/props.rs # crates/ui-contract/src/contract/cx/pointer.rs # crates/ui-contract/src/contract/items/access.rs # crates/ui-contract/src/contract/items/drag.rs # crates/ui-contract/src/contract/items/lead.rs # crates/ui-contract/src/contract/label.rs # crates/ui-contract/src/contract/mod.rs # crates/ui-contract/src/contract/run.rs # crates/ui-contract/src/contract/steps/report.rs # crates/ui-contract/src/contract/steps/update.rs # crates/ui-contract/src/primitives/mod.rs # crates/ui-contract/src/primitives/sensing.rs # crates/ui-contract/src/primitives/text_adapter.rs # crates/ui-contract/src/primitives/text_input.rs # crates/ui-contract/src/runtime/access/emit.rs # crates/ui-contract/src/runtime/access/text.rs # crates/ui-contract/src/runtime/commands/board.rs # crates/ui-contract/src/runtime/commands/mod.rs # crates/ui-contract/src/runtime/drag.rs # crates/ui-contract/src/runtime/egui/access.rs # crates/ui-contract/src/runtime/egui/access/tree.rs # crates/ui-contract/src/runtime/egui/input.rs # crates/ui-contract/src/runtime/env.rs # crates/ui-contract/src/runtime/focus/nav.rs # crates/ui-contract/src/runtime/frame.rs # crates/ui-contract/src/runtime/keys.rs # crates/ui-contract/src/runtime/keys/claims.rs # crates/ui-contract/src/runtime/keys/input.rs # crates/ui-contract/src/runtime/keys/route.rs # crates/ui-contract/src/runtime/pointer.rs # crates/ui-contract/src/runtime/windows.rs # crates/ui-contract/src/testing/fixture.rs # crates/ui-contract/src/testing/host_key_trials.rs # crates/ui-contract/tests/it/collection/rows.rs # crates/ui-contract/tests/it/drag/mod.rs # crates/ui-contract/tests/it/runtime/wheel.rs # crates/ui-macros/src/props.rs # crates/ui-panels/src/dialog/mod.rs # crates/ui-panels/src/dialog/prompts.rs # crates/ui-panels/src/dialog/recovery.rs # crates/ui-panels/src/dialog/trials.rs # crates/ui-panels/src/dialog/words.rs # crates/ui-panels/src/rails/drawer.rs # crates/ui-panels/src/rails/drawer/stories.rs # crates/ui-panels/src/rails/tool/trials.rs # crates/ui-panels/src/sections/footer.rs # crates/ui-panels/tests/it/rails/drawer.rs # crates/ui-patterns/src/patterns/dock/area.rs # crates/ui-patterns/src/patterns/form_dialogs/about.rs # crates/ui-patterns/src/patterns/form_dialogs/dialog.rs # crates/ui-patterns/src/patterns/form_dialogs/dialog/stories.rs # crates/ui-patterns/src/patterns/form_dialogs/host_requests.rs # crates/ui-patterns/src/patterns/form_dialogs/mod.rs # crates/ui-patterns/src/patterns/form_dialogs/param.rs # crates/ui-patterns/src/patterns/form_dialogs/properties.rs # crates/ui-patterns/src/patterns/form_dialogs/properties/stories.rs # crates/ui-patterns/src/patterns/form_dialogs/rows.rs # crates/ui-patterns/src/patterns/form_dialogs/rows/stories.rs # crates/ui-patterns/src/patterns/panels/history.rs # crates/ui-patterns/src/patterns/preferences/document/model.rs # crates/ui-patterns/src/patterns/preferences/model.rs # crates/ui-patterns/src/patterns/preferences/page.rs # crates/ui-patterns/src/patterns/preferences/rows.rs # crates/ui-patterns/src/patterns/preferences/window.rs # crates/ui-patterns/src/patterns/preferences/window/stories.rs # crates/ui-patterns/src/patterns/presets/browser.rs # crates/ui-patterns/tests/it/docking/group_view.rs # crates/ui-patterns/tests/it/form_dialogs/dialog.rs # crates/ui-patterns/tests/it/form_dialogs/params.rs # crates/ui-patterns/tests/it/preferences/model.rs # crates/ui-shell/src/shell/start.rs # crates/ui-shell/src/shell/start/stories.rs # crates/ui-widgets/src/buttons/icon_button.rs # crates/ui-widgets/src/buttons/mod.rs # crates/ui-widgets/src/buttons/stories.rs # crates/ui-widgets/src/choice/swatch.rs # crates/ui-widgets/src/choice/swatch_table.rs # crates/ui-widgets/src/display/text.rs # crates/ui-widgets/src/form/slider_field.rs # crates/ui-widgets/src/layout_components/mod.rs # crates/ui-widgets/src/list/mod.rs # crates/ui-widgets/src/list/row/paint.rs # crates/ui-widgets/src/list/row/show.rs # crates/ui-widgets/src/list/stories.rs # crates/ui-widgets/src/list/view.rs # crates/ui-widgets/src/list/view_stories.rs # crates/ui-widgets/src/menu/mod.rs # crates/ui-widgets/src/menu/stories.rs # crates/ui-widgets/src/pickers/select.rs # crates/ui-widgets/src/tabs/document.rs # crates/ui-widgets/src/text/number.rs # crates/ui-widgets/src/text/zoom_field.rs # crates/ui-widgets/src/tile_grid/grid.rs # crates/ui-widgets/src/tile_grid/grid/stories.rs # crates/ui-widgets/src/tile_grid/mod.rs # crates/ui-widgets/tests/it/label/tooltip.rs # crates/ui-widgets/tests/it/popover/popup.rs # crates/ui-widgets/tests/it/tabs.rs # crates/ui-widgets/tests/it/text/mod.rs # crates/ui-widgets/tests/it/text/number.rs # crates/ui-widgets/tests/it/tile_grid/grid.rs # crates/window/Cargo.toml # docs/components-index.md # docs/porting.md
242 lines
10 KiB
Rust
242 lines
10 KiB
Rust
use super::kit::{AppCommand, Dock, DockCommand, PaneCommand, Silent, Tap};
|
|
use craft_ui::contract::{Props as _, Show as _, ShowContainer as _, Tracked};
|
|
use craft_ui::runtime::commands::{
|
|
CommandGate, CommandInput, CommandRef, CommandState, CommandStates, Commands as _, EditCommands, FirePhase, FireSource, Fired, chord_text_builds,
|
|
fire, resolve,
|
|
};
|
|
use craft_ui::runtime::focus::{FocusTarget, request};
|
|
use craft_ui::runtime::frame::take_output;
|
|
use craft_ui::runtime::measure::chord_texts;
|
|
use craft_ui::testing::focus::key_release;
|
|
use craft_ui::testing::input::key_press;
|
|
use craft_ui::testing::pointer::{moved, press, release};
|
|
use craft_ui::testing::{Frames, trial};
|
|
use egui::{Key, Modifiers, Pos2, Ui};
|
|
use std::cell::Cell;
|
|
|
|
fn ids(fired: &[Fired]) -> Vec<&str> {
|
|
fired.iter().map(|f| f.command.id.as_str()).collect()
|
|
}
|
|
|
|
/// Hovers, presses and releases at `at` over three frames and settles for one; the first pass of the release
|
|
/// frame asks for a discard pass when `discard` is set.
|
|
fn click_at(frames: &mut Frames, at: Pos2, show: &mut impl FnMut(&mut Ui), discard: bool) {
|
|
frames.frame(vec![moved(at)], &mut *show);
|
|
frames.frame(vec![press(at)], &mut *show);
|
|
let mut first = discard;
|
|
frames.frame(vec![release(at)], |ui| {
|
|
show(ui);
|
|
if std::mem::take(&mut first) {
|
|
ui.ctx().request_discard("command trial");
|
|
}
|
|
});
|
|
frames.frame(vec![], &mut *show);
|
|
}
|
|
|
|
fn tap_center(frames: &mut Frames, command: AppCommand) -> Pos2 {
|
|
let mut center = Pos2::ZERO;
|
|
frames.idle(2, |ui| center = Tap::new().id_salt("go").command(command).show(ui).rect.center());
|
|
center
|
|
}
|
|
|
|
/// Prototype: `discarded_pass_delivers_a_click_and_an_edit_once` (the click).
|
|
#[trial]
|
|
fn a_command_fired_in_a_discarded_pass_is_delivered_once() {
|
|
let mut frames = Frames::new();
|
|
let at = tap_center(&mut frames, AppCommand::Go);
|
|
let mut show = |ui: &mut Ui| {
|
|
Tap::new().id_salt("go").command(AppCommand::Go).show(ui);
|
|
};
|
|
click_at(&mut frames, at, &mut show, true);
|
|
assert_eq!(ids(&take_output(&frames.ctx).fired), ["trial.go"]);
|
|
frames.idle(2, &mut show);
|
|
assert!(take_output(&frames.ctx).fired.is_empty(), "nothing fires again in later frames");
|
|
}
|
|
|
|
fn focus_on(frames: &mut Frames, id: egui::Id, show: &mut impl FnMut(&mut Ui)) {
|
|
request(&frames.ctx, FocusTarget::Id(id));
|
|
frames.frame(vec![], &mut *show);
|
|
}
|
|
|
|
/// 9.3 and 11.3: with focus in a pane inside a dock, the dock's shortcut beats an application shortcut on the same
|
|
/// chord; with focus outside, the application's does.
|
|
#[trial]
|
|
fn an_enclosing_scopes_shortcut_beats_the_applications() {
|
|
let tap = Cell::new(None);
|
|
let closed = Cell::new(0);
|
|
let outside = Cell::new(None);
|
|
let mut show = |ui: &mut Ui| {
|
|
Dock::new().id_salt("dock").show(ui, |ui, cx| {
|
|
while cx.take_command::<DockCommand>().is_some() {
|
|
closed.set(closed.get() + 1);
|
|
}
|
|
super::kit::Pane::new().id_salt("pane").show(ui, |ui, _| tap.set(Some(Tap::new().id_salt("in").show(ui).id)));
|
|
});
|
|
outside.set(Some(Tap::new().id_salt("out").show(ui).id));
|
|
};
|
|
let mut frames = Frames::new();
|
|
frames.idle(3, &mut show);
|
|
focus_on(&mut frames, tap.get().unwrap(), &mut show);
|
|
// Each press is released: a second press with no release between is a repeat.
|
|
let chord = || vec![key_press(Key::J, Modifiers::COMMAND | Modifiers::ALT), key_release(Key::J, Modifiers::COMMAND | Modifiers::ALT)];
|
|
frames.frame(chord(), &mut show);
|
|
assert_eq!((closed.get(), take_output(&frames.ctx).fired.len()), (1, 0), "the dock takes it");
|
|
focus_on(&mut frames, outside.get().unwrap(), &mut show);
|
|
frames.frame(chord(), &mut show);
|
|
assert_eq!((closed.get(), ids(&take_output(&frames.ctx).fired)), (1, vec!["trial.jump"]), "outside the dock the application's command fires");
|
|
}
|
|
|
|
/// 11.3: a key whose app-scope command only the app's own state disables is declined, in order, once, though a discard
|
|
/// pass reruns its frame; one the gate also refuses is not, and none of them fires.
|
|
#[trial]
|
|
fn a_disabled_commands_key_is_declined_and_a_gated_one_is_not() {
|
|
let mut frames = Frames::new();
|
|
let mut states = Tracked::new(CommandStates::new());
|
|
for command in [AppCommand::Play, AppCommand::Save, AppCommand::Jump] {
|
|
states.set(command, CommandState::new().enabled(false));
|
|
}
|
|
frames.commands(
|
|
CommandInput::new().states(&states).gate(CommandGate::new((), |_: &(), command: &CommandRef| *command != CommandRef::from(AppCommand::Jump))),
|
|
);
|
|
let mut show = |ui: &mut Ui| {
|
|
Tap::new().id_salt("go").command(AppCommand::Go).show(ui);
|
|
};
|
|
frames.idle(2, &mut show);
|
|
let keys = vec![
|
|
key_press(Key::F8, Modifiers::NONE),
|
|
key_press(Key::J, Modifiers::COMMAND | Modifiers::ALT),
|
|
key_press(Key::K, Modifiers::COMMAND | Modifiers::ALT | Modifiers::SHIFT),
|
|
];
|
|
let mut first = true;
|
|
frames.frame(keys, |ui| {
|
|
show(ui);
|
|
if std::mem::take(&mut first) {
|
|
ui.ctx().request_discard("declined trial");
|
|
}
|
|
});
|
|
frames.idle(1, &mut show);
|
|
let output = take_output(&frames.ctx);
|
|
assert_eq!(ids(&output.declined), ["trial.play", "trial.save"], "the state-disabled keys, in order, and not the gated one");
|
|
assert!(output.declined.iter().all(|declined| declined.source == FireSource::Key && declined.phase == FirePhase::Press));
|
|
assert!(output.fired.is_empty(), "a declined key fires nothing");
|
|
frames.idle(2, &mut show);
|
|
assert!(take_output(&frames.ctx).declined.is_empty(), "nothing is declined again in later frames");
|
|
}
|
|
|
|
/// 11.3: a scoped entry waits for its instance and is dropped when the instance stops being live.
|
|
#[trial]
|
|
fn an_untaken_scoped_entry_is_dropped_with_its_instance() {
|
|
let panes = Cell::new(true);
|
|
let taking = Cell::new(false);
|
|
let taken = Cell::new(0);
|
|
let mut show = |ui: &mut Ui| {
|
|
if panes.get() {
|
|
super::kit::Pane::new().id_salt("a").show(ui, |_, cx| {
|
|
if taking.get() {
|
|
taken.set(taken.get() + usize::from(cx.take_command::<PaneCommand>().is_some()));
|
|
}
|
|
});
|
|
}
|
|
};
|
|
let mut frames = Frames::new();
|
|
frames.idle(3, &mut show);
|
|
assert!(fire(&frames.ctx, PaneCommand::ZoomIn, FireSource::Palette));
|
|
panes.set(false);
|
|
frames.idle(3, &mut show);
|
|
panes.set(true);
|
|
taking.set(true);
|
|
frames.idle(3, &mut show);
|
|
assert_eq!(taken.get(), 0, "the entry died with its instance");
|
|
}
|
|
|
|
/// 11.4: a modal layer blocks the application's `ModalRule::Declared` edit commands, except for an instance inside it
|
|
/// that declares them: a dialog that publishes the undo state takes it, and the command resolves enabled there.
|
|
#[trial]
|
|
fn undo_in_a_dialog_reaches_the_dialog_and_not_the_application() {
|
|
use craft_ui::contract::Bind;
|
|
use craft_ui::dialog::Dialog;
|
|
use craft_ui::runtime::focus::Initial;
|
|
use craft_ui::sections::{DialogRole, FooterButton};
|
|
|
|
let undone = Cell::new(0);
|
|
let mut show = |ui: &mut Ui| {
|
|
let mut open = true;
|
|
Dialog::new("Options")
|
|
.opened(Bind::bound(&mut open))
|
|
.initial_focus(Initial::First)
|
|
.buttons([FooterButton::new("OK", DialogRole::Accept)])
|
|
.show(ui, |_, cx| {
|
|
cx.publish(EditCommands::Undo, CommandState::new());
|
|
while let Some((command, _)) = cx.take_command::<EditCommands>() {
|
|
if command == EditCommands::Undo {
|
|
undone.set(undone.get() + 1);
|
|
}
|
|
}
|
|
});
|
|
};
|
|
let mut frames = Frames::new();
|
|
frames.idle(4, &mut show);
|
|
assert!(resolve(&frames.ctx, EditCommands::Undo).enabled, "the declared undo is enabled in the dialog");
|
|
assert!(!resolve(&frames.ctx, EditCommands::Redo).enabled, "the redo the dialog does not declare is not");
|
|
frames.frame(vec![key_press(Key::Z, Modifiers::COMMAND)], &mut show);
|
|
frames.idle(2, &mut show);
|
|
assert_eq!(undone.get(), 1, "the dialog took the undo");
|
|
assert!(take_output(&frames.ctx).fired.is_empty(), "the application's undo stays blocked");
|
|
}
|
|
|
|
/// Count relation: an access report reads a chord text for each command-bound node that exists, at any number of nodes,
|
|
/// never for a component with no node, and writes none; the texts are written when the keymap is built.
|
|
#[trial]
|
|
fn access_reports_read_chord_texts_for_their_nodes_and_write_none() {
|
|
let mut frames = Frames::new();
|
|
let mut built = 0;
|
|
for count in [6, 12] {
|
|
let mut show = |ui: &mut Ui| {
|
|
for salt in 0..count {
|
|
Tap::new().id_salt(salt).command(AppCommand::Save).show(ui);
|
|
}
|
|
Tap::new().id_salt("go").command(AppCommand::Go).show(ui);
|
|
Silent::new().id_salt("silent").command(AppCommand::Save).show(ui);
|
|
};
|
|
frames.idle(3, &mut show);
|
|
if built == 0 {
|
|
built = chord_text_builds();
|
|
}
|
|
frames.idle(3, &mut show);
|
|
assert_eq!(chord_text_builds(), built, "passes write no chord text");
|
|
assert_eq!(chord_texts(&frames.ctx), count, "one read per node whose command has a chord, none for a component with no node");
|
|
}
|
|
}
|
|
|
|
/// An instance that stops registering and registers again goes last in registration order, which a non-component
|
|
/// source's pick follows (the first live instance); lookups by id stay right throughout.
|
|
#[trial]
|
|
fn a_re_registered_instance_goes_last_in_the_pick() {
|
|
let a_shown = Cell::new(true);
|
|
let taker = Cell::new("");
|
|
let mut show = |ui: &mut Ui| {
|
|
for name in ["a", "b", "c"] {
|
|
if name == "a" && !a_shown.get() {
|
|
continue;
|
|
}
|
|
super::kit::Pane::new().id_salt(name).show(ui, |_, cx| {
|
|
if cx.take_command::<PaneCommand>().is_some() {
|
|
taker.set(name);
|
|
}
|
|
});
|
|
}
|
|
};
|
|
let mut frames = Frames::new();
|
|
frames.idle(3, &mut show);
|
|
assert!(fire(&frames.ctx, PaneCommand::ZoomIn, FireSource::Palette));
|
|
frames.idle(2, &mut show);
|
|
assert_eq!(taker.get(), "a", "registered first");
|
|
a_shown.set(false);
|
|
frames.idle(3, &mut show);
|
|
a_shown.set(true);
|
|
frames.idle(3, &mut show);
|
|
assert!(fire(&frames.ctx, PaneCommand::ZoomIn, FireSource::Palette));
|
|
frames.idle(2, &mut show);
|
|
assert_eq!(taker.get(), "b", "a registered again after b and c");
|
|
}
|