From b5fbd9e02749b90fc1e00235c76a637ec0edc96c Mon Sep 17 00:00:00 2001 From: Hermes Date: Sun, 9 Aug 2026 13:29:59 +0200 Subject: [PATCH] catalog: self-heal lost cursor (focus a card if none focused on toggle); sync selection on all arrows --- plugin.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/plugin.js b/plugin.js index c8e2646..e88187d 100644 --- a/plugin.js +++ b/plugin.js @@ -93,6 +93,12 @@ // Keep the focused card visible while navigating down/up (native Lampa behaviour). // Also remember the focused card + scroll offset so we can restore it on return. + function syncSelection() { + var el = document.querySelector('.mymovies .card.selector.focus'); + var cont = document.querySelector('.mymovies'); + if (el) comp._selectedId = el.getAttribute('data-id'); + if (cont) comp._scrollTop = cont.scrollTop; + } function scrollFocused() { var cont = document.querySelector('.mymovies'); var el = document.querySelector('.mymovies .card.selector.focus'); @@ -110,8 +116,9 @@ // Our keydown listener only drives scroll on arrow navigation to avoid a // double-back (our call + Lampa's native call) that skips a screen. comp._onKey = function (e) { - if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { - setTimeout(scrollFocused, 30); + if (e.key === 'ArrowDown' || e.key === 'ArrowUp' || + e.key === 'ArrowLeft' || e.key === 'ArrowRight') { + setTimeout(function () { scrollFocused(); syncSelection(); }, 30); } }; document.addEventListener('keydown', comp._onKey, true); @@ -163,7 +170,16 @@ var cards = wrap.find('.card.selector'); if (!cards.length) return; Lampa.Controller.collectionSet(wrap); - if (started) return; // already focused once; keep current position + // If no card is currently focused, focus one (restore last, else first). + // This self-heals a lost cursor after returning from detail / menu. + var focused = wrap.find('.card.selector.focus'); + if (!focused.length) { + started = false; + } + if (started) { + syncSelection(); + return; + } started = true; var target = comp._selectedId ? cards.filter('[data-id="' + comp._selectedId + '"]')[0] @@ -172,12 +188,11 @@ Lampa.Controller.collectionFocus(target, wrap); if (comp._scrollTop) { var cont = wrap[0]; - // restore on next frame so layout is ready requestAnimationFrame(function () { cont.scrollTop = comp._scrollTop; }); } }, left: function () { if (Navigator.canmove('left')) Navigator.move('left'); else Lampa.Controller.toggle('menu'); }, - right: function () { Navigator.move('right'); }, + right: function () { Navigator.move('right'); Lampa.Controller.collectionFocus(document.querySelector('.mymovies .card.selector.focus'), $('.mymovies')); }, up: function () { Navigator.move('up'); }, down: function () { Navigator.move('down'); }, back: function () { Lampa.Activity.backward(); }