import 'package:flutter/material.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import '../../l10n/l10n.dart'; import '../../workspace/project_state.dart'; import 'media_browser_body.dart'; import 'media_browser_controller.dart'; class MediaBrowserToolbar extends StatefulWidget { const MediaBrowserToolbar({ super.key, required this.canImport, required this.onImport, required this.filter, required this.onFilterChanged, required this.grid, required this.onToggleGrid, this.projects = const [], this.selectedProjectId, this.onProjectSelected, }); final bool canImport; final VoidCallback onImport; final MediaFilter filter; final ValueChanged onFilterChanged; final bool grid; final VoidCallback onToggleGrid; final List projects; final String? selectedProjectId; final ValueChanged? onProjectSelected; @override State createState() => _MediaBrowserToolbarState(); } class _MediaBrowserToolbarState extends State { Object? _filterConfiguration; Widget? _filterControls; bool get canImport => widget.canImport; VoidCallback get onImport => widget.onImport; MediaFilter get filter => widget.filter; ValueChanged get onFilterChanged => (value) => widget.onFilterChanged(value); bool get grid => widget.grid; VoidCallback get onToggleGrid => () => widget.onToggleGrid(); List get projects => widget.projects; String? get selectedProjectId => widget.selectedProjectId; ValueChanged? get onProjectSelected => widget.onProjectSelected; @override Widget build(BuildContext context) { final configuration = ( filter, grid, Theme.of(context), Localizations.localeOf(context), MediaQuery.textScalerOf(context), Directionality.of(context), ); if (_filterConfiguration != configuration) { _filterConfiguration = configuration; _filterControls = null; } final selected = projects .where((project) => project.id == selectedProjectId) .firstOrNull; Widget row(Key key, Widget child) => SizedBox( key: key, height: AppControlSize.panelToolbar, child: Padding( padding: const EdgeInsets.symmetric( horizontal: AppSidebarMetrics.outerPadding, vertical: AppSpacing.gap, ), child: child, ), ); return Column( key: const Key('media-panel-header'), mainAxisSize: MainAxisSize.min, children: [ row( const Key('media-project-row'), LayoutBuilder( builder: (context, constraints) { final painter = TextPainter( text: TextSpan( text: context.l10n.importMedia, style: AppTheme.toolbarTextStyle(context), ), textDirection: Directionality.of(context), textScaler: MediaQuery.textScalerOf(context), )..layout(); final compact = constraints.maxWidth < painter.width + 140; painter.dispose(); return Row( children: [ Expanded( child: AppDropdownMenu( enabled: onProjectSelected != null && projects.isNotEmpty, maxHeight: 320, items: [ for (final project in projects) AppMenuItem( key: ValueKey('media-project-${project.id}'), label: project.name, selected: project.id == selectedProjectId, selectOption: true, onPressed: onProjectSelected == null ? null : () => onProjectSelected!(project), ), ], builder: (context, toggle, focusNode) => AppToolbarDropdownButton( buttonKey: const Key('media-project-button'), focusNode: focusNode, onPressed: toggle, child: Row( children: [ Expanded( child: Text( selected?.name ?? context.l10n.selectProject, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), const SizedBox(width: AppSpacing.tiny), const Icon( Icons.keyboard_arrow_down, size: AppIconSize.header, ), ], ), ), ), ), const SizedBox(width: AppSpacing.tiny), if (compact) SizedBox.square( dimension: AppControlSize.headerButton, child: IconButton( key: const Key('media-import-button'), tooltip: context.l10n.importMedia, onPressed: canImport ? onImport : null, icon: const Icon(Icons.add, size: AppIconSize.header), ), ) else SizedBox( height: AppControlSize.headerButton, child: TextButton.icon( key: const Key('media-import-button'), style: AppTheme.toolbarButtonStyle(context), onPressed: canImport ? onImport : null, icon: const Icon(Icons.add, size: AppIconSize.header), label: Text(context.l10n.importMedia), ), ), ], ); }, ), ), _filterControls ??= row( const Key('media-filter-row'), Row( children: [ Expanded( child: Align( alignment: AlignmentDirectional.centerStart, child: SizedBox( width: 88, height: AppControlSize.headerButton, child: Semantics( label: context.l10n.mediaCategory, child: AppDropdownMenu( items: [ for (final option in MediaFilter.values) AppMenuItem( label: option.label(context), selected: option == filter, selectOption: true, onPressed: () => onFilterChanged(option), ), ], builder: (context, toggle, focus) => AppToolbarDropdownButton( buttonKey: const Key('media-category-button'), focusNode: focus, onPressed: toggle, child: Row( children: [ Expanded( child: Text( filter.label(context), maxLines: 1, overflow: TextOverflow.ellipsis, ), ), const SizedBox(width: AppSpacing.tiny), const Icon( Icons.keyboard_arrow_down, size: AppIconSize.header, ), ], ), ), ), ), ), ), ), const SizedBox(width: AppSpacing.tiny), SizedBox.square( dimension: AppControlSize.headerButton, child: IconButton( key: const Key('media-layout-toggle'), onPressed: onToggleGrid, icon: Icon( grid ? Icons.view_list_outlined : Icons.grid_view_outlined, size: AppIconSize.header, ), tooltip: grid ? context.l10n.listLayout : context.l10n.gridLayout, ), ), ], ), ), ], ); } }