Size motion photo clip to the still and make the toggle sticky #286

This commit is contained in:
Alan Grainger
2026-09-17 21:45:55 +12:00
parent c3dccf4d25
commit 16ba140766
10 changed files with 82 additions and 70 deletions
+4 -6
View File
@@ -139,14 +139,12 @@ html.pswp-no-arrows .pswp__button--arrow {
background: #000;
}
/* Motion photo (Live Photo) clip. It lives inside the slide's zoom wrap, which
carries PhotoSwipe's pan/zoom transform, so it stays pinned to the still
underneath through zoom, pan and resize with no geometry tracking in JS. */
/* Motion photo (Live Photo) clip, inside the slide's zoom wrap so it follows
PhotoSwipe's pan / zoom. Width / height are set inline from the still. */
.pswp__motion-video {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
top: 0;
left: 0;
object-fit: contain;
pointer-events: none; /* PhotoSwipe keeps handling drag / zoom / swipe */
}
+2 -4
View File
@@ -16,8 +16,7 @@ export const ICON_FULLSCREEN = '<svg class="pswp__icn" viewBox="0 0 24 24" aria-
export const ICON_FULLSCREEN_EXIT = '<svg class="pswp__icn" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M14,14H19V16H16V19H14V14M5,14H10V19H8V16H5V14M8,5H10V10H5V8H8V5M19,8V10H14V5H16V8H19Z"/></svg>'
export const ICON_INFO = '<svg class="pswp__icn" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M11,9H13V7H11M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M11,17H13V11H11V17Z"/></svg>'
export const ICON_BACK = '<svg class="pswp__icn" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M20,11V13H8L13.5,18.5L12.08,19.92L4.16,12L12.08,4.08L13.5,5.5L8,11H20Z"/></svg>'
// Motion photo (Live Photo) toggle - play when stopped, pause while the
// clip is running. Same pair Immich uses in its own asset viewer.
// Motion photo (Live Photo) toggle, off / on. Same pair as Immich's viewer.
export const ICON_MOTION_PLAY = '<svg class="pswp__icn" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M10 16.5L16 12L10 7.5M22 12C22 6.46 17.54 2 12 2C10.83 2 9.7 2.19 8.62 2.56L9.32 4.5C10.17 4.16 11.06 3.97 12 3.97C16.41 3.97 20.03 7.59 20.03 12C20.03 16.41 16.41 20.03 12 20.03C7.59 20.03 3.97 16.41 3.97 12C3.97 11.06 4.16 10.12 4.5 9.28L2.56 8.62C2.19 9.7 2 10.83 2 12C2 17.54 6.46 22 12 22C17.54 22 22 17.54 22 12M5.47 3.97C6.32 3.97 7 4.68 7 5.47C7 6.32 6.32 7 5.47 7C4.68 7 3.97 6.32 3.97 5.47C3.97 4.68 4.68 3.97 5.47 3.97Z"/></svg>'
export const ICON_MOTION_PAUSE = '<svg class="pswp__icn" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M22 12C22 6.46 17.54 2 12 2C10.83 2 9.7 2.19 8.62 2.56L9.32 4.5C10.17 4.16 11.06 3.97 12 3.97C16.41 3.97 20.03 7.59 20.03 12C20.03 16.41 16.41 20.03 12 20.03C7.59 20.03 3.97 16.41 3.97 12C3.97 11.06 4.16 10.12 4.5 9.28L2.56 8.62C2.19 9.7 2 10.83 2 12C2 17.54 6.46 22 12 22C17.54 22 22 17.54 22 12M5.47 7C4.68 7 3.97 6.32 3.97 5.47C3.97 4.68 4.68 3.97 5.47 3.97C6.32 3.97 7 4.68 7 5.47C7 6.32 6.32 7 5.47 7M9 9H11V15H9M13 9H15V15H13"/></svg>'
export const ICON_CLOSE = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z"/></svg>'
@@ -32,6 +31,5 @@ export const ICON_MAP = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill=
// Check glyph shown inside selected tiles' checkmark circle
export const CHECK_SVG = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"/></svg>'
// Motion photo indicator drawn in the corner of a gallery tile. Same glyph
// as ICON_MOTION_PLAY without PhotoSwipe's `pswp__icn` sizing class.
// Tile-corner motion badge: ICON_MOTION_PLAY without the `pswp__icn` sizing.
export const ICON_MOTION_BADGE = '<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M10 16.5L16 12L10 7.5M22 12C22 6.46 17.54 2 12 2C10.83 2 9.7 2.19 8.62 2.56L9.32 4.5C10.17 4.16 11.06 3.97 12 3.97C16.41 3.97 20.03 7.59 20.03 12C20.03 16.41 16.41 20.03 12 20.03C7.59 20.03 3.97 16.41 3.97 12C3.97 11.06 4.16 10.12 4.5 9.28L2.56 8.62C2.19 9.7 2 10.83 2 12C2 17.54 6.46 22 12 22C17.54 22 22 17.54 22 12M5.47 3.97C6.32 3.97 7 4.68 7 5.47C7 6.32 6.32 7 5.47 7C4.68 7 3.97 6.32 3.97 5.47C3.97 4.68 4.68 3.97 5.47 3.97Z"/></svg>'
+62 -31
View File
@@ -171,32 +171,35 @@ export function registerFullscreenButton (lightbox: LightboxInstance) {
})
}
// Set on the zoom wrap while a motion photo's clip is painting, so CSS can
// fade the still out underneath it.
// Set on the zoom wrap while a clip is painting, so CSS can fade the still out.
const MOTION_PLAYING_CLASS = 'pswp--motion-playing'
// Sticky: once on, every motion photo the visitor opens plays its clip until
// they turn it off. Module-level so it survives reopening the lightbox.
let motionEnabled = false
/**
* The `.pswp__zoom-wrap` of the slide currently on screen. Mounting the clip
* inside it is what makes pan / zoom free: that element carries PhotoSwipe's
* transform, so an `inset: 0` child stays pinned to the photo without any
* geometry tracking of our own.
* The `.pswp__zoom-wrap` of the current slide. It carries PhotoSwipe's pan /
* zoom transform, so a child at its origin follows the still for free. It has
* no box of its own though (auto width / height, absolute children), so the
* clip can't be sized from it.
*/
function currentZoomWrap (pswp: PswpInstance): HTMLElement | null {
const container = pswp.currSlide?.container
if (container instanceof HTMLElement) return container
// Fallback for PhotoSwipe builds that don't expose the slide off `pswp`.
const fallback = pswp.element.querySelector('.pswp__item[aria-hidden="false"] .pswp__zoom-wrap')
return fallback instanceof HTMLElement ? fallback : null
return container instanceof HTMLElement ? container : null
}
/** The loaded still in a zoom wrap (not the thumbnail placeholder), if any. */
function loadedStill (wrap: HTMLElement): HTMLElement | null {
const still = wrap.querySelector('.pswp__img:not(.pswp__img--placeholder)')
return still instanceof HTMLElement ? still : null
}
/**
* Register the motion photo (Live Photo) toggle: swaps the still for its short
* clip and returns to the still when the clip ends. The button hides itself on
* slides with no clip, and the server omits `motionUrl` altogether when
* `ipp.motionPhotos` is off, so it then never appears at all.
*
* The `<video>` is created on demand with `preload="none"`: nothing is fetched
* from Immich until a visitor actually presses the button.
* Register the motion photo (Live Photo) toggle. While on, each motion photo
* plays its clip over the still on arrival and reverts when the clip ends.
* The button hides itself on slides without a clip. Nothing is fetched from
* Immich until the toggle is turned on.
*/
export function registerMotionButton (lightbox: LightboxInstance) {
lightbox.on('uiRegister', () => {
@@ -207,59 +210,87 @@ export function registerMotionButton (lightbox: LightboxInstance) {
html: ICON_MOTION_PLAY,
onInit: (el: HTMLElement, pswp: PswpInstance) => {
let video: HTMLVideoElement | null = null
let sizer: ResizeObserver | null = null
let stillWatcher: MutationObserver | null = null
const update = () => {
el.hidden = !state.items[pswp.currIndex]?.motionUrl
const label = video ? 'Stop motion photo' : 'Play motion photo'
el.innerHTML = video ? ICON_MOTION_PAUSE : ICON_MOTION_PLAY
const label = motionEnabled ? 'Stop playing motion photos' : 'Play motion photos'
el.innerHTML = motionEnabled ? ICON_MOTION_PAUSE : ICON_MOTION_PLAY
el.setAttribute('aria-label', label)
el.setAttribute('title', label)
}
// PhotoSwipe reuses slide DOM, so the clip has to be torn down on slide
// change too, not only when it reaches its end.
const stop = () => {
stillWatcher?.disconnect()
stillWatcher = null
const clip = video
if (!clip) return
video = null
sizer?.disconnect()
sizer = null
clip.pause()
clip.parentElement?.classList.remove(MOTION_PLAYING_CLASS)
clip.remove()
update()
}
const play = () => {
if (video) return
const item = state.items[pswp.currIndex]
const wrap = currentZoomWrap(pswp)
if (!item?.motionUrl || !wrap) return
const still = loadedStill(wrap)
if (!still) {
// PhotoSwipe appends the still after it loads, which can be after
// the slide change. Wait for it.
stillWatcher?.disconnect()
stillWatcher = new MutationObserver(() => {
if (loadedStill(wrap)) play()
})
stillWatcher.observe(wrap, { childList: true })
return
}
stillWatcher?.disconnect()
stillWatcher = null
const clip = document.createElement('video')
clip.className = 'pswp__motion-video'
// Muted is not optional - browsers refuse programmatic playback of
// audible media, and Immich's motion clips are silent regardless.
clip.muted = true
clip.muted = true // required for programmatic playback
clip.playsInline = true
clip.preload = 'none'
clip.src = item.motionUrl
// Mirror the still's inline size, which PhotoSwipe rewrites on
// resize and after each zoom gesture.
const sizeToStill = () => {
clip.style.width = still.offsetWidth + 'px'
clip.style.height = still.offsetHeight + 'px'
}
sizeToStill()
sizer = new ResizeObserver(sizeToStill)
sizer.observe(still)
clip.addEventListener('ended', stop, { once: true })
// Hide the still only once the clip is actually painting, so a slow
// clip never leaves the slide blank. The identity check keeps a late
// event from a clip we already stopped from hiding the still for good.
clip.addEventListener('error', stop, { once: true })
// Hide the still only once the clip is painting, so a slow clip never
// leaves the slide blank.
clip.addEventListener('playing', () => {
if (video === clip) wrap.classList.add(MOTION_PLAYING_CLASS)
}, { once: true })
wrap.appendChild(clip)
video = clip
update()
clip.play().catch(() => stop())
}
el.addEventListener('click', () => {
if (video) stop()
else play()
motionEnabled = !motionEnabled
if (motionEnabled) play()
else stop()
update()
})
// Also fires for the opening slide, so this covers reopening with
// the toggle on.
pswp.on('change', () => {
stop()
update()
if (motionEnabled) play()
})
pswp.on('destroy', stop)
update()
+1 -2
View File
@@ -239,8 +239,7 @@ export function initLightbox () {
registerBackButton(state.lightbox)
if (state.lightboxConfig.showDownload) registerDownloadButton(state.lightbox)
registerFullscreenButton(state.lightbox)
// Hides itself on slides with no clip, so no config check is needed here -
// the server omits `motionUrl` when `ipp.motionPhotos` is off.
// No config gate: the server omits `motionUrl` when motion photos are off.
registerMotionButton(state.lightbox)
if (state.metadataConfig.descriptionInCaption) registerCaption(state.lightbox)
if (state.metadataConfig.sidebarHasContent) registerSidebar(state.lightbox)
+1 -2
View File
@@ -79,8 +79,7 @@ export async function gallery (res: Response, share: SharedLink, openItem?: numb
const fullUrl = zoomUpgrade && asset.type === AssetType.image && !requiresOriginal(asset)
? photoUrl(share.key, asset.id, ImageSize.fullsize)
: undefined
// Motion photo clip. Reuses the video route; Immich authorises the clip
// under the same share key.
// Motion photo clip, served through the video route under the same key.
const motionUrl = motionPhotos && asset.type === AssetType.image && asset.livePhotoVideoId
? videoUrl(share.key, asset.livePhotoVideoId)
: undefined
+3 -6
View File
@@ -131,18 +131,15 @@ async function resolveSharedAsset (req: Request, keyType: KeyType, allowMotion =
// enforces this via the share key).
const asset = resolved.link.assets.find(a => a.id === req.params.id)
if (asset) return { ok: true, link: resolved.link, asset }
// A motion photo's clip is a hidden asset that never appears in the share's
// own asset list, but Immich authorises it under the same key. Accept it only
// for the video route, and only when a shared still actually points at it -
// the whitelist stays bounded by the share's contents.
// A motion photo's clip is a hidden asset outside the share's asset list.
// Serve it on the video route only, and only if a shared still points at it.
if (allowMotion) {
const parent = findMotionPhotoStill(resolved.link, req.params.id)
if (parent) {
return {
ok: true,
link: resolved.link,
// The clip streams inline from /video/playback, so the still's filename
// and mime must not follow it into Content-Disposition / sizing.
// Streams inline, so the still's filename / mime must not carry over.
asset: { ...parent, id: req.params.id, type: AssetType.video, originalFileName: undefined, originalMimeType: undefined }
}
}
+3 -7
View File
@@ -80,13 +80,9 @@ function expiryDateLocale (): string | undefined {
}
/**
* Find the shared still that a motion photo (Live Photo) clip belongs to.
*
* The clip is a separate, hidden Immich asset: it never appears in the share's
* own asset list, but Immich authorises it under the same shared-link key
* (`checkSharedLinkAccess` whitelists each shared asset's `livePhotoVideoId`).
* Matching against the share's own assets is what keeps the set of ids IPP will
* serve bounded by the share's contents.
* Find the shared still that a motion photo (Live Photo) clip belongs to. The
* clip is a hidden asset that Immich authorises under the same share key;
* requiring a shared still to point at it keeps the ids IPP serves bounded.
*/
export function findMotionPhotoStill (share: SharedLink, clipId: string): Asset | undefined {
if (!clipId) return undefined
+1 -3
View File
@@ -61,9 +61,7 @@ export interface Asset {
height?: number;
// Base64-encoded thumbhash for tasteful blur placeholders during lazy-load
thumbhash?: string;
// Motion photo (Live Photo) clip. A separate, hidden Immich asset; the same
// shared-link key authorises it (Immich's checkSharedLinkAccess whitelists
// each shared asset's livePhotoVideoId alongside the asset itself).
// Motion photo (Live Photo) clip: a hidden asset authorised by the same key
livePhotoVideoId?: string;
// True for album assets enumerated via the timeline API, which give us only
// grid fields (id, type, ratio, thumbhash, isTrashed, fileCreatedAt). Their
+3 -7
View File
@@ -6,13 +6,9 @@ import { findMotionPhotoStill } from '../src/share'
import { gallery } from '../src/gallery/builder'
/*
Motion photo (Live Photo) support. Two rules worth pinning down: the set of
clip ids the video route will serve stays bounded by the share's own assets,
and `motionUrl` reaches the client only for images that have a clip, and only
while `ipp.motionPhotos` is on.
The builder reads config through config/access, so mock that module and set
options per test rather than loading a real config file.
Motion photos: clip ids stay bounded by the share's own assets, and
`motionUrl` is emitted only for images with a clip while `ipp.motionPhotos`
is on. Config is mocked per test rather than loaded from a file.
*/
const cfg: Record<string, unknown> = {}
vi.mock('../src/config/access', () => ({