Skip to content

The swiss army knife of lossless video/audio editing

improve dialog focus

Mikael Finstad251117e6a4da
diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx
index ad4462e5..99bbce87 100644
--- a/src/renderer/src/App.tsx
+++ b/src/renderer/src/App.tsx
@@ -1605,7 +1605,7 @@ function App() {
const extractAllStreams = useCallback(async () => {
if (!filePath) return;
- if (!(await confirmDialog({ description: t('Please confirm that you want to extract all tracks as separate files'), confirmButtonText: t('Extract all tracks') }))) return;
+ if (!(await confirmDialog({ focusConfirm: true, description: t('Please confirm that you want to extract all tracks as separate files'), confirmButtonText: t('Extract all tracks') }))) return;
if (workingRef.current) return;
try {
diff --git a/src/renderer/src/components/GenericDialog.tsx b/src/renderer/src/components/GenericDialog.tsx
index 255d4572..77a14aae 100644
--- a/src/renderer/src/components/GenericDialog.tsx
+++ b/src/renderer/src/components/GenericDialog.tsx
@@ -14,7 +14,7 @@ import { saveColor, warningColor } from '../colors';
export interface GenericDialogParams {
isAlert?: boolean;
- content: React.ReactNode;
+ render: () => React.ReactNode;
onClose?: () => void;
}
@@ -38,34 +38,30 @@ export default function GenericDialog({ dialog, onOpenChange }: {
}) {
const context = useMemo(() => ({ onOpenChange }), [onOpenChange]);
- if (dialog == null) {
- return null;
- }
+ return (
+ <>
+ {/* Non-alert dialog */ }
+ <Dialog.Root open={dialog != null && !dialog.isAlert} onOpenChange={onOpenChange}>
+ <Dialog.Portal>
+ <Dialog.Overlay />
+
+ <GenericDialogContext.Provider value={context}>
+ {dialog != null && !dialog.isAlert && dialog.render()}
+ </GenericDialogContext.Provider>
+ </Dialog.Portal>
+ </Dialog.Root>
- if (dialog.isAlert) {
- return (
- <AlertDialog.Root open={dialog != null} onOpenChange={onOpenChange}>
+ {/* Alert dialog */ }
+ <AlertDialog.Root open={dialog != null && !!dialog.isAlert} onOpenChange={onOpenChange}>
<AlertDialog.Portal>
<AlertDialog.Overlay />
<GenericDialogContext.Provider value={context}>
- {dialog.content}
+ {dialog != null && dialog.isAlert && dialog.render()}
</GenericDialogContext.Provider>
</AlertDialog.Portal>
</AlertDialog.Root>
- );
- }
-
- return (
- <Dialog.Root open={dialog != null} onOpenChange={onOpenChange}>
- <Dialog.Portal>
- <Dialog.Overlay />
-
- <GenericDialogContext.Provider value={context}>
- {dialog?.content}
- </GenericDialogContext.Provider>
- </Dialog.Portal>
- </Dialog.Root>
+ </>
);
}
@@ -88,14 +84,16 @@ export function useDialog() {
setGenericDialog(undefined);
}, []);
- const confirmDialog = useCallback(({ title = t('Please confirm'), description, confirmButtonText = t('Confirm'), cancelButtonText = t('Cancel') }: {
+ const confirmDialog = useCallback(({ title = t('Please confirm'), description, confirmButtonText = t('Confirm'), cancelButtonText = t('Cancel'), focusConfirm = false }: {
title?: ReactNode,
description?: string,
confirmButtonText?: string,
cancelButtonText?: string,
+ focusConfirm?: boolean,
}) => new Promise<boolean>((resolve) => {
function ConfirmDialog() {
const { onOpenChange } = useGenericDialogContext();
+ const confirmRef = useRef<HTMLButtonElement>(null);
const handleConfirmClick = useCallback<MouseEventHandler<HTMLButtonElement>>((e) => {
e.preventDefault();
@@ -103,8 +101,15 @@ export function useDialog() {
onOpenChange(false);
}, [onOpenChange]);
+ const handleOpenAutoFocus = useCallback((e: Event) => {
+ if (focusConfirm && confirmRef.current) {
+ e.preventDefault();
+ confirmRef.current.focus();
+ }
+ }, []);
+
return (
- <AlertDialog.Content aria-describedby={description} style={{ width: '40vw' }}>
+ <AlertDialog.Content aria-describedby={description} style={{ width: '40vw' }} onOpenAutoFocus={handleOpenAutoFocus}>
<AlertDialog.Title>{title}</AlertDialog.Title>
{description && <AlertDialog.Description>{description}</AlertDialog.Description>}
@@ -114,7 +119,7 @@ export function useDialog() {
<DialogButton>{cancelButtonText}</DialogButton>
</AlertDialog.Cancel>
- <DialogButton primary onClick={handleConfirmClick}>{confirmButtonText}</DialogButton>
+ <DialogButton primary onClick={handleConfirmClick} ref={confirmRef}>{confirmButtonText}</DialogButton>
</Dialog.ButtonRow>
</AlertDialog.Content>
);
@@ -122,7 +127,7 @@ export function useDialog() {
showGenericDialog({
isAlert: true,
- content: <ConfirmDialog />,
+ render: () => <ConfirmDialog />,
onClose: () => resolve(false),
});
}), [showGenericDialog, t]);
@@ -160,7 +165,7 @@ export function useDialog() {
}
showGenericDialog({
- content: <ExportFinishedDialog />,
+ render: () => <ExportFinishedDialog />,
onClose: () => resolve(false),
});
});
@@ -237,15 +242,27 @@ export function useDialog() {
onOpenChange(false);
}, [choices, onOpenChange]);
+ // for convenience, focus confirm button when opening dialog
+ // they probably just want to use current options
+ // https://github.com/mifi/lossless-cut/issues/2622
+ const confirmRef = useRef<HTMLButtonElement>(null);
+
+ const handleOpenAutoFocus = useCallback((e: Event) => {
+ if (confirmRef.current) {
+ e.preventDefault();
+ confirmRef.current.focus();
+ }
+ }, []);
+
return (
- <AlertDialog.Content aria-describedby={undefined} style={{ width: '80vw' }}>
- <AlertDialog.Title>
+ <Dialog.Content aria-describedby={undefined} style={{ width: '80vw' }} onOpenAutoFocus={handleOpenAutoFocus}>
+ <Dialog.Title>
{t('Cleanup files?')}
- </AlertDialog.Title>
+ </Dialog.Title>
- <AlertDialog.Description>
+ <Dialog.Description>
{t('What do you want to do after exporting a file or when pressing the "delete source file" button?')}
- </AlertDialog.Description>
+ </Dialog.Description>
<Checkbox label={t('Close currently opened file')} checked={closeFile} disabled={trashSourceFile || trashTmpFiles} onCheckedChange={(checked) => onChange('closeFile', checked)} />
@@ -262,19 +279,18 @@ export function useDialog() {
</div>
<Dialog.ButtonRow>
- <AlertDialog.Cancel asChild>
+ <Dialog.Close asChild>
<DialogButton>{t('Cancel')}</DialogButton>
- </AlertDialog.Cancel>
+ </Dialog.Close>
- <DialogButton onClick={handleOkClick} primary>{t('Confirm')}</DialogButton>
+ <DialogButton ref={confirmRef} onClick={handleOkClick} primary>{t('Confirm')}</DialogButton>
</Dialog.ButtonRow>
- </AlertDialog.Content>
+ </Dialog.Content>
);
}
showGenericDialog({
- isAlert: true,
- content: <CleanupChoices />,
+ render: () => <CleanupChoices />,
onClose: () => resolve(undefined),
});
});
diff --git a/src/renderer/src/hooks/useHtml5ify.tsx b/src/renderer/src/hooks/useHtml5ify.tsx
index 85684793..7381bee5 100644
--- a/src/renderer/src/hooks/useHtml5ify.tsx
+++ b/src/renderer/src/hooks/useHtml5ify.tsx
@@ -11,7 +11,7 @@ import { SetWorking } from './useLoading';
import { WithErrorHandling } from './useErrorHandling';
import { FfmpegOperations } from './useFfmpegOperations';
import { ShowGenericDialog, useGenericDialogContext } from '../components/GenericDialog';
-import * as AlertDialog from '../components/AlertDialog';
+import * as Dialog from '../components/Dialog';
import { ButtonRow } from '../components/Dialog';
import { DialogButton } from '../components/Button';
@@ -99,20 +99,20 @@ export default function useHtml5ify({ filePath, hasVideo, hasAudio, workingRef,
const onRememberChange = useCallback((checked: boolean) => setRemember(checked), []);
- const handleOkClick = useCallback(() => {
+ const handleConfirmClick = useCallback(() => {
resolve({ selectedOption: option, rememberChoice: remember });
onOpenChange(false);
}, [onOpenChange, option, remember]);
return (
- <AlertDialog.Content aria-describedby={undefined} style={{ width: '80vw' }}>
- <AlertDialog.Title>
+ <Dialog.Content aria-describedby={undefined} style={{ width: '80vw' }}>
+ <Dialog.Title>
{i18n.t('Convert to supported format')}
- </AlertDialog.Title>
+ </Dialog.Title>
- <AlertDialog.Description>
+ <Dialog.Description>
{i18n.t('These options will let you convert files to a format that is supported by the player. You can try different options and see which works with your file. Note that the conversion is for preview only. When you run an export, the output will still be lossless with full quality')}
- </AlertDialog.Description>
+ </Dialog.Description>
{Object.entries(inputOptions).map(([value, label]) => {
const id = `html5ify-${value}`;
@@ -135,19 +135,18 @@ export default function useHtml5ify({ filePath, hasVideo, hasAudio, workingRef,
{showRemember && <Checkbox checked={remember} onCheckedChange={onRememberChange} label={t('Use this for all files until LosslessCut is restarted?')} style={{ marginTop: '.5em' }} />}
<ButtonRow>
- <AlertDialog.Cancel asChild>
+ <Dialog.Close asChild>
<DialogButton>{t('Cancel')}</DialogButton>
- </AlertDialog.Cancel>
+ </Dialog.Close>
- <DialogButton onClick={handleOkClick} primary>{t('OK')}</DialogButton>
+ <DialogButton onClick={handleConfirmClick} primary>{t('OK')}</DialogButton>
</ButtonRow>
- </AlertDialog.Content>
+ </Dialog.Content>
);
}
showGenericDialog({
- isAlert: true,
- content: <AskForHtml5ifySpeed />,
+ render: () => <AskForHtml5ifySpeed />,
onClose: () => resolve(undefined),
});
});
diff --git a/src/renderer/src/hooks/useSegments.tsx b/src/renderer/src/hooks/useSegments.tsx
index e09fd9f6..2c491646 100644
--- a/src/renderer/src/hooks/useSegments.tsx
+++ b/src/renderer/src/hooks/useSegments.tsx
@@ -24,7 +24,7 @@ import { ShowGenericDialog, useGenericDialogContext } from '../components/Generi
import ExpressionDialog from '../components/ExpressionDialog';
import Button, { DialogButton } from '../components/Button';
import { ButtonRow } from '../components/Dialog';
-import * as AlertDialog from '../components/AlertDialog';
+import * as Dialog from '../components/Dialog';
import { UserFacingError } from '../../errors';
const remote = window.require('@electron/remote');
@@ -238,7 +238,7 @@ function useSegments({ filePath, workingRef, setWorking, setProgress, videoStrea
parameters: ParameterDialogParameters,
docUrl?: string,
}) => new Promise<ParameterDialogParameters | undefined>((resolve) => {
- function Dialog() {
+ function ParametersDialog() {
const { onOpenChange } = useGenericDialogContext();
const firstInputRef = useRef<HTMLInputElement>(null);
@@ -259,10 +259,10 @@ function useSegments({ filePath, workingRef, setWorking, setProgress, videoStrea
}, []);
return (
- <AlertDialog.Content aria-describedby={undefined} style={{ width: '80vw' }}>
- <AlertDialog.Title>{title}</AlertDialog.Title>
+ <Dialog.Content aria-describedby={undefined} style={{ width: '80vw' }}>
+ <Dialog.Title>{title}</Dialog.Title>
- <AlertDialog.Description>{description}</AlertDialog.Description>
+ <Dialog.Description>{description}</Dialog.Description>
{docUrl && <p><Button onClick={() => shell.openExternal(docUrl)}><FaLink style={{ fontSize: '.8em' }} /> {t('Read more')}</Button></p>}
@@ -285,20 +285,19 @@ function useSegments({ filePath, workingRef, setWorking, setProgress, videoStrea
})}
<ButtonRow>
- <AlertDialog.Cancel asChild>
+ <Dialog.Close asChild>
<DialogButton>{t('Cancel')}</DialogButton>
- </AlertDialog.Cancel>
+ </Dialog.Close>
<DialogButton type="submit" primary>{t('Confirm')}</DialogButton>
</ButtonRow>
</form>
- </AlertDialog.Content>
+ </Dialog.Content>
);
}
showGenericDialog({
- isAlert: true,
- content: <Dialog />,
+ render: () => <ParametersDialog />,
onClose: () => resolve(undefined),
});
}), [showGenericDialog, t]);
@@ -779,7 +778,7 @@ function useSegments({ filePath, workingRef, setWorking, setProgress, videoStrea
showGenericDialog({
isAlert: true,
- content: (
+ render: () => (
<ExpressionDialog
onSubmit={onSubmit}
confirmButtonText={t('Select segments')}
@@ -847,7 +846,7 @@ function useSegments({ filePath, workingRef, setWorking, setProgress, videoStrea
showGenericDialog({
isAlert: true,
- content: (
+ render: () => (
<ExpressionDialog
onSubmit={onSubmit}
confirmButtonText={t('Apply change')}
diff --git a/src/renderer/src/hooks/useStreamsMeta.tsx b/src/renderer/src/hooks/useStreamsMeta.tsx
index 0ceb2e76..23eee50c 100644
--- a/src/renderer/src/hooks/useStreamsMeta.tsx
+++ b/src/renderer/src/hooks/useStreamsMeta.tsx
@@ -94,7 +94,7 @@ export default function useStreamsMeta({ mainStreams, externalFilesMeta, filePat
showGenericDialog({
isAlert: true,
- content: (
+ render: () => (
<ExpressionDialog
confirmButtonText={t('Apply filter')}
onSubmit={async (value: string) => {
diff --git a/src/renderer/src/hooks/useTimecode.tsx b/src/renderer/src/hooks/useTimecode.tsx
index ce9e7a0f..e9995e2f 100644
--- a/src/renderer/src/hooks/useTimecode.tsx
+++ b/src/renderer/src/hooks/useTimecode.tsx
@@ -1,4 +1,4 @@
-import { FormEventHandler, useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import { FormEventHandler, useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { FormatTimecode, ParseTimecode } from '../types';
@@ -7,7 +7,7 @@ import { getFrameDuration } from '../util';
import { TimecodeFormat } from '../../../common/types';
import { formatDuration, parseDuration } from '../util/duration';
import { ShowGenericDialog, useGenericDialogContext } from '../components/GenericDialog';
-import * as AlertDialog from '../components/AlertDialog';
+import * as Dialog from '../components/Dialog';
import TextInput from '../components/TextInput';
import { ButtonRow } from '../components/Dialog';
import { DialogButton } from '../components/Button';
@@ -97,22 +97,15 @@ export default ({ detectedFps, timecodeFormat, showGenericDialog }: {
}
}, [onOpenChange, t, value]);
- const valueRef = useRef<HTMLInputElement>(null);
-
- useEffect(() => {
- valueRef.current?.focus();
- }, []);
-
return (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
- <AlertDialog.Content aria-describedby={undefined} style={{ width: '80vw' }}>
- <AlertDialog.Title>{title}</AlertDialog.Title>
+ <Dialog.Content aria-describedby={undefined} style={{ width: '80vw' }}>
+ <Dialog.Title>{title}</Dialog.Title>
- {description && <AlertDialog.Description>{description}</AlertDialog.Description>}
+ {description && <Dialog.Description>{description}</Dialog.Description>}
<form onSubmit={handleSubmit}>
<TextInput
- ref={valueRef}
value={value}
placeholder={inputPlaceholder}
onChange={(e) => setValue(e.target.value)}
@@ -126,20 +119,19 @@ export default ({ detectedFps, timecodeFormat, showGenericDialog }: {
)}
<ButtonRow>
- <AlertDialog.Cancel asChild>
+ <Dialog.Close asChild>
<DialogButton>{t('Cancel')}</DialogButton>
- </AlertDialog.Cancel>
+ </Dialog.Close>
<DialogButton type="submit" primary>{t('Go')}</DialogButton>
</ButtonRow>
</form>
- </AlertDialog.Content>
+ </Dialog.Content>
);
}
showGenericDialog({
- isAlert: true,
- content: <TimecodeDialog />,
+ render: () => <TimecodeDialog />,
onClose: () => resolve(undefined),
});
}), [parseTimecode, showGenericDialog]);