Skip to content

Commit 6c4f1d9

Browse files
Expand items to approximately one pixel, without overlapping neighboring items.
1 parent 181b17f commit 6c4f1d9

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/app.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,9 @@ impl Slot {
677677
return hover_pos;
678678
}
679679

680+
// Figure out roughly how large a pixel is on the screen.
681+
let pixel_ns = (cx.view_interval.duration_ns() as f32 / rect.width()) as i64;
682+
680683
// Track which item, if any, we're interacting with
681684
let mut interact_item = None;
682685

@@ -709,11 +712,27 @@ impl Slot {
709712
continue;
710713
}
711714

715+
// Expand interval to use at least one pixel, but do NOT
716+
// overlap neighboring items.
717+
let mut interval = item.interval;
718+
if interval.duration_ns() < pixel_ns {
719+
let expand_ns = (pixel_ns - interval.duration_ns()) / 2;
720+
interval = interval.grow(expand_ns);
721+
if item_idx > 0 {
722+
let last_item = &row_items[item_idx - 1];
723+
interval = interval.subtract_before(last_item.interval.stop);
724+
}
725+
if item_idx < row_items.len() - 1 {
726+
let next_item = &row_items[item_idx + 1];
727+
interval = interval.subtract_after(next_item.interval.start);
728+
}
729+
}
730+
712731
// Note: the interval is EXCLUSIVE. This turns out to be what
713732
// we want here, because in screen coordinates interval.stop
714733
// is the BEGINNING of the interval.stop nanosecond.
715-
let start = cx.view_interval.unlerp(item.interval.start).at_least(0.0);
716-
let stop = cx.view_interval.unlerp(item.interval.stop).at_most(1.0);
734+
let start = cx.view_interval.unlerp(interval.start).at_least(0.0);
735+
let stop = cx.view_interval.unlerp(interval.stop).at_most(1.0);
717736
let min = rect.lerp_inside(Vec2::new(start, (irow as f32 + 0.05) / rows as f32));
718737
let max = rect.lerp_inside(Vec2::new(stop, (irow as f32 + 0.95) / rows as f32));
719738

src/timestamp.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,26 @@ impl Interval {
103103
}
104104
pub fn intersection(self, other: Interval) -> Self {
105105
Self {
106-
start: Timestamp(self.start.0.max(other.start.0)),
107-
stop: Timestamp(self.stop.0.min(other.stop.0)),
106+
start: self.start.max(other.start),
107+
stop: self.stop.min(other.stop),
108108
}
109109
}
110110
pub fn union(self, other: Interval) -> Self {
111111
Self {
112-
start: Timestamp(self.start.0.min(other.start.0)),
113-
stop: Timestamp(self.stop.0.max(other.stop.0)),
112+
start: self.start.min(other.start),
113+
stop: self.stop.max(other.stop),
114+
}
115+
}
116+
pub fn subtract_before(self, point: Timestamp) -> Self {
117+
Self {
118+
start: self.start.max(point),
119+
stop: self.stop.max(point),
120+
}
121+
}
122+
pub fn subtract_after(self, point: Timestamp) -> Self {
123+
Self {
124+
start: self.start.min(point),
125+
stop: self.stop.min(point),
114126
}
115127
}
116128
// Convert a timestamp into [0,1] relative space
@@ -488,6 +500,46 @@ mod tests {
488500
assert_eq!(i1.union(i0), Interval::new(Timestamp(0), Timestamp(15)));
489501
}
490502

503+
#[test]
504+
fn test_subtract_before() {
505+
let p0 = Timestamp(0);
506+
let p1 = Timestamp(10);
507+
let p2 = Timestamp(20);
508+
let i0 = Interval::new(Timestamp(5), Timestamp(15));
509+
assert_eq!(
510+
i0.subtract_before(p0),
511+
Interval::new(Timestamp(5), Timestamp(15))
512+
);
513+
assert_eq!(
514+
i0.subtract_before(p1),
515+
Interval::new(Timestamp(10), Timestamp(15))
516+
);
517+
assert_eq!(
518+
i0.subtract_before(p2),
519+
Interval::new(Timestamp(20), Timestamp(20))
520+
);
521+
}
522+
523+
#[test]
524+
fn test_subtract_after() {
525+
let p0 = Timestamp(0);
526+
let p1 = Timestamp(10);
527+
let p2 = Timestamp(20);
528+
let i0 = Interval::new(Timestamp(5), Timestamp(15));
529+
assert_eq!(
530+
i0.subtract_after(p0),
531+
Interval::new(Timestamp(0), Timestamp(0))
532+
);
533+
assert_eq!(
534+
i0.subtract_after(p1),
535+
Interval::new(Timestamp(5), Timestamp(10))
536+
);
537+
assert_eq!(
538+
i0.subtract_after(p2),
539+
Interval::new(Timestamp(5), Timestamp(15))
540+
);
541+
}
542+
491543
#[test]
492544
fn test_grow() {
493545
let i0 = Interval::new(Timestamp(5), Timestamp(10));

0 commit comments

Comments
 (0)