import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/segmented_bar.dart'; import '../../../ui/adaptive/segmented_bar_overflow.dart'; import '../ui/sessions/chat_adaptive.dart'; /// The open conversation's bar: the app's [SegmentedBar] above the /// transcript. /// /// From the leading edge: a round Back (only where the page can go back), a /// pill with the conversation's title and folder that opens the chat's /// information, a selector island holding [controls] (model and mode), /// then the Files action and a native More menu in a separate island. /// Narrow panes give readable model/mode values a separate row; exceptionally /// short viewports can scroll the header to keep controls reachable. class ChatThreadHeader extends StatelessWidget { const ChatThreadHeader({ super.key, required this.title, required this.folder, required this.onInfo, this.showBack = false, this.onBack, this.controls = const [], this.actions = const [], this.moreActions = const [], this.backdrop, }); final String title; /// The working folder's name, shown beneath the title; empty for none. final String folder; final VoidCallback onInfo; /// Whether the bar shows Back at all. final bool showBack; /// What Back does; null keeps the platform's stock action (pop the route). final VoidCallback? onBack; final List controls; final List actions; final List moreActions; /// Painted over the bar's scrim, under its pieces (the chat's colour). final Widget? backdrop; @override Widget build(BuildContext context) { final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; return LayoutBuilder( builder: (context, constraints) { final textScale = MediaQuery.textScalerOf(context).scale(14) / 14; final readableWidth = constraints.maxWidth / textScale; final narrow = readableWidth < 800; final bar = SegmentedBar( cupertino: cupertino, automaticallyImplyLeading: false, // The platform's own Back, so it reads and behaves as every other // page's Back; its key stays for finders. leading: showBack ? SegmentedBackButton( key: const Key('chat-back'), cupertino: cupertino, onPressed: onBack, ) : null, title: title.isEmpty ? 'Chat' : title, subtitle: folder, subtitleIcon: cupertino ? CupertinoIcons.folder : Icons.folder_outlined, onTitlePressed: onInfo, titleSemanticLabel: 'Chat information', titleKey: const Key('chat-info'), separateActionIslands: true, actions: [ if (!narrow && controls.isNotEmpty) _ControlIsland( key: const Key('chat-model-permission-island'), children: controls, ), if (actions.isNotEmpty || moreActions.isNotEmpty) Row( key: const Key('chat-actions-island'), mainAxisSize: MainAxisSize.min, children: [ ...actions, // Unknown action widgets retain their original behavior and stay // visible; only the explicit overflow contract becomes a menu. for (final action in moreActions) if (action is! SegmentedOverflowable) action, if (moreActions.whereType().isNotEmpty) SegmentedMoreButton( cupertino: cupertino, entries: [ for (final action in moreActions.whereType()) action.overflowEntry, ], ), ], ), ], bottomPadding: narrow ? 8 : 16, backdrop: backdrop, ); return ColoredBox( color: cupertino ? CupertinoTheme.of(context).scaffoldBackgroundColor : Theme.of(context).colorScheme.surface, child: SingleChildScrollView( primary: false, child: Column( mainAxisSize: MainAxisSize.min, children: [ bar, if (narrow && controls.isNotEmpty) Padding( padding: const EdgeInsets.fromLTRB(12, 0, 12, 8), child: SegmentedPill( cupertino: cupertino, child: _ControlIsland( key: const Key('chat-model-permission-island'), fill: true, vertical: readableWidth < 340, children: controls, ), ), ), ], ), ), ); }, ); } } /// Related selectors share the available width. Each remains a real control; /// they never migrate into the unrelated actions menu. class _ControlIsland extends StatelessWidget { const _ControlIsland({ super.key, required this.children, this.fill = false, this.vertical = false, }); final List children; final bool fill; final bool vertical; @override Widget build(BuildContext context) => vertical ? Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: children, ) : Row( mainAxisSize: fill ? MainAxisSize.max : MainAxisSize.min, children: [ for (final child in children) if (fill) Expanded(child: child) else Flexible(child: child), ], ); }