ui: make the VR button draggable

It was pinned to the bottom-right corner. On a short window that put it straight
on top of the bitrate choices the playback watchdog shows (that panel is
centred), and there was no way to move it out of the way.

The button now uses left/top (set from JS) instead of right/bottom, is draggable
with pointer events, and remembers where it was dropped. Details that matter:

  - touch-action:none, otherwise a drag on a touch screen scrolls the page
  - a drag still fires a click on most browsers, so the click that ends a drag is
    swallowed; dragging never opens the panel by accident
  - clamped to the viewport, and re-clamped on resize
  - double click resets to the default corner (escape hatch for a button dragged
    somewhere awkward)
  - the settings panel follows the button instead of the viewport corner, so the
    two cannot drift apart; it prefers to sit just above the button, falls back
    to below, and clamps on screen when it is taller than the space available

Verified against a local harness, 18 checks, all passing: default placement
matches the old corner, a drag moves it by exactly the pointer delta and
persists it, the click ending a drag does not open the panel, a plain click
still does, the panel tracks the button's right edge, clamping holds at all four
edges, and double click restores the default.
This commit is contained in:
2026-10-18 01:20:22 +08:00
parent 85fea48159
commit 30f97808ae
2 changed files with 316 additions and 10 deletions
+158 -5
View File
@@ -5219,16 +5219,23 @@
// Sits ABOVE Jellyfin's own OSD bar: at the viewport's bottom-right it
// covered the player's fullscreen button (owner report). 120 px clears the
// OSD bottom bar; the settings panel stacks above it.
'.jfvr-btn{position:fixed;right:16px;bottom:120px;z-index:2147483000;display:flex;align-items:center;gap:8px;',
// left/top are set from JS (see placeButton) so the button can be dragged
// anywhere. touch-action:none is what makes a drag work on a touch screen
// instead of scrolling the page underneath.
'.jfvr-btn{position:fixed;z-index:2147483000;display:flex;align-items:center;gap:8px;',
'padding:10px 16px;border:0;border-radius:24px;background:linear-gradient(135deg,#4ea1ff,#7b5cff);color:#fff;',
'font-size:15px;font-weight:600;cursor:pointer;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit}',
'font-size:15px;font-weight:600;cursor:grab;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit;',
'touch-action:none;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}',
'.jfvr-btn:active{transform:scale(.96)}',
'.jfvr-btn.jfvr-drag{cursor:grabbing;transform:none;box-shadow:0 10px 32px rgba(0,0,0,.65)}',
// the panel must always fit the viewport AND keep the CTA reachable, so the
// body scrolls and the button lives in a sticky footer
'.jfvr-panel{position:fixed;right:16px;bottom:180px;z-index:2147483001;width:290px;background:rgba(20,20,24,.96);',
// Positioned from JS (positionPanel) so it follows the button once the
// button has been dragged; it no longer lives in the viewport corner.
'.jfvr-panel{position:fixed;z-index:2147483001;width:290px;background:rgba(20,20,24,.96);',
'color:#eaeaea;border:1px solid rgba(255,255,255,.14);border-radius:14px;box-shadow:0 12px 40px rgba(0,0,0,.6);',
'font-family:inherit;font-size:14px;backdrop-filter:blur(10px);display:flex;flex-direction:column;',
'max-height:calc(100vh - 200px);overflow:hidden}',
'max-height:calc(100vh - 32px);overflow:hidden}',
'.jfvr-head{display:flex;justify-content:space-between;align-items:center;padding:10px 12px;background:rgba(255,255,255,.06);font-weight:600;flex:0 0 auto}',
'.jfvr-x{background:none;border:0;color:#aaa;font-size:20px;line-height:1;cursor:pointer;padding:0 4px}',
'.jfvr-body{padding:12px;overflow-y:auto;flex:1 1 auto;min-height:0}',
@@ -5276,9 +5283,151 @@
var b = NS.makeEl('button', 'jfvr-btn');
b.type = 'button';
b.innerHTML = '<span>&#127909;</span><span>VR 播放</span>';
b.addEventListener('click', function (ev) { ev.stopPropagation(); togglePanel(); });
b.title = '点击打开设置;按住拖动可以换位置;双击回到默认位置';
b.addEventListener('click', function (ev) {
ev.stopPropagation();
// A drag still produces a click on most browsers. Swallow that one so
// dragging the button never opens the panel by accident.
if (dragMoved) { dragMoved = false; return; }
togglePanel();
});
// Escape hatch: a button dragged half off a small window is annoying to
// recover by hand.
b.addEventListener('dblclick', function (ev) {
ev.stopPropagation();
resetButtonPos();
});
installDrag(b);
document.body.appendChild(b);
btn = b;
btnPos = readBtnPos();
placeButton(b, btnPos);
}
/* ------------------------------------------------------------------ *
* button placement / dragging
* ------------------------------------------------------------------
* The button used to be pinned to the bottom-right corner. On a short window
* that put it right on top of the bitrate choices the playback watchdog
* shows (that panel is centred), and there was no way to move it. It is
* draggable now and the spot is remembered per browser.
*
* The settings panel follows the button rather than the viewport corner, so
* the two cannot drift apart.
* ------------------------------------------------------------------ */
var BTN_POS_KEY = 'jfvr-btn-pos';
var BTN_MARGIN = 8;
var DRAG_SLOP = 4; // px of travel before it counts as a drag, not a tap
var btnPos = null; // {left, top} once placed; persisted on drop
var dragging = false;
var dragMoved = false;
var dragStart = null;
function readBtnPos() {
try {
var o = JSON.parse(localStorage.getItem(BTN_POS_KEY) || 'null');
if (o && typeof o.left === 'number' && typeof o.top === 'number') return o;
} catch (e) {}
return null;
}
function writeBtnPos(pos) {
try { localStorage.setItem(BTN_POS_KEY, JSON.stringify(pos)); } catch (e) {}
}
function clampPos(left, top, w, h) {
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
return {
left: Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)),
top: Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop))
};
}
/** The default spot: the old fixed corner, expressed in left/top. */
function defaultPos(b) {
return { left: window.innerWidth - b.offsetWidth - 16,
top: window.innerHeight - b.offsetHeight - 120 };
}
function placeButton(b, pos) {
var p = pos || defaultPos(b);
var c = clampPos(p.left, p.top, b.offsetWidth, b.offsetHeight);
b.style.left = c.left + 'px';
b.style.top = c.top + 'px';
b.style.right = 'auto';
b.style.bottom = 'auto';
return c;
}
function resetButtonPos() {
if (!btn) return;
try { localStorage.removeItem(BTN_POS_KEY); } catch (e) {}
btnPos = placeButton(btn, null);
positionPanel();
}
function installDrag(b) {
b.addEventListener('pointerdown', function (ev) {
if (ev.button != null && ev.button !== 0) return; // primary button / touch only
var r = b.getBoundingClientRect();
dragging = true;
dragMoved = false;
dragStart = { x: ev.clientX, y: ev.clientY, left: r.left, top: r.top };
b.classList.add('jfvr-drag');
try { b.setPointerCapture(ev.pointerId); } catch (e) {}
});
b.addEventListener('pointermove', function (ev) {
if (!dragging || !dragStart) return;
var dx = ev.clientX - dragStart.x;
var dy = ev.clientY - dragStart.y;
if (!dragMoved) {
if (Math.abs(dx) < DRAG_SLOP && Math.abs(dy) < DRAG_SLOP) return;
dragMoved = true;
if (panel) hidePanel(); // do not drag the panel around with it
}
btnPos = clampPos(dragStart.left + dx, dragStart.top + dy, b.offsetWidth, b.offsetHeight);
b.style.left = btnPos.left + 'px';
b.style.top = btnPos.top + 'px';
});
function endDrag(ev) {
if (!dragging) return;
dragging = false;
dragStart = null;
b.classList.remove('jfvr-drag');
try { b.releasePointerCapture(ev.pointerId); } catch (e) {}
if (dragMoved) writeBtnPos(btnPos);
}
b.addEventListener('pointerup', endDrag);
b.addEventListener('pointercancel', endDrag);
}
/** Put the panel next to the button: above it, or below when there is no room. */
function positionPanel() {
if (!panel || !btn) return;
var b = btn.getBoundingClientRect();
var w = panel.offsetWidth;
var h = panel.offsetHeight;
var gap = 8;
var left = b.right - w; // right edges aligned
var top = b.top - h - gap;
if (top < BTN_MARGIN) top = b.bottom + gap; // no room above -> below
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
panel.style.left = Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)) + 'px';
panel.style.top = Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop)) + 'px';
panel.style.right = 'auto';
panel.style.bottom = 'auto';
}
function onViewportResize() {
if (btn && document.body.contains(btn)) btnPos = placeButton(btn, btnPos);
positionPanel();
}
function removeButton() {
@@ -5462,6 +5611,7 @@
panel.appendChild(foot);
document.body.appendChild(panel);
positionPanel();
panelSync = function () {
for (var i = 0; i < panelSyncs.length; i++) {
@@ -6873,6 +7023,9 @@
window.addEventListener('beforeunload', function () {
if (S.session) { try { S.session.end(); } catch (e) {} }
});
// Keep the draggable button (and the panel that follows it) on screen when
// the window changes size.
window.addEventListener('resize', onViewportResize);
installFrameTracker();
window.__JFVR_STATE__ = S;
+158 -5
View File
@@ -299,16 +299,23 @@
// Sits ABOVE Jellyfin's own OSD bar: at the viewport's bottom-right it
// covered the player's fullscreen button (owner report). 120 px clears the
// OSD bottom bar; the settings panel stacks above it.
'.jfvr-btn{position:fixed;right:16px;bottom:120px;z-index:2147483000;display:flex;align-items:center;gap:8px;',
// left/top are set from JS (see placeButton) so the button can be dragged
// anywhere. touch-action:none is what makes a drag work on a touch screen
// instead of scrolling the page underneath.
'.jfvr-btn{position:fixed;z-index:2147483000;display:flex;align-items:center;gap:8px;',
'padding:10px 16px;border:0;border-radius:24px;background:linear-gradient(135deg,#4ea1ff,#7b5cff);color:#fff;',
'font-size:15px;font-weight:600;cursor:pointer;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit}',
'font-size:15px;font-weight:600;cursor:grab;box-shadow:0 6px 22px rgba(0,0,0,.45);font-family:inherit;',
'touch-action:none;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}',
'.jfvr-btn:active{transform:scale(.96)}',
'.jfvr-btn.jfvr-drag{cursor:grabbing;transform:none;box-shadow:0 10px 32px rgba(0,0,0,.65)}',
// the panel must always fit the viewport AND keep the CTA reachable, so the
// body scrolls and the button lives in a sticky footer
'.jfvr-panel{position:fixed;right:16px;bottom:180px;z-index:2147483001;width:290px;background:rgba(20,20,24,.96);',
// Positioned from JS (positionPanel) so it follows the button once the
// button has been dragged; it no longer lives in the viewport corner.
'.jfvr-panel{position:fixed;z-index:2147483001;width:290px;background:rgba(20,20,24,.96);',
'color:#eaeaea;border:1px solid rgba(255,255,255,.14);border-radius:14px;box-shadow:0 12px 40px rgba(0,0,0,.6);',
'font-family:inherit;font-size:14px;backdrop-filter:blur(10px);display:flex;flex-direction:column;',
'max-height:calc(100vh - 200px);overflow:hidden}',
'max-height:calc(100vh - 32px);overflow:hidden}',
'.jfvr-head{display:flex;justify-content:space-between;align-items:center;padding:10px 12px;background:rgba(255,255,255,.06);font-weight:600;flex:0 0 auto}',
'.jfvr-x{background:none;border:0;color:#aaa;font-size:20px;line-height:1;cursor:pointer;padding:0 4px}',
'.jfvr-body{padding:12px;overflow-y:auto;flex:1 1 auto;min-height:0}',
@@ -356,9 +363,151 @@
var b = NS.makeEl('button', 'jfvr-btn');
b.type = 'button';
b.innerHTML = '<span>&#127909;</span><span>VR 播放</span>';
b.addEventListener('click', function (ev) { ev.stopPropagation(); togglePanel(); });
b.title = '点击打开设置;按住拖动可以换位置;双击回到默认位置';
b.addEventListener('click', function (ev) {
ev.stopPropagation();
// A drag still produces a click on most browsers. Swallow that one so
// dragging the button never opens the panel by accident.
if (dragMoved) { dragMoved = false; return; }
togglePanel();
});
// Escape hatch: a button dragged half off a small window is annoying to
// recover by hand.
b.addEventListener('dblclick', function (ev) {
ev.stopPropagation();
resetButtonPos();
});
installDrag(b);
document.body.appendChild(b);
btn = b;
btnPos = readBtnPos();
placeButton(b, btnPos);
}
/* ------------------------------------------------------------------ *
* button placement / dragging
* ------------------------------------------------------------------
* The button used to be pinned to the bottom-right corner. On a short window
* that put it right on top of the bitrate choices the playback watchdog
* shows (that panel is centred), and there was no way to move it. It is
* draggable now and the spot is remembered per browser.
*
* The settings panel follows the button rather than the viewport corner, so
* the two cannot drift apart.
* ------------------------------------------------------------------ */
var BTN_POS_KEY = 'jfvr-btn-pos';
var BTN_MARGIN = 8;
var DRAG_SLOP = 4; // px of travel before it counts as a drag, not a tap
var btnPos = null; // {left, top} once placed; persisted on drop
var dragging = false;
var dragMoved = false;
var dragStart = null;
function readBtnPos() {
try {
var o = JSON.parse(localStorage.getItem(BTN_POS_KEY) || 'null');
if (o && typeof o.left === 'number' && typeof o.top === 'number') return o;
} catch (e) {}
return null;
}
function writeBtnPos(pos) {
try { localStorage.setItem(BTN_POS_KEY, JSON.stringify(pos)); } catch (e) {}
}
function clampPos(left, top, w, h) {
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
return {
left: Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)),
top: Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop))
};
}
/** The default spot: the old fixed corner, expressed in left/top. */
function defaultPos(b) {
return { left: window.innerWidth - b.offsetWidth - 16,
top: window.innerHeight - b.offsetHeight - 120 };
}
function placeButton(b, pos) {
var p = pos || defaultPos(b);
var c = clampPos(p.left, p.top, b.offsetWidth, b.offsetHeight);
b.style.left = c.left + 'px';
b.style.top = c.top + 'px';
b.style.right = 'auto';
b.style.bottom = 'auto';
return c;
}
function resetButtonPos() {
if (!btn) return;
try { localStorage.removeItem(BTN_POS_KEY); } catch (e) {}
btnPos = placeButton(btn, null);
positionPanel();
}
function installDrag(b) {
b.addEventListener('pointerdown', function (ev) {
if (ev.button != null && ev.button !== 0) return; // primary button / touch only
var r = b.getBoundingClientRect();
dragging = true;
dragMoved = false;
dragStart = { x: ev.clientX, y: ev.clientY, left: r.left, top: r.top };
b.classList.add('jfvr-drag');
try { b.setPointerCapture(ev.pointerId); } catch (e) {}
});
b.addEventListener('pointermove', function (ev) {
if (!dragging || !dragStart) return;
var dx = ev.clientX - dragStart.x;
var dy = ev.clientY - dragStart.y;
if (!dragMoved) {
if (Math.abs(dx) < DRAG_SLOP && Math.abs(dy) < DRAG_SLOP) return;
dragMoved = true;
if (panel) hidePanel(); // do not drag the panel around with it
}
btnPos = clampPos(dragStart.left + dx, dragStart.top + dy, b.offsetWidth, b.offsetHeight);
b.style.left = btnPos.left + 'px';
b.style.top = btnPos.top + 'px';
});
function endDrag(ev) {
if (!dragging) return;
dragging = false;
dragStart = null;
b.classList.remove('jfvr-drag');
try { b.releasePointerCapture(ev.pointerId); } catch (e) {}
if (dragMoved) writeBtnPos(btnPos);
}
b.addEventListener('pointerup', endDrag);
b.addEventListener('pointercancel', endDrag);
}
/** Put the panel next to the button: above it, or below when there is no room. */
function positionPanel() {
if (!panel || !btn) return;
var b = btn.getBoundingClientRect();
var w = panel.offsetWidth;
var h = panel.offsetHeight;
var gap = 8;
var left = b.right - w; // right edges aligned
var top = b.top - h - gap;
if (top < BTN_MARGIN) top = b.bottom + gap; // no room above -> below
var maxLeft = Math.max(BTN_MARGIN, window.innerWidth - w - BTN_MARGIN);
var maxTop = Math.max(BTN_MARGIN, window.innerHeight - h - BTN_MARGIN);
panel.style.left = Math.round(Math.min(Math.max(left, BTN_MARGIN), maxLeft)) + 'px';
panel.style.top = Math.round(Math.min(Math.max(top, BTN_MARGIN), maxTop)) + 'px';
panel.style.right = 'auto';
panel.style.bottom = 'auto';
}
function onViewportResize() {
if (btn && document.body.contains(btn)) btnPos = placeButton(btn, btnPos);
positionPanel();
}
function removeButton() {
@@ -542,6 +691,7 @@
panel.appendChild(foot);
document.body.appendChild(panel);
positionPanel();
panelSync = function () {
for (var i = 0; i < panelSyncs.length; i++) {
@@ -1953,6 +2103,9 @@
window.addEventListener('beforeunload', function () {
if (S.session) { try { S.session.end(); } catch (e) {} }
});
// Keep the draggable button (and the panel that follows it) on screen when
// the window changes size.
window.addEventListener('resize', onViewportResize);
installFrameTracker();
window.__JFVR_STATE__ = S;