Files
buzz/mobile/lib/shared/widgets/ios_glass_navigation_button.dart
klopez4212 d647c9fd98 Refine iOS composer and transcribe recordings into drafts (#9574)
Refine the iOS composer with 40-point glass controls, native attachment
menus, persistent emoji access, and selected-text formatting. The mic
now transcribes recordings on device into an editable, unsent draft;
Dictation has no five-minute recording cap or timer label, and its
waveform fills the space between the controls. Voice note remains
available in attachments with its five-minute cap. Unsupported devices
fail before recording, and audio is retained until the draft is durably
saved. Stopped dictation remains recoverable across editor teardown;
failed retention and cleanup can be retried. Recovered transcripts are
inserted only once across navigation or restart, preserving live exact
mention bindings.

Preserve selection handles over glass with animated avatars. Reduce the
conversation fade to 100 points above the keyboard and transition
continuously to 180 as the keyboard closes, without a first-frame jump
when opening; Home is unchanged.

Validation: 5,117 mobile tests and 27 crypto tests pass; analysis
passes. Prior native suite on iPhone 16/iOS 26.5: 119 pass, one
pre-existing expected failure, zero unexpected failures. Physical Air
preview approved; simulator selection/dragging and attachment flows
exercised. Fixed the native navigation test's timing race and a
community-cache cleanup test that checked a parent directory before its
asynchronous removal completed. Repository-wide `just ci` previously
stopped in unchanged `buzz-test-client` load-generator tests; new-head
hosted checks are pending.

Snapshots: production composer in an iPhone Air simulator fixture with
fictional conversation text; Photos snapshot retained from the
attachment preview.

| Composer | Attachments | Photos |
| --- | --- | --- |
| ![Keyboard-up
composer](https://github.com/user-attachments/assets/b7c8f0a4-25f8-4ca2-b8ae-11e18655e64a)
| ![Native attachment
menu](https://github.com/user-attachments/assets/5eaf5259-8e04-4da7-93f5-1b2eccab3c3c)
|
![Photos](https://github.com/user-attachments/assets/a6216b7d-5eaf-482c-b587-6052825b88fc)
|

---------

Signed-off-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@squareup.com>
Signed-off-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
Co-authored-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
2026-11-20 16:35:20 +00:00

384 lines
13 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 {
plus,
send,
microphone,
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.iconSize = 24,
this.fillWidth = false,
this.buttonCenterX,
this.foregroundColor,
this.backgroundColor,
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;
/// Size of the glyph inside the visual control.
final double iconSize;
/// 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 solid fill instead of the default glass surface.
final Color? backgroundColor;
/// 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 backgroundValue = backgroundColor?.toARGB32();
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,
'backgroundColor': backgroundValue ?? 0,
'enabled': enabled,
'busy': isBusy,
'selected': isSelected,
'tintsSelection': tintsSelection,
'swatchColor': ?swatchColorValue,
}),
);
}
return null;
},
[
nativeChannel.value,
brightness,
foregroundValue,
backgroundValue,
enabled,
isBusy,
isSelected,
tintsSelection,
swatchColorValue,
],
);
useEffect(() {
final channel = nativeChannel.value;
if (channel != null) {
final content = <String, Object>{
'icon': icon.name,
'iconSize': iconSize,
'accessibilityLabel': semanticLabel,
};
if (label != null) content['label'] = label!;
unawaited(channel.invokeMethod<void>('setContent', content));
}
return null;
}, [nativeChannel.value, icon, iconSize, label, semanticLabel]);
final creationParams = <String, Object>{
'icon': icon.name,
'iconSize': iconSize,
'accessibilityLabel': semanticLabel,
'accessibilityHidden': false,
'brightness': brightness,
'foregroundColor': foregroundValue,
'backgroundColor': backgroundValue ?? 0,
'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:
backgroundColor ??
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.plus => BuzzIcons.plus,
IosGlassNavigationIcon.send => BuzzIcons.arrowUp,
IosGlassNavigationIcon.microphone => BuzzIcons.mic,
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
: iconSize,
color: icon == IosGlassNavigationIcon.colorSwatch
? swatchColor
: effectiveForeground,
),
),
),
],
),
),
);
}
}