From 0566075efeba0b143be586092175879615bca6f8 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sat, 25 Mar 2023 08:08:49 -0700 Subject: [PATCH] ply: restore screen orientation on fullscreen exit (fix #521) Signed-off-by: Varun Patil --- src/components/viewer/PsVideo.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/viewer/PsVideo.ts b/src/components/viewer/PsVideo.ts index 5b426cd0..95b46785 100644 --- a/src/components/viewer/PsVideo.ts +++ b/src/components/viewer/PsVideo.ts @@ -402,21 +402,32 @@ class VideoContentSetup { // Add fullscreen orientation hooks if (screen.orientation?.lock) { + // Store the previous orientation + // This is because unlocking (at least on Chrome) does + // not restore the previous orientation + let previousOrientation: OrientationLockType; + + // Lock orientation when entering fullscreen plyr.on("enterfullscreen", (event) => { const rotation = this.updateRotation(content); const exif = content.data.photo.imageInfo?.exif; const h = Number(exif?.ImageHeight || 0); const w = Number(exif?.ImageWidth || 1); + if (h && w) { - if (h < w && !rotation) { - screen.orientation.lock("landscape"); - } else { - screen.orientation.lock("portrait"); - } + previousOrientation = screen.orientation.type; + const orientation = h < w && !rotation ? "landscape" : "portrait"; + screen.orientation.lock(orientation); } }); + // Unlock orientation when exiting fullscreen plyr.on("exitfullscreen", (event) => { + if (previousOrientation) { + screen.orientation.lock(previousOrientation); + previousOrientation = undefined; + } + screen.orientation.unlock(); }); }