refactor(media): compact browsing controls and isolate asset row updates

This commit is contained in:
lingjhf committed 2026-10-09 19:23:49 +08:00
1 parent 1acfe4a005
commit 107e93b959
8 files changed
+287 -257

No files matched your search

@@ -24,12 +24,23 @@ class DesktopMediaAssetTile extends StatelessWidget {
final MediaAvailability? availability;
static TextStyle? _nameStyle(BuildContext context) =>
Theme.of(context).textTheme.bodyMedium
?.copyWith(fontSize: 13, height: 20 / 13);
Theme.of(context).textTheme.bodyMedium?.copyWith(
fontFamily: 'packages/cutdex_ui/Inter',
fontSize: AppInspectorMetrics.fontSize,
height: AppInspectorMetrics.lineHeight / AppInspectorMetrics.fontSize,
fontWeight: FontWeight.w500,
fontVariations: const [FontVariation('wght', 500)],
letterSpacing: AppInspectorMetrics.fontSize * .005,
);
static TextStyle? _metadataStyle(BuildContext context) =>
Theme.of(context).textTheme.bodySmall
?.copyWith(fontSize: 12, height: 16 / 12);
Theme.of(context).textTheme.bodySmall?.copyWith(
fontFamily: 'packages/cutdex_ui/Inter',
fontSize: AppInspectorMetrics.fontSize,
height: AppInspectorMetrics.lineHeight / AppInspectorMetrics.fontSize,
fontVariations: const [FontVariation('wght', 450)],
letterSpacing: AppInspectorMetrics.fontSize * .005,
);
/// Font fallback and fractional scaling can round a line above size × height.
static double gridLabelHeight(BuildContext context) {
@@ -106,7 +117,7 @@ class DesktopMediaAssetTile extends StatelessWidget {
);
final cover = ClipRRect(
key: ValueKey('desktop-media-cover-${asset.id}'),
borderRadius: BorderRadius.circular(4),
borderRadius: BorderRadius.circular(AppInspectorMetrics.radius),
child: Stack(
children: [
if (unavailable)
@@ -215,66 +226,3 @@ class DesktopMediaAssetTile extends StatelessWidget {
);
}
}
/// List labels follow the same column widths and breakpoints as asset rows.
class DesktopMediaListHeader extends StatelessWidget {
const DesktopMediaListHeader({super.key});
@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.symmetric(
horizontal: DesktopMediaMetrics.padding + DesktopMediaMetrics.tilePadding,
),
child: LayoutBuilder(
builder: (context, constraints) {
final columns = DesktopMediaListColumns(constraints.maxWidth);
Widget label(String text, {bool end = false}) => Text(
text,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: end ? TextAlign.end : TextAlign.start,
style: DesktopMediaAssetTile._metadataStyle(context)
?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant),
);
return SizedBox(
height: (MediaQuery.textScalerOf(context).scale(16) + 8).clamp(
DesktopMediaMetrics.listHeader,
double.infinity,
),
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsetsDirectional.only(
start:
DesktopMediaMetrics.listCover + DesktopMediaMetrics.gap,
),
child: label(context.l10n.name),
),
),
if (columns.duration)
SizedBox(
width: DesktopMediaListColumns.durationWidth,
child: label(context.l10n.duration, end: true),
),
if (columns.specification) ...[
const SizedBox(width: DesktopMediaMetrics.gap),
SizedBox(
width: DesktopMediaListColumns.specificationWidth,
child: label(context.l10n.mediaSpecification),
),
],
if (columns.kind) ...[
const SizedBox(width: DesktopMediaMetrics.gap),
SizedBox(
width: DesktopMediaListColumns.kindWidth,
child: label(context.l10n.mediaCategory),
),
],
],
),
);
},
),
);
}
+105 -104
View File
@@ -298,6 +298,95 @@ class _MediaBrowserBodyState extends ConsumerState<MediaBrowserBody>
_anchorOwner = null;
_anchorAssets = const [];
}
Widget item(BuildContext context, int index) {
final asset = assets[index];
return Consumer(
builder: (context, ref, _) {
final availability = ref
.watch(mediaAvailabilityProvider(widget.projectId, asset))
.asData
?.value;
final unavailable =
availability != null &&
availability != MediaAvailability.available;
return ListenableSelector<(bool, bool)>(
listenable: _browser,
select: () => (_selectedAssetId == asset.id, _checkingDrop),
builder: (context, presentation) => AppContextMenu(
key: ValueKey(('media-menu', asset.id)),
itemsBuilder: (_) {
final available = ref.read(projectsProvider).asData?.value;
final canEdit =
available?.activeProject?.id == widget.projectId &&
available?.projectBusy == false &&
!_checkingDrop;
final sequence = timeline?.activeSequenceId;
return [
AppMenuItem(
label: context.l10n.addToTimeline,
icon: AppIcons.add,
onPressed:
canEdit &&
!unavailable &&
sequence != null &&
available?.timeline?.id == timelineId
? () => _action(() => _browser.insertAsset(asset))
: null,
),
AppMenuItem(
label: context.l10n.rename,
icon: AppIcons.rename,
onPressed: canEdit ? () => _rename(asset) : null,
),
AppMenuItem(
label: defaultTargetPlatform == TargetPlatform.macOS
? context.l10n.revealFinder
: context.l10n.revealFileManager,
icon: AppIcons.folderOpen,
onPressed: canEdit && _desktop
? () => _action(() => _browser.reveal(asset))
: null,
),
const AppMenuSeparator(),
AppMenuItem(
label: context.l10n.removeMedia,
icon: AppIcons.delete,
destructive: true,
onPressed: canEdit ? () => _remove(asset) : null,
),
];
},
child: Listener(
onPointerDown: (event) {
if (event.buttons == 2) {
_selectedAssetId = asset.id;
}
},
child: _SelectableAsset(
key: ValueKey(asset.id),
grid: _isGrid,
desktop: widget.desktop,
drag: MediaAssetDrag(
projectId: widget.projectId,
assetId: asset.id,
),
name: asset.name,
draggable:
widget.draggable &&
!workspace!.projectBusy &&
!presentation.$2 &&
!unavailable,
selected: presentation.$1,
onTap: () => _browser.selectAsset(asset),
child: _assetTile(asset, availability),
),
),
),
);
},
);
}
final content = assets.isEmpty
? Center(
child: Padding(
@@ -382,105 +471,6 @@ class _MediaBrowserBodyState extends ConsumerState<MediaBrowserBody>
height: constraints.maxHeight,
);
}
Widget item(BuildContext context, int index) {
final asset = assets[index];
return Consumer(
builder: (context, ref, _) {
final availability = ref
.watch(
mediaAvailabilityProvider(widget.projectId, asset),
)
.asData
?.value;
final unavailable =
availability != null &&
availability != MediaAvailability.available;
return ListenableSelector<(bool, bool)>(
listenable: _browser,
select: () =>
(_selectedAssetId == asset.id, _checkingDrop),
builder: (context, presentation) => AppContextMenu(
key: ValueKey(('media-menu', asset.id)),
itemsBuilder: (_) {
final available = ref
.read(projectsProvider)
.asData
?.value;
final canEdit =
available?.activeProject?.id ==
widget.projectId &&
available?.projectBusy == false &&
!_checkingDrop;
final sequence = timeline?.activeSequenceId;
return [
AppMenuItem(
label: context.l10n.addToTimeline,
icon: AppIcons.add,
onPressed:
canEdit &&
!unavailable &&
sequence != null &&
available?.timeline?.id == timelineId
? () => _action(
() => _browser.insertAsset(asset),
)
: null,
),
AppMenuItem(
label: context.l10n.rename,
icon: AppIcons.rename,
onPressed: canEdit ? () => _rename(asset) : null,
),
AppMenuItem(
label:
defaultTargetPlatform == TargetPlatform.macOS
? context.l10n.revealFinder
: context.l10n.revealFileManager,
icon: AppIcons.folderOpen,
onPressed: canEdit && _desktop
? () => _action(() => _browser.reveal(asset))
: null,
),
const AppMenuSeparator(),
AppMenuItem(
label: context.l10n.removeMedia,
icon: AppIcons.delete,
destructive: true,
onPressed: canEdit ? () => _remove(asset) : null,
),
];
},
child: Listener(
onPointerDown: (event) {
if (event.buttons == 2) {
_selectedAssetId = asset.id;
}
},
child: _SelectableAsset(
key: ValueKey(asset.id),
grid: _isGrid,
desktop: widget.desktop,
drag: MediaAssetDrag(
projectId: widget.projectId,
assetId: asset.id,
),
name: asset.name,
draggable:
widget.draggable &&
!workspace!.projectBusy &&
!presentation.$2 &&
!unavailable,
selected: presentation.$1,
onTap: () => _browser.selectAsset(asset),
child: _assetTile(asset, availability),
),
),
),
);
},
);
}
Widget scrollView(ScrollController controller) => !_isGrid
? widget.desktop
? ListView.builder(
@@ -509,9 +499,10 @@ class _MediaBrowserBodyState extends ConsumerState<MediaBrowserBody>
separatorBuilder: (_, _) =>
SizedBox(height: spacing),
)
: GridView.builder(
: GridView.custom(
key: PageStorageKey(('media-grid', widget.projectId)),
controller: controller,
semanticChildCount: assets.length,
padding: EdgeInsets.all(padding),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: columns,
@@ -528,8 +519,10 @@ class _MediaBrowserBodyState extends ConsumerState<MediaBrowserBody>
labelHeight +
2,
),
itemCount: assets.length,
itemBuilder: item,
childrenDelegate: _MediaChildrenDelegate(
item,
childCount: assets.length,
),
);
if (widget.desktop) {
return ScrollEdgeFade(
@@ -546,8 +539,6 @@ class _MediaBrowserBodyState extends ConsumerState<MediaBrowserBody>
if (!widget.desktop) return content;
return Column(
children: [
if (!_isGrid)
const DesktopMediaListHeader(key: Key('media-list-header')),
Expanded(child: content),
const Divider(height: 1),
SizedBox(
@@ -811,3 +802,13 @@ class _SelectableAsset extends StatelessWidget {
);
}
}
// Constraint changes reflow existing cells without rebuilding menus and tiles.
// A new builder is created when assets, filters or the project change.
class _MediaChildrenDelegate extends SliverChildBuilderDelegate {
const _MediaChildrenDelegate(super.builder, {super.childCount});
@override
bool shouldRebuild(covariant _MediaChildrenDelegate oldDelegate) =>
builder != oldDelegate.builder || childCount != oldDelegate.childCount;
}
@@ -30,9 +30,9 @@ enum MediaSort { importOrder, nameAscending, nameDescending }
/// Desktop thumbnail density, independent of touch-page presentation.
enum MediaGridSize {
small(120),
medium(160),
large(200);
small(96),
medium(120),
large(160);
const MediaGridSize(this.targetWidth);
final double targetWidth;
+4 -5
View File
@@ -2,15 +2,14 @@ import 'dart:math' as math;
/// Shared desktop geometry for cards, rows, headers and browse anchoring.
abstract final class DesktopMediaMetrics {
static const padding = 12.0;
static const padding = 16.0;
static const gap = 8.0;
static const tilePadding = 5.0;
static const tilePadding = 8.0;
static const listCover = 56.0;
static const listRow = 48.0;
static const listHeader = 28.0;
static int columns(double width, double targetWidth) =>
math.max(1, ((width + gap) / (targetWidth + gap)).floor());
math.max(1, ((width + gap) / (targetWidth + gap)).ceil());
static double gridExtent(double tileWidth, double labelHeight) =>
(tileWidth - tilePadding * 2) * 9 / 16 +
@@ -19,7 +18,7 @@ abstract final class DesktopMediaMetrics {
tilePadding * 2;
}
/// Matching list-column visibility for a header and its asset rows.
/// List-column visibility within asset rows.
final class DesktopMediaListColumns {
const DesktopMediaListColumns(double width)
: duration = width >= 240,
+97 -27
View File
@@ -292,6 +292,34 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
}
Widget _desktopToolbar(BuildContext context, RecentProject? selected) {
final desktopButtonStyle = AppTheme.toolbarButtonStyle(context).copyWith(
textStyle: WidgetStatePropertyAll(
Theme.of(context).textTheme.bodyMedium!.copyWith(
fontFamily: 'packages/cutdex_ui/Inter',
fontSize: AppInspectorMetrics.fontSize,
height: AppInspectorMetrics.lineHeight / AppInspectorMetrics.fontSize,
fontWeight: FontWeight.w400,
fontVariations: const [FontVariation('wght', 450)],
letterSpacing: AppInspectorMetrics.fontSize * .005,
),
),
minimumSize: const WidgetStatePropertyAll(
Size(0, AppInspectorMetrics.input),
),
maximumSize: const WidgetStatePropertyAll(
Size(double.infinity, AppInspectorMetrics.input),
),
padding: const WidgetStatePropertyAll(
EdgeInsets.symmetric(horizontal: AppSpacing.small),
),
shape: const WidgetStatePropertyAll(
RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(AppInspectorMetrics.radius),
),
),
),
);
Widget category() => Semantics(
label: context.l10n.mediaCategory,
child: AppDropdownMenu(
@@ -309,6 +337,7 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
buttonKey: const Key('media-category-button'),
focusNode: focus,
onPressed: toggle,
style: desktopButtonStyle,
child: Row(
children: [
Expanded(
@@ -341,9 +370,14 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
else
SizedBox(
key: const Key('media-project-row'),
height: 36,
height: AppInspectorMetrics.row,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
padding: const EdgeInsetsDirectional.fromSTEB(
AppInspectorMetrics.padding,
AppInspectorMetrics.rowPadding,
AppInspectorMetrics.trailingPadding,
AppInspectorMetrics.rowPadding,
),
child: LayoutBuilder(
builder: (context, constraints) => Row(
children: [
@@ -369,6 +403,7 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
buttonKey: const Key('media-project-button'),
focusNode: focusNode,
onPressed: toggle,
style: desktopButtonStyle,
child: Row(
children: [
Expanded(
@@ -385,7 +420,7 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
),
),
),
const SizedBox(width: 4),
const SizedBox(width: AppSpacing.small),
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: (constraints.maxWidth * .45).clamp(
@@ -394,12 +429,12 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
),
),
child: SizedBox(
height: 28,
height: AppInspectorMetrics.input,
child: Tooltip(
message: context.l10n.importMedia,
child: TextButton(
key: const Key('media-import-button'),
style: AppTheme.toolbarButtonStyle(context),
style: desktopButtonStyle,
onPressed: canImport ? onImport : null,
child: Text(
context.l10n.importMedia,
@@ -427,6 +462,14 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
Widget Function() category,
String Function(MediaSort) sortLabel,
) {
final searchText = Theme.of(context).textTheme.bodyMedium?.copyWith(
fontFamily: 'packages/cutdex_ui/Inter',
fontSize: AppInspectorMetrics.fontSize,
height: AppInspectorMetrics.lineHeight / AppInspectorMetrics.fontSize,
fontVariations: const [FontVariation('wght', 450)],
letterSpacing: AppInspectorMetrics.fontSize * .005,
);
final colors = Theme.of(context).colorScheme;
List<Widget> sortItems() => [
for (final value in MediaSort.values)
AppMenuItem(
@@ -462,7 +505,7 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
required IconData icon,
required List<Widget> items,
}) => SizedBox.square(
dimension: 28,
dimension: AppInspectorMetrics.action,
child: AppDropdownMenu(
items: items,
builder: (context, toggle, focus) => IconButton(
@@ -477,9 +520,14 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
);
return SizedBox(
key: const Key('media-filter-row'),
height: 40,
height: AppInspectorMetrics.row,
child: Padding(
padding: const EdgeInsets.fromLTRB(12, 4, 12, 4),
padding: const EdgeInsets.fromLTRB(
AppInspectorMetrics.padding,
AppInspectorMetrics.rowPadding,
AppInspectorMetrics.trailingPadding,
AppInspectorMetrics.rowPadding,
),
child: LayoutBuilder(
builder: (context, constraints) {
final wide = constraints.maxWidth >= 400;
@@ -490,13 +538,10 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
key: const Key('media-search'),
controller: _search,
enabled: widget.onQueryChanged != null,
style: AppTheme.inputTextStyle(
context,
style: AppTheme.toolbarTextStyle(context),
),
style: AppTheme.inputTextStyle(context, style: searchText),
cursorHeight: AppTheme.inputCursorHeight(
context,
style: AppTheme.toolbarTextStyle(context),
style: searchText,
),
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
@@ -505,36 +550,61 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
),
hintStyle: AppTheme.inputHintStyle(
context,
style: AppTheme.toolbarTextStyle(context),
style: searchText,
),
filled: true,
fillColor: colors.surfaceContainer,
isDense: true,
constraints: const BoxConstraints(minHeight: 32),
constraints: const BoxConstraints(
minHeight: AppInspectorMetrics.input,
),
contentPadding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
horizontal: AppSpacing.small,
),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(AppInspectorMetrics.radius),
),
borderSide: BorderSide.none,
),
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(AppInspectorMetrics.radius),
),
borderSide: BorderSide.none,
),
focusedBorder: OutlineInputBorder(
borderRadius: const BorderRadius.all(
Radius.circular(AppInspectorMetrics.radius),
),
borderSide: BorderSide(color: colors.primary),
),
prefixIcon: constraints.maxWidth < 160
? null
: const Icon(AppIcons.search, size: 16),
prefixIconConstraints: const BoxConstraints(
minWidth: 28,
minHeight: 28,
minWidth: AppInspectorMetrics.input,
minHeight: AppInspectorMetrics.input,
),
),
onChanged: (value) => widget.onQueryChanged?.call(value),
),
),
const SizedBox(width: 4),
const SizedBox(width: AppSpacing.small),
if (wide) ...[
SizedBox(width: 88, height: 28, child: category()),
const SizedBox(width: 4),
SizedBox(
width: 88,
height: AppInspectorMetrics.input,
child: category(),
),
const SizedBox(width: AppSpacing.small),
menuButton(
key: const Key('media-sort-button'),
tooltip: context.l10n.sortMedia,
icon: AppIcons.sort,
items: sortItems(),
),
const SizedBox(width: 4),
const SizedBox(width: AppSpacing.small),
menuButton(
key: const Key('media-size-button'),
tooltip: context.l10n.mediaThumbnailSize,
@@ -564,9 +634,9 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
...sizeItems(),
],
),
const SizedBox(width: 4),
const SizedBox(width: AppSpacing.small),
SizedBox.square(
dimension: 28,
dimension: AppInspectorMetrics.action,
child: IconButton(
key: const Key('media-layout-toggle'),
onPressed: onToggleGrid,
@@ -581,9 +651,9 @@ class _MediaBrowserToolbarState extends State<MediaBrowserToolbar> {
),
),
if (widget.projectScoped) ...[
const SizedBox(width: 4),
const SizedBox(width: AppSpacing.small),
SizedBox.square(
dimension: 28,
dimension: AppInspectorMetrics.action,
child: IconButton(
key: const Key('media-import-button'),
onPressed: canImport ? () => onImport() : null,
+26 -27
View File
@@ -115,9 +115,13 @@ class _ResourceSectionTabsState extends State<ResourceSectionTabs> {
final l10n = AppLocalizations.of(context)!;
return SizedBox(
key: const Key('resource-section-tabs'),
height: 36,
height: AppDesktopMetrics.timelineTabs,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 5),
padding: const EdgeInsetsDirectional.only(
start: AppSpacing.small,
top: 4,
bottom: 4,
),
child: ScrollEdgeFade(
controller: _scrollController,
axis: Axis.horizontal,
@@ -127,35 +131,30 @@ class _ResourceSectionTabsState extends State<ResourceSectionTabs> {
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),
child: Row(
children: [
for (final section in ResourceSection.values) ...[
if (section != ResourceSection.values.first)
const SizedBox(width: AppSpacing.tiny),
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),
),
),
],
),
],
),
],
),
),
),