-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from mbret/develop
feat: fixed fullscreen switch
- Loading branch information
Showing
7 changed files
with
155 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { useEffect } from "react" | ||
import screenfull from "screenfull" | ||
import { Report } from "../debug/report.shared" | ||
import { useDialogManager } from "../dialog" | ||
import { | ||
EMPTY, | ||
catchError, | ||
defer, | ||
endWith, | ||
from, | ||
mergeMap, | ||
retry, | ||
throwError, | ||
timer | ||
} from "rxjs" | ||
import { useSubscribe } from "reactjrx" | ||
import { CancelError } from "../errors" | ||
|
||
const isPermissionCheckFailedError = (error: unknown): error is TypeError => | ||
error instanceof TypeError && | ||
// chrome | ||
(error.message === "Permissions check failed" || | ||
// safari | ||
error.message === "Type error" || | ||
// firefox | ||
error.message === "Fullscreen request denied") | ||
|
||
export const useFullscreenOnMount = ({ enabled }: { enabled: boolean }) => { | ||
const dialog = useDialogManager() | ||
|
||
useSubscribe(() => { | ||
if (enabled && screenfull.isEnabled && !screenfull.isFullscreen) { | ||
return defer(() => { | ||
return from(screenfull.request(undefined, { navigationUI: "hide" })) | ||
}).pipe( | ||
retry({ | ||
count: 1, | ||
delay: (error) => { | ||
if (isPermissionCheckFailedError(error)) { | ||
return timer(5).pipe( | ||
mergeMap(() => | ||
// we avoid double dialog because of strict mode | ||
screenfull.isFullscreen | ||
? throwError(() => error) | ||
: dialog({ | ||
title: "Fullscreen request", | ||
content: | ||
"Your browser does not allow automatic fullscreen without an interaction", | ||
confirmTitle: "Fullscreen", | ||
cancellable: true | ||
}).$.pipe(endWith(true)) | ||
) | ||
) | ||
} | ||
|
||
throw error | ||
} | ||
}), | ||
catchError((error) => { | ||
if ( | ||
isPermissionCheckFailedError(error) || | ||
error instanceof CancelError | ||
) { | ||
return EMPTY | ||
} | ||
|
||
Report.error(error) | ||
|
||
return EMPTY | ||
}) | ||
) | ||
} | ||
|
||
return EMPTY | ||
}, [enabled, dialog]) | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (screenfull.isEnabled && screenfull.isFullscreen) { | ||
screenfull.exit().catch(Report.error) | ||
} | ||
} | ||
}, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,13 @@ | ||
import { useEffect } from "react" | ||
import screenfull from "screenfull" | ||
import { IS_MOBILE_DEVICE } from "../constants" | ||
import { Report } from "../debug/report.shared" | ||
import { useLocalSettings } from "../settings/states" | ||
import { useFullscreenOnMount } from "../fullscreen/useFullscreenOnMount" | ||
|
||
export const useFullScreenSwitch = () => { | ||
const localSettings = useLocalSettings() | ||
export const useFullscreenAutoSwitch = () => { | ||
const { readingFullScreenSwitchMode } = useLocalSettings() | ||
|
||
useEffect(() => { | ||
if ( | ||
(localSettings.readingFullScreenSwitchMode === "always" || | ||
(localSettings.readingFullScreenSwitchMode === "automatic" && | ||
IS_MOBILE_DEVICE)) && | ||
screenfull.isEnabled && | ||
!screenfull.isFullscreen | ||
) { | ||
screenfull | ||
.request(undefined, { navigationUI: "hide" }) | ||
.catch(Report.error) | ||
} | ||
|
||
return () => { | ||
if (screenfull.isEnabled && screenfull.isFullscreen) { | ||
screenfull.exit().catch(Report.error) | ||
} | ||
} | ||
}, [localSettings]) | ||
useFullscreenOnMount({ | ||
enabled: | ||
readingFullScreenSwitchMode === "always" || | ||
(readingFullScreenSwitchMode === "automatic" && IS_MOBILE_DEVICE) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters