import 'package:cutdex_ui/icons.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import '../../l10n/app_localizations.dart'; import 'resource_section.dart'; export 'resource_section.dart'; extension ResourceSectionPresentation on ResourceSection { String label(AppLocalizations l10n) => switch (this) { ResourceSection.imported => l10n.resourceMyMedia, ResourceSection.audio => l10n.audio, ResourceSection.text => l10n.text, ResourceSection.stickers => l10n.resourceStickers, ResourceSection.effects => l10n.resourceEffects, ResourceSection.transitions => l10n.resourceTransitions, }; IconData get icon => switch (this) { ResourceSection.imported => AppIcons.mediaLibrary, ResourceSection.audio => AppIcons.audio, ResourceSection.text => AppIcons.rename, ResourceSection.stickers => AppIcons.image, ResourceSection.effects => AppIcons.assistant, ResourceSection.transitions => AppIcons.splitVertical, }; } /// Desktop resource tabs share the timeline's scrolling and edge fades. class ResourceSectionTabs extends StatefulWidget { const ResourceSectionTabs({ super.key, required this.selected, required this.onSelected, }); final ResourceSection selected; final ValueChanged? onSelected; @override State createState() => _ResourceSectionTabsState(); } class _ResourceSectionTabsState extends State { final _scrollController = ScrollController(); final _selectedTabKey = GlobalKey(); @override void initState() { super.initState(); _showSelectedTab(); } @override void didUpdateWidget(covariant ResourceSectionTabs oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.selected != widget.selected) _showSelectedTab(); } void _showSelectedTab() { WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted) return; final selected = _selectedTabKey.currentContext; if (selected == null) return; if (_scrollController.hasClients) { final position = _scrollController.position; if (widget.selected == ResourceSection.values.first) { _scrollController.jumpTo(position.minScrollExtent); return; } if (widget.selected == ResourceSection.values.last) { _scrollController.jumpTo(position.maxScrollExtent); return; } } Scrollable.ensureVisible( selected, alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd, ); Scrollable.ensureVisible( selected, alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart, ); }); } void _onPointerSignal(PointerSignalEvent event) { if (event is! PointerScrollEvent || !_scrollController.hasClients) return; final delta = event.scrollDelta.dy != 0 ? event.scrollDelta.dy : event.scrollDelta.dx; if (delta == 0) return; final position = _scrollController.position; final target = (position.pixels + delta).clamp( position.minScrollExtent, position.maxScrollExtent, ); if (target == position.pixels) return; GestureBinding.instance.pointerSignalResolver.register(event, (_) { if (_scrollController.hasClients) _scrollController.jumpTo(target); }); } @override void dispose() { _scrollController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final l10n = AppLocalizations.of(context)!; return SizedBox( key: const Key('resource-section-tabs'), height: 36, child: Padding( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 5), child: ScrollEdgeFade( controller: _scrollController, axis: Axis.horizontal, child: Listener( onPointerSignal: _onPointerSignal, child: SingleChildScrollView( key: const Key('resource-tabs-list'), controller: _scrollController, scrollDirection: Axis.horizontal, child: Padding( padding: const EdgeInsets.symmetric(horizontal: 4), child: Row( children: [ for (final section in ResourceSection.values) ...[ if (section != ResourceSection.values.first) const SizedBox(width: 4), KeyedSubtree( key: widget.selected == section ? _selectedTabKey : null, child: Semantics( selected: widget.selected == section, child: TextButton( key: ValueKey('resource-tab-${section.name}'), style: AppTheme.timelineTabStyle( context, selected: widget.selected == section, ), onPressed: widget.onSelected == null ? null : () => widget.onSelected!(section), child: Text(section.label(l10n), maxLines: 1), ), ), ), ], ], ), ), ), ), ), ), ); } } class ResourceLibraryEmpty extends StatelessWidget { const ResourceLibraryEmpty({super.key, required this.section}); final ResourceSection section; @override Widget build(BuildContext context) { final l10n = AppLocalizations.of(context)!; final theme = Theme.of(context); return Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon( section.icon, size: 28, color: theme.colorScheme.onSurfaceVariant, ), const SizedBox(height: 12), Text( l10n.resourceLibraryEmpty(section.label(l10n)), textAlign: TextAlign.center, style: theme.textTheme.bodyMedium, ), if (section == ResourceSection.audio) ...[ const SizedBox(height: 6), Text( l10n.resourceImportedAudioHint, textAlign: TextAlign.center, style: theme.textTheme.bodySmall?.copyWith( color: theme.colorScheme.onSurfaceVariant, ), ), ], ], ), ), ); } }