Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
367 lines
13 KiB
Dart
367 lines
13 KiB
Dart
import '../../shared/widgets/listenable_selector.dart';
|
|
|
|
import 'package:cutdex/l10n/l10n.dart';
|
|
|
|
import 'package:cutdex_core/project.dart';
|
|
import 'package:cutdex_native_flutter/processing_contracts.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../integrations/runtime/processing_resource_repository.dart';
|
|
|
|
import 'package:cutdex_ui/theme.dart';
|
|
import 'package:cutdex_ui/widgets.dart';
|
|
|
|
import '../../shared/widgets/confirm_resource_download_cancel.dart';
|
|
import '../../shared/widgets/error_notification_host.dart';
|
|
import '../../shared/widgets/processing_resource_download_dialog.dart';
|
|
import '../../workspace/timeline_session.dart';
|
|
|
|
import 'processing_targets.dart';
|
|
import 'subtitle_download.dart';
|
|
export 'processing_targets.dart';
|
|
|
|
class TranscriptionDialog extends ConsumerStatefulWidget {
|
|
const TranscriptionDialog({
|
|
super.key,
|
|
required this.session,
|
|
this.asset,
|
|
this.target,
|
|
}) : assert(asset == null || target == null);
|
|
const TranscriptionDialog.forCurrentTask({
|
|
super.key,
|
|
required this.session,
|
|
this.asset,
|
|
this.target,
|
|
}) : assert(asset == null || target == null);
|
|
final EditorTimelineSession session;
|
|
final AssetDocument? asset;
|
|
final TranscriptionClipTarget? target;
|
|
@override
|
|
ConsumerState<TranscriptionDialog> createState() =>
|
|
_TranscriptionDialogState();
|
|
}
|
|
|
|
class _TranscriptionDialogState extends ConsumerState<TranscriptionDialog> {
|
|
String _translationSource = 'auto';
|
|
String _translationTarget = 'none';
|
|
bool _closing = false;
|
|
late final VoidCallback _release;
|
|
SubtitleStartProvider get _startProvider =>
|
|
subtitleStartProvider(widget.session);
|
|
SubtitleDownloadState get _download {
|
|
final global = ref.read(subtitleDownloadProvider);
|
|
final local = ref.read(_startProvider);
|
|
if (identical(global.request?.session, widget.session) && global.running) {
|
|
return global;
|
|
}
|
|
return local is SubtitleIdle ? global : local;
|
|
}
|
|
|
|
bool get _ownsDownload =>
|
|
identical(_download.request?.session, widget.session);
|
|
bool get _installingResource =>
|
|
_ownsDownload && _download is SubtitleDownloading;
|
|
bool get _picking => _ownsDownload && _download is SubtitleStarting;
|
|
bool get _resourcePaused => _ownsDownload
|
|
? switch (_download) {
|
|
SubtitleDownloading(:final paused) => paused,
|
|
_ => false,
|
|
}
|
|
: false;
|
|
double? get _resourceProgress => _ownsDownload
|
|
? switch (_download) {
|
|
SubtitleDownloading(:final progress) => progress,
|
|
_ => null,
|
|
}
|
|
: null;
|
|
ProcessingCapability get _resourceStage => _ownsDownload
|
|
? switch (_download) {
|
|
SubtitleDownloading(:final stage) => stage,
|
|
_ => ProcessingCapability.transcription,
|
|
}
|
|
: ProcessingCapability.transcription;
|
|
ProcessingResourceRepository? get _resourceRepository => _ownsDownload
|
|
? switch (_download) {
|
|
SubtitleDownloading(:final repository) => repository,
|
|
_ => null,
|
|
}
|
|
: null;
|
|
TranscriptionClipTarget? get _target =>
|
|
_installingResource ? _download.request?.target : widget.target;
|
|
AssetDocument? get _asset =>
|
|
_installingResource ? _download.request?.asset : widget.asset;
|
|
String? get _mediaId => _target?.mediaId ?? _asset?.id;
|
|
String? get _error => !_ownsDownload
|
|
? null
|
|
: switch (_download) {
|
|
SubtitleFailed(:final failure) => switch (failure) {
|
|
SubtitleStartFailure.start => context.l10n.startTranscriptionFailed,
|
|
SubtitleStartFailure.download =>
|
|
context.l10n.processingResourceDownloadFailed,
|
|
SubtitleStartFailure.engine =>
|
|
context.l10n.subtitleEngineUnavailable,
|
|
},
|
|
_ => null,
|
|
};
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_release = widget.session.retainViews();
|
|
if (_ownsDownload && _download.running) {
|
|
_translationSource = _download.request!.language;
|
|
_translationTarget = _download.request!.targetLanguage;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_release();
|
|
super.dispose();
|
|
}
|
|
|
|
void _dismissDialog() {
|
|
if (!mounted) return;
|
|
final route = ModalRoute.of(context);
|
|
if (route == null) return;
|
|
final navigator = Navigator.of(context);
|
|
if (route.isCurrent) {
|
|
navigator.pop();
|
|
} else {
|
|
navigator.removeRoute(route);
|
|
}
|
|
}
|
|
|
|
SubtitleRequest _request() => SubtitleRequest(
|
|
session: widget.session,
|
|
sequence: _target?.sequence ?? widget.session.activeSequenceId,
|
|
target: _target,
|
|
asset: _asset,
|
|
language: _translationSource,
|
|
targetLanguage: _translationTarget,
|
|
);
|
|
Future<void> _start() => ref.read(_startProvider.notifier).start(_request());
|
|
Future<void> _downloadResourceAndStart() {
|
|
ref.read(_startProvider.notifier).reset();
|
|
return ref
|
|
.read(subtitleDownloadProvider.notifier)
|
|
.start(_request(), download: true);
|
|
}
|
|
|
|
void _toggleResourceDownload() =>
|
|
ref.read(subtitleDownloadProvider.notifier).togglePause();
|
|
void _cancelResourceDownload() =>
|
|
ref.read(subtitleDownloadProvider.notifier).cancel();
|
|
|
|
String _translationLanguageLabel(String code) =>
|
|
context.l10n.subtitleLanguageName(code);
|
|
|
|
Widget _translationLanguageMenu({
|
|
required String label,
|
|
required String value,
|
|
required String other,
|
|
required ValueChanged<String> onChanged,
|
|
bool source = false,
|
|
bool enabled = true,
|
|
}) => AppField(
|
|
label: label,
|
|
child: AppSelect<String>(
|
|
emptyLabel: context.l10n.noMatchingOptions,
|
|
label: label,
|
|
value: value,
|
|
searchable: true,
|
|
searchHint: context.l10n.searchOptions(label),
|
|
onChanged: enabled ? onChanged : null,
|
|
options: [
|
|
if (source) ...[
|
|
AppSelectOption('auto', context.l10n.autoDetect),
|
|
] else
|
|
AppSelectOption('none', context.l10n.noTranslation),
|
|
for (final code in [
|
|
'zh',
|
|
'en',
|
|
'ja',
|
|
'ko',
|
|
if (source) 'yue',
|
|
...m2m100LanguageNames.keys.where(
|
|
(code) => !const {'zh', 'en', 'ja', 'ko'}.contains(code),
|
|
),
|
|
])
|
|
AppSelectOption(
|
|
code,
|
|
_translationLanguageLabel(code),
|
|
enabled: code != other,
|
|
searchTerms: [
|
|
code,
|
|
?subtitleNativeLanguageNames[code],
|
|
?m2m100LanguageNames[code],
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
Widget _buildModelDownloadDialog() {
|
|
final packages =
|
|
_resourceRepository?.catalog ?? defaultProcessingResourceCatalog;
|
|
final service = widget.session.transcriptions;
|
|
final downloadBytes = packages
|
|
.where(
|
|
(package) =>
|
|
ProcessingResourceRepository.subtitleCapabilities.contains(
|
|
package.capability,
|
|
) &&
|
|
!(package.capability == ProcessingCapability.transcription &&
|
|
service?.recognitionAvailable == true) &&
|
|
!(package.capability == ProcessingCapability.translation &&
|
|
service?.translationAvailable == true),
|
|
)
|
|
.fold<int>(0, (total, package) => total + package.bytes);
|
|
final totalBytes = packages
|
|
.where(
|
|
(package) => ProcessingResourceRepository.subtitleCapabilities
|
|
.contains(package.capability),
|
|
)
|
|
.fold<int>(0, (total, package) => total + package.bytes);
|
|
final progressTotal = _resourceRepository?.subtitleTotalBytes ?? totalBytes;
|
|
final downloadedBytes =
|
|
_resourceRepository?.subtitleDownloadedBytes ??
|
|
((_resourceProgress ?? 0) * progressTotal).round();
|
|
return ProcessingResourceDownloadDialog(
|
|
title: context.l10n.subtitleResourceDownloadTask,
|
|
description: context.l10n.subtitleResourcesRequired,
|
|
resourceName: _resourceStage == ProcessingCapability.transcription
|
|
? context.l10n.speechResourceStage
|
|
: context.l10n.translationResourceStage,
|
|
downloadSize: context.l10n.subtitleResourcesDownloadSize(
|
|
formatResourceBytes(downloadBytes),
|
|
),
|
|
downloading: _installingResource,
|
|
paused: _resourcePaused,
|
|
downloadedBytes: downloadedBytes,
|
|
totalBytes: progressTotal,
|
|
bytesPerSecond: _resourceRepository?.subtitleDownloadBytesPerSecond ?? 0,
|
|
error: _error,
|
|
downloadButtonKey: const Key('download-transcription-resource'),
|
|
pauseButtonKey: const Key('download-transcription-resource'),
|
|
backgroundButtonKey: const Key('background-transcription-resource'),
|
|
onCancel: () async {
|
|
if (_installingResource &&
|
|
!await confirmResourceDownloadCancellation(context)) {
|
|
return;
|
|
}
|
|
if (!mounted) return;
|
|
_cancelResourceDownload();
|
|
_dismissDialog();
|
|
},
|
|
onDownload: _downloadResourceAndStart,
|
|
onTogglePause: _toggleResourceDownload,
|
|
onBackground: _dismissDialog,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
ref.watch(subtitleDownloadProvider);
|
|
ref.watch(_startProvider);
|
|
void handleResult(SubtitleDownloadState next) {
|
|
if (_closing || !identical(next.request?.session, widget.session)) return;
|
|
if (next is SubtitleStarted || next is SubtitleCancelled) {
|
|
_closing = true;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
final notifications = ErrorNotificationHost.maybeOf(context);
|
|
final message = context.l10n.subtitleTaskStarted;
|
|
_dismissDialog();
|
|
if (next is SubtitleStarted) {
|
|
notifications?.showMessage(content: Text(message));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
ref.listen(subtitleDownloadProvider, (_, next) => handleResult(next));
|
|
ref.listen(_startProvider, (_, next) => handleResult(next));
|
|
return ListenableSelector<Object>(
|
|
listenable: widget.session,
|
|
select: () {
|
|
final service = widget.session.transcriptions;
|
|
return (
|
|
service?.recognitionAvailable,
|
|
service?.translationAvailable,
|
|
service?.tasks.any((task) => !task.terminal),
|
|
service?.translationTasks.any((task) => !task.terminal),
|
|
widget.session.acceptsEdits,
|
|
);
|
|
},
|
|
builder: (context, _) {
|
|
final service = widget.session.transcriptions;
|
|
if (service?.recognitionAvailable != true ||
|
|
service?.translationAvailable != true ||
|
|
_installingResource) {
|
|
return _buildModelDownloadDialog();
|
|
}
|
|
final busy =
|
|
(service?.tasks.any((task) => !task.terminal) ?? false) ||
|
|
(service?.translationTasks.any((task) => !task.terminal) ?? false);
|
|
return AppDialog(
|
|
title: _target == null
|
|
? context.l10n.transcribe
|
|
: context.l10n.generateCaptions,
|
|
withShadow: true,
|
|
width: 560,
|
|
actions: [
|
|
if (_mediaId != null && service != null)
|
|
FilledButton(
|
|
onPressed: busy || _picking || !widget.session.acceptsEdits
|
|
? null
|
|
: _start,
|
|
child: Text(context.l10n.generateCaptions),
|
|
),
|
|
],
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
_translationLanguageMenu(
|
|
label: context.l10n.sourceLanguage,
|
|
value: _translationSource,
|
|
other: _translationTarget,
|
|
source: true,
|
|
enabled: !busy && !_picking,
|
|
onChanged: (value) =>
|
|
setState(() => _translationSource = value),
|
|
),
|
|
const SizedBox(height: AppSpacing.compact),
|
|
_translationLanguageMenu(
|
|
label: context.l10n.translationLanguage,
|
|
value: _translationTarget,
|
|
other: _translationSource,
|
|
enabled:
|
|
service?.translationAvailable == true &&
|
|
!busy &&
|
|
!_picking,
|
|
onChanged: (value) =>
|
|
setState(() => _translationTarget = value),
|
|
),
|
|
if (service?.translationAvailable != true)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: AppSpacing.small),
|
|
child: Text(context.l10n.translationNotConfigured),
|
|
),
|
|
if (_error != null)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: AppSpacing.compact),
|
|
child: Text(_error!),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|