Files
cutdex/lib/features/assistant/composer_project_selector.dart
T
lingjhf 1f73765745
Gitea CI / Windows x64 and Android (pull_request) Successful in 16m21s
Gitea CI / Gitea CI passed (pull_request) Successful in 3s
refactor(desktop): reorganize workspace panels and navigation
2026-10-06 12:11:59 +08:00

227 lines
7.9 KiB
Dart

import 'package:flutter/material.dart';
import '../../l10n/l10n.dart';
import '../../shared/layout/composer_layout.dart';
import '../../shared/theme/composer_style.dart';
import 'package:cutdex_ui/theme.dart';
import '../../workspace/project_state.dart';
class ComposerProjectSelector extends StatefulWidget {
const ComposerProjectSelector({
super.key,
required this.projects,
required this.selected,
required this.busy,
required this.onSelect,
required this.onCreate,
required this.backgroundColor,
});
final List<RecentProject> projects;
final RecentProject? selected;
final bool busy;
final ValueChanged<RecentProject> onSelect;
final VoidCallback? onCreate;
final Color backgroundColor;
@override
State<ComposerProjectSelector> createState() =>
_ComposerProjectSelectorState();
}
class _ComposerProjectSelectorState extends State<ComposerProjectSelector> {
final _menu = MenuController();
final _search = TextEditingController();
@override
void dispose() {
_search.dispose();
super.dispose();
}
Widget _item({
required Key key,
required String label,
required IconData icon,
bool? selected,
bool secondary = false,
VoidCallback? onPressed,
}) {
final colors = Theme.of(context).colorScheme;
return MenuItemButton(
key: key,
onPressed: onPressed,
style: AppComposerStyle.projectMenuItem(context, secondary: secondary),
child: Semantics(
selected: selected,
child: Row(
children: [
Icon(
icon,
size: ComposerLayout.menuIconSize,
color: onPressed == null
? Theme.of(context).disabledColor
: colors.onSurfaceVariant,
),
const SizedBox(width: ComposerLayout.menuIconGap),
Expanded(
child: Text(label, maxLines: 1, overflow: TextOverflow.ellipsis),
),
if (selected == true) ...[
const SizedBox(width: ComposerLayout.menuIconGap),
Icon(
Icons.check,
size: ComposerLayout.menuIconSize,
color: colors.onSurfaceVariant,
),
],
],
),
),
);
}
Widget _separator() => const Padding(
padding: AppComposerStyle.menuSeparatorPadding,
child: Divider(
height: 1,
thickness: AppComposerStyle.menuSeparatorThickness,
),
);
@override
Widget build(BuildContext context) {
final query = _search.text.trim().toLowerCase();
final projects = widget.projects
.where((project) => project.name.toLowerCase().contains(query))
.toList();
final listHeight = projects.isEmpty
? ComposerLayout.menuRowHeight +
AppComposerStyle.menuEmptyPadding.vertical
: (projects.length * ComposerLayout.menuRowHeight).clamp(
0.0,
ComposerLayout.menuListMaxHeight,
);
final contentHeight =
ComposerLayout.menuRowHeight * 2 +
(AppComposerStyle.menuSeparatorPadding.vertical + 1) * 2 +
listHeight;
final menuHeight = contentHeight + ComposerLayout.menuGutter * 2;
return Material(
key: const Key('composer-project-bar'),
color: widget.backgroundColor,
borderRadius: const BorderRadius.vertical(
top: Radius.circular(ComposerLayout.utilityRadius),
),
child: Padding(
padding: AppComposerStyle.utilityPadding,
child: Align(
alignment: AlignmentDirectional.centerStart,
child: MenuAnchor(
controller: _menu,
alignmentOffset: Offset(0, -menuHeight - 4),
consumeOutsideTap: false,
onClose: () => setState(_search.clear),
style: AppComposerStyle.projectMenu(context)
.copyWith(alignment: AlignmentDirectional.topStart),
menuChildren: [
SizedBox(
width: ComposerLayout.menuContentWidth,
height: contentHeight,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(
height: ComposerLayout.menuRowHeight,
child: TextField(
key: const Key('composer-project-search'),
controller: _search,
autofocus: true,
style: AppTheme.composerTextStyle(context),
onChanged: (_) => setState(() {}),
decoration: AppComposerStyle.projectSearchDecoration(
context,
hintText: context.l10n.searchProjects,
),
),
),
_separator(),
Expanded(
child: SingleChildScrollView(
primary: false,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (projects.isEmpty)
Padding(
padding: AppComposerStyle.menuEmptyPadding,
child: Text(
query.isEmpty
? context.l10n.projectsNone
: context.l10n.noMatchingProjects,
style: Theme.of(context).textTheme.bodySmall,
),
),
for (final project in projects)
_item(
key: ValueKey('composer-project-${project.id}'),
label: project.name,
icon: Icons.folder_outlined,
selected: project.id == widget.selected?.id,
onPressed:
widget.busy || !project.availability.canOpen
? null
: () {
_menu.close();
widget.onSelect(project);
},
),
],
),
),
),
_separator(),
_item(
key: const Key('composer-new-project'),
secondary: true,
label: context.l10n.newProject,
icon: Icons.add,
onPressed: widget.busy || widget.onCreate == null
? null
: () {
_menu.close();
widget.onCreate!();
},
),
],
),
),
],
builder: (context, controller, _) => TextButton.icon(
key: const Key('composer-project-trigger'),
onPressed: widget.busy
? null
: () => controller.isOpen
? controller.close()
: controller.open(),
style: AppComposerStyle.projectTrigger(context),
icon: const Icon(
Icons.folder_outlined,
size: ComposerLayout.menuIconSize,
),
label: Text(
widget.selected?.name ?? context.l10n.selectProject,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
),
),
),
);
}
}