gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc5b8fde9c5b8fde9jukebox JB3b: keyboard gap repro as a reportmrec5b8fde9/components/main.hl

10.8 KB

  1. // components/main.hl — THE SHELL: the layouts header with the main menu (the phone's sidebar behind the toggle), the slot every screen renders
  2. // into, and the PLAYER BAR at the bottom. The shell stays when a screen is replaced by a click on a link (hl:web navigates in the page), so the song keeps playing
  3. // while you browse. A screen starts a song with `emit jbPlay(ctx, index)`: the context says which list the song was tapped in (`{ kind, sort, minPlays, ... }`,
  4. // lib/rows.hl), the index which row. A screen starts a shuffle with `emit jbShuffle(spec)` (lib/queue.hl: favourites or the list the screen shows); the server then holds the
  5. // shuffled order and the browser asks for 40 songs at a time. The queue of the media player always starts at the song that plays: the bar shows it, the overlay lists the 20 after it.
  6. // THE NOW-PLAYING OVERLAY is the same bar and the same media player, drawn bigger by CSS: a tap on the bar opens it (full screen on the phone, a side panel from 60rem), the
  7. // close button, the Escape key or a swipe down closes it. THE COUNT: a song counts as played after half of its length or 4 minutes of listening (whichever comes first); the shell counts
  8. // the seconds the audio really plays (a one second timer), asks the server once (`jbPlayed`) and the jukebox database adds 1 to its own counter. The files are never written.
  9. // KEYBOARD: NOT HERE. Every key handler of the shell (document listener, body, the app div) made the audio element load its source again on each key press (the song
  10. // restarted): Hybriel gap, repro in reports/JB3b-keyboard-gap.md. The media player's own keys (Space, arrows on its audio element) stay as they are.
  11. // LOCK SCREEN: the browser's media session shows title, artist, album and cover and gives play, pause, previous and next to the lock screen and the headphone keys.
  12. import AppHeader from '../theme/shell/app-header.hl'
  13. import MainMenu from '../theme/shell/main-menu.hl'
  14. import Sidebar from '../theme/shell/app-sidebar.hl'
  15. import MediaPlayer from './media-player/media-player.hl'
  16. import Icon from './icons/icons.hl'
  17. import { now } from 'hl:time'
  18. import { rowsFor, queueFor } from '../lib/rows.hl'
  19. import { countPlay } from '../lib/plays.hl'
  20. slot = null
  21. menu = [ { label = 'Songs' href = '/' current = false } { label = 'Artists' href = '/artists' } { label = 'Albums' href = '/albums' } { label = 'Search' href = '/search' } ]
  22. // the player: what plays now, and the songs that follow in the list it came from (the first item of the queue is the song that plays)
  23. String nowSrc = ''
  24. String nowTitle = ''
  25. String nowArtist = ''
  26. String nowAlbum = ''
  27. String nowCover = ''
  28. String nowArt = ''
  29. String nowId = ''
  30. String autoPlay = 'false'
  31. Hybrid nowQueue = []
  32. Hybrid nowCtx = {}
  33. Number nowIdx = 0
  34. String overlay = 'false'
  35. String barClass = 'jb-app'
  36. String coverNow = overlay == 'true' ? nowArt : nowCover
  37. Number heard = 0
  38. String countedId = ''
  39. Number swipeY = -1
  40. String ticking = 'false'
  41. View {
  42. body {
  43. AppHeader { brand = "Jukebox"
  44. MainMenu { items = menu }
  45. }
  46. Sidebar { desktop = "false" MainMenu { items = menu } }
  47. div { class = barClass
  48. slot
  49. jbTick { on animationiteration(e) { emit jbMount(e) } }
  50. div { class = "jb-bar" on click(e) { emit jbBarClick(e) } on pointerdown(e) { emit jbDown(e) } on pointerup(e) { emit jbUp(e) } on pointercancel(e) { emit jbCancel(e) }
  51. button { type = "button" class = "jb-close" title = "Close" 'aria-label' = "Close" Icon { name = "close" size = "1.5em" } on click(e) { emit jbClose(e) } }
  52. MediaPlayer { queueLabel = "Next up" src = nowSrc kind = "audio" title = nowTitle artist = nowArtist cover = coverNow queue = nowQueue onPlay = pickedNext autoPlay = autoPlay }
  53. p { class = "jb-album" nowAlbum }
  54. button { type = "button" class = "jb-prev" title = "Previous song" 'aria-label' = "Previous song" Icon { name = "prevTrack" size = "1.5em" } on click(e) { emit jbPrevClick(e) } }
  55. button { type = "button" class = "jb-next" title = "Next song" 'aria-label' = "Next song" Icon { name = "nextTrack" size = "1.5em" } on click(e) { emit jbNextClick(e) } }
  56. }
  57. }
  58. }
  59. }
  60. // ---- the server's part -------------------------------------------------------------------------------------------------------
  61. // rows of a list for a screen: `{ total, offset, rows }`
  62. on server jbRows(ctx, offset, limit) {
  63. return rowsFor(ctx, offset, limit)
  64. }
  65. // the songs of a list from `index` on, as queue items
  66. on server jbQueue(ctx, index, count) {
  67. return queueFor(ctx, index, count)
  68. }
  69. // a new shuffle of a pool (`{ kind: 'favourites', minPlays }` or `{ kind: 'list', ctx }`): its context, with a seed from the clock
  70. on server jbShuffleCtx(spec) {
  71. return { kind = 'shuffle' seed = now() % 2147483000 spec = spec }
  72. }
  73. // the song was listened to long enough: the jukebox database adds 1 to its own counter (the same call as POST /api/played/<id>)
  74. on server jbPlayed(id) {
  75. return countPlay(id, now())
  76. }
  77. // ---- playing -----------------------------------------------------------------------------------------------------------------
  78. setBar = () => {
  79. barClass = 'jb-app' + (nowId != '' ? ' playing' : '') + (overlay == 'true' ? ' open' : '')
  80. }
  81. // the audio element of the media player
  82. audioEl = () => { return document.querySelector('media-player audio') }
  83. // show what plays (a queue item) and start it
  84. showNow = (item) => {
  85. nowSrc = item.src
  86. nowTitle = item.title
  87. nowArtist = item.artist
  88. nowAlbum = item.album
  89. nowCover = item.cover
  90. nowArt = item.art
  91. nowId = item.id
  92. nowIdx = item.idx
  93. heard = 0
  94. countedId = ''
  95. autoPlay = 'true'
  96. setBar()
  97. let ms = window.navigator.mediaSession
  98. if (ms != null) {
  99. ms.metadata = window.Reflect.construct(window.MediaMetadata, [ { title = item.title artist = item.artist album = item.album artwork = [ { src = item.art } ] } ])
  100. }
  101. }
  102. // a queue that starts at the song that plays
  103. fromPlace = (at) => {
  104. out = []
  105. let i = at
  106. while (i < nowQueue.length) {
  107. out.push(nowQueue[i])
  108. i = i + 1
  109. }
  110. return out
  111. }
  112. // the button or row that started a song gives the focus back (the player's own buttons keep it), so Space keeps meaning play / pause
  113. on jbUnfocus() {
  114. f = document.activeElement
  115. if (f != null && f.blur != null && f.closest('media-player') == null) { f.blur() }
  116. return null
  117. }
  118. // a song was tapped in a screen: its list becomes the queue
  119. on jbPlay(ctx, index) {
  120. emit jbUnfocus()
  121. items = emit server jbQueue(ctx, index, 40)
  122. if (items == null || items.length == 0) { return null }
  123. nowCtx = ctx
  124. nowQueue = items
  125. showNow(items[0])
  126. return null
  127. }
  128. // a shuffle was asked for: a new random order of the pool, played from its start
  129. on jbShuffle(spec) {
  130. emit jbUnfocus()
  131. ctx = emit server jbShuffleCtx(spec)
  132. emit jbPlay(ctx, 0)
  133. return null
  134. }
  135. // the place of a song in the queue, -1 when it is not there
  136. placeOf = (src) => {
  137. let at = -1
  138. let i = 0
  139. while (i < nowQueue.length) {
  140. if (nowQueue[i].src == src) { at = i }
  141. i = i + 1
  142. }
  143. return at
  144. }
  145. // the player picked a queue item (a tap on it, or the end of the song before): it becomes the first of the queue; the queue is refilled when fewer than 25 songs follow
  146. pickedNext = (src) => {
  147. at = placeOf(src)
  148. if (at < 0) { return }
  149. item = nowQueue[at]
  150. nowQueue = fromPlace(at)
  151. showNow(item)
  152. if (nowQueue.length < 25) {
  153. more = emit server jbQueue(nowCtx, item.idx, 40)
  154. if (more != null && more.length > 0) { nowQueue = more }
  155. }
  156. }
  157. // next song: the second of the queue, or a request when the list has run out of items here
  158. on jbNext() {
  159. if (nowId == '') { return null }
  160. if (nowQueue.length > 1) { pickedNext(nowQueue[1].src) return null }
  161. more = emit server jbQueue(nowCtx, nowIdx + 1, 40)
  162. if (more != null && more.length > 0) {
  163. nowQueue = more
  164. showNow(more[0])
  165. }
  166. return null
  167. }
  168. // previous song: after 3 seconds into a song the first press starts it again, then the song before it in the list
  169. on jbPrev() {
  170. if (nowId == '') { return null }
  171. let a = audioEl()
  172. if (a != null && a.currentTime > 3) { a.currentTime = 0 return null }
  173. if (nowIdx < 1) { if (a != null) { a.currentTime = 0 } return null }
  174. items = emit server jbQueue(nowCtx, nowIdx - 1, 40)
  175. if (items != null && items.length > 0) {
  176. nowQueue = items
  177. showNow(items[0])
  178. }
  179. return null
  180. }
  181. // the buttons give the focus back, so Space keeps meaning play / pause
  182. on jbNextClick(e) { e.target.closest('button').blur() emit jbNext() return null }
  183. on jbPrevClick(e) { e.target.closest('button').blur() emit jbPrev() return null }
  184. toggleNow = () => {
  185. let a = audioEl()
  186. if (a == null || nowId == '') { return }
  187. if (a.paused) { a.play() } else { a.pause() }
  188. }
  189. jumpBy = (seconds) => {
  190. let a = audioEl()
  191. if (a == null || nowId == '') { return }
  192. let t = a.currentTime + seconds
  193. if (t < 0) { t = 0 }
  194. if (a.duration > 0 && t > a.duration) { t = a.duration }
  195. a.currentTime = t
  196. }
  197. // ---- the overlay ---------------------------------------------------------------------------------------------------------------
  198. on jbOpen() {
  199. if (nowId == '') { return null }
  200. overlay = 'true'
  201. setBar()
  202. return null
  203. }
  204. on jbClose(e) {
  205. if (e.target != null && e.target.closest != null && e.target.closest('button') != null) { e.target.closest('button').blur() }
  206. overlay = 'false'
  207. setBar()
  208. return null
  209. }
  210. // a tap on the bar (not on a button or the seek line) opens the overlay
  211. on jbBarClick(e) {
  212. if (overlay == 'true') { return null }
  213. if (e.target.closest('button, mp-seek') != null) { return null }
  214. emit jbOpen()
  215. return null
  216. }
  217. // a swipe down over the cover or the title closes it (not over the buttons, the seek line or the queue, which scroll)
  218. on jbDown(e) {
  219. if (overlay != 'true' || e.target.closest('button, mp-seek, ol') != null) { swipeY = -1 return null }
  220. swipeY = e.clientY
  221. return null
  222. }
  223. on jbUp(e) {
  224. if (swipeY >= 0 && e.clientY - swipeY > 80) { overlay = 'false' setBar() }
  225. swipeY = -1
  226. return null
  227. }
  228. on jbCancel(e) { swipeY = -1 return null }
  229. // ---- counting, keys and the lock screen -----------------------------------------------------------------------------------
  230. // every second: the seconds the audio really played; at half the song or 4 minutes the server counts the play, once
  231. on jbTick() {
  232. let a = audioEl()
  233. if (a == null || nowId == '' || a.paused || a.ended) { return null }
  234. heard = heard + 1
  235. let need = 240
  236. if (a.duration > 0 && a.duration / 2 < 240) { need = a.duration / 2 }
  237. if (heard >= need && countedId != nowId) {
  238. countedId = nowId
  239. emit server jbPlayed(nowId)
  240. }
  241. return null
  242. }
  243. // the mount tick (the media player's trick): once the page is live the timer, the keys and the lock screen's buttons are set up
  244. on jbMount(e) {
  245. e.target.style.animationName = 'none'
  246. if (ticking == 'true') { return null }
  247. ticking = 'true'
  248. window.setInterval(() => { emit jbTick() }, 1000)
  249. let ms = window.navigator.mediaSession
  250. if (ms != null) {
  251. ms.setActionHandler('play', () => { toggleNow() })
  252. ms.setActionHandler('pause', () => { toggleNow() })
  253. ms.setActionHandler('previoustrack', () => { emit jbPrev() })
  254. ms.setActionHandler('nexttrack', () => { emit jbNext() })
  255. }
  256. return null
  257. }

Branches

Latest commits

  • 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