import 'package:flutter/material.dart'; import 'package:cutdex_ui/icons.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'; import 'resource_navigation.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, this.desktop = false, this.query = '', this.onQueryChanged, this.sort = MediaSort.importOrder, this.onSortChanged, this.projectScoped = false, this.resourceSection = ResourceSection.imported, this.onResourceSectionChanged, this.gridSize = MediaGridSize.medium, this.onGridSizeChanged, }); 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; final bool desktop; final bool projectScoped; final ResourceSection resourceSection; final ValueChanged? onResourceSectionChanged; final String query; final ValueChanged? onQueryChanged; final MediaSort sort; final ValueChanged? onSortChanged; final MediaGridSize gridSize; final ValueChanged? onGridSizeChanged; @override State createState() => _MediaBrowserToolbarState(); } class _MediaBrowserToolbarState extends State { Object? _filterConfiguration; Widget? _filterControls; final _search = TextEditingController(); @override void initState() { super.initState(); _search.text = widget.query; } @override void didUpdateWidget(covariant MediaBrowserToolbar oldWidget) { super.didUpdateWidget(oldWidget); if (_search.text != widget.query) _search.text = widget.query; } @override void dispose() { _search.dispose(); super.dispose(); } 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, widget.sort, widget.gridSize, widget.desktop, widget.projectScoped, canImport, 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; if (widget.desktop || AppTheme.isDesktop(context)) { return _desktopToolbar(context, selected); } Widget row(Key key, Widget child) => SizedBox( key: key, height: AppTheme.isDesktop(context) ? AppDesktopMetrics.panelHeader : AppControlSize.panelToolbar, child: Padding( padding: EdgeInsets.symmetric( horizontal: AppSidebarMetrics.outerPadding, vertical: AppTheme.isDesktop(context) ? AppSpacing.tiny : 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) => Row( children: [ Expanded( child: widget.projectScoped ? Text( context.l10n.materials, maxLines: 1, overflow: TextOverflow.ellipsis, style: Theme.of(context).textTheme.titleSmall, ) : 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( AppIcons.expandMore, size: AppIconSize.header, ), ], ), ), ), ), const SizedBox(width: AppSpacing.tiny), ConstrainedBox( constraints: BoxConstraints( maxWidth: (constraints.maxWidth * .45).clamp( 28, double.infinity, ), ), child: SizedBox( height: AppControlSize.headerButton, child: Tooltip( message: context.l10n.importMedia, child: TextButton( key: const Key('media-import-button'), style: AppTheme.toolbarButtonStyle(context), onPressed: canImport ? onImport : null, child: Text( context.l10n.importMedia, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), ), ), ), ], ), ), ), _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( AppIcons.expandMore, 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 ? AppIcons.listView : AppIcons.gridView, size: AppIconSize.header, ), tooltip: grid ? context.l10n.listLayout : context.l10n.gridLayout, ), ), ], ), ), ], ); } Widget _desktopToolbar(BuildContext context, RecentProject? selected) { Widget category() => Semantics( label: context.l10n.mediaCategory, child: AppDropdownMenu( items: [ for (final option in MediaFilter.values) AppMenuItem( key: ValueKey('media-filter-${option.name}'), 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 Icon(AppIcons.expandMore, size: 16), ], ), ), ), ); String sortLabel(MediaSort value) => switch (value) { MediaSort.importOrder => context.l10n.mediaSortImportOrder, MediaSort.nameAscending => context.l10n.mediaSortNameAscending, MediaSort.nameDescending => context.l10n.mediaSortNameDescending, }; return Column( key: const Key('media-panel-header'), mainAxisSize: MainAxisSize.min, children: [ if (widget.projectScoped) ResourceSectionTabs( selected: widget.resourceSection, onSelected: widget.onResourceSectionChanged, ) else SizedBox( key: const Key('media-project-row'), height: 36, child: Padding( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4), child: LayoutBuilder( builder: (context, constraints) => 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 Icon(AppIcons.expandMore, size: 16), ], ), ), ), ), const SizedBox(width: 4), ConstrainedBox( constraints: BoxConstraints( maxWidth: (constraints.maxWidth * .45).clamp( 28, double.infinity, ), ), child: SizedBox( height: 28, child: Tooltip( message: context.l10n.importMedia, child: TextButton( key: const Key('media-import-button'), style: AppTheme.toolbarButtonStyle(context), onPressed: canImport ? onImport : null, child: Text( context.l10n.importMedia, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), ), ), ), ], ), ), ), ), if (!widget.projectScoped || widget.resourceSection == ResourceSection.imported) _filterControls ??= _desktopControls(context, category, sortLabel), ], ); } Widget _desktopControls( BuildContext context, Widget Function() category, String Function(MediaSort) sortLabel, ) { List sortItems() => [ for (final value in MediaSort.values) AppMenuItem( key: ValueKey('media-sort-${value.name}'), label: sortLabel(value), selected: widget.sort == value, selectOption: true, onPressed: widget.onSortChanged == null ? null : () => widget.onSortChanged!(value), ), ]; String sizeLabel(MediaGridSize value) => switch (value) { MediaGridSize.small => context.l10n.mediaThumbnailSmall, MediaGridSize.medium => context.l10n.mediaThumbnailMedium, MediaGridSize.large => context.l10n.mediaThumbnailLarge, }; List sizeItems() => [ for (final value in MediaGridSize.values) AppMenuItem( key: ValueKey('media-size-${value.name}'), label: sizeLabel(value), selected: widget.gridSize == value, selectOption: true, onPressed: !grid || widget.onGridSizeChanged == null ? null : () => widget.onGridSizeChanged!(value), ), ]; Widget menuButton({ required Key key, required String tooltip, required IconData icon, required List items, }) => SizedBox.square( dimension: 28, child: AppDropdownMenu( items: items, builder: (context, toggle, focus) => IconButton( key: key, focusNode: focus, tooltip: tooltip, onPressed: toggle, padding: EdgeInsets.zero, icon: Icon(icon, size: 16), ), ), ); return SizedBox( key: const Key('media-filter-row'), height: 40, child: Padding( padding: const EdgeInsets.fromLTRB(12, 4, 12, 4), child: LayoutBuilder( builder: (context, constraints) { final wide = constraints.maxWidth >= 400; return Row( children: [ Expanded( child: TextField( key: const Key('media-search'), controller: _search, enabled: widget.onQueryChanged != null, style: AppTheme.inputTextStyle( context, style: AppTheme.toolbarTextStyle(context), ), cursorHeight: AppTheme.inputCursorHeight( context, style: AppTheme.toolbarTextStyle(context), ), textAlignVertical: TextAlignVertical.center, decoration: InputDecoration( hintText: context.l10n.searchOptions( context.l10n.materials, ), hintStyle: AppTheme.inputHintStyle( context, style: AppTheme.toolbarTextStyle(context), ), isDense: true, constraints: const BoxConstraints(minHeight: 32), contentPadding: const EdgeInsets.symmetric( horizontal: 8, vertical: 4, ), prefixIcon: constraints.maxWidth < 160 ? null : const Icon(AppIcons.search, size: 16), prefixIconConstraints: const BoxConstraints( minWidth: 28, minHeight: 28, ), ), onChanged: (value) => widget.onQueryChanged?.call(value), ), ), const SizedBox(width: 4), if (wide) ...[ SizedBox(width: 88, height: 28, child: category()), const SizedBox(width: 4), menuButton( key: const Key('media-sort-button'), tooltip: context.l10n.sortMedia, icon: AppIcons.sort, items: sortItems(), ), const SizedBox(width: 4), menuButton( key: const Key('media-size-button'), tooltip: context.l10n.mediaThumbnailSize, icon: AppIcons.gridView, items: sizeItems(), ), ] else menuButton( key: const Key('media-category-button'), tooltip: MaterialLocalizations.of(context).showMenuTooltip, icon: AppIcons.more, items: [ AppMenuLabel(context.l10n.mediaCategory), for (final option in MediaFilter.values) AppMenuItem( key: ValueKey('media-filter-${option.name}'), label: option.label(context), selected: option == filter, selectOption: true, onPressed: () => onFilterChanged(option), ), const AppMenuSeparator(), AppMenuLabel(context.l10n.sortMedia), ...sortItems(), const AppMenuSeparator(), AppMenuLabel(context.l10n.mediaThumbnailSize), ...sizeItems(), ], ), const SizedBox(width: 4), SizedBox.square( dimension: 28, child: IconButton( key: const Key('media-layout-toggle'), onPressed: onToggleGrid, padding: EdgeInsets.zero, icon: Icon( grid ? AppIcons.listView : AppIcons.gridView, size: 16, ), tooltip: grid ? context.l10n.listLayout : context.l10n.gridLayout, ), ), if (widget.projectScoped) ...[ const SizedBox(width: 4), SizedBox.square( dimension: 28, child: IconButton( key: const Key('media-import-button'), onPressed: canImport ? () => onImport() : null, tooltip: context.l10n.importMedia, padding: EdgeInsets.zero, icon: const Icon(AppIcons.add, size: 16), ), ), ], ], ); }, ), ), ); } }