Files
cutdex/lib/features/media/background_removal_dialog.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

203 lines
7.6 KiB
Dart

import 'package:cutdex/l10n/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../integrations/runtime/processing_configuration.dart';
import '../../integrations/runtime/processing_resource_repository.dart';
import '../../integrations/runtime/processing_resource_state.dart';
import 'package:cutdex_ui/theme.dart';
import 'package:cutdex_ui/widgets.dart';
import '../../shared/widgets/confirm_resource_download_cancel.dart';
import '../../shared/widgets/processing_resource_download_dialog.dart';
import '../../workspace/timeline_session.dart';
import 'processing_targets.dart';
import 'background_removal_controller.dart';
export 'processing_targets.dart';
class BackgroundRemovalDialog extends ConsumerStatefulWidget {
const BackgroundRemovalDialog({
super.key,
required this.session,
required this.target,
});
final EditorTimelineSession session;
final BackgroundRemovalClipTarget target;
@override
ConsumerState<BackgroundRemovalDialog> createState() =>
_BackgroundRemovalDialogState();
}
class _BackgroundRemovalDialogState
extends ConsumerState<BackgroundRemovalDialog> {
late final VoidCallback _release;
bool _closing = false;
BackgroundRemovalControllerProvider get _provider =>
backgroundRemovalControllerProvider(widget.session, widget.target);
BackgroundRemovalState get _state => ref.read(_provider);
bool get _installing => _state is RemovalDownloading;
bool get _starting => _state is RemovalStarting;
String? get _error => switch (_state) {
RemovalFailed(:final failure, :final message) => switch (failure) {
BackgroundRemovalFailure.download =>
context.l10n.processingResourceDownloadFailed,
BackgroundRemovalFailure.start =>
context.l10n.startBackgroundRemovalFailed,
BackgroundRemovalFailure.rejected => message,
},
_ => null,
};
@override
void initState() {
super.initState();
_release = widget.session.retainViews();
}
@override
void dispose() {
_release();
super.dispose();
}
Future<void> _start(ProcessingResourceRepository repository) =>
ref.read(_provider.notifier).start(repository);
Future<void> _downloadAndStart(ProcessingResourceRepository repository) =>
ref.read(_provider.notifier).start(repository, download: true);
@override
Widget build(BuildContext context) {
ref.watch(_provider);
ref.listen(_provider, (previous, next) {
if (next is! RemovalStarted || _closing) return;
_closing = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
final messenger = ScaffoldMessenger.maybeOf(context);
final message = context.l10n.backgroundRemovalTaskStarted;
Navigator.of(context).pop();
messenger?.showSnackBar(SnackBar(content: Text(message)));
});
});
return ref
.watch(processingResourceRepositoryProvider)
.when(
loading: () => const DelayedLoadingIndicator(
child: AppDialog(
title: '',
width: 520,
child: Center(child: CircularProgressIndicator()),
),
),
error: (_, _) => AppDialog(
title: context.l10n.removeBackground,
width: 520,
child: Text(context.l10n.loadResourceLocationFailed),
),
data: (repository) => Consumer(
builder: (context, ref, _) {
final resources = ref.watch(
processingResourcesProvider(repository),
);
final install =
resources.installs
.where(
(item) =>
item.package.capability ==
ProcessingCapability.backgroundRemoval,
)
.firstOrNull ??
repository.installFor(ProcessingCapability.backgroundRemoval);
final installed =
install.state == ProcessingResourceState.installed ||
ProcessingConfiguration.configured(
resourcesRoot: repository.root.parent.path,
)?.backgroundRemovalModel !=
null;
final downloading =
install.state == ProcessingResourceState.downloading;
if (!installed) {
return ProcessingResourceDownloadDialog(
title: context.l10n.backgroundRemovalDownloadTask,
description: context.l10n.backgroundRemovalResourceRequired,
resourceName: context.l10n.backgroundRemovalResourceName,
downloadSize: context.l10n.subtitleResourcesDownloadSize(
formatResourceBytes(install.package.bytes),
),
downloading: downloading,
paused: resources.paused.contains(
ProcessingCapability.backgroundRemoval,
),
downloadedBytes: install.downloadedBytes,
totalBytes: install.package.bytes,
bytesPerSecond:
resources.rates[ProcessingCapability.backgroundRemoval] ??
0,
error: _error,
downloadButtonKey: const Key(
'download-background-removal-resource',
),
pauseButtonKey: const Key(
'pause-background-removal-resource',
),
backgroundButtonKey: const Key(
'background-background-removal-resource',
),
onCancel: () async {
if (downloading &&
!await confirmResourceDownloadCancellation(context)) {
return;
}
if (!mounted || !context.mounted) return;
if (downloading) {
ref.read(_provider.notifier).cancel(repository);
}
Navigator.of(context).pop();
},
onDownload: () {
if (!_installing && !downloading) {
_downloadAndStart(repository);
}
},
onTogglePause: () =>
ref.read(_provider.notifier).togglePause(repository),
onBackground: () => Navigator.of(context).pop(),
);
}
return AppDialog(
title: context.l10n.removeBackground,
width: 520,
description: context.l10n.backgroundRemovalDestructiveHint,
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(context.l10n.close),
),
FilledButton(
onPressed:
_installing || _starting || !widget.session.acceptsEdits
? null
: () => _start(repository),
child: Text(context.l10n.startBackgroundRemoval),
),
],
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
if (_error case final error?) ...[
const SizedBox(height: AppSpacing.compact),
Text(error),
],
],
),
);
},
),
);
}
}