From 51917b445434c34511e7bc21eb541763e63ab175 Mon Sep 17 00:00:00 2001 From: Hermes Date: Sun, 9 Aug 2026 13:04:57 +0200 Subject: [PATCH] back: drop duplicate native back handlers (fix double-back); save/restore catalog scroll+selection --- plugin.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/plugin.js b/plugin.js index babc411..71d175a 100644 --- a/plugin.js +++ b/plugin.js @@ -92,24 +92,25 @@ var grid; // 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 scrollFocused() { var cont = document.querySelector('.mymovies'); var el = document.querySelector('.mymovies .card.selector.focus'); if (cont && el) { var delta = el.getBoundingClientRect().top - cont.getBoundingClientRect().top; cont.scrollTop += delta - 100; + comp._selectedId = el.getAttribute('data-id'); + comp._scrollTop = cont.scrollTop; } } this.create = function (b) { body = b; + // Back (ESC / Backspace) is handled natively by Lampa -> Controller.back(). + // 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 === 'Backspace' || e.key === 'Escape') { - e.preventDefault(); - e.stopPropagation(); - Lampa.Activity.backward(); - } else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { - // After Navigator moves focus, keep the focused card visible. + if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { setTimeout(scrollFocused, 30); } }; @@ -153,6 +154,7 @@ // Required by ActivitySlide contract: wire remote/keyboard navigation. // Cards are native .selector elements; Navigator moves focus, we scroll the // container so the focused card stays visible (native Lampa behaviour). + // On return from detail we restore the previously focused card + scroll offset. this.start = function () { var started = false; Lampa.Controller.add('content', { @@ -161,9 +163,17 @@ var cards = wrap.find('.card.selector'); if (!cards.length) return; Lampa.Controller.collectionSet(wrap); - if (!started) { - started = true; - Lampa.Controller.collectionFocus(cards[0], wrap); + if (started) return; // already focused once; keep current position + started = true; + var target = comp._selectedId + ? cards.filter('[data-id="' + comp._selectedId + '"]')[0] + : cards[0]; + if (!target) target = cards[0]; + 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'); }, @@ -224,19 +234,12 @@ this.create = function (b) { body = b; - // Keyboard fallback for Backspace / Escape (remote uses Controller 'back' -> Activity.backward) - comp._onKey = function (e) { - if (e.key === 'Backspace' || e.key === 'Escape') { - e.preventDefault(); - e.stopPropagation(); - Lampa.Activity.backward(); - } - }; - document.addEventListener('keydown', comp._onKey, true); + // Back (ESC / Backspace) is handled natively by Lampa -> Controller.back(), + // which calls our controller's `back` below. No keydown listener needed + // (a listener here would duplicate the native back and skip a screen). }; this.destroy = function () { - if (comp._onKey) document.removeEventListener('keydown', comp._onKey, true); }; this.start = function () {