Files
buzz/mobile/lib/shared/widgets/adaptive_conversation_pane.dart
Tom Brow 7361326717 fix(mobile): keep glass controls mounted and the wide thread push parallax (#9726)
Fixes three regressions that #9717 (Duo chat switching) introduced.

**Glass controls blank when anything covers their route.** #9717 gated
`IosGlassNavigationButton` and `IosGlassNavigationAction` on
`useNativeRouteControlsVisible`, so every glass control (Home's filter
chips, glass Back, Close, Done) unmounted under any dialog, alert or
modal sheet and during every push, pop and back swipe, then came back as
a new platform view near the end of the transition. This drops the gate
from those two widgets, which restores their behaviour from before
#9717. The problem #9717 targets is unaffected: under Search and
Activity sheets the hook already returns true and the opaque backing
hides the controls. Home's gate, the Channels route sync and
`IosMessageCreateButton` keep the hook.

**The visibility hook rebuilt its host on every animation frame.** It
subscribed to the route's animation values but only reads whether the
covering animation is at zero and whether the route is reversing. It now
uses `useListenableSelector` on those two booleans, so a host rebuilds
only when visibility can change.

**A wide thread push lost the outgoing chat's slide.** For an immediate
wide selection, `_ConversationDetailRoute.buildTransitions` returned its
child directly, which also discarded the secondary animation that moves
the chat left when a thread is pushed over it. It now builds the
platform transition with a completed primary animation, so selection
stays immediate and the covered chat keeps its parallax.

### Regressions

- Glass controls go back to staying mounted under covering routes, as
they did before #9717. Platform-view bleed under non-sheet routes is
therefore the pre-#9717 behaviour, not new.
- No change to compact navigation or to the Search and Activity sheet
composition.

### Validation

Added tests:
-
[native_route_controls_test.dart](fb0d16ef56/mobile/test/shared/widgets/native_route_controls_test.dart):
glass controls stay mounted, with the same channels, under a dialog and
keep receiving appearance updates, while only the create control
unmounts and returns; glass controls stay mounted with no platform-view
churn across a modal bottom sheet; the `bottomUp=false` composition case
keeps the glass views; the hook rebuilds its host a small constant
number of times across a full push and pop.
-
[adaptive_conversation_pane_test.dart](fb0d16ef56/mobile/test/shared/widgets/adaptive_conversation_pane_test.dart):
in the wide layout, the covered chat has moved left 150 ms into a thread
push on iOS.

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-19 18:09:17 +00:00

684 lines
27 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:duo_kit/duo_kit.dart';
import 'duo_toolbar.dart';
import 'app_route_observer.dart';
import 'covered_route_keyboard_insets.dart';
import 'ios_navigation_metrics.dart';
import 'duo_workspace_scaffold.dart';
import '../theme/theme.dart';
/// Duo uses the phone navigation model in portrait, even on its large display.
/// Callers inside a pane supply the full window size.
bool usesConversationPanes(
BuildContext context, {
FoldStrip? strip,
Size? windowSize,
}) {
final size = windowSize ?? MediaQuery.sizeOf(context);
final rail = strip ?? FoldGeometry.of(context).strip;
// The portrait Duo status cluster becomes a tall top inset, not a side
// strip. Keep that phone layout compact without excluding ordinary iPads.
final phoneStatusCluster = MediaQuery.viewPaddingOf(context).top > 50;
if (defaultTargetPlatform == TargetPlatform.iOS &&
size.height >= size.width &&
(rail != null || phoneStatusCluster || size.shortestSide < 600)) {
return false;
}
final bookDivision = bookDivisionOf(context);
// A landscape phone is wide enough for the breakpoint, but not for two
// usable panes. Fold geometry keeps Duo eligible in its supported poses.
if (size.shortestSide < 600 && rail == null && bookDivision == null) {
return false;
}
return bookDivision != null || size.width - (rail?.width ?? 0) >= 640;
}
/// The active vertical fold, in window coordinates, when held like a book.
Rect? bookDivisionOf(BuildContext context) {
final geometry = FoldGeometry.of(context);
final division = geometry.division;
if (division != null &&
division.axis == Axis.vertical &&
division.isHalfOpened) {
return division.bounds;
}
final native = IosNavigationMetrics.of(context);
return native.windowSize == geometry.size ? native.bookDivision : null;
}
final _compactPaneNavigators = Expando<GlobalKey<NavigatorState>>();
/// Unwinds a compact destination for a shortcut, without bypassing page vetoes.
/// Returns false for other layouts or when a nested page refuses to close.
bool popCompactConversationDestination(Route<dynamic> route) {
final navigator = _compactPaneNavigators[route]?.currentState;
if (navigator == null) return false;
var cleared = false;
navigator.popUntil((nested) {
if (nested.popDisposition == RoutePopDisposition.doNotPop) return true;
cleared = nested.isFirst;
return cleared;
});
return cleared;
}
/// Selection belongs to the workspace, rather than to a particular display pose.
class ConversationPaneController extends ChangeNotifier {
final _nativeHeaderKey = GlobalKey();
String? selectedId;
WidgetBuilder? detailBuilder;
bool animateSelection = true;
int _request = 0;
// Deep-link requests may target the same item after its nested route popped.
// A fresh page identity reruns initialization without clearing the selection
// when Navigator removes the previous page.
LocalKey get selectionKey => ValueKey((selectedId, _request));
void open(
String id,
WidgetBuilder builder, {
bool animate = true,
bool reopen = false,
}) {
if (id == selectedId && !reopen) return;
if (reopen) _request++;
selectedId = id;
detailBuilder = builder;
animateSelection = animate;
notifyListeners();
}
void clear() {
if (selectedId == null) return;
selectedId = null;
detailBuilder = null;
notifyListeners();
}
}
class ConversationPaneScope
extends InheritedNotifier<ConversationPaneController> {
ConversationPaneScope({
required ConversationPaneController controller,
required Widget child,
bool active = true,
super.key,
}) : super(
notifier: controller,
child: _ConversationPaneActive(active: active, child: child),
);
/// Only the selected page owns the pane's implicit root navigation.
/// Imperatively pushed pages retain ordinary Back navigation.
static bool isBaseRouteOf(BuildContext context) {
final controller = maybeOf(context);
final settings = ModalRoute.of(context)?.settings;
return controller?.selectedId != null &&
settings is Page &&
settings.key == controller!.selectionKey;
}
/// Keeps the native header surface alive across immediate sidebar selections.
/// Conversation state still belongs to its separately keyed route.
static GlobalKey? nativeHeaderKeyOf(BuildContext context) {
final controller = maybeOf(context);
if (!isBaseRouteOf(context) || !_isWide(context)) return null;
return controller?._nativeHeaderKey;
}
/// A replaced zero-duration page can remain mounted until frame cleanup.
/// It must relinquish the shared header before the next page adopts it.
static bool isReplacedBaseRouteOf(BuildContext context) {
final settings = ModalRoute.of(context)?.settings;
return settings is _ConversationDetailPage &&
!isBaseRouteOf(context) &&
_isWide(context);
}
static bool _isWide(BuildContext context) => usesConversationPanes(
context,
windowSize:
View.of(context).physicalSize / View.of(context).devicePixelRatio,
);
/// Depends only on [active], which flips whenever a route covers the pane,
/// so readers of [maybeOf] (rows, the list) don't rebuild with it.
static bool isActiveOf(BuildContext context) =>
context
.dependOnInheritedWidgetOfExactType<_ConversationPaneActive>()
?.active ??
true;
static ConversationPaneController? maybeOf(BuildContext context) => context
.dependOnInheritedWidgetOfExactType<ConversationPaneScope>()
?.notifier;
}
class _ConversationPaneActive extends InheritedWidget {
const _ConversationPaneActive({required this.active, required super.child});
final bool active;
@override
bool updateShouldNotify(_ConversationPaneActive oldWidget) =>
active != oldWidget.active;
}
/// Keeps both the list and its detail navigator mounted across compact/regular
/// layouts, so opening or closing Duo does not recreate the composer or thread.
class AdaptiveConversationPane extends HookConsumerWidget {
const AdaptiveConversationPane({
required this.controller,
required this.list,
this.active = true,
this.strip,
this.emptyLabel = 'Select a conversation',
this.emptyContent,
this.listWidthFraction,
this.preferredListWidth,
this.persistentSelection = false,
this.listToolbarSlots = 0,
super.key,
});
final bool persistentSelection;
/// Rail slots reserved for workspace controls that stay active in wide mode.
final int listToolbarSlots;
/// Optional list share of the usable width; defaults to the compact sidebar.
final double? listWidthFraction;
/// Preferred sidebar width, capped at half the usable area for narrow windows.
final double? preferredListWidth;
final String emptyLabel;
final Widget? emptyContent;
final FoldStrip? strip;
final ConversationPaneController controller;
final Widget list;
final bool active;
@override
Widget build(BuildContext context, WidgetRef ref) {
final routeObserver = useMemoized(appRouteObserver.createNestedObserver);
final navigator = useMemoized(() => GlobalKey<NavigatorState>());
final ownerRoute = ModalRoute.of(context);
useEffect(() {
return () {
if (ownerRoute != null &&
_compactPaneNavigators[ownerRoute] == navigator) {
_compactPaneNavigators[ownerRoute] = null;
}
};
}, [ownerRoute, navigator]);
// The compact detail route that fully covers the list. While a detail
// pushes, pops or follows a back swipe, the list stays painted beneath it.
final coveringDetail = useValueNotifier<Route<Object?>?>(null);
// Compact detail routes still mounted. A pop clears the selection as it
// starts, so the detail stays onstage until its reverse transition ends.
// Not disposed with the pane: a detail unmounting with the pane reports
// after the frame, once the pane is gone.
final mountedDetails = useMemoized(() => ValueNotifier(0));
return ConversationPaneScope(
controller: controller,
active: active && (ModalRoute.isCurrentOf(context) ?? true),
child: ListenableBuilder(
listenable: controller,
builder: (context, _) => LayoutBuilder(
builder: (context, constraints) {
final stripWidth = strip?.width ?? 0;
final leftInset = strip?.side == FoldStripSide.left
? stripWidth
: 0.0;
final rightInset = strip?.side == FoldStripSide.right
? stripWidth
: 0.0;
final contentWidth = constraints.maxWidth - stripWidth;
final wide = usesConversationPanes(context, strip: strip);
final book = wide ? bookDivisionOf(context) : null;
final selected = controller.selectedId;
if (ownerRoute != null) {
_compactPaneNavigators[ownerRoute] =
active && !wide && selected != null ? navigator : null;
}
// Pane surfaces meet at the fold so their header backdrops remain
// continuous. Content insets below keep controls out of its bounds.
final listWidth = book != null
? book.center.dx - leftInset
: wide
? listWidthFraction == null
? preferredListWidth?.clamp(
0.0,
(contentWidth - 1) / 2,
) ??
(contentWidth / 3).clamp(280.0, 360.0)
: (contentWidth - 1) * listWidthFraction!
: constraints.maxWidth;
final detailLeft =
book?.center.dx ?? (wide ? listWidth + leftInset + 1 : 0.0);
return TweenAnimationBuilder<Offset>(
tween: Tween(end: Offset(listWidth, detailLeft)),
duration: wide && !MediaQuery.disableAnimationsOf(context)
? const Duration(milliseconds: 240)
: Duration.zero,
curve: Curves.easeInOutCubic,
builder: (context, geometry, _) {
final listWidth = geometry.dx;
final detailLeft = geometry.dy;
final detailWidth = constraints.maxWidth - detailLeft;
return Stack(
fit: StackFit.expand,
children: [
Positioned(
left: 0,
top: 0,
bottom: 0,
width: wide ? listWidth + leftInset : listWidth,
child: ValueListenableBuilder(
valueListenable: coveringDetail,
builder: (context, covering, list) => Offstage(
offstage:
!wide &&
selected != null &&
(covering?.isActive ?? false),
child: list,
),
child: _CoveredList(
covered: !wide && selected != null,
child: MediaQueryOverride(
transform: (media) => media.copyWith(
size: Size(listWidth, constraints.maxHeight),
),
child: DuoToolbarScope(
strip: strip,
contentInsets: EdgeInsets.only(
left: leftInset,
right: wide
? (book?.width ?? 0) / 2
: rightInset,
),
active:
active &&
(selected == null ||
(wide &&
(persistentSelection ||
listToolbarSlots > 0))) &&
(ModalRoute.isCurrentOf(context) ?? true),
child: list,
),
),
),
),
),
Positioned(
left: detailLeft,
top: 0,
bottom: 0,
width: detailWidth,
child: ValueListenableBuilder<int>(
valueListenable: mountedDetails,
builder: (context, mounted, detail) {
final shown = wide || selected != null || mounted > 0;
return Offstage(
offstage: !shown,
child: TickerMode(enabled: shown, child: detail!),
);
},
child: MediaQueryOverride(
transform: (media) => media.copyWith(
size: Size(detailWidth, constraints.maxHeight),
),
child: DuoToolbarScope(
strip: strip,
leadingSlots: wide ? listToolbarSlots : 0,
contentInsets: EdgeInsets.only(
left: wide ? (book?.width ?? 0) / 2 : leftInset,
right: rightInset,
),
active:
active &&
selected != null &&
(ModalRoute.isCurrentOf(context) ?? true),
child: Semantics(
container: true,
explicitChildNodes: true,
child: PopScope<Object?>(
canPop:
!active ||
selected == null ||
(wide && persistentSelection),
onPopInvokedWithResult: (didPop, result) {
if (!didPop &&
active &&
selected != null &&
!(wide && persistentSelection)) {
navigator.currentState!.maybePop(result);
}
},
child: ExcludeFocus(
excluding: selected == null,
child: NavigatorPopHandler<Object?>(
enabled: wide && persistentSelection,
onPopWithResult: (result) =>
navigator.currentState!.pop(result),
child: Navigator(
requestFocus: selected != null,
key: navigator,
observers: [routeObserver],
pages: [
if (!(wide &&
persistentSelection &&
selected != null))
MaterialPage<void>(
key: const ValueKey(
'conversation-placeholder',
),
// Compact panes show the list
// through this page while a
// detail pushes or pops.
child: !wide
? const SizedBox.shrink()
: Material(
color:
context.colors.surface,
child: Center(
child:
emptyContent ??
Text(
emptyLabel,
textAlign: TextAlign
.center,
style: Theme.of(context)
.textTheme
.bodyLarge
?.copyWith(
color: Theme.of(
context,
).colorScheme.onSurfaceVariant,
decoration:
TextDecoration
.none,
),
),
),
),
),
if (selected != null)
_ConversationDetailPage(
key: controller.selectionKey,
animate:
!wide &&
controller.animateSelection,
child: _CoveringDetail(
covering: coveringDetail,
mounted: mountedDetails,
builder:
controller.detailBuilder!,
),
),
],
onDidRemovePage: (page) {
if (page.key ==
controller.selectionKey) {
controller.clear();
}
},
),
),
),
),
),
),
),
),
),
if (wide)
Positioned(
left: listWidth + leftInset,
top: 0,
bottom: 0,
width: 1,
child: IgnorePointer(
child: ColoredBox(
color: context.colors.outlineVariant,
),
),
),
],
);
},
);
},
),
),
);
}
}
/// Hides the compact list from input, focus and screen readers while a
/// detail sits above it, even during a transition that still paints it.
class _CoveredList extends StatelessWidget {
const _CoveredList({required this.covered, required this.child});
final bool covered;
final Widget child;
@override
Widget build(BuildContext context) => TickerMode(
enabled: !covered,
child: IgnorePointer(
ignoring: covered,
child: ExcludeFocus(
excluding: covered,
child: ExcludeSemantics(
excluding: covered,
// A detail's keyboard must not relayout the list it covers. The
// scope keeps the detail's focus from counting as the list's own.
child: FocusScope(
child: CoveredRouteKeyboardInsets(isCovered: covered, child: child),
),
),
),
),
);
}
/// Derives [child]'s [MediaQuery] in a leaf, so the caller's build does not
/// depend on every field (keyboard insets change on each animation frame).
class MediaQueryOverride extends StatelessWidget {
const MediaQueryOverride({
required this.transform,
required this.child,
super.key,
});
final MediaQueryData Function(MediaQueryData) transform;
final Widget child;
@override
Widget build(BuildContext context) =>
MediaQuery(data: transform(MediaQuery.of(context)), child: child);
}
/// Reports its route as [covering] once the route fully covers the compact
/// list, and withdraws it while the route pushes, pops or follows a back swipe.
class _CoveringDetail extends HookWidget {
const _CoveringDetail({
required this.covering,
required this.mounted,
required this.builder,
});
final ValueNotifier<Route<Object?>?> covering;
/// Counts mounted details. A popped route unmounts its content only once
/// its reverse transition has finished.
final ValueNotifier<int> mounted;
final WidgetBuilder builder;
@override
Widget build(BuildContext context) {
final route = ModalRoute.of(context);
final navigator = Navigator.of(context);
useEffect(() {
// The count rebuilds an ancestor, which cannot rebuild while this
// mounts or unmounts.
WidgetsBinding.instance.addPostFrameCallback((_) => mounted.value++);
return () =>
WidgetsBinding.instance.addPostFrameCallback((_) => mounted.value--);
}, [mounted]);
useEffect(() {
if (route == null) return null;
void sync() {
final settled =
route.animation?.status != AnimationStatus.forward &&
route.animation?.status != AnimationStatus.reverse &&
!navigator.userGestureInProgress;
if (settled && route.isCurrent) {
covering.value = route;
} else if (covering.value == route) {
covering.value = null;
}
}
void syncStatus(AnimationStatus _) => sync();
route.animation?.addStatusListener(syncStatus);
navigator.userGestureInProgressNotifier.addListener(sync);
// An immediate route can settle before these listeners attach.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (context.mounted) sync();
});
return () {
route.animation?.removeStatusListener(syncStatus);
navigator.userGestureInProgressNotifier.removeListener(sync);
};
}, [route, navigator]);
return builder(context);
}
}
/// Keeps the route identity stable across layout changes. Sidebar selections
/// are immediate; compact navigation retains the platform push and back swipe.
class _ConversationDetailPage extends MaterialPage<void> {
const _ConversationDetailPage({
required this.animate,
required super.child,
super.key,
});
final bool animate;
@override
Route<void> createRoute(BuildContext context) =>
_ConversationDetailRoute(this);
}
class _ConversationDetailRoute extends MaterialPageRoute<void> {
_ConversationDetailRoute(_ConversationDetailPage page)
: super(builder: (_) => page.child, settings: page);
_ConversationDetailPage get _page => settings as _ConversationDetailPage;
@override
Widget buildContent(BuildContext context) => _page.child;
@override
Duration get transitionDuration =>
_page.animate ? super.transitionDuration : Duration.zero;
@override
Duration get reverseTransitionDuration =>
_page.animate ? super.reverseTransitionDuration : Duration.zero;
@override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) => super.buildTransitions(
context,
_page.animate ? animation : kAlwaysCompleteAnimation,
secondaryAnimation,
child,
);
}
/// Opens a result in its workspace's detail pane, or an ordinary pushed route.
Future<void> openConversationPane(
BuildContext context,
String selectionId,
WidgetBuilder builder, {
bool reopen = false,
}) async {
final pane = ConversationPaneScope.maybeOf(context);
if (pane != null) {
pane.open(selectionId, builder, reopen: reopen);
} else {
await Navigator.of(context).push(MaterialPageRoute<void>(builder: builder));
}
}
/// A list/detail workspace for destinations such as Search and Inbox.
class AdaptiveConversationDestination extends HookConsumerWidget {
const AdaptiveConversationDestination({
required this.child,
this.emptyLabel = 'Select a conversation',
this.listWidthFraction,
this.controller,
this.persistentSelection = false,
this.listToolbarSlots = 0,
this.preferredListWidth,
super.key,
});
/// Optional list share of the usable width; defaults to the compact sidebar.
final ConversationPaneController? controller;
final bool persistentSelection;
/// Rail slots reserved for workspace controls that stay active in wide mode.
final int listToolbarSlots;
/// Preferred sidebar width, capped to preserve room for its detail pane.
final double? preferredListWidth;
final double? listWidthFraction;
final String emptyLabel;
final Widget child;
@override
Widget build(BuildContext context, WidgetRef ref) {
final ownedController = useMemoized(ConversationPaneController.new);
useEffect(() => ownedController.dispose, [ownedController]);
final controller = this.controller ?? ownedController;
final strip = FoldGeometry.of(context).strip;
return ColoredBox(
color: context.colors.surface,
child: DuoWorkspaceScaffold(
navigationBuilder: (_, _) => const SizedBox.shrink(),
body: MediaQueryOverride(
transform: (media) => media.removePadding(
removeLeft: strip?.side == FoldStripSide.left,
removeRight: strip?.side == FoldStripSide.right,
),
child: AdaptiveConversationPane(
active: DuoToolbarScope.maybeOf(context)?.active ?? true,
controller: controller,
persistentSelection: persistentSelection,
listToolbarSlots: listToolbarSlots,
emptyLabel: emptyLabel,
listWidthFraction: listWidthFraction,
preferredListWidth: preferredListWidth,
strip: strip,
list: child,
),
),
),
);
}
}