Skip to content

Commit

Permalink
refactor(frontend): フルスクリーン周りの調整
Browse files Browse the repository at this point in the history
  • Loading branch information
taiyme committed Oct 29, 2024
1 parent a93b6c3 commit 783032c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
43 changes: 20 additions & 23 deletions packages/frontend/src/components/MkMediaVideo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ import type { MenuItem } from '@/types/menu.js';
import { i18n } from '@/i18n.js';
import { confirm, popupMenu } from '@/os.js';
import { defaultStore } from '@/store.js';
import { isFullscreenNotSupported } from '@/scripts/device-kind.js';
import { type Keymap } from '@/scripts/hotkey.js';
import hasAudio from '@/scripts/media-has-audio.js';
import { exitFullscreen, requestFullscreen } from '@/scripts/tms/fullscreen.js';
import { getMediaMenu } from '@/scripts/tms/get-media-menu.js';
import { useReactiveDriveFile } from '@/scripts/tms/use-reactive-drive-file.js';
import bytes from '@/filters/bytes.js';
Expand Down Expand Up @@ -359,26 +359,21 @@ function togglePlayPause() {
}

function toggleFullscreen() {
if (isFullscreenNotSupported && videoEl.value) {
if (isFullscreen.value) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
videoEl.value.webkitExitFullscreen();
isFullscreen.value = false;
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
videoEl.value.webkitEnterFullscreen();
isFullscreen.value = true;
}
} else if (playerEl.value) {
if (isFullscreen.value) {
document.exitFullscreen();
isFullscreen.value = false;
} else {
playerEl.value.requestFullscreen({ navigationUI: 'hide' });
isFullscreen.value = true;
}
if (playerEl.value == null || videoEl.value == null) return;
if (isFullscreen.value) {
exitFullscreen({
videoEl: videoEl.value,
});
isFullscreen.value = false;
} else {
requestFullscreen({
videoEl: videoEl.value,
playerEl: playerEl.value,
options: {
navigationUI: 'hide',
},
});
isFullscreen.value = true;
}
}

Expand Down Expand Up @@ -479,8 +474,10 @@ watch(loop, (to) => {
});

watch(hideRef, (to) => {
if (to && isFullscreen.value) {
document.exitFullscreen();
if (videoEl.value && to && isFullscreen.value) {
exitFullscreen({
videoEl: videoEl.value,
});
isFullscreen.value = false;
}
});
Expand Down
7 changes: 0 additions & 7 deletions packages/frontend/src/scripts/device-kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ const ua = navigator.userAgent.toLowerCase();
const isTablet = /ipad/.test(ua) || (/mobile|iphone|android/.test(ua) && window.innerWidth > 700);
const isSmartphone = !isTablet && /mobile|iphone|android/.test(ua);

const isIPhone = /iphone|ipod/gi.test(ua) && navigator.maxTouchPoints > 1;
// navigator.platform may be deprecated but this check is still required
const isIPadOS = navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1;
const isIos = /ipad|iphone|ipod/gi.test(ua) && navigator.maxTouchPoints > 1;

export const isFullscreenNotSupported = isIPhone || isIos;

export const deviceKind: 'smartphone' | 'tablet' | 'desktop' = defaultStore.state.overridedDeviceKind ? defaultStore.state.overridedDeviceKind
: isSmartphone ? 'smartphone'
: isTablet ? 'tablet'
Expand Down
46 changes: 46 additions & 0 deletions packages/frontend/src/scripts/tms/fullscreen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

type PartiallyPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

type VideoEl = PartiallyPartial<HTMLVideoElement, 'requestFullscreen'> & {
webkitEnterFullscreen?(): void;
webkitExitFullscreen?(): void;
};

type PlayerEl = PartiallyPartial<HTMLElement, 'requestFullscreen'>;

type RequestFullscreenProps = {
readonly videoEl: VideoEl;
readonly playerEl: PlayerEl;
readonly options?: FullscreenOptions | null;
};

type ExitFullscreenProps = {
readonly videoEl: VideoEl;
};

export const requestFullscreen = ({ videoEl, playerEl, options }: RequestFullscreenProps) => {
if (playerEl.requestFullscreen != null) {
playerEl.requestFullscreen(options ?? undefined);
return;
}
if (videoEl.webkitEnterFullscreen != null) {
videoEl.webkitEnterFullscreen();
return;
}
};

export const exitFullscreen = ({ videoEl }: ExitFullscreenProps) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (document.exitFullscreen != null) {
document.exitFullscreen();
return;
}
if (videoEl.webkitExitFullscreen != null) {
videoEl.webkitExitFullscreen();
return;
}
};

0 comments on commit 783032c

Please sign in to comment.