Files
cutdex/lib/features/media/background_removal_dialog.dart
T
lingjhf abc1868550
Gitea CI / Check code and tests (push) Successful in 6m37s
Gitea CI / Build Windows x64 app (push) Successful in 3m18s
Gitea CI / Build Android APK (push) Successful in 3m47s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m7s
Gitea CI / Assemble and validate CI artifacts (push) Successful in 50s
Gitea CI / Gitea CI passed (push) Successful in 2s
Gitea Release / Prepare draft Release (push) Successful in 9s
Gitea Release / Upload Android to draft Release (push) Successful in 16s
Gitea Release / Upload Windows to draft Release (push) Successful in 22s
Gitea Release / Verify and publish Release (push) Successful in 14s
feat(workspace): 桌面工作区、主题与交互更新,发布 0.1.2
2026-10-08 18:17:12 +00:00

204 lines
7.7 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/error_notification_host.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 notifications = ErrorNotificationHost.maybeOf(context);
final message = context.l10n.backgroundRemovalTaskStarted;
Navigator.of(context).pop();
notifications?.showMessage(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),
],
],
),
);
},
),
);
}
}