TileGrid: insert gaps are muted at rest, accent under the pointer (trial)

This commit is contained in:
Dylan M. Taylor
2026-11-22 21:21:08 -05:00
parent 276161b92b
commit cfb37fb71f
2 changed files with 37 additions and 4 deletions
+9 -4
View File
@@ -158,6 +158,8 @@ pub struct TileGridStyle {
header: ListRowStyle,
/// The line down the middle of an insert gap under the pointer.
insert: StyleColor,
/// The plus of an insert gap at rest, before the pointer reaches it: muted, so the gaps do not compete with the tiles.
insert_idle: StyleColor,
/// The width of an insert gap, centred on the edge of the tile it precedes.
insert_width: f32,
}
@@ -626,6 +628,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
pinned: cx.color(cx.tokens.color.surface.panel),
header: ListRow::style(cx, ListRow::new(Label::default()).heading().style_key()),
insert: cx.color(cx.tokens.color.accent.solid),
insert_idle: cx.color(cx.tokens.color.text.muted),
insert_width: cx.tokens.size.icon.lg,
}
}
@@ -827,19 +830,21 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
}
// The gap under the pointer shows where an insertion lands; the part's state layer shows hover and focus.
for &(at, rect) in &self.placed_gaps {
if cx.hover_pos(GAP.at(at as u64)).is_some() {
let hovered = cx.hover_pos(GAP.at(at as u64)).is_some();
if hovered {
let line = PaintShape::Path(vec![rect.center_top(), rect.center_bottom()]);
ui.draw().stroke(&line, StrokeWidth::Thick, style.insert);
}
let ink = if hovered { style.insert.clone() } else { style.insert_idle.clone() };
// The insertion target must be discoverable before the pointer is over it.
let center = rect.center();
let radius = (rect.width() * 0.4).min(9.0);
let arm = radius * 0.45;
let draw = ui.draw();
draw.fill(&PaintShape::Circle(center, radius), style.pinned);
draw.stroke(&PaintShape::Circle(center, radius), StrokeWidth::Thin, style.insert);
draw.stroke(&PaintShape::Path(vec![center - vec2(arm, 0.0), center + vec2(arm, 0.0)]), StrokeWidth::Medium, style.insert);
draw.stroke(&PaintShape::Path(vec![center - vec2(0.0, arm), center + vec2(0.0, arm)]), StrokeWidth::Medium, style.insert);
draw.stroke(&PaintShape::Circle(center, radius), StrokeWidth::Thin, ink.clone());
draw.stroke(&PaintShape::Path(vec![center - vec2(arm, 0.0), center + vec2(arm, 0.0)]), StrokeWidth::Medium, ink.clone());
draw.stroke(&PaintShape::Path(vec![center - vec2(0.0, arm), center + vec2(0.0, arm)]), StrokeWidth::Medium, ink);
}
}
@@ -224,3 +224,31 @@ fn insert_gaps_keep_document_positions_when_rows_are_virtualised() {
shown.borrow().grid.iter().filter_map(|event| if let GridEvent::Insert { at, .. } = event { Some(*at) } else { None }).collect();
assert_eq!(inserts, [first], "the gap reports the display position it inserts before, not a row index");
}
/// An insert gap at rest is drawn muted: its circle's stroke differs from the one the pointer's gap takes.
#[craft_ui::testing::trial]
fn an_insert_gap_at_rest_is_muted() {
let mut fixture = Fixture::new(move |ui| {
let keys: Vec<usize> = (0..4).collect();
let source = keyed(&keys, |key: &usize| *key);
let name = |at: usize| Label::from(format!("Insert before {}", at + 1));
let _ = TileGrid::new("Library").insert_gaps(&name).show_items(ui, &source, &mut |key, _| Tile::new(format!("Tile {key}")));
});
fixture.room(Vec2::new(600.0, 300.0));
fixture.frame(vec![pointer::moved(egui::Pos2::new(590.0, 290.0))]).settle();
fn strokes(shapes: &[egui::Shape]) -> Vec<egui::Color32> {
shapes
.iter()
.filter_map(|shape| match shape {
egui::Shape::Circle(circle) if circle.stroke.width > 0.0 => Some(circle.stroke.color),
_ => None,
})
.collect()
}
let rest = strokes(&fixture.frame_shapes());
assert!(!rest.is_empty(), "the gaps draw their circles");
let gap = fixture.by_label("Insert before 1").expect("the first gap").rect;
fixture.frame(vec![pointer::moved(gap.center())]).settle();
let hovered = strokes(&fixture.frame_shapes());
assert!(hovered.iter().any(|color| !rest.contains(color)), "the gap under the pointer is drawn in another colour: {rest:?} / {hovered:?}");
}