Skip to content

Build your personal knowledge base with Trilium Notes

client/media player/preview-embed renderer: use a dedicated layout for the video player (compact layout)

Adorian Doranb755b092f357
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPlayer.css b/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
index 8324e5cb70..411ff4e900 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
+++ b/apps/client/src/widgets/type_widgets/file/MediaPlayer.css
@@ -101,28 +101,10 @@
flex-direction: column;
}
-/* The compact audio player, worn everywhere but the note detail: a full-height visualizer band above a single
- play / seek / volume row. A collection tile or an included note has no room for the speed, skip and
- play-mode controls. */
-.audio-preview-wrapper.media-compact {
- .audio-preview-visualization-wrapper {
- /* The icon this player drops was the band's only in-flow child (the canvas is absolutely positioned),
- so without a floor the band collapses to nothing wherever the host is content-sized — a list-view
- row, as opposed to a fixed-height grid card. It still grows to fill a taller host. */
- min-height: 8rem;
- }
-
- .audio-visualizer-canvas {
- /* No icon to sit behind, so the grid takes the whole band rather than its bottom half, inset by 12px
- from the top and sides. The canvas is absolutely positioned, so padding on the wrapper wouldn't move
- it — and its size has to be stated outright: it is a replaced element, for which `auto` means "use
- the intrinsic size" rather than "stretch between the offsets". It stays anchored to the bottom
- (`bottom: 0`), where the bars grow from. */
- left: 12px;
- width: calc(100% - 24px);
- height: calc(100% - 12px);
- }
-
+/* The compact chrome, worn by audio and video alike everywhere but the note detail: the whole control set
+ collapses to a single play / seek / volume row. A collection tile or an included note has no room for the
+ speed, skip, rotate and play-mode controls. */
+.media-compact {
.media-preview-controls {
padding: 0.5em;
}
@@ -136,13 +118,46 @@
--icon-button-size: 36px;
}
+ /* The seek bar (plain for video, waveform for audio) takes whatever the buttons leave. */
.media-seekbar-row {
flex: 1;
min-width: 0;
}
+ /* A range input has an intrinsic minimum width, so without this the seek bar refuses to shrink past it
+ and runs over the volume slider in a narrow player. */
+ .media-trackbar {
+ min-width: 0;
+ }
+
+ /* The volume control holds its width; the seek bar is what gives way. */
+ .media-volume-row {
+ flex-shrink: 0;
+ }
+
.waveform-canvas {
height: 28px;
}
}
}
+
+/* The compact audio player adds a visualizer band above that row, in place of the full player's icon. */
+.audio-preview-wrapper.media-compact {
+ .audio-preview-visualization-wrapper {
+ /* The icon this player drops was the band's only in-flow child (the canvas is absolutely positioned),
+ so without a floor the band collapses to nothing wherever the host is content-sized — a list-view
+ row, as opposed to a fixed-height grid card. It still grows to fill a taller host. */
+ min-height: 8rem;
+ }
+
+ .audio-visualizer-canvas {
+ /* No icon to sit behind, so the grid takes the whole band rather than its bottom half, inset by 12px
+ from the top and sides. The canvas is absolutely positioned, so padding on the wrapper wouldn't move
+ it — and its size has to be stated outright: it is a replaced element, for which `auto` means "use
+ the intrinsic size" rather than "stretch between the offsets". It stays anchored to the bottom
+ (`bottom: 0`), where the bars grow from. */
+ left: 12px;
+ width: calc(100% - 24px);
+ height: calc(100% - 12px);
+ }
+}
diff --git a/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx b/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx
index bc5a8dbcd8..a22917f457 100644
--- a/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/file/MediaPreview.spec.tsx
@@ -155,6 +155,50 @@ describe("MediaPreview", () => {
});
});
+ describe("compact video chrome", () => {
+ const renderCompactVideo = async (environment: "preview" | "embedded") => {
+ await act(async () => render(<MediaPreview entity={videoNote} environment={environment} />, container));
+ if (environment === "preview") {
+ await act(async () => proxyPlayButton()?.click());
+ }
+ };
+
+ it.each([ "preview", "embedded" ] as const)("collapses %s's overlay to a single play / seek / volume row", async (environment) => {
+ await renderCompactVideo(environment);
+
+ const row = container.querySelector(".video-preview-wrapper.media-compact .media-compact-row");
+ expect(row).not.toBeNull();
+ expect(row?.querySelector(".play-button")).not.toBeNull();
+ expect(row?.querySelector(".media-seekbar-row")).not.toBeNull();
+ expect(row?.querySelector(".media-volume-row")).not.toBeNull();
+
+ // Speed, rotate and the ±10s skips are gone, along with the full player's stacked rows.
+ expect(container.querySelector(".speed-dropdown")).toBeNull();
+ expect(container.querySelector(".bx-rotate-right")).toBeNull();
+ expect(container.querySelector(".bx-rewind")).toBeNull();
+ expect(container.querySelector(".bx-fast-forward")).toBeNull();
+ expect(container.querySelector(".media-buttons-row")).toBeNull();
+ });
+
+ it("offers fullscreen from an embed, but not from a preview tile", async () => {
+ await renderCompactVideo("embedded");
+ expect(container.querySelector(".bx-fullscreen")).not.toBeNull();
+
+ await renderCompactVideo("preview");
+ expect(container.querySelector(".bx-fullscreen")).toBeNull();
+ });
+
+ it("leaves the note detail with the full overlay", async () => {
+ await act(async () => render(<MediaPreview entity={videoNote} environment="standalone" />, container));
+
+ expect(container.querySelector(".media-compact-row")).toBeNull();
+ expect(container.querySelector(".media-buttons-row")).not.toBeNull();
+ expect(container.querySelector(".speed-dropdown")).not.toBeNull();
+ expect(container.querySelector(".bx-rotate-right")).not.toBeNull();
+ expect(container.querySelector(".bx-fullscreen")).not.toBeNull();
+ });
+ });
+
it("keeps a preview's clicks to itself, so pressing play in a collection card doesn't open the note", async () => {
// Both the placeholder and the player it becomes opt out of link navigation (see services/link.ts).
await act(async () => render(<MediaPreview entity={videoNote} environment="preview" />, container));
diff --git a/apps/client/src/widgets/type_widgets/file/Video.tsx b/apps/client/src/widgets/type_widgets/file/Video.tsx
index 28524b68d6..90007e31fd 100644
--- a/apps/client/src/widgets/type_widgets/file/Video.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Video.tsx
@@ -6,7 +6,7 @@ import { MutableRef, useCallback, useEffect, useRef, useState } from "preact/hoo
import { t } from "../../../services/i18n";
import ActionButton from "../../react/ActionButton";
import NoItems from "../../react/NoItems";
-import { playerRootClasses, preloadFor } from "./media_environment";
+import { playerRootClasses, preloadFor, showsViewportControls, usesCompactControls } from "./media_environment";
import { MediaPlayerProps, MediaSiblingButton, PlaybackSpeed, PlayModeButton, PlayPauseButton, SeekBar, SkipButton, useMediaPlayMode, useMediaSessionController, VolumeControl } from "./MediaPlayer";
const AUTO_HIDE_DELAY = 3000;
@@ -64,6 +64,7 @@ export default function VideoPreview({ source, environment, note, noteContext, i
const onKeyDown = useKeyboardShortcuts(videoRef, wrapperRef, togglePlayback, flashControls);
const { mode: playMode, setMode: setPlayMode } = useMediaPlayMode(noteContext, videoRef);
const siblingNavigation = useMediaSessionController({ source, environment, note, noteContext, isVisible, autoPlay, mimePrefix: "video/", mediaRef: videoRef, playMode });
+ const compact = usesCompactControls(environment);
if (error) {
return <NoItems icon="bx bx-video-off" text={t("media.unsupported-format", { mime: source.mime.replace("/", "-") })} />;
@@ -85,29 +86,45 @@ export default function VideoPreview({ source, environment, note, noteContext, i
/>
<div className="media-preview-controls">
- <SeekBar mediaRef={videoRef} />
- <div class="media-buttons-row">
- <div className="left">
- <PlaybackSpeed mediaRef={videoRef} />
- {/* The play mode lives on the parent folder, which only the note detail knows. */}
- {noteContext && <PlayModeButton mode={playMode} onSelectMode={setPlayMode} />}
- <RotateButton videoRef={videoRef} />
- </div>
- <div className="center">
- <div className="spacer" />
- <MediaSiblingButton navigation={siblingNavigation} direction="previous" tooltipI18nKey="media.previous-video" />
- <SkipButton mediaRef={videoRef} seconds={-10} icon="bx bx-rewind" text={t("media.back-10s")} />
+ {compact ? (
+ <div class="media-compact-row">
<PlayPauseButton playing={playing} togglePlayback={togglePlayback} />
- <SkipButton mediaRef={videoRef} seconds={10} icon="bx bx-fast-forward" text={t("media.forward-10s")} />
- <MediaSiblingButton navigation={siblingNavigation} direction="next" tooltipI18nKey="media.next-video" />
- </div>
- <div className="right">
+ <SeekBar mediaRef={videoRef} />
<VolumeControl mediaRef={videoRef} />
- <ZoomToFitButton videoRef={videoRef} />
- <PictureInPictureButton videoRef={videoRef} />
- <FullscreenButton targetRef={wrapperRef} />
+ {showsViewportControls(environment) && (
+ <>
+ <PictureInPictureButton videoRef={videoRef} />
+ <FullscreenButton targetRef={wrapperRef} />
+ </>
+ )}
</div>
- </div>
+ ) : (
+ <>
+ <SeekBar mediaRef={videoRef} />
+ <div class="media-buttons-row">
+ <div className="left">
+ <PlaybackSpeed mediaRef={videoRef} />
+ {/* The play mode lives on the parent folder, which only the note detail knows. */}
+ {noteContext && <PlayModeButton mode={playMode} onSelectMode={setPlayMode} />}
+ <RotateButton videoRef={videoRef} />
+ </div>
+ <div className="center">
+ <div className="spacer" />
+ <MediaSiblingButton navigation={siblingNavigation} direction="previous" tooltipI18nKey="media.previous-video" />
+ <SkipButton mediaRef={videoRef} seconds={-10} icon="bx bx-rewind" text={t("media.back-10s")} />
+ <PlayPauseButton playing={playing} togglePlayback={togglePlayback} />
+ <SkipButton mediaRef={videoRef} seconds={10} icon="bx bx-fast-forward" text={t("media.forward-10s")} />
+ <MediaSiblingButton navigation={siblingNavigation} direction="next" tooltipI18nKey="media.next-video" />
+ </div>
+ <div className="right">
+ <VolumeControl mediaRef={videoRef} />
+ <ZoomToFitButton videoRef={videoRef} />
+ <PictureInPictureButton videoRef={videoRef} />
+ <FullscreenButton targetRef={wrapperRef} />
+ </div>
+ </div>
+ </>
+ )}
</div>
</div>
);
diff --git a/apps/client/src/widgets/type_widgets/file/media_environment.spec.ts b/apps/client/src/widgets/type_widgets/file/media_environment.spec.ts
index 064cd72aa0..d4ee27a698 100644
--- a/apps/client/src/widgets/type_widgets/file/media_environment.spec.ts
+++ b/apps/client/src/widgets/type_widgets/file/media_environment.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { loadsEagerly, playerRootClasses, preloadFor, usesCompactControls } from "./media_environment";
+import { loadsEagerly, playerRootClasses, preloadFor, showsViewportControls, usesCompactControls } from "./media_environment";
describe("media environment", () => {
it("only a preview is lazy — it must not create a media element until the user asks for one", () => {
@@ -26,4 +26,10 @@ describe("media environment", () => {
expect(usesCompactControls("embedded")).toBe(true);
expect(usesCompactControls("standalone")).toBe(false);
});
+
+ it("withholds the escape-to-a-bigger-viewport controls from a preview tile only", () => {
+ expect(showsViewportControls("preview")).toBe(false);
+ expect(showsViewportControls("embedded")).toBe(true);
+ expect(showsViewportControls("standalone")).toBe(true);
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/file/media_environment.ts b/apps/client/src/widgets/type_widgets/file/media_environment.ts
index 3eea829ce7..8338262775 100644
--- a/apps/client/src/widgets/type_widgets/file/media_environment.ts
+++ b/apps/client/src/widgets/type_widgets/file/media_environment.ts
@@ -40,3 +40,12 @@ export function playerRootClasses(environment: MediaEnvironment): string {
export function usesCompactControls(environment: MediaEnvironment): boolean {
return environment !== "standalone";
}
+
+/**
+ * Whether the player offers a way out to a bigger viewport (picture-in-picture, fullscreen). A preview is an
+ * incidental tile in a list of many and stays put; an embedded player is a deliberate placement, where being
+ * able to escape a small box is worth the two buttons.
+ */
+export function showsViewportControls(environment: MediaEnvironment): boolean {
+ return environment !== "preview";
+}