AddBesideHandles: outline the hot side's ghost of what a click adds

This commit is contained in:
aiongg
2026-11-12 14:32:44 +00:00
parent 756524277b
commit 2d4da9a7a6
3 changed files with 69 additions and 16 deletions
+21 -7
View File
@@ -110,7 +110,7 @@ pub struct AddBesideOutput {
pub added: Option<AddSide>,
/// Alt was held with the click: add with contents (an artboard's copy). A key press never asks for a copy.
pub copy: bool,
/// The button under the pointer or focused (the tool draws its ghost).
/// The button under the pointer or focused: the one in the hot look, whose ghost shows.
pub hovered: Option<AddSide>,
}
@@ -138,7 +138,8 @@ impl Outcome for AddBesideOutput {
/// is set (a drag runs); one when its disc touches a [`clear_of`](Self::clear_of) rectangle (a size label, a
/// toolbar) or leaves the canvas.
/// - Pointer: the pointing hand; a click reports [`AddBesideOutput::added`], with Alt held also `copy`; the hot look
/// (the accent disc) on hover and focus.
/// (the accent disc) on hover and focus, with the hot side's [`ghosts`](Self::ghosts) rect outlined: what the click
/// would add.
/// - Keyboard: the group is one Tab stop named by the label (the "Add a connected shape" word after
/// [`connecting`](Self::connecting)); the arrows move the focus to the button that lies in their screen
/// direction, Enter or Space adds; each button is a `Button` node ("Add above", ...).
@@ -152,6 +153,9 @@ pub struct AddBesideHandles<'a> {
/// Screen rectangles no button may cover.
#[prop]
clear_of: &'a [Rect],
/// What each side would add, in document units: the next board or shape, outlined while its button is hot.
#[prop]
ghosts: &'a [(AddSide, DocRect)],
/// Hides every button (while a drag runs).
#[prop(flag)]
hidden: bool,
@@ -171,7 +175,8 @@ struct Report {
impl<'a> AddBesideHandles<'a> {
/// The buttons around `object`, in document units, the group named `label`.
pub fn new(label: impl Into<Label>, object: DocRect) -> Self {
AddBesideHandles { common: Common::new(), object, clear_of: &[], hidden: false, connecting: false, report: Report::default() }.label(label)
AddBesideHandles { common: Common::new(), object, clear_of: &[], ghosts: &[], hidden: false, connecting: false, report: Report::default() }
.label(label)
}
/// The visible buttons' screen centres.
@@ -293,10 +298,19 @@ impl Component for AddBesideHandles<'_> {
draw.stroke(&PaintShape::Path(vec![at - Vec2::new(arm, 0.0), at + Vec2::new(arm, 0.0)]), StrokeWidth::Medium, plus);
draw.stroke(&PaintShape::Path(vec![at - Vec2::new(0.0, arm), at + Vec2::new(0.0, arm)]), StrokeWidth::Medium, plus);
}
let Some(&(_, at)) = buttons.iter().find(|(side, _)| ring && focused == Some(*side)) else { return };
let painter = ui.content_painter(cx);
let ppp = painter.pixels_per_point();
painter.painter.extend(FocusRing::shapes(&style.focus, FocusKind::Halo, &PaintShape::Circle(at, style.radius), ppp));
if let Some(&(_, at)) = buttons.iter().find(|(side, _)| ring && focused == Some(*side)) {
let painter = ui.content_painter(cx);
let ppp = painter.pixels_per_point();
painter.painter.extend(FocusRing::shapes(&style.focus, FocusKind::Halo, &PaintShape::Circle(at, style.radius), ppp));
}
// The ghost goes over the hot button, as the host's preview of what a click adds.
let hot = buttons.iter().map(|&(side, _)| side).find(|&side| self.report.hovered == Some(side) || focused == Some(side));
if let Some(&(_, ghost)) = hot.and_then(|hot| self.ghosts.iter().find(|(side, _)| *side == hot)) {
let quad = space.view.quad(ghost.abs());
if quad.iter().all(|p| p.x.is_finite() && p.y.is_finite()) {
ui.draw().stroke(&PaintShape::Polygon(quad.to_vec()), StrokeWidth::Thin, style.accent);
}
}
}
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
+23 -5
View File
@@ -1,9 +1,9 @@
//! Stories of the "+" buttons: an artboard on a pasteboard with the buttons around it, one side kept clear by a
//! neighbour's rect, and the connected-shape form.
use super::AddBesideHandles;
use super::{AddBesideHandles, AddSide};
use craft_behavior::CanvasView;
use craft_behavior::kurbo::{Point, Rect as DocRect};
use craft_behavior::kurbo::{Point, Rect as DocRect, Vec2};
use craft_ui::canvas::{Canvas, PageFrame, Pasteboard};
use craft_ui::contract::{Show as _, ShowContainer as _};
use craft_ui::testing::{Drive, StoryData, StoryOutput, story};
@@ -14,6 +14,23 @@ fn artboard() -> DocRect {
DocRect::new(220.0, 130.0, 420.0, 270.0)
}
/// The gap between the artboard and the board a button adds, in document units.
const GAP: f64 = 40.0;
/// The board each side's button would add: the artboard's size, `GAP` beyond that side.
fn ghosts() -> [(AddSide, DocRect); 4] {
let board = artboard();
AddSide::ALL.map(|side| {
let by = match side {
AddSide::Top => Vec2::new(0.0, -(board.height() + GAP)),
AddSide::Right => Vec2::new(board.width() + GAP, 0.0),
AddSide::Bottom => Vec2::new(0.0, board.height() + GAP),
_ => Vec2::new(-(board.width() + GAP), 0.0),
};
(side, board + by)
})
}
/// The view of the canvas the story shows: the story document at 100 %, centred.
fn view(ui: &Ui, rect: Rect) -> CanvasView {
craft_ui::testing::canvas_story::StoryDocument::STANDARD.view(rect, ui.pixels_per_point(), 100.0)
@@ -44,12 +61,13 @@ fn add_beside(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
view.to_screen(Point::new(artboard().x1 + 16.0, artboard().y0)),
view.to_screen(Point::new(artboard().x1 + 80.0, artboard().y1)),
);
let clear = [neighbour];
on_canvas(ui, rect, |_| AddBesideHandles::new("Add beside", artboard()).clear_of(&clear))
let (clear, ghosts) = ([neighbour], ghosts());
on_canvas(ui, rect, |_| AddBesideHandles::new("Add beside", artboard()).clear_of(&clear).ghosts(&ghosts))
}
#[story(AddBesideHandles<'static>, covers = [Default], drive = Drive::Never)]
fn add_beside_connecting(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
let rect = craft_ui::testing::stage(ui);
on_canvas(ui, rect, |_| AddBesideHandles::new("Add beside", artboard()).connecting())
let ghosts = ghosts();
on_canvas(ui, rect, |_| AddBesideHandles::new("Add beside", artboard()).connecting().ghosts(&ghosts))
}
+25 -4
View File
@@ -23,18 +23,19 @@ struct Rig {
added: Rc<Cell<Option<(AddSide, bool)>>>,
}
/// What a test varies: the object, whether the buttons hide, the neighbour's rect in document units and the view's
/// turn.
/// What a test varies: the object, whether the buttons hide, the neighbour's rect in document units, the view's turn
/// and what each side would add.
struct Setup {
object: DocRect,
hidden: bool,
keep_clear: Option<DocRect>,
rotation: f64,
ghosts: Vec<(AddSide, DocRect)>,
}
impl Default for Setup {
fn default() -> Self {
Setup { object: board(), hidden: false, keep_clear: None, rotation: 0.0 }
Setup { object: board(), hidden: false, keep_clear: None, rotation: 0.0, ghosts: Vec::new() }
}
}
@@ -50,7 +51,7 @@ fn rig(setup: Setup) -> Rig {
.map(|rect| Rect::from_two_pos(view.to_screen(rect.origin()), view.to_screen(Point::new(rect.x1, rect.y1))))
.collect();
let shown_here = Canvas::new("Document").view(view).show(ui, |ui, body| {
let mut handles = AddBesideHandles::new("Add beside", setup.object).clear_of(&clear);
let mut handles = AddBesideHandles::new("Add beside", setup.object).clear_of(&clear).ghosts(&setup.ghosts);
if setup.hidden {
handles = handles.hidden();
}
@@ -100,3 +101,23 @@ fn arrows_follow_the_screen_direction_on_a_turned_view() {
rig.fixture.key(Key::Enter, Modifiers::NONE);
assert_eq!(rig.added.take().map(|(side, _)| side), Some(highest));
}
/// Hovering a button outlines what it would add, and only while it is hot.
#[trial]
fn hovering_a_button_outlines_the_ghost_of_what_it_adds() {
const GHOST_SIDE: f32 = 100.0;
let ghost = DocRect::new(290.0, 100.0, 290.0 + f64::from(GHOST_SIDE), 100.0 + f64::from(GHOST_SIDE));
let mut rig = rig(Setup { ghosts: vec![(AddSide::Right, ghost)], ..Setup::default() });
let right = rig.fixture.by_label("Add right").expect("the right button").center();
// The ghost on screen: a shape of the ghost's size (the view is at 100 %) beyond the right button.
let outlined = |fixture: &mut Fixture| {
fixture
.frame_shapes()
.iter()
.map(egui::Shape::visual_bounding_rect)
.any(|bounds| (bounds.width() - GHOST_SIDE).abs() < 3.0 && (bounds.height() - GHOST_SIDE).abs() < 3.0 && bounds.min.x > right.x)
};
assert!(!outlined(&mut rig.fixture), "no ghost while no button is hot");
rig.fixture.frame(vec![egui::Event::PointerMoved(right)]);
assert!(outlined(&mut rig.fixture), "the hovered right button outlines its ghost");
}