merge(app): land badges
This commit is contained in:
@@ -251,14 +251,22 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
return ShellBar(
|
||||
active: widget.activeSessionId == null,
|
||||
actions: _actions(),
|
||||
child: body,
|
||||
// The Chat tab counts chats with messages not yet seen.
|
||||
child: ShellBadge(
|
||||
kind: ShellBadgeKind.unread,
|
||||
count: _controller.sessions.where(_controller.isUnseen).length,
|
||||
child: body,
|
||||
),
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
Align(
|
||||
alignment: AlignmentDirectional.centerEnd,
|
||||
child: Row(mainAxisSize: MainAxisSize.min, children: _actions()),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: _actions(),
|
||||
),
|
||||
),
|
||||
Expanded(child: body),
|
||||
],
|
||||
|
||||
@@ -47,7 +47,9 @@ class ChatSessionRowMeta extends StatelessWidget {
|
||||
child: FittedBox(
|
||||
fit: BoxFit.scaleDown,
|
||||
alignment: AlignmentDirectional.centerEnd,
|
||||
child: Text(status!, maxLines: 1, style: statusStyle),
|
||||
child: status == 'New'
|
||||
? _UnreadBadge(style: statusStyle)
|
||||
: Text(status!, maxLines: 1, style: statusStyle),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -56,3 +58,30 @@ class ChatSessionRowMeta extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A chat with messages not yet seen: the word in a filled pill.
|
||||
class _UnreadBadge extends StatelessWidget {
|
||||
const _UnreadBadge({required this.style});
|
||||
|
||||
final TextStyle style;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = style.color ?? const Color(0xFF0A84FF);
|
||||
return DecoratedBox(
|
||||
key: const Key('chat-row-unread'),
|
||||
decoration: ShapeDecoration(color: color, shape: const StadiumBorder()),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 1),
|
||||
child: Text(
|
||||
'New',
|
||||
maxLines: 1,
|
||||
style: style.copyWith(
|
||||
color: const Color(0xFFFFFFFF),
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,16 @@ class RunsList extends StatelessWidget {
|
||||
],
|
||||
);
|
||||
}
|
||||
return ShellBar(active: active, actions: actions, child: _list(context));
|
||||
return ShellBar(
|
||||
active: active,
|
||||
actions: actions,
|
||||
// The Runs tab counts runs still executing.
|
||||
child: ShellBadge(
|
||||
kind: ShellBadgeKind.running,
|
||||
count: controller.runs.where((run) => run['executing'] == true).length,
|
||||
child: _list(context),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _list(BuildContext context) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,4 +60,43 @@ void main() {
|
||||
expect(find.byKey(const ValueKey('shell.search')), findsNothing);
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('tabs show the running and unread counts their screens publish', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(390, 844);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: AdaptiveShell(
|
||||
destination: AppDestination.runs,
|
||||
destinationBodies: {
|
||||
AppDestination.runs: (_) => const ShellBadge(
|
||||
kind: ShellBadgeKind.running,
|
||||
count: 2,
|
||||
child: Text('runs'),
|
||||
),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
final running = find.byKey(const ValueKey('shell.badge.runs.running'));
|
||||
expect(
|
||||
find.descendant(of: running, matching: find.text('2')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.byKey(const ValueKey('shell.badge.chat.unread')),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byKey(const ValueKey('shell.badge.chat.unread')),
|
||||
matching: find.byType(Text),
|
||||
),
|
||||
findsNothing,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user