Files
lingjhf abc1868550
Gitea CI / Check code and tests (push) Successful in 6m37s
Gitea CI / Build Windows x64 app (push) Successful in 3m18s
Gitea CI / Build Android APK (push) Successful in 3m47s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m7s
Gitea CI / Assemble and validate CI artifacts (push) Successful in 50s
Gitea CI / Gitea CI passed (push) Successful in 2s
Gitea Release / Prepare draft Release (push) Successful in 9s
Gitea Release / Upload Android to draft Release (push) Successful in 16s
Gitea Release / Upload Windows to draft Release (push) Successful in 22s
Gitea Release / Verify and publish Release (push) Successful in 14s
feat(workspace): 桌面工作区、主题与交互更新,发布 0.1.2
2026-10-08 18:17:12 +00:00

435 lines
16 KiB
Dart

import 'package:cutdex_ui/icons.dart';
import 'package:cutdex_ui/theme.dart';
import 'package:cutdex_ui/widgets.dart';
import 'package:flutter/material.dart';
import 'package:cutdex_native/cutdex_native.dart';
import '../../l10n/l10n.dart';
import '../../workspace/project_state.dart';
class ProjectTabsBar extends StatefulWidget {
const ProjectTabsBar({
super.key,
required this.projects,
required this.recent,
required this.selectedId,
required this.onSelect,
required this.onClose,
required this.onCreate,
required this.onOpen,
required this.onRename,
this.onRemoveRecent,
this.projectMenuActions = const [],
this.leadingActions = const [],
this.trailingActions = const [],
});
final List<RecentProject> projects;
final List<RecentProject> recent;
final String? selectedId;
final ValueChanged<RecentProject> onSelect;
final ValueChanged<RecentProject> onClose;
final VoidCallback onCreate;
final VoidCallback onOpen;
final ValueChanged<RecentProject> onRename;
final ValueChanged<RecentProject>? onRemoveRecent;
final List<Widget> projectMenuActions;
final List<Widget> leadingActions;
final List<Widget> trailingActions;
@override
State<ProjectTabsBar> createState() => _ProjectTabsBarState();
}
class _ProjectTabsBarState extends State<ProjectTabsBar> {
final _tabKeys = <String, GlobalKey>{};
final _addKey = GlobalKey();
double? _availableWidth;
bool _active = true;
List<RecentProject> get projects => widget.projects;
List<RecentProject> get recent => widget.recent;
String? get selectedId => widget.selectedId;
ValueChanged<RecentProject> get onSelect => widget.onSelect;
ValueChanged<RecentProject> get onClose => widget.onClose;
VoidCallback get onCreate => widget.onCreate;
VoidCallback get onOpen => widget.onOpen;
ValueChanged<RecentProject> get onRename => widget.onRename;
void _revealSelection() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted || !_active) return;
final context = projects.lastOrNull?.id == selectedId
? _addKey.currentContext
: _tabKeys[selectedId]?.currentContext;
if (context != null) {
Scrollable.ensureVisible(
context,
duration: const Duration(milliseconds: 150),
alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
);
}
});
}
@override
void initState() {
super.initState();
_revealSelection();
}
@override
void didUpdateWidget(covariant ProjectTabsBar oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.selectedId != selectedId ||
oldWidget.projects.length != projects.length ||
oldWidget.projects.any(
(project) =>
projects.where((item) => item.id == project.id).firstOrNull !=
project,
)) {
_revealSelection();
}
final ids = projects.map((project) => project.id).toSet();
_tabKeys.removeWhere((id, _) => !ids.contains(id));
}
@override
void deactivate() {
_active = false;
super.deactivate();
}
@override
void activate() {
super.activate();
_active = true;
_revealSelection();
}
@override
Widget build(BuildContext context) => LayoutBuilder(
builder: (context, constraints) {
if (_availableWidth != constraints.maxWidth) {
_availableWidth = constraints.maxWidth;
_revealSelection();
}
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: AppProjectTabMetrics.gap,
),
child: Row(
key: const Key('window-project-tabs'),
children: [
for (final action in widget.leadingActions) ...[
HeaderControlRegion(
child: SizedBox.square(
dimension: AppProjectTabMetrics.height,
child: action,
),
),
const SizedBox(width: AppProjectTabMetrics.gap),
],
AppDropdownMenu(
maxHeight: 400,
items: [
AppMenuItem(
label: context.l10n.newProject,
icon: AppIcons.add,
onPressed: onCreate,
),
AppMenuItem(
label: context.l10n.openProjectMore,
icon: AppIcons.folder,
onPressed: onOpen,
),
if (recent.isNotEmpty) const AppMenuSeparator(),
for (final project in recent)
AppContextMenu(
itemsBuilder: (_) => [
AppMenuItem(
label: context.l10n.rename,
icon: AppIcons.rename,
onPressed: !project.availability.canOpen
? null
: () => onRename(project),
),
if (widget.onRemoveRecent != null)
AppMenuItem(
label: context.l10n.removeProjectFromRecent,
onPressed: () => widget.onRemoveRecent!(project),
),
],
child: AppMenuItem(
key: ValueKey('project-menu-${project.id}'),
label: project.availability.canOpen
? project.name
: '${project.name} · ${context.l10n.projectAccessFailed}',
icon: project.availability.canOpen
? AppIcons.folder
: AppIcons.folderUnavailable,
selected: project.id == selectedId,
onPressed: () => onSelect(project),
),
),
if (widget.projectMenuActions.isNotEmpty) ...[
const AppMenuSeparator(),
...widget.projectMenuActions,
],
],
builder: (context, toggle, focusNode) => HeaderControlRegion(
child: IconButton(
key: const Key('project-tabs-projects'),
focusNode: focusNode,
tooltip: context.l10n.projects,
onPressed: toggle,
constraints: const BoxConstraints.tightFor(
width: AppProjectTabMetrics.height,
height: AppProjectTabMetrics.height,
),
padding: EdgeInsets.zero,
iconSize: AppIconSize.small,
style: AppTheme.projectTabActionStyle(context),
icon: const Icon(AppIcons.home),
),
),
),
const SizedBox(width: AppProjectTabMetrics.gap),
Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
for (var index = 0; index < projects.length; index++) ...[
if (index > 0)
const SizedBox(width: AppProjectTabMetrics.gap),
_ProjectTab(
key: ValueKey(projects[index].id),
anchorKey: _tabKeys.putIfAbsent(
projects[index].id,
GlobalKey.new,
),
project: projects[index],
selected: projects[index].id == selectedId,
maxWidth:
(constraints.maxWidth -
5 * AppProjectTabMetrics.gap -
3 * AppProjectTabMetrics.height -
(widget.leadingActions.length +
widget.trailingActions.length) *
(AppProjectTabMetrics.height +
AppProjectTabMetrics.gap))
.clamp(
AppProjectTabMetrics.minWidth,
AppProjectTabMetrics.maxWidth,
),
onSelect: () => onSelect(projects[index]),
onClose: () => onClose(projects[index]),
onRename: () => onRename(projects[index]),
),
],
if (projects.isNotEmpty)
const SizedBox(width: AppProjectTabMetrics.gap),
HeaderControlRegion(
key: _addKey,
child: IconButton(
key: const Key('project-tabs-add'),
tooltip: context.l10n.newProject,
onPressed: onCreate,
iconSize: 16,
style: AppTheme.projectTabActionStyle(context),
icon: const Icon(AppIcons.add),
),
),
],
),
),
),
const SizedBox(width: AppProjectTabMetrics.gap),
AppDropdownMenu(
enabled: projects.isNotEmpty,
maxHeight: 400,
items: [
for (final project in projects)
AppMenuItem(
key: ValueKey('project-tabs-menu-${project.id}'),
label: project.name,
icon: AppIcons.folder,
selected: project.id == selectedId,
onPressed: () => onSelect(project),
),
],
builder: (context, toggle, focusNode) => HeaderControlRegion(
child: IconButton(
key: const Key('project-tabs-list'),
focusNode: focusNode,
tooltip: context.l10n.selectProject,
onPressed: toggle,
padding: EdgeInsets.zero,
iconSize: 14,
style: AppTheme.projectTabActionStyle(context),
icon: const Icon(AppIcons.expandMore),
),
),
),
for (final action in widget.trailingActions) ...[
const SizedBox(width: AppProjectTabMetrics.gap),
HeaderControlRegion(
child: SizedBox.square(
dimension: AppProjectTabMetrics.height,
child: action,
),
),
],
],
),
);
},
);
}
class _ProjectTab extends StatefulWidget {
const _ProjectTab({
super.key,
required this.anchorKey,
required this.project,
required this.selected,
required this.maxWidth,
required this.onSelect,
required this.onClose,
required this.onRename,
});
final GlobalKey anchorKey;
final RecentProject project;
final bool selected;
final double maxWidth;
final VoidCallback onSelect, onClose, onRename;
@override
State<_ProjectTab> createState() => _ProjectTabState();
}
class _ProjectTabState extends State<_ProjectTab> {
bool _hovered = false;
bool _focused = false;
@override
Widget build(BuildContext context) {
final project = widget.project;
final style = AppTheme.projectTabStyle(context, selected: widget.selected);
final states = <WidgetState>{
if (_hovered) WidgetState.hovered,
if (_focused) WidgetState.focused,
};
final showClose = widget.selected || _hovered || _focused;
return HeaderControlRegion(
child: MouseRegion(
onEnter: (_) => setState(() => _hovered = true),
onExit: (_) => setState(() => _hovered = false),
child: Focus(
canRequestFocus: false,
onFocusChange: (value) => setState(() => _focused = value),
child: AppContextMenu(
itemsBuilder: (_) => [
AppMenuItem(
label: context.l10n.rename,
onPressed: widget.onRename,
),
AppMenuItem(
label: MaterialLocalizations.of(context).closeButtonTooltip,
onPressed: widget.onClose,
),
],
child: Semantics(
selected: widget.selected,
child: Material(
key: widget.anchorKey,
color: style.backgroundColor!.resolve(states),
borderRadius: BorderRadius.circular(
AppProjectTabMetrics.radius,
),
child: ConstrainedBox(
key: ValueKey('project-tab-surface-${project.id}'),
constraints: BoxConstraints(
minWidth: AppProjectTabMetrics.minWidth,
maxWidth: widget.maxWidth,
),
child: IntrinsicWidth(
child: SizedBox(
height: AppProjectTabMetrics.height,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Tooltip(
message: '${project.name}\n${project.path}',
child: TextButton(
key: ValueKey('project-tab-${project.id}'),
onPressed: widget.onSelect,
style: style.copyWith(
alignment: Alignment.centerLeft,
backgroundColor: const WidgetStatePropertyAll(
Colors.transparent,
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(AppIcons.folder, size: 14),
const SizedBox(width: 6),
Flexible(
child: Text(
project.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
),
),
),
),
ExcludeFocus(
excluding: !showClose,
child: IgnorePointer(
ignoring: !showClose,
child: ExcludeSemantics(
excluding: !showClose,
child: Opacity(
opacity: showClose ? 1 : 0,
child: IconButton(
key: ValueKey(
'project-tab-close-${project.id}',
),
tooltip: MaterialLocalizations.of(context)
.closeButtonTooltip,
onPressed: widget.onClose,
constraints: const BoxConstraints.tightFor(
width: AppProjectTabMetrics.close,
height: AppProjectTabMetrics.close,
),
padding: EdgeInsets.zero,
iconSize: 12,
style: AppTheme.projectTabActionStyle(
context,
size: AppProjectTabMetrics.close,
),
icon: const Icon(AppIcons.close),
),
),
),
),
),
const SizedBox(width: 4),
],
),
),
),
),
),
),
),
),
),
);
}
}