Files
immich/web/src/lib/managers/timeline-manager/internal/intersection-support.svelte.ts
midzelis be3cdd3fa4 fix(web): skip thumbhash fade for offscreen-loaded thumbnails
Thumbnails that finish loading while offscreen (in the 500px pre-load
zone) were still playing the thumbhash fade-out transition, causing
visual flicker when scrolling. This adds an `actuallyIntersecting`
property (zero-margin viewport check) alongside the existing
`intersecting` (500px expanded margins) to distinguish between
pre-loaded and truly visible assets.

- Refactor `calculateViewerAssetIntersecting` to return a numeric flag
  (NONE/PRE/ACTUAL) avoiding object allocation in the hot derived path
- Inline `calculateMonthGroupIntersecting` into
  `updateIntersectionMonthGroup` to avoid intermediate object allocation
- Thread `actuallyIntersecting` through AssetLayout → Month → Timeline
  → Thumbnail snippet chain
- Use `actuallyIntersecting` in Thumbnail to skip the fade when loaded
  offscreen
- Add unit tests for `isIntersecting` and
  `calculateViewerAssetIntersecting`
2026-03-21 04:35:53 +00:00

82 lines
2.5 KiB
TypeScript

import { TUNABLES } from '$lib/utils/tunables';
import type { MonthGroup } from '../month-group.svelte';
import { TimelineManager } from '../timeline-manager.svelte';
const {
TIMELINE: { INTERSECTION_EXPAND_TOP, INTERSECTION_EXPAND_BOTTOM },
} = TUNABLES;
/**
* General function to check if a rectangular region intersects with a window.
*/
export function isIntersecting(regionTop: number, regionBottom: number, windowTop: number, windowBottom: number) {
return (
(regionTop >= windowTop && regionTop < windowBottom) ||
(regionBottom >= windowTop && regionBottom < windowBottom) ||
(regionTop < windowTop && regionBottom >= windowBottom)
);
}
export function updateIntersectionMonthGroup(timelineManager: TimelineManager, month: MonthGroup) {
const monthGroupTop = month.top;
const monthGroupBottom = monthGroupTop + month.height;
const windowTop = timelineManager.visibleWindow.top;
const windowBottom = timelineManager.visibleWindow.bottom;
const actuallyIntersecting = isIntersecting(monthGroupTop, monthGroupBottom, windowTop, windowBottom);
let intersecting = actuallyIntersecting;
if (!actuallyIntersecting) {
intersecting = isIntersecting(
monthGroupTop,
monthGroupBottom,
windowTop - INTERSECTION_EXPAND_TOP,
windowBottom + INTERSECTION_EXPAND_BOTTOM,
);
}
month.intersecting = intersecting;
month.actuallyIntersecting = actuallyIntersecting;
if (intersecting) {
timelineManager.clearDeferredLayout(month);
}
}
// Bit flags for intersection state
export const Intersection = {
NONE: 0,
PRE: 1,
ACTUAL: 3, // includes PRE (both bits set)
} as const;
/**
* Returns a numeric flag: NONE (0), PRE (1, within expanded margin only), or ACTUAL (3, truly visible).
*/
export function calculateViewerAssetIntersecting(
timelineManager: TimelineManager,
positionTop: number,
positionHeight: number,
) {
const positionBottom = positionTop + positionHeight;
const headerHeight = timelineManager.headerHeight;
const windowTop = timelineManager.visibleWindow.top - headerHeight;
const windowBottom = timelineManager.visibleWindow.bottom + headerHeight;
if (isIntersecting(positionTop, positionBottom, windowTop, windowBottom)) {
return Intersection.ACTUAL;
}
if (
isIntersecting(
positionTop,
positionBottom,
windowTop - INTERSECTION_EXPAND_TOP,
windowBottom + INTERSECTION_EXPAND_BOTTOM,
)
) {
return Intersection.PRE;
}
return Intersection.NONE;
}