Files
pipelines/app/lib/features/attention/ui/needs_you_bell.dart
T

226 lines
7.7 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../domain/attention_models.dart';
import 'needs_you_keys.dart';
import 'needs_you_sheet.dart';
/// The reusable `Needs you` bell and its native sheet.
///
/// The widget is told what needs the operator and what each action means; it
/// never reads a socket, a store or the clock. The bell is plain when [entries]
/// is empty and carries the count badge from [attentionBadgeLabel] otherwise.
/// Opening it draws the caller's live list; row and Clear-all callbacks only ever
/// name identities from the list the operator saw.
///
/// [onOpen] is also the caller's invitation to navigate: a Failed row is
/// dismissed first, because opening it is the operator dealing with it. A
/// Decision row is not dismissed here — only answering the decision clears it.
///
/// Pass a new list when the rows change; the bell keeps the list it was given
/// and only redraws an open sheet when the new list draws different rows.
///
/// On Material the action reserves its own end room for the count badge, so it
/// can be placed directly in [AppBar.actions]. The badge paints outside the
/// icon and grows with the text scale; the inset keeps it inside the viewport
/// at narrow widths and doubled text instead of handing a clipping trap to the
/// caller.
class NeedsYouBell extends StatefulWidget {
const NeedsYouBell({
super.key,
required this.entries,
required this.onOpen,
required this.onDismiss,
required this.onClearAll,
this.cupertino,
});
final List<AttentionEntry> entries;
final ValueChanged<AttentionEntry> onOpen;
final ValueChanged<AttentionEntry> onDismiss;
final ValueChanged<List<AttentionEntry>> onClearAll;
/// Forces the native host in tests and fixtures; otherwise the ambient
/// [usesCupertinoHost] decides.
final bool? cupertino;
@override
State<NeedsYouBell> createState() => _NeedsYouBellState();
}
class _NeedsYouBellState extends State<NeedsYouBell> {
/// End room the Material action reserves for the count badge.
///
/// `Badge` places its label 12 logical px past the icon and makes the stadium
/// at least as wide as it is tall, so the label grows with the text scale: at
/// 1x it ends inside the 48 px action, at 2x it reaches 8 logical px beyond it.
/// A Material app bar puts its last action flush at the trailing edge, so a
/// flush bell would have the badge sheared by the viewport at enlarged text.
/// The action reserves the label's own scaled height minus its built-in
/// offset, leaving a 16 logical px margin at every scale and keeping the bell
/// safe to place directly in `AppBar.actions`.
static double _materialBadgeEndRoom(BuildContext context) {
final badgeLabel = MediaQuery.textScalerOf(context).scale(16);
return math.max(8, badgeLabel - 8);
}
/// The live rows the open sheet draws. It changes with the caller's state, so
/// a dismissal redraws the sheet instead of leaving a stale row behind.
late final ValueNotifier<List<AttentionEntry>> _live;
final FocusNode _focus = FocusNode(debugLabel: 'NeedsYouBell');
/// True when the sheet went away because a row was chosen, so focus belongs
/// to whatever the caller opened rather than back on the bell.
bool _openedRun = false;
static List<AttentionEntry> _frozen(List<AttentionEntry> entries) =>
List<AttentionEntry>.unmodifiable(entries);
/// True when two lists would draw the same rows: the same identities in the
/// same order with the same text. A title edit or a new diagnostic keeps the
/// identity but changes what the sheet shows, so it must count as a change.
static bool _sameRows(
List<AttentionEntry> before,
List<AttentionEntry> after,
) {
if (before.length != after.length) return false;
for (var i = 0; i < before.length; i++) {
final previous = before[i];
final next = after[i];
if (previous.key != next.key ||
previous.label != next.label ||
attentionRowDescription(previous) != attentionRowDescription(next)) {
return false;
}
}
return true;
}
@override
void initState() {
super.initState();
_live = ValueNotifier<List<AttentionEntry>>(_frozen(widget.entries));
}
@override
void didUpdateWidget(covariant NeedsYouBell oldWidget) {
super.didUpdateWidget(oldWidget);
if (_sameRows(oldWidget.entries, widget.entries)) return;
final next = _frozen(widget.entries);
// The open sheet is another overlay subtree, not a child of this build, so
// handing it the new rows mid-build would mark it dirty outside the current
// build target. The rows are handed over once the frame is painted.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) _live.value = next;
});
}
@override
void dispose() {
_live.dispose();
_focus.dispose();
super.dispose();
}
Future<void> _open() async {
_openedRun = false;
await showNeedsYouSheet(
context: context,
entries: _live,
onOpen: _activate,
onDismiss: widget.onDismiss,
onClearAll: widget.onClearAll,
cupertino: widget.cupertino,
);
if (!mounted || _openedRun) return;
// The browser returns focus to the bell after the sheet closes; do the same
// so keyboard and switch users are not dropped at the top of the page.
_focus.requestFocus();
}
void _activate(AttentionEntry entry) {
_openedRun = true;
if (entry.kind == AttentionKind.failed) widget.onDismiss(entry);
widget.onOpen(entry);
}
@override
Widget build(BuildContext context) {
final native = widget.cupertino ?? usesCupertinoHost(context);
final label = attentionBadgeLabel(widget.entries.length);
final action = native ? _cupertino(context) : _material(context, label);
final semantics = Semantics(
label: label,
button: true,
onTap: _open,
excludeSemantics: true,
child: action,
);
if (native) return semantics;
// The reserved badge room is outside the semantics box, so the focus
// rectangle stays the size of the action rather than the empty end room.
return Padding(
padding: EdgeInsetsDirectional.only(end: _materialBadgeEndRoom(context)),
child: semantics,
);
}
Widget _material(BuildContext context, String label) {
return IconButton(
key: attentionBellKey,
focusNode: _focus,
tooltip: label,
onPressed: _open,
icon: _badged(
context,
hostCupertino: false,
icon: const Icon(Icons.notifications_outlined),
),
);
}
Widget _cupertino(BuildContext context) {
final theme = CupertinoTheme.of(context);
return CupertinoButton(
key: attentionBellKey,
focusNode: _focus,
onPressed: _open,
child: _badged(
context,
hostCupertino: true,
icon: const Icon(CupertinoIcons.bell),
textStyle: theme.textTheme.tabLabelTextStyle,
),
);
}
Widget _badged(
BuildContext context, {
required bool hostCupertino,
required Widget icon,
TextStyle? textStyle,
}) {
final count = widget.entries.length;
// No native Cupertino badge exists; the framework Badge is used with the
// host's semantic red and the host's smallest text role.
if (hostCupertino && count > 0) {
return Badge.count(
key: attentionBadgeKey,
count: count,
textStyle: textStyle,
backgroundColor: CupertinoColors.systemRed.resolveFrom(context),
textColor: CupertinoColors.white,
child: icon,
);
}
if (count > 0) {
return Badge.count(key: attentionBadgeKey, count: count, child: icon);
}
return icon;
}
}