mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 16:59:56 +01:00
draw: TransformBox draws its handles at a side the host passes
`TransformBox::handle_side(points)` sets the side of the corner and side handles, for a host whose handle size follows a preference. Without it the handles keep the theme's `size.handle.md`. The handle squares, a live line's end and side squares, and the overflow handle use the side. A handle's hit circle grows with a larger side and never falls below the theme's reach, so a press anywhere on a large square takes it.
This commit is contained in:
1 parent
8f1bb4c460
commit
4114cb8070
2 files changed
+70
-5
No files matched your search
@@ -492,6 +492,10 @@ pub struct TransformBox<'a> {
|
||||
/// Whether the corner and side handles are circles (a rounded document editor's look) instead of squares.
|
||||
#[prop]
|
||||
round_handles: bool,
|
||||
/// The side of the corner and side handles in points, for a host with a handle size preference; `None` uses the
|
||||
/// theme's (`size.handle.md`). A handle's reach grows with a larger side and stays at least the theme's.
|
||||
#[prop]
|
||||
handle_side: Option<f32>,
|
||||
/// Draws the box as a live line's controls (the Line tool's live line as the whole selection). Only the two ends
|
||||
/// take a press and the keys, each as the box handle it points to ([`LineLook`]), so an end drag scales the box as
|
||||
/// that handle does; while it is dragged, the box shows as it scales. No other part is declared.
|
||||
@@ -538,6 +542,7 @@ impl<'a> TransformBox<'a> {
|
||||
session_arrows: true,
|
||||
centre_mark: false,
|
||||
round_handles: false,
|
||||
handle_side: None,
|
||||
line: None,
|
||||
reach: TransformReach::Box,
|
||||
color: None,
|
||||
@@ -580,6 +585,16 @@ impl<'a> TransformBox<'a> {
|
||||
self.color.map_or(StrokeColor::from(theme), StrokeColor::from)
|
||||
}
|
||||
|
||||
/// The side of a corner or side handle: the host's, else the theme's.
|
||||
fn side_of_handles(&self, style: &TransformBoxStyle) -> f32 {
|
||||
self.handle_side.unwrap_or(style.handle.side)
|
||||
}
|
||||
|
||||
/// The hit radius of a corner or side handle: the theme's, or more for a host's larger handle.
|
||||
fn reach_of_handles(&self, style: &TransformBoxStyle) -> f32 {
|
||||
self.handle_side.map_or(style.handle_reach, |side| style.handle_reach.max(side * HANDLE_REACH_IN_HANDLES))
|
||||
}
|
||||
|
||||
/// Whether the middle of the box is a move part: the reference point is hidden, so it does not take that place.
|
||||
fn declares_centre(&self) -> bool {
|
||||
self.takes_input() && self.move_only().is_none() && !self.reference_point && self.inside_moves && self.rules.allows(BoxPart::Inside)
|
||||
@@ -1032,7 +1047,8 @@ impl TransformBox<'_> {
|
||||
/// press; `enlarged` with the overflow plus.
|
||||
fn paint_handle(&self, draw: &Draw<'_>, cx: &Cx<'_, Self>, part: BoxPart, at: Pos2, enlarged: bool) {
|
||||
let look = &cx.style.handle;
|
||||
let side = if enlarged { look.side * OVERFLOW_IN_HANDLES } else { look.side };
|
||||
let side = self.side_of_handles(cx.style);
|
||||
let side = if enlarged { side * OVERFLOW_IN_HANDLES } else { side };
|
||||
self.paint_handle_shape(draw, cx, self.handle_shape(at, side), self.rules.allows(part).then(|| BOX.at(part.index())));
|
||||
if enlarged {
|
||||
Handle::plus(draw, look, at, side);
|
||||
@@ -1048,14 +1064,14 @@ impl TransformBox<'_> {
|
||||
paint_frame(draw, style, self.ink(style.line), corners);
|
||||
for k in 0..4 {
|
||||
let middle = corners[k].lerp(corners[(k + 1) % 4], 0.5);
|
||||
self.paint_handle_shape(draw, cx, self.handle_shape(middle, style.handle.side), None);
|
||||
self.paint_handle_shape(draw, cx, self.handle_shape(middle, self.side_of_handles(style)), None);
|
||||
}
|
||||
}
|
||||
for (part, end) in line.end_parts() {
|
||||
self.paint_handle(draw, cx, part, view.to_screen(end), false);
|
||||
}
|
||||
for (end, at) in line.free_ends() {
|
||||
self.paint_handle_shape(draw, cx, self.handle_shape(view.to_screen(at), style.handle.side), Some(LINE_END.at(end.index())));
|
||||
self.paint_handle_shape(draw, cx, self.handle_shape(view.to_screen(at), self.side_of_handles(style)), Some(LINE_END.at(end.index())));
|
||||
}
|
||||
paint_widget(draw, view.to_screen(line.from.midpoint(line.to)), &style.widget, None);
|
||||
}
|
||||
@@ -1278,7 +1294,7 @@ impl Component for TransformBox<'_> {
|
||||
handle => {
|
||||
let at = self.part_point(&state, handle).map_or(Pos2::ZERO, |at| space.view.to_screen(at));
|
||||
(
|
||||
HitShape::circle(at, style.handle_reach),
|
||||
HitShape::circle(at, self.reach_of_handles(style)),
|
||||
Sense::DRAG | Sense::CLICK | Sense::TAKE_FOCUS,
|
||||
HitPriority::new(HitLevel::Handle, HANDLE_SUB),
|
||||
FocusShape::Circle,
|
||||
@@ -1290,7 +1306,7 @@ impl Component for TransformBox<'_> {
|
||||
}
|
||||
if let Some(line) = self.line {
|
||||
for (end, at) in line.free_ends() {
|
||||
cx.part(LINE_END.at(end.index()), HitShape::circle(space.view.to_screen(at), style.handle_reach))
|
||||
cx.part(LINE_END.at(end.index()), HitShape::circle(space.view.to_screen(at), self.reach_of_handles(style)))
|
||||
.sense(Sense::DRAG | Sense::CLICK | context)
|
||||
.priority(HitPriority::new(HitLevel::Handle, HANDLE_SUB))
|
||||
.pointer_only(LINE_END_BY_KEY);
|
||||
|
||||
@@ -60,6 +60,8 @@ struct Config {
|
||||
f32_host: bool,
|
||||
centre_mark: bool,
|
||||
round_handles: bool,
|
||||
/// The host's handle side, `None` for the theme's.
|
||||
handle_side: Option<f32>,
|
||||
line: Option<LineLook>,
|
||||
/// Snap targets beside the guides at [`SNAP_XS`].
|
||||
targets: Vec<SnapTarget>,
|
||||
@@ -119,6 +121,7 @@ fn defaults() -> Config {
|
||||
f32_host: false,
|
||||
centre_mark: false,
|
||||
round_handles: false,
|
||||
handle_side: None,
|
||||
line: None,
|
||||
targets: Vec::new(),
|
||||
suspend: SnapSuspend::Control,
|
||||
@@ -218,6 +221,9 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
|
||||
if let Some(line) = config.line {
|
||||
control = control.line(line);
|
||||
}
|
||||
if let Some(side) = config.handle_side {
|
||||
control = control.handle_side(side);
|
||||
}
|
||||
control = control.reach(config.reach).session_commit(config.session_commit);
|
||||
if config.hover_frame {
|
||||
control = control.id(frame_id);
|
||||
@@ -1649,3 +1655,46 @@ fn round_handles_are_circles() {
|
||||
// Each handle is a filled circle and its stroke.
|
||||
assert_eq!(circles(true), circles(false) + 16, "the four corners and four sides are circles");
|
||||
}
|
||||
|
||||
/// A host's handle side draws the corner and side handles at that side: a larger side draws a larger square.
|
||||
#[trial]
|
||||
fn a_host_handle_side_sets_the_drawn_handle_square() {
|
||||
let corner_side = |side: Option<f32>| {
|
||||
let mut h = harness(BoxState::from_rect(RECT), |config| config.handle_side = side);
|
||||
let corner = h.screen(Point::new(RECT.x1, RECT.y1));
|
||||
let sides: Vec<f32> = h
|
||||
.fixture
|
||||
.frame_shapes()
|
||||
.iter()
|
||||
.filter_map(|shape| match shape {
|
||||
Shape::Rect(square) if (square.rect.center() - corner).length() < 0.5 => Some(square.rect.width()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
sides.into_iter().fold(0.0, f32::max)
|
||||
};
|
||||
let (small, large) = (corner_side(Some(4.0)), corner_side(Some(12.0)));
|
||||
assert!(small > 0.0, "the corner handle is drawn as a square");
|
||||
assert!(large > small, "a larger side draws a larger square: {large} against {small}");
|
||||
}
|
||||
|
||||
/// A large host handle takes a press anywhere on its square, also past the theme's reach, and the press drags it.
|
||||
#[trial]
|
||||
fn a_press_just_inside_a_large_handles_edge_grabs_it() {
|
||||
// 7 points in from the bottom right corner on both axes: inside a 16 point square, about 9.9 from its centre.
|
||||
let near_edge = Point::new(RECT.x1 - 7.0, RECT.y1 - 7.0);
|
||||
let mut theme = harness(BoxState::from_rect(RECT), |config| config.rules = free_rules());
|
||||
hover(&mut theme, near_edge);
|
||||
assert_ne!(theme.host.borrow().hovered, Some(BoxPart::Corner(2)), "past the theme's handle reach");
|
||||
|
||||
let mut h = harness(BoxState::from_rect(RECT), |config| {
|
||||
config.rules = free_rules();
|
||||
config.handle_side = Some(16.0);
|
||||
});
|
||||
hover(&mut h, near_edge);
|
||||
assert_eq!(h.host.borrow().hovered, Some(BoxPart::Corner(2)), "on the large handle's square");
|
||||
drag(&mut h, near_edge, Point::new(RECT.x1 + 13.0, RECT.y1 + 13.0));
|
||||
let quad = h.state().quad;
|
||||
assert!(quad[2].x > RECT.x1 + 1.0 && quad[2].y > RECT.y1 + 1.0, "the corner moved with the drag: {quad:?}");
|
||||
assert_eq!(quad[0], Point::new(RECT.x0, RECT.y0), "the opposite corner stayed: {quad:?}");
|
||||
}
|
||||
Reference in new issue
Block a user