Trials and a short-room story for the overlay bar and the named region mark

This commit is contained in:
aiongg
2026-11-17 23:44:00 +00:00
parent 2121d228ed
commit f873abce2c
6 changed files with 125 additions and 8 deletions
+43 -6
View File
@@ -14,7 +14,7 @@ use craft_ui::testing::pointer::{moved, press, release};
use craft_ui::testing::windows::FakeSystem;
use craft_ui::testing::{Frames, trial};
use craft_ui_test_kit::windows::Windows;
use egui::{Pos2, Rect, Response, Ui, Vec2, ViewportBuilder, ViewportId, ViewportInfo};
use egui::{Pos2, Rect, Response, Shape, Ui, Vec2, ViewportBuilder, ViewportId, ViewportInfo};
use std::cell::RefCell;
use std::sync::Arc;
@@ -26,6 +26,9 @@ const SLOT: Rect = Rect { min: Pos2::new(200.0, 10.0), max: Pos2::new(300.0, 60.
const BADGE: &str = "3 files";
/// The name a named drag shows.
const NAME: &str = "Layers";
#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)]
#[non_exhaustive]
pub struct PlainStyle {
@@ -84,7 +87,8 @@ impl Component for Chip<'_> {
}
}
struct Accepts;
/// Takes every drop and marks it with its mark.
struct Accepts(DropMark);
impl DropTarget<Payload> for Accepts {
type Drop = (u32, DropOperation);
@@ -94,16 +98,17 @@ impl DropTarget<Payload> for Accepts {
}
fn mark(&self, _: &Self::Drop) -> DropMark {
DropMark::Into(SLOT)
self.0
}
}
/// Takes drops of the payload.
/// Takes drops of the payload, and marks them with `mark`.
#[derive(Component)]
struct Slot<'a> {
#[common]
common: Common,
seen: &'a RefCell<Seen>,
mark: DropMark,
}
impl Component for Slot<'_> {
@@ -120,7 +125,7 @@ impl Component for Slot<'_> {
}
fn update(&mut self, cx: &mut Cx<'_, Self>) {
if let Some(drop) = cx.drop_target(&Accepts) {
if let Some(drop) = cx.drop_target(&Accepts(self.mark)) {
self.seen.borrow_mut().dropped.push(drop);
}
}
@@ -130,6 +135,38 @@ impl Component for Slot<'_> {
}
}
/// A drag whose preview names its payload shows nothing away from every target, and its name once, inside the region a
/// target marks.
#[trial]
fn a_named_drag_shows_its_name_inside_a_region_mark_and_nothing_without_a_mark() {
let seen = RefCell::new(Seen::default());
let mut show = |ui: &mut Ui| {
Chip { common: Common::new(), seen: &seen, preview: DragPreview::Named(Label::Static(NAME)) }.id_salt("chip").show_at(ui, CHIP);
Slot { common: Common::new(), seen: &seen, mark: DropMark::region(SLOT) }.id_salt("slot").show_at(ui, SLOT);
};
let mut frames = Frames::new();
let (from, open) = (CHIP.center(), Pos2::new(120.0, 200.0));
frames.frame(vec![moved(from)], &mut show);
frames.frame(vec![press(from)], &mut show);
for step in 1..=4 {
frames.frame(vec![moved(from + (open - from) * (step as f32 / 4.0))], &mut show);
}
assert!(drag::mark(&frames.ctx).is_none(), "no target takes the drag in open room");
let away = texts(&frames.frame_shapes(&mut show));
assert!(away.is_empty(), "nothing shows beside the pointer without a mark: {away:?}");
frames.frame(vec![moved(SLOT.center())], &mut show);
frames.idle(1, &mut show);
assert!(matches!(drag::mark(&frames.ctx), Some(DropMark::Region { .. })), "the slot marks the region a drop takes");
let over = texts(&frames.frame_shapes(&mut show));
assert!(over.len() == 1 && SLOT.contains_rect(over[0]), "the name shows once, inside the region: {over:?}");
frames.frame(vec![release(SLOT.center())], &mut show);
}
/// The bounds of the text shapes among `shapes`.
fn texts(shapes: &[Shape]) -> Vec<Rect> {
shapes.iter().filter(|shape| matches!(shape, Shape::Text(_))).map(Shape::visual_bounding_rect).collect()
}
/// A drag that starts in one window and ends over a target in another: the session maps the pointer to desktop
/// space, the other window's target takes the drop one frame after the release, and the source learns the
/// operation in the frame after that.
@@ -145,7 +182,7 @@ fn a_drag_crosses_windows_and_the_target_in_the_other_window_takes_the_drop() {
windows.frame(events, vec![], |ui| {
Chip { common: Common::new(), seen: &seen, preview: DragPreview::Badge(Label::Static(BADGE)) }.id_salt("chip").show_at(ui, CHIP);
ui.ctx().show_viewport_immediate(other, ViewportBuilder::default(), |other_ui, _| {
Slot { common: Common::new(), seen: &seen }.id_salt("slot").show_at(other_ui, SLOT);
Slot { common: Common::new(), seen: &seen, mark: DropMark::Into(SLOT) }.id_salt("slot").show_at(other_ui, SLOT);
});
});
};
@@ -1,3 +1,4 @@
mod body;
mod fill;
mod memory;
mod overlay_bar;
@@ -0,0 +1,57 @@
//! A scroll body with an overlay bar (`Scroll::overlay`): the content keeps the whole view, the bar lies in the
//! container's end padding, and it shows only while the pointer is over the container.
use craft_ui::contract::{ContainerProps, Pad, Props as _, Scroll, Show as _, ShowContainer as _, Sizing};
use craft_ui::runtime::env::{Direction, with_env};
use craft_ui::testing::{Frames, pointer, trial};
use craft_ui_test_kit::body::Slab;
use craft_ui_test_kit::contract::{Probe, Seen};
use egui::{Pos2, Rect, Shape, Ui, Vec2};
use std::cell::RefCell;
/// Half a pixel at the scales the box runs.
const SLACK: f32 = 0.5;
/// The container's rect: shorter than its content, so it scrolls.
const AT: Rect = Rect { min: Pos2::new(20.0, 20.0), max: Pos2::new(180.0, 120.0) };
/// A point outside the container.
const AWAY: Pos2 = Pos2::new(300.0, 300.0);
/// The padded container that scrolls along y under an overlay bar, holding a probe that fills its width and is three
/// times its height.
fn overlay(ui: &mut Ui, seen: &RefCell<Seen>) {
let props = ContainerProps::new().pad(Pad::Control).sizing(Sizing::Fill).scroll(Scroll::vertical().overlay());
Slab::new(props).id_salt("overlay").show_at(ui, AT, |ui, _| {
Probe::new(seen).size(Vec2::new(40.0, AT.height() * 3.0)).sizing(Sizing::FillWidth).id_salt("content").show(ui);
});
}
/// Whether any of `shapes` lies inside `band` (and paints something).
fn paints_in(shapes: &[Shape], band: Rect) -> bool {
shapes.iter().map(Shape::visual_bounding_rect).any(|bounds| bounds.is_positive() && band.expand(SLACK).contains_rect(bounds))
}
#[trial]
fn an_overlay_bar_takes_no_room_lies_in_the_end_padding_and_shows_only_under_the_pointer() {
for direction in [Direction::Ltr, Direction::Rtl] {
let seen = RefCell::new(Seen::default());
let mut show = |ui: &mut Ui| with_env(ui, |o| o.direction = Some(direction), |ui| overlay(ui, &seen));
let mut frames = Frames::new();
frames.frame(vec![pointer::moved(AWAY)], &mut show);
frames.idle(4, &mut show);
let content = seen.borrow().rect;
let (start, end) = (content.min.x - AT.min.x, AT.max.x - content.max.x);
assert!(start > 0.0 && (start - end).abs() < SLACK, "{direction:?}: the content keeps the whole view between equal pads: {content:?}");
let band = match direction {
Direction::Rtl => Rect::from_x_y_ranges(AT.min.x..=content.min.x, content.min.y.max(AT.min.y)..=AT.max.y),
_ => Rect::from_x_y_ranges(content.max.x..=AT.max.x, content.min.y.max(AT.min.y)..=AT.max.y),
};
let away = frames.frame_shapes(&mut show);
assert!(!paints_in(&away, band), "{direction:?}: no bar shows while the pointer is away");
frames.frame(vec![pointer::moved(Pos2::new(content.center().x, AT.center().y))], &mut show);
frames.idle(2, &mut show);
let over = frames.frame_shapes(&mut show);
assert!(paints_in(&over, band), "{direction:?}: the bar shows in the end padding while the pointer is over the container");
}
}
+9 -1
View File
@@ -26,6 +26,9 @@ pub(crate) enum StoryTools {
Stamp,
}
/// The height of the story's short room: lower than its rows at one column.
const SHORT_ROOM: f32 = 120.0;
/// The id of the brush tool; the tools that follow it are the pencil and the eraser.
pub(crate) const BRUSH: u8 = 1;
@@ -39,10 +42,12 @@ fn slots() -> Vec<ToolSlot<u8>> {
vec![ToolSlot::new(0, tools(&[0])), ToolSlot::new(1, tools(&[BRUSH, BRUSH + 1])), ToolSlot::new(2, tools(&[3])), ToolSlot::new(3, tools(&[4]))]
}
/// The rail in a room as wide as its grid at `columns` columns: the stage a dock column of that many steps gives it.
/// The rail in a room as wide as its grid at `columns` columns: the stage a dock column of that many steps gives it. A
/// short room is lower than the rail's rows, so they scroll under the overlay bar.
#[story(ToolRail<'static, u8>, covers = [Default], drive = Drive::Never)]
fn tool_rail(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let columns = *data.ranged("columns", 1_u8, 1.0..=4.0);
let short = *data.choice("room", false, &[("Tall", false), ("Short", true)]);
let hidden =
data.choice("hidden", Vec::<u8>::new(), &[("None", Vec::new()), ("Pencil", vec![2]), ("Stamp", vec![4]), ("Eraser", vec![3])]).clone();
let slots = slots();
@@ -52,6 +57,9 @@ fn tool_rail(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let out = ui
.scope(|ui| {
ui.set_max_width(width);
if short {
ui.set_max_height(SHORT_ROOM);
}
ToolRail::new("Tools", &slots, &BRUSH, &last).hidden(&hidden).edit_toolbar("Edit Toolbar").show_rail(ui, |_, _| {})
})
.inner;
@@ -108,6 +108,9 @@ fn in_a_short_room_the_rows_scroll_above_the_footer_that_stays_in_view() {
let first = rect(&fixture, "Move");
assert!(first.top() < foot.top(), "{height}: the first button is above the footer: {first:?}");
assert!(rect(&fixture, "Clone Stamp").bottom() > foot.top(), "{height}: the last button starts out of view");
// The bar lies in the end padding, so the buttons keep the middle of the rail.
let rail = rect(&fixture, "Tools");
assert!(near(first.center().x, rail.center().x), "{height}: the buttons stay centred in a rail that scrolls: {first:?} in {rail:?}");
fixture.frame(vec![moved(first.center())]);
for _ in 0..10 {
@@ -3,7 +3,7 @@
use craft_behavior::Axis;
use craft_behavior::dock::{Layout, Node, SplitSize, Workspace};
use craft_ui::contract::{Bind, BodyCx, Role};
use craft_ui::docking::{DockConfig, DockPanels, GroupAt, PanelSpec};
use craft_ui::docking::{DockConfig, DockPanels, DragUnit, FloatSpace, GroupAt, PanelSpec, float_drop};
use craft_ui::patterns::dock::DockArea;
use craft_ui::runtime::drag::{self, DropMark};
use craft_ui::testing::pointer::{moved, press, release};
@@ -151,3 +151,14 @@ fn a_tab_dropped_on_a_strip_at_the_layouts_top_edge_joins_its_group_and_the_edge
assert!((row.width() - area.width()).abs() <= SLACK, "the edge drop takes the full width {area:?}: {row:?}");
assert!(group_rect(&bed, 1).width() >= area.width() - SLACK, "the panel went to a full-width row on top");
}
/// A drop that floats a panel in open room marks the region the window takes, as a docked drop marks its group's: one
/// outline with a light fill around the pointer, where the window lands.
#[trial]
fn a_float_drop_marks_the_region_its_window_takes_under_the_pointer() {
let space = FloatSpace::new(Rect::from_min_size(Pos2::ZERO, ROOM), &super::sizes(4.0, 60.0, 20.0));
let at = Pos2::new(400.0, 250.0);
let dropped = float_drop(&DragUnit::Panel(2_usize), &space, at, Some(Vec2::new(52.0, 400.0)), None, false);
let DropMark::Region { area, line: None, .. } = dropped.mark else { panic!("a float marks a region: {:?}", dropped.mark) };
assert!(area.contains(at), "the region {area:?} lies under the pointer {at:?}");
}