Skip to content

High performance self-hosted photo and video management solution.

refactor(web): replace asset-viewer listener based face hover with overlay elements (#27400)

Min Idzelis45ccdb37fbc2
diff --git a/web/src/lib/components/asset-viewer/DetailPanel.svelte b/web/src/lib/components/asset-viewer/DetailPanel.svelte
index f46ddee4d6..8753539715 100644
--- a/web/src/lib/components/asset-viewer/DetailPanel.svelte
+++ b/web/src/lib/components/asset-viewer/DetailPanel.svelte
@@ -10,7 +10,6 @@
import { authManager } from '$lib/managers/auth-manager.svelte';
import { featureFlagsManager } from '$lib/managers/feature-flags-manager.svelte';
import { Route } from '$lib/route';
- import { boundingBoxesArray } from '$lib/stores/people.store';
import { locale } from '$lib/stores/preferences.store';
import { getAssetMediaUrl, getPeopleThumbnailUrl } from '$lib/utils';
import { delay, getDimensions } from '$lib/utils/asset-utils';
@@ -56,7 +55,6 @@
let isOwner = $derived(authManager.authenticated && authManager.user.id === asset.ownerId);
let people = $derived(asset.people || []);
let unassignedFaces = $derived(asset.unassignedFaces || []);
- let showingHiddenPeople = $state(false);
let latlng = $derived(
(() => {
const lat = asset.exifInfo?.latitude;
@@ -173,12 +171,12 @@
{#if people.some((person) => person.isHidden)}
<IconButton
aria-label={$t('show_hidden_people')}
- icon={showingHiddenPeople ? mdiEyeOff : mdiEye}
+ icon={assetViewerManager.isShowingHiddenPeople ? mdiEyeOff : mdiEye}
size="medium"
shape="round"
color="secondary"
variant="ghost"
- onclick={() => (showingHiddenPeople = !showingHiddenPeople)}
+ onclick={() => assetViewerManager.toggleHiddenPeople()}
/>
{/if}
<IconButton
@@ -207,15 +205,17 @@
<div class="mt-2 flex flex-wrap gap-2">
{#each people as person, index (person.id)}
- {#if showingHiddenPeople || !person.isHidden}
- {@const isHighlighted = people[index].faces.some((f) => $boundingBoxesArray.some((b) => b.id === f.id))}
+ {#if assetViewerManager.isShowingHiddenPeople || !person.isHidden}
+ {@const isHighlighted = people[index].faces.some((f) =>
+ assetViewerManager.highlightedFaces.some((b) => b.id === f.id),
+ )}
<a
class="group w-22 outline-none"
href={Route.viewPerson(person, { previousRoute })}
- onfocus={() => ($boundingBoxesArray = people[index].faces)}
- onblur={() => ($boundingBoxesArray = [])}
- onmouseover={() => ($boundingBoxesArray = people[index].faces)}
- onmouseleave={() => ($boundingBoxesArray = [])}
+ onfocus={() => assetViewerManager.setHighlightedFaces(people[index].faces)}
+ onblur={() => assetViewerManager.clearHighlightedFaces()}
+ onpointerenter={() => assetViewerManager.setHighlightedFaces(people[index].faces)}
+ onpointerleave={() => assetViewerManager.clearHighlightedFaces()}
>
<div class="relative">
<ImageThumbnail
diff --git a/web/src/lib/components/asset-viewer/PhotoSphereViewerAdapter.svelte b/web/src/lib/components/asset-viewer/PhotoSphereViewerAdapter.svelte
index 12c4b45541..150b6e4f6b 100644
--- a/web/src/lib/components/asset-viewer/PhotoSphereViewerAdapter.svelte
+++ b/web/src/lib/components/asset-viewer/PhotoSphereViewerAdapter.svelte
@@ -1,9 +1,8 @@
<script lang="ts">
import { shortcuts } from '$lib/actions/shortcut';
import AssetViewerEvents from '$lib/components/AssetViewerEvents.svelte';
- import { assetViewerManager } from '$lib/managers/asset-viewer-manager.svelte';
+ import { assetViewerManager, type Faces } from '$lib/managers/asset-viewer-manager.svelte';
import { ocrManager, type OcrBoundingBox } from '$lib/stores/ocr.svelte';
- import { boundingBoxesArray, type Faces } from '$lib/stores/people.store';
import { alwaysLoadOriginalFile } from '$lib/stores/preferences.store';
import { calculateBoundingBoxMatrix, getOcrBoundingBoxes, type Point } from '$lib/utils/ocr-utils';
import {
@@ -55,14 +54,9 @@
let viewer: Viewer;
let animationInProgress: { cancel: () => void } | undefined;
- let previousFaces: Faces[] = [];
- const boundingBoxesUnsubscribe = boundingBoxesArray.subscribe((faces: Faces[]) => {
- // Debounce; don't do anything when the data didn't actually change.
- if (faces === previousFaces) {
- return;
- }
- previousFaces = faces;
+ $effect(() => {
+ const faces: Faces[] = assetViewerManager.highlightedFaces;
if (animationInProgress) {
animationInProgress.cancel();
@@ -105,7 +99,7 @@
textureX: x,
textureY: y,
zoom: Math.min(viewer.getZoomLevel(), 75),
- speed: 500, // duration in ms
+ speed: 500,
});
}
});
@@ -247,7 +241,8 @@
if (viewer) {
viewer.destroy();
}
- boundingBoxesUnsubscribe();
+ assetViewerManager.clearHighlightedFaces();
+ assetViewerManager.hideHiddenPeople();
assetViewerManager.zoom = 1;
});
</script>
diff --git a/web/src/lib/components/asset-viewer/PhotoViewer.svelte b/web/src/lib/components/asset-viewer/PhotoViewer.svelte
index cf8ee876e4..894ee875e3 100644
--- a/web/src/lib/components/asset-viewer/PhotoViewer.svelte
+++ b/web/src/lib/components/asset-viewer/PhotoViewer.svelte
@@ -6,10 +6,9 @@
import Thumbhash from '$lib/components/Thumbhash.svelte';
import OcrBoundingBox from '$lib/components/asset-viewer/OcrBoundingBox.svelte';
import AssetViewerEvents from '$lib/components/AssetViewerEvents.svelte';
- import { assetViewerManager } from '$lib/managers/asset-viewer-manager.svelte';
+ import { assetViewerManager, type Faces } from '$lib/managers/asset-viewer-manager.svelte';
import { castManager } from '$lib/managers/cast-manager.svelte';
import { ocrManager } from '$lib/stores/ocr.svelte';
- import { boundingBoxesArray, type Faces } from '$lib/stores/people.store';
import { SlideshowLook, SlideshowState, slideshowStore } from '$lib/stores/slideshow.store';
import { handlePromiseError } from '$lib/utils';
import { canCopyImageToClipboard, copyImageToClipboard } from '$lib/utils/asset-utils';
@@ -50,12 +49,13 @@
untrack(() => {
assetViewerManager.resetZoomState();
visibleImageReady = false;
- $boundingBoxesArray = [];
+ assetViewerManager.clearHighlightedFaces();
});
});
onDestroy(() => {
- $boundingBoxesArray = [];
+ assetViewerManager.clearHighlightedFaces();
+ assetViewerManager.hideHiddenPeople();
});
let containerWidth = $state(0);
@@ -74,15 +74,13 @@
return scaleToFit(getNaturalSize(assetViewerManager.imgRef), { width: containerWidth, height: containerHeight });
});
- const highlightedBoxes = $derived(getBoundingBox($boundingBoxesArray, overlaySize));
+ const highlightedBoxes = $derived(getBoundingBox(assetViewerManager.highlightedFaces, overlaySize));
const isHighlighting = $derived(highlightedBoxes.length > 0);
let visibleBoxes = $state<BoundingBox[]>([]);
- let visibleBoundingBoxes = $state<Faces[]>([]);
$effect(() => {
if (isHighlighting) {
visibleBoxes = highlightedBoxes;
- visibleBoundingBoxes = $boundingBoxesArray;
}
});
@@ -160,6 +158,9 @@
// eslint-disable-next-line svelte/prefer-svelte-reactivity
const map = new Map<Faces, string>();
for (const person of asset.people ?? []) {
+ if (person.isHidden && !assetViewerManager.isShowingHiddenPeople) {
+ continue;
+ }
for (const face of person.faces ?? []) {
map.set(face, person.name);
}
@@ -169,35 +170,31 @@
const faces = $derived(Array.from(faceToNameMap.keys()));
- const handleImageMouseMove = (event: MouseEvent) => {
- $boundingBoxesArray = [];
- if (!assetViewerManager.imgRef || !element || assetViewerManager.isFaceEditMode || ocrManager.showOverlay) {
- return;
+ const boundingBoxes = $derived.by(() => {
+ if (assetViewerManager.isFaceEditMode || ocrManager.showOverlay) {
+ return [];
}
- const natural = getNaturalSize(assetViewerManager.imgRef);
- const scaled = scaleToFit(natural, container);
- const { currentZoom, currentPositionX, currentPositionY } = assetViewerManager.zoomState;
-
- const contentOffsetX = (container.width - scaled.width) / 2;
- const contentOffsetY = (container.height - scaled.height) / 2;
+ const knownBoxes = getBoundingBox(faces, overlaySize);
+ const result = knownBoxes.map((box, index) => ({
+ ...box,
+ face: faces[index],
+ name: faceToNameMap.get(faces[index]),
+ }));
- const containerRect = element.getBoundingClientRect();
- const mouseX = (event.clientX - containerRect.left - contentOffsetX * currentZoom - currentPositionX) / currentZoom;
- const mouseY = (event.clientY - containerRect.top - contentOffsetY * currentZoom - currentPositionY) / currentZoom;
-
- const faceBoxes = getBoundingBox(faces, overlaySize);
+ if (assetViewerManager.highlightedFaces.length === 0) {
+ return result;
+ }
- for (const [index, box] of faceBoxes.entries()) {
- if (mouseX >= box.left && mouseX <= box.left + box.width && mouseY >= box.top && mouseY <= box.top + box.height) {
- $boundingBoxesArray.push(faces[index]);
- }
+ const knownIds = new Set(faces.map((f) => f.id));
+ const unassignedFaces = assetViewerManager.highlightedFaces.filter((f) => !knownIds.has(f.id));
+ const unassignedBoxes = getBoundingBox(unassignedFaces, overlaySize);
+ for (let i = 0; i < unassignedBoxes.length; i++) {
+ result.push({ ...unassignedBoxes[i], face: unassignedFaces[i], name: undefined });
}
- };
- const handleImageMouseLeave = () => {
- $boundingBoxesArray = [];
- };
+ return result;
+ });
</script>
<AssetViewerEvents {onCopy} {onZoom} {onFaceEditModeChange} />
@@ -218,8 +215,6 @@
bind:clientHeight={containerHeight}
role="presentation"
ondblclick={onZoom}
- onmousemove={handleImageMouseMove}
- onmouseleave={handleImageMouseLeave}
use:zoomImageAction={{ zoomTarget: adaptiveImage }}
{...useSwipe((event) => onSwipe?.(event))}
>
@@ -261,22 +256,27 @@
</defs>
<rect width="100%" height="100%" fill="rgba(0,0,0,0.4)" mask="url(#face-dim-mask)" />
</svg>
- {#each visibleBoxes as boundingbox, index (boundingbox.id)}
- <div
- class="absolute border-solid border-white border-3 rounded-lg"
- style="top: {boundingbox.top}px; left: {boundingbox.left}px; height: {boundingbox.height}px; width: {boundingbox.width}px;"
- ></div>
- {#if faceToNameMap.get(visibleBoundingBoxes[index])}
+ </div>
+ {#each boundingBoxes as boundingbox (boundingbox.id)}
+ {@const isActive = assetViewerManager.highlightedFaces.some((f) => f.id === boundingbox.id)}
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
+ <div
+ class="absolute pointer-events-auto rounded-lg {isActive && 'border-solid border-white border-3'}"
+ style="top: {boundingbox.top}px; left: {boundingbox.left}px; height: {boundingbox.height}px; width: {boundingbox.width}px;"
+ onpointerenter={() => assetViewerManager.setHighlightedFaces([boundingbox.face])}
+ onpointerleave={() => assetViewerManager.clearHighlightedFaces()}
+ >
+ {#if isActive && boundingbox.name}
<div
- class="absolute bg-white/90 text-black px-2 py-1 rounded text-sm font-medium whitespace-nowrap pointer-events-none shadow-lg"
- style="top: {boundingbox.top + boundingbox.height + 4}px; left: {boundingbox.left +
- boundingbox.width}px; transform: translateX(-100%);"
+ aria-hidden="true"
+ class="absolute bg-white/90 text-black px-2 py-1 rounded text-sm font-medium whitespace-nowrap shadow-lg"
+ style="top: {boundingbox.height + 4}px; right: 0;"
>
- {faceToNameMap.get(visibleBoundingBoxes[index])}
+ {boundingbox.name}
</div>
{/if}
- {/each}
- </div>
+ </div>
+ {/each}
{#each ocrBoxes as ocrBox (ocrBox.id)}
<OcrBoundingBox {ocrBox} />
diff --git a/web/src/lib/components/faces-page/PersonSidePanel.svelte b/web/src/lib/components/faces-page/PersonSidePanel.svelte
index c8f5d1429c..657be179c6 100644
--- a/web/src/lib/components/faces-page/PersonSidePanel.svelte
+++ b/web/src/lib/components/faces-page/PersonSidePanel.svelte
@@ -4,7 +4,6 @@
import { timeBeforeShowLoadingSpinner } from '$lib/constants';
import { assetViewerManager } from '$lib/managers/asset-viewer-manager.svelte';
import { eventManager } from '$lib/managers/event-manager.svelte';
- import { boundingBoxesArray } from '$lib/stores/people.store';
import { getPeopleThumbnailUrl, handlePromiseError } from '$lib/utils';
import { handleError } from '$lib/utils/handle-error';
import { zoomImageToBase64 } from '$lib/utils/people-utils';
@@ -239,15 +238,15 @@
{:else}
{#each peopleWithFaces as face, index (face.id)}
{@const personName = face.person ? face.person?.name : $t('face_unassigned')}
- {@const isHighlighted = $boundingBoxesArray.some((b) => b.id === face.id)}
+ {@const isHighlighted = assetViewerManager.highlightedFaces.some((b) => b.id === face.id)}
<div class="relative h-29 w-24">
<div
role="button"
tabindex={index}
class="absolute start-0 top-0 h-22.5 w-22.5 cursor-default"
- onfocus={() => ($boundingBoxesArray = [peopleWithFaces[index]])}
- onmouseover={() => ($boundingBoxesArray = [peopleWithFaces[index]])}
- onmouseleave={() => ($boundingBoxesArray = [])}
+ onfocus={() => assetViewerManager.setHighlightedFaces([peopleWithFaces[index]])}
+ onpointerenter={() => assetViewerManager.setHighlightedFaces([peopleWithFaces[index]])}
+ onpointerleave={() => assetViewerManager.clearHighlightedFaces()}
>
<div class="relative">
{#if selectedPersonToCreate[face.id]}
diff --git a/web/src/lib/managers/asset-viewer-manager.svelte.ts b/web/src/lib/managers/asset-viewer-manager.svelte.ts
index 4e8ac0060e..55c8570b78 100644
--- a/web/src/lib/managers/asset-viewer-manager.svelte.ts
+++ b/web/src/lib/managers/asset-viewer-manager.svelte.ts
@@ -8,6 +8,16 @@ import { BaseEventManager } from '$lib/utils/base-event-manager.svelte';
import type { AssetGridRouteSearchParams } from '$lib/utils/navigation';
import { PersistedLocalStorage } from '$lib/utils/persisted';
+export interface Faces {
+ id: string;
+ imageHeight: number;
+ imageWidth: number;
+ boundingBoxX1: number;
+ boundingBoxX2: number;
+ boundingBoxY1: number;
+ boundingBoxY2: number;
+}
+
const isShowDetailPanel = new PersistedLocalStorage<boolean>('asset-viewer-state', false);
const isShowAssetPath = new PersistedLocalStorage<boolean>('asset-viewer-show-path', false);
@@ -48,6 +58,8 @@ class AssetViewerManager extends BaseEventManager<Events> {
#isEditFacesPanelOpen = $state(false);
#viewingAssetStoreState = $state<AssetResponseDto>();
#viewState = $state<boolean>(false);
+ #highlightedFaces = $state<Faces[]>([]);
+ #showingHiddenPeople = $state(false);
gridScrollTarget = $state<AssetGridRouteSearchParams | null | undefined>();
get asset() {
@@ -209,6 +221,31 @@ class AssetViewerManager extends BaseEventManager<Events> {
this.closeFaceEditMode();
this.closeEditFacesPanel();
}
+
+ get highlightedFaces() {
+ return this.#highlightedFaces;
+ }
+
+ setHighlightedFaces(faces: Faces[]) {
+ this.#highlightedFaces = faces;
+ }
+
+ clearHighlightedFaces() {
+ this.#highlightedFaces = [];
+ }
+
+ get isShowingHiddenPeople() {
+ return this.#showingHiddenPeople;
+ }
+
+ toggleHiddenPeople() {
+ this.#showingHiddenPeople = !this.#showingHiddenPeople;
+ }
+
+ hideHiddenPeople() {
+ this.#showingHiddenPeople = false;
+ }
+
setAsset(asset: AssetResponseDto) {
this.#viewingAssetStoreState = asset;
this.#viewState = true;
diff --git a/web/src/lib/stores/people.store.ts b/web/src/lib/stores/people.store.ts
deleted file mode 100644
index 34e927cf36..0000000000
--- a/web/src/lib/stores/people.store.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { writable } from 'svelte/store';
-
-export interface Faces {
- id: string;
- imageHeight: number;
- imageWidth: number;
- boundingBoxX1: number;
- boundingBoxX2: number;
- boundingBoxY1: number;
- boundingBoxY2: number;
-}
-
-export const boundingBoxesArray = writable<Faces[]>([]);
diff --git a/web/src/lib/utils/people-utils.spec.ts b/web/src/lib/utils/people-utils.spec.ts
index f27a1855b5..0601f5022a 100644
--- a/web/src/lib/utils/people-utils.spec.ts
+++ b/web/src/lib/utils/people-utils.spec.ts
@@ -1,4 +1,4 @@
-import type { Faces } from '$lib/stores/people.store';
+import type { Faces } from '$lib/managers/asset-viewer-manager.svelte';
import type { Size } from '$lib/utils/container-utils';
import { getBoundingBox } from '$lib/utils/people-utils';
diff --git a/web/src/lib/utils/people-utils.ts b/web/src/lib/utils/people-utils.ts
index 67c4329736..9d7b8be799 100644
--- a/web/src/lib/utils/people-utils.ts
+++ b/web/src/lib/utils/people-utils.ts
@@ -1,5 +1,5 @@
import { AssetTypeEnum, type AssetFaceResponseDto } from '@immich/sdk';
-import type { Faces } from '$lib/stores/people.store';
+import type { Faces } from '$lib/managers/asset-viewer-manager.svelte';
import { getAssetMediaUrl } from '$lib/utils';
import { mapNormalizedRectToContent, type Rect, type Size } from '$lib/utils/container-utils';