import 'dart:math' as math; import 'package:flutter/semantics.dart'; import 'package:flutter/widgets.dart'; import '../conversation/chat_pane_presentation.dart'; import 'chat_peek_gesture.dart'; import 'chat_peek_header.dart'; import 'chat_workspace_controller.dart'; /// A phone presentation only: saved split axes and ratios are left untouched. class ChatMobilePanes { ChatMobilePanes({ required List panes, required String activeId, required Size size, double peekFraction = .2, }) { final active = panes.firstWhere((pane) => pane.id == activeId); this.activeId = active.id; // A stable circular order reaches every added pane. Choosing the last // active here would trap repeated taps in a two-chat toggle. final index = panes.indexWhere((pane) => pane.id == activeId); final peek = panes.length > 1 ? panes[(index + 1) % panes.length] : null; if (peek == null) { contentHeight = size.height; rects[active.id] = Offset.zero & size; return; } peekId = peek.id; const gap = 6.0; final available = math.max(0.0, size.height - gap); final maximum = available * .2; final minimum = math.min(48.0, maximum); final peekHeight = (available * peekFraction).clamp(minimum, maximum); contentHeight = available - peekHeight; // Selection always promotes the active conversation to the top. rects[active.id] = Rect.fromLTWH(0, 0, size.width, contentHeight); rects[peek.id] = Rect.fromLTWH( 0, contentHeight + gap, size.width, peekHeight, ); } late final double contentHeight; final rects = {}; String? peekId; String? activeId; /// [panes] with the active one last: it is drawn over the other during a /// switch and takes pointers first. List paintOrder(List panes) => [ for (final pane in panes) if (pane.id != activeId) pane, for (final pane in panes) if (pane.id == activeId) pane, ]; } /// One retained chat in the phone presentation. /// /// The scrollable receives the pane's actual visible constraints. A peek is /// never a clipped full-height transcript: its bottom must be reachable. /// Stable siblings, focus scopes and repaint boundaries retain chat state as /// the rectangles resize during a swap. class ChatMobilePaneViewport extends StatelessWidget { const ChatMobilePaneViewport({ super.key, required this.peek, required this.active, required this.barReveal, this.fixedChromeLayout = false, required this.child, required this.onActivate, required this.label, required this.paneId, }); final bool peek; final bool active; final bool fixedChromeLayout; /// The bar's reveal for this pane's role (see [ChatPanePresentation]). final Animation barReveal; final Widget child; final VoidCallback onActivate; final String label; final String paneId; @override Widget build(BuildContext context) => LayoutBuilder( builder: (context, constraints) => ClipRect( child: Semantics( key: ValueKey('chat-peek-$paneId'), identifier: peek ? 'chat-peek-$paneId' : null, container: peek, sortKey: peek ? const OrdinalSortKey(1) : null, label: peek ? 'Expand $label' : null, button: peek ? true : null, onTap: peek ? onActivate : null, child: ChatPeekGesture( onTap: peek ? onActivate : null, child: Column( children: [ Offstage( offstage: !peek || fixedChromeLayout, child: ConstrainedBox( constraints: BoxConstraints(maxHeight: constraints.maxHeight), child: ClipRect( child: ExcludeSemantics( child: ChatPeekHeader( title: label, onActivate: onActivate, ), ), ), ), ), Expanded( child: ExcludeFocus( excluding: peek, child: ExcludeSemantics( excluding: peek, child: RepaintBoundary( child: ChatPanePresentation( peek: peek, barReveal: barReveal, fixedChromeLayout: fixedChromeLayout, child: child, ), ), ), ), ), ], ), ), ), ), ); }