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}'),
@@ -0,0 +1,154 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
Widget _host(
ChatWorkspaceController controller, {
bool mobile = true,
bool reduced = false,
}) => MaterialApp(
home: MediaQuery(
data: MediaQueryData(disableAnimations: reduced),
child: SizedBox(
width: 560,
height: 800,
child: ChatWorkspace(
controller: controller,
singlePane: mobile,
mobilePeeks: mobile,
paneBuilder: (_, pane, active) => _Draft(key: ValueKey(pane.sessionId)),
),
),
),
);
class _Draft extends StatefulWidget {
const _Draft({super.key});
@override
State<_Draft> createState() => _DraftState();
}
class _DraftState extends State<_Draft> {
final controller = TextEditingController();
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) =>
Material(child: TextField(controller: controller));
}
Finder _rect(String pane) => find.byKey(ValueKey('chat-workspace-$pane'));
void main() {
testWidgets(
'mobile peeks animate80/20 and retain drafts and saved geometry',
(tester) async {
tester.view.physicalSize = const Size(560, 800);
tester.view.devicePixelRatio = 1;
tester.platformDispatcher.accessibilityFeaturesTestValue =
const FakeAccessibilityFeatures(disableAnimations: false);
addTearDown(
tester.platformDispatcher.clearAccessibilityFeaturesTestValue,
);
addTearDown(tester.view.reset);
final controller = ChatWorkspaceController()..open('a');
final first = controller.activePaneId!;
controller.splitActive('b', ChatSplitAxis.horizontal);
final second = controller.activePaneId!;
controller.resize(controller.root!.id, .31);
final saved = controller.snapshot()!.encode();
await tester.pumpWidget(_host(controller));
await tester.pumpAndSettle();
final aState = tester.state(find.byKey(const ValueKey('a')));
final bState = tester.state(find.byKey(const ValueKey('b')));
await tester.enterText(
find.descendant(
of: find.byKey(const ValueKey('b')),
matching: find.byType(EditableText),
),
'Draft b',
);
final oldHeight = tester.getSize(_rect(first)).height;
expect(oldHeight, closeTo((800 - 6) * .2, 1));
expect(tester.getSize(_rect(second)).height, closeTo((800 - 6) * .8, 1));
await tester.pumpAndSettle();
final positionState = tester.state(_rect(first));
await tester.tap(find.byKey(ValueKey('chat-peek-$first')));
expect(
controller.activePane?.sessionId,
'a',
reason: 'tap activates peek immediately',
);
await tester.pump();
expect(tester.state(_rect(first)), same(positionState));
await tester.pump(const Duration(milliseconds: 100));
expect(
tester.widget<AnimatedPositioned>(_rect(first)).duration,
const Duration(milliseconds: 220),
);
final intermediate = tester.getSize(_rect(first)).height;
expect(intermediate, greaterThan(oldHeight));
expect(intermediate, lessThan((800 - 6) * .8));
await tester.pumpAndSettle();
expect(controller.activePane?.sessionId, 'a');
expect(tester.getSize(_rect(first)).height, closeTo((800 - 6) * .8, 1));
expect(tester.state(find.byKey(const ValueKey('a'))), same(aState));
expect(tester.state(find.byKey(const ValueKey('b'))), same(bState));
expect((bState as _DraftState).controller.text, 'Draft b');
expect(
tester
.widget<EditableText>(
find.descendant(
of: find.byKey(const ValueKey('b')),
matching: find.byType(EditableText),
),
)
.focusNode
.hasFocus,
isFalse,
);
controller.activate(second);
await tester.pumpAndSettle();
expect(controller.snapshot()!.encode(), saved);
await tester.pumpWidget(_host(controller, mobile: false));
await tester.pumpAndSettle();
expect(tester.state(find.byKey(const ValueKey('a'))), same(aState));
expect(
tester.getSize(_rect(first)).width,
lessThan(tester.getSize(_rect(second)).width),
);
await tester.pumpWidget(const SizedBox());
controller.dispose();
},
);
testWidgets(
'three retained panes expose active+previous and honor reduced motion',
(tester) async {
tester.view.physicalSize = const Size(560, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final controller = ChatWorkspaceController()..open('a');
final first = controller.activePaneId!;
controller.splitActive('b', ChatSplitAxis.horizontal);
controller.splitActive('c', ChatSplitAxis.vertical);
await tester.pumpWidget(_host(controller, reduced: true));
await tester.pumpAndSettle();
expect(find.byType(_Draft), findsNWidgets(2));
final third = controller.activePaneId!;
controller.activate(first);
await tester.pump();
expect(tester.getSize(_rect(first)).height, closeTo((800 - 6) * .8, 1));
expect(find.byType(_Draft), findsNWidgets(2));
expect(find.byType(_Draft, skipOffstage: false), findsNWidgets(3));
controller.activate(third);
await tester.pump();
expect(tester.getSize(_rect(third)).height, closeTo((800 - 6) * .8, 1));
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
controller.dispose();
},
);
}