dymokomi/luce-ui
Source of release 0.19.5
0.19.5 / src / widgets / strip_lanes.lucb
## Strip lanes, the sideways twin of justified rows: a filmstrip that scrolls in x.
## The strip holds as many lanes (rows of one height) as fit its height, at least
## one; every item keeps its aspect ratio at that height, and each item joins the
## lane that ends leftmost (the upper one on a tie), so items run left to right in
## reading order whatever their widths. An item's left edge never comes before
## the previous item's, which lets the visible range and hit tests search by x. A
## gap separates items and lanes and frames the whole; the lanes are centred in
## the height. ThumbnailGrid draws with it in its horizontal flow.
import memory
from luce_std import math
## The most lanes a strip holds.
let most_lanes: usize = 512
pub struct StripLanes:
## Per item: its lane, its left edge and width, in points.
var item_lane: u32[]
var item_x: f64[]
var item_width: f64[]
## Where each lane ends while arranging.
var lane_end: f64[]
pub var lanes: usize
pub var count: usize
## Every lane's height, and the first lane's top.
pub var lane_height: f64
var top: f64
var gap: f64
## The widest item, which bounds the searches by x.
var widest: f64
## The width of everything laid out, the right gap included.
pub var content: f64
## Room for `count` items.
pub static func make(count: usize) -> StripLanes!:
var made = StripLanes(count = count)
made.item_lane = try new u32[count] in memory.heap
errdefer free(made.item_lane) in memory.heap
made.item_x = try new f64[count] in memory.heap
errdefer free(made.item_x) in memory.heap
made.item_width = try new f64[count] in memory.heap
errdefer free(made.item_width) in memory.heap
made.lane_end = try new f64[most_lanes] in memory.heap
return made
## Lay out items of aspect ratios `aspects` (width over height) in a strip
## `height` points tall, lanes `cell` points tall or as tall as fits, `gap`
## points apart.
pub func arrange(aspects: const f32[], height: f64, cell: f64, gap: f64):
self.gap = gap
self.lane_height = math.max(1.0, math.min(cell, height - gap * 2.0))
let fit = math.floor((height - gap) / (self.lane_height + gap))
self.lanes = (usize)math.clamp(fit, 1.0, (f64)most_lanes)
let used = (f64)self.lanes * (self.lane_height + gap) + gap
self.top = math.max(0.0, (height - used) * 0.5) + gap
for lane in 0..<self.lanes:
self.lane_end[lane] = gap
self.widest = 0.0
for at in 0..<self.count:
var lane: usize = 0
for other in 1..<self.lanes:
if self.lane_end[other] < self.lane_end[lane]:
lane = other
let width = (f64)aspects[at] * self.lane_height
self.item_lane[at] = (u32)lane
self.item_x[at] = self.lane_end[lane]
self.item_width[at] = width
self.lane_end[lane] += width + gap
self.widest = math.max(self.widest, width)
self.content = gap
for lane in 0..<self.lanes:
self.content = math.max(self.content, self.lane_end[lane])
pub func lane_of(item: i64) -> i64:
return (i64)self.item_lane[(usize)item]
pub func x_of(item: i64) -> f64:
return self.item_x[(usize)item]
pub func width_of(item: i64) -> f64:
return self.item_width[(usize)item]
## The top of `lane`.
pub func top_of(lane: i64) -> f64:
return self.top + (f64)lane * (self.lane_height + self.gap)
## The first item whose left edge is at or right of `x` (`count` if none).
func first_from(x: f64) -> usize:
var low: usize = 0
var high = self.count
while low < high:
let middle = (low + high) // 2
if self.item_x[middle] < x:
low = middle + 1
else:
high = middle
return low
## The first item reaching right of content `x`: where the range on screen starts.
pub func first_after(x: f64) -> i64:
var at = self.first_from(x - self.widest)
while at < self.count and self.item_x[at] + self.item_width[at] <= x:
at += 1
return (i64)at
## One past the last item starting left of content `x`: where the range on screen ends.
pub func end_before(x: f64) -> i64:
return (i64)self.first_from(x)
## The item at content point (`x`, `y`), or -1 (a gap, or past the end).
pub func item_at(x: f64, y: f64) -> i64:
if self.count == 0 or y < self.top:
return -1
let lane = (i64)math.floor((y - self.top) / (self.lane_height + self.gap))
if lane >= (i64)self.lanes or y >= self.top_of(lane) + self.lane_height:
return -1
var at = (i64)self.first_from(x + 1.0e-9) - 1
while at >= 0 and self.item_x[(usize)at] >= x - self.widest:
if self.lane_of(at) == lane and x < self.item_x[(usize)at] + self.item_width[(usize)at]:
return at
at -= 1
return -1
## The item of `lane` whose middle is nearest `x`, or -1 when the lane is empty.
pub func nearest_in(lane: i64, x: f64) -> i64:
var best: i64 = -1
var distance = 1.0e300
# Near x first; the whole strip only when the lane has nothing there.
var at = self.first_from(x - self.widest * 2.0)
let end = self.first_from(x + self.widest * 2.0)
for pass in 0..<2:
while at < end or (pass == 1 and at < self.count):
if self.item_lane[at] == (u32)lane:
let away = math.abs(self.item_x[at] + self.item_width[at] * 0.5 - x)
if away < distance:
distance = away
best = (i64)at
at += 1
if best >= 0:
return best
at = 0
return best
pub func destroy():
free(self.item_lane) in memory.heap
free(self.item_x) in memory.heap
free(self.item_width) in memory.heap
free(self.lane_end) in memory.heap
self.lanes = 0
self.count = 0
test "items join the lane that ends leftmost, in reading order":
var aspects: f32[5] = [2.0, 1.0, 1.0, 1.0, 0.5]
var lanes = try StripLanes.make(5)
defer lanes.destroy()
# 250 tall with 100-point lanes and 10-point gaps: two lanes, 230 used, centred.
lanes.arrange(aspects, 250.0, 100.0, 10.0)
assert(lanes.lanes == 2 and lanes.lane_height == 100.0 and lanes.top_of(0) == 20.0 and lanes.top_of(1) == 130.0)
# 0 (200 wide) in lane 0, 1 and 2 in lane 1, 3 back in lane 0 after 2 passes it.
assert(lanes.lane_of(0) == 0 and lanes.lane_of(1) == 1 and lanes.lane_of(2) == 1 and lanes.lane_of(3) == 0)
assert(lanes.x_of(1) == 10.0 and lanes.x_of(2) == 120.0 and lanes.x_of(3) == 220.0 and lanes.x_of(4) == 230.0)
assert(lanes.lane_of(4) == 1 and lanes.content == 330.0)
assert(lanes.item_at(50.0, 50.0) == 0 and lanes.item_at(150.0, 150.0) == 2 and lanes.item_at(235.0, 150.0) == 4)
assert(lanes.item_at(115.0, 150.0) == -1 and lanes.item_at(50.0, 125.0) == -1 and lanes.item_at(300.0, 150.0) == -1)
assert(lanes.first_after(215.0) == 2 and lanes.first_after(221.0) == 3 and lanes.end_before(225.0) == 4 and lanes.first_after(0.0) == 0)
assert(lanes.nearest_in(1, 250.0) == 4 and lanes.nearest_in(0, 60.0) == 0)
test "a strip lower than a lane holds one, as tall as fits":
var aspects: f32[2] = [1.0, 1.0]
var lanes = try StripLanes.make(2)
defer lanes.destroy()
lanes.arrange(aspects, 60.0, 160.0, 4.0)
assert(lanes.lanes == 1 and lanes.lane_height == 52.0 and lanes.top_of(0) == 4.0)
assert(lanes.x_of(1) == 60.0 and lanes.item_at(70.0, 20.0) == 1)