mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
Rulers: leave out a tick label that would run into the next labelled tick
A label as long along its strip as the room between labelled ticks (a host table that labels every unit at a low zoom, or a vertical strip's stacked characters) is not painted, so no label overlaps the next one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4fe5bbd953
commit
4d1b4434bf
2 files changed
+43
-1
No files matched your search
@@ -631,6 +631,10 @@ impl Component for Rulers<'_> {
|
||||
for (((part, strip, edge), bar), bands) in parts.into_iter().zip(bars).zip(&margin_bands) {
|
||||
let vertical = part == RulerPart::Vertical;
|
||||
let ticks = Self::axis_ticks(&text_of(part), &level, strip, part, zero);
|
||||
// A label as long along the strip as the room between labelled ticks would run into the next one.
|
||||
let majors: Vec<f32> = ticks.iter().filter(|(tick, _)| tick.rank == TickRank::Major).map(|(_, at)| *at).collect();
|
||||
let room = majors.windows(2).map(|pair| (pair[1] - pair[0]).abs()).fold(f32::INFINITY, f32::min);
|
||||
let fits = |label: Rect| (if vertical { label.height() } else { label.width() }) + style.pad < room;
|
||||
let mut galleys = Vec::new();
|
||||
for (tick, at) in ticks.iter().filter(|(tick, _)| tick.rank == TickRank::Major) {
|
||||
let text = text_of(part).tick_label(&numbers, tick.value, tick.step);
|
||||
@@ -655,6 +659,9 @@ impl Component for Rulers<'_> {
|
||||
})
|
||||
.collect();
|
||||
let column = placed.iter().fold(Rect::NOTHING, |all, (at, layout)| all.union(Rect::from_min_size(*at, layout.size())));
|
||||
if !fits(column) {
|
||||
continue;
|
||||
}
|
||||
if face == RulerFace::Centred && (!shows(strip, column) || bands.iter().any(|band| band.intersects(column))) {
|
||||
continue;
|
||||
}
|
||||
@@ -670,7 +677,7 @@ impl Component for Rulers<'_> {
|
||||
RulerFace::Centred => RulerFace::centred_number(bar, edge, *at, layout.size()),
|
||||
};
|
||||
let clear = face == RulerFace::Edge || !bands.iter().any(|band| band.intersects(boxed));
|
||||
if shows(strip, boxed) && clear {
|
||||
if shows(strip, boxed) && clear && fits(boxed) {
|
||||
galleys.push((boxed.min, layout));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,6 +173,41 @@ fn signed_tick_labels_carry_the_sign_and_drop_trailing_zeros() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A host table that labels every unit, whatever the zoom.
|
||||
fn every_unit(_: f64) -> (f64, u32) {
|
||||
(1.0, 1)
|
||||
}
|
||||
|
||||
/// Where a host's table labels ticks closer than a label is long, the rulers leave the labels out rather than run one
|
||||
/// into the next: no two label runs in the strips overlap, the vertical strip's stacked characters included.
|
||||
#[trial]
|
||||
fn labels_that_do_not_fit_between_labelled_ticks_are_left_out() {
|
||||
let room = Vec2::new(800.0, 600.0);
|
||||
let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, room), Point::new(400.0, 300.0), 1.0);
|
||||
// A unit every 8 points: two-digit labels are longer than that across and down.
|
||||
let text = RulerText::new(RulerUnits::new(1.0 / 8.0, Ticks::Custom(every_unit)), UnitKind::Points);
|
||||
let content = Rc::new(Cell::new(Rect::NOTHING));
|
||||
let seen = Rc::clone(&content);
|
||||
let mut zero = Point::ZERO;
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let strips = Rulers::new("Rulers", Bind::bound(&mut zero));
|
||||
let shown = Canvas::new("Document").view(view).with_rulers(Some(strips)).rulers(text, text).show(ui, |_, _| {});
|
||||
if let Some(rulers) = shown.output.rulers {
|
||||
seen.set(rulers.content_rect());
|
||||
}
|
||||
});
|
||||
fixture.room(room);
|
||||
fixture.settle();
|
||||
let content = content.get();
|
||||
let runs: Vec<Rect> =
|
||||
fixture.texts().iter().map(|run| run.rect).filter(|rect| rect.max.y <= content.min.y + 0.5 || rect.max.x <= content.min.x + 0.5).collect();
|
||||
for (at, a) in runs.iter().enumerate() {
|
||||
for b in &runs[at + 1..] {
|
||||
assert!(!a.shrink(0.5).intersects(b.shrink(0.5)), "ruler labels overlap: {a:?} and {b:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// How far past the canvas the tool's fill and the marquee reach.
|
||||
const PAST: f32 = 60.0;
|
||||
|
||||
|
||||
Reference in new issue
Block a user