gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb58e4495b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mreb58e4495/components/virtual-list/virtual-list.hl

12.9 KB

  1. // virtual-list.hl — THE VIRTUAL LIST: a very long list (tens of thousands of rows) that scrolls smoothly on a phone because only the rows
  2. // near the viewport exist in the page. Every row has the same fixed height, so the list knows where row N is without measuring: a spacer
  3. // as tall as the whole list (`itemCount * rowHeight`) makes the scroll bar honest, and the few rows that are near the viewport are placed in it.
  4. // The component does not fetch anything. The HOST owns the data: it hands in the rows it has loaded (`loaded`, the first of them is row number
  5. // `loadedFrom`), and the component calls the host's `onRange(first, count)` when the rows near the viewport are not all there yet; the host loads
  6. // them (usually page by page from its server) and hands in the new `loaded` / `loadedFrom`. Until they arrive the missing rows are blank rows of the same
  7. // height, so the list never jumps. A row is `{ id, title, sub, aside, img }`: a thumbnail (`img`, loaded lazily; a plain grey square when it is empty),
  8. // the title with a second line under it, and a short text at the right (a play count, a length). A tap calls `onPick(id, index)`.
  9. //
  10. // VirtualList { itemCount = 13449 rowHeight = 56 loaded = chunk loadedFrom = chunkFrom onRange = loadRange onPick = picked selectedId = playingId }
  11. //
  12. // Anything written inside the tag (`VirtualList { … h1 { "Title" } }`) stands above the rows and scrolls away with them.
  13. // The host gives the list a height (it scrolls inside its own box): `virtual-list { height: 70dvh }` or a flex child. `emit vlTop()` scrolls back to the
  14. // first row (after the host changed the sort order or the filter). USE ONE PER PAGE: a composed component's members are the page's state, so the
  15. // public names below are chosen to stay clear of a host's own (no `rows`, `total`, `current`, `offset`).
  16. import Icon from '../icons/icons.hl'
  17. Number itemCount = 0
  18. Number rowHeight = 56
  19. Number overscan = 10
  20. Hybrid loaded = []
  21. Number loadedFrom = 0
  22. String selectedId = ''
  23. String listLabel = 'List'
  24. String emptyText = ''
  25. String thumbs = 'true'
  26. onRange = null
  27. onPick = null
  28. slot = null
  29. // what the scroll position says: the first row that exists in the page and how many exist
  30. Number vlFirst = 0
  31. Number vlCount = 30
  32. // the finger handle (touch screens only, see the Style): its height, where it sits (px from the top of the list box), whether the list scrolls at all,
  33. // and while a finger holds it: how far below the handle's top edge the finger grabbed it
  34. Number vlHandleH = 56
  35. Number vlThumbTop = 0
  36. String vlScrolls = 'false'
  37. Number vlGrab = 0
  38. String vlDragging = 'false'
  39. String spacerStyle = 'height:' + (itemCount * rowHeight) + 'px;--vl-row:' + rowHeight + 'px'
  40. String windowStyle = 'transform:translateY(' + (vlFirst * rowHeight) + 'px)'
  41. String rootClass = thumbs == 'true' ? 'vl-thumbs' : 'vl-plain'
  42. String handleStyle = 'transform:translateY(' + vlThumbTop + 'px);height:' + vlHandleH + 'px;visibility:' + (vlScrolls == 'true' ? 'visible' : 'hidden')
  43. Boolean isEmpty = itemCount == 0 && emptyText != ''
  44. Hybrid shown = shownOf(loaded, loadedFrom, vlFirst, vlCount, itemCount, selectedId)
  45. // the rows that exist in the page: from the host's loaded rows, a blank row where the host has not delivered one yet
  46. shownOf(rows, from0, first0, count0, n, sel) {
  47. out = []
  48. let i = first0
  49. let end = first0 + count0
  50. if (end > n) { end = n }
  51. while (i < end) {
  52. let k = i - from0
  53. if (k >= 0 && k < rows.length) {
  54. r = rows[k]
  55. out.push({ idx = '' + i id = r.id title = r.title sub = r.sub aside = r.aside img = r.img hasImg = r.img != '' cls = r.id == sel ? 'vl-row current' : 'vl-row' pos = i + 1 })
  56. } else {
  57. out.push({ idx = '' + i id = '' title = '' sub = '' aside = '' img = '' hasImg = false cls = 'vl-row blank' pos = i + 1 })
  58. }
  59. i = i + 1
  60. }
  61. return out
  62. }
  63. Style {
  64. 'virtual-list' { display = 'block' position = 'relative' height = '100%' minHeight = '0' color = 'var(--color-text, #000)' }
  65. // the box that scrolls (the host gives virtual-list its height); the mount tick (the code editor's trick) reads the real size once the page is live
  66. '@keyframes virtual-list-mount' { 'from' { opacity = '.999' } to { opacity = '1' } }
  67. 'virtual-list .vl-scroller' { position = 'absolute' inset = '0' overflowY = 'auto' overflowX = 'hidden' overscrollBehavior = 'contain' animation = 'virtual-list-mount 100ms linear infinite' }
  68. 'virtual-list .vl-scroller:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-2px' }
  69. 'virtual-list .vl-head' { display = 'block' }
  70. 'virtual-list .vl-head:empty' { display = 'none' }
  71. 'virtual-list .vl-spacer' { position = 'relative' width = '100%' }
  72. 'virtual-list .vl-window' { position = 'absolute' left = '0' right = '0' top = '0' willChange = 'transform' }
  73. // a row: exactly rowHeight tall (the tap area is the row), thumbnail · title over sub · aside; no frames, no gaps between rows
  74. 'virtual-list .vl-row' { display = 'flex' alignItems = 'center' gap = '.7rem' boxSizing = 'border-box' width = '100%' height = 'var(--vl-row)' margin = '0' padding = '0 .6rem' font = 'inherit' fontWeight = '400' textAlign = 'left' color = 'var(--color-text, #000)' background = 'transparent' border = '0' borderRadius = 'var(--radius-small, 5px)' cursor = 'pointer' overflow = 'hidden' }
  75. 'virtual-list .vl-row:hover' { background = 'var(--color-surface, #f2f2f2)' }
  76. 'virtual-list .vl-row:focus-visible' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-2px' }
  77. 'virtual-list .vl-row.current' { color = 'var(--color-accent, #000)' background = 'var(--color-surface, #f2f2f2)' }
  78. 'virtual-list .vl-row.blank' { cursor = 'default' pointerEvents = 'none' }
  79. 'virtual-list .vl-thumb' { display = 'block' flex = 'none' width = 'calc(var(--vl-row) - 1rem)' height = 'calc(var(--vl-row) - 1rem)' borderRadius = 'var(--radius-small, 5px)' background = 'var(--color-surface, #ddd)' overflow = 'hidden' }
  80. // on the marked row the grey of the empty thumbnail would be the row's own surface: it takes the darker sunken colour
  81. 'virtual-list .vl-row.current .vl-thumb' { background = 'var(--color-background-sunken, #ccc)' }
  82. 'virtual-list .vl-row.blank .vl-thumb' { background = 'transparent' }
  83. 'virtual-list .vl-thumb img' { display = 'block' width = '100%' height = '100%' objectFit = 'cover' }
  84. 'virtual-list.vl-plain .vl-thumb' { display = 'none' }
  85. 'virtual-list .vl-text' { display = 'flex' flexDirection = 'column' justifyContent = 'center' flex = '1 1 auto' minWidth = '0' }
  86. 'virtual-list .vl-title' { display = 'block' overflow = 'hidden' textOverflow = 'ellipsis' whiteSpace = 'nowrap' lineHeight = '1.3' }
  87. 'virtual-list .vl-sub' { display = 'block' overflow = 'hidden' textOverflow = 'ellipsis' whiteSpace = 'nowrap' lineHeight = '1.3' fontSize = '.85rem' color = 'var(--color-text-muted, #555)' }
  88. 'virtual-list .vl-row.current .vl-sub' { color = 'inherit' }
  89. 'virtual-list .vl-aside' { display = 'block' flex = 'none' fontSize = '.85rem' fontVariantNumeric = 'tabular-nums' color = 'var(--color-text-muted, #555)' }
  90. 'virtual-list .vl-row.current .vl-aside' { color = 'inherit' }
  91. // THE FINGER HANDLE (jukebox#12): on a touch screen (pointer: coarse) the browser's thin scroll bar cannot be grabbed, so the list draws its own handle at the right edge.
  92. // Its tap area is 44 px wide and 56 px tall; what is drawn stays slim (the transparent left border carries the width, background-clip keeps the colour inside the padding box).
  93. // A mouse and a keyboard never see it (display none): the desktop look is the browser's own scroll bar as before.
  94. vlRail { display = 'none' position = 'absolute' top = '0' bottom = '0' right = '0' width = '44px' pointerEvents = 'none'
  95. vlHandle { display = 'block' position = 'absolute' top = '0' right = '0' boxSizing = 'border-box' width = '44px' pointerEvents = 'auto' touchAction = 'none' cursor = 'grab' border = '0 solid transparent' borderLeftWidth = '32px' borderRightWidth = '3px' borderRadius = '9px' background = 'var(--color-text-muted, #888)' backgroundClip = 'padding-box' opacity = '.75'
  96. active { opacity = '1' }
  97. }
  98. }
  99. '@media' = { pointer = 'coarse'
  100. vlRail { display = 'block' }
  101. // quoted: the scroller is a div with a class (the host's gates and the mount tick find it by `.vl-scroller`), nothing else can name it.
  102. // On a touch screen the native bar gives way to the handle and the rows keep clear of the handle's drawn strip.
  103. '.vl-scroller' { scrollbarWidth = 'none' paddingRight = '14px' boxSizing = 'border-box' }
  104. }
  105. 'virtual-list .vl-empty' { margin = '0' padding = '1rem .6rem' color = 'var(--color-text-muted, #555)' }
  106. }
  107. View {
  108. virtualList { class = rootClass
  109. div { class = "vl-scroller" role = "list" tabindex = "0" 'aria-label' = listLabel 'aria-rowcount' = itemCount
  110. on scroll(e) { emit vlScroll(e) } on animationiteration(e) { emit vlMount(e) }
  111. div { class = "vl-head" slot }
  112. div { class = "vl-spacer" style = spacerStyle
  113. div { class = "vl-window" style = windowStyle
  114. for (r of shown) {
  115. button { type = "button" class = r.cls role = "listitem" 'data-index' = r.idx 'data-id' = r.id 'aria-posinset' = r.pos 'aria-setsize' = itemCount title = r.title
  116. span { class = "vl-thumb" if (r.hasImg) { img { src = r.img alt = "" loading = "lazy" decoding = "async" } } }
  117. span { class = "vl-text"
  118. span { class = "vl-title" r.title }
  119. span { class = "vl-sub" r.sub }
  120. }
  121. span { class = "vl-aside" r.aside }
  122. on click(e) { emit vlPick(e) }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. vlRail { vlHandle { role = "scrollbar" 'aria-orientation' = "vertical" 'aria-label' = "Drag to scroll the list" style = handleStyle
  129. on pointerdown(e) { emit vlGrabDown(e) } on pointermove(e) { emit vlGrabMove(e) } on pointerup(e) { emit vlGrabUp(e) } on pointercancel(e) { emit vlGrabUp(e) }
  130. } }
  131. if (isEmpty) { p { class = "vl-empty" emptyText } }
  132. }
  133. }
  134. // ---- the scroll position -> the rows that exist ------------------------------------------------------------------------------
  135. // look at the scroll box: which rows should exist, and ask the host for them when it has not delivered all
  136. vlSync(box) {
  137. // what the host put in front of the rows (a title, a picture) scrolls away with them: the rows start below it
  138. head = box.querySelector('.vl-head')
  139. let top = head == null ? box.scrollTop : box.scrollTop - head.offsetHeight
  140. if (top < 0) { top = 0 }
  141. idx = (top - top % rowHeight) / rowHeight
  142. let first = idx - overscan
  143. if (first < 0) { first = 0 }
  144. fit = box.clientHeight > 0 ? box.clientHeight : window.innerHeight
  145. count = (fit - fit % rowHeight) / rowHeight + 2 + 2 * overscan
  146. // the handle shows where the list is: the share of the scrolling distance already scrolled, spread over the free room of the box
  147. maxScroll = box.scrollHeight - box.clientHeight
  148. let room = box.clientHeight - vlHandleH
  149. if (room < 0) { room = 0 }
  150. let at = maxScroll > 0 ? box.scrollTop / maxScroll * room : 0
  151. at = at - at % 1
  152. if (at != vlThumbTop) { vlThumbTop = at }
  153. sc = maxScroll > 0 ? 'true' : 'false'
  154. if (sc != vlScrolls) { vlScrolls = sc }
  155. if (first != vlFirst) { vlFirst = first }
  156. if (count != vlCount) { vlCount = count }
  157. let last = first + count
  158. if (last > itemCount) { last = itemCount }
  159. if (onRange != null && itemCount > 0 && (first < loadedFrom || last > loadedFrom + loaded.length)) { onRange(first, last - first) }
  160. }
  161. on vlScroll(e) {
  162. vlSync(e.target)
  163. return null
  164. }
  165. // the first tick of the mount animation: the page is live, read the real size and position, then stop the animation
  166. on vlMount(e) {
  167. e.target.style.animationName = 'none'
  168. vlSync(e.target)
  169. return null
  170. }
  171. // the host changed the order or the filter: back to the first row
  172. on vlTop() {
  173. box = document.querySelector('virtual-list .vl-scroller')
  174. if (box != null) { box.scrollTop = 0 vlSync(box) }
  175. return null
  176. }
  177. on vlPick(e) {
  178. b = e.target.closest('button')
  179. if (b == null) { return null }
  180. id = b.getAttribute('data-id')
  181. if (id == '' || onPick == null) { return null }
  182. onPick(id, toNumber(b.getAttribute('data-index')))
  183. return null
  184. }
  185. // ---- the finger handle: drag it and the list follows proportionally -----------------------------------------------------------
  186. // the list box that belongs to a handle
  187. boxOfHandle(h) {
  188. return h.closest('virtual-list').querySelector('.vl-scroller')
  189. }
  190. // scroll the box so the handle's top edge would be at y (a pointer y minus the grab offset), in the whole distance the list has
  191. vlGoTo(h, y) {
  192. box = boxOfHandle(h)
  193. r = box.getBoundingClientRect()
  194. room = box.clientHeight - vlHandleH
  195. if (room <= 0) { return null }
  196. let share = (y - r.top) / room
  197. if (share < 0) { share = 0 }
  198. if (share > 1) { share = 1 }
  199. box.scrollTop = share * (box.scrollHeight - box.clientHeight)
  200. return null
  201. }
  202. on vlGrabDown(e) {
  203. h = e.currentTarget
  204. h.setPointerCapture(e.pointerId)
  205. vlDragging = 'true'
  206. vlGrab = e.clientY - h.getBoundingClientRect().top
  207. e.preventDefault()
  208. return null
  209. }
  210. on vlGrabMove(e) {
  211. if (vlDragging != 'true') { return null }
  212. vlGoTo(e.currentTarget, e.clientY - vlGrab)
  213. return null
  214. }
  215. on vlGrabUp(e) {
  216. vlDragging = 'false'
  217. return null
  218. }

Branches

Latest commits

  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
  • 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
  • c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
  • 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
  • 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
  • 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
  • 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
  • bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre