import 'dart:async'; import 'package:flutter/material.dart'; import '../../../core/servers/server_context.dart'; import '../../../ui/adaptive/app_popups.dart'; import '../../../ui/adaptive/segmented_bar.dart'; import '../../../ui/adaptive/segmented_bar_overflow.dart'; import 'chat_bar_tint.dart'; import '../workspace/chat_workspace_pane_scope.dart'; /// Below this width (in text-scaled logical pixels) the selectors leave the /// bar for its More menu's settings entry, so the title keeps /// [chatHeaderTitleShare]. const double chatHeaderNarrowWidth = 600; /// The share of a narrow bar the conversation's title always keeps. const double chatHeaderTitleShare = 0.6; /// 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, mode and /// thinking), then the Files action and a native More menu in a separate /// island. /// /// A narrow bar ([chatHeaderNarrowWidth]) keeps the title at /// [chatHeaderTitleShare] of the bar: the [controls] join the More menu as one /// "Chat settings" entry that opens them in a sheet, and the overflowable /// actions join it too. A floating chat passes [showTitle] false, since its window bar names /// the chat. /// /// A chat with a [chatColor] carries it in the bar: each glass piece is /// filled with its shade over the raised surface and its labels and icons /// take the colour's foreground ([chatBarTintOf]), faded in an inactive split /// pane. A chat with none keeps the neutral bar. class ChatThreadHeader extends StatelessWidget { const ChatThreadHeader({ super.key, required this.title, required this.folder, required this.onInfo, this.showTitle = true, this.showBack = false, this.onBack, this.controls = const [], this.actions = const [], this.moreActions = const [], this.chatColor, this.identity, }); final String title; /// The working folder's name, shown beneath the title; empty for none. final String folder; final VoidCallback onInfo; /// Whether the title pill (and its folder) shows; false in a floating /// window, whose own bar names the chat. final bool showTitle; /// 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; /// The chat's colour name, or null for none. final String? chatColor; /// The chat's identity avatar, shown in the title on a phone. final Widget? identity; @override Widget build(BuildContext context) { final tint = chatBarTintOf(context, chatColor); return LayoutBuilder( builder: (context, constraints) { final textScale = MediaQuery.textScalerOf(context).scale(14) / 14; final scaledWidth = constraints.maxWidth / textScale; final compact = scaledWidth < 800; final narrow = scaledWidth < chatHeaderNarrowWidth; // A narrow bar moves the selectors, and the overflowable actions, into // its More menu; the others stay in the bar. final menuActions = [ if (narrow) ...actions.whereType(), ...moreActions.whereType(), ]; final settingsEntry = narrow && controls.isNotEmpty ? SegmentedOverflowEntry( label: 'Chat settings', icon: Icons.tune, onPressed: () => unawaited(_showSettings(context, controls)), ) : null; final actionChildren = [ for (final action in actions) if (!narrow || action is! SegmentedOverflowable) action, for (final action in moreActions) if (action is! SegmentedOverflowable) action, if (settingsEntry != null || menuActions.isNotEmpty) SegmentedMoreButton( entries: [ ?settingsEntry, for (final action in menuActions) action.overflowEntry, ], ), ]; final bar = SegmentedBar( pillColor: tint?.fill, foregroundColor: tint?.foreground, 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'), onPressed: onBack, ) : null, title: !showTitle ? '' : (title.isEmpty ? 'Chat' : title), titleLeading: showTitle ? identity : null, subtitle: showTitle ? ServerContext.metadata(context, folder) : null, subtitleIcon: Icons.folder_outlined, onTitlePressed: showTitle ? onInfo : null, titleSemanticLabel: 'Chat information', titleKey: const Key('chat-info'), separateActionIslands: true, titleShare: narrow && showTitle ? chatHeaderTitleShare : null, actions: [ if (controls.isNotEmpty && !narrow) ChatWorkspacePaneActionBar( child: _ControlIsland( key: const Key('chat-model-permission-island'), gap: compact ? 0 : 8, children: controls, ), ), if (actionChildren.isNotEmpty) ChatWorkspacePaneActionBar( child: Row( key: const Key('chat-actions-island'), mainAxisSize: MainAxisSize.min, children: [ for (final (index, action) in actionChildren.indexed) ...[ if (index > 0 && !compact) const SizedBox(width: 8), action, ], ], ), ), ], bottomPadding: compact ? 8 : 16, ); return ColoredBox( color: Theme.of(context).colorScheme.surface, child: tint == null ? bar : ChatBarTintScope( key: const Key('chat-bar-tint'), tint: tint, child: bar, ), ); }, ); } } /// A narrow bar's selectors (model, mode and thinking), each with its label, /// in a sheet opened from the More menu. Future _showSettings(BuildContext context, List controls) => showAppChoiceMenu( context, semanticLabel: 'Chat settings', builder: (_) => SafeArea( child: Padding( padding: const EdgeInsets.fromLTRB(16, 0, 16, 16), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, 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, required this.gap}); final List children; final double gap; @override Widget build(BuildContext context) => Row( mainAxisSize: MainAxisSize.min, children: [ for (final (index, child) in children.indexed) ...[ if (index > 0) SizedBox(width: gap), Flexible(child: child), ], ], ); }