Add "auto" progress bar position

Automatically choose the progress bar position based on the available space.
Previously it was only possible to automatically switch from a horizontal to a vertical position but not the other way around
This commit is contained in:
schroda
2025-01-11 02:02:48 +01:00
parent f032f971ac
commit 7901961821
7 changed files with 64 additions and 38 deletions

View File

@@ -940,7 +940,6 @@
}, },
"progress_bar": { "progress_bar": {
"auto_vertical_position": { "auto_vertical_position": {
"description": "Automatically change the progress bar to a vertical position depending on the available space",
"title": "Automatic progress bar vertical position" "title": "Automatic progress bar vertical position"
}, },
"position": "Progress bar position", "position": "Progress bar position",

View File

@@ -7,7 +7,8 @@
*/ */
import { AppMetadataKeys, IMetadataMigration } from '@/modules/metadata/Metadata.types.ts'; import { AppMetadataKeys, IMetadataMigration } from '@/modules/metadata/Metadata.types.ts';
import { ReaderPageScaleMode, ReadingMode } from '@/modules/reader/types/Reader.types.ts'; import { ProgressBarPosition, ReaderPageScaleMode, ReadingMode } from '@/modules/reader/types/Reader.types.ts';
import { DEFAULT_READER_SETTINGS } from '@/modules/reader/constants/ReaderSettings.constants.tsx';
export const APP_METADATA_KEY_PREFIX = 'webUI'; export const APP_METADATA_KEY_PREFIX = 'webUI';
@@ -308,4 +309,28 @@ export const METADATA_MIGRATIONS: IMetadataMigration[] = [
{ {
deleteKeys: ['pageScaleMode', 'shouldStretchPage', 'readerWidth'], deleteKeys: ['pageScaleMode', 'shouldStretchPage', 'readerWidth'],
}, },
{
values: [
{
key: 'progressBarPositionAutoVertical',
oldValue: '-1',
newValue: `${DEFAULT_READER_SETTINGS.progressBarPositionAutoVertical}`,
},
{
key: 'progressBarPosition',
oldValue: '0',
newValue: `${ProgressBarPosition.BOTTOM}`,
},
{
key: 'progressBarPosition',
oldValue: '1',
newValue: `${ProgressBarPosition.LEFT}`,
},
{
key: 'progressBarPosition',
oldValue: '2',
newValue: `${ProgressBarPosition.RIGHT}`,
},
],
},
]; ];

View File

@@ -41,6 +41,8 @@ const PROGRESS_BAR_POSITION_TO_SLIDE_DIRECTION: Record<ProgressBarPosition, Slid
[ProgressBarPosition.BOTTOM]: 'up', [ProgressBarPosition.BOTTOM]: 'up',
[ProgressBarPosition.LEFT]: 'right', [ProgressBarPosition.LEFT]: 'right',
[ProgressBarPosition.RIGHT]: 'left', [ProgressBarPosition.RIGHT]: 'left',
// should never get accessed
[ProgressBarPosition.AUTO]: 'left',
}; };
const BaseMobileReaderProgressBar = ({ const BaseMobileReaderProgressBar = ({

View File

@@ -9,7 +9,7 @@
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos'; import ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos';
import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew'; import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew';
import NotInterestedIcon from '@mui/icons-material/NotInterested'; import AutoModeIcon from '@mui/icons-material/AutoMode';
import { import {
IReaderSettings, IReaderSettings,
ProgressBarPosition, ProgressBarPosition,
@@ -19,6 +19,10 @@ import { ValueToDisplayData } from '@/modules/core/Core.types.ts';
import { ButtonSelectInput } from '@/modules/core/components/inputs/ButtonSelectInput.tsx'; import { ButtonSelectInput } from '@/modules/core/components/inputs/ButtonSelectInput.tsx';
const VALUE_TO_DISPLAY_DATA: ValueToDisplayData<ProgressBarPosition> = { const VALUE_TO_DISPLAY_DATA: ValueToDisplayData<ProgressBarPosition> = {
[ProgressBarPosition.AUTO]: {
title: 'global.label.auto',
icon: <AutoModeIcon />,
},
[ProgressBarPosition.BOTTOM]: { [ProgressBarPosition.BOTTOM]: {
title: 'global.label.bottom', title: 'global.label.bottom',
icon: <ArrowBackIosNewIcon sx={{ transform: 'rotate(90deg)' }} />, icon: <ArrowBackIosNewIcon sx={{ transform: 'rotate(90deg)' }} />,
@@ -35,17 +39,9 @@ const VALUE_TO_DISPLAY_DATA: ValueToDisplayData<ProgressBarPosition> = {
const PROGRESS_BAR_POSITION_VALUES = Object.values(ProgressBarPosition).filter((value) => typeof value === 'number'); const PROGRESS_BAR_POSITION_VALUES = Object.values(ProgressBarPosition).filter((value) => typeof value === 'number');
const VALUE_TO_DISPLAY_DATA_AUTO_VERTICAL: ValueToDisplayData<ProgressBarPositionAutoVertical> = { const PROGRESS_BAR_AUTO_VERTICAL_POSITION_VALUES = Object.values(
...VALUE_TO_DISPLAY_DATA, ProgressBarPositionAutoVertical,
[ProgressBarPositionAutoVertical.OFF]: { ) as unknown as TupleUnion<keyof typeof ProgressBarPositionAutoVertical>;
title: 'global.label.disabled',
icon: <NotInterestedIcon />,
},
};
const PROGRESS_BAR_AUTO_VERTICAL_POSITION_VALUES = Object.values(ProgressBarPositionAutoVertical).filter(
(value) => typeof value === 'number',
);
export const ReaderSettingProgressBarPosition = ({ export const ReaderSettingProgressBarPosition = ({
progressBarPosition, progressBarPosition,
@@ -61,7 +57,7 @@ export const ReaderSettingProgressBarPosition = ({
}) => { }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const supportsAutoVerticalPosition = progressBarPosition === ProgressBarPosition.BOTTOM; const isAutoPosition = progressBarPosition === ProgressBarPosition.AUTO;
return ( return (
<> <>
@@ -72,14 +68,13 @@ export const ReaderSettingProgressBarPosition = ({
setValue={(position) => updateSetting('progressBarPosition', position)} setValue={(position) => updateSetting('progressBarPosition', position)}
valueToDisplayData={VALUE_TO_DISPLAY_DATA} valueToDisplayData={VALUE_TO_DISPLAY_DATA}
/> />
{supportsAutoVerticalPosition && ( {isAutoPosition && (
<ButtonSelectInput <ButtonSelectInput
label={t('reader.settings.progress_bar.auto_vertical_position.title')} label={t('reader.settings.progress_bar.auto_vertical_position.title')}
description={t('reader.settings.progress_bar.auto_vertical_position.description')}
value={progressBarPositionAutoVertical} value={progressBarPositionAutoVertical}
values={PROGRESS_BAR_AUTO_VERTICAL_POSITION_VALUES} values={PROGRESS_BAR_AUTO_VERTICAL_POSITION_VALUES}
setValue={(position) => updateSetting('progressBarPositionAutoVertical', position)} setValue={(position) => updateSetting('progressBarPositionAutoVertical', position)}
valueToDisplayData={VALUE_TO_DISPLAY_DATA_AUTO_VERTICAL} valueToDisplayData={VALUE_TO_DISPLAY_DATA}
/> />
)} )}
</> </>

View File

@@ -18,7 +18,6 @@ import {
IReaderSettings, IReaderSettings,
IReaderSettingsGlobal, IReaderSettingsGlobal,
ProgressBarPosition, ProgressBarPosition,
ProgressBarPositionAutoVertical,
ProgressBarType, ProgressBarType,
ReaderBackgroundColor, ReaderBackgroundColor,
ReaderExitMode, ReaderExitMode,
@@ -74,8 +73,8 @@ export const DEFAULT_READER_SETTINGS: IReaderSettings = {
tapZoneInvertMode: { vertical: false, horizontal: false }, tapZoneInvertMode: { vertical: false, horizontal: false },
progressBarType: ProgressBarType.STANDARD, progressBarType: ProgressBarType.STANDARD,
progressBarSize: 4, progressBarSize: 4,
progressBarPosition: ProgressBarPosition.BOTTOM, progressBarPosition: ProgressBarPosition.AUTO,
progressBarPositionAutoVertical: ProgressBarPositionAutoVertical.RIGHT, progressBarPositionAutoVertical: ProgressBarPosition.RIGHT,
pageScaleMode: ReaderPageScaleMode.ORIGINAL, pageScaleMode: ReaderPageScaleMode.ORIGINAL,
shouldStretchPage: false, shouldStretchPage: false,
shouldOffsetDoubleSpreads: false, shouldOffsetDoubleSpreads: false,
@@ -150,6 +149,8 @@ export const READER_PROGRESS_BAR_POSITION_TO_PLACEMENT: Record<ProgressBarPositi
[ProgressBarPosition.BOTTOM]: 'top', [ProgressBarPosition.BOTTOM]: 'top',
[ProgressBarPosition.LEFT]: 'right', [ProgressBarPosition.LEFT]: 'right',
[ProgressBarPosition.RIGHT]: 'left', [ProgressBarPosition.RIGHT]: 'left',
// should never get accessed
[ProgressBarPosition.AUTO]: 'left',
}; };
export const READING_DIRECTION_VALUE_TO_DISPLAY_DATA: ValueToDisplayData<ReadingDirection> = { export const READING_DIRECTION_VALUE_TO_DISPLAY_DATA: ValueToDisplayData<ReadingDirection> = {

View File

@@ -20,16 +20,20 @@ export enum ProgressBarType {
} }
export enum ProgressBarPosition { export enum ProgressBarPosition {
AUTO,
BOTTOM, BOTTOM,
LEFT, LEFT,
RIGHT, RIGHT,
} }
export enum ProgressBarPositionAutoVertical { type TProgressBarPositionAutoVertical = Exclude<
OFF = -1, ProgressBarPosition,
LEFT = ProgressBarPosition.LEFT, ProgressBarPosition.BOTTOM | ProgressBarPosition.AUTO
RIGHT = ProgressBarPosition.RIGHT, >;
} export const ProgressBarPositionAutoVertical = {
[ProgressBarPosition.LEFT]: ProgressBarPosition.LEFT,
[ProgressBarPosition.RIGHT]: ProgressBarPosition.RIGHT,
} satisfies Record<TProgressBarPositionAutoVertical, TProgressBarPositionAutoVertical>;
export enum ReadingDirection { export enum ReadingDirection {
LTR, LTR,
@@ -125,7 +129,7 @@ export interface IReaderSettingsGlobal {
*/ */
progressBarSize: number; progressBarSize: number;
progressBarPosition: ProgressBarPosition; progressBarPosition: ProgressBarPosition;
progressBarPositionAutoVertical: ProgressBarPositionAutoVertical; progressBarPositionAutoVertical: TProgressBarPositionAutoVertical;
shouldShowPageNumber: boolean; shouldShowPageNumber: boolean;
isStaticNav: boolean; isStaticNav: boolean;
backgroundColor: ReaderBackgroundColor; backgroundColor: ReaderBackgroundColor;

View File

@@ -66,19 +66,19 @@ export const getPagerForReadingMode = (
export const getProgressBarPosition = ( export const getProgressBarPosition = (
progressBarPosition: ProgressBarPosition, progressBarPosition: ProgressBarPosition,
progressBarPositionAutoVertical: ProgressBarPositionAutoVertical, progressBarPositionAutoVertical: keyof typeof ProgressBarPositionAutoVertical,
topOffset: number = 0, topOffset: number = 0,
bottomOffset: number = 0, bottomOffset: number = 0,
): ProgressBarPosition => { ): Exclude<ProgressBarPosition, ProgressBarPosition.AUTO> => {
const isAutoVerticalEnabled = if (progressBarPosition !== ProgressBarPosition.AUTO) {
progressBarPositionAutoVertical !== ProgressBarPositionAutoVertical.OFF && return progressBarPosition;
progressBarPosition === ProgressBarPosition.BOTTOM;
const isVerticalSpaceLarger = window.innerHeight - topOffset - bottomOffset > window.innerWidth;
const shouldUseVerticalPosition = isAutoVerticalEnabled && isVerticalSpaceLarger;
if (shouldUseVerticalPosition) {
return progressBarPositionAutoVertical as unknown as ProgressBarPosition;
} }
return progressBarPosition; const isVerticalSpaceLarger = window.innerHeight - topOffset - bottomOffset > window.innerWidth;
if (isVerticalSpaceLarger) {
return progressBarPositionAutoVertical;
}
return ProgressBarPosition.BOTTOM;
}; };