mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 19:13:28 +00:00
Trials: settled dock chrome, a drop on a settled group, a theme change and a settled menu bar's keys
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
//! The runs of a settled docked layout: each group adds the same runs whatever its tab count, a closed menu, the
|
||||
//! group's header controls and its tabs add none, and a panel body with a content version runs only when it changes.
|
||||
//! With a host spec version the groups' chrome settles too: it runs only for the pointer, an arrangement or spec
|
||||
//! change, or a body without a version.
|
||||
|
||||
use craft_behavior::Axis;
|
||||
use craft_behavior::dock::{Layout, Node, SplitSize, Workspace};
|
||||
use craft_behavior::dock::{Action, Floating, Layout, Node, Permissions, SplitSize, Workspace};
|
||||
use craft_ui::contract::{Bind, BodyCx};
|
||||
use craft_ui::docking::{DockConfig, DockPanels, PanelSpec};
|
||||
use craft_ui::docking::{DockConfig, DockPanels, PanelDrag, PanelSpec};
|
||||
use craft_ui::menu::PanelRow;
|
||||
use craft_ui::patterns::dock::DockArea;
|
||||
use craft_ui::runtime::drag;
|
||||
use craft_ui::testing::pointer::{moved, press};
|
||||
use craft_ui::testing::{Fixture, last_pass_runs, trial};
|
||||
use egui::{Ui, Vec2};
|
||||
use std::cell::Cell;
|
||||
use egui::{Pos2, Rect, Ui, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
struct Panels;
|
||||
@@ -116,3 +120,130 @@ fn a_panel_body_with_a_content_version_runs_only_when_its_version_changes() {
|
||||
assert!(frame()[0] > 0, "a new version runs the versioned body");
|
||||
assert_eq!(frame()[0], 0, "and it settles at it");
|
||||
}
|
||||
|
||||
/// Panels whose specs and bodies all have versions, so the dock's chrome settles; `plain` is a panel whose body has
|
||||
/// none.
|
||||
struct Settling {
|
||||
spec: Rc<Cell<u64>>,
|
||||
plain: Option<usize>,
|
||||
}
|
||||
|
||||
impl DockPanels<usize> for Settling {
|
||||
fn spec(&self, panel: &usize) -> PanelSpec {
|
||||
PanelSpec::new(format!("Panel {panel}"))
|
||||
}
|
||||
|
||||
fn body(&mut self, _: &usize, _: &mut Ui, _: &mut BodyCx<'_>) {}
|
||||
|
||||
fn content(&self, panel: &usize) -> Option<u64> {
|
||||
(self.plain != Some(*panel)).then_some(1)
|
||||
}
|
||||
|
||||
fn spec_version(&self) -> Option<u64> {
|
||||
Some(self.spec.get())
|
||||
}
|
||||
|
||||
fn panel_rows(&mut self, _: &usize) -> Vec<PanelRow> {
|
||||
vec![PanelRow::Separator]
|
||||
}
|
||||
}
|
||||
|
||||
/// A settled dock over `layout` whose panels are [`Settling`]: the fixture, the spec version it reads and the
|
||||
/// workspace it shows.
|
||||
fn settling(layout: Layout<usize>, plain: Option<usize>) -> (Fixture, Rc<Cell<u64>>, Rc<RefCell<Workspace<usize>>>) {
|
||||
let spec = Rc::new(Cell::new(1));
|
||||
let workspace = Rc::new(RefCell::new(Workspace::new(layout)));
|
||||
let mut panels = Settling { spec: Rc::clone(&spec), plain };
|
||||
let shown = Rc::clone(&workspace);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
DockArea::new("Dock", Bind::bound(&mut *shown.borrow_mut()), &mut panels, DockConfig::default()).show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(1200.0, 600.0)).settle().idle(1);
|
||||
(fixture, spec, workspace)
|
||||
}
|
||||
|
||||
/// `groups` groups side by side, two tabs each.
|
||||
fn side_by_side(groups: usize) -> Layout<usize> {
|
||||
let group = |at: usize| Node::tabs(vec![2 * at, 2 * at + 1], 0);
|
||||
let root = (1..groups).fold(group(0), |left, at| Node::split(Axis::X, SplitSize::Ratio(0.5), left, group(at)));
|
||||
Layout::new(Some(root), Vec::new())
|
||||
}
|
||||
|
||||
/// How many runs of the types named `names` the last pass made.
|
||||
fn runs_of(fixture: &Fixture, names: &[&str]) -> u32 {
|
||||
last_pass_runs(fixture.ctx()).iter().filter(|(ty, _)| names.contains(&short(ty))).map(|(_, runs)| runs).sum()
|
||||
}
|
||||
|
||||
const CHROME: &[&str] = &["DockGroup", "TabStrip", "Tab", "GroupGrip"];
|
||||
|
||||
/// At rest a dock whose host versions its specs and bodies runs none of its groups' chrome, however many groups it
|
||||
/// shows, and still paints their tabs.
|
||||
#[trial]
|
||||
fn a_settled_dock_runs_no_group_chrome_at_rest() {
|
||||
for groups in 1..=3 {
|
||||
let (mut fixture, _, _) = settling(side_by_side(groups), None);
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(runs_of(&fixture, CHROME), 0, "{groups} settled groups ran chrome: {:?}", last_pass_runs(fixture.ctx()));
|
||||
let painted = |title: &str| fixture.texts().iter().any(|text| text.text == title);
|
||||
assert!((0..2 * groups).all(|panel| painted(&format!("Panel {panel}"))), "the replayed tabs paint their titles");
|
||||
}
|
||||
}
|
||||
|
||||
/// The pointer over one group's tab runs that group alone; the others replay.
|
||||
#[trial]
|
||||
fn hovering_a_tab_runs_only_its_group() {
|
||||
let (mut fixture, _, _) = settling(side_by_side(3), None);
|
||||
let tab = fixture.by_label("Panel 4").expect("the third group's tab");
|
||||
fixture.frame(vec![egui::Event::PointerMoved(tab.center())]);
|
||||
assert_eq!(runs_of(&fixture, &["DockGroup"]), 1, "{:?}", last_pass_runs(fixture.ctx()));
|
||||
}
|
||||
|
||||
/// An edit of the arrangement (a tab picked from outside the dock) runs every group once, and a change of the host's
|
||||
/// spec version does too; the frame after settles again.
|
||||
#[trial]
|
||||
fn an_arrangement_or_spec_change_runs_every_group_once() {
|
||||
let (mut fixture, spec, workspace) = settling(side_by_side(3), None);
|
||||
workspace.borrow_mut().apply(Action::activate(1), false, |_| Permissions::ALL).expect("panel 1 is shown");
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(runs_of(&fixture, &["DockGroup"]), 3, "the edit runs every group");
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(runs_of(&fixture, &["DockGroup"]), 0, "and they settle at it");
|
||||
spec.set(2);
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(runs_of(&fixture, &["DockGroup"]), 3, "a new spec version runs every group");
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(runs_of(&fixture, &["DockGroup"]), 0, "and they settle at it");
|
||||
}
|
||||
|
||||
/// A group whose shown body has no content version runs every pass, as its body does; the others settle.
|
||||
#[trial]
|
||||
fn a_group_whose_body_has_no_version_keeps_running() {
|
||||
let (mut fixture, _, _) = settling(side_by_side(2), Some(0));
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(runs_of(&fixture, &["DockGroup"]), 1, "{:?}", last_pass_runs(fixture.ctx()));
|
||||
}
|
||||
|
||||
/// A floating window's groups settle as the docked ones do.
|
||||
#[trial]
|
||||
fn a_settled_floating_window_runs_no_group_chrome() {
|
||||
let window = Floating::single(vec![9], 0, Rect::from_min_size(Pos2::new(600.0, 100.0), Vec2::new(300.0, 300.0)));
|
||||
let (mut fixture, _, _) = settling(Layout::new(Some(Node::tabs(vec![0], 0)), vec![window]), None);
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(runs_of(&fixture, CHROME), 0, "{:?}", last_pass_runs(fixture.ctx()));
|
||||
}
|
||||
|
||||
/// A tab dragged straight onto the strip of a group that replayed its last run finds the group's drop spot in the
|
||||
/// frame it arrives: the replay registered the spots again.
|
||||
#[trial]
|
||||
fn a_tab_dropped_on_a_settled_group_lands_there() {
|
||||
let (mut fixture, _, workspace) = settling(side_by_side(3), None);
|
||||
let (from, to) = (fixture.by_label("Panel 0").expect("a tab").center(), fixture.by_label("Panel 4").expect("a tab").center());
|
||||
fixture.frame(vec![moved(from)]).frame(vec![press(from)]).frame(vec![moved(from + Vec2::new(0.0, 12.0))]);
|
||||
assert!(drag::active::<PanelDrag>(fixture.ctx()).is_some(), "the tab drag started");
|
||||
fixture.frame(vec![moved(to)]);
|
||||
assert!(drag::mark(fixture.ctx()).is_some(), "the settled group's strip takes the drop in the frame the pointer arrives");
|
||||
fixture.release_at(to).idle(1);
|
||||
let layout = &workspace.borrow().layout;
|
||||
let group = layout.place(&4).and_then(|place| layout.node_of(&place));
|
||||
assert!(matches!(group, Some(Node::Tabs { panels, .. }) if panels.contains(&0)), "the tab joined the third group: {group:?}");
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
//! Settling children (`Lazy::settles`): a settled child adds its last run to the pass in place of running, and runs
|
||||
//! when something it shows may have changed: the pointer over it, a bound value, inspection record or command state it
|
||||
//! read, its room, its content version, an animation or a repaint asked inside it.
|
||||
//! read, its room, its content version, its environment (theme, density), an animation or a repaint asked inside it.
|
||||
|
||||
use crate::values::kit::{AT, INITIAL, Scrub, Seen};
|
||||
use craft_theme::{Contrast, Mode, Size};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::choice::{Radio, RadioGroup};
|
||||
use craft_ui::contract::{Bind, CommandSet, Lazy, Props as _, Show as _, ShowContainer as _, Sizing};
|
||||
@@ -12,7 +13,7 @@ use craft_ui::runtime::commands::{CommandInput, CommandScope, CommandState, Comm
|
||||
use craft_ui::runtime::{frame, inspect};
|
||||
use craft_ui::testing::layout::in_region;
|
||||
use craft_ui::testing::pointer::{moved, press, release};
|
||||
use craft_ui::testing::{Frames, last_pass_measuring_runs, trial};
|
||||
use craft_ui::testing::{Fixture, Frames, last_pass_measuring_runs, trial};
|
||||
use egui::{Id, Pos2, Rect, Ui, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
@@ -431,3 +432,29 @@ fn a_press_with_the_pointer_arriving_reaches_a_member_of_a_settled_child() {
|
||||
frames.frame(vec![release(medium)], &mut show);
|
||||
assert_eq!(picked.get(), 2, "the press that came with the pointer picks Medium");
|
||||
}
|
||||
|
||||
/// A child with a content version runs when the theme changes, whose tokens it painted with, and when the density
|
||||
/// does; it settles again after each.
|
||||
#[trial]
|
||||
fn a_settled_child_runs_when_the_theme_or_density_changes() {
|
||||
let runs = std::rc::Rc::new(Cell::new(0));
|
||||
let counted = std::rc::Rc::clone(&runs);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Lazy::new("themed", 0.0).settles(true).content(1).show(ui, |ui| {
|
||||
counted.set(counted.get() + 1);
|
||||
Button::new("Themed").show(ui);
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let mut frame = |fixture: &mut Fixture| {
|
||||
runs.set(0);
|
||||
fixture.frame(Vec::new());
|
||||
runs.get()
|
||||
};
|
||||
assert_eq!(frame(&mut fixture), 0, "the child settles");
|
||||
fixture.theme(Mode::Light, Contrast::High);
|
||||
assert!(frame(&mut fixture) > 0, "a theme change runs it");
|
||||
assert_eq!(frame(&mut fixture), 0, "and it settles in the new theme");
|
||||
fixture.density(Size::Comfortable);
|
||||
assert!(frame(&mut fixture) > 0, "a density change runs it");
|
||||
}
|
||||
|
||||
@@ -146,3 +146,28 @@ fn a_pointer_move_over_menu_rows_lights_the_row_under_it_in_that_frame() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A menu bar that settles (in a title bar slot with a content version) still takes its keys while it replays its
|
||||
/// last run: F10 takes the focus to the bar, and Alt with a title's marked letter opens that menu.
|
||||
#[trial]
|
||||
fn a_settled_menu_bar_takes_its_mnemonics_and_f10() {
|
||||
use craft_ui::contract::Lazy;
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
Lazy::new("title", 0.0).settles(true).content(1).show(ui, |ui| {
|
||||
let titles = vec![MenuTitle::new("&File", |ui| {
|
||||
MenuItem::new("&New").show(ui);
|
||||
})];
|
||||
MenuBar::new("Main menu", titles).show(ui);
|
||||
});
|
||||
});
|
||||
fixture.settle().idle(2);
|
||||
assert!(fixture.focused_stop().is_none(), "nothing holds the focus at the start");
|
||||
fixture.key(Key::F10, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert!(fixture.focused_stop().is_some(), "F10 takes the focus to the bar");
|
||||
fixture.key(Key::Escape, Modifiers::NONE);
|
||||
fixture.settle().idle(2);
|
||||
fixture.key(Key::F, Modifiers::ALT);
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("New").is_some(), "Alt+F opens File");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user