Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4c54dad7a |
@@ -89,6 +89,9 @@ import { zIndices } from '../styles/zIndex';
|
||||
|
||||
const PIP_MIN_W = 200;
|
||||
const PIP_MIN_H = 112;
|
||||
/** [Gitea #323] Minimised PiP bar size. */
|
||||
const PIP_MINI_W = 220;
|
||||
const PIP_MINI_H = 40;
|
||||
|
||||
type Corner = 'se' | 'sw' | 'ne' | 'nw';
|
||||
|
||||
@@ -981,6 +984,28 @@ export function CallEmbedProvider({ children }: CallEmbedProviderProps) {
|
||||
);
|
||||
|
||||
const [pipIsFullscreen, setPipIsFullscreen] = useState(false);
|
||||
// [Gitea #323] Minimised PiP: a small draggable bar (room name + expand)
|
||||
// instead of the video. Remembered; phones start minimised.
|
||||
const [pipMinimized, setPipMinimized] = useState<boolean>(() => {
|
||||
try {
|
||||
const v = localStorage.getItem('pip-minimized');
|
||||
if (v !== null) return v === '1';
|
||||
} catch {
|
||||
/* storage unavailable */
|
||||
}
|
||||
return window.innerWidth < 600;
|
||||
});
|
||||
const pipFullSizeRef = React.useRef({ width: 280, height: 158 });
|
||||
const togglePipMinimized = useCallback(() => {
|
||||
setPipMinimized((m) => {
|
||||
try {
|
||||
localStorage.setItem('pip-minimized', m ? '0' : '1');
|
||||
} catch {
|
||||
/* storage unavailable */
|
||||
}
|
||||
return !m;
|
||||
});
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
const onFsChange = () => setPipIsFullscreen(!!document.fullscreenElement);
|
||||
document.addEventListener('fullscreenchange', onFsChange);
|
||||
@@ -1078,6 +1103,26 @@ export function CallEmbedProvider({ children }: CallEmbedProviderProps) {
|
||||
}
|
||||
}, [pipMode, callVisible]);
|
||||
|
||||
// [Gitea #323] Apply the minimised / full PiP size (after the entry effect
|
||||
// below has positioned and sized it). Keeps the bar on screen.
|
||||
React.useEffect(() => {
|
||||
const el = callEmbedRef.current;
|
||||
if (!el || !pipMode) return;
|
||||
if (pipMinimized) {
|
||||
if (el.offsetHeight > PIP_MINI_H) {
|
||||
pipFullSizeRef.current = { width: el.offsetWidth, height: el.offsetHeight };
|
||||
}
|
||||
el.style.width = `${PIP_MINI_W}px`;
|
||||
el.style.height = `${PIP_MINI_H}px`;
|
||||
} else {
|
||||
el.style.width = `${pipFullSizeRef.current.width}px`;
|
||||
el.style.height = `${pipFullSizeRef.current.height}px`;
|
||||
}
|
||||
const rect = el.getBoundingClientRect();
|
||||
el.style.left = `${Math.max(0, Math.min(rect.left, window.innerWidth - el.offsetWidth))}px`;
|
||||
el.style.top = `${Math.max(0, Math.min(rect.top, window.innerHeight - el.offsetHeight))}px`;
|
||||
}, [pipMode, pipMinimized, callEmbedRef]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!pipMode) return;
|
||||
const onPipWindowResize = (): void => {
|
||||
@@ -1396,19 +1441,50 @@ export function CallEmbedProvider({ children }: CallEmbedProviderProps) {
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
zIndex: 1,
|
||||
background: 'transparent',
|
||||
// Minimised: cover the frame so only the bar shows.
|
||||
background: pipMinimized ? color.Surface.Container : 'transparent',
|
||||
cursor: 'grab',
|
||||
// [Gitea #43] The call's "Share your screen?" prompt is drawn
|
||||
// inside the frame: let the click reach its Share button.
|
||||
pointerEvents: pipPromptOpen ? 'none' : undefined,
|
||||
display: 'flex',
|
||||
alignItems: 'flex-start',
|
||||
justifyContent: 'flex-end',
|
||||
alignItems: pipMinimized ? 'center' : 'flex-start',
|
||||
padding: '6px',
|
||||
}}
|
||||
>
|
||||
<div style={{ display: 'flex', gap: config.space.S100, alignItems: 'center' }}>
|
||||
{document.fullscreenEnabled && (
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
gap: config.space.S100,
|
||||
alignItems: 'center',
|
||||
...(pipMinimized ? { width: '100%' } : {}),
|
||||
}}
|
||||
>
|
||||
{/* Leftmost: the top-right corner is a resize handle. */}
|
||||
<IconButton
|
||||
type="button"
|
||||
size="300"
|
||||
radii="300"
|
||||
variant="Surface"
|
||||
fill="None"
|
||||
aria-label={pipMinimized ? 'Expand call' : 'Minimise call'}
|
||||
aria-pressed={pipMinimized}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
togglePipMinimized();
|
||||
}}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
onTouchStart={(e) => e.stopPropagation()}
|
||||
style={{
|
||||
background: 'rgba(0,0,0,0.65)',
|
||||
backdropFilter: 'blur(4px)',
|
||||
color: '#fff',
|
||||
}}
|
||||
>
|
||||
<Icon size="100" src={pipMinimized ? Icons.ChevronTop : Icons.Minus} />
|
||||
</IconButton>
|
||||
{!pipMinimized && document.fullscreenEnabled && (
|
||||
<IconButton
|
||||
type="button"
|
||||
size="300"
|
||||
@@ -1440,14 +1516,23 @@ export function CallEmbedProvider({ children }: CallEmbedProviderProps) {
|
||||
fontSize: '11px',
|
||||
fontWeight: 600,
|
||||
pointerEvents: 'none',
|
||||
...(pipMinimized
|
||||
? {
|
||||
flexGrow: 1,
|
||||
minWidth: 0,
|
||||
overflow: 'hidden',
|
||||
textOverflow: 'ellipsis',
|
||||
whiteSpace: 'nowrap',
|
||||
}
|
||||
: {}),
|
||||
}}
|
||||
>
|
||||
↗ Return to call
|
||||
{pipMinimized ? `↗ ${callEmbed.room.name ?? 'Call'}` : '↗ Return to call'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<PipMuteOverlay callEmbed={callEmbed} />
|
||||
{(['se', 'sw', 'ne', 'nw'] as Corner[]).map((corner) => {
|
||||
{!pipMinimized && <PipMuteOverlay callEmbed={callEmbed} />}
|
||||
{(pipMinimized ? [] : (['se', 'sw', 'ne', 'nw'] as Corner[])).map((corner) => {
|
||||
const s = corner.includes('s');
|
||||
const e2 = corner.includes('e');
|
||||
const dots = [
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
formatFileSize,
|
||||
hasTransparency,
|
||||
isAnimatedWebp,
|
||||
isCompressible,
|
||||
} from './imageCompression';
|
||||
import { formatFileSize, isCompressible } from './imageCompression';
|
||||
|
||||
test('formatFileSize formats bytes below 1KB as B', () => {
|
||||
assert.equal(formatFileSize(0), '0 B');
|
||||
@@ -30,38 +25,14 @@ test('formatFileSize formats values in the MB range', () => {
|
||||
test('isCompressible accepts raster image types', () => {
|
||||
assert.equal(isCompressible({ type: 'image/png' } as Blob), true);
|
||||
assert.equal(isCompressible({ type: 'image/jpeg' } as Blob), true);
|
||||
assert.equal(isCompressible({ type: 'image/gif' } as Blob), true);
|
||||
assert.equal(isCompressible({ type: 'image/webp' } as Blob), true);
|
||||
});
|
||||
|
||||
test('isCompressible rejects svg, empty type, and non-images', () => {
|
||||
assert.equal(isCompressible({ type: 'image/svg+xml' } as Blob), false);
|
||||
// [Gitea #331] usually animated; a canvas keeps one frame
|
||||
assert.equal(isCompressible({ type: 'image/gif' } as Blob), false);
|
||||
assert.equal(isCompressible({ type: 'image/apng' } as Blob), false);
|
||||
assert.equal(isCompressible({ type: '' } as Blob), false);
|
||||
assert.equal(isCompressible({ type: 'application/pdf' } as Blob), false);
|
||||
assert.equal(isCompressible({ type: 'text/plain' } as Blob), false);
|
||||
assert.equal(isCompressible({ type: 'video/mp4' } as Blob), false);
|
||||
});
|
||||
|
||||
const webpHeader = (chunk: string, flags = 0) => {
|
||||
const b = new Uint8Array(32);
|
||||
const put = (at: number, str: string) => b.set(new TextEncoder().encode(str), at);
|
||||
put(0, 'RIFF');
|
||||
put(8, 'WEBP');
|
||||
put(12, chunk);
|
||||
b[20] = flags;
|
||||
return b;
|
||||
};
|
||||
|
||||
test('isAnimatedWebp: VP8X with the animation flag (#331)', () => {
|
||||
assert.equal(isAnimatedWebp(webpHeader('VP8X', 0x02)), true);
|
||||
assert.equal(isAnimatedWebp(webpHeader('VP8X', 0x10)), false, 'alpha flag only');
|
||||
assert.equal(isAnimatedWebp(webpHeader('VP8 ')), false, 'simple lossy');
|
||||
assert.equal(isAnimatedWebp(new Uint8Array(8)), false);
|
||||
});
|
||||
|
||||
test('hasTransparency (#331)', () => {
|
||||
assert.equal(hasTransparency(new Uint8ClampedArray([1, 2, 3, 255, 4, 5, 6, 255])), false);
|
||||
assert.equal(hasTransparency(new Uint8ClampedArray([1, 2, 3, 255, 0, 0, 0, 0])), true);
|
||||
});
|
||||
|
||||
@@ -8,32 +8,11 @@ export type CompressionResult = {
|
||||
height: number;
|
||||
};
|
||||
|
||||
// Raster images the browser can render to canvas can be compressed to JPEG.
|
||||
// SVG is vector and must stay as-is. [Gitea #331] GIF and APNG are usually
|
||||
// animated, and a canvas keeps only the first frame, so they aren't offered.
|
||||
// Empty type string (undetected MIME) is excluded.
|
||||
const NOT_COMPRESSIBLE = new Set(['image/svg+xml', 'image/gif', 'image/apng']);
|
||||
// Any raster image the browser can render to canvas can be compressed to JPEG.
|
||||
// SVG is vector and must stay as-is; GIF animation is lost on canvas but static
|
||||
// frames compress fine. Empty type string (undetected MIME) is excluded.
|
||||
const isCompressibleType = (type: string): boolean =>
|
||||
type.startsWith('image/') && !NOT_COMPRESSIBLE.has(type);
|
||||
|
||||
/**
|
||||
* Pure: whether a WebP file is animated. An animated WebP is a VP8X file
|
||||
* whose header has the animation flag (byte 20, bit 0x02) and an ANIM chunk.
|
||||
*/
|
||||
export function isAnimatedWebp(bytes: Uint8Array): boolean {
|
||||
const ascii = (at: number, len: number) => String.fromCharCode(...bytes.subarray(at, at + len));
|
||||
if (bytes.length < 21 || ascii(0, 4) !== 'RIFF' || ascii(8, 4) !== 'WEBP') return false;
|
||||
// Bit 0x02 of the VP8X flags byte (no-bitwise: test it arithmetically).
|
||||
return ascii(12, 4) === 'VP8X' && Math.floor(bytes[20] / 2) % 2 === 1;
|
||||
}
|
||||
|
||||
/** Pure: whether RGBA pixel data has any pixel that isn't fully opaque. */
|
||||
export function hasTransparency(rgba: Uint8ClampedArray): boolean {
|
||||
for (let i = 3; i < rgba.length; i += 4) {
|
||||
if (rgba[i] !== 255) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
type.startsWith('image/') && type !== 'image/svg+xml' && type !== '';
|
||||
|
||||
/** Returns true if this file can be compressed via canvas (any raster image type). */
|
||||
export function isCompressible(file: File | Blob): boolean {
|
||||
@@ -49,8 +28,7 @@ const JPEG_OUTPUT_TYPE = 'image/jpeg';
|
||||
*
|
||||
* PNG is skipped entirely: it may carry an alpha channel, and re-encoding to
|
||||
* JPEG composites transparency onto an opaque (black) background, corrupting the
|
||||
* image. Any other image with transparent pixels, and animated WebP, are left
|
||||
* alone for the same reason (#331). Returning null makes callers fall back to uploading the lossless
|
||||
* image. Returning null makes callers fall back to uploading the lossless
|
||||
* original. The image is decoded with `imageOrientation: 'from-image'` so any
|
||||
* EXIF orientation is baked into the pixels instead of being silently dropped.
|
||||
*/
|
||||
@@ -61,15 +39,6 @@ export async function compressImage(
|
||||
if (!isCompressibleType(file.type)) return null;
|
||||
// Skip PNG (potential alpha) — re-encoding to JPEG would flatten transparency.
|
||||
if (file.type === 'image/png') return null;
|
||||
// [Gitea #331] An animated WebP would become a single still frame.
|
||||
if (file.type === 'image/webp') {
|
||||
try {
|
||||
const head = new Uint8Array(await file.slice(0, 32).arrayBuffer());
|
||||
if (isAnimatedWebp(head)) return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
let bitmap: ImageBitmap;
|
||||
try {
|
||||
@@ -91,13 +60,6 @@ export async function compressImage(
|
||||
}
|
||||
ctx.drawImage(bitmap, 0, 0);
|
||||
bitmap.close();
|
||||
// [Gitea #331] JPEG has no alpha: a transparent WebP (sticker, logo) came out
|
||||
// with a black background. Keep any image with transparency as the original.
|
||||
try {
|
||||
if (hasTransparency(ctx.getImageData(0, 0, width, height).data)) return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
canvas.toBlob(
|
||||
|
||||
Reference in New Issue
Block a user