feat(app): Runs and Chat tabs badge running runs and unread chats; unread chat rows carry a New badge
This commit is contained in:
@@ -81,8 +81,26 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
|
||||
|
||||
/// The destination's icon; only Runs carries the attention count.
|
||||
Widget _icon(AppDestination destination, IconData data) {
|
||||
final icon = Icon(data);
|
||||
Widget icon = Icon(data);
|
||||
if (destination == AppDestination.chat) {
|
||||
return ActivityBadge(
|
||||
key: const ValueKey('shell.badge.chat.unread'),
|
||||
count: _bar.badge(destination, ShellBadgeKind.unread),
|
||||
label: 'unread chats',
|
||||
cupertino: widget.cupertino,
|
||||
child: icon,
|
||||
);
|
||||
}
|
||||
if (destination != AppDestination.runs) return icon;
|
||||
// Runs in progress at the bottom, runs that need you at the top.
|
||||
icon = ActivityBadge(
|
||||
key: const ValueKey('shell.badge.runs.running'),
|
||||
count: _bar.badge(destination, ShellBadgeKind.running),
|
||||
label: 'runs in progress',
|
||||
cupertino: widget.cupertino,
|
||||
bottom: true,
|
||||
child: icon,
|
||||
);
|
||||
return AttentionDestinationIcon(
|
||||
count: widget.attentionCount,
|
||||
cupertino: widget.cupertino,
|
||||
|
||||
@@ -80,3 +80,69 @@ class _CupertinoCountOverlay extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A small count on a tab icon: blue for activity (runs in progress, unread
|
||||
/// chats), so it reads apart from the red needs-you count.
|
||||
class ActivityBadge extends StatelessWidget {
|
||||
const ActivityBadge({
|
||||
super.key,
|
||||
required this.count,
|
||||
required this.label,
|
||||
required this.cupertino,
|
||||
required this.child,
|
||||
this.bottom = false,
|
||||
});
|
||||
|
||||
final int count;
|
||||
final String label;
|
||||
final bool cupertino;
|
||||
final bool bottom;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (count <= 0) return child;
|
||||
final color = cupertino
|
||||
? CupertinoColors.systemBlue.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.primary;
|
||||
final text = cupertino
|
||||
? CupertinoColors.white
|
||||
: Theme.of(context).colorScheme.onPrimary;
|
||||
return Semantics(
|
||||
label: '$count $label',
|
||||
child: ExcludeSemantics(
|
||||
child: Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
child,
|
||||
PositionedDirectional(
|
||||
top: bottom ? null : -6,
|
||||
bottom: bottom ? -6 : null,
|
||||
end: -10,
|
||||
child: DecoratedBox(
|
||||
decoration: ShapeDecoration(
|
||||
color: color,
|
||||
shape: const StadiumBorder(),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 5,
|
||||
vertical: 1,
|
||||
),
|
||||
child: Text(
|
||||
'$count',
|
||||
style: TextStyle(
|
||||
color: text,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,18 @@ class ShellBarSpec {
|
||||
/// The bar contents each tab's open screen has published.
|
||||
class ShellBarRegistry extends ChangeNotifier {
|
||||
final Map<AppDestination, (Object, ShellBarSpec)> _specs = {};
|
||||
final Map<(AppDestination, ShellBadgeKind), int> _badges = {};
|
||||
|
||||
/// The count a tab's screen published for [kind], 0 when none.
|
||||
int badge(AppDestination destination, ShellBadgeKind kind) =>
|
||||
_badges[(destination, kind)] ?? 0;
|
||||
|
||||
void _setBadge(AppDestination destination, ShellBadgeKind kind, int count) {
|
||||
if (_disposed || badge(destination, kind) == count) return;
|
||||
_badges[(destination, kind)] = count;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool _disposed = false;
|
||||
|
||||
@override
|
||||
@@ -138,3 +150,64 @@ class _ShellBarState extends State<ShellBar> {
|
||||
return widget.child;
|
||||
}
|
||||
}
|
||||
|
||||
/// What a tab badge counts.
|
||||
enum ShellBadgeKind {
|
||||
/// Runs still executing.
|
||||
running,
|
||||
|
||||
/// Chats with messages not yet seen.
|
||||
unread,
|
||||
}
|
||||
|
||||
/// Publishes a count for this tab's badge while [child] is mounted.
|
||||
class ShellBadge extends StatefulWidget {
|
||||
const ShellBadge({
|
||||
super.key,
|
||||
required this.kind,
|
||||
required this.count,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final ShellBadgeKind kind;
|
||||
final int count;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<ShellBadge> createState() => _ShellBadgeState();
|
||||
}
|
||||
|
||||
class _ShellBadgeState extends State<ShellBadge> {
|
||||
ShellBarScope? _scope;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_scope = ShellBarScope.maybeOf(context);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
final scope = _scope;
|
||||
final kind = widget.kind;
|
||||
if (scope != null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback(
|
||||
(_) => scope.registry._setBadge(scope.destination, kind, 0),
|
||||
);
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scope = _scope;
|
||||
if (scope != null) {
|
||||
final count = widget.count;
|
||||
final kind = widget.kind;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) scope.registry._setBadge(scope.destination, kind, count);
|
||||
});
|
||||
}
|
||||
return widget.child;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user