Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

feat(photos):add-slideshow-settings-and-auto-hide-controls

Aswin Asok1b41ed65ebda
diff --git a/.github/assets/slideshow-settings-desktop.png b/.github/assets/slideshow-settings-desktop.png
new file mode 100644
index 0000000000..a74501448b
Binary files /dev/null and b/.github/assets/slideshow-settings-desktop.png differ
diff --git a/.github/assets/slideshow-settings-mobile.png b/.github/assets/slideshow-settings-mobile.png
new file mode 100644
index 0000000000..9c504a9dfe
Binary files /dev/null and b/.github/assets/slideshow-settings-mobile.png differ
diff --git a/web/apps/photos/src/components/Collections/AlbumSlideshow.tsx b/web/apps/photos/src/components/Collections/AlbumSlideshow.tsx
index d778c1d039..a2c0e4d6c8 100644
--- a/web/apps/photos/src/components/Collections/AlbumSlideshow.tsx
+++ b/web/apps/photos/src/components/Collections/AlbumSlideshow.tsx
@@ -3,6 +3,7 @@ import NavigateBeforeIcon from "@mui/icons-material/NavigateBefore";
import NavigateNextIcon from "@mui/icons-material/NavigateNext";
import PauseIcon from "@mui/icons-material/Pause";
import PlayArrowIcon from "@mui/icons-material/PlayArrow";
+import SettingsIcon from "@mui/icons-material/Settings";
import {
Box,
CircularProgress,
@@ -17,15 +18,24 @@ import { downloadManager } from "ente-gallery/services/download";
import type { EnteFile } from "ente-media/file";
import { fileFileName } from "ente-media/file-metadata";
import { usePhotosAppContext } from "ente-new/photos/types/context";
+import { shuffled } from "ente-utils/array";
import { t } from "i18next";
import {
useCallback,
useEffect,
+ useRef,
useState,
type KeyboardEvent,
type MouseEvent,
} from "react";
-import { scheduleSlideshowAdvance, slideshowIndex } from "./album-slideshow";
+import {
+ savedSlideshowSettings,
+ saveSlideshowSettings,
+ scheduleSlideshowAdvance,
+ slideshowIndex,
+ type SlideshowSettings,
+} from "./album-slideshow";
+import { AlbumSlideshowSettings } from "./AlbumSlideshowSettings";
interface AlbumSlideshowProps {
files: EnteFile[];
@@ -34,11 +44,34 @@ interface AlbumSlideshowProps {
}
export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
+ const [settings, setSettings] = useState(savedSlideshowSettings);
+ const [orderedFiles, setOrderedFiles] = useState(() =>
+ settings.randomOrder ? shuffled(files) : files,
+ );
+ const [settingsOpen, setSettingsOpen] = useState(false);
const [slide, setSlide] = useState({ index: 0, ready: false });
const [playing, setPlaying] = useState(true);
const [visible, setVisible] = useState(() => !document.hidden);
+ const [controlsVisible, setControlsVisible] = useState(true);
+ const controlsHideTimer = useRef<ReturnType<typeof setTimeout>>(undefined);
const { setIsFileViewerOpen } = usePhotosAppContext();
- const current = files[slide.index]!;
+ const current = orderedFiles[slide.index]!;
+
+ const showControls = useCallback(() => {
+ setControlsVisible(true);
+ clearTimeout(controlsHideTimer.current);
+ if (playing && visible && !settingsOpen) {
+ controlsHideTimer.current = setTimeout(
+ () => setControlsVisible(false),
+ 2000,
+ );
+ }
+ }, [playing, visible, settingsOpen]);
+
+ useEffect(() => {
+ showControls();
+ return () => clearTimeout(controlsHideTimer.current);
+ }, [showControls]);
const navigate = useCallback(
(offset: number) => {
@@ -59,25 +92,35 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
const markReady = useCallback(
(id: number) => {
setSlide((previous) =>
- files[previous.index]?.id === id && !previous.ready
+ orderedFiles[previous.index]?.id === id && !previous.ready
? { ...previous, ready: true }
: previous,
);
},
- [files],
+ [orderedFiles],
);
useEffect(
() =>
scheduleSlideshowAdvance(
{
- enabled: playing && visible,
+ enabled: playing && visible && !settingsOpen,
ready: slide.ready,
count: files.length,
+ durationSeconds: settings.durationSeconds,
},
() => navigate(1),
),
- [playing, visible, slide.ready, files.length, current.id, navigate],
+ [
+ playing,
+ visible,
+ settingsOpen,
+ settings.durationSeconds,
+ slide.ready,
+ files.length,
+ current.id,
+ navigate,
+ ],
);
useEffect(() => {
@@ -92,6 +135,8 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
}, [setIsFileViewerOpen]);
const handleKeyDown = (event: KeyboardEvent) => {
+ if (settingsOpen) return;
+ showControls();
if (
(event.target as HTMLElement).closest("button") &&
event.key === " "
@@ -121,6 +166,22 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
else if (x > 0.75) navigate(1);
};
+ const openSettings = () => setSettingsOpen(true);
+ const closeSettings = () => setSettingsOpen(false);
+
+ const handleSettingsChange = (next: SlideshowSettings) => {
+ if (next.randomOrder !== settings.randomOrder) {
+ const reordered = next.randomOrder ? shuffled(files) : files;
+ setOrderedFiles(reordered);
+ setSlide((previous) => ({
+ ...previous,
+ index: reordered.findIndex((file) => file.id === current.id),
+ }));
+ }
+ setSettings(next);
+ saveSlideshowSettings(next);
+ };
+
return (
<Dialog
open
@@ -128,18 +189,39 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
onClose={onClose}
aria-labelledby="album-slideshow-title"
onKeyDown={handleKeyDown}
+ onPointerMove={showControls}
+ onPointerDown={showControls}
+ onFocusCapture={showControls}
slotProps={{
paper: {
- sx: { bgcolor: "#000", color: "#fff", overflow: "hidden" },
+ sx: {
+ bgcolor: "#000",
+ color: "#fff",
+ overflow: "hidden",
+ "& .slideshow-controls": {
+ opacity: controlsVisible ? 1 : 0,
+ transition: "opacity 200ms",
+ },
+ "&:has(button:focus-visible) .slideshow-controls": {
+ opacity: 1,
+ "& button": { pointerEvents: "auto" },
+ },
+ },
},
}}
>
- <Slide key={current.id} file={current} onReady={markReady} />
+ <Slide
+ key={current.id}
+ file={current}
+ onReady={markReady}
+ blurredBackground={settings.blurredBackground}
+ />
<Box
onClick={handlePhotoClick}
sx={{ position: "absolute", inset: 0 }}
/>
<Stack
+ className="slideshow-controls"
direction="row"
sx={{
position: "relative",
@@ -147,6 +229,7 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
p: 2,
gap: 2,
background: "linear-gradient(#000b, transparent)",
+ pointerEvents: controlsVisible ? "auto" : "none",
}}
>
<IconButton
@@ -156,12 +239,21 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
>
<CloseIcon />
</IconButton>
- <Typography id="album-slideshow-title" noWrap>
+ <Typography id="album-slideshow-title" noWrap sx={{ flex: 1 }}>
{title}
</Typography>
+ <IconButton
+ aria-label={t("slideshow_settings")}
+ aria-haspopup="dialog"
+ onClick={openSettings}
+ color="inherit"
+ >
+ <SettingsIcon />
+ </IconButton>
</Stack>
{files.length > 1 && (
<Stack
+ className="slideshow-controls"
direction="row"
sx={{
position: "absolute",
@@ -172,7 +264,10 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
justifyContent: "space-between",
px: 2,
pointerEvents: "none",
- "& button": { pointerEvents: "auto", color: "inherit" },
+ "& button": {
+ pointerEvents: controlsVisible ? "auto" : "none",
+ color: "inherit",
+ },
}}
>
<IconButton
@@ -196,6 +291,12 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
</IconButton>
</Stack>
)}
+ <AlbumSlideshowSettings
+ open={settingsOpen}
+ onClose={closeSettings}
+ settings={settings}
+ onChange={handleSettingsChange}
+ />
</Dialog>
);
}
@@ -203,9 +304,11 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
function Slide({
file,
onReady,
+ blurredBackground,
}: {
file: EnteFile;
onReady: (id: number) => void;
+ blurredBackground: boolean;
}) {
const [thumbnail, setThumbnail] = useState<string>();
const [original, setOriginal] = useState<string>();
@@ -254,18 +357,20 @@ function Slide({
return (
<>
- <Box
- aria-hidden
- sx={{
- position: "absolute",
- inset: -100,
- backgroundImage: `url("${thumbnail ?? original ?? ""}")`,
- backgroundSize: "cover",
- backgroundPosition: "center",
- filter: "blur(100px)",
- opacity: 0.6,
- }}
- />
+ {blurredBackground && (
+ <Box
+ aria-hidden
+ sx={{
+ position: "absolute",
+ inset: -100,
+ backgroundImage: `url("${thumbnail ?? original ?? ""}")`,
+ backgroundSize: "cover",
+ backgroundPosition: "center",
+ filter: "blur(100px)",
+ opacity: 0.6,
+ }}
+ />
+ )}
{thumbnail && (
<Box
component="img"
diff --git a/web/apps/photos/src/components/Collections/AlbumSlideshowSettings.tsx b/web/apps/photos/src/components/Collections/AlbumSlideshowSettings.tsx
new file mode 100644
index 0000000000..43f8fcab2a
--- /dev/null
+++ b/web/apps/photos/src/components/Collections/AlbumSlideshowSettings.tsx
@@ -0,0 +1,208 @@
+import CheckIcon from "@mui/icons-material/Check";
+import CloseIcon from "@mui/icons-material/Close";
+import {
+ Box,
+ ButtonBase,
+ Dialog,
+ IconButton,
+ Stack,
+ Typography,
+} from "@mui/material";
+import { t } from "i18next";
+import type { PropsWithChildren } from "react";
+import {
+ slideshowDurationOptions,
+ type SlideshowSettings,
+} from "./album-slideshow";
+
+interface AlbumSlideshowSettingsProps {
+ open: boolean;
+ onClose: () => void;
+ settings: SlideshowSettings;
+ onChange: (settings: SlideshowSettings) => void;
+}
+
+export function AlbumSlideshowSettings({
+ open,
+ onClose,
+ settings,
+ onChange,
+}: AlbumSlideshowSettingsProps) {
+ return (
+ <Dialog
+ open={open}
+ onClose={onClose}
+ aria-labelledby="slideshow-settings-title"
+ maxWidth={false}
+ sx={{ "& .MuiBackdrop-root": { bgcolor: "#0008" } }}
+ slotProps={{
+ container: {
+ sx: { alignItems: { xs: "flex-end", sm: "center" } },
+ },
+ paper: {
+ sx: {
+ m: { xs: 0, sm: "20px" },
+ p: "20px",
+ pb: "max(20px, env(safe-area-inset-bottom))",
+ width: { xs: "100%", sm: 440 },
+ maxWidth: { xs: "100%", sm: "calc(100% - 40px)" },
+ maxHeight: "80dvh",
+ borderRadius: { xs: "20px 20px 0 0", sm: "24px" },
+ bgcolor: "#161616",
+ color: "#fff",
+ backgroundImage: "none",
+ },
+ },
+ }}
+ >
+ <Stack
+ direction="row"
+ sx={{ alignItems: "center", gap: "12px", mb: "16px" }}
+ >
+ <Typography
+ id="slideshow-settings-title"
+ component="h2"
+ sx={{
+ flex: 1,
+ fontSize: 18,
+ lineHeight: "24px",
+ fontWeight: 600,
+ }}
+ >
+ {t("slideshow_settings")}
+ </Typography>
+ <IconButton
+ aria-label={t("close")}
+ onClick={onClose}
+ color="inherit"
+ sx={{ width: 38, height: 38, bgcolor: "#212121" }}
+ >
+ <CloseIcon sx={{ fontSize: 20 }} />
+ </IconButton>
+ </Stack>
+ <Stack sx={{ gap: "20px" }}>
+ <SettingsOptions title={t("time_per_photo")}>
+ {slideshowDurationOptions.map((seconds) => (
+ <SettingsChip
+ key={seconds}
+ label={
+ seconds < 60
+ ? t("seconds_count_short", {
+ count: seconds,
+ })
+ : t("minutes_count_short", {
+ count: seconds / 60,
+ })
+ }
+ selected={settings.durationSeconds === seconds}
+ onClick={() =>
+ onChange({
+ ...settings,
+ durationSeconds: seconds,
+ })
+ }
+ />
+ ))}
+ </SettingsOptions>
+ <SettingsOptions title={t("photo_order")}>
+ <SettingsChip
+ label={t("in_order")}
+ selected={!settings.randomOrder}
+ onClick={() =>
+ onChange({ ...settings, randomOrder: false })
+ }
+ />
+ <SettingsChip
+ label={t("shuffle")}
+ selected={settings.randomOrder}
+ onClick={() =>
+ onChange({ ...settings, randomOrder: true })
+ }
+ />
+ </SettingsOptions>
+ <SettingsOptions title={t("background")}>
+ <SettingsChip
+ label={t("blurred")}
+ selected={settings.blurredBackground}
+ onClick={() =>
+ onChange({ ...settings, blurredBackground: true })
+ }
+ />
+ <SettingsChip
+ label={t("black")}
+ selected={!settings.blurredBackground}
+ onClick={() =>
+ onChange({ ...settings, blurredBackground: false })
+ }
+ />
+ </SettingsOptions>
+ </Stack>
+ </Dialog>
+ );
+}
+
+function SettingsOptions({
+ title,
+ children,
+}: PropsWithChildren<{ title: string }>) {
+ return (
+ <Box component="fieldset" sx={{ m: 0, p: 0, border: 0, minWidth: 0 }}>
+ <Typography
+ component="legend"
+ sx={{
+ mb: "8px",
+ p: 0,
+ fontSize: 14,
+ lineHeight: "20px",
+ fontWeight: 600,
+ }}
+ >
+ {title}
+ </Typography>
+ <Stack
+ direction="row"
+ useFlexGap
+ sx={{ flexWrap: "wrap", gap: "12px 8px" }}
+ >
+ {children}
+ </Stack>
+ </Box>
+ );
+}
+
+function SettingsChip({
+ label,
+ selected,
+ onClick,
+}: {
+ label: string;
+ selected: boolean;
+ onClick: () => void;
+}) {
+ return (
+ <ButtonBase
+ aria-pressed={selected}
+ onClick={selected ? undefined : onClick}
+ sx={{
+ minHeight: 40,
+ minWidth: 40,
+ p: selected ? "12px 12px 12px 16px" : "12px 18px",
+ gap: "8px",
+ borderRadius: "20px",
+ bgcolor: selected ? "#f4f4f4" : "#212121",
+ color: selected ? "#000" : "#999",
+ fontSize: 12,
+ lineHeight: "16px",
+ fontWeight: 500,
+ "&:hover": { bgcolor: selected ? "#fff" : "#2a2a2a" },
+ "&.Mui-focusVisible": {
+ outline: "2px solid currentColor",
+ outlineOffset: 3,
+ },
+ }}
+ >
+ {label}
+ {selected && <CheckIcon sx={{ fontSize: 18 }} />}
+ </ButtonBase>
+ );
+}
diff --git a/web/apps/photos/src/components/Collections/album-slideshow.ts b/web/apps/photos/src/components/Collections/album-slideshow.ts
index 30ff9c2130..5e89c522ce 100644
--- a/web/apps/photos/src/components/Collections/album-slideshow.ts
+++ b/web/apps/photos/src/components/Collections/album-slideshow.ts
@@ -2,6 +2,45 @@ import { uniqueFilesByID } from "ente-gallery/utils/file";
import type { EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
+export const slideshowDurationOptions = [5, 10, 15, 30, 60, 300, 600];
+
+export interface SlideshowSettings {
+ durationSeconds: number;
+ randomOrder: boolean;
+ blurredBackground: boolean;
+}
+
+export const savedSlideshowSettings = (): SlideshowSettings => {
+ const durationSeconds = Number(
+ localStorage.getItem("album_slideshow.duration_seconds"),
+ );
+ return {
+ durationSeconds: slideshowDurationOptions.includes(durationSeconds)
+ ? durationSeconds
+ : 5,
+ randomOrder:
+ localStorage.getItem("album_slideshow.random_order") === "true",
+ blurredBackground:
+ localStorage.getItem("album_slideshow.blurred_background") !==
+ "false",
+ };
+};
+
+export const saveSlideshowSettings = (settings: SlideshowSettings) => {
+ localStorage.setItem(
+ "album_slideshow.duration_seconds",
+ String(settings.durationSeconds),
+ );
+ localStorage.setItem(
+ "album_slideshow.random_order",
+ String(settings.randomOrder),
+ );
+ localStorage.setItem(
+ "album_slideshow.blurred_background",
+ String(settings.blurredBackground),
+ );
+};
+
export const slideshowFiles = (files: EnteFile[]) =>
uniqueFilesByID(
files.filter(
@@ -18,12 +57,18 @@ export function scheduleSlideshowAdvance(
enabled,
ready,
count,
- }: { enabled: boolean; ready: boolean; count: number },
+ durationSeconds = 5,
+ }: {
+ enabled: boolean;
+ ready: boolean;
+ count: number;
+ durationSeconds?: number;
+ },
advance: () => void,
) {
const timer =
enabled && count > 1
- ? setTimeout(advance, ready ? 5000 : 10_000)
+ ? setTimeout(advance, ready ? durationSeconds * 1000 : 10_000)
: undefined;
return () => clearTimeout(timer);
}
diff --git a/web/apps/photos/tests/album-slideshow.test.ts b/web/apps/photos/tests/album-slideshow.test.ts
index ed8900442c..6ae91417af 100644
--- a/web/apps/photos/tests/album-slideshow.test.ts
+++ b/web/apps/photos/tests/album-slideshow.test.ts
@@ -36,23 +36,26 @@ test("navigation loops in both directions and handles empty and single-photo alb
describe("playback timeout", () => {
const ready = { enabled: true, ready: true, count: 2 };
- test("advances once after five seconds", () => {
- vi.useFakeTimers();
- const advance = vi.fn();
- scheduleSlideshowAdvance(ready, advance);
- vi.advanceTimersByTime(4999);
- expect(advance).not.toHaveBeenCalled();
- vi.advanceTimersByTime(1);
- expect(advance).toHaveBeenCalledTimes(1);
- vi.advanceTimersByTime(60_000);
- expect(advance).toHaveBeenCalledTimes(1);
- });
+ test.each([5, 15, 600])(
+ "advances once after the selected %i seconds",
+ (durationSeconds) => {
+ vi.useFakeTimers();
+ const advance = vi.fn();
+ scheduleSlideshowAdvance({ ...ready, durationSeconds }, advance);
+ vi.advanceTimersByTime(durationSeconds * 1000 - 1);
+ expect(advance).not.toHaveBeenCalled();
+ vi.advanceTimersByTime(1);
+ expect(advance).toHaveBeenCalledTimes(1);
+ vi.advanceTimersByTime(60_000);
+ expect(advance).toHaveBeenCalledTimes(1);
+ },
+ );
test("skips undisplayable media after ten seconds, or replaces that timeout once ready", () => {
vi.useFakeTimers();
const advance = vi.fn();
const cancel = scheduleSlideshowAdvance(
- { ...ready, ready: false },
+ { ...ready, ready: false, durationSeconds: 600 },
advance,
);
vi.advanceTimersByTime(9999);
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index b99211fde4..0ea12e56da 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -479,6 +479,16 @@
"file_type": "File type",
"settings": "Settings",
"slideshow": "Slideshow",
+ "slideshow_settings": "Slideshow settings",
+ "time_per_photo": "Time per photo",
+ "seconds_count_short": "{{count}}s",
+ "minutes_count_short": "{{count}}m",
+ "photo_order": "Photo order",
+ "in_order": "In order",
+ "shuffle": "Shuffle",
+ "background": "Background",
+ "blurred": "Blurred",
+ "black": "Black",
"magic": "Magic",
"photos_count_zero": "No memories",
"photos_count_one": "1 memory",