Animate focused mobile chat panes while preserving saved geometry

This commit is contained in:
2026-11-06 21:46:45 +00:00
parent 9820efd3d3
commit 6bba6cf8d8
3 changed files with 326 additions and 25 deletions
@@ -0,0 +1,100 @@
import 'dart:math' as math;
import 'package:flutter/widgets.dart';
import 'chat_workspace_controller.dart';
/// A phone presentation only: saved split axes and ratios are left untouched.
class ChatMobilePanes {
ChatMobilePanes({
required List<ChatWorkspacePane> panes,
required String activeId,
required String? peekId,
required Size size,
}) {
final active = panes.firstWhere((pane) => pane.id == activeId);
final peek =
panes
.where((pane) => pane.id == peekId && pane.id != activeId)
.firstOrNull ??
panes.where((pane) => pane.id != activeId).firstOrNull;
if (peek == null) {
rects[active.id] = Offset.zero & size;
return;
}
this.peekId = peek.id;
const gap = 6.0;
final available = math.max(0.0, size.height - gap);
final peekHeight = available * .2;
final activeHeight = available - peekHeight;
final activeFirst = panes.indexOf(active) < panes.indexOf(peek);
rects[activeFirst ? active.id : peek.id] = Rect.fromLTWH(
0,
0,
size.width,
activeFirst ? activeHeight : peekHeight,
);
final topHeight = activeFirst ? activeHeight : peekHeight;
rects[activeFirst ? peek.id : active.id] = Rect.fromLTWH(
0,
topHeight + gap,
size.width,
activeFirst ? peekHeight : activeHeight,
);
}
final rects = <String, Rect>{};
String? peekId;
}
/// Keep a peek's full header/transcript layout available without squeezing its
/// composer into a tiny viewport. The clipped preview cannot take input focus.
class ChatMobilePaneViewport extends StatelessWidget {
const ChatMobilePaneViewport({
super.key,
required this.peek,
required this.child,
required this.height,
required this.onActivate,
required this.label,
required this.paneId,
});
final bool peek;
final double height;
final Widget child;
final VoidCallback onActivate;
final String label;
final String paneId;
@override
Widget build(BuildContext context) => ClipRect(
child: Stack(
fit: StackFit.expand,
children: [
OverflowBox(
alignment: Alignment.topCenter,
minHeight: height,
maxHeight: peek ? math.max(height, 300) : height,
child: SizedBox(
height: peek ? math.max(height, 300) : height,
child: ExcludeFocus(
excluding: peek,
child: ExcludeSemantics(
excluding: peek,
child: IgnorePointer(ignoring: peek, child: child),
),
),
),
),
if (peek)
Semantics(
key: ValueKey('chat-peek-$paneId'),
identifier: 'chat-peek-$paneId',
container: true,
label: 'Expand $label',
button: true,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onActivate,
),
),
],
),
);
}
@@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
import 'chat_workspace_controller.dart';
import 'chat_workspace_divider.dart';
import 'chat_workspace_geometry.dart';
import 'chat_mobile_panes.dart';
export 'chat_workspace_controller.dart';
export 'chat_workspace_geometry.dart' show ChatPaneDirection;
@@ -22,6 +23,8 @@ class ChatWorkspace extends StatefulWidget {
this.onFocusPane,
this.shortcutsEnabled = true,
this.singlePane = false,
this.mobilePeeks = false,
this.paneTitle,
});
final ChatWorkspaceController controller;
final ChatWorkspacePaneBuilder paneBuilder;
@@ -29,6 +32,8 @@ class ChatWorkspace extends StatefulWidget {
/// Keeps the retained layout while presenting only the active chat.
final bool singlePane;
final bool mobilePeeks;
final String Function(ChatWorkspacePane)? paneTitle;
/// Keyboard navigation only: the owner can focus the target composer.
/// Pointer activation leaves text-selection and pointer focus alone.
@@ -42,6 +47,8 @@ class _ChatWorkspaceState extends State<ChatWorkspace> {
final _focus = FocusNode(debugLabel: 'Chat workspace');
final Map<String, FocusScopeNode> _panes = {};
ChatWorkspaceGeometry? _geometry;
String? _lastActiveId;
String? _peekId;
@override
void initState() {
@@ -115,6 +122,23 @@ class _ChatWorkspaceState extends State<ChatWorkspace> {
_geometry = geometry;
final collapsed = widget.singlePane || geometry.collapsed;
final panes = widget.controller.panes;
final activeId = widget.controller.activePaneId;
if (_lastActiveId != activeId) {
_peekId = _lastActiveId;
_lastActiveId = activeId;
}
final mobile = widget.mobilePeeks && activeId != null
? ChatMobilePanes(
panes: panes,
activeId: activeId,
peekId: _peekId,
size: constraints.biggest,
)
: null;
final duration =
mobile != null && !MediaQuery.disableAnimationsOf(context)
? const Duration(milliseconds: 220)
: Duration.zero;
final existing = panes.map((p) => p.id).toSet();
for (final id
in _panes.keys.where((id) => !existing.contains(id)).toList()) {
@@ -126,37 +150,60 @@ class _ChatWorkspaceState extends State<ChatWorkspace> {
clipBehavior: Clip.hardEdge,
children: [
for (final pane in panes)
Positioned.fromRect(
AnimatedPositioned.fromRect(
duration: duration,
curve: Curves.easeInOutCubic,
key: ValueKey('chat-workspace-${pane.id}'),
rect: collapsed
? Offset.zero & constraints.biggest
: geometry.panes[pane.id]!,
rect:
mobile?.rects[pane.id] ??
(collapsed
? Offset.zero & constraints.biggest
: geometry.panes[pane.id]!),
child: Offstage(
offstage:
collapsed && pane.id != widget.controller.activePaneId,
offstage: mobile != null
? !mobile.rects.containsKey(pane.id)
: collapsed &&
pane.id != widget.controller.activePaneId,
child: ExcludeFocus(
excluding:
collapsed &&
pane.id != widget.controller.activePaneId,
child: FocusScope(
node: _panes.putIfAbsent(
pane.id,
() => FocusScopeNode(debugLabel: pane.id),
),
onFocusChange: (focused) {
if (focused) widget.controller.activate(pane.id);
child: ChatMobilePaneViewport(
peek: mobile?.peekId == pane.id,
label: widget.paneTitle?.call(pane) ?? pane.sessionId,
paneId: pane.id,
height:
(mobile?.rects[pane.id] ??
(collapsed
? Offset.zero & constraints.biggest
: geometry.panes[pane.id]!))
.height,
onActivate: () {
FocusManager.instance.primaryFocus?.unfocus();
widget.controller.activate(pane.id);
},
child: Listener(
behavior: HitTestBehavior.translucent,
onPointerDown: (_) =>
widget.controller.activate(pane.id),
child: Semantics(
identifier: 'chat-pane-${pane.id}',
container: true,
child: widget.paneBuilder(
context,
pane,
pane.id == widget.controller.activePaneId,
child: FocusScope(
node: _panes.putIfAbsent(
pane.id,
() => FocusScopeNode(debugLabel: pane.id),
),
onFocusChange: (focused) {
if (focused && !widget.mobilePeeks) {
widget.controller.activate(pane.id);
}
},
child: Listener(
behavior: HitTestBehavior.translucent,
onPointerDown: (_) =>
widget.controller.activate(pane.id),
child: Semantics(
identifier: 'chat-pane-${pane.id}',
container: true,
child: widget.paneBuilder(
context,
pane,
pane.id == widget.controller.activePaneId,
),
),
),
),
@@ -164,7 +211,7 @@ class _ChatWorkspaceState extends State<ChatWorkspace> {
),
),
),
if (!collapsed)
if (!collapsed && mobile == null)
for (final divider in geometry.dividers)
Positioned.fromRect(
key: ValueKey('chat-workspace-${divider.split.id}'),