Files
buzz/mobile/lib/shared/widgets/ios_glass_navigation_button.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

362 lines
12 KiB
Dart

import 'dart:async';
import 'package:buzz/shared/theme/buzz_icons.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart' show PlatformViewHitTestBehavior;
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import '../theme/theme.dart';
/// The navigation glyph displayed by [IosGlassNavigationButton].
enum IosGlassNavigationIcon {
back,
close,
check,
camera,
photoLibrary,
palette,
droplet,
emoji,
person,
frame,
rotateCamera,
shutter,
colorSwatch,
sun,
moon,
systemAppearance,
more,
reply,
trash,
bell,
}
/// Leading width used by iOS channel-style headers.
const iosGlassChannelHeaderLeadingWidth = 58.0;
/// Horizontal center of the native button inside a channel-style leading.
const iosGlassChannelHeaderButtonCenterX = 38.0;
/// Space between the leading region and a channel-style title.
const iosGlassChannelHeaderTitleSpacing = Grid.xs;
/// A native iOS navigation control using the system glass button treatment.
///
/// Callers should only insert this widget on iOS and retain their existing
/// Flutter control on other platforms.
class IosGlassNavigationButton extends HookWidget {
const IosGlassNavigationButton({
super.key,
required this.icon,
required this.semanticLabel,
required this.onPressed,
this.label,
this.width = 48,
this.height = 48,
this.controlSize = 40,
this.fillWidth = false,
this.buttonCenterX,
this.foregroundColor,
this.swatchColor,
this.isBusy = false,
this.isSelected = false,
this.tintsSelection = true,
this.nativeViewSuppressed,
this.artwork,
});
static const viewType = 'buzz/navigation_glass';
/// The SF Symbol-style glyph rendered by the native control.
final IosGlassNavigationIcon icon;
/// Optional text rendered by the native control instead of [icon].
final String? label;
/// Accessibility label exposed by the native view or Flutter fallback.
final String semanticLabel;
/// Invoked when the enabled control is activated.
final VoidCallback? onPressed;
/// Width of the platform-view hit target.
final double width;
/// Height of the platform-view hit target.
final double height;
/// Diameter of the visual glass control inside its hit target.
final double controlSize;
/// Whether the native visual control fills the available width.
final bool fillWidth;
/// Horizontal center for the visual control within its hit target.
final double? buttonCenterX;
/// Optional foreground tint for the native control and Flutter fallback.
final Color? foregroundColor;
/// Optional inset color swatch used by [IosGlassNavigationIcon.colorSwatch].
final Color? swatchColor;
/// Whether the native control presents its busy state.
final bool isBusy;
/// Whether the native control exposes its selected state.
final bool isSelected;
/// Whether selection changes the glass fill as well as accessibility state.
final bool tintsSelection;
/// When true, substitutes an accessible Flutter control for the native view.
final ValueListenable<bool>? nativeViewSuppressed;
/// Flutter artwork over the native glass control; UIKit still handles touch.
final Widget? artwork;
@override
Widget build(BuildContext context) {
assert(defaultTargetPlatform == TargetPlatform.iOS);
final native = HookBuilder(builder: _buildNative);
return Tooltip(
message: semanticLabel,
excludeFromSemantics: true,
child: SizedBox(
width: width,
height: height,
child: nativeViewSuppressed == null
? native
: ValueListenableBuilder<bool>(
valueListenable: nativeViewSuppressed!,
builder: (context, suppressNativeView, _) =>
suppressNativeView ? _buildFallback(context) : native,
),
),
);
}
Widget _buildNative(BuildContext context) {
final nativeChannel = useState<MethodChannel?>(null);
final highlighted = useState(false);
final onPressedRef = useRef(onPressed)..value = onPressed;
final brightness = context.theme.brightness.name;
final effectiveForeground = foregroundColor ?? context.colors.primary;
final foregroundValue = effectiveForeground.toARGB32();
final swatchColorValue = swatchColor?.toARGB32();
final enabled = onPressed != null;
useEffect(() {
final channel = nativeChannel.value;
if (channel == null) return null;
channel.setMethodCallHandler((call) async {
if (call.method == 'highlighted') {
highlighted.value = call.arguments == true;
}
if (call.method == 'pressed') {
onPressedRef.value?.call();
}
});
return () => channel.setMethodCallHandler(null);
}, [nativeChannel.value]);
useEffect(
() {
final channel = nativeChannel.value;
if (channel != null) {
unawaited(
channel.invokeMethod<void>('setAppearance', <String, Object>{
'brightness': brightness,
'foregroundColor': foregroundValue,
'enabled': enabled,
'busy': isBusy,
'selected': isSelected,
'tintsSelection': tintsSelection,
'swatchColor': ?swatchColorValue,
}),
);
}
return null;
},
[
nativeChannel.value,
brightness,
foregroundValue,
enabled,
isBusy,
isSelected,
tintsSelection,
swatchColorValue,
],
);
useEffect(() {
final channel = nativeChannel.value;
if (channel != null) {
final content = <String, Object>{
'icon': icon.name,
'accessibilityLabel': semanticLabel,
};
if (label != null) content['label'] = label!;
unawaited(channel.invokeMethod<void>('setContent', content));
}
return null;
}, [nativeChannel.value, icon, label, semanticLabel]);
final creationParams = <String, Object>{
'icon': icon.name,
'accessibilityLabel': semanticLabel,
'accessibilityHidden': false,
'brightness': brightness,
'foregroundColor': foregroundValue,
'enabled': enabled,
'busy': isBusy,
'selected': isSelected,
'tintsSelection': tintsSelection,
'controlSize': controlSize,
'controlWidth': controlSize,
'fillWidth': fillWidth,
'buttonCenterX': buttonCenterX ?? width / 2,
'hitTargetWidth': width,
'hitTargetHeight': height,
'swatchColor': ?swatchColorValue,
};
if (label != null) creationParams['label'] = label!;
final nativeControl = UiKitView(
viewType: viewType,
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onPlatformViewCreated: (viewId) {
nativeChannel.value = MethodChannel('$viewType/$viewId');
},
);
return Stack(
alignment: Alignment.center,
children: [
Positioned.fill(child: nativeControl),
if (artwork != null)
IgnorePointer(
child: ExcludeSemantics(
child: AnimatedScale(
scale: highlighted.value && enabled ? 0.94 : 1,
duration: MediaQuery.disableAnimationsOf(context)
? Duration.zero
: const Duration(milliseconds: 100),
child: SizedBox.square(
dimension: controlSize - 8,
child: artwork,
),
),
),
),
],
);
}
Widget _buildFallback(BuildContext context) {
final effectiveForeground = foregroundColor ?? context.colors.primary;
final enabled = onPressed != null;
final resolvedButtonCenterX = buttonCenterX ?? width / 2;
return Semantics(
container: true,
button: true,
enabled: enabled,
selected: isSelected,
label: semanticLabel,
onTap: onPressed,
child: ExcludeSemantics(
child: Stack(
key: const ValueKey('ios-glass-navigation-flutter-fallback'),
children: [
Positioned(
left: resolvedButtonCenterX - controlSize / 2,
top: (height - controlSize) / 2,
width: controlSize,
height: controlSize,
child: DecoratedBox(
decoration: BoxDecoration(
color: context.colors.surface.withValues(alpha: 0.72),
shape: BoxShape.circle,
border: Border.all(
color: context.colors.inverseSurface.withValues(
alpha: 0.07,
),
),
),
child: isBusy
? Center(
child: SizedBox.square(
dimension: 22,
child: CircularProgressIndicator(
strokeWidth: 2,
color: effectiveForeground,
),
),
)
: icon == IosGlassNavigationIcon.colorSwatch
? Padding(
padding: const EdgeInsets.all(4),
child: DecoratedBox(
decoration: BoxDecoration(
color: swatchColor,
shape: BoxShape.circle,
),
),
)
: label != null
? Text(
label!,
maxLines: 1,
style: context.textTheme.labelMedium?.copyWith(
color: effectiveForeground,
fontWeight: FontWeight.w600,
),
)
: Icon(
switch (icon) {
IosGlassNavigationIcon.more => BuzzIcons.ellipsis,
IosGlassNavigationIcon.bell => BuzzIcons.bell,
IosGlassNavigationIcon.trash => BuzzIcons.trash2,
IosGlassNavigationIcon.reply =>
BuzzIcons.cornerUpLeft,
IosGlassNavigationIcon.back => BuzzIcons.chevronLeft,
IosGlassNavigationIcon.close => BuzzIcons.x,
IosGlassNavigationIcon.check => BuzzIcons.check,
IosGlassNavigationIcon.camera => BuzzIcons.camera,
IosGlassNavigationIcon.photoLibrary =>
BuzzIcons.images,
IosGlassNavigationIcon.palette => BuzzIcons.palette,
IosGlassNavigationIcon.droplet => BuzzIcons.droplet,
IosGlassNavigationIcon.emoji => BuzzIcons.smile,
IosGlassNavigationIcon.person => BuzzIcons.user,
IosGlassNavigationIcon.frame => BuzzIcons.square,
IosGlassNavigationIcon.rotateCamera =>
BuzzIcons.switchCamera,
IosGlassNavigationIcon.shutter => BuzzIcons.circle,
IosGlassNavigationIcon.colorSwatch =>
BuzzIcons.circle,
IosGlassNavigationIcon.sun => BuzzIcons.sun,
IosGlassNavigationIcon.moon => BuzzIcons.moon,
IosGlassNavigationIcon.systemAppearance =>
BuzzIcons.sunMoon,
},
size: icon == IosGlassNavigationIcon.shutter
? controlSize * 0.72
: 24,
color: icon == IosGlassNavigationIcon.colorSwatch
? swatchColor
: effectiveForeground,
),
),
),
],
),
),
);
}
}