Add the Flutter video editing application with project management, media import, timeline editing, preview, AI assistance, diagnostics and settings. Include shared UI and native host packages, platform runners, application tests and release tooling. Provide usage and contribution guides, the MIT license and third-party notices. Exclude local credentials, build outputs and validation data.
507 lines
18 KiB
Dart
507 lines
18 KiB
Dart
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:cutdex_sdk/project.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../../l10n/app_localizations.dart';
|
|
|
|
import 'package:cutdex_ui/theme.dart';
|
|
|
|
import 'composer_document.dart';
|
|
import 'composer_suggestions.dart';
|
|
|
|
part 'composer_suggestion_surface.freezed.dart';
|
|
|
|
/// Owns suggestion focus, keyboard navigation, selection and the anchored menu.
|
|
/// The caller supplies the composer body and the action used when Enter sends.
|
|
class ComposerSuggestionSurface extends StatefulWidget {
|
|
const ComposerSuggestionSurface({
|
|
super.key,
|
|
required this.controller,
|
|
required this.width,
|
|
required this.projectId,
|
|
required this.assets,
|
|
required this.timelines,
|
|
required this.onSend,
|
|
required this.builder,
|
|
});
|
|
|
|
final ComposerDocumentController controller;
|
|
final double width;
|
|
final String? projectId;
|
|
final Iterable<AssetDocument> assets;
|
|
final Iterable<TimelineSummary> timelines;
|
|
final VoidCallback onSend;
|
|
final Widget Function(BuildContext context, FocusNode inputFocus) builder;
|
|
|
|
@override
|
|
State<ComposerSuggestionSurface> createState() =>
|
|
_ComposerSuggestionSurfaceState();
|
|
}
|
|
|
|
class _ComposerSuggestionSurfaceState extends State<ComposerSuggestionSurface> {
|
|
final _focus = FocusNode();
|
|
final _link = LayerLink();
|
|
final _portal = OverlayPortalController();
|
|
final _anchorKey = GlobalKey();
|
|
final _tapGroup = Object();
|
|
final _menuScrollController = ScrollController(keepScrollOffset: false);
|
|
ComposerTriggerQuery? _trigger;
|
|
int _selected = 0;
|
|
ComposerTriggerQuery? _suppressedQuery;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
widget.controller.addListener(_onDocumentChanged);
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ComposerSuggestionSurface oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.controller != widget.controller) {
|
|
oldWidget.controller.removeListener(_onDocumentChanged);
|
|
widget.controller.addListener(_onDocumentChanged);
|
|
_onDocumentChanged();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.controller.removeListener(_onDocumentChanged);
|
|
_menuScrollController.dispose();
|
|
_focus.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onDocumentChanged() {
|
|
final candidate = findComposerTrigger(
|
|
widget.controller.value,
|
|
widget.controller.marks,
|
|
);
|
|
final suppressed = _suppressedQuery;
|
|
final stillSuppressed =
|
|
candidate != null &&
|
|
suppressed != null &&
|
|
candidate.start == suppressed.start &&
|
|
candidate.trigger == suppressed.trigger &&
|
|
candidate.query == suppressed.query;
|
|
if (!stillSuppressed) _suppressedQuery = null;
|
|
final next = stillSuppressed ? null : candidate;
|
|
if (next?.start == _trigger?.start &&
|
|
next?.query == _trigger?.query &&
|
|
next?.trigger == _trigger?.trigger) {
|
|
return;
|
|
}
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_trigger = next;
|
|
_selected = 0;
|
|
});
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
if (_menuScrollController.hasClients) _menuScrollController.jumpTo(0);
|
|
if (_trigger == null) {
|
|
if (_portal.isShowing) _portal.hide();
|
|
} else if (!_portal.isShowing) {
|
|
_portal.show();
|
|
}
|
|
});
|
|
}
|
|
|
|
void _dismiss({bool suppress = false}) {
|
|
if (suppress) _suppressedQuery = _trigger;
|
|
setState(() => _trigger = null);
|
|
if (_portal.isShowing) _portal.hide();
|
|
}
|
|
|
|
List<ComposerOption> _options(ComposerTriggerQuery query) =>
|
|
filterComposerOptions(
|
|
ComposerOptionCatalog.forTrigger(
|
|
query.trigger,
|
|
l10n: AppLocalizations.of(context)!,
|
|
projectId: widget.projectId,
|
|
assets: widget.assets,
|
|
timelines: widget.timelines,
|
|
),
|
|
query.query,
|
|
);
|
|
|
|
void _select(ComposerOption option) {
|
|
final query = _trigger;
|
|
if (query == null) return;
|
|
_dismiss();
|
|
switch (option) {
|
|
case ComposerReferenceOption(:final reference):
|
|
widget.controller.insertReference(query.start, query.end, reference);
|
|
case ComposerTextOption(:final text):
|
|
widget.controller.value = widget.controller.value.copyWith(
|
|
text: widget.controller.text.replaceRange(
|
|
query.start,
|
|
query.end,
|
|
text,
|
|
),
|
|
selection: TextSelection.collapsed(offset: query.start + text.length),
|
|
);
|
|
case ComposerPickerOption(:final onInvoke):
|
|
case ComposerInvokeOption(:final onInvoke):
|
|
onInvoke();
|
|
}
|
|
_focus.requestFocus();
|
|
}
|
|
|
|
void _selectCurrent() {
|
|
final query = _trigger;
|
|
if (query == null) return;
|
|
final options = _options(query);
|
|
if (options.isNotEmpty) {
|
|
_select(options[_selected.clamp(0, options.length - 1)]);
|
|
}
|
|
}
|
|
|
|
void _move(int delta) {
|
|
final query = _trigger;
|
|
if (query == null) return;
|
|
final options = _options(query);
|
|
final count = options.length;
|
|
if (count > 0) {
|
|
setState(() => _selected = (_selected + delta + count) % count);
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted || !_menuScrollController.hasClients) return;
|
|
final currentQuery = _trigger;
|
|
if (currentQuery == null) return;
|
|
final rows = _rows(_options(currentQuery));
|
|
var top = AppMenuMetrics.gutter;
|
|
for (final row in rows) {
|
|
if (row case _SuggestionOption(optionIndex: final index)
|
|
when index == _selected) {
|
|
final position = _menuScrollController.position;
|
|
final bottom = top + row.height;
|
|
final visibleTop = position.pixels;
|
|
final visibleBottom = visibleTop + position.viewportDimension;
|
|
final target = top < visibleTop
|
|
? top - 4
|
|
: bottom > visibleBottom
|
|
? bottom - position.viewportDimension + 4
|
|
: null;
|
|
if (target != null) {
|
|
_menuScrollController.animateTo(
|
|
target.clamp(
|
|
position.minScrollExtent,
|
|
position.maxScrollExtent,
|
|
),
|
|
duration: const Duration(milliseconds: 120),
|
|
curve: Curves.easeOut,
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
top += row.height;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
List<_SuggestionRow> _rows(List<ComposerOption> options) {
|
|
final rows = <_SuggestionRow>[];
|
|
for (final group in ComposerOptionGroup.values) {
|
|
final grouped = options.indexed.where((entry) => entry.$2.group == group);
|
|
if (grouped.isEmpty) continue;
|
|
rows.add(_SuggestionRow.header(group, rows.isEmpty ? 28 : 36));
|
|
for (final (index, option) in grouped) {
|
|
rows.add(_SuggestionRow.option(option, index));
|
|
}
|
|
}
|
|
for (final (index, option) in options.indexed) {
|
|
if (option.group == null) {
|
|
rows.add(_SuggestionRow.option(option, index));
|
|
}
|
|
}
|
|
return rows;
|
|
}
|
|
|
|
Widget _menu(BuildContext context) {
|
|
final query = _trigger;
|
|
if (query == null) return const SizedBox.shrink();
|
|
final options = _options(query);
|
|
final rows = _rows(options);
|
|
final anchor = _anchorKey.currentContext?.findRenderObject();
|
|
final availableAbove = anchor is RenderBox
|
|
? anchor.localToGlobal(Offset.zero).dy - 16
|
|
: MediaQuery.sizeOf(context).height / 2;
|
|
final placeBelow = availableAbove < 100;
|
|
final maxHeight = placeBelow
|
|
? (MediaQuery.sizeOf(context).height - availableAbove - 120).clamp(
|
|
72.0,
|
|
320.0,
|
|
)
|
|
: availableAbove.clamp(72.0, 320.0);
|
|
final theme = Theme.of(context);
|
|
return Stack(
|
|
children: [
|
|
CompositedTransformFollower(
|
|
link: _link,
|
|
showWhenUnlinked: false,
|
|
targetAnchor: placeBelow ? Alignment.bottomLeft : Alignment.topLeft,
|
|
followerAnchor: placeBelow ? Alignment.topLeft : Alignment.bottomLeft,
|
|
offset: Offset(0, placeBelow ? 4 : -4),
|
|
child: TapRegion(
|
|
groupId: _tapGroup,
|
|
child: Material(
|
|
color: AppTheme.popoverBackground(context),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(AppMenuMetrics.radius),
|
|
side: BorderSide(
|
|
color: theme.colorScheme.outlineVariant,
|
|
width: AppMenuMetrics.borderWidth,
|
|
),
|
|
),
|
|
clipBehavior: Clip.antiAlias,
|
|
child: SizedBox(
|
|
key: const Key('agent-composer-suggestions'),
|
|
width: widget.width,
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(maxHeight: maxHeight),
|
|
child: options.isEmpty
|
|
? Padding(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Text(
|
|
AppLocalizations.of(context)!.noAvailableOptions,
|
|
),
|
|
)
|
|
: ListView.builder(
|
|
key: const Key('agent-composer-suggestion-list'),
|
|
controller: _menuScrollController,
|
|
shrinkWrap: true,
|
|
padding: const EdgeInsets.all(AppMenuMetrics.gutter),
|
|
itemCount: rows.length,
|
|
itemBuilder: (context, index) {
|
|
final row = rows[index];
|
|
if (row case _SuggestionHeader(:final group)) {
|
|
final labels = AppLocalizations.of(context)!;
|
|
return Container(
|
|
key: Key(
|
|
'agent-suggestion-group-${group.name}',
|
|
),
|
|
height: row.height,
|
|
alignment: Alignment.centerLeft,
|
|
padding: EdgeInsets.fromLTRB(
|
|
10,
|
|
index == 0 ? 4 : 12,
|
|
10,
|
|
5,
|
|
),
|
|
child: Text(
|
|
switch (group) {
|
|
ComposerOptionGroup.media =>
|
|
labels.materials,
|
|
ComposerOptionGroup.timeline =>
|
|
labels.timelines,
|
|
},
|
|
style: theme.textTheme.bodySmall?.copyWith(
|
|
color: theme.colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
final _SuggestionOption(:option, :optionIndex) =
|
|
row as _SuggestionOption;
|
|
final selected = optionIndex == _selected;
|
|
return InkWell(
|
|
key: Key(
|
|
'agent-suggestion-${option is ComposerReferenceOption ? option.reference.kind.name : option.trigger}-${option is ComposerReferenceOption ? option.reference.id : index}',
|
|
),
|
|
borderRadius: BorderRadius.circular(
|
|
AppMenuMetrics.itemRadius,
|
|
),
|
|
onTap: () => _select(option),
|
|
child: Container(
|
|
height: 40,
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 10,
|
|
),
|
|
decoration: BoxDecoration(
|
|
color: selected
|
|
? theme.colorScheme.onSurface.withValues(
|
|
alpha: .1,
|
|
)
|
|
: null,
|
|
borderRadius: BorderRadius.circular(
|
|
AppMenuMetrics.itemRadius,
|
|
),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Icon(option.icon, size: 15),
|
|
const SizedBox(
|
|
width: AppMenuMetrics.iconGap,
|
|
),
|
|
Expanded(
|
|
child: Text(
|
|
option.title,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: theme.textTheme.bodyMedium,
|
|
),
|
|
),
|
|
if (option.group == null &&
|
|
option.subtitle.isNotEmpty) ...[
|
|
const SizedBox(width: 8),
|
|
Text(
|
|
option.subtitle,
|
|
style: theme.textTheme.bodySmall,
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final content = widget.builder(context, _focus);
|
|
return OverlayPortal(
|
|
controller: _portal,
|
|
overlayChildBuilder: _menu,
|
|
child: TapRegion(
|
|
groupId: _tapGroup,
|
|
onTapOutside: (_) {
|
|
if (_trigger != null) _dismiss(suppress: true);
|
|
},
|
|
child: CompositedTransformTarget(
|
|
key: _anchorKey,
|
|
link: _link,
|
|
child: ValueListenableBuilder<TextEditingValue>(
|
|
valueListenable: widget.controller,
|
|
child: content,
|
|
builder: (context, value, child) {
|
|
final composing =
|
|
value.composing.isValid && !value.composing.isCollapsed;
|
|
final enter = composing
|
|
? const DoNothingAndStopPropagationIntent()
|
|
: _trigger != null
|
|
? const _SelectSuggestionIntent()
|
|
: const _SendComposerIntent();
|
|
return Shortcuts(
|
|
shortcuts: {
|
|
const SingleActivator(LogicalKeyboardKey.enter): enter,
|
|
const SingleActivator(LogicalKeyboardKey.numpadEnter): enter,
|
|
if (_trigger != null) ...{
|
|
const SingleActivator(LogicalKeyboardKey.arrowDown):
|
|
const _MoveSuggestionIntent(1),
|
|
const SingleActivator(LogicalKeyboardKey.arrowUp):
|
|
const _MoveSuggestionIntent(-1),
|
|
const SingleActivator(LogicalKeyboardKey.escape):
|
|
const _DismissSuggestionIntent(),
|
|
},
|
|
const SingleActivator(LogicalKeyboardKey.keyZ, meta: true):
|
|
const _UndoComposerIntent(),
|
|
const SingleActivator(
|
|
LogicalKeyboardKey.keyZ,
|
|
meta: true,
|
|
shift: true,
|
|
): const _RedoComposerIntent(),
|
|
},
|
|
child: Actions(
|
|
actions: {
|
|
_SendComposerIntent: CallbackAction<_SendComposerIntent>(
|
|
onInvoke: (_) {
|
|
widget.onSend();
|
|
return null;
|
|
},
|
|
),
|
|
_SelectSuggestionIntent:
|
|
CallbackAction<_SelectSuggestionIntent>(
|
|
onInvoke: (_) {
|
|
_selectCurrent();
|
|
return null;
|
|
},
|
|
),
|
|
_MoveSuggestionIntent:
|
|
CallbackAction<_MoveSuggestionIntent>(
|
|
onInvoke: (intent) {
|
|
_move(intent.delta);
|
|
return null;
|
|
},
|
|
),
|
|
_DismissSuggestionIntent:
|
|
CallbackAction<_DismissSuggestionIntent>(
|
|
onInvoke: (_) {
|
|
_dismiss(suppress: true);
|
|
return null;
|
|
},
|
|
),
|
|
_UndoComposerIntent: CallbackAction<_UndoComposerIntent>(
|
|
onInvoke: (_) {
|
|
widget.controller.undoDocument();
|
|
return null;
|
|
},
|
|
),
|
|
_RedoComposerIntent: CallbackAction<_RedoComposerIntent>(
|
|
onInvoke: (_) {
|
|
widget.controller.redoDocument();
|
|
return null;
|
|
},
|
|
),
|
|
},
|
|
child: child!,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _SendComposerIntent extends Intent {
|
|
const _SendComposerIntent();
|
|
}
|
|
|
|
class _SelectSuggestionIntent extends Intent {
|
|
const _SelectSuggestionIntent();
|
|
}
|
|
|
|
class _MoveSuggestionIntent extends Intent {
|
|
const _MoveSuggestionIntent(this.delta);
|
|
final int delta;
|
|
}
|
|
|
|
class _DismissSuggestionIntent extends Intent {
|
|
const _DismissSuggestionIntent();
|
|
}
|
|
|
|
class _UndoComposerIntent extends Intent {
|
|
const _UndoComposerIntent();
|
|
}
|
|
|
|
class _RedoComposerIntent extends Intent {
|
|
const _RedoComposerIntent();
|
|
}
|
|
|
|
@freezed
|
|
sealed class _SuggestionRow with _$SuggestionRow {
|
|
const _SuggestionRow._();
|
|
const factory _SuggestionRow.header(
|
|
ComposerOptionGroup group,
|
|
double height,
|
|
) = _SuggestionHeader;
|
|
const factory _SuggestionRow.option(ComposerOption option, int optionIndex) =
|
|
_SuggestionOption;
|
|
double get height => switch (this) {
|
|
_SuggestionHeader(:final height) => height,
|
|
_SuggestionOption() => 40,
|
|
};
|
|
}
|